Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +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_REMAP_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_REMAP_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 34 | #include "tests/validation/reference/Remap.h" |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 35 | |
| 36 | #include <random> |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 45 | class RemapValidationGenericFixture : public framework::Fixture |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 46 | { |
| 47 | public: |
| 48 | template <typename...> |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 49 | void setup(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, DataLayout data_layout = DataLayout::NCHW) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 50 | { |
| 51 | std::mt19937 gen(library->seed()); |
| 52 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 53 | PixelValue constant_border_value{ static_cast<T>(distribution(gen)) }; |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 54 | |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 55 | _data_layout = data_layout; |
| 56 | _target = compute_target(shape, policy, data_type, border_mode, constant_border_value); |
| 57 | _reference = compute_reference(shape, policy, data_type, border_mode, constant_border_value); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
| 61 | template <typename U> |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 62 | void fill(U &&tensor, int i, int min, int max) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 63 | { |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 64 | switch(tensor.data_type()) |
| 65 | { |
| 66 | case DataType::F32: |
| 67 | { |
| 68 | // map_x,y as integer values |
| 69 | std::uniform_int_distribution<int> distribution(min, max); |
| 70 | library->fill(tensor, distribution, i); |
| 71 | break; |
| 72 | } |
| 73 | case DataType::F16: |
| 74 | { |
| 75 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution(static_cast<float>(min), static_cast<float>(max)); |
| 76 | library->fill(tensor, distribution, i); |
| 77 | break; |
| 78 | } |
| 79 | case DataType::U8: |
| 80 | { |
| 81 | std::uniform_int_distribution<uint8_t> distribution(min, max); |
| 82 | library->fill(tensor, distribution, i); |
| 83 | break; |
| 84 | } |
| 85 | default: |
| 86 | ARM_COMPUTE_ERROR("DataType for Remap not supported"); |
| 87 | } |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 90 | TensorType compute_target(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, PixelValue constant_border_value) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 91 | { |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 92 | if(_data_layout == DataLayout::NHWC) |
| 93 | { |
| 94 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 95 | } |
| 96 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 97 | // Create tensors |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 98 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, QuantizationInfo(), _data_layout); |
| 99 | TensorType map_x = create_tensor<TensorType>(shape, DataType::F32, 1, QuantizationInfo(), _data_layout); |
| 100 | TensorType map_y = create_tensor<TensorType>(shape, DataType::F32, 1, QuantizationInfo(), _data_layout); |
| 101 | TensorType dst = create_tensor<TensorType>(shape, data_type, 1, QuantizationInfo(), _data_layout); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 102 | |
| 103 | // Create and configure function |
| 104 | FunctionType remap; |
| 105 | remap.configure(&src, &map_x, &map_y, &dst, policy, border_mode, constant_border_value); |
| 106 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 108 | ARM_COMPUTE_ASSERT(map_x.info()->is_resizable()); |
| 109 | ARM_COMPUTE_ASSERT(map_y.info()->is_resizable()); |
| 110 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 111 | |
| 112 | // Allocate tensors |
| 113 | src.allocator()->allocate(); |
| 114 | map_x.allocator()->allocate(); |
| 115 | map_y.allocator()->allocate(); |
| 116 | dst.allocator()->allocate(); |
| 117 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 119 | ARM_COMPUTE_ASSERT(!map_x.info()->is_resizable()); |
| 120 | ARM_COMPUTE_ASSERT(!map_y.info()->is_resizable()); |
| 121 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 122 | |
| 123 | // Fill tensors |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 124 | int max_val = std::max({ shape.x(), shape.y(), shape.z() }); |
| 125 | |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 126 | fill(AccessorType(src), 0, 0, 255); |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 127 | fill(AccessorType(map_x), 1, -5, max_val); |
| 128 | fill(AccessorType(map_y), 2, -5, max_val); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 129 | |
| 130 | // Compute function |
| 131 | remap.run(); |
| 132 | |
| 133 | return dst; |
| 134 | } |
| 135 | |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 136 | SimpleTensor<T> compute_reference(const TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, PixelValue constant_border_value) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 137 | { |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 138 | ARM_COMPUTE_ERROR_ON(data_type != DataType::U8 && data_type != DataType::F16); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 139 | |
| 140 | // Create reference |
| 141 | SimpleTensor<T> src{ shape, data_type }; |
| 142 | SimpleTensor<float> map_x{ shape, DataType::F32 }; |
| 143 | SimpleTensor<float> map_y{ shape, DataType::F32 }; |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 144 | T border_value{}; |
| 145 | constant_border_value.get(border_value); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 146 | |
| 147 | // Create the valid mask Tensor |
| 148 | _valid_mask = SimpleTensor<T> { shape, data_type }; |
| 149 | |
| 150 | // Fill reference |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 151 | int max_val = std::max({ shape.x(), shape.y(), shape.z() }); |
| 152 | |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 153 | fill(src, 0, 0, 255); |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 154 | fill(map_x, 1, -5, max_val); |
| 155 | fill(map_y, 2, -5, max_val); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 156 | |
| 157 | // Compute reference |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 158 | return reference::remap<T>(src, map_x, map_y, _valid_mask, policy, border_mode, border_value); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | TensorType _target{}; |
| 162 | SimpleTensor<T> _reference{}; |
| 163 | SimpleTensor<T> _valid_mask{}; |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 164 | DataLayout _data_layout{}; |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 165 | }; |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 166 | |
| 167 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 168 | class RemapValidationFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 169 | { |
| 170 | public: |
| 171 | template <typename...> |
| 172 | void setup(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode) |
| 173 | { |
| 174 | RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, policy, data_type, border_mode); |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 179 | class RemapValidationMixedLayoutFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 180 | { |
| 181 | public: |
| 182 | template <typename...> |
| 183 | void setup(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, DataLayout data_layout) |
| 184 | { |
| 185 | RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, policy, data_type, border_mode, data_layout); |
| 186 | } |
| 187 | }; |
| 188 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 189 | } // namespace validation |
| 190 | } // namespace test |
| 191 | } // namespace arm_compute |
| 192 | #endif /* ARM_COMPUTE_TEST_REMAP_FIXTURE */ |