From 68d2450747ec046a43fd0917ace7560af44c9825 Mon Sep 17 00:00:00 2001 From: hjchen2 Date: Wed, 20 Feb 2019 15:59:17 +0800 Subject: [PATCH] add pad2d op, and fix bugs --- src/common/types.cpp | 5 +- src/common/types.h | 2 + src/framework/executor.cpp | 60 +++++++++-------------- src/framework/load_ops.h | 3 ++ src/operators/kernel/arm/pad2d_kernel.cpp | 45 +++++++++++++++++ src/operators/kernel/pad2d_kernel.h | 52 ++++++++++++++++++++ src/operators/pad2d.cpp | 42 ++++++++++++++++ src/operators/pad2d.h | 32 ++++++++++++ tools/op.cmake | 4 ++ 9 files changed, 208 insertions(+), 37 deletions(-) create mode 100644 src/operators/kernel/arm/pad2d_kernel.cpp create mode 100644 src/operators/kernel/pad2d_kernel.h create mode 100644 src/operators/pad2d.cpp create mode 100644 src/operators/pad2d.h diff --git a/src/common/types.cpp b/src/common/types.cpp index 93e3ee516a..c812917e9e 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -112,6 +112,8 @@ const char *G_OP_TYPE_GENERATE_PROPOSALS = "generate_proposals"; const char *G_OP_TYPE_PSROI_POOL = "psroi_pool"; const char *G_OP_TYPE_ROI_PERSPECTIVE = "roi_perspective_transform"; +const char *G_OP_TYPE_PAD2D = "pad2d"; + std::unordered_map< std::string, std::pair, std::vector>> op_input_output_key = { @@ -210,5 +212,6 @@ std::unordered_map< {{"Scores", "BboxDeltas", "ImInfo", "Anchors", "Variances"}, {"RpnRois", "RpnRoiProbs"}}}, {G_OP_TYPE_PSROI_POOL, {{"X", "ROIs"}, {"Out"}}}, - {G_OP_TYPE_ROI_PERSPECTIVE, {{"X", "ROIs"}, {"Out"}}}}; + {G_OP_TYPE_ROI_PERSPECTIVE, {{"X", "ROIs"}, {"Out"}}}, + {G_OP_TYPE_PAD2D, {{"X"}, {"Out"}}}}; } // namespace paddle_mobile diff --git a/src/common/types.h b/src/common/types.h index 9c189d5921..b55b88ea68 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -200,6 +200,8 @@ extern const char *G_OP_TYPE_GENERATE_PROPOSALS; extern const char *G_OP_TYPE_PSROI_POOL; extern const char *G_OP_TYPE_ROI_PERSPECTIVE; +extern const char *G_OP_TYPE_PAD2D; + extern std::unordered_map< std::string, std::pair, std::vector>> op_input_output_key; diff --git a/src/framework/executor.cpp b/src/framework/executor.cpp index 203effd03d..e9438dcc06 100644 --- a/src/framework/executor.cpp +++ b/src/framework/executor.cpp @@ -378,18 +378,14 @@ std::vector Executor::Predict(const std::vector &input, template void Executor::SetInput(const Tensor &input, const std::string &var_name) { - framework::LoDTensor *target = nullptr; + int index = 0; if (feed_indices_.find(var_name) != feed_indices_.end()) { - int index = feed_indices_.find(var_name)->second; - auto *feed_var = program_.scope->Var("feed"); - target = &( - feed_var->template GetMutable()->at(index)); - } else { - auto *target_var = program_.scope->FindVar(var_name); - PADDLE_MOBILE_ENFORCE(target_var != nullptr, "Variable %s is not exist", - var_name.c_str()); - target = target_var->template GetMutable(); + index = feed_indices_.find(var_name)->second; } + auto *feed_var = program_.scope->Var("feed"); + framework::LoDTensor &target = + feed_var->template GetMutable()->at(index); + if (config_.load_when_predict) { if (input_dim_last_ != input.dims()) { InitNoPersistableMemory(input); @@ -397,25 +393,21 @@ void Executor::SetInput(const Tensor &input, } } - target->Resize(input.dims()); - target->ShareDataWith(input); + target.Resize(input.dims()); + target.ShareDataWith(input); } template void Executor::SetInput(const LoDTensor &input, const std::string &var_name) { - framework::LoDTensor *target = nullptr; + int index = 0; if (feed_indices_.find(var_name) != feed_indices_.end()) { - int index = feed_indices_.find(var_name)->second; - auto *feed_var = program_.scope->Var("feed"); - target = &( - feed_var->template GetMutable()->at(index)); - } else { - auto *target_var = program_.scope->FindVar(var_name); - PADDLE_MOBILE_ENFORCE(target_var != nullptr, "Variable %s is not exist", - var_name.c_str()); - target = target_var->template GetMutable(); + index = feed_indices_.find(var_name)->second; } + auto *feed_var = program_.scope->Var("feed"); + framework::LoDTensor &target = + feed_var->template GetMutable()->at(index); + if (config_.load_when_predict) { if (input_dim_last_ != input.dims()) { InitNoPersistableMemory(input); @@ -423,27 +415,23 @@ void Executor::SetInput(const LoDTensor &input, } } - target->Resize(input.dims()); - target->ShareDataWith(input); - target->set_lod(input.lod()); + target.Resize(input.dims()); + target.ShareDataWith(input); + target.set_lod(input.lod()); } template std::shared_ptr Executor::GetOutput( const std::string &var_name) { - framework::LoDTensor *target = nullptr; + int index = 0; if (fetch_indices_.find(var_name) != fetch_indices_.end()) { - int index = fetch_indices_.find(var_name)->second; - auto *fetch_var = program_.scope->Var("fetch"); - target = &( - fetch_var->template GetMutable()->at(index)); - } else { - auto *target_var = program_.scope->FindVar(var_name); - PADDLE_MOBILE_ENFORCE(target_var != nullptr, "Variable %s is not exist", - var_name.c_str()); - target = target_var->template GetMutable(); + index = fetch_indices_.find(var_name)->second; } - return std::make_shared(*target); + auto *fetch_var = program_.scope->Var("fetch"); + framework::LoDTensor &target = + fetch_var->template GetMutable()->at(index); + + return std::make_shared(target); } template diff --git a/src/framework/load_ops.h b/src/framework/load_ops.h index 95524a7676..7c14b81f68 100644 --- a/src/framework/load_ops.h +++ b/src/framework/load_ops.h @@ -327,3 +327,6 @@ LOAD_OP1(roi_perspective_transform, CPU); #ifdef BEAM_SEARCH_DECODE_OP LOAD_OP1(beam_search_decode, CPU); #endif +#ifdef PAD2D_OP +LOAD_OP1(pad2d, CPU); +#endif diff --git a/src/operators/kernel/arm/pad2d_kernel.cpp b/src/operators/kernel/arm/pad2d_kernel.cpp new file mode 100644 index 0000000000..6e2505316f --- /dev/null +++ b/src/operators/kernel/arm/pad2d_kernel.cpp @@ -0,0 +1,45 @@ +/* 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 PAD2D_OP + +#include "operators/kernel/pad2d_kernel.h" +#include "operators/math/pad.h" + +namespace paddle_mobile { +namespace operators { + +template <> +bool Pad2DKernel::Init(Pad2DParam *param) { + return true; +} + +template <> +void Pad2DKernel::Compute(const Pad2DParam ¶m) { + const auto *input = param.input_; + auto *output = param.output_; + const auto &paddings = param.paddings_; + // if (param.mode_ == "constant" && param.pad_value_ == 0) { + math::PadFunctor pad; + pad(*input, paddings[0], paddings[1], paddings[2], paddings[3], output); + // } else { + // PADDLE_MOBILE_THROW_EXCEPTION("Pad2D has not been implemented."); + // } + output->set_lod(input->lod()); +} + +} // namespace operators +} // namespace paddle_mobile + +#endif // PAD2D_OP diff --git a/src/operators/kernel/pad2d_kernel.h b/src/operators/kernel/pad2d_kernel.h new file mode 100644 index 0000000000..4959346b56 --- /dev/null +++ b/src/operators/kernel/pad2d_kernel.h @@ -0,0 +1,52 @@ +/* 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 PAD2D_OP + +#pragma once + +#include +#include +#include "framework/operator.h" +#include "operators/op_param.h" + +namespace paddle_mobile { +namespace operators { + +template +class Pad2DParam : public OpParam { + public: + Pad2DParam(const VariableNameMap &inputs, const VariableNameMap &outputs, + const AttributeMap &attrs, const Scope &scope) { + input_ = OpParam::GetVarValue("X", inputs, scope); + output_ = OpParam::GetVarValue("Out", outputs, scope); + paddings_ = OpParam::GetAttr>("paddings", attrs); + pad_value_ = OpParam::GetAttr("pad_value", attrs); + mode_ = OpParam::GetStringAttr("mode", attrs); + } + + public: + framework::LoDTensor *input_; + framework::LoDTensor *output_; + std::vector paddings_; + float pad_value_; + std::string mode_; +}; + +DECLARE_KERNEL(Pad2D, Pad2DParam); + +} // namespace operators +} // namespace paddle_mobile + +#endif // PAD2D_OP diff --git a/src/operators/pad2d.cpp b/src/operators/pad2d.cpp new file mode 100644 index 0000000000..886c53612d --- /dev/null +++ b/src/operators/pad2d.cpp @@ -0,0 +1,42 @@ +/* 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 PAD2D_OP + +#include "operators/pad2d.h" + +namespace paddle_mobile { +namespace operators { + +template +void Pad2DOp::InferShape() const { + auto input_dims = this->param_.input_->dims(); + const auto &paddings = this->param_.paddings_; + PADDLE_MOBILE_ENFORCE(paddings.size() == 4, + "Size of paddings should be equal to 4."); + + input_dims[2] += paddings[0] + paddings[1]; + input_dims[3] += paddings[2] + paddings[3]; + this->param_.output_->Resize(input_dims); +} + +} // namespace operators +} // namespace paddle_mobile + +namespace ops = paddle_mobile::operators; +#ifdef PADDLE_MOBILE_CPU +REGISTER_OPERATOR_CPU(pad2d, ops::Pad2DOp); +#endif + +#endif // PAD2D_OP diff --git a/src/operators/pad2d.h b/src/operators/pad2d.h new file mode 100644 index 0000000000..1a80cbac40 --- /dev/null +++ b/src/operators/pad2d.h @@ -0,0 +1,32 @@ +/* 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 PAD2D_OP + +#pragma once + +#include +#include "framework/operator.h" +#include "operators/kernel/pad2d_kernel.h" +#include "operators/op_param.h" + +namespace paddle_mobile { +namespace operators { + +DECLARE_OPERATOR(Pad2D, Pad2DParam, Pad2DKernel); + +} // namespace operators +} // namespace paddle_mobile + +#endif // PAD2D_OP diff --git a/tools/op.cmake b/tools/op.cmake index a7d79a71d1..a362f96858 100644 --- a/tools/op.cmake +++ b/tools/op.cmake @@ -300,6 +300,7 @@ if(NOT FOUND_MATCH) set(PSROI_POOL_OP ON) set(ROI_PERSPECTIVE_OP ON) set(BEAM_SEARCH_DECODE_OP ON) + set(PAD2D_OP ON) endif() # option(BATCHNORM_OP "" ON) @@ -606,3 +607,6 @@ endif() if (BEAM_SEARCH_DECODE_OP) add_definitions(-DBEAM_SEARCH_DECODE_OP) endif() +if (PAD2D_OP) + add_definitions(-DPAD2D_OP) +endif() -- GitLab