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); |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 53 | const T 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> |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 62 | void fill(U &&tensor, int i, float min, float max) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 63 | { |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 64 | std::uniform_int_distribution<> distribution((int)min, (int)max); |
| 65 | library->fill(tensor, distribution, i); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 68 | TensorType compute_target(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, T constant_border_value) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 69 | { |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 70 | if(_data_layout == DataLayout::NHWC) |
| 71 | { |
| 72 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 73 | } |
| 74 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 75 | // Create tensors |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 76 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, QuantizationInfo(), _data_layout); |
| 77 | TensorType map_x = create_tensor<TensorType>(shape, DataType::F32, 1, QuantizationInfo(), _data_layout); |
| 78 | TensorType map_y = create_tensor<TensorType>(shape, DataType::F32, 1, QuantizationInfo(), _data_layout); |
| 79 | TensorType dst = create_tensor<TensorType>(shape, data_type, 1, QuantizationInfo(), _data_layout); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 80 | |
| 81 | // Create and configure function |
| 82 | FunctionType remap; |
| 83 | remap.configure(&src, &map_x, &map_y, &dst, policy, border_mode, constant_border_value); |
| 84 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 86 | ARM_COMPUTE_ASSERT(map_x.info()->is_resizable()); |
| 87 | ARM_COMPUTE_ASSERT(map_y.info()->is_resizable()); |
| 88 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 89 | |
| 90 | // Allocate tensors |
| 91 | src.allocator()->allocate(); |
| 92 | map_x.allocator()->allocate(); |
| 93 | map_y.allocator()->allocate(); |
| 94 | dst.allocator()->allocate(); |
| 95 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 96 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 97 | ARM_COMPUTE_ASSERT(!map_x.info()->is_resizable()); |
| 98 | ARM_COMPUTE_ASSERT(!map_y.info()->is_resizable()); |
| 99 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 100 | |
| 101 | // Fill tensors |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 102 | int max_val = std::max({ shape.x(), shape.y(), shape.z() }); |
| 103 | |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 104 | fill(AccessorType(src), 0, 0, 255); |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 105 | fill(AccessorType(map_x), 1, -5, max_val); |
| 106 | fill(AccessorType(map_y), 2, -5, max_val); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 107 | |
| 108 | // Compute function |
| 109 | remap.run(); |
| 110 | |
| 111 | return dst; |
| 112 | } |
| 113 | |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 114 | SimpleTensor<T> compute_reference(const TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, T constant_border_value) |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 115 | { |
| 116 | ARM_COMPUTE_ERROR_ON(data_type != DataType::U8); |
| 117 | |
| 118 | // Create reference |
| 119 | SimpleTensor<T> src{ shape, data_type }; |
| 120 | SimpleTensor<float> map_x{ shape, DataType::F32 }; |
| 121 | SimpleTensor<float> map_y{ shape, DataType::F32 }; |
| 122 | |
| 123 | // Create the valid mask Tensor |
| 124 | _valid_mask = SimpleTensor<T> { shape, data_type }; |
| 125 | |
| 126 | // Fill reference |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 127 | int max_val = std::max({ shape.x(), shape.y(), shape.z() }); |
| 128 | |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 129 | fill(src, 0, 0, 255); |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 130 | fill(map_x, 1, -5, max_val); |
| 131 | fill(map_y, 2, -5, max_val); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 132 | |
| 133 | // Compute reference |
| 134 | return reference::remap<T>(src, map_x, map_y, _valid_mask, policy, border_mode, constant_border_value); |
| 135 | } |
| 136 | |
| 137 | TensorType _target{}; |
| 138 | SimpleTensor<T> _reference{}; |
| 139 | SimpleTensor<T> _valid_mask{}; |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 140 | DataLayout _data_layout{}; |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 141 | }; |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 142 | |
| 143 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 144 | class RemapValidationFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 145 | { |
| 146 | public: |
| 147 | template <typename...> |
| 148 | void setup(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode) |
| 149 | { |
| 150 | RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, policy, data_type, border_mode); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 155 | class RemapValidationMixedLayoutFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 156 | { |
| 157 | public: |
| 158 | template <typename...> |
| 159 | void setup(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, DataLayout data_layout) |
| 160 | { |
| 161 | RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, policy, data_type, border_mode, data_layout); |
| 162 | } |
| 163 | }; |
| 164 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 165 | } // namespace validation |
| 166 | } // namespace test |
| 167 | } // namespace arm_compute |
| 168 | #endif /* ARM_COMPUTE_TEST_REMAP_FIXTURE */ |