Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions lite/backends/opencl/cl_kernel/cl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ inline CL_DTYPE activation(CL_DTYPE in, CL_DTYPE prelu_alpha) {
(CL_DTYPE)1.0);
#endif

#ifdef SIGMOID
output = (CL_DTYPE)(1.0f / (1.0f + pow(2.71828182f, -1.0f * (float)(in))));
#endif

return output;
}

Expand Down Expand Up @@ -174,6 +178,17 @@ inline CL_COMPUTE_DTYPE4 activation_type4(CL_COMPUTE_DTYPE4 in,
(CL_COMPUTE_DTYPE4)1.0);
#endif

#ifdef SIGMOID
output.x =
(CL_DTYPE)(1.0f / (1.0f + pow(2.71828182f, -1.0f * (float)(in.x))));
output.y =
(CL_DTYPE)(1.0f / (1.0f + pow(2.71828182f, -1.0f * (float)(in.y))));
output.z =
(CL_DTYPE)(1.0f / (1.0f + pow(2.71828182f, -1.0f * (float)(in.z))));
output.w =
(CL_DTYPE)(1.0f / (1.0f + pow(2.71828182f, -1.0f * (float)(in.w))));
#endif

return output;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void ConvActivationFusePass::Apply(const std::unique_ptr<SSAGraph>& graph) {
act_types.push_back("hard_swish");
act_types.push_back("hard_sigmoid");
act_types.push_back("prelu");
act_types.push_back("sigmoid");
}

if (!has_int8 && has_cuda) {
Expand Down
3 changes: 3 additions & 0 deletions lite/core/optimizer/mir/fusion/conv_activation_fuser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ cpp::OpDesc ConvActivationFuser::GenOpDesc(const key2nodes_t& matched) {
auto prelu_mode = act_op_desc.GetAttr<std::string>("mode");
op_desc.SetAttr("prelu_mode", prelu_mode);
op_desc.SetInput("Prelu_alpha", {matched.at("alpha")->arg()->name});
} else if (act_type_ == "sigmoid") {
op_desc.SetAttr("fuse_sigmoid", true);
}

return op_desc;
}

Expand Down
7 changes: 7 additions & 0 deletions lite/kernels/opencl/conv_image_compute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void ConvImageCompute::PrepareForRun() {
groups_ = conv_param_->groups;
fuse_eltwise_op_type_ = conv_param_->fuse_elementwise_op_type;
relu_fused_ = conv_param_->fuse_relu;
sigmoid_fused_ = conv_param_->fuse_sigmoid;
has_bias_ = (conv_param_->bias) != nullptr;
offset_ = filter_tensor_h_ / 2 - pad_up_;
offset_w_ = filter_tensor_w_ / 2 - pad_left_;
Expand All @@ -97,6 +98,7 @@ void ConvImageCompute::PrepareForRun() {

#ifdef LITE_WITH_LOG
VLOG(3) << "Is relu fused? / " << (relu_fused_ ? "Yes" : "No");
VLOG(3) << "Is sigmoid fused? / " << (sigmoid_fused_ ? "Yes" : "No");
VLOG(3) << "groups:" << groups_ << " stride_h_:" << stride_h_
<< " stride_w_:" << stride_w_ << " pad_left_:" << pad_left_
<< " pad_up_:" << pad_up_ << " filter_tensor_h_:" << filter_tensor_h_
Expand Down Expand Up @@ -624,6 +626,7 @@ void ConvImageCompute::PrepareForRun() {
std::string build_options_single{""};
// relu options
VLOG(3) << "relu_fused_:" << relu_fused_
<< " sigmoid_fused_:" << sigmoid_fused_
<< " conv_param_->activation_param.active_type:"
<< static_cast<int>(conv_param_->activation_param.active_type)
<< " conv_param_->activation_param.has_active:"
Expand All @@ -640,6 +643,9 @@ void ConvImageCompute::PrepareForRun() {
if (conv_param_->activation_param.active_type ==
lite_api::ActivationType::kRelu) {
build_options_single += " -DRELU";
} else if (conv_param_->activation_param.active_type ==
lite_api::ActivationType::kSigmoid) {
build_options_single += " -DSIGMOID";
} else if (conv_param_->activation_param.active_type ==
lite_api::ActivationType::kRelu6) {
build_options_single += " -DRELU6";
Expand Down Expand Up @@ -2456,6 +2462,7 @@ void ConvImageCompute::PrintConvInfo() {
LOG(INFO) << "offset_:" << offset_;
LOG(INFO) << "groups_:" << groups_;
LOG(INFO) << "relu_fused_:" << relu_fused_;
LOG(INFO) << "sigmoid_fused_:" << sigmoid_fused_;
LOG(INFO) << "has_bias_:" << has_bias_;
LOG(INFO) << "fuse_eltwise_op_type_:" << fuse_eltwise_op_type_;

Expand Down
1 change: 1 addition & 0 deletions lite/kernels/opencl/conv_image_compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class ConvImageCompute : public KernelLite<TARGET(kOpenCL),
int groups_{-1};
std::string fuse_eltwise_op_type_;
bool relu_fused_{false};
bool sigmoid_fused_{false};
bool has_bias_{false};
bool is_mali_{false};
bool is_wino_{false};
Expand Down
3 changes: 3 additions & 0 deletions lite/kernels/opencl/conv_transpose_image_compute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ void ConvTransposeImageCompute::PrepareForRun() {
if (conv_param_->activation_param.active_type ==
lite_api::ActivationType::kRelu) {
build_options_single += " -DRELU";
} else if (conv_param_->activation_param.active_type ==
lite_api::ActivationType::kSigmoid) {
build_options_single += " -DSIGMOID";
} else if (conv_param_->activation_param.active_type ==
lite_api::ActivationType::kRelu6) {
build_options_single += " -DRELU6";
Expand Down
4 changes: 4 additions & 0 deletions lite/operators/conv_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class ConvOpLite : public OpLite {
if (act_type == "relu") {
param_.activation_param.active_type = lite_api::ActivationType::kRelu;
param_.fuse_relu = true;
} else if (act_type == "sigmoid") {
param_.activation_param.active_type =
lite_api::ActivationType::kSigmoid;
param_.fuse_sigmoid = true;
} else if (act_type == "relu6") {
param_.activation_param.active_type = lite_api::ActivationType::kRelu6;
param_.activation_param.Relu_clipped_coef =
Expand Down
3 changes: 3 additions & 0 deletions lite/operators/conv_transpose_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ bool ConvTransposeOpLite::AttachImpl(const cpp::OpDesc& op_desc,
if (act_type == "relu") {
param_.activation_param.active_type = lite_api::ActivationType::kRelu;
param_.fuse_relu = true;
} else if (act_type == "sigmoid") {
param_.activation_param.active_type = lite_api::ActivationType::kSigmoid;
param_.fuse_sigmoid = true;
} else if (act_type == "relu6") {
param_.activation_param.active_type = lite_api::ActivationType::kRelu6;
param_.activation_param.Relu_clipped_coef =
Expand Down
1 change: 1 addition & 0 deletions lite/operators/op_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ struct ConvParam : ParamBase {
bool fuse_relu_before_depthwise_conv{false};
bool use_mkldnn{false};
bool fuse_relu{false}; // only used in mkldnn kernel
bool fuse_sigmoid{false};
bool use_quantizer{
false}; // set true for op that should be quantized, only used for cpu
bool fuse_residual_connection{false};
Expand Down