Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 4ceb453 | 2023-03-13 15:02:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 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 | |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 27 | #include "arm_compute/core/Helpers.h" |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 28 | #include "tests/Globals.h" |
| 29 | #include "tests/framework/Asserts.h" |
| 30 | #include "tests/framework/Fixture.h" |
| 31 | #include "tests/validation/reference/BatchToSpaceLayer.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace validation |
| 38 | { |
| 39 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 40 | class BatchToSpaceLayerValidationFixture : public framework::Fixture |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | template <typename...> |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 44 | void setup(const TensorShape &input_shape, const std::vector<int32_t> &block_shape, const CropInfo &crop_info, const TensorShape &output_shape, DataType data_type, DataLayout data_layout) |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 45 | { |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 46 | _target = compute_target(input_shape, block_shape, crop_info, output_shape, data_type, data_layout); |
| 47 | _reference = compute_reference(input_shape, block_shape, crop_info, output_shape, data_type); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | protected: |
| 51 | template <typename U> |
| 52 | void fill(U &&tensor, int i) |
| 53 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 54 | 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] | 55 | 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] | 56 | |
| 57 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 58 | library->fill(tensor, distribution, i); |
| 59 | } |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 60 | TensorType compute_target(TensorShape input_shape, const std::vector<int32_t> &block_shape, const CropInfo &crop_info, TensorShape output_shape, |
| 61 | DataType data_type, DataLayout data_layout) |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 62 | { |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 63 | ARM_COMPUTE_ERROR_ON(block_shape.size() != 2U); // Only support batch to 2D space (x, y) for now |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 64 | if(data_layout == DataLayout::NHWC) |
| 65 | { |
| 66 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 67 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 68 | } |
| 69 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 70 | // Create tensors |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 71 | TensorType input = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 72 | 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] | 73 | |
| 74 | // Create and configure function |
| 75 | FunctionType batch_to_space; |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 76 | batch_to_space.configure(&input, block_shape.at(0), block_shape.at(1), &output, crop_info); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 77 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_ASSERT(input.info()->is_resizable()); |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ASSERT(output.info()->is_resizable()); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 80 | |
| 81 | // Allocate tensors |
| 82 | input.allocator()->allocate(); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 83 | output.allocator()->allocate(); |
| 84 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ASSERT(!input.info()->is_resizable()); |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_ASSERT(!output.info()->is_resizable()); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 87 | |
| 88 | // Fill tensors |
| 89 | fill(AccessorType(input), 0); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 90 | // Compute function |
| 91 | batch_to_space.run(); |
| 92 | |
| 93 | return output; |
| 94 | } |
| 95 | |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 96 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const std::vector<int32_t> &block_shape, |
| 97 | const CropInfo &crop_info, const TensorShape &output_shape, DataType data_type) |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 98 | { |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 99 | ARM_COMPUTE_ERROR_ON(block_shape.size() != 2U); // Only support batch to 2D space (x, y) for now |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 100 | // Create reference |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 101 | SimpleTensor<T> input{ input_shape, data_type }; |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 102 | |
| 103 | // Fill reference |
| 104 | fill(input, 0); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 105 | |
| 106 | // Compute reference |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame^] | 107 | return reference::batch_to_space(input, block_shape, crop_info, output_shape); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TensorType _target{}; |
| 111 | SimpleTensor<T> _reference{}; |
| 112 | }; |
SiCong Li | 4ceb453 | 2023-03-13 15:02:23 +0000 | [diff] [blame] | 113 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 114 | } // namespace validation |
| 115 | } // namespace test |
| 116 | } // namespace arm_compute |
| 117 | #endif /* ARM_COMPUTE_TEST_BATCH_TO_SPACE_LAYER_FIXTURE */ |