blob: f10ad10146bdb7b9001579a52a0188ba762aa769 [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
Viet-Hoa Do0250fa62023-07-24 15:47:34 +010025#ifndef CKW_TESTS_CLCONSTANTTILETEST_HPP
26#define CKW_TESTS_CLCONSTANTTILETEST_HPP
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010027
Nikolaj Jensenacea4072023-07-03 09:44:42 +010028#include "common/Common.h"
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010029#include "src/Helpers.h"
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010030#include "src/cl/CLHelpers.h"
Viet-Hoa Do0250fa62023-07-24 15:47:34 +010031#include "src/cl/CLTile.h"
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010032
33#include <random>
34#include <string>
35#include <vector>
36
37namespace ckw
38{
39class CLConstantTileInternalValuesTest : public ITest
40{
41public:
42 CLConstantTileInternalValuesTest()
43 {
Nikolaj Jensenacea4072023-07-03 09:44:42 +010044 _values.push_back({ { "1.2", "3.5" },
45 { "4.2", "1.3" } });
46 _values.push_back({ { "1.2" } });
47 _values.push_back({ { "1.2", "6.9" } });
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010048 }
49
50 bool run() override
51 {
52 // The status of this variable can change in VALIDATE_TEST()
53 bool all_tests_passed = true;
54
55 int32_t test_idx = 0;
56 for(const auto &test : _values)
57 {
Viet-Hoa Do0250fa62023-07-24 15:47:34 +010058 const CLTile tile(test, DataType::Fp16);
59 const auto vars = tile.all();
60 const int32_t num_vars = vars.size();
61 const int32_t width = tile.info().width();
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010062
63 for(int32_t y = 0; y < num_vars; ++y)
64 {
Nikolaj Jensenacea4072023-07-03 09:44:42 +010065 const int32_t col = y % width;
66 const int32_t row = y / width;
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010067 const std::string expected_var_name = "((half)(" + test[row][col] + "))";
Nikolaj Jensenacea4072023-07-03 09:44:42 +010068 const std::string actual_var_name = vars[y].str;
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010069 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
70 }
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010071 }
72 return all_tests_passed;
73 }
74
75 std::string name() override
76 {
77 return "CLConstantTileInternalValuesTest";
78 }
79
80private:
Nikolaj Jensenacea4072023-07-03 09:44:42 +010081 std::vector<TileContainer> _values{};
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +010082};
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
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100116 std::random_device rd;
117 std::mt19937 gen(rd());
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100118 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
Viet-Hoa Do0250fa62023-07-24 15:47:34 +0100139 const CLTile tile(container, dt);
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100140
141 const TileVariable var = tile.scalar(y_coord, x_coord);
142
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100143 const std::string actual_var_name = var.str;
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100144 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:
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100157 std::vector<int32_t> _width{};
158 std::vector<int32_t> _x_coord{};
159 std::vector<int32_t> _y_coord{};
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100160};
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
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100192 std::random_device rd;
193 std::mt19937 gen(rd());
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100194 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
Viet-Hoa Do0250fa62023-07-24 15:47:34 +0100217 const CLTile tile(container, dt);
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100218
219 const TileVariable var = tile.scalar(y_coord, x_coord);
220
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100221 const std::string actual_var_name = var.str;
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100222 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:
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100235 std::vector<int32_t> _height{};
236 std::vector<int32_t> _x_coord{};
237 std::vector<int32_t> _y_coord{};
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100238};
239
240class CLConstantTileAccessVectorVariablesTest : public ITest
241{
242public:
243 const DataType dt = DataType::Fp16;
244
245 CLConstantTileAccessVectorVariablesTest()
246 {
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100247 _values.push_back({ { "1.2", "3.5" },
248 { "4.2", "1.3" } });
249 _values.push_back({ { "1.2" } });
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100250 // Mix variable names and values
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100251 _values.push_back({ { "1.2", "acc", "8.7", "9.3", "ratio", "2.9", "1.7", "0.3" } });
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100252 }
253
254 bool run() override
255 {
256 // The status of this variable can change in VALIDATE_TEST()
257 bool all_tests_passed = true;
258
259 int32_t test_idx = 0;
260
261 for(const auto &test : _values)
262 {
Viet-Hoa Do0250fa62023-07-24 15:47:34 +0100263 const CLTile tile(test, dt);
264 const int32_t width = tile.info().width();
265 const int32_t height = tile.info().height();
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100266
267 for(int32_t row = 0; row < height; ++row)
268 {
269 std::string expected_var_name = "((";
270 expected_var_name += cl_get_variable_datatype_as_string(dt, width);
271 expected_var_name += ")(";
272
273 int32_t col = 0;
274 for(; col < width - 1; ++col)
275 {
276 expected_var_name += test[row][col];
277 expected_var_name += ", ";
278 }
279
280 expected_var_name += test[row][col];
281 expected_var_name += "))";
282
283 const std::string actual_var_name = tile.vector(row).str;
284 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++);
285 }
286 }
287 return all_tests_passed;
288 }
289
290 std::string name() override
291 {
292 return "CLConstantTileAccessVectorVariablesTest";
293 }
294
295private:
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100296 std::vector<TileContainer> _values{};
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100297};
298
299class CLConstantTileAccessSubVectorVariablesTest : public ITest
300{
301public:
302 const DataType dt = DataType::Fp16;
303
304 CLConstantTileAccessSubVectorVariablesTest()
305 {
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100306 _values.push_back({ { "1.2", "acc", "8.7", "9.3", "ratio", "2.9", "1.7", "0.3" } });
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100307 _subwidths.push_back(1);
308 _subwidths.push_back(2);
309 _subwidths.push_back(3);
310 _subwidths.push_back(4);
311 _offsets.push_back(1);
312 _offsets.push_back(3);
313 _offsets.push_back(4);
314 }
315
316 bool run() override
317 {
318 // The status of this variable can change in VALIDATE_TEST()
319 bool all_tests_passed = true;
320
321 size_t test_idx = 0;
322
323 for(auto &test : _values)
324 {
325 for(auto &col_start : _offsets)
326 {
327 for(auto &subwidth : _subwidths)
328 {
Viet-Hoa Do0250fa62023-07-24 15:47:34 +0100329 const CLTile tile(test, dt);
330 const int32_t height = tile.info().height();
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100331
332 for(int32_t row = 0; row < height; ++row)
333 {
334 std::string expected_var_name = "((";
335 expected_var_name += cl_get_variable_datatype_as_string(dt, subwidth);
336 expected_var_name += ")(";
337
338 int32_t col = col_start;
339 for(; col < subwidth - 1; ++col)
340 {
341 expected_var_name += test[row][col];
342 expected_var_name += ", ";
343 }
344
345 expected_var_name += test[row][col];
346 expected_var_name += "))";
347
348 const std::string actual_var_name = tile.vector(row, col_start, subwidth).str;
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100349 VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed,
350 test_idx++);
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100351 }
352 }
353 }
354 }
355 return all_tests_passed;
356 }
357
358 std::string name() override
359 {
360 return "CLConstantTileAccessSubVectorVariablesTest";
361 }
362
363private:
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100364 std::vector<TileContainer> _values{};
365 std::vector<int32_t> _subwidths{};
366 std::vector<int32_t> _offsets{};
Gian Marco Iodice68e9c4d2023-06-15 17:40:28 +0100367};
368
369} // namespace ckw
370
Viet-Hoa Do0250fa62023-07-24 15:47:34 +0100371#endif // CKW_TESTS_CLCONSTANTTILETEST_HPP