From 46da23fac3bba64f78f20192750aa094781efbb3 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Tue, 10 Apr 2018 13:41:30 +0100 Subject: 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 Reviewed-by: Anthony Barbier --- arm_compute/core/CL/kernels/CLScaleKernel.h | 30 +++- src/core/CL/CLKernelLibrary.cpp | 6 +- src/core/CL/cl_kernels/scale.cl | 127 ++++++++++++++- src/core/CL/kernels/CLScaleKernel.cpp | 214 ++++++++++++++++++++----- src/runtime/CL/functions/CLGaussianPyramid.cpp | 2 +- src/runtime/CL/functions/CLScale.cpp | 11 +- tests/validation/CL/Scale.cpp | 16 +- tests/validation/fixtures/ScaleFixture.h | 1 + 8 files changed, 340 insertions(+), 67 deletions(-) diff --git a/arm_compute/core/CL/kernels/CLScaleKernel.h b/arm_compute/core/CL/kernels/CLScaleKernel.h index 3bca6efd0a..b089e8f61a 100644 --- a/arm_compute/core/CL/kernels/CLScaleKernel.h +++ b/arm_compute/core/CL/kernels/CLScaleKernel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -37,17 +37,31 @@ class CLScaleKernel : public ICLSimple2DKernel public: /** Initialise the kernel's inputs, output and interpolation policy * - * @param[in] input Source tensor. Data types supported: U8/S16/F16/F32 - * @param[out] output Destination tensor. Data types supported: Same as @p input - * All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane. - * @param[in] policy Interpolation type to use - * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant. - * @param[in] sampling_policy (Optional) Sampling policy used by the interpolation. Defaults to @ref SamplingPolicy::CENTER + * @param[in] input Source tensor. Data types supported: U8/S16/F16/F32 + * @param[out] output Destination tensor. Data types supported: Same as @p input + * All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane. + * @param[in] policy Interpolation type to use + * @param[in] border_mode Selected border mode. + * @param[in] sampling_policy (Optional) Sampling policy used by the interpolation. Defaults to @ref SamplingPolicy::CENTER */ - void configure(const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy = SamplingPolicy::CENTER); + void configure(const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy = SamplingPolicy::CENTER); + + /** Static function to check if given info will lead to a valid configuration of @ref CLScaleKernel + * + * @param[in] input Source tensor info. Data types supported: U8/S16/F16/F32 + * @param[in] output Destination tensor info. Data types supported: Same as @p input + * All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane. + * @param[in] policy Interpolation type to use + * @param[in] border_mode Selected border mode. + * @param[in] sampling_policy (Optional) Sampling policy used by the interpolation. Defaults to @ref SamplingPolicy::CENTER + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy = SamplingPolicy::CENTER); // Inherited methods overridden: BorderSize border_size() const override; + void run(const Window &window, cl::CommandQueue &queue) override; }; } // namespace arm_compute #endif /*__ARM_COMPUTE_CLSCALEKERNEL_H__ */ 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 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 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(input_width) / static_cast(output_width); + float hr = static_cast(input_height) / static_cast(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 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(input->info()->dimension(0)) / static_cast(output->info()->dimension(0)); - const auto hr = static_cast(input->info()->dimension(1)) / static_cast(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(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(input->info()->dimension(0)) / output->info()->dimension(0); - const float scale_y = static_cast(input->info()->dimension(1)) / output->info()->dimension(1); + const float scale_x = static_cast(input_width) / output_width; + const float scale_y = static_cast(input_height) / output_height; - unsigned int idx = 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters - _kernel.setArg(idx++, input->info()->dimension(0)); - _kernel.setArg(idx++, input->info()->dimension(1)); + _kernel.setArg(idx++, input_width); + _kernel.setArg(idx++, input_height); _kernel.setArg(idx++, scale_x); _kernel.setArg(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"); + } +} diff --git a/src/runtime/CL/functions/CLGaussianPyramid.cpp b/src/runtime/CL/functions/CLGaussianPyramid.cpp index ddce5fb8c3..fd82769004 100644 --- a/src/runtime/CL/functions/CLGaussianPyramid.cpp +++ b/src/runtime/CL/functions/CLGaussianPyramid.cpp @@ -166,7 +166,7 @@ void CLGaussianPyramidOrb::configure(ICLTensor *input, CLPyramid *pyramid, Borde _gauss5x5[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value); /* Configure scale image kernel */ - _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), InterpolationPolicy::NEAREST_NEIGHBOR, border_mode == BorderMode::UNDEFINED, SamplingPolicy::CENTER); + _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), InterpolationPolicy::NEAREST_NEIGHBOR, border_mode, SamplingPolicy::CENTER); } _tmp.allocate(); diff --git a/src/runtime/CL/functions/CLScale.cpp b/src/runtime/CL/functions/CLScale.cpp index cb68481787..7ef55f9f08 100644 --- a/src/runtime/CL/functions/CLScale.cpp +++ b/src/runtime/CL/functions/CLScale.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -34,7 +34,14 @@ using namespace arm_compute; void CLScale::configure(ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy) { auto k = arm_compute::support::cpp14::make_unique(); - k->configure(input, output, policy, border_mode == BorderMode::UNDEFINED, sampling_policy); + k->configure(input, output, policy, border_mode, sampling_policy); _kernel = std::move(k); + + // In the case of NHWC we can't have undefined border mode as this would require to access elements outside z dimension, + // so we treat it like border constant. + if(border_mode == BorderMode::UNDEFINED && input->info()->data_layout() == DataLayout::NHWC) + { + border_mode = BorderMode::CONSTANT; + } _border_handler.configure(input, _kernel->border_size(), border_mode, constant_border_value); } diff --git a/tests/validation/CL/Scale.cpp b/tests/validation/CL/Scale.cpp index 3d8750ad28..ddc93d045f 100644 --- a/tests/validation/CL/Scale.cpp +++ b/tests/validation/CL/Scale.cpp @@ -120,7 +120,7 @@ TEST_SUITE(Float) TEST_SUITE(FP32) FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -134,7 +134,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode:: } FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -150,7 +150,7 @@ TEST_SUITE_END() TEST_SUITE(FP16) FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -164,7 +164,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode::A } FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -183,7 +183,7 @@ TEST_SUITE(Integer) TEST_SUITE(U8) FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::U8)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -197,7 +197,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode } FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::U8)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -213,7 +213,7 @@ TEST_SUITE_END() TEST_SUITE(S16) FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::S16)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) @@ -227,7 +227,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture, framework::DatasetMode } FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::S16)), - framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })), framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })), datasets::BorderModes()), datasets::SamplingPolicies())) diff --git a/tests/validation/fixtures/ScaleFixture.h b/tests/validation/fixtures/ScaleFixture.h index ec102313c5..05e1bf5ffe 100644 --- a/tests/validation/fixtures/ScaleFixture.h +++ b/tests/validation/fixtures/ScaleFixture.h @@ -31,6 +31,7 @@ #include "tests/IAccessor.h" #include "tests/framework/Asserts.h" #include "tests/framework/Fixture.h" +#include "tests/validation/reference/Permute.h" #include "tests/validation/reference/Scale.h" namespace arm_compute -- cgit v1.2.1