diff options
author | Michalis Spyrou <michalis.spyrou@arm.com> | 2018-04-10 13:41:30 +0100 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:52:54 +0000 |
commit | 46da23fac3bba64f78f20192750aa094781efbb3 (patch) | |
tree | 929e8237c1efe2fcd93a4baf1b74e30383c54b4f /src/core | |
parent | b940fd6a17b32ad87bdafc57adccb4433cf3fb75 (diff) | |
download | ComputeLibrary-46da23fac3bba64f78f20192750aa094781efbb3.tar.gz |
COMPMID-813 Add NHWC data format support for CL scale
Change-Id: Ie218447c4f3f94a37b5dd2d3b33488c7f5869adf
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/128520
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CL/CLKernelLibrary.cpp | 6 | ||||
-rw-r--r-- | src/core/CL/cl_kernels/scale.cl | 127 | ||||
-rw-r--r-- | src/core/CL/kernels/CLScaleKernel.cpp | 214 |
3 files changed, 299 insertions, 48 deletions
diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 7f1667a9cd..cdde7ef75a 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -336,8 +336,10 @@ const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map = { "RGBA8888_to_RGB888_bt709", "color_convert.cl" }, { "RGBA8888_to_YUV444_bt709", "color_convert.cl" }, { "roi_pooling_layer", "roi_pooling_layer.cl" }, - { "scale_nearest_neighbour", "scale.cl" }, - { "scale_bilinear", "scale.cl" }, + { "scale_nearest_neighbour_nchw", "scale.cl" }, + { "scale_nearest_neighbour_nhwc", "scale.cl" }, + { "scale_bilinear_nchw", "scale.cl" }, + { "scale_bilinear_nhwc", "scale.cl" }, { "scharr3x3", "scharr_filter.cl" }, { "sobel3x3", "sobel_filter.cl" }, { "sobel_separable5x1", "sobel_filter.cl" }, diff --git a/src/core/CL/cl_kernels/scale.cl b/src/core/CL/cl_kernels/scale.cl index a2ae8c4dd6..744f28a918 100644 --- a/src/core/CL/cl_kernels/scale.cl +++ b/src/core/CL/cl_kernels/scale.cl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -83,7 +83,7 @@ inline const float8 transform_bilinear(const float2 coord, const float2 scale) * @param[in] scale_x The scale factor along x dimension * @param[in] scale_y The scale factor along y dimension */ -__kernel void scale_nearest_neighbour( +__kernel void scale_nearest_neighbour_nchw( IMAGE_DECLARATION(in), IMAGE_DECLARATION(out), const float input_width, @@ -119,7 +119,7 @@ __kernel void scale_nearest_neighbour( * @param[in] scale_x The scale factor along x dimension * @param[in] scale_y The scale factor along y dimension */ -__kernel void scale_bilinear( +__kernel void scale_bilinear_nchw( IMAGE_DECLARATION(in), IMAGE_DECLARATION(out), const float input_width, @@ -133,3 +133,124 @@ __kernel void scale_bilinear( const float8 tc = transform_bilinear(get_current_coords(), r); vstore4(bilinear_interpolate_with_border(&in, tc, input_width, input_height, BORDER_SIZE), 0, (__global DATA_TYPE *)out.ptr); } + +/** Performs scale on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel F32. (NHWC) + * + * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT + * + * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32. + * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes) + * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image + * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr + * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) + * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) + * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes) + * @param[in] out_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image + * @param[in] input_width Input image width + * @param[in] input_height Input image height + * @param[in] scale_x The scale factor along x dimension + * @param[in] scale_y The scale factor along y dimension + */ +__kernel void scale_nearest_neighbour_nhwc( + TENSOR3D_DECLARATION(in), + TENSOR3D_DECLARATION(out), + const float input_width, + const float input_height, + const float scale_x, + const float scale_y) +{ + Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(in); + Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); + + const float new_x = (get_global_id(1) + 0.5f) * scale_x; + const float new_y = (get_global_id(2) + 0.5f) * scale_y; + const float clamped_x = clamp(new_x, 0.0f, input_width - 1); + const float clamped_y = clamp(new_y, 0.0f, input_height - 1); + + *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y))); +} + +/** Performs scale on an image interpolating with the BILINEAR method. (NHWC) + * + * @note Sampling policy to be used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT + * @note If border mode replicate is used, is should be passed as -DBORDER_MODE_REPLICATE + * + * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32. + * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes) + * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image + * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr + * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) + * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) + * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes) + * @param[in] out_step_z dst_stride_y * number of elements along Z processed per workitem(in bytes) + * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image + * @param[in] input_width Input image width + * @param[in] input_height Input image height + * @param[in] scale_x The scale factor along x dimension + * @param[in] scale_y The scale factor along y dimension + */ +__kernel void scale_bilinear_nhwc( + TENSOR3D_DECLARATION(in), + TENSOR3D_DECLARATION(out), + const float input_width, + const float input_height, + const float scale_x, + const float scale_y) +{ + Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(in); + Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out); + +#ifdef SAMPLING_POLICY_TOP_LEFT + const float new_x = get_global_id(1) * scale_x; + const float new_y = get_global_id(2) * scale_y; +#elif SAMPLING_POLICY_CENTER + const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f; + const float new_y = (get_global_id(2) + 0.5f) * scale_y - 0.5f; +#else /* SAMPLING_POLICY */ +#error("Unsupported sampling policy"); +#endif /* SAMPLING_POLICY */ + + const float new_xf = floor(new_x); + const float new_yf = floor(new_y); + float clamped_x = clamp(new_xf, 0.0f, input_width - 1); + float clamped_x1 = clamp(new_xf + 1, 0.0f, input_width - 1); + float clamped_x_ = clamped_x; + float clamped_x1_ = clamped_x1; + const float clamped_y = clamp(new_yf, 0.0f, input_height - 1); + const float clamped_y1 = clamp(new_yf + 1, 0.0f, input_height - 1); + +#ifndef BORDER_MODE_REPLICATE + clamped_x1 = select(clamped_x1, 0.0f - BORDER_SIZE, new_yf + 1 < 0.f || new_yf + 1 > input_height - 1 || new_xf + 1 < 0.f || new_xf + 1 > input_width - 1); + clamped_x_ = select(clamped_x_, 0.0f - BORDER_SIZE, new_yf + 1 > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1); + clamped_x = select(clamped_x, 0.0f - BORDER_SIZE, new_yf < 0.f || new_yf > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1); + clamped_x1_ = select(clamped_x1_, 0.0f - BORDER_SIZE, new_xf + 1 < 0.f || new_xf + 1 > input_width - 1 || new_yf < 0.f || new_yf > input_height - 1); +#endif /* BORDER_MODE_REPLICATE */ + + float4 ins = (float4)(*((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y))), + *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x1_), convert_int(clamped_y))), + *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x_), convert_int(clamped_y1))), + *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1)))); + + const float a = new_x - new_xf; + const float b = 1.f - a; + const float a1 = new_y - new_yf; + const float b1 = 1.f - a1; + const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1)); + + *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE); +} diff --git a/src/core/CL/kernels/CLScaleKernel.cpp b/src/core/CL/kernels/CLScaleKernel.cpp index 10be140dea..b1655d5cc1 100644 --- a/src/core/CL/kernels/CLScaleKernel.cpp +++ b/src/core/CL/kernels/CLScaleKernel.cpp @@ -39,82 +39,210 @@ using namespace arm_compute; +namespace +{ +inline std::pair<float, float> calculate_scale_factors(const ITensorInfo &input, const ITensorInfo &output) +{ + DataLayout data_layout = input.data_layout(); + const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + + // Compute the ratio between source width/height and destination width/height + const unsigned int input_width = input.dimension(idx_width); + const unsigned int input_height = input.dimension(idx_height); + const unsigned int output_width = output.dimension(idx_width); + const unsigned int output_height = output.dimension(idx_height); + + float wr = static_cast<float>(input_width) / static_cast<float>(output_width); + float hr = static_cast<float>(input_height) / static_cast<float>(output_height); + + return std::make_pair(wr, hr); +} + +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy) +{ + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_RETURN_ERROR_ON(output == input); + + float wr = 0.f; + float hr = 0.f; + std::tie(wr, hr) = calculate_scale_factors(*input, *output); + + ARM_COMPUTE_RETURN_ERROR_ON(policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f)); + + return Status{}; +} + +std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, BorderSize &border) +{ + Window win{}; + bool window_changed{}; + unsigned int num_elems_processed_per_iteration = 0; + DataLayout data_layout = input->data_layout(); + + switch(data_layout) + { + case DataLayout::NCHW: + { + if(border_mode == BorderMode::UNDEFINED) + { + border = BorderSize(0); + } + + num_elems_processed_per_iteration = 4; + // Configure kernel window + win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); + const ValidRegion &input_valid_region = input->valid_region(); + + // Reads can occur within the valid region of the input + AccessWindowStatic input_access(input, + input_valid_region.anchor[0] - border.left, input_valid_region.anchor[1] - border.top, + input_valid_region.anchor[0] + input_valid_region.shape[0] + border.right, + input_valid_region.anchor[1] + input_valid_region.shape[1] + border.bottom); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + + output_access.set_valid_region(win, calculate_valid_region_scale(*(input), + output->tensor_shape(), + policy, + sampling_policy, + border_mode == BorderMode::UNDEFINED)); + + window_changed = update_window_and_padding(win, input_access, output_access); + } + break; + case DataLayout::NHWC: + { + num_elems_processed_per_iteration = 1; + // Configure kernel window + win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); + AccessWindowRectangle input_access(input, -border.left, -border.top, num_elems_processed_per_iteration, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + window_changed = update_window_and_padding(win, input_access, output_access); + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); + } + break; + default: + ARM_COMPUTE_ERROR("Data layout not supported"); + } + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, win); +} +} // namespace + BorderSize CLScaleKernel::border_size() const { return BorderSize(1); } -void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy) +Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, + BorderMode border_mode, SamplingPolicy sampling_policy) { - ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(input); - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); - ARM_COMPUTE_ERROR_ON_NULLPTR(output); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_ERROR_ON(output == input); + BorderSize border = BorderSize(1); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, policy)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), policy, border_mode, sampling_policy, border).first); + + return Status{}; +} +void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy) +{ _input = input; _output = output; + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), policy)); + + float wr = 0.f; + float hr = 0.f; + std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info()); + + DataLayout data_layout = input->info()->data_layout(); + const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + // Compute the ratio between source width/height and destination width/height - const auto wr = static_cast<float>(input->info()->dimension(0)) / static_cast<float>(output->info()->dimension(0)); - const auto hr = static_cast<float>(input->info()->dimension(1)) / static_cast<float>(output->info()->dimension(1)); + const unsigned int input_width = input->info()->dimension(idx_width); + const unsigned int input_height = input->info()->dimension(idx_height); + const unsigned int output_width = output->info()->dimension(idx_width); + const unsigned int output_height = output->info()->dimension(idx_height); // Compute actual border size - BorderSize border = border_undefined ? BorderSize(0) : border_size(); + BorderSize border = border_size(); // Area interpolation behaves as Nearest Neighbour in case of up-sampling if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) { policy = InterpolationPolicy::NEAREST_NEIGHBOR; } - else - { - ARM_COMPUTE_ERROR_ON(policy == InterpolationPolicy::AREA); - } + + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), output->info(), policy, border_mode, sampling_policy, border); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + ICLKernel::configure(win_config.second); // Create kernel CLBuildOptions build_opts; build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right)); + build_opts.add_option_if(border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE"); build_opts.add_option_if_else(sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT"); std::string interpolation_name = string_from_interpolation_policy(policy); std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower); - std::string kernel_name = "scale_" + interpolation_name; + std::string kernel_name = "scale_" + interpolation_name + "_" + lower_string(string_from_data_layout(data_layout)); _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); - // Configure kernel window - constexpr unsigned int num_elems_processed_per_iteration = 4; - - Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); - - const ValidRegion &input_valid_region = input->info()->valid_region(); - - // Reads can occur within the valid region of the input - AccessWindowStatic input_access(input->info(), - input_valid_region.anchor[0] - border.left, input_valid_region.anchor[1] - border.top, - input_valid_region.anchor[0] + input_valid_region.shape[0] + border.right, - input_valid_region.anchor[1] + input_valid_region.shape[1] + border.bottom); - - AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); - - update_window_and_padding(win, input_access, output_access); - - output_access.set_valid_region(win, calculate_valid_region_scale(*(input->info()), - output->info()->tensor_shape(), - policy, - sampling_policy, - border_undefined)); - - ICLKernel::configure(win); + unsigned int idx = data_layout == DataLayout::NHWC ? 2 * num_arguments_per_3D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters // Set static kernel arguments - const float scale_x = static_cast<float>(input->info()->dimension(0)) / output->info()->dimension(0); - const float scale_y = static_cast<float>(input->info()->dimension(1)) / output->info()->dimension(1); + const float scale_x = static_cast<float>(input_width) / output_width; + const float scale_y = static_cast<float>(input_height) / output_height; - unsigned int idx = 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters - _kernel.setArg<float>(idx++, input->info()->dimension(0)); - _kernel.setArg<float>(idx++, input->info()->dimension(1)); + _kernel.setArg<float>(idx++, input_width); + _kernel.setArg<float>(idx++, input_height); _kernel.setArg<float>(idx++, scale_x); _kernel.setArg<float>(idx++, scale_y); } + +void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + + switch(_input->info()->data_layout()) + { + case DataLayout::NCHW: + { + Window slice = window.first_slice_window_2D(); + + do + { + unsigned int idx = 0; + add_2D_tensor_argument(idx, _input, slice); + add_2D_tensor_argument(idx, _output, slice); + enqueue(queue, *this, slice, _lws_hint); + } + while(window.slide_window_slice_2D(slice)); + break; + } + case DataLayout::NHWC: + { + Window slice = window.first_slice_window_3D(); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice); + add_3D_tensor_argument(idx, _output, slice); + enqueue(queue, *this, slice, _lws_hint); + } + while(window.slide_window_slice_3D(slice)); + break; + } + default: + ARM_COMPUTE_ERROR("Data layout not supported"); + } +} |