blob: 03cb6aef42f285ad9973f0303889c652e4cbb409 [file] [log] [blame]
Sanghoon Leefc35b512017-10-18 12:09:04 +01001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2017-2021 Arm Limited.
Sanghoon Leefc35b512017-10-18 12:09:04 +01003 *
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 Pinitas5a7e7762017-12-01 16:27:29 +000034#include "tests/validation/reference/Remap.h"
Sanghoon Leefc35b512017-10-18 12:09:04 +010035
36#include <random>
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Frederick Liardet36dff9f2021-04-22 21:13:21 +010045class RemapValidationGenericFixture : public framework::Fixture
Sanghoon Leefc35b512017-10-18 12:09:04 +010046{
47public:
48 template <typename...>
Frederick Liardet36dff9f2021-04-22 21:13:21 +010049 void setup(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, DataLayout data_layout = DataLayout::NCHW)
Sanghoon Leefc35b512017-10-18 12:09:04 +010050 {
51 std::mt19937 gen(library->seed());
52 std::uniform_int_distribution<uint8_t> distribution(0, 255);
Freddie Liardetef5aac62021-06-10 16:45:58 +010053 PixelValue constant_border_value{ static_cast<T>(distribution(gen)) };
Sanghoon Leefc35b512017-10-18 12:09:04 +010054
Frederick Liardet36dff9f2021-04-22 21:13:21 +010055 _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 Leefc35b512017-10-18 12:09:04 +010058 }
59
60protected:
61 template <typename U>
Freddie Liardetef5aac62021-06-10 16:45:58 +010062 void fill(U &&tensor, int i, int min, int max)
Sanghoon Leefc35b512017-10-18 12:09:04 +010063 {
Freddie Liardetef5aac62021-06-10 16:45:58 +010064 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 Leefc35b512017-10-18 12:09:04 +010088 }
89
Freddie Liardetef5aac62021-06-10 16:45:58 +010090 TensorType compute_target(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, PixelValue constant_border_value)
Sanghoon Leefc35b512017-10-18 12:09:04 +010091 {
Frederick Liardet36dff9f2021-04-22 21:13:21 +010092 if(_data_layout == DataLayout::NHWC)
93 {
94 permute(shape, PermutationVector(2U, 0U, 1U));
95 }
96
Sanghoon Leefc35b512017-10-18 12:09:04 +010097 // Create tensors
Frederick Liardet36dff9f2021-04-22 21:13:21 +010098 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 Leefc35b512017-10-18 12:09:04 +0100102
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 Giorgio4fc10b32021-04-30 18:30:41 +0100107 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 Leefc35b512017-10-18 12:09:04 +0100111
112 // Allocate tensors
113 src.allocator()->allocate();
114 map_x.allocator()->allocate();
115 map_y.allocator()->allocate();
116 dst.allocator()->allocate();
117
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100118 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 Leefc35b512017-10-18 12:09:04 +0100122
123 // Fill tensors
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100124 int max_val = std::max({ shape.x(), shape.y(), shape.z() });
125
Pablo Tellofc1ffe62018-01-23 08:31:41 +0000126 fill(AccessorType(src), 0, 0, 255);
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100127 fill(AccessorType(map_x), 1, -5, max_val);
128 fill(AccessorType(map_y), 2, -5, max_val);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100129
130 // Compute function
131 remap.run();
132
133 return dst;
134 }
135
Freddie Liardetef5aac62021-06-10 16:45:58 +0100136 SimpleTensor<T> compute_reference(const TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, PixelValue constant_border_value)
Sanghoon Leefc35b512017-10-18 12:09:04 +0100137 {
Freddie Liardetef5aac62021-06-10 16:45:58 +0100138 ARM_COMPUTE_ERROR_ON(data_type != DataType::U8 && data_type != DataType::F16);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100139
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 Liardetef5aac62021-06-10 16:45:58 +0100144 T border_value{};
145 constant_border_value.get(border_value);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100146
147 // Create the valid mask Tensor
148 _valid_mask = SimpleTensor<T> { shape, data_type };
149
150 // Fill reference
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100151 int max_val = std::max({ shape.x(), shape.y(), shape.z() });
152
Pablo Tellofc1ffe62018-01-23 08:31:41 +0000153 fill(src, 0, 0, 255);
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100154 fill(map_x, 1, -5, max_val);
155 fill(map_y, 2, -5, max_val);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100156
157 // Compute reference
Freddie Liardetef5aac62021-06-10 16:45:58 +0100158 return reference::remap<T>(src, map_x, map_y, _valid_mask, policy, border_mode, border_value);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100159 }
160
161 TensorType _target{};
162 SimpleTensor<T> _reference{};
163 SimpleTensor<T> _valid_mask{};
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100164 DataLayout _data_layout{};
Sanghoon Leefc35b512017-10-18 12:09:04 +0100165};
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100166
167template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
168class RemapValidationFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
169{
170public:
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
178template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
179class RemapValidationMixedLayoutFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
180{
181public:
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 Leefc35b512017-10-18 12:09:04 +0100189} // namespace validation
190} // namespace test
191} // namespace arm_compute
192#endif /* ARM_COMPUTE_TEST_REMAP_FIXTURE */