Commit e15928b4 authored by Li Peng's avatar Li Peng

convolution and tanh layer fusion

Signed-off-by: 's avatarLi Peng <peng.li@intel.com>
parent b97b650a
...@@ -1239,13 +1239,14 @@ struct Net::Impl ...@@ -1239,13 +1239,14 @@ struct Net::Impl
} }
} }
// For now, OpenCL target only support fusion with activation of ReLU/ChannelsPReLU/Power // For now, OpenCL target support fusion with activation of ReLU/ChannelsPReLU/Power/Tanh
if ( preferableTarget != DNN_TARGET_OPENCL || if ( preferableTarget != DNN_TARGET_OPENCL ||
(preferableTarget == DNN_TARGET_OPENCL && (preferableTarget == DNN_TARGET_OPENCL &&
nextData && nextData &&
(!nextData->type.compare("ReLU") || ((nextData->type == "ReLU") ||
!nextData->type.compare("ChannelsPReLU") || (nextData->type == "ChannelsPReLU") ||
!nextData->type.compare("Power"))) ) (nextData->type == "TanH") ||
(nextData->type == "Power"))) )
{ {
Ptr<ActivationLayer> nextActivLayer; Ptr<ActivationLayer> nextActivLayer;
......
...@@ -246,6 +246,11 @@ public: ...@@ -246,6 +246,11 @@ public:
power = activ_power->power; power = activ_power->power;
activType = OCL4DNN_CONV_FUSED_ACTIV_POWER; activType = OCL4DNN_CONV_FUSED_ACTIV_POWER;
} }
Ptr<TanHLayer> activ_tanh = activ.dynamicCast<TanHLayer>();
if (!activ_tanh.empty())
{
activType = OCL4DNN_CONV_FUSED_ACTIV_TANH;
}
} }
#endif #endif
return !activ.empty(); return !activ.empty();
...@@ -877,11 +882,16 @@ public: ...@@ -877,11 +882,16 @@ public:
{ {
convolutionOp->setActivPower(true, power); convolutionOp->setActivPower(true, power);
} }
else if ( activType == OCL4DNN_CONV_FUSED_ACTIV_TANH)
{
convolutionOp->setActivTanh(true);
}
else else
{ {
convolutionOp->setActivReLU(false, 0); convolutionOp->setActivReLU(false, 0);
convolutionOp->setActivPReLU(false, reluslope); convolutionOp->setActivPReLU(false, reluslope);
convolutionOp->setActivPower(false, 1.f); convolutionOp->setActivPower(false, 1.f);
convolutionOp->setActivTanh(false);
} }
newActiv = false; newActiv = false;
} }
......
...@@ -77,7 +77,8 @@ typedef enum { ...@@ -77,7 +77,8 @@ typedef enum {
OCL4DNN_CONV_FUSED_ACTIV_NONE = 0, OCL4DNN_CONV_FUSED_ACTIV_NONE = 0,
OCL4DNN_CONV_FUSED_ACTIV_RELU = 1, OCL4DNN_CONV_FUSED_ACTIV_RELU = 1,
OCL4DNN_CONV_FUSED_ACTIV_PRELU = 2, OCL4DNN_CONV_FUSED_ACTIV_PRELU = 2,
OCL4DNN_CONV_FUSED_ACTIV_POWER = 3 OCL4DNN_CONV_FUSED_ACTIV_POWER = 3,
OCL4DNN_CONV_FUSED_ACTIV_TANH = 4
} ocl4dnnFusedActiv_t; } ocl4dnnFusedActiv_t;
template<typename Dtype> template<typename Dtype>
...@@ -94,6 +95,7 @@ class OCL4DNNConvSpatial ...@@ -94,6 +95,7 @@ class OCL4DNNConvSpatial
void setActivReLU(bool fuse_activ, float slope); void setActivReLU(bool fuse_activ, float slope);
void setActivPReLU(bool fuse_activ, std::vector<float> &slope); void setActivPReLU(bool fuse_activ, std::vector<float> &slope);
void setActivPower(bool fuse_activ, float power); void setActivPower(bool fuse_activ, float power);
void setActivTanh(bool fuse_activ);
void setBias(bool bias_term); void setBias(bool bias_term);
private: private:
......
...@@ -159,6 +159,9 @@ void OCL4DNNConvSpatial<Dtype>::setFusionDefine(ocl4dnnFusedActiv_t fused_activ, ...@@ -159,6 +159,9 @@ void OCL4DNNConvSpatial<Dtype>::setFusionDefine(ocl4dnnFusedActiv_t fused_activ,
case OCL4DNN_CONV_FUSED_ACTIV_POWER: case OCL4DNN_CONV_FUSED_ACTIV_POWER:
addDef("FUSED_CONV_POWER", 1); addDef("FUSED_CONV_POWER", 1);
break; break;
case OCL4DNN_CONV_FUSED_ACTIV_TANH:
addDef("FUSED_CONV_TANH", 1);
break;
default: default:
; ;
} }
...@@ -415,6 +418,17 @@ void OCL4DNNConvSpatial<Dtype>::setActivPower(bool fuse_activ, float power) ...@@ -415,6 +418,17 @@ void OCL4DNNConvSpatial<Dtype>::setActivPower(bool fuse_activ, float power)
fused_activ_ = OCL4DNN_CONV_FUSED_ACTIV_NONE; fused_activ_ = OCL4DNN_CONV_FUSED_ACTIV_NONE;
} }
template<typename Dtype>
void OCL4DNNConvSpatial<Dtype>::setActivTanh(bool fuse_activ)
{
if ( fuse_activ )
{
fused_activ_ = OCL4DNN_CONV_FUSED_ACTIV_TANH;
}
else
fused_activ_ = OCL4DNN_CONV_FUSED_ACTIV_NONE;
}
template<typename Dtype> template<typename Dtype>
bool OCL4DNNConvSpatial<Dtype>::Forward(const UMat& bottom, bool OCL4DNNConvSpatial<Dtype>::Forward(const UMat& bottom,
const UMat& bottom2, const UMat& bottom2,
......
...@@ -55,6 +55,9 @@ ...@@ -55,6 +55,9 @@
#elif defined(FUSED_CONV_POWER) #elif defined(FUSED_CONV_POWER)
#define ACTIVATION_RELU_FUNCTION(x, c) pow(x, power) #define ACTIVATION_RELU_FUNCTION(x, c) pow(x, power)
#define NEGATIVE_SLOPE_ARG Dtype power, #define NEGATIVE_SLOPE_ARG Dtype power,
#elif defined(FUSED_CONV_TANH)
#define ACTIVATION_RELU_FUNCTION(x, c) tanh(x)
#define NEGATIVE_SLOPE_ARG
#else #else
#define ACTIVATION_RELU_FUNCTION(x, c) (x) #define ACTIVATION_RELU_FUNCTION(x, c) (x)
#define NEGATIVE_SLOPE_ARG #define NEGATIVE_SLOPE_ARG
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment