Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 24 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 25 | #include "src/cl/CLHelpers.h" |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 26 | |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 27 | #include "ckw/Error.h" |
Nikolaj Jensen | 5ff4802 | 2023-06-27 14:13:24 +0100 | [diff] [blame] | 28 | #include "ckw/types/DataType.h" |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 29 | #include "ckw/types/Operators.h" |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 30 | #include "ckw/types/TensorStorageType.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 31 | |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 32 | #include "src/types/DataTypeHelpers.h" |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 33 | |
| 34 | namespace ckw |
| 35 | { |
| 36 | bool cl_validate_vector_length(int32_t len) |
| 37 | { |
| 38 | bool valid_vector_length = true; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 39 | if (len < 1 || len > 16 || (len > 4 && len < 8) || (len > 8 && len < 16)) |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 40 | { |
| 41 | valid_vector_length = false; |
| 42 | } |
| 43 | return valid_vector_length; |
| 44 | } |
| 45 | |
| 46 | std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len) |
| 47 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 48 | if (cl_validate_vector_length(len) == false) |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 49 | { |
Gunes Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 50 | CKW_THROW_MSG("Unsupported vector length"); |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 51 | return ""; |
| 52 | } |
| 53 | |
| 54 | std::string res; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 55 | switch (dt) |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 56 | { |
| 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 Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 85 | CKW_THROW_MSG("Unsupported datatype"); |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 86 | return ""; |
| 87 | } |
| 88 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 89 | if (len > 1) |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 90 | { |
| 91 | res += std::to_string(len); |
| 92 | } |
| 93 | |
| 94 | return res; |
| 95 | } |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 96 | |
Gunes Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 97 | int32_t cl_round_up_to_nearest_valid_vector_width(int32_t width) |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 98 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 99 | switch (width) |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 100 | { |
| 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 Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 128 | |
| 129 | std::string cl_get_variable_storagetype_as_string(TensorStorageType storage) |
| 130 | { |
| 131 | std::string res; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 132 | switch (storage) |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 133 | { |
| 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 Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 144 | CKW_THROW_MSG("Unsupported storage type"); |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return res; |
| 148 | } |
| 149 | |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 150 | std::string cl_get_assignment_op_as_string(AssignmentOp op) |
| 151 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 152 | switch (op) |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 153 | { |
| 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 Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 165 | std::tuple<bool, std::string> cl_get_unary_op(UnaryOp op) |
| 166 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 167 | switch (op) |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 168 | { |
| 169 | case UnaryOp::LogicalNot: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 170 | return {false, "!"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 171 | |
| 172 | case UnaryOp::BitwiseNot: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 173 | return {false, "~"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 174 | |
| 175 | case UnaryOp::Exp: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 176 | return {true, "exp"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 177 | |
| 178 | case UnaryOp::Tanh: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 179 | return {true, "tanh"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 180 | |
| 181 | case UnaryOp::Sqrt: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 182 | return {true, "sqrt"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 183 | |
| 184 | case UnaryOp::Erf: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 185 | return {true, "erf"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 186 | |
| 187 | case UnaryOp::Fabs: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 188 | return {true, "fabs"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 189 | |
| 190 | case UnaryOp::Log: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 191 | return {true, "log"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 192 | |
| 193 | case UnaryOp::Round: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 194 | return {true, "round"}; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 195 | |
| 196 | default: |
| 197 | CKW_THROW_MSG("Unsupported unary operation!"); |
| 198 | } |
| 199 | } |
| 200 | |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 201 | std::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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 205 | switch (op) |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 206 | { |
| 207 | case BinaryOp::Add: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 208 | return {false, "+"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 209 | |
| 210 | case BinaryOp::Sub: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 211 | return {false, "-"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 212 | |
| 213 | case BinaryOp::Mul: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 214 | return {false, "*"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 215 | |
| 216 | case BinaryOp::Div: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 217 | return {false, "/"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 218 | |
| 219 | case BinaryOp::Mod: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 220 | return {false, "%"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 221 | |
| 222 | case BinaryOp::Equal: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 223 | return {false, "=="}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 224 | |
| 225 | case BinaryOp::Less: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 226 | return {false, "<"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 227 | |
| 228 | case BinaryOp::LessEqual: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 229 | return {false, "<="}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 230 | |
| 231 | case BinaryOp::Greater: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 232 | return {false, ">"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 233 | |
| 234 | case BinaryOp::GreaterEqual: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 235 | return {false, ">="}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 236 | |
| 237 | case BinaryOp::LogicalAnd: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 238 | return {false, "&&"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 239 | |
| 240 | case BinaryOp::LogicalOr: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 241 | return {false, "||"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 242 | |
| 243 | case BinaryOp::BitwiseXOR: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 244 | return {false, "^"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 245 | |
| 246 | case BinaryOp::Min: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 247 | return {true, is_float ? "fmin" : "min"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 248 | |
| 249 | case BinaryOp::Max: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 250 | return {true, is_float ? "fmax" : "max"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 251 | |
| 252 | default: |
| 253 | CKW_THROW_MSG("Unsupported binary operator/function!"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | std::tuple<bool, std::string> cl_get_ternary_op(TernaryOp op) |
| 258 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 259 | switch (op) |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 260 | { |
| 261 | case TernaryOp::Select: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 262 | return {true, "select"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 263 | |
| 264 | case TernaryOp::Clamp: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 265 | return {true, "clamp"}; |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 266 | |
| 267 | default: |
| 268 | CKW_THROW_MSG("Unsupported ternary function!"); |
| 269 | } |
| 270 | } |
| 271 | |
Gunes Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 272 | std::string cl_data_type_rounded_up_to_valid_vector_width(DataType dt, int32_t width) |
| 273 | { |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 274 | std::string data_type; |
| 275 | const int32_t w = cl_round_up_to_nearest_valid_vector_width(width); |
Gunes Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 276 | data_type += cl_get_variable_datatype_as_string(dt, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 277 | if (w != 1) |
Gunes Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 278 | { |
| 279 | data_type += std::to_string(w); |
| 280 | } |
| 281 | return data_type; |
| 282 | } |
| 283 | |
| 284 | std::vector<int32_t> cl_decompose_vector_width(int32_t vector_width) |
| 285 | { |
| 286 | std::vector<int32_t> x; |
| 287 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 288 | switch (vector_width) |
Gunes Bayir | e98413e | 2023-07-27 22:50:22 +0100 | [diff] [blame] | 289 | { |
| 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 Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 350 | } // namespace ckw |