Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_BATCH_TO_SPACE_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_BATCH_TO_SPACE_LAYER_FIXTURE |
| 26 | |
| 27 | #include "tests/Globals.h" |
| 28 | #include "tests/framework/Asserts.h" |
| 29 | #include "tests/framework/Fixture.h" |
| 30 | #include "tests/validation/reference/BatchToSpaceLayer.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 39 | class BatchToSpaceLayerValidationFixture : public framework::Fixture |
| 40 | { |
| 41 | public: |
| 42 | template <typename...> |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 43 | void setup(TensorShape input_shape, TensorShape block_shape_shape, TensorShape output_shape, DataType data_type, DataLayout data_layout) |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 44 | { |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 45 | _target = compute_target(input_shape, block_shape_shape, output_shape, data_type, data_layout); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 46 | _reference = compute_reference(input_shape, block_shape_shape, output_shape, data_type); |
| 47 | } |
| 48 | |
| 49 | protected: |
| 50 | template <typename U> |
| 51 | void fill(U &&tensor, int i) |
| 52 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 53 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 54 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 55 | |
| 56 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 57 | library->fill(tensor, distribution, i); |
| 58 | } |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 59 | TensorType compute_target(TensorShape input_shape, TensorShape block_shape_shape, TensorShape output_shape, |
| 60 | DataType data_type, DataLayout data_layout) |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 61 | { |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 62 | if(data_layout == DataLayout::NHWC) |
| 63 | { |
| 64 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 65 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 66 | } |
| 67 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 68 | // Create tensors |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 69 | TensorType input = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 70 | TensorType block_shape = create_tensor<TensorType>(block_shape_shape, DataType::S32); |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 71 | TensorType output = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo(), data_layout); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 72 | |
| 73 | // Create and configure function |
| 74 | FunctionType batch_to_space; |
| 75 | batch_to_space.configure(&input, &block_shape, &output); |
| 76 | |
| 77 | ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 78 | ARM_COMPUTE_EXPECT(block_shape.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 79 | ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 80 | |
| 81 | // Allocate tensors |
| 82 | input.allocator()->allocate(); |
| 83 | block_shape.allocator()->allocate(); |
| 84 | output.allocator()->allocate(); |
| 85 | |
| 86 | ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 87 | ARM_COMPUTE_EXPECT(!block_shape.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 89 | |
| 90 | // Fill tensors |
| 91 | fill(AccessorType(input), 0); |
| 92 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 93 | auto block_shape_data = AccessorType(block_shape); |
| 94 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 95 | for(unsigned int i = 0; i < block_shape_shape.x(); ++i) |
| 96 | { |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 97 | static_cast<int32_t *>(block_shape_data.data())[i] = output_shape[i + idx_width] / input_shape[i + idx_width]; |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | // Compute function |
| 101 | batch_to_space.run(); |
| 102 | |
| 103 | return output; |
| 104 | } |
| 105 | |
| 106 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &block_shape_shape, |
| 107 | const TensorShape &output_shape, DataType data_type) |
| 108 | { |
| 109 | // Create reference |
| 110 | SimpleTensor<T> input{ input_shape, data_type }; |
| 111 | SimpleTensor<int32_t> block_shape{ block_shape_shape, DataType::S32 }; |
| 112 | |
| 113 | // Fill reference |
| 114 | fill(input, 0); |
| 115 | for(unsigned int i = 0; i < block_shape_shape.x(); ++i) |
| 116 | { |
| 117 | block_shape[i] = output_shape[i] / input_shape[i]; |
| 118 | } |
| 119 | |
| 120 | // Compute reference |
| 121 | return reference::batch_to_space(input, block_shape, output_shape); |
| 122 | } |
| 123 | |
| 124 | TensorType _target{}; |
| 125 | SimpleTensor<T> _reference{}; |
| 126 | }; |
| 127 | } // namespace validation |
| 128 | } // namespace test |
| 129 | } // namespace arm_compute |
| 130 | #endif /* ARM_COMPUTE_TEST_BATCH_TO_SPACE_LAYER_FIXTURE */ |