From c26179810823bb36375111f4a5485a6475fc97c5 Mon Sep 17 00:00:00 2001 From: Sang-Hoon Park Date: Wed, 20 May 2020 22:13:47 +0100 Subject: COMPMID-3363: Create ScaleKernelInfo NEScaleKernel and NEScale are modified to use the new kernel descriptor. Change-Id: I32e3e2a5209a38f5bf32c43f64b2aa4430825988 Signed-off-by: Sang-Hoon Park Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3239 Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas Comments-Addressed: Arm Jenkins --- arm_compute/core/KernelDescriptors.h | 35 ++++++++++++ arm_compute/core/NEON/kernels/NEScaleKernel.h | 42 +++++--------- arm_compute/runtime/NEON/functions/NEScale.h | 16 ++++++ src/core/NEON/kernels/NEScaleKernel.cpp | 82 ++++++++++++--------------- src/runtime/NEON/functions/NEScale.cpp | 58 ++++++++++--------- tests/validation/NEON/Scale.cpp | 41 +++++++++++++- 6 files changed, 174 insertions(+), 100 deletions(-) diff --git a/arm_compute/core/KernelDescriptors.h b/arm_compute/core/KernelDescriptors.h index 6b4691bc83..de08288dec 100644 --- a/arm_compute/core/KernelDescriptors.h +++ b/arm_compute/core/KernelDescriptors.h @@ -24,6 +24,7 @@ #ifndef ARM_COMPUTE_CORE_KERNEL_DESCRIPTORS_H #define ARM_COMPUTE_CORE_KERNEL_DESCRIPTORS_H +#include "arm_compute/core/PixelValue.h" #include "arm_compute/core/Types.h" namespace arm_compute @@ -168,5 +169,39 @@ struct GEMMLowpReductionKernelInfo int32_t scalar{ 0 }; /**< Scalar value to multiply each reduced column/row by */ bool mul_by_scalar{ false }; /**< True if each column/row reduction has to be multiplied by a scalar value */ }; + +struct ScaleKernelInfo +{ + /** Constructor + * + * @param[in] interpolation_policy Interpolation type to use + * @param[in] border_mode Border mode policy + * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT and use_padding is set to false. Defaults to default @ref PixelValue + * @param[in] sampling_policy (Optional) Sampling policy used by the interpolation. Defaults to @ref SamplingPolicy::CENTER + * @param[in] use_padding (Optional) Is padding in use or not. Defaults to true. + * @param[in] align_corners (Optional) Align corners of input and output, only affecting bilinear policy with TOP_LEFT sampling policy. Defaults to false. + */ + ScaleKernelInfo(InterpolationPolicy interpolation_policy, + BorderMode border_mode, + PixelValue constant_border_value = PixelValue(), + SamplingPolicy sampling_policy = SamplingPolicy::CENTER, + bool use_padding = true, + bool align_corners = false) + : interpolation_policy{ interpolation_policy }, + border_mode{ border_mode }, + constant_border_value{ constant_border_value }, + sampling_policy{ sampling_policy }, + use_padding{ use_padding }, + align_corners{ align_corners } + { + } + + InterpolationPolicy interpolation_policy; /**< Interpolation type to use */ + BorderMode border_mode; /**< Border mode policy */ + PixelValue constant_border_value; /**< Constant value to use for constant border mode policy */ + SamplingPolicy sampling_policy; /**< Sampling policy used by the interpolation. */ + bool use_padding; /**< Indication of using padding */ + bool align_corners; /**< Align corners of input and output */ +}; } // namespace arm_compute #endif /* ARM_COMPUTE_CORE_KERNEL_DESCRIPTORS_H */ diff --git a/arm_compute/core/NEON/kernels/NEScaleKernel.h b/arm_compute/core/NEON/kernels/NEScaleKernel.h index 0d0d457d51..9bc04129e0 100644 --- a/arm_compute/core/NEON/kernels/NEScaleKernel.h +++ b/arm_compute/core/NEON/kernels/NEScaleKernel.h @@ -24,8 +24,8 @@ #ifndef ARM_COMPUTE_NESCALEKERNEL_H #define ARM_COMPUTE_NESCALEKERNEL_H +#include "arm_compute/core/KernelDescriptors.h" #include "arm_compute/core/NEON/INEKernel.h" -#include "arm_compute/core/Types.h" namespace arm_compute { @@ -57,41 +57,29 @@ public: * @note dx, dy and offsets have the same dimensions (width and height) of the output tensor * @note Using @p policy Area only supports data layout NCHW and input data type U8. * - * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/U8/S16/F16/F32. - * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer. Data type supported: F32 - * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer. Data type supported: F32 - * @param[in] offsets Offset to access the pixel with NEAREST interpolation or the top-left pixel with BILINEAR interpolation in the input tensor. Data type supported: S32. - * @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 Border mode policy - * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT and use_padding is set to false. - * @param[in] sampling_policy (Optional) Sampling policy used by the interpolation. Defaults to @ref SamplingPolicy::CENTER - * @param[in] use_padding (Optional) Is padding in use or not. Defaults to true. - * @param[in] align_corners (Optional) Align corners of input and output, only affecting bilinear policy with TOP_LEFT sampling policy. Defaults to false. + * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/U8/S16/F16/F32. + * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer. Data type supported: F32 + * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer. Data type supported: F32 + * @param[in] offsets Offset to access the pixel with NEAREST interpolation or the top-left pixel with BILINEAR interpolation in the input tensor. Data type supported: S32. + * @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] info @ref ScaleKernelInfo to use for configuration */ void configure(const ITensor *input, const ITensor *dx, const ITensor *dy, const ITensor *offsets, ITensor *output, - InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value = PixelValue(), - SamplingPolicy sampling_policy = SamplingPolicy::CENTER, bool use_padding = true, bool align_corners = false); + const ScaleKernelInfo &info); /** Static function to check if given info will lead to a valid configuration of @ref NEScaleKernel * * @note dx, dy and offsets have the same dimensions (width and height) of the output tensor * @note Using @p policy Area only supports data layout NCHW and input data type U8. * - * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/U8/S16/F16/F32. - * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer. Data type supported: F32 - * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer. Data type supported: F32 - * @param[in] offsets Offset to access the pixel with NEAREST interpolation or the top-left pixel with BILINEAR interpolation in the input tensor. Data type supported: S32. - * @param[in] 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 Border mode policy - * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT and use_padding is set to false. - * @param[in] sampling_policy (Optional) Sampling policy used by the interpolation. Defaults to @ref SamplingPolicy::CENTER - * @param[in] use_padding (Optional) Is padding in use or not. Defaults to true. - * @param[in] align_corners (Optional) Align corners of input and output, only affecting bilinear policy with TOP_LEFT sampling policy. Defaults to false. + * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/U8/S16/F16/F32. + * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer. Data type supported: F32 + * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer. Data type supported: F32 + * @param[in] offsets Offset to access the pixel with NEAREST interpolation or the top-left pixel with BILINEAR interpolation in the input tensor. Data type supported: S32. + * @param[in] 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] info @ref ScaleKernelInfo to use for validation */ static Status validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *output, - InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value = PixelValue(), - SamplingPolicy sampling_policy = SamplingPolicy::CENTER, bool use_padding = true, bool align_corners = false); + const ScaleKernelInfo &info); // Inherited methods overridden: void run(const Window &window, const ThreadInfo &info) override; diff --git a/arm_compute/runtime/NEON/functions/NEScale.h b/arm_compute/runtime/NEON/functions/NEScale.h index 75acb96b55..5350d0646c 100644 --- a/arm_compute/runtime/NEON/functions/NEScale.h +++ b/arm_compute/runtime/NEON/functions/NEScale.h @@ -58,6 +58,13 @@ public: */ void configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value = PixelValue(), SamplingPolicy sampling_policy = SamplingPolicy::CENTER, bool use_padding = true, bool align_corners = false); + /** Initialize the function's source, destination, interpolation type and border_mode. + * + * @param[in, out] input Source tensor. Data type supported: U8/S16/F16/F32. (Written to only for @p border_mode != UNDEFINED) + * @param[out] output Destination tensor. Data type 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] info @ref ScaleKernelInfo to be used for configuration + */ + void configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info); /** Static function to check if given info will lead to a valid configuration of @ref NEScale * * @param[in] input Source tensor. Data type supported: U8/S16/F16/F32. (Written to only for @p border_mode != UNDEFINED) @@ -73,6 +80,15 @@ public: */ static Status validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value = PixelValue(), SamplingPolicy sampling_policy = SamplingPolicy::CENTER, bool use_padding = true, bool align_corners = false); + /** Static function to check if given info will lead to a valid configuration of @ref NEScale + * + * @param[in] input Source tensor. Data type supported: U8/S16/F16/F32. (Written to only for @p border_mode != UNDEFINED) + * @param[in] output Destination tensor. Data type 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] info @ref ScaleKernelInfo to be used for validation + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info); // Inherited methods overridden: void run() override; diff --git a/src/core/NEON/kernels/NEScaleKernel.cpp b/src/core/NEON/kernels/NEScaleKernel.cpp index 4f2f925c3c..763ad49cb7 100644 --- a/src/core/NEON/kernels/NEScaleKernel.cpp +++ b/src/core/NEON/kernels/NEScaleKernel.cpp @@ -37,17 +37,16 @@ namespace arm_compute namespace { Status validate_arguments(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy, - const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy, - BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners) + const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info) { ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32, DataType::QASYMM8, DataType::QASYMM8_SIGNED); ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); ARM_COMPUTE_RETURN_ERROR_ON(output == input); - ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER && sampling_policy != SamplingPolicy::TOP_LEFT); - ARM_COMPUTE_RETURN_ERROR_ON(!use_padding && border_mode != BorderMode::CONSTANT); - ARM_COMPUTE_UNUSED(constant_border_value); + ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT); + ARM_COMPUTE_RETURN_ERROR_ON(!info.use_padding && info.border_mode != BorderMode::CONSTANT); + ARM_COMPUTE_UNUSED(info.constant_border_value); const DataLayout data_layout = input->data_layout(); const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); @@ -57,18 +56,18 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *dx, const ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0); ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0); - if(policy == InterpolationPolicy::NEAREST_NEIGHBOR) + if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR) { ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32); } - if(policy == InterpolationPolicy::BILINEAR) + if(info.interpolation_policy == InterpolationPolicy::BILINEAR) { ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32); - if(align_corners) + if(info.align_corners) { // For bilinear method with aligned corners, the resize ratio will // be calculated by (input_size - 1)/(output_size - 1). Belows are @@ -81,7 +80,7 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *dx, const } } - if(policy == InterpolationPolicy::AREA) + if(info.interpolation_policy == InterpolationPolicy::AREA) { ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); @@ -91,7 +90,7 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *dx, const } std::pair validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *dx, ITensorInfo *dy, ITensorInfo *offsets, ITensorInfo *output, - InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy, BorderSize border_size) + const ScaleKernelInfo &info, BorderSize border_size) { bool window_changed{ false }; Window win{}; @@ -123,30 +122,28 @@ std::pair validate_and_configure_window_nchw(ITensorInfo *input, AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); window_changed = window_changed || update_window_and_padding(win, input_access, output_access); output_access.set_valid_region(win, calculate_valid_region_scale(*input, output->tensor_shape(), - policy, sampling_policy, border_undefined)); + info.interpolation_policy, info.sampling_policy, info.border_mode == BorderMode::UNDEFINED)); Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; return std::make_pair(err, win); } -std::pair validate_and_configure_window_nhwc(ITensorInfo *input, ITensorInfo *output, - InterpolationPolicy policy, bool border_undefined, - SamplingPolicy sampling_policy, BorderSize border_size, bool use_padding) +std::pair validate_and_configure_window_nhwc(ITensorInfo *input, ITensorInfo *output, const ScaleKernelInfo &info, BorderSize border_size) { bool window_changed{ false }; Window win{}; - const unsigned int num_elems_processed_per_iteration = (use_padding && policy == InterpolationPolicy::NEAREST_NEIGHBOR) ? 16 / input->element_size() : 1; + const unsigned int num_elems_processed_per_iteration = (info.use_padding && info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR) ? 16 / input->element_size() : 1; // Configure kernel window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); - if(use_padding) + if(info.use_padding) { AccessWindowStatic input_access(input, 0, -border_size.top, ceil_to_multiple(input->tensor_shape()[0], num_elems_processed_per_iteration), input->tensor_shape()[1]); AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); window_changed = update_window_and_padding(win, input_access, output_access); - output->set_valid_region(calculate_valid_region_scale(*input, output->tensor_shape(), policy, sampling_policy, border_undefined)); + output->set_valid_region(calculate_valid_region_scale(*input, output->tensor_shape(), info.interpolation_policy, info.sampling_policy, info.border_mode == BorderMode::UNDEFINED)); } Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; @@ -154,20 +151,20 @@ std::pair validate_and_configure_window_nhwc(ITensorInfo *input, } std::pair validate_and_configure_window(ITensorInfo *input, ITensorInfo *dx, ITensorInfo *dy, ITensorInfo *offsets, ITensorInfo *output, - InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy, BorderSize border_size, bool use_padding) + const ScaleKernelInfo &info, BorderSize border_size) { std::pair win_config; switch(input->data_layout()) { case DataLayout::NCHW: - if(!use_padding) + if(!info.use_padding) { return std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Padding required for NCHW"), Window{}); } - win_config = validate_and_configure_window_nchw(input, dx, dy, offsets, output, policy, border_undefined, sampling_policy, border_size); + win_config = validate_and_configure_window_nchw(input, dx, dy, offsets, output, info, border_size); break; case DataLayout::NHWC: - win_config = validate_and_configure_window_nhwc(input, output, policy, border_undefined, sampling_policy, border_size, use_padding); + win_config = validate_and_configure_window_nhwc(input, output, info, border_size); break; default: win_config = std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported data layout!"), Window{}); @@ -353,8 +350,7 @@ BorderSize NEScaleKernel::border_size() const } void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITensor *dy, const ITensor *offsets, - ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, - bool use_padding, bool align_corners) + ITensor *output, const ScaleKernelInfo &info) { ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); // Perform validation step @@ -363,7 +359,7 @@ void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITe dy != nullptr ? dy->info() : nullptr, offsets != nullptr ? offsets->info() : nullptr, output->info(), - policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners)); + info)); // Get data layout and width/height indices const DataLayout data_layout = input->info()->data_layout(); @@ -375,16 +371,16 @@ void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITe _offsets = offsets; _dx = dx; _dy = dy; - _policy = policy; + _policy = info.interpolation_policy; _border_size = BorderSize(1); - _border_mode = border_mode; - _constant_border_value = constant_border_value; - _use_padding = use_padding; - _align_corners = _policy == InterpolationPolicy::BILINEAR - && sampling_policy == SamplingPolicy::TOP_LEFT - && align_corners; - - if(sampling_policy == SamplingPolicy::CENTER) + _border_mode = info.border_mode; + _constant_border_value = info.constant_border_value; + _use_padding = info.use_padding; + _align_corners = info.interpolation_policy == InterpolationPolicy::BILINEAR + && info.sampling_policy == SamplingPolicy::TOP_LEFT + && info.align_corners; + + if(info.sampling_policy == SamplingPolicy::CENTER) { _sampling_offset = 0.5f; } @@ -396,17 +392,14 @@ void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITe // Add constant border only on top in case of NHWC layout if(data_layout == DataLayout::NHWC) { - _border_size = (border_mode != BorderMode::REPLICATE && policy == InterpolationPolicy::BILINEAR && use_padding) ? BorderSize(1, 0, 0, 0) : BorderSize(0); + _border_size = (info.border_mode != BorderMode::REPLICATE && info.interpolation_policy == InterpolationPolicy::BILINEAR && info.use_padding) ? BorderSize(1, 0, 0, 0) : BorderSize(0); } // 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; - } + const auto policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy; // Select interpolation function - switch(policy) + switch(policy_to_use) { case InterpolationPolicy::NEAREST_NEIGHBOR: { @@ -433,7 +426,7 @@ void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITe dy != nullptr ? dy->info() : nullptr, offsets != nullptr ? offsets->info() : nullptr, output->info(), - policy, border_mode == BorderMode::UNDEFINED, sampling_policy, border_size(), use_padding); + info, border_size()); ARM_COMPUTE_ERROR_THROW_ON(win_config.first); INEKernel::configure(win_config.second); @@ -1113,22 +1106,21 @@ void NEScaleKernel::scale_nhwc(const Window &window) } Status NEScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy, - const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy, - BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners) + const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info) { BorderSize border_size(1); if(input->data_layout() == DataLayout::NHWC) { - border_size = (border_mode == BorderMode::CONSTANT && policy == InterpolationPolicy::BILINEAR) ? BorderSize(1, 0, 0, 0) : BorderSize(0); + border_size = (info.border_mode == BorderMode::CONSTANT && info.interpolation_policy == InterpolationPolicy::BILINEAR) ? BorderSize(1, 0, 0, 0) : BorderSize(0); } - ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info)); ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), dx != nullptr ? dx->clone().get() : nullptr, dy != nullptr ? dy->clone().get() : nullptr, offsets != nullptr ? offsets->clone().get() : nullptr, output->clone().get(), - policy, border_mode == BorderMode::UNDEFINED, sampling_policy, border_size, use_padding) + info, border_size) .first); return Status{}; diff --git a/src/runtime/NEON/functions/NEScale.cpp b/src/runtime/NEON/functions/NEScale.cpp index f1e9a87b36..acde0cfcc5 100644 --- a/src/runtime/NEON/functions/NEScale.cpp +++ b/src/runtime/NEON/functions/NEScale.cpp @@ -102,16 +102,15 @@ NEScale::NEScale() // NOLINT { } -void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, - bool align_corners) +void NEScale::configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info) { ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), policy, border_mode, constant_border_value, sampling_policy, use_padding)); + ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), info)); - _use_padding = use_padding; - _align_corners = policy == InterpolationPolicy::BILINEAR - && sampling_policy == SamplingPolicy::TOP_LEFT - && align_corners; + _use_padding = info.use_padding; + _align_corners = info.interpolation_policy == InterpolationPolicy::BILINEAR + && info.sampling_policy == SamplingPolicy::TOP_LEFT + && info.align_corners; // Get data layout and width/height indices const DataLayout data_layout = input->info()->data_layout(); @@ -129,25 +128,22 @@ void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy pol const size_t input_element_size = input->info()->element_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; - } + const auto policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy; - switch(policy) + switch(policy_to_use) { case InterpolationPolicy::NEAREST_NEIGHBOR: { TensorInfo tensor_info_offsets(shape, Format::S32); _offsets.allocator()->init(tensor_info_offsets); - _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding); + _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, info); // Allocate once the configure methods have been called _offsets.allocator()->allocate(); // Pre-compute offsets for nearest interpolation - precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, sampling_policy); + precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, info.sampling_policy); break; } case InterpolationPolicy::BILINEAR: @@ -159,7 +155,7 @@ void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy pol _dx.allocator()->init(tensor_info_dxdy); _dy.allocator()->init(tensor_info_dxdy); - _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners); + _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, info); // Allocate once the configure methods have been called _offsets.allocator()->allocate(); @@ -167,29 +163,33 @@ void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy pol _dy.allocator()->allocate(); // Pre-compute dx, dy and offsets for bilinear interpolation - precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, sampling_policy); + precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, info.sampling_policy); break; } case InterpolationPolicy::AREA: { - _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, policy, border_mode, constant_border_value); + _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, info); break; } default: ARM_COMPUTE_ERROR("Unsupported interpolation mode"); } - if(use_padding) + if(info.use_padding) { - _border_handler.configure(input, _scale_kernel.border_size(), border_mode, constant_border_value); + _border_handler.configure(input, _scale_kernel.border_size(), info.border_mode, info.constant_border_value); } } -Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, - BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners) +void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, + bool align_corners) +{ + configure(input, output, ScaleKernelInfo{ policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners }); +} + +Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info) { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER && sampling_policy != SamplingPolicy::TOP_LEFT); - ARM_COMPUTE_UNUSED(border_mode, constant_border_value); + ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT); ITensorInfo *offsets = nullptr; ITensorInfo *dx = nullptr; @@ -207,7 +207,7 @@ Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, In TensorInfo tensor_info_dx(shape, Format::F32); TensorInfo tensor_info_dy(shape, Format::F32); - switch(policy) + switch(info.interpolation_policy) { case InterpolationPolicy::NEAREST_NEIGHBOR: offsets = &tensor_info_offsets; @@ -221,8 +221,14 @@ Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, In break; } - ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(), - policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners)); + ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(), info)); + return Status{}; +} + +Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, + BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners) +{ + ARM_COMPUTE_RETURN_ON_ERROR(NEScale::validate(input, output, ScaleKernelInfo{ policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners })); return Status{}; } diff --git a/tests/validation/NEON/Scale.cpp b/tests/validation/NEON/Scale.cpp index 82ab666d4a..9ab169b251 100644 --- a/tests/validation/NEON/Scale.cpp +++ b/tests/validation/NEON/Scale.cpp @@ -230,6 +230,15 @@ TEST_SUITE(Validate) * the kernel are not currently tested. * - The same input and output * - Data type of offset, dx and dy + * This suite also tests two different validate() APIs - one is + * using @ref ScaleKernelInfo and the other one is more verbose + * one calls the other one - in the same test case. Even though + * there are possibility that it makes debugging for regression + * harder, belows are reasons of this test case implementation. + * - The more verbose one is just a wrapper function calls + * the other one without any additional logic. So we are + * safe to merge two tests into one. + * - A large amount of code duplication is test suite can be prevented. */ const auto input_shape = TensorShape{ 2, 3, 3, 2 }; @@ -252,9 +261,15 @@ TEST_CASE(NullPtr, framework::DatasetMode::ALL) result = NEScale::validate(nullptr, &output, default_interpolation_policy, default_border_mode); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + result = NEScale::validate(nullptr, &output, ScaleKernelInfo{ default_interpolation_policy, default_border_mode }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + // nullptr is given as output result = NEScale::validate(&input, nullptr, default_interpolation_policy, default_border_mode); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + + result = NEScale::validate(&input, nullptr, ScaleKernelInfo{ default_interpolation_policy, default_border_mode }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); } TEST_CASE(SupportDataType, framework::DatasetMode::ALL) @@ -290,7 +305,11 @@ TEST_CASE(SupportDataType, framework::DatasetMode::ALL) { const auto input = TensorInfo{ input_shape, 1, kv.first, default_data_layout }; const auto output = TensorInfo{ output_shape, 1, kv.first, default_data_layout }; - result = NEScale::validate(&input, &output, default_interpolation_policy, default_border_mode); + + result = NEScale::validate(&input, &output, default_interpolation_policy, default_border_mode); + ARM_COMPUTE_EXPECT(bool(result) == kv.second, framework::LogLevel::ERRORS); + + result = NEScale::validate(&input, &output, ScaleKernelInfo{ default_interpolation_policy, default_border_mode }); ARM_COMPUTE_EXPECT(bool(result) == kv.second, framework::LogLevel::ERRORS); } } @@ -302,8 +321,12 @@ TEST_CASE(MissmatchingDataType, framework::DatasetMode::ALL) const auto input = TensorInfo{ input_shape, 1, default_data_type, default_data_layout }; const auto output = TensorInfo{ output_shape, 1, non_default_data_type, default_data_layout }; Status result{}; + result = NEScale::validate(&input, &output, default_interpolation_policy, default_border_mode); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + + result = NEScale::validate(&input, &output, ScaleKernelInfo{ default_interpolation_policy, default_border_mode }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); } TEST_CASE(UsePadding, framework::DatasetMode::ALL) @@ -318,6 +341,9 @@ TEST_CASE(UsePadding, framework::DatasetMode::ALL) result = NEScale::validate(&input, &output, default_interpolation_policy, border_mode, PixelValue(), default_sampling_policy, use_padding); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + + result = NEScale::validate(&input, &output, ScaleKernelInfo{ default_interpolation_policy, border_mode, PixelValue(), default_sampling_policy, use_padding }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); } TEST_CASE(AreaWithNHWC, framework::DatasetMode::ALL) @@ -329,8 +355,12 @@ TEST_CASE(AreaWithNHWC, framework::DatasetMode::ALL) const auto input = TensorInfo{ input_shape, 1, default_data_type, data_layout }; const auto output = TensorInfo{ output_shape, 1, default_data_type, data_layout }; Status result{}; + result = NEScale::validate(&input, &output, interpolation_policy, default_border_mode); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + + result = NEScale::validate(&input, &output, ScaleKernelInfo{ interpolation_policy, default_border_mode }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); } TEST_CASE(AreaWithNonU8, framework::DatasetMode::ALL) @@ -343,8 +373,12 @@ TEST_CASE(AreaWithNonU8, framework::DatasetMode::ALL) const auto input = TensorInfo{ input_shape, 1, data_type, data_layout }; const auto output = TensorInfo{ output_shape, 1, data_type, data_layout }; Status result{}; + result = NEScale::validate(&input, &output, interpolation_policy, default_border_mode); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); + + result = NEScale::validate(&input, &output, ScaleKernelInfo{ interpolation_policy, default_border_mode }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); } TEST_CASE(InvalidAlignedCornerOutput, framework::DatasetMode::ALL) @@ -359,10 +393,13 @@ TEST_CASE(InvalidAlignedCornerOutput, framework::DatasetMode::ALL) const auto input = TensorInfo{ input_shape, 1, default_data_type, default_data_layout }; const auto output = TensorInfo{ invalid_output_shape, 1, default_data_type, default_data_layout }; Status result{}; + result = NEScale::validate(&input, &output, interpolation_policy, default_border_mode, PixelValue(), sampling_policy, default_use_padding, align_corners); ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); -} + result = NEScale::validate(&input, &output, ScaleKernelInfo{ interpolation_policy, default_border_mode, PixelValue(), sampling_policy, default_use_padding, align_corners }); + ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS); +} TEST_SUITE_END() // Validate template -- cgit v1.2.1