Skip to content

Commit 123538d

Browse files
authored
[KERNEL][OPENCL] Add hard sigmoid opencl kernel. test=develop (#4177)
* [OPENCL] Add hard sigmoid opencl kernel. test=develop * optimize hard sigmoid. test=develop
1 parent 9bf15fe commit 123538d

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

lite/api/paddle_place.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ const std::string& ActivationTypeToStr(ActivationType act) {
5555
"Tanh",
5656
"Swish",
5757
"Exp",
58+
"Abs",
59+
"HardSwish",
60+
"Reciprocal",
5861
"ThresholdedRelu",
59-
"Elu"};
62+
"Elu",
63+
"HardSigmoid"};
6064
auto x = static_cast<int>(act);
6165
CHECK_LT(x, static_cast<int>(ActivationType::NUM));
6266
return act2string[x];

lite/api/paddle_place.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ enum class ActivationType : int {
109109
kReciprocal = 11,
110110
kThresholdedRelu = 12,
111111
kElu = 13,
112-
NUM = 14,
112+
kHardSigmoid = 14,
113+
NUM = 15,
113114
};
114115

115116
static size_t PrecisionTypeLength(PrecisionType type) {

lite/backends/opencl/cl_kernel/image/activation_kernel.cl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ __kernel void sigmoid(__read_only image2d_t input,
6666
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), out);
6767
}
6868

69+
__kernel void hard_sigmoid(__read_only image2d_t input,
70+
__write_only image2d_t output,
71+
__private const float value_offset,
72+
__private const float scale) {
73+
const int x = get_global_id(0); // image_width
74+
const int y = get_global_id(1); // image_height
75+
76+
const sampler_t sampler =
77+
CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
78+
79+
CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y));
80+
CL_DTYPE4 out = clamp(in * scale + value_offset, 0.0, 1.0);
81+
82+
WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), out);
83+
}
84+
6985
__kernel void leaky_relu(__read_only image2d_t input,
7086
__write_only image2d_t output,
7187
__private const float threshold,

lite/kernels/opencl/activation_image_compute.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ class ActivationComputeImageDefault
7272
case 8:
7373
kernel_func_name_ = "exp_act";
7474
break;
75-
default:
75+
case 14:
76+
kernel_func_name_ = "hard_sigmoid";
77+
scale_ = act_param_->hard_sigmoid_slope;
78+
threshold_ = act_param_->hard_sigmoid_offset;
79+
break;
80+
defauln:
7681
LOG(FATAL) << "This act type:" << act_type << " doesn't support.";
7782
return;
7883
}
@@ -316,3 +321,21 @@ REGISTER_LITE_KERNEL(
316321
PRECISION(kFP16),
317322
DATALAYOUT(kImageDefault))})
318323
.Finalize();
324+
325+
// Hard Sigmoid
326+
REGISTER_LITE_KERNEL(
327+
hard_sigmoid,
328+
kOpenCL,
329+
kFP16,
330+
kImageDefault,
331+
paddle::lite::kernels::opencl::ActivationComputeImageDefault,
332+
ImageDefault)
333+
.BindInput("X",
334+
{LiteType::GetTensorTy(TARGET(kOpenCL),
335+
PRECISION(kFP16),
336+
DATALAYOUT(kImageDefault))})
337+
.BindOutput("Out",
338+
{LiteType::GetTensorTy(TARGET(kOpenCL),
339+
PRECISION(kFP16),
340+
DATALAYOUT(kImageDefault))})
341+
.Finalize();

lite/kernels/opencl/activation_image_compute_test.cc

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,22 @@ void act_compute_ref(const dtype *x_data,
5757
case 8: // exp
5858
out_data[i] = expf(x_data[i]);
5959
break;
60+
case 14: // hard sigmoid
61+
// scale ==> slope
62+
{
63+
float tmp = x_data[i] * scale + threshold;
64+
tmp = tmp < 1.0f ? tmp : 1.0f;
65+
tmp = tmp > 0.0f ? tmp : 0.0f;
66+
out_data[i] = tmp;
67+
break;
68+
}
6069
default:
6170
break;
6271
}
6372
}
6473
}
6574

