From 60954c671ffdc3422bbdb728fc022eb6896c1e17 Mon Sep 17 00:00:00 2001 From: Alex Gilday Date: Mon, 5 Mar 2018 16:22:48 +0000 Subject: COMPMID-754: Add validation to (De)QuantizationLayers Change-Id: If8fa1277e8dc5b8e28a8bcad4ff9fc672b00ce9a Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/123275 Reviewed-by: Anthony Barbier Tested-by: Jenkins Reviewed-by: Michalis Spyrou --- .../CL/kernels/CLDequantizationLayerKernel.cpp | 73 ++++++++++++++------ src/core/CL/kernels/CLMinMaxLayerKernel.cpp | 80 +++++++++++++++------- src/core/CL/kernels/CLQuantizationLayerKernel.cpp | 73 ++++++++++++++------ .../NEON/kernels/NEDequantizationLayerKernel.cpp | 73 ++++++++++++++------ src/core/NEON/kernels/NEMinMaxLayerKernel.cpp | 79 +++++++++++++++------ .../NEON/kernels/NEQuantizationLayerKernel.cpp | 72 ++++++++++++++----- 6 files changed, 326 insertions(+), 124 deletions(-) (limited to 'src/core') diff --git a/src/core/CL/kernels/CLDequantizationLayerKernel.cpp b/src/core/CL/kernels/CLDequantizationLayerKernel.cpp index 4efdb764bd..fa982d6cf2 100644 --- a/src/core/CL/kernels/CLDequantizationLayerKernel.cpp +++ b/src/core/CL/kernels/CLDequantizationLayerKernel.cpp @@ -34,6 +34,46 @@ using namespace arm_compute; +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); + + if(output->tensor_shape().total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); + } + + return Status{}; +} + +std::tuple validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max) +{ + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32, 0); + + constexpr unsigned int num_elems_processed_per_iteration = 4; + + // Configure window + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + AccessWindowStatic min_max_access(min_max, 0, 0, 2, min_max->dimension(1)); + + // Update window and padding + bool window_changed = update_window_and_padding(win, input_access, output_access, min_max_access); + + output_access.set_valid_region(win, input->valid_region()); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_tuple(err, win); +} +} // namespace + CLDequantizationLayerKernel::CLDequantizationLayerKernel() : _input(nullptr), _output(nullptr), _min_max(nullptr) { @@ -41,37 +81,30 @@ CLDequantizationLayerKernel::CLDequantizationLayerKernel() void CLDequantizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *min_max) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); - ARM_COMPUTE_ERROR_ON_NULLPTR(output, min_max); - ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); - - // Output tensor auto initialization if not yet initialized - auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::F32, 0); - - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), min_max->info())); _input = input; _output = output; _min_max = min_max; - constexpr unsigned int num_elems_processed_per_iteration = 4; - // Create kernel _kernel = static_cast(CLKernelLibrary::get().create_kernel("dequantization_layer")); - // Configure window - Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); - AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); - AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info()); - // Update window and padding - update_window_and_padding(win, input_access, output_access, min_max_access); + ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); - output_access.set_valid_region(win, input->info()->valid_region()); + ICLKernel::configure(std::get<1>(win_config)); +} + +Status CLDequantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, min_max)); + ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), min_max->clone().get()))); - ICLKernel::configure(win); + return Status{}; } void CLDequantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) diff --git a/src/core/CL/kernels/CLMinMaxLayerKernel.cpp b/src/core/CL/kernels/CLMinMaxLayerKernel.cpp index 8ba1f776a1..60dd5e7de3 100644 --- a/src/core/CL/kernels/CLMinMaxLayerKernel.cpp +++ b/src/core/CL/kernels/CLMinMaxLayerKernel.cpp @@ -30,38 +30,69 @@ #include "arm_compute/core/Helpers.h" #include "arm_compute/core/Validate.h" #include "arm_compute/core/Window.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" #include using namespace arm_compute; +using namespace arm_compute::misc::shape_calculator; -CLMinMaxLayerKernel::CLMinMaxLayerKernel() - : _input(nullptr), _output(nullptr) +namespace { +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); + + if(output->tensor_shape().total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + + TensorShape output_shape = compute_min_max_shape(input); + + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); + } + + return Status{}; } -void CLMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output) +std::tuple validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); - ARM_COMPUTE_ERROR_ON_NULLPTR(output); - - TensorShape output_shape{ input->info()->tensor_shape() }; - output_shape.set(Window::DimX, 2); - output_shape.remove_dimension(1); - output_shape.remove_dimension(1); + TensorShape output_shape = compute_min_max_shape(input); // Output auto initialization if not yet initialized - auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); + auto_init_if_empty(*output, output_shape, 1, input->data_type(), input->fixed_point_position()); + + const unsigned int num_elems_processed_per_iteration = 1; + + // Configure kernel window + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowStatic output_access(output, 0, 0, 2, output->dimension(1)); + + bool window_changed = update_window_and_padding(win, input_access, output_access); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_tuple(err, win); +} +} // namespace + +CLMinMaxLayerKernel::CLMinMaxLayerKernel() + : _input(nullptr), _output(nullptr) +{ +} + +void CLMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); _input = input; _output = output; - const unsigned int num_elems_processed_per_iteration = 1; - std::set build_opts; build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); @@ -70,16 +101,19 @@ void CLMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output) // Create kernel _kernel = static_cast(CLKernelLibrary::get().create_kernel("minmax_layer", build_opts)); - // Configure kernel window - Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); - AccessWindowStatic output_access(output->info(), 0, 0, 2, output->info()->dimension(1)); + auto win_config = validate_and_configure_window(input->info(), output->info()); + + ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); - update_window_and_padding(win, input_access, output_access); + ICLKernel::configure(std::get<1>(win_config)); +} - output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); +Status CLMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); + ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); - ICLKernel::configure(win); + return Status{}; } void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue) diff --git a/src/core/CL/kernels/CLQuantizationLayerKernel.cpp b/src/core/CL/kernels/CLQuantizationLayerKernel.cpp index 8b082a8704..028e50821f 100644 --- a/src/core/CL/kernels/CLQuantizationLayerKernel.cpp +++ b/src/core/CL/kernels/CLQuantizationLayerKernel.cpp @@ -34,6 +34,46 @@ using namespace arm_compute; +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); + + if(output->tensor_shape().total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); + } + + return Status{}; +} + +std::tuple validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max) +{ + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::U8, 0); + + constexpr unsigned int num_elems_processed_per_iteration = 4; + + // Configure window + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + AccessWindowStatic min_max_access(min_max, 0, 0, 2, min_max->dimension(1)); + + // Update window and padding + bool window_changed = update_window_and_padding(win, input_access, output_access, min_max_access); + + output_access.set_valid_region(win, input->valid_region()); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_tuple(err, win); +} +} // namespace + CLQuantizationLayerKernel::CLQuantizationLayerKernel() : _input(nullptr), _output(nullptr), _min_max(nullptr) { @@ -41,37 +81,30 @@ CLQuantizationLayerKernel::CLQuantizationLayerKernel() void CLQuantizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, ICLTensor *min_max) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON_NULLPTR(output, min_max); - ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); - - // Output tensor auto initialization if not yet initialized - auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::U8, 0); - - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); - ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), min_max->info())); _input = input; _output = output; _min_max = min_max; - constexpr unsigned int num_elems_processed_per_iteration = 4; - // Create kernel _kernel = static_cast(CLKernelLibrary::get().create_kernel("quantization_layer")); - // Configure window - Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); - AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); - AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info()); - // Update window and padding - update_window_and_padding(win, input_access, output_access, min_max_access); + ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); - output_access.set_valid_region(win, input->info()->valid_region()); + ICLKernel::configure(std::get<1>(win_config)); +} + +Status CLQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, min_max)); + ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), min_max->clone().get()))); - ICLKernel::configure(win); + return Status{}; } void CLQuantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) diff --git a/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp b/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp index be211b2cb2..4120e5f87a 100644 --- a/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp +++ b/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp @@ -34,6 +34,46 @@ using namespace arm_compute; +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); + + if(output->tensor_shape().total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); + } + + return Status{}; +} + +std::tuple validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max) +{ + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32, 0); + + constexpr unsigned int num_elems_processed_per_iteration = 8; + + // Configure window + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + AccessWindowStatic min_max_access(min_max, 0, 0, 2, min_max->dimension(1)); + + // Update window and padding + bool window_changed = update_window_and_padding(win, input_access, output_access, min_max_access); + + output_access.set_valid_region(win, input->valid_region()); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_tuple(err, win); +} +} // namespace + NEDequantizationLayerKernel::NEDequantizationLayerKernel() : _input(nullptr), _output(nullptr), _min_max(nullptr) { @@ -41,34 +81,27 @@ NEDequantizationLayerKernel::NEDequantizationLayerKernel() void NEDequantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); - ARM_COMPUTE_ERROR_ON_NULLPTR(output); - ARM_COMPUTE_ERROR_ON_NULLPTR(min_max); - ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); - - // Output tensor auto initialization if not yet initialized - auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::F32, 0); - - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), min_max->info())); _input = input; _output = output; _min_max = min_max; - constexpr unsigned int num_elems_processed_per_iteration = 8; + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info()); - // Configure window - Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); - AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); - AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); + ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); - // Update window and padding - update_window_and_padding(win, input_access, output_access, min_max_access); - output_access.set_valid_region(win, input->info()->valid_region()); + INEKernel::configure(std::get<1>(win_config)); +} + +Status NEDequantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, min_max)); + ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), min_max->clone().get()))); - INEKernel::configure(win); + return Status{}; } void NEDequantizationLayerKernel::run(const Window &window, const ThreadInfo &info) diff --git a/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp b/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp index cc03a517bd..434f4eb3e9 100644 --- a/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp +++ b/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp @@ -32,51 +32,86 @@ #include "arm_compute/core/Types.h" #include "arm_compute/core/Validate.h" #include "arm_compute/core/Window.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" #include #include #include #include +using namespace arm_compute::misc::shape_calculator; + namespace arm_compute { -NEMinMaxLayerKernel::NEMinMaxLayerKernel() - : _input(nullptr), _output(nullptr), _mtx() +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); + + if(output->tensor_shape().total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + + TensorShape output_shape = compute_min_max_shape(input); + + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); + } + + return Status{}; } -void NEMinMaxLayerKernel::configure(const ITensor *input, ITensor *output) +std::tuple validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); - ARM_COMPUTE_ERROR_ON(output == nullptr); - - TensorShape output_shape{ input->info()->tensor_shape() }; - output_shape.set(Window::DimX, 2); - output_shape.remove_dimension(1); - output_shape.remove_dimension(1); + TensorShape output_shape = compute_min_max_shape(input); // Output auto initialization if not yet initialized - auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); + auto_init_if_empty(*output, output_shape, 1, input->data_type(), input->fixed_point_position()); + + constexpr unsigned int num_elems_processed_per_iteration = 1; + + // Configure kernel window + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, 2); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); + bool window_changed = update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_tuple(err, win); +} +} // namespace + +NEMinMaxLayerKernel::NEMinMaxLayerKernel() + : _input(nullptr), _output(nullptr), _mtx() +{ +} + +void NEMinMaxLayerKernel::configure(const ITensor *input, ITensor *output) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); _input = input; _output = output; - // Configure kernel window - constexpr unsigned int num_elems_processed_per_iteration = 1; + auto win_config = validate_and_configure_window(input->info(), output->info()); - Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); - AccessWindowHorizontal output_access(output->info(), 0, 2); + ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); - update_window_and_padding(win, input_access, output_access); + INEKernel::configure(std::get<1>(win_config)); +} - output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); +Status NEMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); + ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); - INEKernel::configure(win); + return Status{}; } void NEMinMaxLayerKernel::run(const Window &window, const ThreadInfo &info) diff --git a/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp b/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp index 767af08d0d..ee23e76c5c 100644 --- a/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp +++ b/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp @@ -34,6 +34,46 @@ using namespace arm_compute; +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); + + if(output->tensor_shape().total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); + } + + return Status{}; +} + +std::tuple validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max) +{ + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::U8, 0); + + constexpr unsigned int num_elems_processed_per_iteration = 8; + + // Configure window + Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + AccessWindowStatic min_max_access(min_max, 0, 0, 2, min_max->dimension(1)); + + // Update window and padding + bool window_changed = update_window_and_padding(win, input_access, output_access, min_max_access); + + output_access.set_valid_region(win, input->valid_region()); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_tuple(err, win); +} +} // namespace + NEQuantizationLayerKernel::NEQuantizationLayerKernel() : _input(nullptr), _output(nullptr), _min_max(nullptr) { @@ -41,33 +81,27 @@ NEQuantizationLayerKernel::NEQuantizationLayerKernel() void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON_NULLPTR(output); - ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); - - // Output tensor auto initialization if not yet initialized - auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::U8, 0); - - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); - ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, min_max); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), min_max->info())); _input = input; _output = output; _min_max = min_max; - constexpr unsigned int num_elems_processed_per_iteration = 8; + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info()); - // Configure window - Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); - AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); - AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); + ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); - // Update window and padding - update_window_and_padding(win, input_access, output_access, min_max_access); - output_access.set_valid_region(win, input->info()->valid_region()); + INEKernel::configure(std::get<1>(win_config)); +} + +Status NEQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, min_max)); + ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), min_max->clone().get()))); - INEKernel::configure(win); + return Status{}; } void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info) -- cgit v1.2.1