diff --git a/mobile/CMakeLists.txt b/mobile/CMakeLists.txt index 00a53035a118c0ee76025ab50790672c02391ae3..1883da85739f15ada96fead77a02b72b3bcceb6a 100644 --- a/mobile/CMakeLists.txt +++ b/mobile/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.0.0) if(IS_IOS) option(USE_OPENMP "build with openmp support" OFF) else() - option(USE_OPENMP "build with openmp support" ON) + option(USE_OPENMP "build with openmp support" OFF) endif() option(USE_EXCEPTION "build with exception" ON) option(WITH_LOGGING "print logging for debug" OFF) diff --git a/mobile/src/operators/kernel/cl/density_prior_box_kernel.cpp b/mobile/src/operators/kernel/cl/density_prior_box_kernel.cpp index 1cd1ff8de88fa13f08490e0fcc7c5b7e0c865849..1a5cf0f061606d82076ce0f231e03ba3b36753a0 100644 --- a/mobile/src/operators/kernel/cl/density_prior_box_kernel.cpp +++ b/mobile/src/operators/kernel/cl/density_prior_box_kernel.cpp @@ -68,7 +68,7 @@ void DensityPriorBoxKernel::Compute( auto output_boxes = param.OutputBoxes()->GetCLImage(); auto output_var = param.OutputVariances()->GetCLImage(); - auto new_deensity = param.getNewDensity()->GetCLImage(); + auto new_density = param.getNewDensity()->GetCLImage(); float step_w = param.StepW(); float step_h = param.StepH(); @@ -113,7 +113,7 @@ void DensityPriorBoxKernel::Compute( CL_CHECK_ERRORS(status); status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output_var); CL_CHECK_ERRORS(status); - status = clSetKernelArg(kernel, 2, sizeof(cl_mem), &new_deensity); + status = clSetKernelArg(kernel, 2, sizeof(cl_mem), &new_density); CL_CHECK_ERRORS(status); status = clSetKernelArg(kernel, 3, sizeof(float), &step_h); CL_CHECK_ERRORS(status); diff --git a/mobile/src/operators/kernel/prior_box_kernel.h b/mobile/src/operators/kernel/prior_box_kernel.h index 89bccebcee3e911410ec89ff2c4ae4ae48b2ac59..c5d561083d13f878b6b46ccd03d4ae3c4d1f233f 100644 --- a/mobile/src/operators/kernel/prior_box_kernel.h +++ b/mobile/src/operators/kernel/prior_box_kernel.h @@ -16,6 +16,7 @@ limitations under the License. */ #include #include +#include #include #include "framework/operator.h" #include "operators/math/transform.h" @@ -77,11 +78,7 @@ class DensityPriorBoxParam : public OpParam { densities_ = GetAttr>("densities", attrs); } - ~DensityPriorBoxParam() { - if (new_density) { - delete new_density; - } - } + ~DensityPriorBoxParam() {} const GType *Input() const { return input_; } const GType *InputImage() const { return input_image_; } @@ -96,8 +93,8 @@ class DensityPriorBoxParam : public OpParam { const vector &FixedRatios() const { return fixed_ratios_; } const vector &Densities() const { return densities_; } const vector &Variances() const { return variances_; } - GType *getNewDensity() const { return new_density; } - void setNewDensity(GType *newDensity) { new_density = newDensity; } + GType *getNewDensity() const { return new_density.get(); } + void setNewDensity(GType *newDensity) { new_density.reset(newDensity); } public: GType *input_; @@ -113,7 +110,7 @@ class DensityPriorBoxParam : public OpParam { vector fixed_ratios_; vector densities_; vector variances_; - GType *new_density; + std::shared_ptr new_density; }; DECLARE_KERNEL(DensityPriorBox, DensityPriorBoxParam); diff --git a/mobile/src/operators/op_param.h b/mobile/src/operators/op_param.h index ec803f0590e9c534e73514ae45a489d6aa777528..2c695dfe053a3557052a093a34cd8aeb6da88381 100644 --- a/mobile/src/operators/op_param.h +++ b/mobile/src/operators/op_param.h @@ -57,6 +57,21 @@ using std::vector; using framework::DtypeTensorTrait; +template +class CLImageDeleter { + typedef typename DtypeTensorTrait::gtype GType; + + public: + void operator()(GType *ptr) { +#ifdef PADDLE_MOBILE_CL + framework::CLImage *image = dynamic_cast(ptr); + if (image) { + delete image; + } +#endif + } +}; + class OpParam { public: OpParam(const VariableNameMap &inputs, const VariableNameMap &outputs, @@ -850,14 +865,7 @@ class BatchNormParam : public OpParam { // is_test_ = GetAttr("is_test", attrs); } - ~BatchNormParam() { - if (new_bias_) { - delete new_bias_; - } - if (new_scale_) { - delete new_scale_; - } - } + ~BatchNormParam() {} const GType *InputX() const { return input_x_; } @@ -879,13 +887,17 @@ class BatchNormParam : public OpParam { const string &DataFormat() const { return data_format_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } private: GType *input_x_; @@ -898,8 +910,8 @@ class BatchNormParam : public OpParam { float momentum_; bool is_test_; string data_format_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif @@ -2086,14 +2098,7 @@ class FusionConvAddBNReluParam : public ConvParam { this->output_ = OpParam::OutFrom(outputs, *scope); } - ~FusionConvAddBNReluParam() { - if (new_bias_) { - delete new_bias_; - } - if (new_scale_) { - delete new_scale_; - } - } + ~FusionConvAddBNReluParam() {} GType *Bias() const { return bias_; } @@ -2111,13 +2116,17 @@ class FusionConvAddBNReluParam : public ConvParam { const float &Momentum() const { return momentum_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } protected: GType *bias_; @@ -2128,8 +2137,8 @@ class FusionConvAddBNReluParam : public ConvParam { GType *input_variance_; float epsilon_; float momentum_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif @@ -2163,14 +2172,7 @@ class FusionConvBNAddReluParam : public ConvParam { this->output_ = OpParam::OutFrom(outputs, *scope); } - ~FusionConvBNAddReluParam() { - if (new_bias_) { - delete new_bias_; - } - if (new_scale_) { - delete new_scale_; - } - } + ~FusionConvBNAddReluParam() {} GType *Bias() const { return bias_; } const int &Axis() const { return axis_; } @@ -2187,13 +2189,17 @@ class FusionConvBNAddReluParam : public ConvParam { const float &Momentum() const { return momentum_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } protected: GType *bias_; @@ -2204,8 +2210,8 @@ class FusionConvBNAddReluParam : public ConvParam { GType *input_variance_; float epsilon_; float momentum_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; std::string keyBNY_; std::string keyX_; std::string keyY_; @@ -2244,13 +2250,17 @@ class FusionConvBNParam : public ConvParam { const float &Momentum() const { return momentum_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } protected: GType *input_bias_; @@ -2259,8 +2269,8 @@ class FusionConvBNParam : public ConvParam { GType *input_variance_; float epsilon_; float momentum_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif @@ -2301,13 +2311,17 @@ class FusionConvAddBNParam : public ConvParam { const float &Momentum() const { return momentum_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } protected: GType *bias_; @@ -2318,8 +2332,8 @@ class FusionConvAddBNParam : public ConvParam { GType *input_variance_; float epsilon_; float momentum_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif @@ -2343,14 +2357,7 @@ class FusionDWConvBNReluParam : public ConvParam { this->output_ = OpParam::OutFrom(outputs, *scope); } - ~FusionDWConvBNReluParam() { - if (new_bias_) { - delete new_bias_; - } - if (new_scale_) { - delete new_scale_; - } - } + ~FusionDWConvBNReluParam() {} const GType *InputBias() const { return input_bias_; } @@ -2364,13 +2371,17 @@ class FusionDWConvBNReluParam : public ConvParam { const float &Momentum() const { return momentum_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } protected: GType *input_bias_; @@ -2379,8 +2390,8 @@ class FusionDWConvBNReluParam : public ConvParam { GType *input_variance_; float epsilon_; float momentum_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif @@ -2421,14 +2432,7 @@ class FusionConvBNReluParam : public ConvParam { this->output_ = OpParam::OutFrom(outputs, *scope); } - ~FusionConvBNReluParam() { - if (new_bias_) { - delete new_bias_; - } - if (new_scale_) { - delete new_scale_; - } - } + ~FusionConvBNReluParam() {} const GType *InputBias() const { return input_bias_; } @@ -2442,13 +2446,17 @@ class FusionConvBNReluParam : public ConvParam { const float &Momentum() const { return momentum_; } - void SetNewScale(GType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(GType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(GType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(GType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const GType *NewScale() const { return new_scale_; } + const GType *NewScale() const { return new_scale_.get(); } - const GType *NewBias() const { return new_bias_; } + const GType *NewBias() const { return new_bias_.get(); } protected: GType *input_bias_; @@ -2457,8 +2465,8 @@ class FusionConvBNReluParam : public ConvParam { GType *input_variance_; float epsilon_; float momentum_; - GType *new_bias_; - GType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif @@ -2683,13 +2691,17 @@ class FusionDeconvAddBNParam : public ConvTransposeParam { const bool &IsTest() const { return is_test_; } - void SetNewScale(RType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(RType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(RType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(RType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const RType *NewScale() const { return new_scale_; } + const RType *NewScale() const { return new_scale_.get(); } - const RType *NewBias() const { return new_bias_; } + const RType *NewBias() const { return new_bias_.get(); } protected: RType *output_; @@ -2700,8 +2712,8 @@ class FusionDeconvAddBNParam : public ConvTransposeParam { float epsilon_; float momentum_; bool is_test_; - RType *new_bias_; - RType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif #ifdef FUSION_DECONVBNRELU_OP @@ -2739,13 +2751,17 @@ class FusionDeconvBNReluParam : public ConvTransposeParam { const bool &IsTest() const { return is_test_; } - void SetNewScale(RType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(RType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(RType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(RType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const RType *NewScale() const { return new_scale_; } + const RType *NewScale() const { return new_scale_.get(); } - const RType *NewBias() const { return new_bias_; } + const RType *NewBias() const { return new_bias_.get(); } protected: RType *output_; @@ -2756,8 +2772,8 @@ class FusionDeconvBNReluParam : public ConvTransposeParam { float epsilon_; float momentum_; bool is_test_; - RType *new_bias_; - RType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif #ifdef FUSION_DECONVADDBNRELU_OP @@ -2796,13 +2812,17 @@ class FusionDeconvAddBNReluParam : public ConvTransposeParam { const bool &IsTest() const { return is_test_; } - void SetNewScale(RType *new_scale) { new_scale_ = new_scale; } + void SetNewScale(RType *new_scale) { + new_scale_.reset(new_scale, CLImageDeleter()); + } - void SetNewBias(RType *new_bias) { new_bias_ = new_bias; } + void SetNewBias(RType *new_bias) { + new_bias_.reset(new_bias, CLImageDeleter()); + } - const RType *NewScale() const { return new_scale_; } + const RType *NewScale() const { return new_scale_.get(); } - const RType *NewBias() const { return new_bias_; } + const RType *NewBias() const { return new_bias_.get(); } protected: RType *output_; @@ -2813,8 +2833,8 @@ class FusionDeconvAddBNReluParam : public ConvTransposeParam { float epsilon_; float momentum_; bool is_test_; - RType *new_bias_; - RType *new_scale_; + std::shared_ptr new_bias_; + std::shared_ptr new_scale_; }; #endif