blob: 8e4a932764832c18da4f3ad49d36778b970a221c [file] [log] [blame]
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +00001/*
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 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +010024
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010025#include "src/cl/CLHelpers.h"
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +010026
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000027#include "ckw/Error.h"
Nikolaj Jensen5ff48022023-06-27 14:13:24 +010028#include "ckw/types/DataType.h"
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010029#include "ckw/types/Operators.h"
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010030#include "ckw/types/TensorStorageType.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +010032#include "src/types/DataTypeHelpers.h"
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000033
34namespace ckw
35{
36bool cl_validate_vector_length(int32_t len)
37{
38 bool valid_vector_length = true;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 if (len < 1 || len > 16 || (len > 4 && len < 8) || (len > 8 && len < 16))
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000040 {
41 valid_vector_length = false;
42 }
43 return valid_vector_length;
44}
45
46std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len)
47{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 if (cl_validate_vector_length(len) == false)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000049 {
Gunes Bayire98413e2023-07-27 22:50:22 +010050 CKW_THROW_MSG("Unsupported vector length");
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000051 return "";
52 }
53
54 std::string res;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 switch (dt)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000056 {
57 case DataType::Fp32:
58 res += "float";
59 break;
60 case DataType::Fp16:
61 res += "half";
62 break;
63 case DataType::Int8:
64 res += "char";
65 break;
66 case DataType::Uint8:
67 res += "uchar";
68 break;
69 case DataType::Uint16:
70 res += "ushort";
71 break;
72 case DataType::Int16:
73 res += "short";
74 break;
75 case DataType::Uint32:
76 res += "uint";
77 break;
78 case DataType::Int32:
79 res += "int";
80 break;
81 case DataType::Bool:
82 res += "bool";
83 break;
84 default:
Gunes Bayire98413e2023-07-27 22:50:22 +010085 CKW_THROW_MSG("Unsupported datatype");
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000086 return "";
87 }
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 if (len > 1)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000090 {
91 res += std::to_string(len);
92 }
93
94 return res;
95}
Gunes Bayirab0b7502023-07-11 14:57:36 +010096
Gunes Bayire98413e2023-07-27 22:50:22 +010097int32_t cl_round_up_to_nearest_valid_vector_width(int32_t width)
Gunes Bayirab0b7502023-07-11 14:57:36 +010098{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 switch (width)
Gunes Bayirab0b7502023-07-11 14:57:36 +0100100 {
101 case 1:
102 return 1;
103 case 2:
104 return 2;
105 case 3:
106 return 3;
107 case 4:
108 return 4;
109 case 5:
110 case 6:
111 case 7:
112 case 8:
113 return 8;
114 case 9:
115 case 10:
116 case 11:
117 case 12:
118 case 13:
119 case 14:
120 case 15:
121 case 16:
122 return 16;
123 default:
124 CKW_THROW_MSG("Unsupported width to convert to OpenCL vector");
125 return 0;
126 }
127}
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100128
129std::string cl_get_variable_storagetype_as_string(TensorStorageType storage)
130{
131 std::string res;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 switch (storage)
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100133 {
134 case TensorStorageType::BufferUint8Ptr:
135 res += "__global uchar*";
136 break;
137 case TensorStorageType::Texture2dReadOnly:
138 res += "__read_only image2d_t";
139 break;
140 case TensorStorageType::Texture2dWriteOnly:
141 res += "__write_only image2d_t";
142 break;
143 default:
Gunes Bayire98413e2023-07-27 22:50:22 +0100144 CKW_THROW_MSG("Unsupported storage type");
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100145 }
146
147 return res;
148}
149
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +0100150std::string cl_get_assignment_op_as_string(AssignmentOp op)
151{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100152 switch (op)
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +0100153 {
154 case AssignmentOp::Increment:
155 return "+=";
156
157 case AssignmentOp::Decrement:
158 return "-=";
159
160 default:
161 CKW_THROW_MSG("Unsupported assignment operator!");
162 }
163}
164
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100165std::tuple<bool, std::string> cl_get_unary_op(UnaryOp op)
166{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 switch (op)
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100168 {
169 case UnaryOp::LogicalNot:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 return {false, "!"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100171
172 case UnaryOp::BitwiseNot:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 return {false, "~"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100174
175 case UnaryOp::Exp:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176 return {true, "exp"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100177
178 case UnaryOp::Tanh:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 return {true, "tanh"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100180
181 case UnaryOp::Sqrt:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182 return {true, "sqrt"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100183
184 case UnaryOp::Erf:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100185 return {true, "erf"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100186
187 case UnaryOp::Fabs:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 return {true, "fabs"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100189
190 case UnaryOp::Log:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 return {true, "log"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100192
193 case UnaryOp::Round:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 return {true, "round"};
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100195
196 default:
197 CKW_THROW_MSG("Unsupported unary operation!");
198 }
199}
200
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100201std::tuple<bool, std::string> cl_get_binary_op(BinaryOp op, DataType data_type)
202{
203 const auto is_float = is_data_type_float(data_type);
204
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 switch (op)
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100206 {
207 case BinaryOp::Add:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208 return {false, "+"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100209
210 case BinaryOp::Sub:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 return {false, "-"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100212
213 case BinaryOp::Mul:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 return {false, "*"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100215
216 case BinaryOp::Div:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100217 return {false, "/"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100218
219 case BinaryOp::Mod:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100220 return {false, "%"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100221
222 case BinaryOp::Equal:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 return {false, "=="};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100224
225 case BinaryOp::Less:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 return {false, "<"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100227
228 case BinaryOp::LessEqual:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229 return {false, "<="};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100230
231 case BinaryOp::Greater:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100232 return {false, ">"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100233
234 case BinaryOp::GreaterEqual:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100235 return {false, ">="};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100236
237 case BinaryOp::LogicalAnd:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238 return {false, "&&"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100239
240 case BinaryOp::LogicalOr:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100241 return {false, "||"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100242
243 case BinaryOp::BitwiseXOR:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100244 return {false, "^"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100245
246 case BinaryOp::Min:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 return {true, is_float ? "fmin" : "min"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100248
249 case BinaryOp::Max:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100250 return {true, is_float ? "fmax" : "max"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100251
252 default:
253 CKW_THROW_MSG("Unsupported binary operator/function!");
254 }
255}
256
257std::tuple<bool, std::string> cl_get_ternary_op(TernaryOp op)
258{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 switch (op)
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100260 {
261 case TernaryOp::Select:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100262 return {true, "select"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100263
264 case TernaryOp::Clamp:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 return {true, "clamp"};
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100266
267 default:
268 CKW_THROW_MSG("Unsupported ternary function!");
269 }
270}
271
Gunes Bayire98413e2023-07-27 22:50:22 +0100272std::string cl_data_type_rounded_up_to_valid_vector_width(DataType dt, int32_t width)
273{
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100274 std::string data_type;
275 const int32_t w = cl_round_up_to_nearest_valid_vector_width(width);
Gunes Bayire98413e2023-07-27 22:50:22 +0100276 data_type += cl_get_variable_datatype_as_string(dt, 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100277 if (w != 1)
Gunes Bayire98413e2023-07-27 22:50:22 +0100278 {
279 data_type += std::to_string(w);
280 }
281 return data_type;
282}
283
284std::vector<int32_t> cl_decompose_vector_width(int32_t vector_width)
285{
286 std::vector<int32_t> x;
287
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100288 switch (vector_width)
Gunes Bayire98413e2023-07-27 22:50:22 +0100289 {
290 case 0:
291 break;
292 case 1:
293 case 2:
294 case 3:
295 case 4:
296 case 8:
297 case 16:
298 x.push_back(vector_width);
299 break;
300 case 5:
301 x.push_back(4);
302 x.push_back(1);
303 break;
304 case 6:
305 x.push_back(4);
306 x.push_back(2);
307 break;
308 case 7:
309 x.push_back(4);
310 x.push_back(3);
311 break;
312 case 9:
313 x.push_back(8);
314 x.push_back(1);
315 break;
316 case 10:
317 x.push_back(8);
318 x.push_back(2);
319 break;
320 case 11:
321 x.push_back(8);
322 x.push_back(3);
323 break;
324 case 12:
325 x.push_back(8);
326 x.push_back(4);
327 break;
328 case 13:
329 x.push_back(8);
330 x.push_back(4);
331 x.push_back(1);
332 break;
333 case 14:
334 x.push_back(8);
335 x.push_back(4);
336 x.push_back(2);
337 break;
338 case 15:
339 x.push_back(8);
340 x.push_back(4);
341 x.push_back(3);
342 break;
343
344 default:
345 CKW_THROW_MSG("Vector width is too large");
346 }
347 return x;
348}
349
Gunes Bayirab0b7502023-07-11 14:57:36 +0100350} // namespace ckw