blob: 4fb2d7f12790977898ede7a871fb4e03196c4698 [file] [log] [blame]
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +00001/*
2 * Copyright (c) 2024 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#ifndef ACL_TESTS_VALIDATION_FIXTURES_SCATTERLAYERFIXTURE_H
25#define ACL_TESTS_VALIDATION_FIXTURES_SCATTERLAYERFIXTURE_H
26
27#include "arm_compute/core/Utils.h"
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +000028#include "arm_compute/runtime/CL/CLTensorAllocator.h"
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000029#include "tests/Globals.h"
Mohammed Suhail Munshi73771072024-03-25 15:55:42 +000030#include "tests/framework/Asserts.h"
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000031#include "tests/framework/Fixture.h"
Gunes Bayirada32002024-04-24 10:27:13 +010032#include "tests/validation/Helpers.h"
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000033#include "tests/validation/Validation.h"
34#include "tests/validation/reference/ScatterLayer.h"
35#include "tests/SimpleTensor.h"
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +000036
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000037#include <random>
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +000038#include <cstdint>
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000039
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
47class ScatterGenericValidationFixture : public framework::Fixture
48{
49public:
Gunes Bayirada32002024-04-24 10:27:13 +010050 void setup(TensorShape src_shape, TensorShape updates_shape, TensorShape indices_shape,
51 TensorShape out_shape, DataType data_type, ScatterInfo scatter_info, bool inplace,
52 QuantizationInfo src_qinfo = QuantizationInfo(), QuantizationInfo o_qinfo = QuantizationInfo())
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000053 {
Gunes Bayirada32002024-04-24 10:27:13 +010054 // this is for improving randomness across tests
55 _hash = src_shape[0] + src_shape[1] + src_shape[2] + src_shape[3] + src_shape[4] + src_shape[5]
56 + updates_shape[0] + updates_shape[1] + updates_shape[2] + updates_shape[3]
57 + updates_shape[4] + updates_shape[5]
58 + indices_shape[0] + indices_shape[1] + indices_shape[2] + indices_shape[3];
59
60 _target = compute_target(src_shape, updates_shape, indices_shape, out_shape, data_type, scatter_info, inplace, src_qinfo, o_qinfo);
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000061 _reference = compute_reference(src_shape, updates_shape, indices_shape, out_shape, data_type,scatter_info, src_qinfo , o_qinfo);
62 }
63
64protected:
65 template <typename U>
Mohammed Suhail Munshi0e212362024-04-08 14:38:31 +010066 void fill(U &&tensor, int i, float lo = -10.f, float hi = 10.f)
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000067 {
68 switch(tensor.data_type())
69 {
70 case DataType::F32:
71 {
72 std::uniform_real_distribution<float> distribution(lo, hi);
73 library->fill(tensor, distribution, i);
74 break;
75 }
76 default:
77 {
78 ARM_COMPUTE_ERROR("Unsupported data type.");
79 }
80 }
81 }
82
Mohammed Suhail Munshi73771072024-03-25 15:55:42 +000083 // This is used to fill indices tensor with S32 datatype.
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +000084 // Used to prevent ONLY having values that are out of bounds.
85 template <typename U>
86 void fill_indices(U &&tensor, int i, const TensorShape &shape)
87 {
Mohammed Suhail Munshi73771072024-03-25 15:55:42 +000088 // Calculate max indices the shape should contain. Add an arbitrary value to allow testing for some out of bounds values (In this case min dimension)
89 const int32_t max = std::max({shape[0] , shape[1], shape[2]});
90 library->fill_tensor_uniform(tensor, i, static_cast<int32_t>(-2), static_cast<int32_t>(max));
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +000091 }
92
Gunes Bayirada32002024-04-24 10:27:13 +010093 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c,
94 const TensorShape &out_shape, DataType data_type, const ScatterInfo info, bool inplace,
95 QuantizationInfo a_qinfo, QuantizationInfo o_qinfo)
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +000096 {
97 // 1. Create relevant tensors using ScatterInfo data structure.
98 // ----------------------------------------------------
99 // In order - src, updates, indices, output.
100 TensorType src = create_tensor<TensorType>(shape_a, data_type, 1, a_qinfo);
101 TensorType updates = create_tensor<TensorType>(shape_b, data_type, 1, a_qinfo);
Mohammed Suhail Munshi73771072024-03-25 15:55:42 +0000102 TensorType indices = create_tensor<TensorType>(shape_c, DataType::S32, 1, QuantizationInfo());
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000103 TensorType dst = create_tensor<TensorType>(out_shape, data_type, 1, o_qinfo);
104
105 FunctionType scatter;
106
107 // Configure operator
Gunes Bayirada32002024-04-24 10:27:13 +0100108 // When scatter_info.zero_initialization is true, pass nullptr for src
109 // because dst does not need to be initialized with src values.
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +0000110 if(info.zero_initialization)
111 {
112 scatter.configure(nullptr, &updates, &indices, &dst, info);
113 }
114 else
115 {
Gunes Bayirada32002024-04-24 10:27:13 +0100116 if(inplace)
117 {
118 scatter.configure(&src, &updates, &indices, &src, info);
119 }
120 else
121 {
122 scatter.configure(&src, &updates, &indices, &dst, info);
123 }
Mohammed Suhail Munshi473b8292024-03-18 12:13:30 +0000124 }
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000125
126 // Assertions
127 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
128 ARM_COMPUTE_ASSERT(updates.info()->is_resizable());
129 ARM_COMPUTE_ASSERT(indices.info()->is_resizable());
130 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
131
Gunes Bayirada32002024-04-24 10:27:13 +0100132 add_padding_x({ &src, &updates, &indices});
133
134 if(!inplace)
135 {
136 add_padding_x({ &dst });
137 }
138
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000139 // Allocate tensors
140 src.allocator()->allocate();
141 updates.allocator()->allocate();
142 indices.allocator()->allocate();
Gunes Bayirada32002024-04-24 10:27:13 +0100143
144 if(!inplace)
145 {
146 dst.allocator()->allocate();
147 }
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000148
149 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
150 ARM_COMPUTE_ASSERT(!updates.info()->is_resizable());
151 ARM_COMPUTE_ASSERT(!indices.info()->is_resizable());
Gunes Bayirada32002024-04-24 10:27:13 +0100152
153 if(!inplace)
154 {
155 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
156 }
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000157
158 // Fill update (a) and indices (b) tensors.
Gunes Bayirada32002024-04-24 10:27:13 +0100159 fill(AccessorType(src), 0 + _hash);
160 fill(AccessorType(updates), 1+ _hash);
161 fill_indices(AccessorType(indices), 2 + _hash, out_shape);
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000162
163 scatter.run();
Gunes Bayirada32002024-04-24 10:27:13 +0100164
165 if(inplace)
166 {
167 return src;
168 }
169 else
170 {
171 return dst;
172 }
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000173 }
174
Gunes Bayirada32002024-04-24 10:27:13 +0100175 SimpleTensor<T> compute_reference(const TensorShape &a_shape, const TensorShape &b_shape, const TensorShape &c_shape,
176 const TensorShape &out_shape, DataType data_type, ScatterInfo info, QuantizationInfo a_qinfo, QuantizationInfo o_qinfo)
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000177 {
178 // Output Quantization not currently in use - fixture should be extended to support this.
179 ARM_COMPUTE_UNUSED(o_qinfo);
Mohammed Suhail Munshi0e212362024-04-08 14:38:31 +0100180 TensorShape src_shape = a_shape;
181 TensorShape updates_shape = b_shape;
182 TensorShape indices_shape = c_shape;
183
184 // 1. Collapse batch index into a single dim if necessary for update tensor and indices tensor.
185 if(c_shape.num_dimensions() >= 3)
186 {
187 indices_shape = indices_shape.collapsed_from(1);
188 updates_shape = updates_shape.collapsed_from(updates_shape.num_dimensions() - 2); // Collapses from last 2 dims
189 }
190
191 // 2. Collapse data dims into a single dim.
192 // Collapse all src dims into 2 dims. First one holding data, the other being the index we iterate over.
193 src_shape.collapse(updates_shape.num_dimensions() - 1); // Collapse all data dims into single dim.
194 src_shape = src_shape.collapsed_from(1); // Collapse all index dims into a single dim
195 updates_shape.collapse(updates_shape.num_dimensions() - 1); // Collapse data dims (all except last dim which is batch dim)
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000196
197 // Create reference tensors
198 SimpleTensor<T> src{ a_shape, data_type, 1, a_qinfo };
199 SimpleTensor<T> updates{b_shape, data_type, 1, QuantizationInfo() };
Mohammed Suhail Munshi73771072024-03-25 15:55:42 +0000200 SimpleTensor<int32_t> indices{ c_shape, DataType::S32, 1, QuantizationInfo() };
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000201
202 // Fill reference
Gunes Bayirada32002024-04-24 10:27:13 +0100203 fill(src, 0 + _hash);
204 fill(updates, 1 + _hash);
205 fill_indices(indices, 2 + _hash, out_shape);
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000206
207 // Calculate individual reference.
Mohammed Suhail Munshi73771072024-03-25 15:55:42 +0000208 return reference::scatter_layer<T>(src, updates, indices, out_shape, info);
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000209 }
210
211 TensorType _target{};
212 SimpleTensor<T> _reference{};
Gunes Bayirada32002024-04-24 10:27:13 +0100213 int32_t _hash{};
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000214};
215
216// This fixture will use the same shape for updates as indices.
217template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
218class ScatterValidationFixture : public ScatterGenericValidationFixture<TensorType, AccessorType, FunctionType, T>
219{
220public:
Gunes Bayirada32002024-04-24 10:27:13 +0100221 void setup(TensorShape src_shape, TensorShape update_shape, TensorShape indices_shape,
222 TensorShape out_shape, DataType data_type, ScatterFunction func, bool zero_init, bool inplace)
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000223 {
Gunes Bayirada32002024-04-24 10:27:13 +0100224 ScatterGenericValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, update_shape,
225 indices_shape, out_shape, data_type, ScatterInfo(func, zero_init), inplace,
226 QuantizationInfo(), QuantizationInfo());
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +0000227 }
228};
229
230} // namespace validation
231} // namespace test
232} // namespace arm_compute
233#endif // ACL_TESTS_VALIDATION_FIXTURES_SCATTERLAYERFIXTURE_H