blob: 857b0387b7fee28b2f8b5937a644691032cdd3ea [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:
Manuel Bottini8529bd62018-11-21 11:53:04 +000050 void setup(TensorShape input_shape, TensorShape indices_shape, int axis, DataType data_type)
51 {
52 _target = compute_target(input_shape, data_type, axis, indices_shape);
53 _reference = compute_reference(input_shape, data_type, axis, indices_shape);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor)
59 {
60 library->fill_tensor_uniform(tensor, 0);
61 }
62
63 template <typename U>
64 void generate_indices(U &&indices, const TensorShape &input_shape, uint32_t actual_axis, TensorShape indices_shape)
65 {
66 std::mt19937 gen(library->seed());
67 uint32_t *indices_ptr = static_cast<uint32_t *>(indices.data());
68
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000069 // 10% of the time the index is out-of-range.
70 uint32_t max_index = input_shape[actual_axis] + input_shape[actual_axis] / 9 + 1;
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000071 std::uniform_int_distribution<uint32_t> dist_index(0, max_index - 1);
Omar Al Khatibcdd1e032023-04-26 11:31:45 +010072
73 for(unsigned int ind = 0; ind < indices_shape.total_size(); ind++)
Manuel Bottini8529bd62018-11-21 11:53:04 +000074 {
75 indices_ptr[ind] = dist_index(gen);
76 }
77 }
78
79 TensorType compute_target(const TensorShape &input_shape,
80 DataType data_type,
81 int axis,
82 const TensorShape indices_shape)
83 {
84 // Create tensors
85 TensorType src = create_tensor<TensorType>(input_shape, data_type);
86 TensorType indices_tensor = create_tensor<TensorType>(indices_shape, DataType::U32);
87 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
88 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input_shape, indices_shape, actual_axis);
89 TensorType dst = create_tensor<TensorType>(output_shape, data_type);
90
91 // Create and configure function
92 FunctionType gather;
93 gather.configure(&src, &indices_tensor, &dst, axis);
94
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010095 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
96 ARM_COMPUTE_ASSERT(indices_tensor.info()->is_resizable());
97 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Manuel Bottini8529bd62018-11-21 11:53:04 +000098
99 // Allocate tensors
100 src.allocator()->allocate();
101 indices_tensor.allocator()->allocate();
102 dst.allocator()->allocate();
103
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100104 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
105 ARM_COMPUTE_ASSERT(!indices_tensor.info()->is_resizable());
106 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Manuel Bottini8529bd62018-11-21 11:53:04 +0000107
108 // Fill tensors
109 fill(AccessorType(src));
110 generate_indices(AccessorType(indices_tensor), input_shape, actual_axis, indices_shape);
111
112 // Compute function
113 gather.run();
114
115 return dst;
116 }
117
118 SimpleTensor<T> compute_reference(const TensorShape &input_shape,
119 DataType data_type,
120 int axis,
121 const TensorShape indices_shape)
122 {
123 // Create reference tensor
124 SimpleTensor<T> src{ input_shape, data_type };
125 SimpleTensor<uint32_t> indices_tensor{ indices_shape, DataType::U32 };
126 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
127
128 // Fill reference tensor
129 fill(src);
130 generate_indices(indices_tensor, input_shape, actual_axis, indices_shape);
131
132 return reference::gather(src, indices_tensor, actual_axis);
133 }
134
135 TensorType _target{};
136 SimpleTensor<T> _reference{};
137};
138
139} // namespace validation
140} // namespace test
141} // namespace arm_compute
142
143#endif /* ARM_COMPUTE_TEST_GATHER_FIXTURE */