blob: f2dcd4a1a49c86020c3c7675b4c6bd2b1a61b421 [file] [log] [blame]
Manuel Bottini8529bd62018-11-21 11:53:04 +00001/*
2 * Copyright (c) 2018-2019 ARM Limited.
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
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
70 std::uniform_int_distribution<uint32_t> dist_index(0, input_shape[actual_axis] - 1);
71 //Let's consider 1D indices
72 for(unsigned int ind = 0; ind < indices_shape[0]; ind++)
73 {
74 indices_ptr[ind] = dist_index(gen);
75 }
76 }
77
78 TensorType compute_target(const TensorShape &input_shape,
79 DataType data_type,
80 int axis,
81 const TensorShape indices_shape)
82 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(input_shape, data_type);
85 TensorType indices_tensor = create_tensor<TensorType>(indices_shape, DataType::U32);
86 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
87 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input_shape, indices_shape, actual_axis);
88 TensorType dst = create_tensor<TensorType>(output_shape, data_type);
89
90 // Create and configure function
91 FunctionType gather;
92 gather.configure(&src, &indices_tensor, &dst, axis);
93
94 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(indices_tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
97
98 // Allocate tensors
99 src.allocator()->allocate();
100 indices_tensor.allocator()->allocate();
101 dst.allocator()->allocate();
102
103 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(!indices_tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
106
107 // Fill tensors
108 fill(AccessorType(src));
109 generate_indices(AccessorType(indices_tensor), input_shape, actual_axis, indices_shape);
110
111 // Compute function
112 gather.run();
113
114 return dst;
115 }
116
117 SimpleTensor<T> compute_reference(const TensorShape &input_shape,
118 DataType data_type,
119 int axis,
120 const TensorShape indices_shape)
121 {
122 // Create reference tensor
123 SimpleTensor<T> src{ input_shape, data_type };
124 SimpleTensor<uint32_t> indices_tensor{ indices_shape, DataType::U32 };
125 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
126
127 // Fill reference tensor
128 fill(src);
129 generate_indices(indices_tensor, input_shape, actual_axis, indices_shape);
130
131 return reference::gather(src, indices_tensor, actual_axis);
132 }
133
134 TensorType _target{};
135 SimpleTensor<T> _reference{};
136};
137
138} // namespace validation
139} // namespace test
140} // namespace arm_compute
141
142#endif /* ARM_COMPUTE_TEST_GATHER_FIXTURE */