From 60f3911996871e6163ce5a9a2dfca02125db8ecf Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Mon, 18 Mar 2019 15:25:15 +0000 Subject: COMPMID-2008: Add support for "reflect" padding mode in CLPad Change-Id: I469f8173d5c4a1b6f03b52b9ddd33928dacd1e7b Signed-off-by: Manuel Bottini Reviewed-on: https://review.mlplatform.org/c/869 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Reviewed-by: Giuseppe Rossini --- arm_compute/runtime/CL/functions/CLPadLayer.h | 20 +- src/runtime/CL/functions/CLPadLayer.cpp | 275 ++++++++++++++++++++++++-- tests/validation/CL/PadLayer.cpp | 89 +++++---- 3 files changed, 321 insertions(+), 63 deletions(-) diff --git a/arm_compute/runtime/CL/functions/CLPadLayer.h b/arm_compute/runtime/CL/functions/CLPadLayer.h index 0179441af2..33b09d60a2 100644 --- a/arm_compute/runtime/CL/functions/CLPadLayer.h +++ b/arm_compute/runtime/CL/functions/CLPadLayer.h @@ -25,10 +25,13 @@ #define __ARM_COMPUTE_CLPADLAYER_H__ #include "arm_compute/core/CL/kernels/CLCopyKernel.h" -#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h" #include "arm_compute/core/CL/kernels/CLMemsetKernel.h" #include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/functions/CLConcatenateLayer.h" + #include "arm_compute/runtime/CL/CLScheduler.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/functions/CLStridedSlice.h" #include "arm_compute/runtime/IFunction.h" namespace arm_compute @@ -77,9 +80,18 @@ public: void run() override; private: - CLCopyKernel _copy_kernel; - CLFillBorderKernel _fillborder_kernel; - CLMemsetKernel _memset_kernel; + void configure_constant_mode(ICLTensor *input, ICLTensor *output, const PaddingList &padding, const PixelValue constant_value); + void configure_reflect_symmetric_mode(ICLTensor *input, ICLTensor *output); + + CLCopyKernel _copy_kernel; + PaddingMode _mode; + PaddingList _padding; + CLMemsetKernel _memset_kernel; + size_t _num_dimensions; + std::unique_ptr _slice_functions; + std::unique_ptr _concat_functions; + std::unique_ptr _slice_results; + std::unique_ptr _concat_results; }; } // namespace arm_compute #endif /*__ARM_COMPUTE_PADLAYER_H__ */ diff --git a/src/runtime/CL/functions/CLPadLayer.cpp b/src/runtime/CL/functions/CLPadLayer.cpp index fac2364ae5..f88cb388be 100644 --- a/src/runtime/CL/functions/CLPadLayer.cpp +++ b/src/runtime/CL/functions/CLPadLayer.cpp @@ -25,41 +25,288 @@ #include "arm_compute/core/CL/ICLTensor.h" #include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "support/ToolchainSupport.h" namespace arm_compute { CLPadLayer::CLPadLayer() - : _copy_kernel(), _fillborder_kernel(), _memset_kernel() + : _copy_kernel(), _mode(), _padding(), _memset_kernel(), _num_dimensions(0), _slice_functions(nullptr), _concat_functions(nullptr), _slice_results(nullptr), _concat_results(nullptr) { } +void CLPadLayer::configure_constant_mode(ICLTensor *input, ICLTensor *output, const PaddingList &padding, const PixelValue constant_value) +{ + // Set the pages of the output to the constant_value. + _memset_kernel.configure(output, constant_value); + + // Fill out padding list with zeroes. + PaddingList padding_extended = padding; + for(size_t i = padding.size(); i < TensorShape::num_max_dimensions; i++) + { + padding_extended.emplace_back(PaddingInfo{ 0, 0 }); + } + + // Create a window within the output tensor where the input will be copied. + Window copy_window = Window(); + for(uint32_t i = 0; i < output->info()->num_dimensions(); ++i) + { + copy_window.set(i, Window::Dimension(padding_extended[i].first, padding_extended[i].first + input->info()->dimension(i), 1)); + } + // Copy the input to the output, leaving the padding filled with the constant_value. + _copy_kernel.configure(input, output, PaddingList(), ©_window); +} + +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 = arm_compute::support::cpp14::make_unique(2 * _num_dimensions); + _slice_results = arm_compute::support::cpp14::make_unique(2 * _num_dimensions); + _concat_functions = arm_compute::support::cpp14::make_unique(_num_dimensions); + _concat_results = arm_compute::support::cpp14::make_unique(_num_dimensions - 1); + Coordinates starts_before, ends_before, starts_after, ends_after, 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, get_index_data_layout_dimension(prev->info()->data_layout(), 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) { - ARM_COMPUTE_UNUSED(mode); - // Copy the input to the output - _copy_kernel.configure(input, output, padding); + ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode)); - // Set the pages of the output to zero - _memset_kernel.configure(output, constant_value); + _padding = padding; + _mode = mode; + + TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding); - // Fill padding on the first two dimensions with zeros - _fillborder_kernel.configure(input, input->info()->padding(), BorderMode::CONSTANT, constant_value); + auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape)); + + // 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) + { + if(_padding[last_padding_dimension].first > 0 || _padding[last_padding_dimension].second > 0) + { + break; + } + } + _num_dimensions = last_padding_dimension + 1; + if(_num_dimensions > 0) + { + switch(_mode) + { + case PaddingMode::CONSTANT: + { + configure_constant_mode(input, output, padding, constant_value); + break; + } + case PaddingMode::REFLECT: + case PaddingMode::SYMMETRIC: + { + configure_reflect_symmetric_mode(input, output); + break; + } + default: + ARM_COMPUTE_ERROR("Padding mode not supported."); + } + } + else + { + // Copy the input to the whole output if no padding is applied + _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_ON_ERROR(CLMemsetKernel::validate(input, constant_value)); - ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output, padding)); - ARM_COMPUTE_RETURN_ERROR_ON(mode != PaddingMode::CONSTANT); + 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++) + { + padding_extended.emplace_back(PaddingInfo{ 0, 0 }); + } + + Window copy_window = Window(); + for(uint32_t i = 0; i < padded_shape.num_dimensions(); ++i) + { + 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(CLMemsetKernel::validate(output, constant_value)); + } + else + { + ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, &input->clone()->set_tensor_shape(padded_shape), PaddingList(), ©_window)); + ARM_COMPUTE_RETURN_ON_ERROR(CLMemsetKernel::validate(&input->clone()->set_tensor_shape(padded_shape), constant_value)); + } + switch(mode) + { + case PaddingMode::CONSTANT: + { + 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"); + } + } return Status{}; } void CLPadLayer::run() { - CLScheduler::get().enqueue(_memset_kernel, false); - CLScheduler::get().enqueue(_fillborder_kernel, false); - CLScheduler::get().enqueue(_copy_kernel, true); + if(_num_dimensions > 0) + { + switch(_mode) + { + case PaddingMode::CONSTANT: + { + CLScheduler::get().enqueue(_memset_kernel, false); + CLScheduler::get().enqueue(_copy_kernel, true); + 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."); + } + } + else + { + CLScheduler::get().enqueue(_copy_kernel, true); + } } } // namespace arm_compute diff --git a/tests/validation/CL/PadLayer.cpp b/tests/validation/CL/PadLayer.cpp index 9430b1212b..2ad29fc0e5 100644 --- a/tests/validation/CL/PadLayer.cpp +++ b/tests/validation/CL/PadLayer.cpp @@ -43,9 +43,9 @@ namespace const auto PaddingSizesDataset = framework::dataset::make("PaddingSize", { PaddingList{ { 0, 0 } }, PaddingList{ { 1, 1 } }, PaddingList{ { 1, 1 }, { 2, 2 } }, - PaddingList{ { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 } }, - PaddingList{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 2 } }, - PaddingList{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 1, 1 } } + PaddingList{ { 1, 1 }, { 1, 1 }, { 1, 1 } }, + PaddingList{ { 0, 0 }, { 1, 0 }, { 0, 1 } }, + PaddingList{ { 0, 0 }, { 0, 0 }, { 0, 0 } } }); } // namespace @@ -55,32 +55,44 @@ TEST_SUITE(PadLayer) // *INDENT-OFF* // clang-format off -DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data type input/output - TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes with padding TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes dimension TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), - TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), - TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32) + TensorInfo(TensorShape(32U, 13U), 1, DataType::F32) // Invalid padding list }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(28U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(29U, 17U, 2U), 1, DataType::F32), TensorInfo(TensorShape(29U, 15U, 4U, 3U), 1, DataType::F32), - TensorInfo(TensorShape(27U, 14U, 3U, 4U), 1, DataType::F32), - TensorInfo(TensorShape(32U, 13U, 2U, 3U), 1, DataType::F32) + TensorInfo(TensorShape(29U, 17U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U), 1, DataType::F32) })), framework::dataset::make("PaddingSize", { PaddingList{{0, 0}}, - PaddingList{{1, 1}}, - PaddingList{{1, 1}, {2, 2}}, - PaddingList{{1,1}, {1,1}, {1,1}, {1,1}}, - PaddingList{{0,0}, {1,0}, {0,1}, {1,2}}, - PaddingList{{0,0}, {0,0}, {0,0}, {1,1}} - })), - framework::dataset::make("Expected", { false, false, true, true, true, true })), - input_info, output_info, padding, expected) + PaddingList{{1, 1}}, + PaddingList{{1, 1}, {2, 2}}, + PaddingList{{1,1}, {1,1}, {1,1}}, + PaddingList{{1, 1}, {2, 2}}, + PaddingList{{0,0}, {0,0}, {1,1}} + })), + framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT, + PaddingMode::CONSTANT, + PaddingMode::CONSTANT, + PaddingMode::SYMMETRIC, + PaddingMode::REFLECT, + PaddingMode::REFLECT +})), + framework::dataset::make("Expected", { false, + false, + true, + false, + true, + false })), + input_info, output_info, padding, mode, expected) { - ARM_COMPUTE_EXPECT(bool(CLPadLayer::validate(&input_info.clone()->set_is_resizable(true), &output_info.clone()->set_is_resizable(true), padding)) == expected, framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bool(CLPadLayer::validate(&input_info.clone()->set_is_resizable(true), &output_info.clone()->set_is_resizable(true), padding, PixelValue(), mode)) == expected, framework::LogLevel::ERRORS); } // clang-format on @@ -92,11 +104,9 @@ using CLPaddingFixture = PaddingFixture; TEST_SUITE(Float) TEST_SUITE(FP32) -FIXTURE_DATA_TEST_CASE(RunPadding, CLPaddingFixture, framework::DatasetMode::ALL, - combine( - combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", { DataType::F32 })), - PaddingSizesDataset), - framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT }))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLPaddingFixture, framework::DatasetMode::ALL, + combine(combine(combine(datasets::Small3DShapes(), framework::dataset::make("DataType", { DataType::F32 })), PaddingSizesDataset), + framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT, PaddingMode::REFLECT, PaddingMode::SYMMETRIC }))) { // Validate output validate(CLAccessor(_target), _reference); @@ -104,11 +114,9 @@ FIXTURE_DATA_TEST_CASE(RunPadding, CLPaddingFixture, framework::DatasetMo TEST_SUITE_END() // FP32 TEST_SUITE(FP16) -FIXTURE_DATA_TEST_CASE(RunPadding, CLPaddingFixture, framework::DatasetMode::ALL, - combine( - combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", { DataType::F16 })), - PaddingSizesDataset), - framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT }))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLPaddingFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(datasets::Large3DShapes(), framework::dataset::make("DataType", { DataType::F16 })), PaddingSizesDataset), + framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT, PaddingMode::REFLECT }))) { // Validate output validate(CLAccessor(_target), _reference); @@ -116,27 +124,18 @@ FIXTURE_DATA_TEST_CASE(RunPadding, CLPaddingFixture, framework::DatasetMod TEST_SUITE_END() // FP16 TEST_SUITE_END() // Float -TEST_SUITE(Integer) -TEST_SUITE(S8) -FIXTURE_DATA_TEST_CASE(RunPadding, CLPaddingFixture, framework::DatasetMode::ALL, - combine( - combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", { DataType::S8 })), - PaddingSizesDataset), - framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT }))) +TEST_SUITE(Quantized) +TEST_SUITE(QASYMM8) +FIXTURE_DATA_TEST_CASE(RunSmall, CLPaddingFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine(datasets::Small3DShapes(), framework::dataset::make("DataType", { DataType::QASYMM8 })), PaddingSizesDataset), + framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT, PaddingMode::REFLECT }))) { // Validate output validate(CLAccessor(_target), _reference); } -TEST_SUITE_END() // S8 -TEST_SUITE_END() // Integer - -TEST_SUITE(Quantized) -TEST_SUITE(QASYMM8) -FIXTURE_DATA_TEST_CASE(RunPadding, CLPaddingFixture, framework::DatasetMode::ALL, - combine( - combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", { DataType::QASYMM8 })), - PaddingSizesDataset), - framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT }))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLPaddingFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(datasets::Large3DShapes(), framework::dataset::make("DataType", { DataType::QASYMM8 })), PaddingSizesDataset), + framework::dataset::make("PaddingMode", { PaddingMode::CONSTANT, PaddingMode::REFLECT }))) { // Validate output validate(CLAccessor(_target), _reference); -- cgit v1.2.1