From 1337bd1961a415366c8e73584afab51ac1d75862 Mon Sep 17 00:00:00 2001 From: Jiaying Zhao Date: Wed, 12 Feb 2020 21:14:47 +0800 Subject: [PATCH] [LITE][OPENCL]Add scale kernel. (#2861) * [LITE][OPENCL]Add scale kernel. * [LITE][OPENCL]Add scale kernel, format code style. --- .../opencl/cl_kernel/image/scale_kernel.cl | 32 +++++ lite/kernels/opencl/CMakeLists.txt | 5 + lite/kernels/opencl/scale_compute.cc | 115 ++++++++++++++++ lite/kernels/opencl/scale_compute_test.cc | 124 ++++++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 lite/backends/opencl/cl_kernel/image/scale_kernel.cl create mode 100644 lite/kernels/opencl/scale_compute.cc create mode 100644 lite/kernels/opencl/scale_compute_test.cc diff --git a/lite/backends/opencl/cl_kernel/image/scale_kernel.cl b/lite/backends/opencl/cl_kernel/image/scale_kernel.cl new file mode 100644 index 0000000000..739ff13385 --- /dev/null +++ b/lite/backends/opencl/cl_kernel/image/scale_kernel.cl @@ -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. */ + +#include + +__kernel void scale(__read_only image2d_t input, + __write_only image2d_t output, + __private float scale, + __private float bias){ + + const int x = get_global_id(0); // image_width + const int y = get_global_id(1); // image_height + + const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | + CLK_ADDRESS_CLAMP | + CLK_FILTER_NEAREST; + + CL_DTYPE4 in = READ_IMG_TYPE(CL_DTYPE_CHAR, input, sampler, (int2)(x, y)); + in = convert_float(scale) * in + convert_float(bias); + WRITE_IMG_TYPE(CL_DTYPE_CHAR, output, (int2)(x, y), in); +} diff --git a/lite/kernels/opencl/CMakeLists.txt b/lite/kernels/opencl/CMakeLists.txt index 9074a503df..e81fdf307e 100644 --- a/lite/kernels/opencl/CMakeLists.txt +++ b/lite/kernels/opencl/CMakeLists.txt @@ -22,6 +22,7 @@ add_kernel(conv_opencl OPENCL basic SRCS conv_compute.cc DEPS ${cl_kernel_deps} add_kernel(layout_opencl OPENCL basic SRCS layout_compute.cc DEPS ${cl_kernel_deps}) add_kernel(concat_opencl OPENCL basic SRCS concat_compute.cc DEPS ${cl_kernel_deps}) add_kernel(nearest_interp_opencl OPENCL basic SRCS nearest_interp_compute.cc DEPS ${cl_kernel_deps}) +add_kernel(scale_opencl OPENCL basic SRCS scale_compute.cc DEPS ${cl_kernel_deps}) lite_cc_test(test_elementwise_add_opencl SRCS elementwise_add_compute_test.cc DEPS elementwise_add_opencl fusion_elementwise_add_activation_opencl op_registry program context @@ -92,3 +93,7 @@ lite_cc_test(test_concat_opencl SRCS concat_compute_test.cc lite_cc_test(test_nearest_interp_opencl SRCS nearest_interp_compute_test.cc DEPS nearest_interp_opencl layout_opencl op_registry program context cl_image_converter ARGS --cl_path=${CMAKE_SOURCE_DIR}/lite/backends/opencl) + +lite_cc_test(test_scale_opencl SRCS scale_compute_test.cc + DEPS scale_opencl op_registry program context + ARGS --cl_path=${CMAKE_SOURCE_DIR}/lite/backends/opencl) diff --git a/lite/kernels/opencl/scale_compute.cc b/lite/kernels/opencl/scale_compute.cc new file mode 100644 index 0000000000..6a7d4d4f61 --- /dev/null +++ b/lite/kernels/opencl/scale_compute.cc @@ -0,0 +1,115 @@ +// 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 +#include "lite/backends/opencl/cl_include.h" +#include "lite/core/kernel.h" +#include "lite/core/op_registry.h" +#include "lite/kernels/opencl/image_helper.h" +#include "lite/operators/op_params.h" +#include "lite/utils/replace_stl/stream.h" +#include "lite/utils/string.h" + +namespace paddle { +namespace lite { +namespace kernels { +namespace opencl { + +class ScaleComputeImage2D : public KernelLite { + public: + using param_t = operators::ScaleParam; + + std::string doc() const override { return "Scale using cl::Image2D, kFloat"; } + + void PrepareForRun() override { + auto& context = ctx_->As(); + context.cl_context()->AddKernel( + kernel_func_name_, "image/scale_kernel.cl", build_options_); + } + + void Run() override { + const auto& param = *param_.get_mutable(); + const auto& in_dims = param.x->dims(); + auto* x_img = param.x->data(); + const float scale = param.scale; + const float bias = param.bias; + + LOG(INFO) << "x_image" << x_img; + auto out_image_shape = InitImageDimInfoWith(in_dims); + LOG(INFO) << "out_image_shape = " << out_image_shape["width"] << " " + << out_image_shape["height"]; + auto* out_img = param.output->mutable_data( + out_image_shape["width"], out_image_shape["height"]); + LOG(INFO) << "out_image" << out_img; + + auto& context = ctx_->As(); + CHECK(context.cl_context() != nullptr); + STL::stringstream kernel_key; + kernel_key << kernel_func_name_ << build_options_; + auto kernel = context.cl_context()->GetKernel(kernel_key.str()); + + auto global_work_size = + cl::NDRange{static_cast(out_image_shape["width"]), + static_cast(out_image_shape["height"])}; + + cl_int status; + int arg_idx = 0; + status = kernel.setArg(arg_idx, *x_img); + CL_CHECK_FATAL(status); + status = kernel.setArg(++arg_idx, *out_img); + CL_CHECK_FATAL(status); + status = kernel.setArg(++arg_idx, scale); + CL_CHECK_FATAL(status); + status = kernel.setArg(++arg_idx, bias); + CL_CHECK_FATAL(status); + + status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel( + kernel, + cl::NullRange, + global_work_size, + cl::NullRange, + nullptr, + event_.get()); + CL_CHECK_FATAL(status); + context.cl_wait_list()->emplace(out_img, event_); + } + + private: + std::string kernel_func_name_{"scale"}; + std::string build_options_{"-DCL_DTYPE_float"}; + std::shared_ptr event_{new cl::Event}; +}; + +} // namespace opencl +} // namespace kernels +} // namespace lite +} // namespace paddle + +REGISTER_LITE_KERNEL(scale, + kOpenCL, + kFloat, + kImageDefault, + paddle::lite::kernels::opencl::ScaleComputeImage2D, + image2d) + .BindInput("X", + {LiteType::GetTensorTy(TARGET(kOpenCL), + PRECISION(kFloat), + DATALAYOUT(kImageDefault))}) + .BindOutput("Out", + {LiteType::GetTensorTy(TARGET(kOpenCL), + PRECISION(kFloat), + DATALAYOUT(kImageDefault))}) + .Finalize(); diff --git a/lite/kernels/opencl/scale_compute_test.cc b/lite/kernels/opencl/scale_compute_test.cc new file mode 100644 index 0000000000..72381fee4f --- /dev/null +++ b/lite/kernels/opencl/scale_compute_test.cc @@ -0,0 +1,124 @@ +// 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 +#include +#include +#include "lite/backends/opencl/target_wrapper.h" +#include "lite/core/op_registry.h" +#include "lite/core/tensor.h" + +namespace paddle { +namespace lite { + +void scale(const float* input_data, + const DDim& in_dim, + float* output_data, + const float scale, + const float bias) { + for (int i = 0; i < in_dim.production(); i++) { + output_data[i] = input_data[i] * scale + bias; + } +} + +TEST(scale_image2d_fp32, compute) { + LOG(INFO) << "to get kernel ..."; + auto kernels = KernelRegistry::Global().Create( + "scale", TARGET(kOpenCL), PRECISION(kFloat), DATALAYOUT(kImageDefault)); + ASSERT_FALSE(kernels.empty()); + + auto kernel = std::move(kernels.front()); + + LOG(INFO) << "get kernel:" << kernel->doc(); + + lite::Tensor x, out; + operators::ScaleParam param; + param.x = &x; + param.output = &out; + param.scale = 1.5f; + param.bias = 0.3f; + + std::unique_ptr context(new KernelContext); + context->As().InitOnce(); + + kernel->SetParam(param); + std::unique_ptr scale_context(new KernelContext); + context->As().CopySharedTo( + &(scale_context->As())); + kernel->SetContext(std::move(scale_context)); + + const DDim in_dim = DDim(std::vector{4, 11, 107, 107}); + const DDim out_dim = DDim(std::vector{4, 11, 107, 107}); + x.Resize(in_dim); + out.Resize(out_dim); + + std::default_random_engine engine; + std::uniform_real_distribution dist(-5, 5); + std::vector input_v(4 * 11 * 107 * 107); + for (auto& i : input_v) { + i = dist(engine); + } + + LOG(INFO) << "prepare input"; + CLImageConverterDefault* default_converter = new CLImageConverterDefault(); + DDim image_shape = default_converter->InitImageDimInfoWith(in_dim); + LOG(INFO) << "image_shape = " << image_shape[0] << " " << image_shape[1]; + std::vector x_image_data(image_shape.production() * 4); // 4 : RGBA + default_converter->NCHWToImage(input_v.data(), x_image_data.data(), in_dim); + auto* x_image = x.mutable_data( + image_shape[0], image_shape[1], x_image_data.data()); + LOG(INFO) << "x_image:" << x_image; + + auto* out_image = + out.mutable_data(image_shape[0], image_shape[1]); + LOG(INFO) << "out_image:" << out_image; + kernel->Launch(); + + auto* wait_list = context->As().cl_wait_list(); + auto* out_ptr = param.output->data(); + auto it = wait_list->find(out_ptr); + if (it != wait_list->end()) { + VLOG(4) << "--- Find the sync event for the target cl tensor. ---"; + auto& event = *(it->second); + event.wait(); + } else { + LOG(FATAL) << "Could not find the sync event for the target cl tensor."; + } + + std::unique_ptr out_ref(new float[out_dim.production()]); + scale(input_v.data(), in_dim, out_ref.get(), 1.5f, 0.3f); + + const size_t cl_image2d_row_pitch{0}; + const size_t cl_image2d_slice_pitch{0}; + float* out_image_data = new float[image_shape.production() * 4]; + TargetWrapperCL::ImgcpySync(out_image_data, + out_image, + image_shape[0], + image_shape[1], + cl_image2d_row_pitch, + cl_image2d_slice_pitch, + IoDirection::DtoH); + float* out_data = new float[image_shape.production() * 4]; + default_converter->ImageToNCHW( + out_image_data, out_data, image_shape, out_dim); + + for (int i = 0; i < out_dim.production(); i++) { + EXPECT_NEAR(out_data[i], out_ref[i], 1e-6); + } +} + +} // namespace lite +} // namespace paddle + +USE_LITE_KERNEL(scale, kOpenCL, kFloat, kImageDefault, image2d); -- GitLab