From 22f917cfd2282052c447068b188eee0c59f737fd Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Tue, 21 May 2019 13:30:10 +0100 Subject: COMPMID-2240 Implement DEPTH_TO_SPACE for NEON Change-Id: I705aa0f804093c3628c691e46cca475f2819dc65 Signed-off-by: Michalis Spyrou Reviewed-on: https://review.mlplatform.org/c/1198 Comments-Addressed: Arm Jenkins Reviewed-by: Giuseppe Rossini Tested-by: Arm Jenkins --- arm_compute/core/NEON/NEKernels.h | 1 + .../core/NEON/kernels/NEDepthToSpaceLayerKernel.h | 79 +++++++++++ arm_compute/core/utils/misc/ShapeCalculator.h | 24 ++++ arm_compute/runtime/NEON/NEFunctions.h | 1 + .../runtime/NEON/functions/NEDepthToSpaceLayer.h | 59 ++++++++ .../NEON/kernels/NEDepthToSpaceLayerKernel.cpp | 158 +++++++++++++++++++++ src/runtime/NEON/functions/NEDepthToSpaceLayer.cpp | 45 ++++++ tests/datasets/DepthToSpaceDataset.h | 139 ++++++++++++++++++ tests/validation/NEON/DepthToSpaceLayer.cpp | 115 +++++++++++++++ .../validation/fixtures/DepthToSpaceLayerFixture.h | 111 +++++++++++++++ tests/validation/reference/DepthToSpaceLayer.cpp | 75 ++++++++++ tests/validation/reference/DepthToSpaceLayer.h | 44 ++++++ 12 files changed, 851 insertions(+) create mode 100644 arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h create mode 100644 arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h create mode 100644 src/core/NEON/kernels/NEDepthToSpaceLayerKernel.cpp create mode 100644 src/runtime/NEON/functions/NEDepthToSpaceLayer.cpp create mode 100644 tests/datasets/DepthToSpaceDataset.h create mode 100644 tests/validation/NEON/DepthToSpaceLayer.cpp create mode 100644 tests/validation/fixtures/DepthToSpaceLayerFixture.h create mode 100644 tests/validation/reference/DepthToSpaceLayer.cpp create mode 100644 tests/validation/reference/DepthToSpaceLayer.h diff --git a/arm_compute/core/NEON/NEKernels.h b/arm_compute/core/NEON/NEKernels.h index b9716b1e43..d19b0c71ba 100644 --- a/arm_compute/core/NEON/NEKernels.h +++ b/arm_compute/core/NEON/NEKernels.h @@ -50,6 +50,7 @@ #include "arm_compute/core/NEON/kernels/NECumulativeDistributionKernel.h" #include "arm_compute/core/NEON/kernels/NEDepthConcatenateLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEDepthConvertLayerKernel.h" +#include "arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h" #include "arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h" #include "arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h" diff --git a/arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h b/arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h new file mode 100644 index 0000000000..a62da049a5 --- /dev/null +++ b/arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_NEDEPTHTOSPACELAYERKERNEL_H__ +#define __ARM_COMPUTE_NEDEPTHTOSPACELAYERKERNEL_H__ + +#include "arm_compute/core/NEON/INEKernel.h" + +namespace arm_compute +{ +class ITensor; + +/** Interface for the depth to space kernel */ +class NEDepthToSpaceLayerKernel : public INEKernel +{ +public: + const char *name() const override + { + return "NEDepthToSpaceLayerKernel"; + } + /** Default constructor */ + NEDepthToSpaceLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEDepthToSpaceLayerKernel(const NEDepthToSpaceLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEDepthToSpaceLayerKernel &operator=(const NEDepthToSpaceLayerKernel &) = delete; + /** Allow instances of this class to be moved */ + NEDepthToSpaceLayerKernel(NEDepthToSpaceLayerKernel &&) = default; + /** Allow instances of this class to be moved */ + NEDepthToSpaceLayerKernel &operator=(NEDepthToSpaceLayerKernel &&) = default; + /** Default destructor */ + ~NEDepthToSpaceLayerKernel() = default; + /** Initialise the kernel's inputs and output. + * + * @param[in] input Tensor input. Supported tensor rank: 4. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. + * @param[out] output Tensor output. Data types supported: same as @p input + * @param[in] block_shape Block shape x value. + */ + void configure(const ITensor *input, ITensor *output, int32_t block_shape); + /** Static function to check if given info will lead to a valid configuration of @ref NEDepthToSpaceLayerKernel. + * + * @param[in] input Tensor input info. Supported tensor rank: 4. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. + * @param[in] output Tensor output info. Data types supported: same as @p input + * @param[in] block_shape Block shape value. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape); + + // Inherited methods overridden: + void run(const Window &window, const ThreadInfo &info) override; + +private: + const ITensor *_input; /**< Source tensor */ + ITensor *_output; /**< Destination tensor */ + int32_t _block_shape; /**< Block shape */ +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NEDEPTHTOSPACELAYERKERNEL_H__ */ diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index d66c87fb42..8394bbaed8 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -975,6 +975,30 @@ inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const return output_shape; } +/** Calculate the depth to space output shape of a tensor + * + * @param[in] input Input tensor info + * @param[in] block Block shape value + * + * @return the calculated shape + */ +inline TensorShape compute_depth_to_space_shape(const ITensorInfo *input, int block) +{ + ARM_COMPUTE_ERROR_ON(block < 2); + + const DataLayout data_layout = input->data_layout(); + const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); + + TensorShape output_shape{ input->tensor_shape() }; + output_shape.set(idx_width, input->dimension(idx_width) * block); + output_shape.set(idx_height, input->dimension(idx_height) * block); + output_shape.set(idx_channel, input->dimension(idx_channel) / (block * block)); + + return output_shape; +} + /** Calculate the split output shape of a tensor * * @param[in] input Input tensor info diff --git a/arm_compute/runtime/NEON/NEFunctions.h b/arm_compute/runtime/NEON/NEFunctions.h index 9094ee4bdf..971ddfa703 100644 --- a/arm_compute/runtime/NEON/NEFunctions.h +++ b/arm_compute/runtime/NEON/NEFunctions.h @@ -53,6 +53,7 @@ #include "arm_compute/runtime/NEON/functions/NECropResize.h" #include "arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h" #include "arm_compute/runtime/NEON/functions/NEDepthConvertLayer.h" +#include "arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h" #include "arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h" #include "arm_compute/runtime/NEON/functions/NEDepthwiseSeparableConvolutionLayer.h" #include "arm_compute/runtime/NEON/functions/NEDequantizationLayer.h" diff --git a/arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h b/arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h new file mode 100644 index 0000000000..54a6ddaefc --- /dev/null +++ b/arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_NEDEPTHTOSPACELAYER_H__ +#define __ARM_COMPUTE_NEDEPTHTOSPACELAYER_H__ + +#include "arm_compute/runtime/IFunction.h" + +#include "arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/INESimpleFunctionNoBorder.h" + +namespace arm_compute +{ +class ITensor; + +/** Basic function to run @ref NEDepthToSpaceLayerKernel. */ +class NEDepthToSpaceLayer : public INESimpleFunctionNoBorder +{ +public: + /** Set the input and output tensors. + * + * @param[in] input Tensor input. Supported tensor rank: 4. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. + * @param[out] output Tensor output. Data types supported: same as @p input + * @param[in] block_shape Block shape value. + */ + void configure(const ITensor *input, ITensor *output, int32_t block_shape); + /** Static function to check if given info will lead to a valid configuration of @ref NEDepthToSpaceLayer. + * + * @param[in] input Tensor input info. Supported tensor rank: 4. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. + * @param[in] output Tensor output info. Data types supported: same as @p input + * @param[in] block_shape Block shape x value. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NEDEPTHTOSPACELAYER_H__ */ diff --git a/src/core/NEON/kernels/NEDepthToSpaceLayerKernel.cpp b/src/core/NEON/kernels/NEDepthToSpaceLayerKernel.cpp new file mode 100644 index 0000000000..df631c3c03 --- /dev/null +++ b/src/core/NEON/kernels/NEDepthToSpaceLayerKernel.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/NEON/kernels/NEDepthToSpaceLayerKernel.h" + +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/NEON/wrapper/wrapper.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include +#include + +using namespace arm_compute::misc::shape_calculator; + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); + ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 2); + + const DataLayout data_layout = input->data_layout(); + const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); + ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0); + // Validate output if initialized + if(output->total_size() != 0) + { + const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != (block_shape * input->tensor_shape()[idx_width])); + ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != (block_shape * input->tensor_shape()[idx_height])); + ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + } + + return Status{}; +} +} // namespace + +NEDepthToSpaceLayerKernel::NEDepthToSpaceLayerKernel() + : _input(nullptr), _output(nullptr), _block_shape() +{ +} + +void NEDepthToSpaceLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + TensorShape output_shape = compute_depth_to_space_shape(input->info(), block_shape); + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); + + // Perform validation step + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape)); + + _input = input; + _output = output; + _block_shape = block_shape; + + // Configure kernel window + Window win = calculate_max_window(*input->info(), Steps()); + ICPPKernel::configure(win); +} + +Status NEDepthToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape)); + return Status{}; +} + +void NEDepthToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info) +{ + ARM_COMPUTE_UNUSED(info); + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); + + const int idx_channel = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL); + const int depth_size = _input->info()->dimension(idx_channel); + const int r = (depth_size / (_block_shape * _block_shape)); + const int element_size = _input->info()->element_size(); + + Window slice_out = window.first_slice_window_3D(); + + // The slice_out slice does not move + slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); + slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); + slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); + + // Main loop for NCHW and NHWC + if(_input->info()->data_layout() == DataLayout::NCHW) + { + Window slice_in = window.first_slice_window_2D(); + do + { + Iterator in(_input, slice_in); + execute_window_loop(slice_in, [&](const Coordinates & id) + { + const int x = id.x(); + const int y = id.y(); + + const int z = id.z() % r; + const int out_x = x * _block_shape + (id.z() / r) % _block_shape; + const int out_y = y * _block_shape + (id.z() / r) / _block_shape; + Coordinates output_coords{ out_x, out_y, z, id[3] }; + memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size); + }, + in); + } + while(window.slide_window_slice_2D(slice_in)); + } + else + { + Window slice_in = window.first_slice_window_3D(); + do + { + Iterator in(_input, slice_in); + execute_window_loop(slice_in, [&](const Coordinates & id) + { + const int x = id.y(); + const int y = id.z(); + + const int z = id.x() % r; + const int out_x = x * _block_shape + (id.x() / r) % _block_shape; + const int out_y = y * _block_shape + (id.x() / r) / _block_shape; + Coordinates output_coords{ z, out_x, out_y, id[3] }; + memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size); + }, + in); + } + while(window.slide_window_slice_3D(slice_in)); + } +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEDepthToSpaceLayer.cpp b/src/runtime/NEON/functions/NEDepthToSpaceLayer.cpp new file mode 100644 index 0000000000..3569eecf0a --- /dev/null +++ b/src/runtime/NEON/functions/NEDepthToSpaceLayer.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" + +namespace arm_compute +{ +void NEDepthToSpaceLayer::configure(const ITensor *input, ITensor *output, int32_t block_shape) +{ + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(input, output, block_shape); + _kernel = std::move(k); +} + +Status NEDepthToSpaceLayer::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) +{ + return NEDepthToSpaceLayerKernel::validate(input, output, block_shape); +} +} // namespace arm_compute diff --git a/tests/datasets/DepthToSpaceDataset.h b/tests/datasets/DepthToSpaceDataset.h new file mode 100644 index 0000000000..06ee5ba432 --- /dev/null +++ b/tests/datasets/DepthToSpaceDataset.h @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_DATASET +#define ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class DepthToSpaceLayerDataset +{ +public: + using type = std::tuple; + + struct iterator + { + iterator(std::vector::const_iterator src_it, + std::vector::const_iterator block_shape_it, + std::vector::const_iterator dst_it) + : _src_it{ std::move(src_it) }, + _block_shape_it{ std::move(block_shape_it) }, + _dst_it{ std::move(dst_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_src_it << ":"; + description << "BlockShape=" << *_block_shape_it << ":"; + description << "Out=" << *_dst_it; + return description.str(); + } + + DepthToSpaceLayerDataset::type operator*() const + { + return std::make_tuple(*_src_it, *_block_shape_it, *_dst_it); + } + + iterator &operator++() + { + ++_src_it; + ++_block_shape_it; + ++_dst_it; + + return *this; + } + + private: + std::vector::const_iterator _src_it; + std::vector::const_iterator _block_shape_it; + std::vector::const_iterator _dst_it; + }; + + iterator begin() const + { + return iterator(_src_shapes.begin(), _block_shape.begin(), _dst_shapes.begin()); + } + + int size() const + { + return std::min(_src_shapes.size(), std::min(_block_shape.size(), _dst_shapes.size())); + } + + void add_config(TensorShape src, int32_t block_shape, TensorShape dst) + { + _src_shapes.emplace_back(std::move(src)); + _block_shape.emplace_back(std::move(block_shape)); + _dst_shapes.emplace_back(std::move(dst)); + } + +protected: + DepthToSpaceLayerDataset() = default; + DepthToSpaceLayerDataset(DepthToSpaceLayerDataset &&) = default; + +private: + std::vector _src_shapes{}; + std::vector _block_shape{}; + std::vector _dst_shapes{}; +}; + +class SmallDepthToSpaceLayerDataset final : public DepthToSpaceLayerDataset +{ +public: + SmallDepthToSpaceLayerDataset() + { + add_config(TensorShape(1U, 1U, 4U, 4U), 2U, TensorShape(2U, 2U, 1U, 4U)); + add_config(TensorShape(3U, 1U, 4U, 4U), 2U, TensorShape(6U, 2U, 1U, 4U)); + add_config(TensorShape(1U, 2U, 4U, 4U), 2U, TensorShape(2U, 4U, 1U, 4U)); + add_config(TensorShape(1U, 3U, 4U, 8U), 2U, TensorShape(2U, 6U, 1U, 8U)); + add_config(TensorShape(3U, 4U, 4U, 4U), 2U, TensorShape(6U, 8U, 1U, 4U)); + add_config(TensorShape(1U, 1U, 16U, 8U), 4U, TensorShape(4U, 4U, 1U, 8U)); + add_config(TensorShape(1U, 1U, 8U), 2U, TensorShape(2U, 2U, 2U)); + } +}; + +class LargeDepthToSpaceLayerDataset final : public DepthToSpaceLayerDataset +{ +public: + LargeDepthToSpaceLayerDataset() + { + add_config(TensorShape(64U, 32U, 4U), 2U, TensorShape(128U, 64U, 1U)); + add_config(TensorShape(128U, 16U, 16U), 4U, TensorShape(512U, 64U, 1U)); + add_config(TensorShape(16U, 8U, 8U), 2U, TensorShape(32U, 16U, 2U)); + add_config(TensorShape(8U, 16U, 16U), 2U, TensorShape(16U, 32U, 4U)); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_DATASET */ diff --git a/tests/validation/NEON/DepthToSpaceLayer.cpp b/tests/validation/NEON/DepthToSpaceLayer.cpp new file mode 100644 index 0000000000..abc8c4f266 --- /dev/null +++ b/tests/validation/NEON/DepthToSpaceLayer.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/functions/NEDepthToSpaceLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/DepthToSpaceDataset.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/DepthToSpaceLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(NEON) +TEST_SUITE(DepthToSpaceLayer) + +template +using NEDepthToSpaceLayerFixture = DepthToSpaceLayerValidationFixture; + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(16U, 8U, 4U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 8U, 4U, 4U), 1, DataType::F32), // block < 2 + TensorInfo(TensorShape(16U, 8U, 2U, 4U), 1, DataType::F32), // Mismatching data types + TensorInfo(TensorShape(16U, 8U, 2U, 4U), 1, DataType::F32), // Negative block shape + TensorInfo(TensorShape(32U, 16U, 2U, 4U, 4U), 1, DataType::F32), // Wrong tensor shape + }), + framework::dataset::make("BlockShape", { 2, 1, 2, 2, 2 })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 16U, 1U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 16U, 1U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 16U, 2U, 1U), 1, DataType::F16), + TensorInfo(TensorShape(32U, 16U, 2U, 1U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 8U, 2U, 1U), 1, DataType::F32), + })), + framework::dataset::make("Expected", { true, false, false, false, false})), + input_info, block_shape, output_info, expected) +{ + bool has_error = bool(NEDepthToSpaceLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), block_shape)); + ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NEDepthToSpaceLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEDepthToSpaceLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() + +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEDepthToSpaceLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEDepthToSpaceLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeDepthToSpaceLayerDataset(), framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() // DepthToSpace +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/DepthToSpaceLayerFixture.h b/tests/validation/fixtures/DepthToSpaceLayerFixture.h new file mode 100644 index 0000000000..b42bed52a7 --- /dev/null +++ b/tests/validation/fixtures/DepthToSpaceLayerFixture.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_FIXTURE +#define ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_FIXTURE + +#include "tests/Globals.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/reference/DepthToSpaceLayer.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class DepthToSpaceLayerValidationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape input_shape, int32_t block_shape, TensorShape output_shape, DataType data_type, DataLayout data_layout) + { + _target = compute_target(input_shape, block_shape, output_shape, data_type, data_layout); + _reference = compute_reference(input_shape, block_shape, output_shape, data_type); + } + +protected: + template + void fill(U &&tensor, int i) + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + } + TensorType compute_target(TensorShape input_shape, int32_t block_shape, TensorShape output_shape, + DataType data_type, DataLayout data_layout) + { + if(data_layout == DataLayout::NHWC) + { + permute(input_shape, PermutationVector(2U, 0U, 1U)); + permute(output_shape, PermutationVector(2U, 0U, 1U)); + } + + // Create tensors + TensorType input = create_tensor(input_shape, data_type, 1, QuantizationInfo(), data_layout); + TensorType output = create_tensor(output_shape, data_type, 1, QuantizationInfo(), data_layout); + + // Create and configure function + FunctionType depth_to_space; + depth_to_space.configure(&input, &output, block_shape); + + ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + input.allocator()->allocate(); + output.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(input), 0); + + // Compute function + depth_to_space.run(); + + return output; + } + + SimpleTensor compute_reference(const TensorShape &input_shape, int32_t block_shape, + const TensorShape &output_shape, DataType data_type) + { + // Create reference + SimpleTensor input{ input_shape, data_type }; + + // Fill reference + fill(input, 0); + + // Compute reference + return reference::depth_to_space(input, output_shape, block_shape); + } + + TensorType _target{}; + SimpleTensor _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_FIXTURE */ diff --git a/tests/validation/reference/DepthToSpaceLayer.cpp b/tests/validation/reference/DepthToSpaceLayer.cpp new file mode 100644 index 0000000000..4135ce5471 --- /dev/null +++ b/tests/validation/reference/DepthToSpaceLayer.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "DepthToSpaceLayer.h" + +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +// Batch to Space +template +SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape) +{ + ARM_COMPUTE_ERROR_ON(block_shape <= 0); + SimpleTensor result(dst_shape, src.data_type()); + + int in_pos = 0; + const auto width_in = static_cast(src.shape()[0]); + const auto height_in = static_cast(src.shape()[1]); + const auto channel_in = static_cast(src.shape()[2]); + const auto batch_in = static_cast(src.shape()[3]); + const int r = channel_in / (block_shape * block_shape); + + for(int b = 0; b < batch_in; ++b) + { + for(int z = 0; z < channel_in; ++z) + { + for(int y = 0; y < height_in; ++y) + { + for(int x = 0; x < width_in; ++x) + { + const int out_x = (block_shape * x + (z / r) % block_shape); + const int out_y = (block_shape * y + (z / r) / block_shape); + const int out_pos = out_x + dst_shape[0] * out_y + (z % r) * dst_shape[0] * dst_shape[1] + b * dst_shape[0] * dst_shape[1] * dst_shape[2]; + result[out_pos] = src[in_pos]; + ++in_pos; + } + } + } + } + + return result; +} +template SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape); +template SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/DepthToSpaceLayer.h b/tests/validation/reference/DepthToSpaceLayer.h new file mode 100644 index 0000000000..3989401c08 --- /dev/null +++ b/tests/validation/reference/DepthToSpaceLayer.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_H__ +#define __ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor depth_to_space(const SimpleTensor &src, const TensorShape &dst_shape, int32_t block_shape); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_DEPTH_TO_SPACE_LAYER_H__ */ -- cgit v1.2.1