diff --git a/lite/kernels/arm/CMakeLists.txt b/lite/kernels/arm/CMakeLists.txt index c4b03b03072b36ff10d53f7da9a90b8ea5607818..1f9cd45d616bf0af753a4bfbda2e4cf8c79a78f5 100644 --- a/lite/kernels/arm/CMakeLists.txt +++ b/lite/kernels/arm/CMakeLists.txt @@ -40,7 +40,6 @@ add_kernel(box_coder_compute_arm ARM basic SRCS box_coder_compute.cc DEPS ${lite add_kernel(slice_compute_arm ARM basic SRCS slice_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(cast_compute_arm ARM basic SRCS cast_compute.cc DEPS ${lite_kernel_deps} math_arm) add_kernel(squeeze_compute_arm ARM basic SRCS squeeze_compute.cc DEPS ${lite_kernel_deps} math_arm) -add_kernel(unsqueeze_compute_arm ARM basic SRCS unsqueeze_compute.cc DEPS ${lite_kernel_deps} math_arm) 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) diff --git a/lite/kernels/arm/unsqueeze_compute.cc b/lite/kernels/arm/unsqueeze_compute.cc deleted file mode 100644 index 91c8c0423b6fcc5bade5751985f190b3395b0779..0000000000000000000000000000000000000000 --- a/lite/kernels/arm/unsqueeze_compute.cc +++ /dev/null @@ -1,78 +0,0 @@ -// 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/unsqueeze_compute.h" -#include - -namespace paddle { -namespace lite { -namespace kernels { -namespace host { - -void UnsqueezeCompute::Run() { - auto& param = Param(); - auto x = param.X; - auto output = param.Out; - auto x_dims = x->dims(); - auto* x_data = x->data(); - auto* out_data = output->mutable_data(); - memcpy(out_data, x_data, x_dims.production() * sizeof(float)); -} - -void Unsqueeze2Compute::Run() { - auto& param = Param(); - auto x = param.X; - auto output = param.Out; - auto xshape = param.XShape; - auto x_dims = x->dims(); - auto* x_data = x->data(); - auto* out_data = output->mutable_data(); - auto* xshape_data = xshape->mutable_data(); - memcpy(out_data, x_data, x_dims.production() * sizeof(float)); - memcpy(xshape_data, x_data, x_dims.production() * sizeof(float)); -} - -} // namespace host -} // namespace kernels -} // namespace lite -} // namespace paddle - -REGISTER_LITE_KERNEL(unsqueeze, - kARM, - kFloat, - kNCHW, - paddle::lite::kernels::host::UnsqueezeCompute, - def) - .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) - .BindInput("AxesTensor", - {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) - .BindInput("AxesTensorList", - {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) - .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) - .Finalize(); - -REGISTER_LITE_KERNEL(unsqueeze2, - kARM, - kFloat, - kNCHW, - paddle::lite::kernels::host::Unsqueeze2Compute, - def) - .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) - .BindInput("AxesTensor", - {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) - .BindInput("AxesTensorList", - {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))}) - .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))}) - .BindOutput("XShape", {LiteType::GetTensorTy(TARGET(kARM))}) - .Finalize(); diff --git a/lite/kernels/host/CMakeLists.txt b/lite/kernels/host/CMakeLists.txt index 078fad7aa0221a0e60b1f4dd928136b38f198dcb..4334ee220a814c65fe0141bd2a1bb5ebb7c0705e 100644 --- a/lite/kernels/host/CMakeLists.txt +++ b/lite/kernels/host/CMakeLists.txt @@ -3,6 +3,7 @@ message(STATUS "compile with lite host kernels") add_kernel(feed_compute_host Host basic SRCS feed_compute.cc DEPS ${lite_kernel_deps}) add_kernel(fetch_compute_host Host basic SRCS fetch_compute.cc DEPS ${lite_kernel_deps}) add_kernel(reshape_compute_host Host basic SRCS reshape_compute.cc DEPS ${lite_kernel_deps}) +add_kernel(unsqueeze_compute_host Host basic SRCS unsqueeze_compute.cc DEPS ${lite_kernel_deps}) add_kernel(multiclass_nms_compute_host Host basic SRCS multiclass_nms_compute.cc DEPS ${lite_kernel_deps}) add_kernel(expand_compute_host Host basic SRCS expand_compute.cc DEPS ${lite_kernel_deps}) add_kernel(shape_compute_host Host extra SRCS shape_compute.cc DEPS ${lite_kernel_deps}) diff --git a/lite/kernels/host/unsqueeze_compute.cc b/lite/kernels/host/unsqueeze_compute.cc new file mode 100644 index 0000000000000000000000000000000000000000..aa525880af890486401dba24c84d256447a5f225 --- /dev/null +++ b/lite/kernels/host/unsqueeze_compute.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2020 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/host/unsqueeze_compute.h" + +#include + +namespace paddle { +namespace lite { +namespace kernels { +namespace host { + +void UnsqueezeCompute::Run() { + auto& param = Param(); + auto x = param.X; + auto output = param.Out; + auto output_dims = output->dims(); + output->CopyDataFrom(*x); + output->Resize(output_dims); +} + +void Unsqueeze2Compute::Run() { + auto& param = Param(); + auto x = param.X; + auto output = param.Out; + auto xshape = param.XShape; + auto output_dims = output->dims(); + auto xshape_dims = xshape->dims(); + output->CopyDataFrom(*x); + xshape->CopyDataFrom(*x); + output->Resize(output_dims); + xshape->Resize(xshape_dims); +} + +} // namespace host +} // namespace kernels +} // namespace lite +} // namespace paddle + +REGISTER_LITE_KERNEL(unsqueeze, + kHost, + kAny, + kAny, + paddle::lite::kernels::host::UnsqueezeCompute, + def) + .BindInput("X", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)}) + .BindInput("AxesTensor", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kInt32), DATALAYOUT(kAny), -1)}) + .BindInput("AxesTensorList", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kInt32), DATALAYOUT(kAny), -1)}) + .BindOutput("Out", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)}) + .Finalize(); + +REGISTER_LITE_KERNEL(unsqueeze2, + kHost, + kAny, + kAny, + paddle::lite::kernels::host::Unsqueeze2Compute, + def) + .BindInput("X", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)}) + .BindInput("AxesTensor", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kInt32), DATALAYOUT(kAny), -1)}) + .BindInput("AxesTensorList", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kInt32), DATALAYOUT(kAny), -1)}) + .BindOutput("Out", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)}) + .BindOutput("XShape", + {LiteType::GetTensorTy( + TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny), -1)}) + .Finalize(); diff --git a/lite/kernels/arm/unsqueeze_compute.h b/lite/kernels/host/unsqueeze_compute.h similarity index 78% rename from lite/kernels/arm/unsqueeze_compute.h rename to lite/kernels/host/unsqueeze_compute.h index 57d4c657f682e130f8eab830222d9b0eeec8a367..64bdae8e5ba82e050ec8fd29802705ad01aa2e2a 100644 --- a/lite/kernels/arm/unsqueeze_compute.h +++ b/lite/kernels/host/unsqueeze_compute.h @@ -1,4 +1,4 @@ -// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +// Copyright (c) 2020 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. @@ -22,14 +22,16 @@ namespace lite { namespace kernels { namespace host { -class UnsqueezeCompute : public KernelLite { +class UnsqueezeCompute + : public KernelLite { public: void Run() override; virtual ~UnsqueezeCompute() = default; }; -class Unsqueeze2Compute : public KernelLite { +class Unsqueeze2Compute + : public KernelLite { public: void Run() override; diff --git a/lite/tests/kernels/squeeze_compute_test.cc b/lite/tests/kernels/squeeze_compute_test.cc index 36efe76978e348136e0677d87c63ef3f162513d9..30c56d532eaa9a3452f0f9233a2c5127bace358c 100644 --- a/lite/tests/kernels/squeeze_compute_test.cc +++ b/lite/tests/kernels/squeeze_compute_test.cc @@ -47,7 +47,7 @@ class SqueezeComputeTester : public arena::TestCase { bool should_squeeze[9] = {false}; if (num_squeeze_dims == 0) { - for (int idx = 0; idx < in_dims.size(); ++idx) { + for (size_t idx = 0; idx < in_dims.size(); ++idx) { if (in_dims[idx] == 1) { should_squeeze[idx] = true; ++cnt_squeezed_dims; @@ -71,7 +71,7 @@ class SqueezeComputeTester : public arena::TestCase { } std::vector output_shape(in_dims.size() - cnt_squeezed_dims, 0); - for (int in_idx = 0, out_idx = 0; in_idx < in_dims.size(); ++in_idx) { + for (size_t in_idx = 0, out_idx = 0; in_idx < in_dims.size(); ++in_idx) { if (!should_squeeze[in_idx]) { output_shape[out_idx++] = in_dims[in_idx]; } @@ -135,7 +135,7 @@ class Squeeze2ComputeTester : public arena::TestCase { bool should_squeeze[9] = {false}; if (num_squeeze_dims == 0) { - for (int idx = 0; idx < in_dims.size(); ++idx) { + for (size_t idx = 0; idx < in_dims.size(); ++idx) { if (in_dims[idx] == 1) { should_squeeze[idx] = true; ++cnt_squeezed_dims; @@ -159,7 +159,7 @@ class Squeeze2ComputeTester : public arena::TestCase { } std::vector output_shape(in_dims.size() - cnt_squeezed_dims, 0); - for (int in_idx = 0, out_idx = 0; in_idx < in_dims.size(); ++in_idx) { + for (size_t in_idx = 0, out_idx = 0; in_idx < in_dims.size(); ++in_idx) { if (!should_squeeze[in_idx]) { output_shape[out_idx++] = in_dims[in_idx]; } diff --git a/lite/tests/kernels/unsqueeze_compute_test.cc b/lite/tests/kernels/unsqueeze_compute_test.cc index 461ef7215e3ceb779b2522adbd5bb286036a0d8e..c59e732d7da4d8dc112720f61cd2c0b813309c2b 100644 --- a/lite/tests/kernels/unsqueeze_compute_test.cc +++ b/lite/tests/kernels/unsqueeze_compute_test.cc @@ -84,8 +84,7 @@ class UnsqueezeComputeTester : public arena::TestCase { output_shape[out_idx] = in_dims[in_idx++]; } } - for (size_t i = 0; i < output_shape.size(); ++i) - out->Resize(DDim(output_shape)); + out->Resize(DDim(output_shape)); auto* input_data = input->data(); auto* out_data = out->mutable_data(); memcpy(out_data, input_data, sizeof(float) * dims_.production()); @@ -258,22 +257,19 @@ void test_unsqueeze2(Place place, } } -TEST(squeeze, precision) { +TEST(unsqueeze, precision) { Place place; float abs_error = 2e-5; #ifdef LITE_WITH_NPU place = TARGET(kNPU); abs_error = 1e-2; // Using fp16 in NPU -#elif defined(LITE_WITH_ARM) - place = TARGET(kARM); #else - return; + place = TARGET(kHost); #endif - test_unsqueeze(place, abs_error); } -TEST(squeeze2, precision) { +TEST(unsqueeze2, precision) { Place place; float abs_error = 2e-5; std::vector ignored_outs = {}; @@ -281,10 +277,8 @@ TEST(squeeze2, precision) { place = TARGET(kNPU); abs_error = 1e-2; // Using fp16 in NPU ignored_outs.push_back("XShape"); // not supported out in NPU -#elif defined(LITE_WITH_ARM) - place = TARGET(kARM); #else - return; + place = TARGET(kHost); #endif test_unsqueeze2(place, abs_error, ignored_outs);