From 5c4a8e96460eb83a6caef1c69ea5cbb4893858d7 Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Wed, 28 Aug 2019 17:55:07 +0100 Subject: COMPMID-2592 Create a new kernel for CLPad with SYMMETRIC and REFLECT mode Change-Id: Icaf0516f490b2ddca6d1ea03a5cf26cc7d43041f Signed-off-by: Giorgio Arena Reviewed-on: https://review.mlplatform.org/c/1872 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Gian Marco Iodice --- src/runtime/CL/functions/CLPadLayer.cpp | 245 +++----------------------------- 1 file changed, 20 insertions(+), 225 deletions(-) (limited to 'src/runtime/CL/functions/CLPadLayer.cpp') diff --git a/src/runtime/CL/functions/CLPadLayer.cpp b/src/runtime/CL/functions/CLPadLayer.cpp index 88b1b77a0d..8f36a69866 100644 --- a/src/runtime/CL/functions/CLPadLayer.cpp +++ b/src/runtime/CL/functions/CLPadLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 ARM Limited. + * Copyright (c) 2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -23,155 +23,25 @@ */ #include "arm_compute/runtime/CL/functions/CLPadLayer.h" -#include "arm_compute/core/utils/misc/ShapeCalculator.h" - namespace arm_compute { CLPadLayer::CLPadLayer() - : _pad_kernel(), _copy_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results() + : _pad_kernel(), _copy_kernel(), _perform_pad(false) { } -void CLPadLayer::configure_reflect_symmetric_mode(ICLTensor *input, ICLTensor *output) -{ - int64_t last_padding_dimension = _padding.size() - 1; - // Reflecting can be performed by effectively unfolding the input as follows: - // For each dimension starting at DimX: - // Create a before and after slice, which values depend on the selected padding mode - // Concatenate the before and after padding with the tensor to be padded - - // Two strided slice functions will be required for each dimension padded as well as a - // concatenate function and the tensors to hold the temporary results. - _slice_functions.resize(2 * _num_dimensions); - _slice_results.resize(2 * _num_dimensions); - _concat_functions.resize(_num_dimensions); - _concat_results.resize(_num_dimensions - 1); - - Coordinates starts_before{}; - Coordinates ends_before{}; - Coordinates starts_after{}; - Coordinates ends_after{}; - Coordinates strides{}; - ICLTensor *prev = input; - for(uint32_t i = 0; i < _num_dimensions; ++i) - { - // Values in strides from the previous dimensions need to be set to 1 to avoid reversing again. - if(i > 0) - { - strides.set(i - 1, 1); - } - - if(_padding[i].first > 0 || _padding[i].second > 0) - { - // Set the starts, ends, and strides values for the current dimension. - // Due to the bit masks passed to strided slice, the values below the current dimension in - // starts and ends will be ignored so do not need to be modified. - if(_mode == PaddingMode::REFLECT) - { - starts_before.set(i, _padding[i].first); - ends_before.set(i, 0); - starts_after.set(i, input->info()->dimension(i) - 2); - ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 2); - strides.set(i, -1); - } - else - { - starts_before.set(i, _padding[i].first - 1); - ends_before.set(i, -1); - starts_after.set(i, input->info()->dimension(i) - 1); - ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 1); - strides.set(i, -1); - } - - // Strided slice wraps negative indexes around to the end of the range, - // instead this should indicate use of the full range and so the bit mask will be modified. - const int32_t begin_mask_before = starts_before[i] < 0 ? ~0 : ~(1u << i); - const int32_t end_mask_before = ends_before[i] < 0 ? ~0 : ~(1u << i); - const int32_t begin_mask_after = starts_after[i] < 0 ? ~0 : ~(1u << i); - const int32_t end_mask_after = ends_after[i] < 0 ? ~0 : ~(1u << i); - - // Reflect the input values for the padding before and after the input. - std::vector concat_vector; - if(_padding[i].first > 0) - { - if(i < prev->info()->num_dimensions()) - { - _slice_functions[2 * i].configure(prev, &_slice_results[2 * i], starts_before, ends_before, strides, begin_mask_before, end_mask_before); - concat_vector.push_back(&_slice_results[2 * i]); - } - else - { - // Performing the slice is unnecessary if the result would simply be a copy of the tensor. - concat_vector.push_back(prev); - } - } - concat_vector.push_back(prev); - if(_padding[i].second > 0) - { - if(i < prev->info()->num_dimensions()) - { - _slice_functions[2 * i + 1].configure(prev, &_slice_results[2 * i + 1], starts_after, ends_after, strides, begin_mask_after, end_mask_after); - concat_vector.push_back(&_slice_results[2 * i + 1]); - } - else - { - // Performing the slice is unnecessary if the result would simply be a copy of the tensor. - concat_vector.push_back(prev); - } - } - // Concatenate the padding before and after with the input. - ICLTensor *out = (static_cast(i) == last_padding_dimension) ? output : &_concat_results[i]; - _concat_functions[i].configure(concat_vector, out, i); - prev = out; - } - } - for(uint32_t i = 0; i < _num_dimensions; ++i) - { - if((static_cast(i) != last_padding_dimension)) - { - _concat_results[i].allocator()->allocate(); - } - _slice_results[2 * i].allocator()->allocate(); - _slice_results[2 * i + 1].allocator()->allocate(); - } -} void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) { - _padding = padding; - _mode = mode; - - TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding); - auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape)); ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode)); - // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied. - int64_t last_padding_dimension = _padding.size() - 1; - for(; last_padding_dimension >= 0; --last_padding_dimension) + _perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info) { - if(_padding[last_padding_dimension].first > 0 || _padding[last_padding_dimension].second > 0) - { - break; - } - } - _num_dimensions = last_padding_dimension + 1; - if(_num_dimensions > 0) + return info.first > 0 || info.second > 0; + }); + + if(_perform_pad) { - switch(_mode) - { - case PaddingMode::CONSTANT: - { - _pad_kernel.configure(input, output, padding, constant_value, mode); - break; - } - case PaddingMode::REFLECT: - case PaddingMode::SYMMETRIC: - { - configure_reflect_symmetric_mode(input, output); - break; - } - default: - ARM_COMPUTE_ERROR("Padding mode not supported."); - } + _pad_kernel.configure(input, output, padding, constant_value, mode); } else { @@ -179,109 +49,34 @@ void CLPadLayer::configure(ICLTensor *input, ICLTensor *output, const PaddingLis _copy_kernel.configure(input, output); } } - Status CLPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) { - ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > input->num_dimensions()); - TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding); - - // Use CLCopyKernel and CLMemsetKernel to validate all padding modes as this includes all of the shape and info validation. - PaddingList padding_extended = padding; - for(size_t i = padding.size(); i < TensorShape::num_max_dimensions; i++) + bool perform_pad = std::any_of(padding.begin(), padding.end(), [](PaddingInfo info) { - padding_extended.emplace_back(PaddingInfo{ 0, 0 }); - } + return info.first > 0 || info.second > 0; + }); - Window copy_window = Window(); - for(uint32_t i = 0; i < padded_shape.num_dimensions(); ++i) + if(perform_pad) { - copy_window.set(i, Window::Dimension(padding_extended[i].first, padding_extended[i].first + input->dimension(i), 1)); - } - if(output->total_size() > 0) - { - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(output, input); - ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output, PaddingList(), ©_window)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode)); } else { - ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, &input->clone()->set_tensor_shape(padded_shape), PaddingList(), ©_window)); - } - - switch(mode) - { - case PaddingMode::CONSTANT: - { - ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4); - ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(input, output, padding, constant_value, mode)); - break; - } - case PaddingMode::REFLECT: - case PaddingMode::SYMMETRIC: - { - for(uint32_t i = 0; i < padding.size(); ++i) - { - if(mode == PaddingMode::REFLECT) - { - ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i)); - ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i)); - } - else - { - ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i)); - ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i)); - } - } - break; - } - default: - { - ARM_COMPUTE_ERROR("Invalid mode"); - } + Window copy_window = Window(); + copy_window.use_tensor_dimensions(output->tensor_shape()); + ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output, PaddingList(), ©_window)); } return Status{}; } - void CLPadLayer::run() { - if(_num_dimensions > 0) + if(_perform_pad) { - switch(_mode) - { - case PaddingMode::CONSTANT: - { - CLScheduler::get().enqueue(_pad_kernel, false); - break; - } - case PaddingMode::REFLECT: - case PaddingMode::SYMMETRIC: - { - for(uint32_t i = 0; i < _num_dimensions; ++i) - { - if(_padding[i].first > 0 || _padding[i].second > 0) - { - if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0) - { - _slice_functions[2 * i].run(); - } - if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0) - { - _slice_functions[2 * i + 1].run(); - } - CLScheduler::get().sync(); - _concat_functions[i].run(); - CLScheduler::get().sync(); - } - } - break; - } - default: - ARM_COMPUTE_ERROR("Padding mode not supported."); - } + CLScheduler::get().enqueue(_pad_kernel); } else { - CLScheduler::get().enqueue(_copy_kernel, true); + CLScheduler::get().enqueue(_copy_kernel); } } -} // namespace arm_compute +} // namespace arm_compute \ No newline at end of file -- cgit v1.2.1