blob: 33942c707dbc80f2033952ee0bf4a1bab5e1cf75 [file] [log] [blame]
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +01001/*
2 * Copyright (c) 2023 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 COMPUTE_KERNEL_WRITER_TESTS_CLCONSTANTTILETEST_HPP
26#define COMPUTE_KERNEL_WRITER_TESTS_CLCONSTANTTILETEST_HPP
27
28#include "src/Helpers.h"
29#include "src/cl/CLConstantTile.h"
30#include "src/cl/CLHelpers.h"
31#include "common/Common.h"
32
33#include <random>
34#include <string>
35#include <vector>
36
37namespace ckw
38{
39class CLConstantTileInternalValuesTest : public ITest
40{
41public:
42 CLConstantTileInternalValuesTest()
43 {
44 _values.push_back({{"1.2", "3.5"}, {"4.2", "1.3"}});
45 _values.push_back({{"1.2"}});
46 _values.push_back({{"1.2", "6.9"}});
47 }
48
49 bool run() override
50 {
51 // The status of this variable can change in VALIDATE_TEST()
52 bool all_tests_passed = true;
53
54 int32_t test_idx = 0;
55 for(const auto &test : _values)
56 {
57 const CLConstantTile tile(test, DataType::Fp16);
58 const auto vars = tile.all();
59 const int32_t num_vars = vars.size();
60 const int32_t width = tile.info().width();
61
62 for(int32_t y = 0; y < num_vars; ++y)
63 {
64 const int32_t col = y % width;
65 const int32_t row = y / width;
66 const std::string expected_var_name = "((half)(" + test[row][col] + "))";
67 const std::string actual_var_name = vars[y].str;
68 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
69 }
70
71 }
72 return all_tests_passed;
73 }
74
75 std::string name() override
76 {
77 return "CLConstantTileInternalValuesTest";
78 }
79
80private:
81 std::vector<TileContainer> _values {};
82};
83
84class CLConstantTileAccessScalarVariableBroadcastXTest : public ITest
85{
86public:
87 const std::string tile_name = "src";
88 const int32_t height = 8;
89 const DataType dt = DataType::Fp16;
90
91 CLConstantTileAccessScalarVariableBroadcastXTest()
92 {
93 _width.push_back(1);
94 _width.push_back(2);
95 _width.push_back(3);
96
97 _x_coord.push_back(4);
98 _x_coord.push_back(5);
99 _x_coord.push_back(6);
100
101 _y_coord.push_back(1);
102 _y_coord.push_back(3);
103 _y_coord.push_back(2);
104 }
105
106 bool run() override
107 {
108 VALIDATE_ON_MSG(_width.size() == _y_coord.size(), "The number of widths and y-coords does not match");
109 VALIDATE_ON_MSG(_x_coord.size() == _y_coord.size(), "The number of x-coords and y-coords does not match");
110
111 // The status of this variable can change in VALIDATE_TEST()
112 bool all_tests_passed = true;
113
114 const size_t num_coords = _x_coord.size();
115
116 std::random_device rd;
117 std::mt19937 gen(rd());
118 std::uniform_real_distribution<> dist(-1, 1);
119
120 int32_t test_idx = 0;
121 for(size_t i = 0; i < num_coords; ++i)
122 {
123 const int32_t width = _width[i];
124 const int32_t x_coord = _x_coord[i];
125 const int32_t y_coord = _y_coord[i];
126
127 const int32_t x_coord_clamped = clamp(x_coord, static_cast<int32_t>(0), width - 1);
128
129 TileContainer container = TileContainer(height, std::vector<std::string>(width));
130
131 for(int32_t row = 0; row < height; ++row)
132 {
133 for(int32_t col = 0; col < width; ++col)
134 {
135 container[row][col] = std::to_string(dist(gen));
136 }
137 }
138
139 const CLConstantTile tile(container, dt);
140
141 const TileVariable var = tile.scalar(y_coord, x_coord);
142
143 const std::string actual_var_name = var.str;
144 const std::string expected_var_name = "((half)(" + container[y_coord][x_coord_clamped] + "))";
145
146 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
147 }
148 return all_tests_passed;
149 }
150
151 std::string name() override
152 {
153 return "CLConstantTileAccessScalarVariableBroadcastXTest";
154 }
155
156private:
157 std::vector<int32_t> _width {};
158 std::vector<int32_t> _x_coord {};
159 std::vector<int32_t> _y_coord {};
160};
161
162class CLConstantTileAccessScalarVariableBroadcastYTest : public ITest
163{
164public:
165 const std::string tile_name = "src";
166 const int32_t width = 8;
167 const DataType dt = DataType::Fp16;
168
169 CLConstantTileAccessScalarVariableBroadcastYTest()
170 {
171 _height.push_back(1);
172 _height.push_back(2);
173 _height.push_back(3);
174
175 _x_coord.push_back(4);
176 _x_coord.push_back(5);
177 _x_coord.push_back(6);
178
179 _y_coord.push_back(3);
180 _y_coord.push_back(4);
181 _y_coord.push_back(5);
182 }
183
184 bool run() override
185 {
186 VALIDATE_ON_MSG(_height.size() == _y_coord.size(), "The number of widths and y-coords does not match");
187 VALIDATE_ON_MSG(_x_coord.size() == _y_coord.size(), "The number of x-coords and y-coords does not match");
188
189 // The status of this variable can change in VALIDATE_TEST()
190 bool all_tests_passed = true;
191
192 std::random_device rd;
193 std::mt19937 gen(rd());
194 std::uniform_real_distribution<> dist(-1, 1);
195
196 const size_t num_coords = _x_coord.size();
197
198 int32_t test_idx = 0;
199 for(size_t i = 0; i < num_coords; ++i)
200 {
201 const int32_t height = _height[i];
202 const int32_t x_coord = _x_coord[i];
203 const int32_t y_coord = _y_coord[i];
204
205 const int32_t y_coord_clamped = clamp(y_coord, static_cast<int32_t>(0), height - 1);
206
207 TileContainer container = TileContainer(height, std::vector<std::string>(width));
208
209 for(int32_t row = 0; row < height; ++row)
210 {
211 for(int32_t col = 0; col < width; ++col)
212 {
213 container[row][col] = std::to_string(dist(gen));
214 }
215 }
216
217 const CLConstantTile tile(container, dt);
218
219 const TileVariable var = tile.scalar(y_coord, x_coord);
220
221 const std::string actual_var_name = var.str;
222 const std::string expected_var_name = "((half)(" + container[y_coord_clamped][x_coord] + "))";
223
224 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
225 }
226 return all_tests_passed;
227 }
228
229 std::string name() override
230 {
231 return "CLConstantTileAccessScalarVariableBroadcastYTest";
232 }
233
234private:
235 std::vector<int32_t> _height {};
236 std::vector<int32_t> _x_coord {};
237 std::vector<int32_t> _y_coord {};
238};
239
240class CLConstantTileAccessVectorVariablesTest : public ITest
241{
242public:
243 const DataType dt = DataType::Fp16;
244
245 CLConstantTileAccessVectorVariablesTest()
246 {
247 _values.push_back({{"1.2", "3.5"}, {"4.2", "1.3"}});
248 _values.push_back({{"1.2"}});
249 // Mix variable names and values
250 _values.push_back({{"1.2", "acc", "8.7", "9.3", "ratio", "2.9", "1.7", "0.3"}});
251 }
252
253 bool run() override
254 {
255 // The status of this variable can change in VALIDATE_TEST()
256 bool all_tests_passed = true;
257
258 int32_t test_idx = 0;
259
260 for(const auto &test : _values)
261 {
262 const CLConstantTile tile(test, dt);
263 const int32_t width = tile.info().width();
264 const int32_t height = tile.info().height();
265
266 for(int32_t row = 0; row < height; ++row)
267 {
268 std::string expected_var_name = "((";
269 expected_var_name += cl_get_variable_datatype_as_string(dt, width);
270 expected_var_name += ")(";
271
272 int32_t col = 0;
273 for(; col < width - 1; ++col)
274 {
275 expected_var_name += test[row][col];
276 expected_var_name += ", ";
277 }
278
279 expected_var_name += test[row][col];
280 expected_var_name += "))";
281
282 const std::string actual_var_name = tile.vector(row).str;
283 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
284 }
285 }
286 return all_tests_passed;
287 }
288
289 std::string name() override
290 {
291 return "CLConstantTileAccessVectorVariablesTest";
292 }
293
294private:
295 std::vector<TileContainer> _values {};
296};
297
298class CLConstantTileAccessSubVectorVariablesTest : public ITest
299{
300public:
301 const DataType dt = DataType::Fp16;
302
303 CLConstantTileAccessSubVectorVariablesTest()
304 {
305 _values.push_back({{"1.2", "acc", "8.7", "9.3", "ratio", "2.9", "1.7", "0.3"}});
306 _subwidths.push_back(1);
307 _subwidths.push_back(2);
308 _subwidths.push_back(3);
309 _subwidths.push_back(4);
310 _offsets.push_back(1);
311 _offsets.push_back(3);
312 _offsets.push_back(4);
313 }
314
315 bool run() override
316 {
317 // The status of this variable can change in VALIDATE_TEST()
318 bool all_tests_passed = true;
319
320 size_t test_idx = 0;
321
322 for(auto &test : _values)
323 {
324 for(auto &col_start : _offsets)
325 {
326 for(auto &subwidth : _subwidths)
327 {
328 const CLConstantTile tile(test, dt);
329 const int32_t height = tile.info().height();
330
331 for(int32_t row = 0; row < height; ++row)
332 {
333 std::string expected_var_name = "((";
334 expected_var_name += cl_get_variable_datatype_as_string(dt, subwidth);
335 expected_var_name += ")(";
336
337 int32_t col = col_start;
338 for(; col < subwidth - 1; ++col)
339 {
340 expected_var_name += test[row][col];
341 expected_var_name += ", ";
342 }
343
344 expected_var_name += test[row][col];
345 expected_var_name += "))";
346
347 const std::string actual_var_name = tile.vector(row, col_start, subwidth).str;
348 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
349 }
350 }
351 }
352 }
353 return all_tests_passed;
354 }
355
356 std::string name() override
357 {
358 return "CLConstantTileAccessSubVectorVariablesTest";
359 }
360
361private:
362 std::vector<TileContainer> _values {};
363 std::vector<int32_t> _subwidths {};
364 std::vector<int32_t> _offsets {};
365};
366
367} // namespace ckw
368
369#endif /* COMPUTE_KERNEL_WRITER_TESTS_CLCONSTANTTILETEST_HPP */