66-
// #define ACT_FP16_LOOP_TEST
75+
// #define ACT_FP16_LOOP_TEST
6776
// #define ACT_FP16_PRINT_RESULT
6877
TEST(act_image2d_fp16, compute) {
6978
LOG(INFO) << "main steps of test: host -> layout(buf2img) -> relu(img) -> "
@@ -75,17 +84,17 @@ TEST(act_image2d_fp16, compute) {
7584
for (auto c : {1, 3, 8, 23, 32}) {
7685
for (int h = 12; h <= 100; h += 13) {
7786
for (int w = 12; w <= 100; w += 25) {
78-
for (auto act_type : {1, 2, 4, 5, 6, 7, 8}) {
87+
for (auto act_type : {1, 2, 4, 5, 6, 7, 8, 14}) {
7988
for (auto scale : {0.5, 0.8}) {
8089
for (auto threshold : {6.0}) {
8190
#else
8291
const int n = 1;
8392
const int c = 2;
8493
const int h = 3;
8594
const int w = 4;
86-
const int act_type = 4;
87-
const float scale = 0.5f;
88-
const float threshold = 6.f;
95+
const int act_type = 14;
96+
const float scale = 2.0f;
97+
const float threshold = 1.0f;
8998

9099
#endif // ACT_FP16_LOOP_TEST
91100

@@ -117,6 +126,9 @@ TEST(act_image2d_fp16, compute) {
117126
case 8: // tanh
118127
func_name = "exp";
119128
break;
129+
case 14: // hard sigmoid
130+
func_name = "hard_sigmoid";
131+
break;
120132
}
121133
LOG(INFO) << "func_name: " << func_name;
122134
// set layout kernels
@@ -166,6 +178,9 @@ TEST(act_image2d_fp16, compute) {
166178
actParam.Relu_clipped_coef = threshold;
167179
actParam.Leaky_relu_alpha = scale;
168180
actParam.Swish_beta = scale;
181+
// hard sigmoid
182+
actParam.hard_sigmoid_slope = scale;
183+
actParam.hard_sigmoid_offset = threshold;
169184

170185
const DDim x_dim =
171186
DDim(std::vector<DDim::value_type>{n, c, h, w});
@@ -191,7 +206,8 @@ TEST(act_image2d_fp16, compute) {
191206
std::default_random_engine engine;
192207
std::uniform_real_distribution<float> dist(-1, 1);
193208
for (int i = 0; i < x_dim.production(); ++i) {
194-
mapped_x[i] = dist(engine);
209+
mapped_x[i] =
210+
(i - x_dim.production() / 2) / 10.; // dist(engine);
195211
mapped_y[i] = 0.0f;
196212
}
197213
auto *act_in_data = act_in.mutable_data<half_t, cl::Image2D>(
@@ -316,3 +332,6 @@ USE_LITE_KERNEL(relu6, kOpenCL, kFP16, kImageDefault, ImageDefault);
316332

317333
// sigmoid image2d fp16
318334
USE_LITE_KERNEL(sigmoid, kOpenCL, kFP16, kImageDefault, ImageDefault);
335+
336+
// hard_sigmoid image2d fp16
337+
USE_LITE_KERNEL(hard_sigmoid, kOpenCL, kFP16, kImageDefault, ImageDefault);

lite/operators/activation_ops.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ bool ActivationOp::AttachImpl(const cpp::OpDesc& opdesc, lite::Scope* scope) {
6060
param_.active_type = lite_api::ActivationType::kSwish;
6161
} else if (opdesc.Type() == "hard_sigmoid") {
6262
// hard_sigomid
63+
param_.active_type = lite_api::ActivationType::kHardSigmoid;
6364
param_.hard_sigmoid_slope = opdesc.GetAttr<float>("slope");
6465
param_.hard_sigmoid_offset = opdesc.GetAttr<float>("offset");
6566
} else if (opdesc.Type() == "sigmoid") {

0 commit comments

Comments
 (0)