diff --git a/src/common/types.cpp b/src/common/types.cpp index a6f32762d3c8a492c3347ebfe65cb50f39425976..d1647ab235186d8449180a8832fd0cafe0c67959 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -23,8 +23,9 @@ const std::string G_OP_TYPE_BOX_CODER = "box_coder"; const std::string G_OP_TYPE_CONCAT = "concat"; const std::string G_OP_TYPE_ELEMENTWISE_ADD = "elementwise_add"; const std::string G_OP_TYPE_FUSION_CONV_ADD_RELU = "fusion_conv_add_relu"; -const std::string G_OP_TYPE_FC = "fc"; -const std::string G_OP_TYPE_CONV_ADD = "conv_add"; +const std::string G_OP_TYPE_FUSION_CONV_ADD_BN_RELU = "fusion_conv_add_bn_relu"; +const std::string G_OP_TYPE_FC = "fusion_fc"; +const std::string G_OP_TYPE_FUSION_CONV_ADD = "fusion_conv_add"; const std::string G_OP_TYPE_LRN = "lrn"; const std::string G_OP_TYPE_MUL = "mul"; const std::string G_OP_TYPE_MULTICLASS_NMS = "multiclass_nms"; @@ -44,7 +45,7 @@ std::unordered_map< std::string, std::pair, std::vector>> op_input_output_key = { {G_OP_TYPE_CONV, {{"Input"}, {"Output"}}}, - {G_OP_TYPE_CONV_ADD, {{"Input"}, {"Out"}}}, + {G_OP_TYPE_FUSION_CONV_ADD, {{"Input"}, {"Out"}}}, {G_OP_TYPE_RELU, {{"X"}, {"Out"}}}, {G_OP_TYPE_SOFTMAX, {{"X"}, {"Out"}}}, {G_OP_TYPE_MUL, {{"X"}, {"Out"}}}, @@ -59,6 +60,8 @@ std::unordered_map< {G_OP_TYPE_TRANSPOSE, {{"X"}, {"Out"}}}, {G_OP_TYPE_BOX_CODER, {{"PriorBox", "PriorBoxVar", "TargetBox"}, {"OutputBox"}}}, + {G_OP_TYPE_FUSION_CONV_ADD_BN_RELU, + {{"Input"}, {"Out"}}}, {G_OP_TYPE_PRIOR_BOX, {{"Image", "Input"}, {"Boxes", "Variances"}}}, {G_OP_TYPE_MULTICLASS_NMS, {{"BBoxes", "Scores"}, {"Out"}}}, {G_OP_TYPE_FC, {{"X", "Y", "Z"}, {"Out"}}}, diff --git a/src/common/types.h b/src/common/types.h index 49f0c49a585ac45cbb0a061f72e33f2fb579a82e..9134ebe3561153e32db157c4c4b835a1bc464149 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -79,7 +79,9 @@ extern const std::string G_OP_TYPE_CONCAT; extern const std::string G_OP_TYPE_ELEMENTWISE_ADD; extern const std::string G_OP_TYPE_FUSION_CONV_ADD_RELU; extern const std::string G_OP_TYPE_FC; -extern const std::string G_OP_TYPE_CONV_ADD; +extern const std::string G_OP_TYPE_FUSION_CONV_ADD; +extern const std::string G_OP_TYPE_FUSION_CONV_ADD_BN_RELU; + extern const std::string G_OP_TYPE_LRN; extern const std::string G_OP_TYPE_MUL; extern const std::string G_OP_TYPE_MULTICLASS_NMS; diff --git a/src/framework/operator.h b/src/framework/operator.h index c68744a676030413e81570ded0db5671cdf4ba7a..793551b0cd3eea290243c156c27616a34c37a3d2 100644 --- a/src/framework/operator.h +++ b/src/framework/operator.h @@ -63,7 +63,7 @@ class OperatorBase { std::vector GetOutKeys() const; virtual void RunImpl() const = 0; - virtual void Init() const = 0; + virtual void Init() = 0; /* * @b op 运算所需的输入, 如上一层的输出结果、卷积核 * */ @@ -117,8 +117,8 @@ class OperatorWithKernel : public OperatorBase { virtual void InferShape() const = 0; - void Init() const { - PADDLE_MOBILE_ENFORCE(kernel_.Init(param_), " %s kernel init failed", + void Init() { + PADDLE_MOBILE_ENFORCE(kernel_.Init(¶m_), " %s kernel init failed", this->type_.c_str()); } @@ -146,7 +146,7 @@ class OpKernelBase { } #endif virtual void Compute(const P ¶) const = 0; - virtual bool Init(const P ¶) const { return true; }; + virtual bool Init(P *para) { return true; }; virtual ~OpKernelBase() = default; private: diff --git a/src/operators/feed_op.h b/src/operators/feed_op.h index bd5fd8cb32d484b7f76652139603f6b0f1b4b5d7..8753bfa9375f50930f9ec57e1b48b26c127edbc6 100644 --- a/src/operators/feed_op.h +++ b/src/operators/feed_op.h @@ -32,7 +32,7 @@ class FeedOp : public framework::OperatorBase { param_(inputs, outputs, attrs, *scope) {} void RunImpl() const { param_.Out()->ShareDataWith(*param_.InputX()); } - void Init() const {} + void Init() {} void InferShape() const { auto out_dims = param_.Out()->dims(); diff --git a/src/operators/fetch_op.h b/src/operators/fetch_op.h index 4b3680b58357d8295b1b6acf111d3573d4e4d1bd..b46093e18e1d92ed9dacbdb456bb591d0c546456 100644 --- a/src/operators/fetch_op.h +++ b/src/operators/fetch_op.h @@ -33,7 +33,7 @@ class FetchOp : public framework::OperatorBase { param_(inputs, outputs, attrs, *scope) {} void RunImpl() const { param_.Out()->ShareDataWith(*param_.InputX()); } - void Init() const {} + void Init() {} void InferShape() const { auto x_dims = param_.InputX()->dims(); diff --git a/src/operators/fusion_conv_add.cpp b/src/operators/fusion_conv_add.cpp index 4c01603509b0a1d9da2c2dc31a38719d5117e05c..731bb66cb058dd8562b5fc9257bd8e9ed5f9c0af 100644 --- a/src/operators/fusion_conv_add.cpp +++ b/src/operators/fusion_conv_add.cpp @@ -50,8 +50,8 @@ template class FusionConvAddOp; namespace ops = paddle_mobile::operators; #ifdef PADDLE_MOBILE_CPU -USE_OP_CPU(conv_add); -REGISTER_OPERATOR_CPU(conv_add, ops::FusionConvAddOp); +USE_OP_CPU(fusion_conv_add); +REGISTER_OPERATOR_CPU(fusion_conv_add, ops::FusionConvAddOp); #endif #ifdef PADDLE_MOBILE_MALI_GPU USE_OP_MALI_GPU(conv_add); diff --git a/src/operators/fusion_conv_add.h b/src/operators/fusion_conv_add.h index 89262cc50849e591694ecb34fec5fac6435b8fa7..e9677b33de8ca9aa875dd709cc283ea7c3d789cb 100644 --- a/src/operators/fusion_conv_add.h +++ b/src/operators/fusion_conv_add.h @@ -42,7 +42,7 @@ class FusionConvAddMatcher : public framework::FusionOpMatcher { {{G_OP_TYPE_ELEMENTWISE_ADD, {{"Y", "Y"}}}}, removed_nodes); } - std::string Type() { return G_OP_TYPE_CONV_ADD; } + std::string Type() { return G_OP_TYPE_FUSION_CONV_ADD; } }; template @@ -68,11 +68,11 @@ class FusionConvAddOp : public framework::OperatorWithKernel< #ifdef PADDLE_MOBILE_CPU -#ifndef CONV_ADD_REGISTER -static framework::FusionOpRegistrar convadd_registrar( - new FusionConvAddMatcher()); -#define CONV_ADD_REGISTER -#endif +//#ifndef CONV_ADD_REGISTER +//static framework::FusionOpRegistrar convadd_registrar( +// new FusionConvAddMatcher()); +//#define CONV_ADD_REGISTER +//#endif #endif diff --git a/src/operators/fusion_conv_add_bn_relu_op.cpp b/src/operators/fusion_conv_add_bn_relu_op.cpp index 5a92eca45a1014ef36cd887e88e364846e693009..63d0b23444ae6bf625e5e8640d3dc2ad314917d2 100644 --- a/src/operators/fusion_conv_add_bn_relu_op.cpp +++ b/src/operators/fusion_conv_add_bn_relu_op.cpp @@ -1,4 +1,61 @@ -// -// Created by Yang,Sui on 2018/6/28. -// +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#ifdef FUSION_CONVADDBNRELU_OP + +#include "operators/fusion_conv_add_bn_relu_op.h" +#include "operators/math/conv_func.h" + +namespace paddle_mobile { +namespace operators { + +template +void FusionConvAddBNReluOp::InferShape() const { + auto in_dims = this->param_.Input()->dims(); + auto filter_dims = this->param_.Filter()->dims(); + const std::vector &strides = this->param_.Strides(); + std::vector paddings = this->param_.Paddings(); + int groups = this->param_.Groups(); + std::vector dilations = this->param_.Dilations(); + + PADDLE_MOBILE_ENFORCE((in_dims.size() == filter_dims.size() && + dilations.size() == paddings.size() && + paddings.size() == strides.size()), + "ConvParam is not suitable"); + + std::vector output_shape({in_dims[0], filter_dims[0]}); + for (size_t i = 0; i < strides.size(); ++i) { + output_shape.push_back( + math::ConvOutputSize(in_dims[i + 2], filter_dims[i + 2], dilations[i], + paddings[i], strides[i])); + } + + framework::DDim ddim = framework::make_ddim(output_shape); + this->param_.Output()->Resize(ddim); +} +template class FusionConvAddBNReluOp; +} // namespace operators +} // namespace paddle_mobile + +namespace ops = paddle_mobile::operators; +#ifdef PADDLE_MOBILE_CPU +USE_OP_CPU(fusion_conv_add_bn_relu); +REGISTER_OPERATOR_CPU(fusion_conv_add_bn_relu, ops::FusionConvAddBNReluOp); +#endif +#ifdef PADDLE_MOBILE_MALI_GPU +#endif +#ifdef PADDLE_MOBILE_FPGA +#endif + +#endif diff --git a/src/operators/fusion_conv_add_bn_relu_op.h b/src/operators/fusion_conv_add_bn_relu_op.h index e265906cb7795715d1219cdb5baf5e5607936e15..0091e30f587bbbb3f83a4772c130bdba1c0645cc 100644 --- a/src/operators/fusion_conv_add_bn_relu_op.h +++ b/src/operators/fusion_conv_add_bn_relu_op.h @@ -1,8 +1,103 @@ -// -// Created by Yang,Sui on 2018/6/28. -// +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -#ifndef PADDLE_MOBILE_FUSION_CONV_ADD_BN_RELU_OP_H -#define PADDLE_MOBILE_FUSION_CONV_ADD_BN_RELU_OP_H +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at -#endif //PADDLE_MOBILE_FUSION_CONV_ADD_BN_RELU_OP_H + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#define FUSION_CONVADDBNRELU_OP +#ifdef FUSION_CONVADDBNRELU_OP + +#pragma once + +#include +#include +#include "framework/operator.h" +#include "framework/program/program-optimize/fusion_op_register.h" +#include "op_param.h" +#include "operators/kernel/conv_add_bn_relu_kernel.h" + +namespace paddle_mobile { +namespace operators { +using std::string; +using std::vector; +class FusionConvAddBNReluMatcher : public framework::FusionOpMatcher { + public: + FusionConvAddBNReluMatcher() { + node_ = framework::Node(G_OP_TYPE_CONV); + node_ > std::make_shared(G_OP_TYPE_ELEMENTWISE_ADD) > + std::make_shared(G_OP_TYPE_BATCHNORM) > + std::make_shared(G_OP_TYPE_RELU); + } + + void FolderNodes( + framework::Node *node, + std::vector> *removed_nodes) { + vector> origin_descs = + node->OpDescs(node_.Depth()); + node->Folder(node_.Depth(), Type(), + { {G_OP_TYPE_ELEMENTWISE_ADD, {{"Y", "Y"}}}, + {G_OP_TYPE_BATCHNORM, {{"Scale", "Scale"}, + {"Mean", "Mean"}, + {"Bias", "Bias"}, + {"Variance", "Variance"}}}}, removed_nodes); + } + + std::string Type() { return G_OP_TYPE_FUSION_CONV_ADD_BN_RELU; } +}; + +template +class FusionConvAddBNReluOp + : public framework::OperatorWithKernel< + DeviceType, FusionConvAddBNReluParam, + operators::ConvAddBNReluKernel> { + public: + FusionConvAddBNReluOp(const string &type, const VariableNameMap &inputs, + const VariableNameMap &outputs, + const framework::AttributeMap &attrs, + std::shared_ptr scope) + : framework::OperatorWithKernel< + DeviceType, FusionConvAddBNReluParam, + operators::ConvAddBNReluKernel>( + type, inputs, outputs, attrs, scope) {} + + using framework::OperatorWithKernel< + DeviceType, FusionConvAddBNReluParam, + operators::ConvAddBNReluKernel>::OperatorWithKernel; + void InferShape() const override; + + protected: +}; + +#ifdef PADDLE_MOBILE_CPU +//#ifndef FUSION_CONV_ADD_BN_RELU_REGISTER +//static framework::FusionOpRegistrar fusion_conv_add_bn_relu_registrar( +// new FusionConvAddBNReluMatcher()); +//#define FUSION_CONV_ADD_BN_RELU_REGISTER +//#endif +#endif + +#ifdef PADDLE_MOBILE_MALI_GPU + +#ifndef FUSION_CONV_ADD_BN_RELU_REGISTER +static framework::FusionOpRegistrar fusion_conv_add_bn_relu_registrar( + new FusionConvAddBNReluMatcher()); +#define FUSION_CONV_ADD_BN_RELU_REGISTER +#endif + +#endif + +#ifdef PADDLE_MOBILE_FPGA +#endif + +} // namespace operators +} // namespace paddle_mobile + +#endif diff --git a/src/operators/fusion_fc_op.cpp b/src/operators/fusion_fc_op.cpp index fae561348899dadc4c25f84ec3a0993d9ae693f9..2e591b678cf7987eba5fdc74643cd7e15c35271f 100644 --- a/src/operators/fusion_fc_op.cpp +++ b/src/operators/fusion_fc_op.cpp @@ -55,8 +55,8 @@ template class FusionFcOp; namespace ops = paddle_mobile::operators; #ifdef PADDLE_MOBILE_CPU -USE_OP_CPU(fc); -REGISTER_OPERATOR_CPU(fc, ops::FusionFcOp); +USE_OP_CPU(fusion_fc); +REGISTER_OPERATOR_CPU(fusion_fc, ops::FusionFcOp); #endif #ifdef PADDLE_MOBILE_MALI_GPU USE_OP_MALI_GPU(fc); diff --git a/src/operators/kernel/arm/batchnorm_kernel.cpp b/src/operators/kernel/arm/batchnorm_kernel.cpp index 964bf71f451e2ca48d3742ed5151e9784c516d5c..dcc20391854a63e807aabb4dea50188adffa2150 100644 --- a/src/operators/kernel/arm/batchnorm_kernel.cpp +++ b/src/operators/kernel/arm/batchnorm_kernel.cpp @@ -21,7 +21,7 @@ namespace paddle_mobile { namespace operators { template <> -bool BatchNormKernel::Init(const BatchNormParam ¶) const { +bool BatchNormKernel::Init(BatchNormParam *param) const { return true; } diff --git a/src/operators/kernel/arm/box_coder_kernel.cpp b/src/operators/kernel/arm/box_coder_kernel.cpp index df0a75f357658736ede4265a6cc57db30afee1d4..0a08205262a2311883311014e906533dfa712a4c 100644 --- a/src/operators/kernel/arm/box_coder_kernel.cpp +++ b/src/operators/kernel/arm/box_coder_kernel.cpp @@ -111,7 +111,7 @@ void DecodeCenterSize(const framework::Tensor& target_box, } template <> -bool BoxCoderKernel::Init(const BoxCoderParam& para) const { +bool BoxCoderKernel::Init(BoxCoderParam* param) const { return true; } diff --git a/src/operators/kernel/arm/concat_kernel.cpp b/src/operators/kernel/arm/concat_kernel.cpp index 0312047b8e8af1eb9dad57c751e392e8a5054878..112d104ea1485204eab943f4bb364fa482607c38 100644 --- a/src/operators/kernel/arm/concat_kernel.cpp +++ b/src/operators/kernel/arm/concat_kernel.cpp @@ -53,7 +53,7 @@ class ConcatFunctor { }; template <> -bool ConcatKernel::Init(const ConcatParam ¶) const { +bool ConcatKernel::Init(ConcatParam *param) const { return true; } diff --git a/src/operators/kernel/arm/conv_add_bn_relu_kernel.cpp b/src/operators/kernel/arm/conv_add_bn_relu_kernel.cpp index 5a92eca45a1014ef36cd887e88e364846e693009..b514b44f4310ca0a6c416e0d68bea4650de6b165 100644 --- a/src/operators/kernel/arm/conv_add_bn_relu_kernel.cpp +++ b/src/operators/kernel/arm/conv_add_bn_relu_kernel.cpp @@ -1,4 +1,67 @@ -// -// Created by Yang,Sui on 2018/6/28. -// +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#ifdef FUSION_CONVADDBNRELU_OP + +#include "operators/kernel/conv_add_bn_relu_kernel.h" +#include "operators/kernel/central-arm-func/conv_add_bn_relu_func.h" + +namespace paddle_mobile { +namespace operators { + +template <> +bool ConvAddBNReluKernel::Init( + FusionConvAddBNReluParam *param) const { + const Tensor *mean = (*param).InputMean(); + const Tensor *variance = (*param).InputVariance(); + const Tensor *scale = (*param).InputScale(); + const Tensor *bias = (*param).InputBias(); + const float epsilon = (*param).Epsilon(); + + auto mean_ptr = mean->data(); + auto variance_ptr = variance->data(); + auto scale_ptr = scale->data(); + auto bias_ptr = bias->data(); + + const int C = mean->numel(); + float inv_std_ptr[C]; + for (int i = 0; i < C; i++) { + inv_std_ptr[i] = + 1 / static_cast(pow((variance_ptr[i] + epsilon), 0.5)); + } + Tensor *new_scale = new Tensor(); + Tensor *new_bias = new Tensor(); + auto new_scale_ptr = new_scale->mutable_data({C}); + auto new_bias_ptr = new_bias->mutable_data({C}); + for (int i = 0; i < C; i++) { + new_scale_ptr[i] = inv_std_ptr[i] * scale_ptr[i]; + new_bias_ptr[i] = bias_ptr[i] - mean_ptr[i] * inv_std_ptr[i] * scale_ptr[i]; + } + std::cout << "yes" << std::endl; + (*param).SetNewScale(new_scale); + (*param).SetNewBias(new_bias); + return true; +} + +template <> +void ConvAddBNReluKernel::Compute( + const FusionConvAddBNReluParam ¶m) const { + ConvAddBNReluCompute(param); +} +template class ConvAddBNReluKernel; + +} // namespace operators +} // namespace paddle_mobile + +#endif diff --git a/src/operators/kernel/arm/conv_add_relu_kernel.cpp b/src/operators/kernel/arm/conv_add_relu_kernel.cpp index d3c04179b37014adc6c81f32dd6c08f697283671..6fa5ce44af2f1315d6c774e14d09eb96e5e88a62 100644 --- a/src/operators/kernel/arm/conv_add_relu_kernel.cpp +++ b/src/operators/kernel/arm/conv_add_relu_kernel.cpp @@ -21,8 +21,7 @@ namespace paddle_mobile { namespace operators { template <> -bool ConvAddReluKernel::Init( - const FusionConvAddReluParam ¶) const { +bool ConvAddReluKernel::Init(FusionConvAddReluParam *param) const { return true; } diff --git a/src/operators/kernel/arm/conv_kernel.cpp b/src/operators/kernel/arm/conv_kernel.cpp index 049425d88f96a322a0b4cb47c18d85f2df03d577..04e7c43b71832605301d29e4f9d81651a0015475 100644 --- a/src/operators/kernel/arm/conv_kernel.cpp +++ b/src/operators/kernel/arm/conv_kernel.cpp @@ -21,7 +21,7 @@ namespace paddle_mobile { namespace operators { template <> -bool ConvKernel::Init(const ConvParam ¶) const { +bool ConvKernel::Init(ConvParam *param) const { return true; } diff --git a/src/operators/kernel/arm/depthwise_conv_kernel.cpp b/src/operators/kernel/arm/depthwise_conv_kernel.cpp index 4cbfa23248e87e2bf3a8d97330fa19f92985a9d0..859622f06385bde78e07ceb34f0ace0f241b5fc1 100644 --- a/src/operators/kernel/arm/depthwise_conv_kernel.cpp +++ b/src/operators/kernel/arm/depthwise_conv_kernel.cpp @@ -21,7 +21,7 @@ namespace paddle_mobile { namespace operators { template <> -bool DepthwiseConvKernel::Init(const ConvParam ¶) const { +bool DepthwiseConvKernel::Init(ConvParam *param) const { return true; } diff --git a/src/operators/kernel/arm/elementwise_add_kernel.cpp b/src/operators/kernel/arm/elementwise_add_kernel.cpp index 2f5e26a37e4f2c1d370805ee7b565a60f4748b0a..e1e1ee0ce8ad144e70eedfc2b264fa8a19a76886 100644 --- a/src/operators/kernel/arm/elementwise_add_kernel.cpp +++ b/src/operators/kernel/arm/elementwise_add_kernel.cpp @@ -27,8 +27,7 @@ struct AddFunctor { }; template <> -bool ElementwiseAddKernel::Init( - const ElementwiseAddParam ¶) const { +bool ElementwiseAddKernel::Init(ElementwiseAddParam *param) const { return true; } diff --git a/src/operators/kernel/arm/fusion_fc_kernel.cpp b/src/operators/kernel/arm/fusion_fc_kernel.cpp index 5fac70e40781593669abd15b8f28ff6272f7133c..0cc060117116c76392b214aaa60e0e903c0949bb 100644 --- a/src/operators/kernel/arm/fusion_fc_kernel.cpp +++ b/src/operators/kernel/arm/fusion_fc_kernel.cpp @@ -22,7 +22,7 @@ namespace paddle_mobile { namespace operators { template <> -bool FusionFcKernel::Init(const FusionFcParam ¶) const { +bool FusionFcKernel::Init(FusionFcParam *param) const { return true; } diff --git a/src/operators/kernel/arm/lrn_kernel.cpp b/src/operators/kernel/arm/lrn_kernel.cpp index 839c5ee95bd4d1e9d3fd80af3df0f8a45797434e..cf6057fd7f9add34aed699fdcdb31c9762a718d8 100644 --- a/src/operators/kernel/arm/lrn_kernel.cpp +++ b/src/operators/kernel/arm/lrn_kernel.cpp @@ -22,7 +22,7 @@ namespace paddle_mobile { namespace operators { template <> -bool LrnKernel::Init(const LrnParam ¶) const { +bool LrnKernel::Init(LrnParam *param) const { return true; } diff --git a/src/operators/kernel/arm/mul_kernel.cpp b/src/operators/kernel/arm/mul_kernel.cpp index b3bb2b8075fdf306d47640c2bee3f2fc00ef0bc0..54ed460ce7290047930f5dcae0205092b8e591a5 100644 --- a/src/operators/kernel/arm/mul_kernel.cpp +++ b/src/operators/kernel/arm/mul_kernel.cpp @@ -22,7 +22,7 @@ namespace paddle_mobile { namespace operators { template <> -bool MulKernel::Init(const MulParam ¶) const { +bool MulKernel::Init(MulParam *param) const { return true; } diff --git a/src/operators/kernel/arm/multiclass_nms_kernel.cpp b/src/operators/kernel/arm/multiclass_nms_kernel.cpp index 67cf8197ca4c3113fc4fde3d493d6ed209221b59..a7d12d47b229edf2e54a0dd52d44b0b23b7a0fbe 100644 --- a/src/operators/kernel/arm/multiclass_nms_kernel.cpp +++ b/src/operators/kernel/arm/multiclass_nms_kernel.cpp @@ -204,8 +204,7 @@ void MultiClassOutput(const Tensor& scores, const Tensor& bboxes, } template <> -bool MultiClassNMSKernel::Init( - const MultiClassNMSParam& para) const { +bool MultiClassNMSKernel::Init(MultiClassNMSParam* param) const { return true; } diff --git a/src/operators/kernel/arm/pool_kernel.cpp b/src/operators/kernel/arm/pool_kernel.cpp index 09162a13a4d0c59220cc25a02d06369c3f21ed32..3031b1899906e9923388567f2bd46649623f9b4d 100644 --- a/src/operators/kernel/arm/pool_kernel.cpp +++ b/src/operators/kernel/arm/pool_kernel.cpp @@ -36,7 +36,7 @@ inline void PoolBasic(std::string pooling_type, std::vector ksize, } template <> -bool PoolKernel::Init(const PoolParam ¶) const { +bool PoolKernel::Init(PoolParam *param) const { return true; } diff --git a/src/operators/kernel/arm/prior_box_kernel.cpp b/src/operators/kernel/arm/prior_box_kernel.cpp index 13939bc7bf27904405677560f17d2e0b85748310..ac560dd9196f5451f8135510fd7c2f746c7e3d0a 100644 --- a/src/operators/kernel/arm/prior_box_kernel.cpp +++ b/src/operators/kernel/arm/prior_box_kernel.cpp @@ -27,7 +27,7 @@ struct ClipFunctor { }; template <> -bool PriorBoxKernel::Init(const PriorBoxParam ¶) const { +bool PriorBoxKernel::Init(PriorBoxParam *param) const { return true; } diff --git a/src/operators/kernel/arm/relu_kernel.cpp b/src/operators/kernel/arm/relu_kernel.cpp index 5bc485b77a8fac9379adbd1a3bd4d406e5a82fcb..c0346ad7226700fbf3772fef659eb75a139a2462 100644 --- a/src/operators/kernel/arm/relu_kernel.cpp +++ b/src/operators/kernel/arm/relu_kernel.cpp @@ -26,7 +26,7 @@ struct ReluFunctor { }; template <> -bool ReluKernel::Init(const ReluParam ¶) const { +bool ReluKernel::Init(ReluParam *param) const { return true; } diff --git a/src/operators/kernel/arm/reshape_kernel.cpp b/src/operators/kernel/arm/reshape_kernel.cpp index 97364f9a3f7ce9fe8da5814ad2a483f858938bbf..e492a98e596355176d6dd9023bc8af8bc2874ec0 100644 --- a/src/operators/kernel/arm/reshape_kernel.cpp +++ b/src/operators/kernel/arm/reshape_kernel.cpp @@ -20,7 +20,7 @@ namespace paddle_mobile { namespace operators { template <> -bool ReshapeKernel::Init(const ReshapeParam ¶) const { +bool ReshapeKernel::Init(ReshapeParam *param) const { return true; } diff --git a/src/operators/kernel/arm/sigmoid_kernel.cpp b/src/operators/kernel/arm/sigmoid_kernel.cpp index 3e87bfacc5335e52ecdcb0b917f5826b80449ef4..44bd48800d8406ed791bb9f14e7732cab069dd4a 100644 --- a/src/operators/kernel/arm/sigmoid_kernel.cpp +++ b/src/operators/kernel/arm/sigmoid_kernel.cpp @@ -72,7 +72,7 @@ void sigmoid(const Tensor *X, Tensor *Y) { } template <> -bool SigmoidKernel::Init(const SigmoidParam ¶) const { +bool SigmoidKernel::Init(SigmoidParam *param) const { return true; } diff --git a/src/operators/kernel/arm/softmax_kernel.cpp b/src/operators/kernel/arm/softmax_kernel.cpp index 8e966aa0af9ac84b70b154b33bad7dad9e79121d..7fe9007ebed72761bb2cd395e9b2f54e2fb4eeac 100644 --- a/src/operators/kernel/arm/softmax_kernel.cpp +++ b/src/operators/kernel/arm/softmax_kernel.cpp @@ -20,7 +20,7 @@ namespace paddle_mobile { namespace operators { template <> -bool SoftmaxKernel::Init(const SoftmaxParam ¶) const { +bool SoftmaxKernel::Init(SoftmaxParam *param) const { return true; } diff --git a/src/operators/kernel/arm/transpose_kernel.cpp b/src/operators/kernel/arm/transpose_kernel.cpp index a44ff22a2f228cc357c066a01e142de7cc4f2083..e43c42010f445b47855e60e56b1be1eb2b93f4cb 100644 --- a/src/operators/kernel/arm/transpose_kernel.cpp +++ b/src/operators/kernel/arm/transpose_kernel.cpp @@ -35,7 +35,7 @@ namespace operators { // } template <> -bool TransposeKernel::Init(const TransposeParam& para) const { +bool TransposeKernel::Init(TransposeParam* param) const { return true; } diff --git a/src/operators/kernel/batchnorm_kernel.h b/src/operators/kernel/batchnorm_kernel.h index 6ef5329bc58fea8bfc17d9115b7004fed2bc4ed7..c143d08a3a5dada9d383dd6d5819f80f2ee9ce13 100644 --- a/src/operators/kernel/batchnorm_kernel.h +++ b/src/operators/kernel/batchnorm_kernel.h @@ -29,7 +29,7 @@ class BatchNormKernel : public framework::OpKernelBase { public: void Compute(const BatchNormParam ¶m) const; - bool Init(const BatchNormParam ¶) const; + bool Init(BatchNormParam *param) const; }; } // namespace operators diff --git a/src/operators/kernel/box_coder_kernel.h b/src/operators/kernel/box_coder_kernel.h index 4c4206f52b3ffc5e60983bf1d6adb25292d01ac4..7b98b0c10cda1186e083f916e915dbd83353d05d 100644 --- a/src/operators/kernel/box_coder_kernel.h +++ b/src/operators/kernel/box_coder_kernel.h @@ -30,7 +30,7 @@ class BoxCoderKernel : public framework::OpKernelBase { public: void Compute(const BoxCoderParam& param) const; - bool Init(const BoxCoderParam& para) const; + bool Init(BoxCoderParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/central-arm-func/batchnorm_arm_func.h b/src/operators/kernel/central-arm-func/batchnorm_arm_func.h index 2086265ec6d9d36c5b3a334fad7a5cf66fc01f2b..5813f39471a4874461abe8563253c034f7e9aa2d 100644 --- a/src/operators/kernel/central-arm-func/batchnorm_arm_func.h +++ b/src/operators/kernel/central-arm-func/batchnorm_arm_func.h @@ -53,7 +53,7 @@ void BatchnormCompute(const BatchNormParam ¶m) { "C must equal to variance.numel()"); int HXW = H * W; - if (HXW > 32) { + if (0&&HXW > 32) { int NXC = N * C; float *inv_std_ptr = new float[NXC * 4]; float *volatile new_scale_ptr = new float[NXC * 4]; @@ -222,8 +222,15 @@ void BatchnormCompute(const BatchNormParam ¶m) { } } } + } } +// for(int i = 0; i < new_scale.numel(); i++){ +// std::cout << "new_scale " << new_scale_ptr[i] < +void ConvAddBNReluCompute(const FusionConvAddBNReluParam ¶m) { + const Tensor *input = param.Input(); + DLOG << "input: " << *input; + Tensor filter = *param.Filter(); + DLOG << "filter: " << filter; + Tensor bias = *param.Bias(); + DLOG << "bias: " << bias; + + Tensor new_bias = *param.NewBias(); + Tensor new_scale = *param.NewScale(); + auto new_bias_ptr = new_bias.data(); + auto new_scale_ptr = new_scale.data(); + // + // for(int i = 0; i < new_scale.numel(); i++){ + // std::cout << "new_scale " << new_scale_ptr[i] < strides = param.Strides(); + std::vector paddings = param.Paddings(); + std::vector dilations = param.Dilations(); + Tensor *output = param.Output(); + std::vector filter_shape_vec(framework::vectorize(filter.dims())); + + if (filter_shape_vec[2] == 3 && strides[0] == 1 && groups > 1) { + math::DepthwiseConv3x3s1p1(input, filter, output, &bias, 1, &new_scale, + &new_bias, 1, 1); + } else { + const int batch_size = static_cast(input->dims()[0]); + + math::expand_bias(bias, axis, output->dims()); + output->ShareDataWith(bias); + + std::vector output_shape_vec(framework::vectorize(output->dims())); + size_t data_dim = filter_shape_vec.size() - 2; + std::vector col_shape_vec(1 + 2 * data_dim); + col_shape_vec[0] = input->dims()[1] / groups; + for (size_t j = 0; j < data_dim; ++j) { + col_shape_vec[j + 1] = filter_shape_vec[j + 2]; + col_shape_vec[j + 1 + data_dim] = output_shape_vec[j + 2]; + } + framework::DDim col_shape(framework::make_ddim(col_shape_vec)); + + framework::DDim col_matrix_shape = + framework::flatten_to_2d(col_shape, data_dim + 1); + + bool is_expand = + math::IsExpand(filter_shape_vec, strides, paddings, dilations); + Tensor col; + Tensor col_matrix; + if (is_expand) { + col.mutable_data(col_shape); + col_matrix.ShareDataWith(col); + col_matrix.Resize(col_matrix_shape); + } + + framework::DDim input_shape = framework::slice_ddim( + input->dims(), 1, static_cast(input->dims().size())); + + framework::DDim filter_matrix_shape = {filter.dims()[0], + filter.numel() / filter.dims()[0]}; + filter.Resize(filter_matrix_shape); + framework::DDim output_matrix_shape = { + output->dims()[1], + output->numel() / (output->dims()[0] * output->dims()[1])}; + + // convolution operator: im2col(or vol2col) + gemm + int in_step = static_cast(input->dims()[1]) / groups; + int out_step = static_cast(output->dims()[1]) / groups; + + math::Vol2ColFunctor vol2col; + math::Im2ColFunctor im2col; + + for (int i = 0; i < batch_size; i++) { + Tensor in_batch = input->Slice(i, i + 1).Resize(input_shape); + Tensor out_batch = output->Slice(i, i + 1).Resize(output_matrix_shape); + + for (int g = 0; g < groups; g++) { + Tensor in_slice = in_batch.Slice(g * in_step, (g + 1) * in_step); + + if (!is_expand) { + col.ShareDataWith(in_slice); + col_matrix.ShareDataWith(col); + col_matrix.Resize(col_matrix_shape); + } else if (data_dim == 2U) { + // im2col + im2col(in_slice, dilations, strides, + std::vector{paddings[0], paddings[1], paddings[0], + paddings[1]}, + &col); + } else if (data_dim == 3U) { + // vol2col + vol2col(in_slice, dilations, strides, paddings, &col); + } + + // gemm + Tensor out_slice = out_batch.Slice(g * out_step, (g + 1) * out_step); + Tensor filter_slice = filter.Slice(g * out_step, (g + 1) * out_step); + math::matmul(filter_slice, false, col_matrix, false, + static_cast(1), &out_slice, + static_cast(1), false); + } + } + + auto output_ptr = output->data(); + for (int c = 0; c < output_matrix_shape[0]; c++){ + // int start = c * output_matrix_shape[1]; + for (int j = 0; j < output_matrix_shape[1]; j++){ + // output_ptr[start + j] = output_ptr[start +j]*new_scale_ptr[c]+new_bias_ptr[c]; + // output_ptr[start + j] = output_ptr[start+j]< 0 ? 0 : output_ptr[start +j]; + } + } + } +} +} // namespace operators +} // namespace paddle_mobile + +#endif diff --git a/src/operators/kernel/concat_kernel.h b/src/operators/kernel/concat_kernel.h index 6a7b7c6005b6e85e5b1ccfee713672b6e333b98a..0c1f1666bb642d5c1993904dcc2d46ebb68d4525 100644 --- a/src/operators/kernel/concat_kernel.h +++ b/src/operators/kernel/concat_kernel.h @@ -27,7 +27,7 @@ template class ConcatKernel : public framework::OpKernelBase { public: void Compute(const ConcatParam ¶m) const; - bool Init(const ConcatParam ¶) const; + bool Init(ConcatParam *param) const; }; } // namespace operators diff --git a/src/operators/kernel/conv_add_bn_relu_kernel.h b/src/operators/kernel/conv_add_bn_relu_kernel.h index 9f0bca0f8792036d373af6d03393999a67f2e76f..ab8773d9dc98c357d196545aa1f1627373269922 100644 --- a/src/operators/kernel/conv_add_bn_relu_kernel.h +++ b/src/operators/kernel/conv_add_bn_relu_kernel.h @@ -14,7 +14,7 @@ limitations under the License. */ #pragma once -#ifdef FUSION_CONVADD_BN_RELU_OP +#ifdef FUSION_CONVADDBNRELU_OP #include #include "framework/ddim.h" @@ -26,20 +26,20 @@ limitations under the License. */ #include "operators/op_param.h" namespace paddle_mobile { - namespace operators { +namespace operators { - using framework::DDim; - using framework::OpKernelBase; +using framework::DDim; +using framework::OpKernelBase; - template - class ConvAddBNReluKernel - : public OpKernelBase { - public: - void Compute(const FusionConvAddBNReluParam ¶m) const; - bool Init(const FusionConvAddBNReluParam ¶) const; - }; +template +class ConvAddBNReluKernel + : public OpKernelBase { + public: + void Compute(const FusionConvAddBNReluParam ¶m) const; + bool Init(FusionConvAddBNReluParam *param) const; +}; - } // namespace operators +} // namespace operators } // namespace paddle_mobile #endif diff --git a/src/operators/kernel/conv_add_kernel.h b/src/operators/kernel/conv_add_kernel.h index fb161238fee0550a42cd62cc132d6e8dbf45872f..d396ca59d123842423992dd5dfdc0f6940ea504a 100644 --- a/src/operators/kernel/conv_add_kernel.h +++ b/src/operators/kernel/conv_add_kernel.h @@ -40,7 +40,7 @@ template class ConvAddKernel : public OpKernelBase { public: void Compute(const FusionConvAddParam ¶m) const; - bool Init(const FusionConvAddParam ¶) const; + bool Init(FusionConvAddParam *param) const; }; } // namespace operators diff --git a/src/operators/kernel/conv_add_relu_kernel.h b/src/operators/kernel/conv_add_relu_kernel.h index 9b86cd22e82e641ee6cb0a15bd25c8a1c6cbe8cb..23862982fce47998b2948c558a22fc59d3a98372 100644 --- a/src/operators/kernel/conv_add_relu_kernel.h +++ b/src/operators/kernel/conv_add_relu_kernel.h @@ -36,7 +36,7 @@ class ConvAddReluKernel : public OpKernelBase { public: void Compute(const FusionConvAddReluParam ¶m) const; - bool Init(const FusionConvAddReluParam ¶) const; + bool Init(FusionConvAddReluParam *param) const; }; } // namespace operators diff --git a/src/operators/kernel/conv_kernel.h b/src/operators/kernel/conv_kernel.h index 812ddd5a441f3a24c557546c1780248a557a6eb0..b2bcc3e0c2bdb384710ac61228e8c8ea61ae4f80 100644 --- a/src/operators/kernel/conv_kernel.h +++ b/src/operators/kernel/conv_kernel.h @@ -32,7 +32,7 @@ template class ConvKernel : public OpKernelBase { public: void Compute(const ConvParam ¶m) const; - bool Init(const ConvParam ¶) const; + bool Init(ConvParam *param) const; }; } // namespace operators diff --git a/src/operators/kernel/depthwise_conv_kernel.h b/src/operators/kernel/depthwise_conv_kernel.h index a8a8fb338620477670477703018bf9e6e9a8a604..075f633a07ce06471975a93e8641de10565a02cf 100644 --- a/src/operators/kernel/depthwise_conv_kernel.h +++ b/src/operators/kernel/depthwise_conv_kernel.h @@ -31,7 +31,7 @@ template class DepthwiseConvKernel : public OpKernelBase { public: void Compute(const ConvParam ¶m) const; - bool Init(const ConvParam ¶) const; + bool Init(ConvParam *param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/elementwise_add_kernel.h b/src/operators/kernel/elementwise_add_kernel.h index fe6a0238dcd5249e822de3b5930438df808bf853..430c3e3fdfedb85a9e7cb4da8fd9c6c1b6fe981a 100644 --- a/src/operators/kernel/elementwise_add_kernel.h +++ b/src/operators/kernel/elementwise_add_kernel.h @@ -30,7 +30,7 @@ class ElementwiseAddKernel : public framework::OpKernelBase { public: void Compute(const ElementwiseAddParam ¶m) const; - bool Init(const ElementwiseAddParam ¶) const; + bool Init(ElementwiseAddParam *param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/fpga/conv_kernel.cpp b/src/operators/kernel/fpga/conv_kernel.cpp index 30dd64fd1466902036a72faa4be5d359d2bdb0bf..126d593762d060c4660f9c52d0161dcccfb3efdb 100644 --- a/src/operators/kernel/fpga/conv_kernel.cpp +++ b/src/operators/kernel/fpga/conv_kernel.cpp @@ -20,7 +20,7 @@ namespace paddle_mobile { namespace operators { template <> -bool ConvKernel::Init(const ConvParam ¶) const { +bool ConvKernel::Init(ConvParam *param) const { return true; } diff --git a/src/operators/kernel/fusion_fc_kernel.h b/src/operators/kernel/fusion_fc_kernel.h index c4e2b30176fb904d7fb906c5efc5137a5dcb8d59..149ddacf812ef01045fe3a2fc415e8c1cc8de661 100644 --- a/src/operators/kernel/fusion_fc_kernel.h +++ b/src/operators/kernel/fusion_fc_kernel.h @@ -28,7 +28,7 @@ class FusionFcKernel : public framework::OpKernelBase { public: void Compute(const FusionFcParam& param) const; - bool Init(const FusionFcParam& para) const; + bool Init(FusionFcParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/lrn_kernel.h b/src/operators/kernel/lrn_kernel.h index 40c48b3663c6825e03028439725c428ce048d254..5cd600a9474c74a4ebc5d07aae52c4ed245cb3b5 100644 --- a/src/operators/kernel/lrn_kernel.h +++ b/src/operators/kernel/lrn_kernel.h @@ -170,7 +170,7 @@ template class LrnKernel : public framework::OpKernelBase { public: void Compute(const LrnParam ¶m) const; - bool Init(const LrnParam ¶) const; + bool Init(LrnParam *param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/mali/batchnorm_kernel.cpp b/src/operators/kernel/mali/batchnorm_kernel.cpp index ff27afc71c42ed1c2b7e67eefbdadd86e92cc0fc..0447361c0bc20c2df28aca20df5f0f1fe61ff46a 100644 --- a/src/operators/kernel/mali/batchnorm_kernel.cpp +++ b/src/operators/kernel/mali/batchnorm_kernel.cpp @@ -128,7 +128,7 @@ class AclBatchNormOp : public acl::ACLOperator { }; template <> -bool BatchNormKernel::Init(const BatchNormParam& param) const { +bool BatchNormKernel::Init(BatchNormParam *param) const { AclBatchNormOp* acl_op = reinterpret_cast*>(this->GetAclOp()); if (acl_op == nullptr) { diff --git a/src/operators/kernel/mali/conv_kernel.cpp b/src/operators/kernel/mali/conv_kernel.cpp index f3212cae970b2a554412f59cf48a6e5156463969..687a53fbee7109345097952ad21bd84dc6f492ba 100644 --- a/src/operators/kernel/mali/conv_kernel.cpp +++ b/src/operators/kernel/mali/conv_kernel.cpp @@ -195,7 +195,7 @@ class AclConvOp : public acl::ACLOperator { }; template <> -bool ConvKernel::Init(const ConvParam& param) const { +bool ConvKernel::Init(ConvParam *param) const { AclConvOp* acl_op = reinterpret_cast*>(this->GetAclOp()); if (acl_op == nullptr) { diff --git a/src/operators/kernel/mul_kernel.h b/src/operators/kernel/mul_kernel.h index 81db202c2d26fae9abb971a2cafe32f9b20dfe22..596e9f85c1b87ce222a604e2ab393ee584b149dd 100644 --- a/src/operators/kernel/mul_kernel.h +++ b/src/operators/kernel/mul_kernel.h @@ -29,7 +29,7 @@ template class MulKernel : public framework::OpKernelBase { public: void Compute(const MulParam ¶m) const; - bool Init(const MulParam ¶) const; + bool Init(MulParam *param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/multiclass_nms_kernel.h b/src/operators/kernel/multiclass_nms_kernel.h index ca86604f2c6e550c219e54b6533c1500fb2912c4..545a152f435d849f7362bb2777d21355456cde1e 100644 --- a/src/operators/kernel/multiclass_nms_kernel.h +++ b/src/operators/kernel/multiclass_nms_kernel.h @@ -28,7 +28,7 @@ class MultiClassNMSKernel : public framework::OpKernelBase { public: void Compute(const MultiClassNMSParam& param) const; - bool Init(const MultiClassNMSParam& para) const; + bool Init(MultiClassNMSParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/pool_kernel.h b/src/operators/kernel/pool_kernel.h index 3285f56cc01fad554bff7e6a4d25769f8ef56d24..f69123bdf4d93321591dda5b73c944d97afae3a9 100644 --- a/src/operators/kernel/pool_kernel.h +++ b/src/operators/kernel/pool_kernel.h @@ -28,7 +28,7 @@ template class PoolKernel : public OpKernelBase { public: void Compute(const PoolParam ¶m) const override; - bool Init(const PoolParam ¶) const; + bool Init(PoolParam *param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/prior_box_kernel.h b/src/operators/kernel/prior_box_kernel.h index 79fc630b8efb50dec1ff336d2b66d5094eaeb5a5..00681f4fc90517ab363ed79ec244b32a1a0f560f 100644 --- a/src/operators/kernel/prior_box_kernel.h +++ b/src/operators/kernel/prior_box_kernel.h @@ -55,7 +55,7 @@ class PriorBoxKernel : public framework::OpKernelBase { public: void Compute(const PriorBoxParam& param) const; - bool Init(const PriorBoxParam& para) const; + bool Init(PriorBoxParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/relu_kernel.h b/src/operators/kernel/relu_kernel.h index 2155c33811f553435e4a89b5b23533e2bd42db5d..8af8c1720a0ff60c689a9437667e2c099149fbf9 100644 --- a/src/operators/kernel/relu_kernel.h +++ b/src/operators/kernel/relu_kernel.h @@ -27,7 +27,7 @@ template class ReluKernel : public framework::OpKernelBase { public: void Compute(const ReluParam& param) const; - bool Init(const ReluParam& para) const; + bool Init(ReluParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/reshape_kernel.h b/src/operators/kernel/reshape_kernel.h index 364f5b0902c2661017f2e72520849836f64dd0bb..2350207b5f0d48b225ff41f7abc61a126d4832e9 100644 --- a/src/operators/kernel/reshape_kernel.h +++ b/src/operators/kernel/reshape_kernel.h @@ -71,7 +71,7 @@ template class ReshapeKernel : public framework::OpKernelBase { public: void Compute(const ReshapeParam& param) const; - bool Init(const ReshapeParam& para) const; + bool Init(ReshapeParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/sigmoid_kernel.h b/src/operators/kernel/sigmoid_kernel.h index e9eaae5ad867c6880db7346f9632ff37a92aaf66..a1ff0afcbfd91f42dbc935a6be1516021061f0c5 100644 --- a/src/operators/kernel/sigmoid_kernel.h +++ b/src/operators/kernel/sigmoid_kernel.h @@ -26,7 +26,7 @@ template class SigmoidKernel : public OpKernelBase { public: void Compute(const SigmoidParam& param) const override; - bool Init(const SigmoidParam& para) const; + bool Init(SigmoidParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/softmax_kernel.h b/src/operators/kernel/softmax_kernel.h index a7a7666e32ef1923a47d71d94c93e813a23028c5..81a3ab331a36d5a7bf6301febe0c52bdf61a559f 100644 --- a/src/operators/kernel/softmax_kernel.h +++ b/src/operators/kernel/softmax_kernel.h @@ -29,7 +29,7 @@ template class SoftmaxKernel : public OpKernelBase { public: void Compute(const SoftmaxParam ¶m) const override; - bool Init(const SoftmaxParam ¶) const; + bool Init(SoftmaxParam *param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/kernel/transpose_kernel.h b/src/operators/kernel/transpose_kernel.h index 6526d97df9863392f783841a784cb5df4e45f218..7fe661d5943e1f45fec169525468e165232d8f04 100644 --- a/src/operators/kernel/transpose_kernel.h +++ b/src/operators/kernel/transpose_kernel.h @@ -29,7 +29,7 @@ class TransposeKernel : public framework::OpKernelBase { public: void Compute(const TransposeParam& param) const; - bool Init(const TransposeParam& para) const; + bool Init(TransposeParam* param) const; }; } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/math/depthwiseconv3x3s1p1.cpp b/src/operators/math/depthwiseconv3x3s1p1.cpp index 88cac515201c114e83cb9e85b39a51fb3f8e7955..ae327c6885ddf98cf5c294b15666fd3c46342908 100644 --- a/src/operators/math/depthwiseconv3x3s1p1.cpp +++ b/src/operators/math/depthwiseconv3x3s1p1.cpp @@ -14,6 +14,7 @@ limitations under the License. */ #include "operators/math/depthwiseconv3x3s1p1.h" #include +#include namespace paddle_mobile { namespace operators { @@ -22,11 +23,14 @@ namespace math { using framework::Tensor; void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, - Tensor bias, bool if_bias) { + Tensor *bias, bool if_bias, Tensor *new_scale, + Tensor *new_bias, bool if_bn, bool if_relu) { const float *input_data = input->data(); const float *filter_data = filter.data(); float *output_data = output->data(); - const float *bias_data = bias.data(); + const float *bias_data = bias->data(); + const float *newscale_data = new_scale->data(); + const float *newbias_data = new_bias->data(); const int h = static_cast(input->dims()[2]); const int w = static_cast(input->dims()[3]); @@ -36,6 +40,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, const int c = static_cast(input->dims()[1]); const int hxw = h * w; float32x4_t vbias = vdupq_n_f32(0.0); + float32x4_t vnewbias = vdupq_n_f32(0.0); + float32x4_t vnewscale = vdupq_n_f32(1.0); + float32x4_t vzero = vdupq_n_f32(0); + for (int b = 0; b < batch_size; ++b) { const float *filter_data_tmp = filter_data; @@ -43,7 +51,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, if (if_bias) { vbias = vdupq_n_f32(bias_data[j]); } - + if (if_bn) { + vnewbias = vdupq_n_f32(newbias_data[j]); + vnewscale = vdupq_n_f32(newscale_data[j]); + } int l_mid = l - 2; // l=1->l_mid=-1,l=2->l_mid=0 float w00 = filter_data_tmp[0]; float w01 = filter_data_tmp[1]; @@ -55,34 +66,55 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, float w21 = filter_data_tmp[7]; float w22 = filter_data_tmp[8]; - output_data[0] = w11 * input_data[0] + w12 * input_data[1] + - w21 * input_data[l] + w22 * input_data[l + 1] + - bias_data[j]; - output_data[l - 1] = w10 * input_data[l - 2] + w11 * input_data[l - 1] + - w20 * input_data[2 * l - 2] + - w21 * input_data[2 * l - 1] + bias_data[j]; - output_data[(l - 1) * l] = - w01 * input_data[(l - 2) * l] + w02 * input_data[(l - 2) * l + 1] + - w11 * input_data[(l - 1) * l] + w12 * input_data[(l - 1) * l + 1] + - bias_data[j]; - output_data[l * l - 1] = w00 * input_data[(l - 2) * (l + 1)] + - w01 * input_data[(l - 2) * (l + 1) + 1] + - w10 * input_data[l * l - 2] + - w11 * input_data[l * l - 1] + bias_data[j]; + output_data[0] =(w11 * input_data[0] + w12 * input_data[1] + w21 * input_data[l] + + w22 * input_data[l + 1] + bias_data[j]) * + newscale_data[j] + + newbias_data[j]; + output_data[l - 1] = (w10 * input_data[l - 2] + w11 * input_data[l - 1] + + w20 * input_data[2 * l - 2] + + w21 * input_data[2 * l - 1] + bias_data[j]) * + newscale_data[j] + + newbias_data[j]; + output_data[(l - 1) * l] = + (w01 * input_data[(l - 2) * l] + w02 * input_data[(l - 2) * l + 1] + + w11 * input_data[(l - 1) * l] + w12 * input_data[(l - 1) * l + 1] + + bias_data[j]) * + newscale_data[j] + + newbias_data[j]; + output_data[l * l - 1] = (w00 * input_data[(l - 2) * (l + 1)] + + w01 * input_data[(l - 2) * (l + 1) + 1] + + w10 * input_data[l * l - 2] + + w11 * input_data[l * l - 1] + bias_data[j]) * + newscale_data[j] + + newbias_data[j]; + if(if_relu){ + output_data[0] = output_data[0] < 0 ? 0 : output_data[0]; + output_data[l-1] = output_data[l-1] < 0 ? 0 : output_data[l-1]; + output_data[(l-1)*l] = output_data[(l-1)*l] < 0 ? 0 : output_data[(l-1)*l]; + output_data[l * l - 1] = output_data[l * l - 1] < 0 ? 0 : output_data[l * l - 1]; + } for (int i = 1; i < l - 1; ++i) { output_data[i * l] = - w01 * input_data[i * l - l] + w02 * input_data[i * l - l + 1] + - w11 * input_data[i * l] + w12 * input_data[i * l + 1] + - w21 * input_data[i * l + l] + w22 * input_data[i * l + l + 1] + - bias_data[j]; - output_data[i * l + l - 1] = w00 * input_data[i * l + l - 1 - l - 1] + - w01 * input_data[i * l + l - 1 - l] + - w10 * input_data[i * l + l - 1 - 1] + - w11 * input_data[i * l + l - 1] + - w20 * input_data[i * l + l - 1 + l - 1] + - w21 * input_data[i * l + l - 1 + l] + - bias_data[j]; + (w01 * input_data[i * l - l] + w02 * input_data[i * l - l + 1] + + w11 * input_data[i * l] + w12 * input_data[i * l + 1] + + w21 * input_data[i * l + l] + w22 * input_data[i * l + l + 1] + + bias_data[j]) * + newscale_data[j] + + newbias_data[j]; + output_data[i * l + l - 1] = + (w00 * input_data[i * l + l - 1 - l - 1] + + w01 * input_data[i * l + l - 1 - l] + + w10 * input_data[i * l + l - 1 - 1] + + w11 * input_data[i * l + l - 1] + + w20 * input_data[i * l + l - 1 + l - 1] + + w21 * input_data[i * l + l - 1 + l] + bias_data[j]) * + newscale_data[j] + + newbias_data[j]; + if(if_relu){ + output_data[i * l] = output_data[i * l] < 0 ? 0 : output_data[i * l]; + output_data[i * l + l - 1] = output_data[i * l + l - 1] < 0 ? 0 : output_data[i * l + l - 1]; + } } // top 1 row and bottom 1 row @@ -114,7 +146,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, out0 = vmlaq_n_f32(out0, tmp2, w21); out0 = vmlaq_n_f32(out0, tmp3, w22); out0 = vaddq_f32(out0, vbias); - + out0 = vmlaq_f32(vnewbias, vnewscale, out0); + if (if_relu) { + out0 = vmaxq_f32(out0, vzero); + } vst1q_f32(output_ptr, out0); in5 = vld1q_f32(input_tmp_end + 4); @@ -132,7 +167,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, out0 = vmlaq_n_f32(out0, tmp2, w11); out0 = vmlaq_n_f32(out0, tmp3, w12); out0 = vaddq_f32(out0, vbias); - + out0 = vmlaq_f32(vnewbias, vnewscale, out0); + if (if_relu) { + out0 = vmaxq_f32(out0, vzero); + } vst1q_f32(output_ptr + (l - 1) * l, out0); // can optimize to each 8 stride. @@ -161,7 +199,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, out0 = vmlaq_n_f32(out0, tmp2, w21); out0 = vmlaq_n_f32(out0, tmp3, w22); out0 = vaddq_f32(out0, vbias); - + out0 = vmlaq_f32(vnewbias, vnewscale, out0); + if (if_relu) { + out0 = vmaxq_f32(out0, vzero); + } for (int i = 0; i < c_mid; ++i) { if (i == 0) { vst1q_lane_f32(output_ptr + i, out0, 0); @@ -190,7 +231,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, out0 = vmlaq_n_f32(out0, tmp2, w11); out0 = vmlaq_n_f32(out0, tmp3, w12); out0 = vaddq_f32(out0, vbias); - + out0 = vmlaq_f32(vnewbias, vnewscale, out0); + if (if_relu) { + out0 = vmaxq_f32(out0, vzero); + } for (int i = 0; i < c_mid; ++i) { if (i == 0) { vst1q_lane_f32(output_ptr + (l - 1) * l + i, out0, 0); @@ -233,7 +277,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, out0 = vmlaq_n_f32(out0, tmp4, w21); out0 = vmlaq_n_f32(out0, tmp5, w22); out0 = vaddq_f32(out0, vbias); - + out0 = vmlaq_f32(vnewbias, vnewscale, out0); + if (if_relu) { + out0 = vmaxq_f32(out0, vzero); + } vst1q_f32(output_ptr, out0); output_ptr += 4; @@ -264,7 +311,10 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, out0 = vmlaq_n_f32(out0, tmp4, w21); out0 = vmlaq_n_f32(out0, tmp5, w22); out0 = vaddq_f32(out0, vbias); - + out0 = vmlaq_f32(vnewbias, vnewscale, out0); + if (if_relu) { + out0 = vmaxq_f32(out0, vzero); + } for (int i = 0; i < c_mid; ++i) { if (i == 0) { vst1q_lane_f32(output_ptr + i, out0, 0); @@ -282,6 +332,7 @@ void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, filter_data_tmp += 9; } } + } } // namespace math } // namespace operators diff --git a/src/operators/math/depthwiseconv3x3s1p1.h b/src/operators/math/depthwiseconv3x3s1p1.h index 019237a43192f30dfb70fe85e6b16a835cba4eba..85b38a69fc52b28efe71f72c1dc618c7541185bc 100644 --- a/src/operators/math/depthwiseconv3x3s1p1.h +++ b/src/operators/math/depthwiseconv3x3s1p1.h @@ -21,7 +21,8 @@ namespace math { using framework::Tensor; void DepthwiseConv3x3s1p1(const Tensor *input, Tensor filter, Tensor *output, - Tensor bias, bool if_bias); + Tensor *bias, bool if_bias, Tensor *new_scale, + Tensor *new_bias, bool if_bn, bool if_relu); } // namespace math } // namespace operators } // namespace paddle_mobile diff --git a/src/operators/op_param.h b/src/operators/op_param.h index ad7de0ee44db3a727ec06d5fabfca203226215f4..7374d65daf4aca9025a46d7ec04d931d67067656 100644 --- a/src/operators/op_param.h +++ b/src/operators/op_param.h @@ -823,6 +823,10 @@ class FusionConvAddParam : public OpParam { const int &Groups() const { return groups; } + void Set(Tensor *t) {t_ = t;} + + const Tensor *Get() const {return t_;} + protected: Tensor *bias_; int axis_; @@ -833,6 +837,7 @@ class FusionConvAddParam : public OpParam { vector paddings_; vector dilations_; int groups; + Tensor *t_; }; Print &operator<<(Print &printer, const FusionConvAddParam &conv_param); @@ -848,5 +853,91 @@ class FusionConvAddReluParam : public FusionConvAddParam { }; #endif +#ifdef FUSION_CONVADDBNRELU_OP +class FusionConvAddBNReluParam : public OpParam { + public: + FusionConvAddBNReluParam(const VariableNameMap &inputs, + const VariableNameMap &outputs, + const AttributeMap &attrs, const Scope &scope) { + bias_ = InputYFrom(inputs, scope); + axis_ = GetAttr("axis", attrs); + filter_ = FilterFrom(inputs, scope); + input_ = InputFrom(inputs, scope); + output_ = OutFrom(outputs, scope); + strides_ = GetAttr>("strides", attrs); + paddings_ = GetAttr>("paddings", attrs); + dilations_ = GetAttr>("dilations", attrs); + groups = GetAttr("groups", attrs); + input_bias_ = InputBiasFrom(inputs, scope); + input_mean_ = InputMeanFrom(inputs, scope); + input_scale_ = InputScaleFrom(inputs, scope); + input_variance_ = InputVarianceFrom(inputs, scope); + epsilon_ = GetAttr("epsilon", attrs); + momentum_ = GetAttr("momentum", attrs); + is_test_ = GetAttr("is_test", attrs); + } + Tensor *Bias() const { return bias_; } + + const int &Axis() const { return axis_; } + + const Tensor *Input() const { return input_; } + + const Tensor *Filter() const { return filter_; } + + Tensor *Output() const { return output_; } + + const vector &Strides() const { return strides_; } + + const vector &Paddings() const { return paddings_; } + + const vector &Dilations() const { return dilations_; } + + const int &Groups() const { return groups; } + + const Tensor *InputBias() const { return input_bias_; } + + const Tensor *InputMean() const { return input_mean_; } + + const Tensor *InputScale() const { return input_scale_; } + + const Tensor *InputVariance() const { return input_variance_; } + + const float &Epsilon() const { return epsilon_; } + + const float &Momentum() const { return momentum_; } + + const bool &IsTest() const { return is_test_; } + + void SetNewScale(Tensor *new_scale) { new_scale_ = new_scale; } + + void SetNewBias(Tensor *new_bias) { new_bias_ = new_bias; } + + const Tensor *NewScale() const { return new_scale_; } + + const Tensor *NewBias() const { return new_bias_; } + + protected: + Tensor *bias_; + int axis_; + Tensor *input_; + Tensor *output_; + Tensor *filter_; + vector strides_; + vector paddings_; + vector dilations_; + int groups; + Tensor *input_bias_; + Tensor *input_mean_; + Tensor *input_scale_; + Tensor *input_variance_; + float epsilon_; + float momentum_; + bool is_test_; + Tensor *new_bias_; + Tensor *new_scale_; +}; + +Print &operator<<(Print &printer, const FusionConvAddParam &conv_param); +#endif } // namespace operators } // namespace paddle_mobile diff --git a/tools/op.cmake b/tools/op.cmake index 2eabac925f6021448243b3668c22cbcaebe2f1d9..c4346c21f9dade37df76841abb3b4446e8f79e91 100644 --- a/tools/op.cmake +++ b/tools/op.cmake @@ -22,6 +22,7 @@ elseif (NET EQUAL "mobilenet") set(BATCHNORM_OP ON) set(POOL_OP ON) set(RESHAPE_OP ON) + set(FUSION_CONVADDBNRELU_OP) elseif (NET EQUAL "yolo") set(BATCHNORM_OP ON) set(CONV_OP ON) @@ -64,6 +65,8 @@ else () set(SOFTMAX_OP ON) set(TRANSPOSE_OP ON) set(FUSION_CONVADD_RELU_OP ON) + set(FUSION_CONVADDBNRELU_OP ON) + # option(BATCHNORM_OP "" ON) # option(BOXCODER_OP "" ON) # option(CONCAT_OP "" ON)