blob: b28f93d8508aac3b26b34fda6c38b717e55c8b94 [file] [log] [blame]
Manuel Bottini8529bd62018-11-21 11:53:04 +00001/*
Viet-Hoa Doa25582c2023-03-15 16:52:05 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Manuel Bottini8529bd62018-11-21 11:53:04 +00003 *
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
25#ifndef ARM_COMPUTE_TEST_GATHER_FIXTURE
26#define ARM_COMPUTE_TEST_GATHER_FIXTURE
27
28#include "arm_compute/core/TensorShape.h"
29#include "arm_compute/core/Types.h"
30
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "tests/AssetsLibrary.h"
33#include "tests/Globals.h"
34#include "tests/IAccessor.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Fixture.h"
37#include "tests/validation/Helpers.h"
38#include "tests/validation/reference/Gather.h"
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
47class GatherFixture : public framework::Fixture
48{
49public:
50 template <typename...>
51 void setup(TensorShape input_shape, TensorShape indices_shape, int axis, DataType data_type)
52 {
53 _target = compute_target(input_shape, data_type, axis, indices_shape);
54 _reference = compute_reference(input_shape, data_type, axis, indices_shape);
55 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor)
60 {
61 library->fill_tensor_uniform(tensor, 0);
62 }
63
64 template <typename U>
65 void generate_indices(U &&indices, const TensorShape &input_shape, uint32_t actual_axis, TensorShape indices_shape)
66 {
67 std::mt19937 gen(library->seed());
68 uint32_t *indices_ptr = static_cast<uint32_t *>(indices.data());
69
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000070 // 10% of the time the index is out-of-range.
71 uint32_t max_index = input_shape[actual_axis] + input_shape[actual_axis] / 9 + 1;
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000072 std::uniform_int_distribution<uint32_t> dist_index(0, max_index - 1);
Omar Al Khatibcdd1e032023-04-26 11:31:45 +010073
74 for(unsigned int ind = 0; ind < indices_shape.total_size(); ind++)
Manuel Bottini8529bd62018-11-21 11:53:04 +000075 {
76 indices_ptr[ind] = dist_index(gen);
77 }
78 }
79
80 TensorType compute_target(const TensorShape &input_shape,
81 DataType data_type,
82 int axis,
83 const TensorShape indices_shape)
84 {
85 // Create tensors
86 TensorType src = create_tensor<TensorType>(input_shape, data_type);
87 TensorType indices_tensor = create_tensor<TensorType>(indices_shape, DataType::U32);
88 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
89 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input_shape, indices_shape, actual_axis);
90 TensorType dst = create_tensor<TensorType>(output_shape, data_type);
91
92 // Create and configure function
93 FunctionType gather;
94 gather.configure(&src, &indices_tensor, &dst, axis);
95
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010096 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
97 ARM_COMPUTE_ASSERT(indices_tensor.info()->is_resizable());
98 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Manuel Bottini8529bd62018-11-21 11:53:04 +000099
100 // Allocate tensors
101 src.allocator()->allocate();
102 indices_tensor.allocator()->allocate();
103 dst.allocator()->allocate();
104
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100105 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
106 ARM_COMPUTE_ASSERT(!indices_tensor.info()->is_resizable());
107 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Manuel Bottini8529bd62018-11-21 11:53:04 +0000108
109 // Fill tensors
110 fill(AccessorType(src));
111 generate_indices(AccessorType(indices_tensor), input_shape, actual_axis, indices_shape);
112
113 // Compute function
114 gather.run();
115
116 return dst;
117 }
118
119 SimpleTensor<T> compute_reference(const TensorShape &input_shape,
120 DataType data_type,
121 int axis,
122 const TensorShape indices_shape)
123 {
124 // Create reference tensor
125 SimpleTensor<T> src{ input_shape, data_type };
126 SimpleTensor<uint32_t> indices_tensor{ indices_shape, DataType::U32 };
127 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
128
129 // Fill reference tensor
130 fill(src);
131 generate_indices(indices_tensor, input_shape, actual_axis, indices_shape);
132
133 return reference::gather(src, indices_tensor, actual_axis);
134 }
135
136 TensorType _target{};
137 SimpleTensor<T> _reference{};
138};
139
140} // namespace validation
141} // namespace test
142} // namespace arm_compute
143
144#endif /* ARM_COMPUTE_TEST_GATHER_FIXTURE */