blob: 2cb8e67f62ff70e6fc1e666e5a2cfab0dc13e591 [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);
Pablo Tellofc1ffe62018-01-23 08:31:41 +000053 const T 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>
Pablo Tellofc1ffe62018-01-23 08:31:41 +000062 void fill(U &&tensor, int i, float min, float max)
Sanghoon Leefc35b512017-10-18 12:09:04 +010063 {
Pablo Tellofc1ffe62018-01-23 08:31:41 +000064 std::uniform_int_distribution<> distribution((int)min, (int)max);
65 library->fill(tensor, distribution, i);
Sanghoon Leefc35b512017-10-18 12:09:04 +010066 }
67
Frederick Liardet36dff9f2021-04-22 21:13:21 +010068 TensorType compute_target(TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, T constant_border_value)
Sanghoon Leefc35b512017-10-18 12:09:04 +010069 {
Frederick Liardet36dff9f2021-04-22 21:13:21 +010070 if(_data_layout == DataLayout::NHWC)
71 {
72 permute(shape, PermutationVector(2U, 0U, 1U));
73 }
74
Sanghoon Leefc35b512017-10-18 12:09:04 +010075 // Create tensors
Frederick Liardet36dff9f2021-04-22 21:13:21 +010076 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 Leefc35b512017-10-18 12:09:04 +010080
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 Giorgio4fc10b32021-04-30 18:30:41 +010085 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 Leefc35b512017-10-18 12:09:04 +010089
90 // Allocate tensors
91 src.allocator()->allocate();
92 map_x.allocator()->allocate();
93 map_y.allocator()->allocate();
94 dst.allocator()->allocate();
95
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010096 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 Leefc35b512017-10-18 12:09:04 +0100100
101 // Fill tensors
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100102 int max_val = std::max({ shape.x(), shape.y(), shape.z() });
103
Pablo Tellofc1ffe62018-01-23 08:31:41 +0000104 fill(AccessorType(src), 0, 0, 255);
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100105 fill(AccessorType(map_x), 1, -5, max_val);
106 fill(AccessorType(map_y), 2, -5, max_val);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100107
108 // Compute function
109 remap.run();
110
111 return dst;
112 }
113
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100114 SimpleTensor<T> compute_reference(const TensorShape shape, InterpolationPolicy policy, DataType data_type, BorderMode border_mode, T constant_border_value)
Sanghoon Leefc35b512017-10-18 12:09:04 +0100115 {
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 Liardet36dff9f2021-04-22 21:13:21 +0100127 int max_val = std::max({ shape.x(), shape.y(), shape.z() });
128
Pablo Tellofc1ffe62018-01-23 08:31:41 +0000129 fill(src, 0, 0, 255);
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100130 fill(map_x, 1, -5, max_val);
131 fill(map_y, 2, -5, max_val);
Sanghoon Leefc35b512017-10-18 12:09:04 +0100132
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 Liardet36dff9f2021-04-22 21:13:21 +0100140 DataLayout _data_layout{};
Sanghoon Leefc35b512017-10-18 12:09:04 +0100141};
Frederick Liardet36dff9f2021-04-22 21:13:21 +0100142
143template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
144class RemapValidationFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
145{
146public:
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
154template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
155class RemapValidationMixedLayoutFixture : public RemapValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
156{
157public:
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 Leefc35b512017-10-18 12:09:04 +0100165} // namespace validation
166} // namespace test
167} // namespace arm_compute
168#endif /* ARM_COMPUTE_TEST_REMAP_FIXTURE */