Skip to content

[OPENCL] add opencl kernel for sin cos tan atan asin acos #4799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions lite/backends/opencl/cl_kernel/image/trigonometric_kernel.cl
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,83 @@ __kernel void trigonometric_sin(__read_only image2d_t input,
in.w = native_sin(in.w);
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in);
}

__kernel void trigonometric_cos(__read_only image2d_t input,
__write_only image2d_t output) {
const int x = get_global_id(0); // image_width
const int y = get_global_id(1); // image_height

const sampler_t sampler =
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y));
in.x = native_cos(in.x);
in.y = native_cos(in.y);
in.z = native_cos(in.z);
in.w = native_cos(in.w);
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in);
}

__kernel void trigonometric_tan(__read_only image2d_t input,
__write_only image2d_t output) {
const int x = get_global_id(0); // image_width
const int y = get_global_id(1); // image_height

const sampler_t sampler =
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y));
in.x = native_tan(in.x);
in.y = native_tan(in.y);
in.z = native_tan(in.z);
in.w = native_tan(in.w);
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in);
}

__kernel void trigonometric_atan(__read_only image2d_t input,
__write_only image2d_t output) {
const int x = get_global_id(0); // image_width
const int y = get_global_id(1); // image_height

const sampler_t sampler =
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y));
in.x = atan(in.x);
in.y = atan(in.y);
in.z = atan(in.z);
in.w = atan(in.w);
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in);
}

__kernel void trigonometric_asin(__read_only image2d_t input,
__write_only image2d_t output) {
const int x = get_global_id(0); // image_width
const int y = get_global_id(1); // image_height

const sampler_t sampler =
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y));
in.x = asin(in.x);
in.y = asin(in.y);
in.z = asin(in.z);
in.w = asin(in.w);
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in);
}

__kernel void trigonometric_acos(__read_only image2d_t input,
__write_only image2d_t output) {
const int x = get_global_id(0); // image_width
const int y = get_global_id(1); // image_height

const sampler_t sampler =
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y));
in.x = acos(in.x);
in.y = acos(in.y);
in.z = acos(in.z);
in.w = acos(in.w);
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in);
}
2 changes: 1 addition & 1 deletion lite/kernels/opencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ lite_cc_test(test_pad2d_image_opencl SRCS pad2d_image_compute_test.cc
lite_cc_test(test_box_coder_image_opencl SRCS box_coder_image_compute_test.cc
DEPS box_coder_opencl_image op_registry program context)

lite_cc_test(test_sin_image_opencl SRCS sin_image_compute_test.cc
lite_cc_test(test_trigonometric_image_opencl SRCS trigonometric_image_compute_test.cc
DEPS trigonometric_opencl_image op_registry program context)

######################
Expand Down
122 changes: 0 additions & 122 deletions lite/kernels/opencl/sin_image_compute_test.cc

This file was deleted.

100 changes: 100 additions & 0 deletions lite/kernels/opencl/trigonometric_image_compute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ class TrigonometricComputeImage2D
class SinComputeImage2D : public TrigonometricComputeImage2D {
std::string KernelFunctionName() override { return "trigonometric_sin"; }
};

class CosComputeImage2D : public TrigonometricComputeImage2D {
std::string KernelFunctionName() override { return "trigonometric_cos"; }
};

class TanComputeImage2D : public TrigonometricComputeImage2D {
std::string KernelFunctionName() override { return "trigonometric_tan"; }
};

class AtanComputeImage2D : public TrigonometricComputeImage2D {
std::string KernelFunctionName() override { return "trigonometric_atan"; }
};

class AsinComputeImage2D : public TrigonometricComputeImage2D {
std::string KernelFunctionName() override { return "trigonometric_asin"; }
};

class AcosComputeImage2D : public TrigonometricComputeImage2D {
std::string KernelFunctionName() override { return "trigonometric_acos"; }
};
} // namespace opencl
} // namespace kernels
} // namespace lite
Expand All @@ -156,3 +176,83 @@ REGISTER_LITE_KERNEL(sin,
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.Finalize();

REGISTER_LITE_KERNEL(cos,
kOpenCL,
kFP16,
kImageDefault,
paddle::lite::kernels::opencl::CosComputeImage2D,
image2d)
.BindInput("X",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.Finalize();

REGISTER_LITE_KERNEL(tan,
kOpenCL,
kFP16,
kImageDefault,
paddle::lite::kernels::opencl::TanComputeImage2D,
image2d)
.BindInput("X",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.Finalize();

REGISTER_LITE_KERNEL(atan,
kOpenCL,
kFP16,
kImageDefault,
paddle::lite::kernels::opencl::AtanComputeImage2D,
image2d)
.BindInput("X",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.Finalize();

REGISTER_LITE_KERNEL(asin,
kOpenCL,
kFP16,
kImageDefault,
paddle::lite::kernels::opencl::AsinComputeImage2D,
image2d)
.BindInput("X",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.Finalize();

REGISTER_LITE_KERNEL(acos,
kOpenCL,
kFP16,
kImageDefault,
paddle::lite::kernels::opencl::AcosComputeImage2D,
image2d)
.BindInput("X",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kOpenCL),
PRECISION(kFP16),
DATALAYOUT(kImageDefault))})
.Finalize();
Loading