diff --git a/lite/core/kernel.h b/lite/core/kernel.h index 9fffcc60012060327612345528c705bcf7722f17..361d014acc512dc2a46061f86efa83e1e1845807 100644 --- a/lite/core/kernel.h +++ b/lite/core/kernel.h @@ -66,7 +66,7 @@ class KernelBase { virtual void SetProfileRuntimeKernelInfo( paddle::lite::profile::OpCharacter* ch) { ch->kernel_func_name = std::string("NotImpl"); -#ifdef LITE_WITH_ARM +#ifdef LITE_WITH_OPENCL ch->cl_event = event_; #endif } diff --git a/lite/kernels/arm/CMakeLists.txt b/lite/kernels/arm/CMakeLists.txt index 218ee3f053fcf49f6a08ffbe0d780509f9b2cc03..bdc8e65cb53633ca9d0912274a633499aca109cf 100644 --- a/lite/kernels/arm/CMakeLists.txt +++ b/lite/kernels/arm/CMakeLists.txt @@ -42,6 +42,7 @@ add_kernel(cast_compute_arm ARM basic SRCS cast_compute.cc DEPS ${lite_kernel_de add_kernel(reduce_mean_compute_arm ARM basic SRCS reduce_mean_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(stack_compute_arm ARM basic SRCS stack_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(affine_channel_compute_arm ARM basic SRCS affine_channel_compute.cc DEPS ${lite_kernel_deps} math_arm) +add_kernel(affine_grid_compute_arm ARM basic SRCS affine_grid_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(range_compute_arm ARM basic SRCS range_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(dropout_compute_arm ARM basic SRCS dropout_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(layout_compute_arm ARM basic SRCS layout_compute.cc DEPS ${lite_kernel_deps} math_arm) diff --git a/lite/kernels/arm/affine_grid_compute.cc b/lite/kernels/arm/affine_grid_compute.cc new file mode 100644 index 0000000000000000000000000000000000000000..1ea5512bf3a3e9944855b36277784b6a06e050bb --- /dev/null +++ b/lite/kernels/arm/affine_grid_compute.cc @@ -0,0 +1,123 @@ +// Copyright (c) 2019 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. + +#include "lite/kernels/arm/affine_grid_compute.h" +#include +#include +#include "lite/backends/arm/math/funcs.h" +#include "lite/backends/arm/math/sgemm.h" +#include "lite/core/op_registry.h" +#include "lite/core/tensor.h" +#include "lite/core/type_system.h" + +namespace paddle { +namespace lite { +namespace kernels { +namespace arm { + +void AffineGridCompute::PrepareForRun() { + auto& param = Param(); + auto& ctx = this->ctx_->template As(); + + const lite::Tensor* x = param.X; + const float* din = x->data(); + lite::Tensor* out = param.Out; + float* dout = param.Out->mutable_data(); + int N = x->dims()[0]; + int H = param.output_shape[2]; + int W = param.output_shape[3]; + + vh = reinterpret_cast(malloc(sizeof(float) * H)); + vw = reinterpret_cast(malloc(sizeof(float) * W)); + int out_size = H * W * 3; + float scale = 2 / (static_cast(H) - 1); + for (int i = 0; i < H; i++) { + vh[i] = -1 + scale * i; + } + scale = 2 / (static_cast(W) - 1); + for (int i = 0; i < W; i++) { + vw[i] = -1 + i * scale; + } + return; +} +void AffineGridCompute::Run() { + auto& param = Param(); + auto& ctx = this->ctx_->template As(); + + const lite::Tensor* x = param.X; + int N = x->dims()[0]; + + int H = param.output_shape[2]; + int W = param.output_shape[3]; + int out_size = H * W * 3; + float* hw3 = ctx.workspace_data() + ctx.llc_size() / sizeof(float); + + for (int i = 0; i < out_size; i += 3) { + hw3[i] = 1; + hw3[i + 1] = 1; + hw3[i + 2] = 1; + } + + for (int i = 0; i < H * W; i++) { + hw3[i * 3 + 1] = vh[i / W]; + } + for (int i = 0; i < H * W; i++) { + hw3[i * 3] = vw[i % W]; + } + + const float* din = x->data(); + float* dout = param.Out->mutable_data(); + float* tmp = dout; + operators::ActivationParam act_param; + act_param.has_active = false; + for (int i = 0; i < N; i++) { + lite::arm::math::sgemm(false, + true, + H * W, + 2, + 3, + 1.f, + hw3, + 3, + din, + 3, + 0.f, + dout, + 2, + nullptr, + false, + act_param, + &ctx); + + din += 6; + dout += H * W * 2; + } + + return; +} + +} // namespace arm +} // namespace kernels +} // namespace lite +} // namespace paddle + +REGISTER_LITE_KERNEL(affine_grid, + kARM, + kFloat, + kNCHW, + paddle::lite::kernels::arm::AffineGridCompute, + def) + .BindInput("Theta", {LiteType::GetTensorTy(TARGET(kARM))}) + .BindOutput("Output", {LiteType::GetTensorTy(TARGET(kARM))}) + .Finalize(); diff --git a/lite/kernels/arm/affine_grid_compute.h b/lite/kernels/arm/affine_grid_compute.h new file mode 100644 index 0000000000000000000000000000000000000000..09f0e1f85c88acc2f70f0ca12f942c560b61a722 --- /dev/null +++ b/lite/kernels/arm/affine_grid_compute.h @@ -0,0 +1,39 @@ +// Copyright (c) 2019 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. + +#pragma once +#include +#include "lite/core/kernel.h" + +namespace paddle { +namespace lite { +namespace kernels { +namespace arm { + +class AffineGridCompute : public KernelLite { + public: + using param_t = operators::AffineGridParam; + void PrepareForRun() override; + + void Run() override; + + virtual ~AffineGridCompute() = default; + float* vh; + float* vw; +}; + +} // namespace arm +} // namespace kernels +} // namespace lite +} // namespace paddle diff --git a/lite/operators/CMakeLists.txt b/lite/operators/CMakeLists.txt index fbf4eda46d54164dd1b2db3465da20465caa6244..7a62ad980560d46bf425dc41fd3b035ee693a7f8 100644 --- a/lite/operators/CMakeLists.txt +++ b/lite/operators/CMakeLists.txt @@ -39,6 +39,7 @@ add_operator(unsqueeze_op_lite basic SRCS unsqueeze_op.cc DEPS ${op_DEPS}) add_operator(stack_op basic SRCS stack_op.cc DEPS ${op_DEPS}) add_operator(cast_op_lite basic SRCS cast_op.cc DEPS ${op_DEPS}) add_operator(affine_channel_op basic SRCS affine_channel_op.cc DEPS ${op_DEPS}) +add_operator(affine_grid_op basic SRCS affine_grid_op.cc DEPS ${op_DEPS}) add_operator(range_op basic SRCS range_op.cc DEPS ${op_DEPS}) add_operator(reduce_mean_op basic SRCS reduce_mean_op.cc DEPS ${op_DEPS}) add_operator(relu_op basic SRCS relu_op.cc DEPS ${op_DEPS}) diff --git a/lite/operators/affine_grid_op.cc b/lite/operators/affine_grid_op.cc new file mode 100644 index 0000000000000000000000000000000000000000..22c6b531ae2f4db4136c842720edf56e41900157 --- /dev/null +++ b/lite/operators/affine_grid_op.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2019 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. + +#include "lite/operators/affine_grid_op.h" +#include +#include "lite/core/op_lite.h" +#include "lite/core/op_registry.h" + +namespace paddle { +namespace lite { +namespace operators { + +bool AffineGridOpLite::CheckShape() const { + CHECK_OR_FALSE(param_.X); + CHECK_OR_FALSE(param_.Out); + + const auto x_dims = param_.X->dims(); + + CHECK_OR_FALSE(x_dims.size() == 3); + CHECK_OR_FALSE(x_dims[1] == 2 && x_dims[2] == 3); + + if (param_.output_shape.size() != 0) { + CHECK_OR_FALSE(param_.output_shape.size() == 4); + } + return true; +} + +bool AffineGridOpLite::InferShapeImpl() const { + int N = param_.X->dims()[0]; + int H, W; + if (param_.output_shape.size() == 0) { + const auto out_shape = param_.OutputShape->dims(); + H = out_shape[2]; + W = out_shape[3]; + + } else { + H = param_.output_shape[2]; + W = param_.output_shape[3]; + } + param_.Out->Resize(std::vector({N, H, W, 2})); + + return true; +} + +bool AffineGridOpLite::AttachImpl(const cpp::OpDesc &op_desc, + lite::Scope *scope) { + auto x = op_desc.Input("Theta").front(); + auto output = op_desc.Output("Output").front(); + + param_.X = scope->FindVar(x)->GetMutable(); + param_.output_shape = op_desc.GetAttr>("output_shape"); + + param_.Out = scope->FindVar(output)->GetMutable(); + + return true; +} + +} // namespace operators +} // namespace lite +} // namespace paddle + +REGISTER_LITE_OP(affine_grid, paddle::lite::operators::AffineGridOpLite); diff --git a/lite/operators/affine_grid_op.h b/lite/operators/affine_grid_op.h new file mode 100644 index 0000000000000000000000000000000000000000..a94eb3d122b74b4e42d8714f284e478e6fb053f6 --- /dev/null +++ b/lite/operators/affine_grid_op.h @@ -0,0 +1,48 @@ +// Copyright (c) 2019 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. + +#pragma once +#include +#include "lite/core/op_lite.h" +#include "lite/core/scope.h" +#include "lite/operators/op_params.h" +#include "lite/utils/all.h" + +namespace paddle { +namespace lite { +namespace operators { + +class AffineGridOpLite : public OpLite { + public: + AffineGridOpLite() {} + + explicit AffineGridOpLite(const std::string &op_type) : OpLite(op_type) {} + + bool CheckShape() const override; + + bool InferShapeImpl() const override; + + bool AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) override; + + void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); } + + std::string DebugString() const override { return "affine_grid"; } + + private: + mutable AffineGridParam param_; +}; + +} // namespace operators +} // namespace lite +} // namespace paddle diff --git a/lite/operators/op_params.h b/lite/operators/op_params.h index de08da1326e4be94137199b59f64659dda1ff7f1..57cfbefa49eb675e882b4420df1b65f0dbc03eac 100644 --- a/lite/operators/op_params.h +++ b/lite/operators/op_params.h @@ -1166,6 +1166,13 @@ struct AffineChannelParam : ParamBase { lite::Tensor* Out{}; }; +struct AffineGridParam : ParamBase { + const lite::Tensor* X{}; // Theta:shape {?, 2, 3} + std::vector output_shape; + const lite::Tensor* OutputShape; + lite::Tensor* Out{}; +}; + struct AnchorGeneratorParam : ParamBase { const lite::Tensor* Input{}; std::vector anchor_sizes{};