Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2024, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "tensor_ops.h" |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 17 | #include "half.hpp" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 18 | #include "quant_util.h" |
| 19 | #include "template_types.h" |
| 20 | |
| 21 | using namespace TosaReference; |
| 22 | using namespace Eigen; |
| 23 | using namespace tosa; |
| 24 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 25 | int check_pool2d_attribute(tosa::TosaPoolAttribute* attribute, |
| 26 | std::vector<int32_t> input_shape, |
| 27 | std::vector<int32_t> output_shape, |
| 28 | std::string& msg) |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 29 | { |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 30 | if (attribute->pad().size() != 4) |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 31 | { |
| 32 | msg = "illegal size for attribute padding"; |
| 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | if (attribute->kernel().size() != 2) |
| 37 | { |
| 38 | msg = "illegal size for attribute kernel"; |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | if (attribute->stride().size() != 2) |
| 43 | { |
| 44 | msg = "illegal size for attribute stride"; |
| 45 | return 1; |
| 46 | } |
| 47 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 48 | for (int32_t i : attribute->pad()) |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 49 | { |
| 50 | if (i < 0) |
| 51 | { |
| 52 | msg = "At least one pad is smaller than zero"; |
| 53 | return 1; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | for (int32_t i : attribute->kernel()) |
| 58 | { |
| 59 | if (i < 1) |
| 60 | { |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 61 | msg = "At least one kernel dimension is smaller than one"; |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | for (int32_t i : attribute->stride()) |
| 67 | { |
| 68 | if (i < 1) |
| 69 | { |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 70 | msg = "At least one stride dimension is smaller than one"; |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | int32_t IH = input_shape[1]; |
| 76 | int32_t IW = input_shape[2]; |
| 77 | int32_t OH = output_shape[1]; |
| 78 | int32_t OW = output_shape[2]; |
| 79 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 80 | int32_t pad_top = attribute->pad()[0]; |
| 81 | int32_t pad_bottom = attribute->pad()[1]; |
| 82 | int32_t pad_left = attribute->pad()[2]; |
| 83 | int32_t pad_right = attribute->pad()[3]; |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 84 | |
| 85 | int32_t stride_y = attribute->stride()[0]; |
| 86 | int32_t stride_x = attribute->stride()[1]; |
| 87 | int32_t kernel_y = attribute->kernel()[0]; |
| 88 | int32_t kernel_x = attribute->kernel()[1]; |
| 89 | |
| 90 | if (pad_top >= kernel_y || pad_bottom >= kernel_y || pad_left >= kernel_x || pad_right >= kernel_x) |
| 91 | { |
| 92 | msg = "At least one pad is >= kernel dimension"; |
| 93 | return 1; |
| 94 | } |
| 95 | |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 96 | int32_t full_H = IH + pad_top + pad_bottom - kernel_y; |
| 97 | int32_t full_W = IW + pad_left + pad_right - kernel_x; |
| 98 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 99 | if ((full_H % stride_y != 0) || (full_W % stride_x != 0)) |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 100 | { |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 101 | msg = "Parameters must yield exact integer output dimensions"; |
| 102 | return 1; |
| 103 | } |
| 104 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 105 | if ((OH != (full_H / stride_y) + 1) || (OW != (full_W / stride_x) + 1)) |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 106 | { |
| 107 | msg = "Mismatch between output shape provided and expected output shape (" + |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 108 | std::to_string((full_H / stride_y) + 1) + "," + std::to_string((full_W / stride_x) + 1) + ")"; |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 115 | int check_conv_attribute(tosa::TosaConvAttribute* attribute, |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 116 | uint32_t conv_dimension, |
| 117 | std::vector<int32_t> input_shape, |
| 118 | std::vector<int32_t> output_shape, |
| 119 | std::vector<int32_t> weights, |
| 120 | uint32_t offset_kernel, |
| 121 | TOSA_REF_TYPE InDtype, |
| 122 | TOSA_REF_TYPE WeightDtype, |
| 123 | std::string& msg) |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 124 | { |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 125 | if (attribute->pad().size() != (2 * conv_dimension)) |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 126 | { |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 127 | msg = "Illegal size for attribute pad"; |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | if (attribute->stride().size() != conv_dimension) |
| 132 | { |
| 133 | msg = "Illegal size for attribute stride"; |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | if (attribute->dilation().size() != conv_dimension) |
| 138 | { |
| 139 | msg = "Illegal size for attribute dilation"; |
| 140 | return 1; |
| 141 | } |
| 142 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 143 | for (int32_t i : attribute->pad()) |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 144 | { |
| 145 | if (i < 0) |
| 146 | { |
| 147 | msg = "At least one pad is smaller than zero"; |
| 148 | return 1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | for (int32_t i : attribute->stride()) |
| 153 | { |
| 154 | if (i < 1) |
| 155 | { |
| 156 | msg = "At least one stride dimension is smaller than one"; |
| 157 | return 1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | for (int32_t i : attribute->dilation()) |
| 162 | { |
| 163 | if (i < 1) |
| 164 | { |
| 165 | msg = "At least one dilation dimension is smaller than one"; |
| 166 | return 1; |
| 167 | } |
| 168 | } |
| 169 | |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 170 | ASSERT_MSG(conv_dimension == 2 || conv_dimension == 3, "Unsupported convolution dimension") |
| 171 | |
TatWai Chong | fd62905 | 2022-07-25 04:01:58 +0000 | [diff] [blame] | 172 | int32_t offset_d = conv_dimension == 3 ? 1 : 0; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 173 | int32_t ID = conv_dimension == 3 ? input_shape[1] : 1; |
| 174 | int32_t IH = input_shape[1 + offset_d]; |
| 175 | int32_t IW = input_shape[2 + offset_d]; |
| 176 | int32_t OD = conv_dimension == 3 ? output_shape[1] : 1; |
| 177 | int32_t OH = output_shape[1 + offset_d]; |
| 178 | int32_t OW = output_shape[2 + offset_d]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 179 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 180 | int32_t stride_d = conv_dimension == 3 ? attribute->stride()[0] : 1; |
| 181 | int32_t stride_y = attribute->stride()[0 + offset_d]; |
| 182 | int32_t stride_x = attribute->stride()[1 + offset_d]; |
| 183 | int32_t kernel_d = conv_dimension == 3 ? weights[offset_kernel] : 1; |
| 184 | int32_t kernel_h = weights[offset_kernel + offset_d]; |
| 185 | int32_t kernel_w = weights[offset_kernel + 1 + offset_d]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 186 | int32_t dilation_d = conv_dimension == 3 ? attribute->dilation()[0] : 1; |
| 187 | int32_t dilation_y = attribute->dilation()[0 + offset_d]; |
| 188 | int32_t dilation_x = attribute->dilation()[1 + offset_d]; |
| 189 | |
| 190 | offset_d *= 2; |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 191 | int32_t pad_d0 = conv_dimension == 3 ? attribute->pad()[0] : 0; |
| 192 | int32_t pad_d1 = conv_dimension == 3 ? attribute->pad()[1] : 0; |
| 193 | int32_t pad_top = attribute->pad()[0 + offset_d]; |
| 194 | int32_t pad_bottom = attribute->pad()[1 + offset_d]; |
| 195 | int32_t pad_left = attribute->pad()[2 + offset_d]; |
| 196 | int32_t pad_right = attribute->pad()[3 + offset_d]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 197 | |
| 198 | int32_t full_D = ID - 1 + pad_d0 + pad_d1 - (kernel_d - 1) * dilation_d; |
| 199 | int32_t full_H = IH - 1 + pad_top + pad_bottom - (kernel_h - 1) * dilation_y; |
| 200 | int32_t full_W = IW - 1 + pad_left + pad_right - (kernel_w - 1) * dilation_x; |
| 201 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 202 | if ((full_H % stride_y != 0) || (full_W % stride_x != 0) || (full_D % stride_d != 0)) |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 203 | { |
| 204 | msg = "Parameters must yield exact integer output dimensions"; |
| 205 | return 1; |
| 206 | } |
| 207 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 208 | if ((OH != (full_H / stride_y) + 1) || (OW != (full_W / stride_x) + 1) || (OD != (full_D / stride_d) + 1)) |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 209 | { |
| 210 | std::string msg_d = ""; |
| 211 | if (conv_dimension == 3) |
| 212 | { |
| 213 | msg_d += std::to_string((full_D / stride_d) + 1) + ","; |
| 214 | } |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 215 | msg = "Mismatch between output shape provided and expected output shape (" + msg_d + |
| 216 | std::to_string((full_H / stride_y) + 1) + "," + std::to_string((full_W / stride_x) + 1) + ")"; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 220 | if (InDtype != TOSA_REF_TYPE_INT8 && attribute->input_zp() != 0) |
| 221 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 222 | msg = "Input zero point must be zero for non-int8 data"; |
| 223 | return 1; |
| 224 | } |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 225 | if (WeightDtype != TOSA_REF_TYPE_INT8 && attribute->weight_zp() != 0) |
| 226 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 227 | msg = "Weight zero point must be zero for non-int8 data"; |
| 228 | return 1; |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 234 | int check_fft_shape(const std::vector<int32_t>& in_real, |
| 235 | const std::vector<int32_t>& in_imag, |
| 236 | const std::vector<int32_t>& out_real, |
| 237 | const std::vector<int32_t>& out_imag, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 238 | std::string& msg) |
| 239 | { |
| 240 | const bool is_rfft = in_imag.empty(); |
| 241 | auto is_power_of_two = [](int32_t n) -> bool { return (n & (n - 1)) == 0 && n > 0; }; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 242 | |
| 243 | if (!is_power_of_two(in_real[1]) || !is_power_of_two(in_real[2])) |
| 244 | { |
| 245 | msg = "Input height and width must be a power of two"; |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | // RFFT does not have a second input |
| 250 | if (!is_rfft) |
| 251 | { |
| 252 | bool input_check = true; |
| 253 | for (size_t i = 0; i < in_real.size(); i++) |
| 254 | { |
| 255 | if (in_real[i] != in_imag[i]) |
| 256 | { |
| 257 | input_check = false; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | if (!input_check) |
| 262 | { |
| 263 | msg = "Mismatch between real input shape and imaginary input shape"; |
| 264 | return 1; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | bool output_check = true; |
| 269 | for (size_t i = 0; i < out_real.size(); i++) |
| 270 | { |
| 271 | if (out_real[i] != out_imag[i]) |
| 272 | { |
| 273 | output_check = false; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | if (!output_check) |
| 278 | { |
| 279 | msg = "Mismatch between real output shape and imaginary output shape"; |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | if (in_real[0] != out_real[0]) |
| 284 | { |
| 285 | msg = "Input and output batch size don't match"; |
| 286 | return 1; |
| 287 | } |
| 288 | if (in_real[1] != out_real[1]) |
| 289 | { |
| 290 | msg = "Input and output height don't match"; |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | if (is_rfft) |
| 295 | { |
| 296 | if (in_real[2] / 2 + 1 != out_real[2]) |
| 297 | { |
| 298 | msg = "Output width is expected to match input width / 2 + 1"; |
| 299 | return 1; |
| 300 | } |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 304 | if (in_real[2] != out_real[2]) |
| 305 | { |
| 306 | msg = "Input and output width don't match"; |
| 307 | return 1; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 314 | template <int Rank, TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 315 | OpArgMax<Rank, Dtype>::OpArgMax(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 316 | : GraphNode(sgt_, Op_ARGMAX, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 317 | { |
| 318 | setRequiredOperands(1, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 319 | setRequiredRank(1); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 320 | |
| 321 | INIT_ATTRIBUTE(Axis); |
| 322 | } |
| 323 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 324 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 325 | OpArgMax<Rank, Dtype>::~OpArgMax() |
| 326 | { |
| 327 | if (attribute) |
| 328 | delete attribute; |
| 329 | } |
| 330 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 331 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 332 | int OpArgMax<Rank, Dtype>::checkTensorAttributes() |
| 333 | { |
| 334 | if (validateRequiredOperands()) |
| 335 | return 1; |
| 336 | |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 337 | if (validateRequiredRank(inputs[0])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 338 | { |
| 339 | return 1; |
| 340 | } |
| 341 | |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 342 | int32_t output_rank = inputs[0]->getRank() - 1; |
| 343 | if (output_rank != outputs[0]->getRank()) |
| 344 | { |
| 345 | printNodeValidationError("OpArgMax: Output rank needs to be rank(input) - 1"); |
| 346 | return 1; |
| 347 | } |
| 348 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 349 | if (outputs[0]->getDtype() != TOSA_REF_TYPE_INT32) |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 350 | { |
| 351 | printNodeValidationError("OpArgMax: Output data type not supported for this configuration of operator"); |
| 352 | return 1; |
| 353 | } |
| 354 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 355 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 356 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 357 | |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 358 | if (attribute->axis() < 0 || attribute->axis() >= input->getRank()) |
| 359 | { |
| 360 | printNodeValidationError("OpArgMax: Axis needs to be within [0, rank(input)]"); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | bool shape_check = true; |
| 365 | for (int32_t i = 0; i < input->getRank(); i++) |
| 366 | { |
| 367 | if (i < attribute->axis()) |
| 368 | { |
| 369 | if (input->getShape()[i] != output->getShape()[i]) |
| 370 | { |
| 371 | shape_check = false; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | else if (i > attribute->axis()) |
| 376 | { |
| 377 | if (input->getShape()[i] != output->getShape()[i - 1]) |
| 378 | { |
| 379 | shape_check = false; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | // No need to check i == axis |
| 384 | } |
| 385 | if (!shape_check) |
| 386 | { |
| 387 | printNodeValidationError("OpArgMax: Mismatch between output shape provided and expected output shape"); |
| 388 | return 1; |
| 389 | } |
| 390 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 394 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 395 | int OpArgMax<Rank, Dtype>::eval() |
| 396 | { |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 397 | // Check Tosa Level |
| 398 | auto tosa_level = g_func_config.tosa_level; |
| 399 | LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK"); |
| 400 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 401 | Eigen::Tensor<DenseIndex, Rank - 1> index = this->input->getTensor().argmax(attribute->axis()); |
| 402 | |
| 403 | this->output->getTensor() = index.unaryExpr([](DenseIndex in) -> OutEigenType { return (OutEigenType)in; }); |
| 404 | |
| 405 | return GraphNode::eval(); |
| 406 | } |
| 407 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 408 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE AccDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 409 | OpAvgPool2d<Dtype, AccDtype>::OpAvgPool2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 410 | : GraphNode(sgt_, Op_AVG_POOL2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 411 | { |
| 412 | setRequiredOperands(1, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 413 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 414 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 415 | INIT_ATTRIBUTE(Pool); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 418 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE AccDtype> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 419 | OpAvgPool2d<Dtype, AccDtype>::~OpAvgPool2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 420 | { |
| 421 | if (attribute) |
| 422 | delete attribute; |
| 423 | } |
| 424 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 425 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE AccDtype> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 426 | int OpAvgPool2d<Dtype, AccDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 427 | { |
| 428 | if (validateRequiredOperands()) |
| 429 | return 1; |
| 430 | |
| 431 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) |
| 432 | { |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | if (inputs[0]->matchType(*outputs[0])) |
| 437 | { |
| 438 | printNodeValidationError("OpAvgPool2d: input and output tensor type mismatch"); |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 443 | out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 444 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 445 | ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->input_zp() != 0, |
| 446 | "OpAvgPool2d: Input zeropoint must be zero for non int8_t data"); |
| 447 | ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->output_zp() != 0, |
| 448 | "OpAvgPool2d: Output zeropoint must be zero for non int8_t data"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 449 | |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 450 | std::string msg; |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 451 | if (check_pool2d_attribute(attribute, in->getShape(), out->getShape(), msg)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 452 | { |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 453 | msg = "OpAvgPool2d: " + msg; |
| 454 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 461 | // assuming input and output tensor have same scales like tflite reference |
| 462 | // so no need to scale input and output |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 463 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE AccDtype> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 464 | int OpAvgPool2d<Dtype, AccDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 465 | { |
| 466 | int in_batch = this->in->getShape()[0]; |
| 467 | int in_height = this->in->getShape()[1]; |
| 468 | int in_width = this->in->getShape()[2]; |
| 469 | int in_channels = this->in->getShape()[3]; |
| 470 | |
| 471 | int out_batch = this->out->getShape()[0]; |
| 472 | int out_height = this->out->getShape()[1]; |
| 473 | int out_width = this->out->getShape()[2]; |
| 474 | int out_channels = this->out->getShape()[3]; |
| 475 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 476 | ERROR_IF(in_batch != out_batch, "OpAvgPool2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 477 | ERROR_IF(in_channels != out_channels, "OpAvgPool2d: tensor channel mismatch %d != %d", in_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 478 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 479 | int pad_top = this->attribute->pad()[0]; |
| 480 | int pad_bottom = this->attribute->pad()[1]; |
| 481 | int pad_left = this->attribute->pad()[2]; |
| 482 | int pad_right = this->attribute->pad()[3]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 483 | int kernel_y = this->attribute->kernel()[0]; |
| 484 | int kernel_x = this->attribute->kernel()[1]; |
| 485 | int stride_y = this->attribute->stride()[0]; |
| 486 | int stride_x = this->attribute->stride()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 487 | |
| 488 | // Check Tosa Level |
| 489 | auto tosa_level = g_func_config.tosa_level; |
| 490 | LEVEL_CHECK(kernel_y <= tosa_level.MAX_KERNEL, "kernel_y should be smaller than or equal to MAX_KERNEL"); |
| 491 | LEVEL_CHECK(kernel_x <= tosa_level.MAX_KERNEL, "kernel_x should be smaller than or equal to MAX_KERNEL"); |
| 492 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 493 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 494 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 495 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 496 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 497 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 498 | |
Tai Ly | 8ead6c4 | 2024-02-14 22:35:44 +0000 | [diff] [blame] | 499 | TOSA_REF_TYPE accum_dtype = ConvertDType(this->attribute->acc_type()); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 500 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 501 | DEBUG_INFO(OP, |
| 502 | "perform AvgPool2d, input.shape=[%d,%d,%d,%d], output.shape=[%d,%d,%d,%d], kernel=[%d,%d], " |
Tai Ly | 8ead6c4 | 2024-02-14 22:35:44 +0000 | [diff] [blame] | 503 | "stride=[%d,%d], pad=[%d,%d,%d,%d], acc_type=%s", |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 504 | in_batch, in_height, in_width, in_channels, out_batch, out_height, out_width, out_channels, kernel_y, |
| 505 | kernel_x, stride_y, stride_x, pad_top, pad_bottom, pad_left, pad_right, EnumNamesDType()[accum_dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 506 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 507 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 508 | pad[0] = std::make_pair(0, 0); |
| 509 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 510 | pad[2] = std::make_pair(pad_left, pad_right); |
| 511 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 512 | |
| 513 | ETensor4<InEigenType> input_val = this->in->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 514 | if (Dtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 515 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 516 | input_val = input_val - (InEigenType)attribute->input_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 519 | if (g_func_config.abs_mode) |
| 520 | { |
James Ward | 29e46cf | 2023-10-23 11:47:25 +0000 | [diff] [blame] | 521 | // in abs_mode: take abs values of input_val |
| 522 | input_val = input_val.abs(); |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 525 | // assuming input and output have same scales |
| 526 | // so input and output scaling is not required |
| 527 | // TODO: check if this assumption TOSA made |
| 528 | |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 529 | ETensor4<OutEigenType> out_tens(out_batch, out_height, out_width, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 530 | |
| 531 | // sum pool |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 532 | for (int ob = 0; ob < out_batch; ++ob) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 533 | { |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 534 | for (int oh = 0; oh < out_height; ++oh) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 535 | { |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 536 | for (int ow = 0; ow < out_width; ++ow) |
| 537 | { |
| 538 | for (int oc = 0; oc < out_channels; ++oc) |
| 539 | { |
| 540 | AccEigenType acc(0); |
| 541 | int filter_count = 0; |
| 542 | const int iy = oh * stride_y - pad_top; |
| 543 | const int ix = ow * stride_x - pad_left; |
| 544 | for (int ky = 0; ky < kernel_y; ++ky) |
| 545 | { |
| 546 | for (int kx = 0; kx < kernel_x; ++kx) |
| 547 | { |
| 548 | const int y = iy + ky; |
| 549 | const int x = ix + kx; |
| 550 | if ((0 <= y && y < in_height) && (0 <= x && x < in_width)) |
| 551 | { |
| 552 | ++filter_count; |
James Ward | 29e46cf | 2023-10-23 11:47:25 +0000 | [diff] [blame] | 553 | acc = acc + (AccEigenType)input_val(ob, y, x, oc); |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
| 557 | if (Dtype != TOSA_REF_TYPE_FP32 && Dtype != TOSA_REF_TYPE_FP16 && Dtype != TOSA_REF_TYPE_BF16 && |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 558 | Dtype != TOSA_REF_TYPE_FP64 && Dtype != TOSA_REF_TYPE_FP8E4M3 && Dtype != TOSA_REF_TYPE_FP8E5M2) |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 559 | { |
| 560 | try |
| 561 | { |
| 562 | int32_t multiplier, shift; |
| 563 | OutEigenType out; |
| 564 | TosaReference::QuantUtil::reciprocal_scale(filter_count, multiplier, shift); |
| 565 | |
| 566 | out = (OutEigenType)TosaReference::QuantUtil::apply_scale_32(acc, multiplier, shift, false); |
| 567 | out = out + (OutEigenType)(attribute->output_zp()); |
| 568 | out = std::max(out, (OutEigenType)QMin); |
| 569 | out_tens(ob, oh, ow, oc) = std::min(out, (OutEigenType)QMax); |
| 570 | } |
| 571 | catch (std::string desc) |
| 572 | { |
| 573 | REQUIRE(false, "OpAvgPool2d apply_scale_32() fails: %s.", desc.c_str()); |
| 574 | } |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | REQUIRE(filter_count != 0, "OpAvgPool2d number of filters should be non-zero."); |
| 579 | out_tens(ob, oh, ow, oc) = acc / static_cast<OutEigenType>(filter_count); |
| 580 | } |
| 581 | } |
| 582 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
James Ward | 5a9e0cd | 2023-10-09 16:51:26 +0000 | [diff] [blame] | 585 | this->out->getTensor() = out_tens; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 586 | return GraphNode::eval(); |
| 587 | } |
| 588 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 589 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 590 | OpConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::OpConv2d(SubgraphTraverser* sgt_, |
| 591 | TosaAttributeBase* attribute_, |
| 592 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 593 | : GraphNode(sgt_, Op_CONV2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 594 | { |
| 595 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 596 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 597 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 598 | INIT_ATTRIBUTE(Conv); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 601 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 602 | OpConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::~OpConv2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 603 | { |
| 604 | if (attribute) |
| 605 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 608 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 609 | int OpConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 610 | { |
| 611 | if (validateRequiredOperands()) |
| 612 | return 1; |
| 613 | |
| 614 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 615 | { |
| 616 | return 1; |
| 617 | } |
| 618 | |
| 619 | // 'bias' checked separatedly since it doens't make sense to make required rank ranging from 1 to 4 |
| 620 | if (inputs[2]->getRank() != 1) |
| 621 | { |
| 622 | printNodeValidationError("OpConv2d: bias tensor must be rank 1"); |
| 623 | } |
| 624 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 625 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 626 | "OpConv2d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 627 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 628 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 629 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 630 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 631 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 632 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 633 | std::string msg; |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 634 | if (check_conv_attribute(attribute, 2 /* conv_dimension */, input->getShape(), output->getShape(), |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 635 | weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 636 | { |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 637 | msg = "OpConv2d: " + msg; |
| 638 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 639 | return 1; |
| 640 | } |
| 641 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 645 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 646 | int OpConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 647 | { |
| 648 | int in_batch = this->input->getShape()[0]; |
| 649 | int in_height = this->input->getShape()[1]; |
| 650 | int in_width = this->input->getShape()[2]; |
| 651 | int in_channels = this->input->getShape()[3]; |
| 652 | |
| 653 | int f_out_channels = this->weight->getShape()[0]; |
| 654 | int f_height = this->weight->getShape()[1]; |
| 655 | int f_width = this->weight->getShape()[2]; |
| 656 | int f_in_channels = this->weight->getShape()[3]; |
| 657 | |
| 658 | int b_out_channels = this->bias->getShape()[0]; |
| 659 | |
| 660 | int out_batch = this->output->getShape()[0]; |
| 661 | int out_height = this->output->getShape()[1]; |
| 662 | int out_width = this->output->getShape()[2]; |
| 663 | int out_channels = this->output->getShape()[3]; |
| 664 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 665 | ERROR_IF(in_batch != out_batch, "OpConv2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 666 | ERROR_IF(f_in_channels != in_channels, "OpConv2d: tensor input channel mismatch %d != %d", f_in_channels, |
| 667 | in_channels); |
| 668 | ERROR_IF(f_out_channels != out_channels, "OpConv2d: tensor output channel mismatch %d != %d", f_out_channels, |
| 669 | out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 670 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, "OpConv2d: bias channel mismatch %d != %d", |
| 671 | b_out_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 672 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 673 | int pad_top = this->attribute->pad()[0]; |
| 674 | int pad_bottom = this->attribute->pad()[1]; |
| 675 | int pad_left = this->attribute->pad()[2]; |
| 676 | int pad_right = this->attribute->pad()[3]; |
| 677 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 678 | int stride_y = this->attribute->stride()[0]; |
| 679 | int stride_x = this->attribute->stride()[1]; |
| 680 | int dilation_y = this->attribute->dilation()[0]; |
| 681 | int dilation_x = this->attribute->dilation()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 682 | |
| 683 | // Check Tosa Level |
| 684 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 685 | LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, |
| 686 | "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); |
| 687 | LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, |
| 688 | "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 689 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 690 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 691 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 692 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
| 693 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 694 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 695 | |
| 696 | DEBUG_INFO(OP, |
| 697 | "perform OpConv2d, input.shape=[%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d], output.shape=[%d,%d,%d,%d], " |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 698 | "stride=[%d,%d], dilation=[%d,%d], pad=[%d,%d,%d,%d]", |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 699 | in_batch, in_height, in_width, in_channels, f_height, f_width, f_in_channels, f_out_channels, out_batch, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 700 | out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, pad_bottom, |
| 701 | pad_left, pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 702 | |
| 703 | // GEMM-conv2d, left matrix is input, right matrix is weight |
| 704 | Eigen::array<Eigen::Index, 2> im2col_input_dims; |
| 705 | im2col_input_dims[0] = out_batch * out_height * out_width; |
| 706 | im2col_input_dims[1] = f_height * f_width * f_in_channels; |
| 707 | |
| 708 | Eigen::array<Eigen::Index, 2> im2col_weight_dims; |
| 709 | im2col_weight_dims[0] = f_height * f_width * f_in_channels; |
| 710 | im2col_weight_dims[1] = f_out_channels; |
| 711 | |
| 712 | Eigen::array<Eigen::Index, 2> bias_reshaped_dims; |
| 713 | bias_reshaped_dims[0] = 1; |
| 714 | bias_reshaped_dims[1] = b_out_channels; |
| 715 | |
| 716 | Eigen::array<Eigen::Index, 4> weight_zp_bcast_dims; |
| 717 | weight_zp_bcast_dims[0] = f_height; |
| 718 | weight_zp_bcast_dims[1] = f_width; |
| 719 | weight_zp_bcast_dims[2] = f_in_channels; |
| 720 | |
| 721 | Eigen::array<Eigen::Index, 2> bias_bcast_dims; |
| 722 | bias_bcast_dims[0] = out_batch * out_height * out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 723 | bias_bcast_dims[1] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 724 | |
| 725 | Eigen::array<Eigen::Index, 4> col2im_output_dims; |
| 726 | col2im_output_dims[0] = out_batch; |
| 727 | col2im_output_dims[1] = out_height; |
| 728 | col2im_output_dims[2] = out_width; |
| 729 | col2im_output_dims[3] = out_channels; |
| 730 | |
| 731 | Eigen::array<Eigen::IndexPair<Eigen::Index>, 1> contract_dims = { Eigen::IndexPair<Eigen::Index>(1, 0) }; |
| 732 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 733 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 734 | pad[0] = std::make_pair(0, 0); |
| 735 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 736 | pad[2] = std::make_pair(pad_left, pad_right); |
| 737 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 738 | |
| 739 | TIn input_val = this->input->getTensor(); |
| 740 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 741 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 742 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 743 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 744 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 745 | } |
| 746 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 747 | ETensor4<InEigenType> input_padded = input_val.pad(pad); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 748 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 749 | TBias bias_val = this->bias->getTensor(); |
| 750 | |
| 751 | if (g_func_config.abs_mode) |
| 752 | { |
| 753 | // in abs_mode: take abs values of conv operands |
| 754 | input_padded = input_padded.abs(); |
| 755 | weight_val = weight_val.abs(); |
| 756 | bias_val = bias_val.abs(); |
| 757 | } |
| 758 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 759 | // extract_image_patches() output [N, KH, KW, H * W, C] |
| 760 | // need to transpose to [N, H * W, KH, KW, C] |
| 761 | ETensor5<InEigenType> input_extract_patches = |
| 762 | input_padded |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 763 | .extract_image_patches(f_height, f_width, stride_y, stride_x, dilation_y, dilation_x, Eigen::PADDING_VALID) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 764 | .shuffle(Eigen::array<Eigen::Index, 5>{ 0, 3, 1, 2, 4 }); |
| 765 | |
| 766 | // reshape input to [N * H * W, KH * KW * C] |
| 767 | ETensor2<InEigenType> im2col_input = input_extract_patches.reshape(im2col_input_dims); |
| 768 | |
| 769 | // transpose and reshape weight from [OC, H, W, IC] to [H * W * IC, OC] |
| 770 | ETensor2<WeightEigenType> im2col_weight = |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 771 | weight_val.shuffle(Eigen::array<Eigen::Index, 4>({ 1, 2, 3, 0 })).reshape(im2col_weight_dims); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 772 | |
| 773 | // don't need to apply bias_multiplier ( * bias_scale and >> bias_shift) since tflite already scale it |
| 774 | // and reshaped from [C] to [1, C], and broadcast to [N * H * W, C] |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 775 | ETensor2<OutEigenType> bias_2d = |
| 776 | (bias_val.reshape(bias_reshaped_dims).broadcast(bias_bcast_dims)).template cast<OutEigenType>(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 777 | |
| 778 | // output matrix is [N * H * W, C] |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 779 | ETensor2<OutEigenType> contracted_result = (im2col_input.template cast<AccEigenType>().contract( |
| 780 | im2col_weight.template cast<AccEigenType>(), contract_dims)) |
| 781 | .template cast<OutEigenType>(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 782 | |
| 783 | // adding bias |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 784 | ETensor2<OutEigenType> biased_output = contracted_result + bias_2d; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 785 | |
| 786 | // reshape back to [N, H, W, C] |
| 787 | this->output->getTensor() = biased_output.reshape(col2im_output_dims); |
| 788 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 789 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 790 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 791 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 792 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return GraphNode::eval(); |
| 796 | } |
| 797 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 798 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 799 | OpConv3d<InDtype, WeightDtype, AccDtype, OutDtype>::OpConv3d(SubgraphTraverser* sgt_, |
| 800 | TosaAttributeBase* attribute_, |
| 801 | uint64_t id_) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 802 | : GraphNode(sgt_, Op_CONV3D, id_) |
| 803 | { |
| 804 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 805 | setRequiredRank(5, 5); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 806 | |
| 807 | INIT_ATTRIBUTE(Conv); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 810 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 811 | OpConv3d<InDtype, WeightDtype, AccDtype, OutDtype>::~OpConv3d() |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 812 | { |
| 813 | if (attribute) |
| 814 | delete attribute; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 817 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 818 | int OpConv3d<InDtype, WeightDtype, AccDtype, OutDtype>::checkTensorAttributes() |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 819 | { |
| 820 | if (validateRequiredOperands()) |
| 821 | return 1; |
| 822 | |
| 823 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 824 | { |
| 825 | return 1; |
| 826 | } |
| 827 | |
| 828 | // 'bias' checked separatedly since it doens't make sense to make required rank ranging from 1 to 4 |
| 829 | if (inputs[2]->getRank() != 1) |
| 830 | { |
| 831 | printNodeValidationError("OpConv3d: bias tensor must be rank 1"); |
| 832 | } |
| 833 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 834 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 835 | "OpConv3d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 836 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 837 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 838 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 839 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 840 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 841 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 842 | std::string msg; |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 843 | if (check_conv_attribute(attribute, 3 /* conv_dimension */, input->getShape(), output->getShape(), |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 844 | weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 845 | { |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 846 | msg = "OpConv3d: " + msg; |
| 847 | printNodeValidationError(msg.c_str()); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 848 | return 1; |
| 849 | } |
| 850 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 854 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 855 | int OpConv3d<InDtype, WeightDtype, AccDtype, OutDtype>::eval() |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 856 | { |
| 857 | int in_batch = this->input->getShape()[0]; |
| 858 | int in_depth = this->input->getShape()[1]; |
| 859 | int in_height = this->input->getShape()[2]; |
| 860 | int in_width = this->input->getShape()[3]; |
| 861 | int in_channels = this->input->getShape()[4]; |
| 862 | |
| 863 | int f_out_channels = this->weight->getShape()[0]; |
| 864 | int f_depth = this->weight->getShape()[1]; |
| 865 | int f_height = this->weight->getShape()[2]; |
| 866 | int f_width = this->weight->getShape()[3]; |
| 867 | int f_in_channels = this->weight->getShape()[4]; |
| 868 | |
| 869 | int b_out_channels = this->bias->getShape()[0]; |
| 870 | |
| 871 | int out_batch = this->output->getShape()[0]; |
| 872 | int out_depth = this->output->getShape()[1]; |
| 873 | int out_height = this->output->getShape()[2]; |
| 874 | int out_width = this->output->getShape()[3]; |
| 875 | int out_channels = this->output->getShape()[4]; |
| 876 | |
| 877 | ERROR_IF(in_batch != out_batch, "OpConv3d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 878 | ERROR_IF(f_in_channels != in_channels, "OpConv3d: tensor input channel mismatch %d != %d", f_in_channels, |
| 879 | in_channels); |
| 880 | ERROR_IF(f_out_channels != out_channels, "OpConv3d: tensor output channel mismatch %d != %d", f_out_channels, |
| 881 | out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 882 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, "OpConv3d: bias channel mismatch %d != %d", |
| 883 | b_out_channels, out_channels); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 884 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 885 | int pad_d0 = this->attribute->pad()[0]; |
| 886 | int pad_d1 = this->attribute->pad()[1]; |
| 887 | int pad_top = this->attribute->pad()[2]; |
| 888 | int pad_bottom = this->attribute->pad()[3]; |
| 889 | int pad_left = this->attribute->pad()[4]; |
| 890 | int pad_right = this->attribute->pad()[5]; |
| 891 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 892 | int stride_d = this->attribute->stride()[0]; |
| 893 | int stride_y = this->attribute->stride()[1]; |
| 894 | int stride_x = this->attribute->stride()[2]; |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 895 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 896 | int dilation_d = this->attribute->dilation()[0]; |
| 897 | int dilation_y = this->attribute->dilation()[1]; |
| 898 | int dilation_x = this->attribute->dilation()[2]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 899 | |
| 900 | // Check Tosa Level |
| 901 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 902 | LEVEL_CHECK(dilation_d * f_depth <= tosa_level.MAX_KERNEL, |
| 903 | "dilation_d * KD should be smaller than or equal to MAX_KERNEL"); |
| 904 | LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, |
| 905 | "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); |
| 906 | LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, |
| 907 | "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 908 | LEVEL_CHECK(pad_d0 <= tosa_level.MAX_KERNEL, "pad_d0 should be smaller than or equal to MAX_KERNEL"); |
| 909 | LEVEL_CHECK(pad_d1 <= tosa_level.MAX_KERNEL, "pad_d1 should be smaller than or equal to MAX_KERNEL"); |
| 910 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 911 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 912 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 913 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
| 914 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 915 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 916 | LEVEL_CHECK(stride_d <= tosa_level.MAX_STRIDE, "stride_d should be smaller than or equal to MAX_STRIDE"); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 917 | |
| 918 | DEBUG_INFO( |
| 919 | OP, |
| 920 | "perform OpConv3d, input.shape=[%d,%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d,%d], output.shape=[%d,%d,%d,%d,%d], " |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 921 | "stride=[%d,%d,%d], dilation=[%d,%d,%d], pad=[%d,%d,%d,%d,%d,%d]", |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 922 | in_batch, in_depth, in_height, in_width, in_channels, f_out_channels, f_depth, f_height, f_width, f_in_channels, |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 923 | out_batch, out_depth, out_height, out_width, out_channels, stride_d, stride_y, stride_x, dilation_d, dilation_y, |
| 924 | dilation_x, pad_d0, pad_d1, pad_top, pad_bottom, pad_left, pad_right); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 925 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 926 | Eigen::array<std::pair<int32_t, int32_t>, 5> pad; |
| 927 | pad[0] = std::make_pair(0, 0); |
| 928 | pad[1] = std::make_pair(pad_d0, pad_d1); |
| 929 | pad[2] = std::make_pair(pad_top, pad_bottom); |
| 930 | pad[3] = std::make_pair(pad_left, pad_right); |
| 931 | pad[4] = std::make_pair(0, 0); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 932 | |
| 933 | TIn input_val = this->input->getTensor(); |
| 934 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 935 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 936 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 937 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 938 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 939 | } |
| 940 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 941 | ETensor5<InEigenType> input_padded = input_val.pad(pad); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 942 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 943 | TBias bias_val = this->bias->getTensor(); |
| 944 | |
| 945 | if (g_func_config.abs_mode) |
| 946 | { |
| 947 | // in abs_mode: take abs values of conv operands |
| 948 | input_padded = input_padded.abs(); |
| 949 | weight_val = weight_val.abs(); |
| 950 | bias_val = bias_val.abs(); |
| 951 | } |
| 952 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 953 | // 1. initialize with bias |
| 954 | Eigen::array<Eigen::Index, 5> reshape_dim; |
| 955 | reshape_dim.fill(1); |
| 956 | reshape_dim[4] = b_out_channels; |
| 957 | |
| 958 | Eigen::array<Eigen::Index, 5> bcast; |
| 959 | bcast[0] = out_batch; |
| 960 | bcast[1] = out_depth; |
| 961 | bcast[2] = out_height; |
| 962 | bcast[3] = out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 963 | bcast[4] = (b_out_channels == 1) ? out_channels : 1; |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 964 | this->output->getTensor() = bias_val.reshape(reshape_dim).broadcast(bcast); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 965 | |
| 966 | // 2. direct convolution |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 967 | AccEigenType acc(0.0); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 968 | int d_idx, h_idx, w_idx; |
| 969 | |
| 970 | for (int ob = 0; ob < out_batch; ob++) |
| 971 | { |
| 972 | for (int od = 0; od < out_depth; od++) |
| 973 | { |
| 974 | for (int oh = 0; oh < out_height; oh++) |
| 975 | { |
| 976 | for (int ow = 0; ow < out_width; ow++) |
| 977 | { |
| 978 | for (int oc = 0; oc < out_channels; oc++) |
| 979 | { |
Eric Kunze | 7edb34c | 2022-05-16 17:34:40 -0700 | [diff] [blame] | 980 | // Initialize accumulator with bias value |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 981 | acc = (AccEigenType)this->output->getTensor()(ob, od, oh, ow, oc); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 982 | for (int fd = 0; fd < f_depth; fd++) |
| 983 | { |
| 984 | d_idx = od * stride_d + fd * dilation_d; |
| 985 | for (int fh = 0; fh < f_height; fh++) |
| 986 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 987 | h_idx = oh * stride_y + fh * dilation_y; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 988 | for (int fw = 0; fw < f_width; fw++) |
| 989 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 990 | w_idx = ow * stride_x + fw * dilation_x; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 991 | for (int ic = 0; ic < in_channels; ic++) |
| 992 | { |
| 993 | acc += ((AccEigenType)input_padded(ob, d_idx, h_idx, w_idx, ic) * |
| 994 | (AccEigenType)weight_val(oc, fd, fh, fw, ic)); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 999 | this->output->getTensor()(ob, od, oh, ow, oc) = (OutEigenType)acc; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1006 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1007 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1008 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1009 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | return GraphNode::eval(); |
| 1013 | } |
| 1014 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1015 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1016 | OpDepthwiseConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::OpDepthwiseConv2d(SubgraphTraverser* sgt_, |
| 1017 | TosaAttributeBase* attribute_, |
| 1018 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1019 | : GraphNode(sgt_, Op_DEPTHWISE_CONV2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1020 | { |
| 1021 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1022 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1023 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1024 | INIT_ATTRIBUTE(Conv); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1027 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1028 | OpDepthwiseConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::~OpDepthwiseConv2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1029 | { |
| 1030 | if (attribute) |
| 1031 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1034 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1035 | int OpDepthwiseConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1036 | { |
| 1037 | if (validateRequiredOperands()) |
| 1038 | return 1; |
| 1039 | |
| 1040 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1041 | { |
| 1042 | return 1; |
| 1043 | } |
| 1044 | |
| 1045 | // 'bias' checked separatedly since it doens't make sense to make required rank ranging from 1 to 4 |
| 1046 | if (inputs[2]->getRank() != 1) |
| 1047 | { |
| 1048 | printNodeValidationError("OpDepthwiseConv2d: bias tensor must be rank 1"); |
| 1049 | } |
| 1050 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1051 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1052 | "OpDepthwiseConv2d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1053 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1054 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1055 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 1056 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1057 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1058 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1059 | std::string msg; |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1060 | if (check_conv_attribute(attribute, 2 /* conv_dimension */, input->getShape(), output->getShape(), |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1061 | weight->getShape(), 0 /* offset_kernel */, InDtype, WeightDtype, msg)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1062 | { |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1063 | msg = "OpDepthwiseConv2d: " + msg; |
| 1064 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1065 | return 1; |
| 1066 | } |
| 1067 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1071 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1072 | int OpDepthwiseConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1073 | { |
| 1074 | int in_batch = this->input->getShape()[0]; |
| 1075 | int in_height = this->input->getShape()[1]; |
| 1076 | int in_width = this->input->getShape()[2]; |
| 1077 | int in_channels = this->input->getShape()[3]; |
| 1078 | |
| 1079 | int f_height = this->weight->getShape()[0]; |
| 1080 | int f_width = this->weight->getShape()[1]; |
| 1081 | int f_in_channels = this->weight->getShape()[2]; |
| 1082 | int f_multiplier = this->weight->getShape()[3]; |
| 1083 | |
| 1084 | int b_out_channels = this->bias->getShape()[0]; |
| 1085 | |
| 1086 | int out_batch = this->output->getShape()[0]; |
| 1087 | int out_height = this->output->getShape()[1]; |
| 1088 | int out_width = this->output->getShape()[2]; |
| 1089 | int out_channels = this->output->getShape()[3]; |
| 1090 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1091 | ERROR_IF(in_batch != out_batch, "OpDepthwiseConv2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 1092 | ERROR_IF(f_in_channels != in_channels, "OpDepthwiseConv2d: tensor input channel mismatch %d != %d", f_in_channels, |
| 1093 | in_channels); |
| 1094 | ERROR_IF(in_channels * f_multiplier != out_channels, "OpDepthwiseConv2d: tensor output channel mismatch %d != %d", |
| 1095 | in_channels * f_multiplier, out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1096 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, |
| 1097 | "OpDepthwiseConv2d: bias channels mismatch %d != %d", b_out_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1098 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1099 | int pad_top = this->attribute->pad()[0]; |
| 1100 | int pad_bottom = this->attribute->pad()[1]; |
| 1101 | int pad_left = this->attribute->pad()[2]; |
| 1102 | int pad_right = this->attribute->pad()[3]; |
| 1103 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1104 | int stride_y = this->attribute->stride()[0]; |
| 1105 | int stride_x = this->attribute->stride()[1]; |
| 1106 | int dilation_y = this->attribute->dilation()[0]; |
| 1107 | int dilation_x = this->attribute->dilation()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1108 | |
| 1109 | // Check Tosa Level |
| 1110 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1111 | LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, |
| 1112 | "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); |
| 1113 | LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, |
| 1114 | "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1115 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 1116 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 1117 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 1118 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
| 1119 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 1120 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1121 | |
| 1122 | DEBUG_INFO(OP, |
| 1123 | "perform OpDepthwiseConv2d, input.shape=[%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d], " |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1124 | "output.shape=[%d,%d,%d,%d], stride=[%d,%d], dilation=[%d,%d], pad=[%d,%d,%d,%d]", |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1125 | in_batch, in_height, in_width, in_channels, f_height, f_width, f_in_channels, f_multiplier, out_batch, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1126 | out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, pad_bottom, |
| 1127 | pad_left, pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1128 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1129 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 1130 | pad[0] = std::make_pair(0, 0); |
| 1131 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 1132 | pad[2] = std::make_pair(pad_left, pad_right); |
| 1133 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1134 | |
| 1135 | TIn input_val = this->input->getTensor(); |
| 1136 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1137 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1138 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1139 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 1140 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1143 | ETensor4<InEigenType> input_padded = input_val.pad(pad); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1144 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1145 | TBias bias_val = this->bias->getTensor(); |
| 1146 | |
| 1147 | if (g_func_config.abs_mode) |
| 1148 | { |
| 1149 | // in abs_mode: take abs values of conv operands |
| 1150 | input_padded = input_padded.abs(); |
| 1151 | weight_val = weight_val.abs(); |
| 1152 | bias_val = bias_val.abs(); |
| 1153 | } |
| 1154 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1155 | // GEMM doesn't fit well with DepthwiseConv2d |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1156 | // 1. use extract_image_patches() to handle stride/dilation/pad |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1157 | // 2. perform direct convolution |
| 1158 | |
| 1159 | // 1. extract_image_patches() output [N, KH, KW, OH * OW, IC] |
| 1160 | ETensor5<InEigenType> input_extract_patches = input_padded.extract_image_patches( |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1161 | f_height, f_width, stride_y, stride_x, dilation_y, dilation_x, Eigen::PADDING_VALID); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1162 | |
| 1163 | Eigen::array<Eigen::Index, 4> reshape_dim; |
| 1164 | reshape_dim.fill(1); |
| 1165 | reshape_dim[3] = b_out_channels; |
| 1166 | |
| 1167 | Eigen::array<Eigen::Index, 4> bcast; |
| 1168 | bcast[0] = out_batch; |
| 1169 | bcast[1] = out_height; |
| 1170 | bcast[2] = out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1171 | bcast[3] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1172 | |
| 1173 | // initialize with bias |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1174 | this->output->getTensor() = bias_val.reshape(reshape_dim).broadcast(bcast); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1175 | |
| 1176 | // 2. direct depthwise convolution |
| 1177 | for (int ob = 0; ob < out_batch; ob++) |
| 1178 | { |
| 1179 | for (int oh = 0; oh < out_height; oh++) |
| 1180 | { |
| 1181 | for (int ow = 0; ow < out_width; ow++) |
| 1182 | { |
| 1183 | for (int ic = 0; ic < in_channels; ic++) |
| 1184 | { |
| 1185 | for (int cm = 0; cm < f_multiplier; cm++) |
| 1186 | { |
| 1187 | for (int fh = 0; fh < f_height; fh++) |
| 1188 | { |
| 1189 | for (int fw = 0; fw < f_width; fw++) |
| 1190 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1191 | // Perform multiplication in AccEigenType then cast to OutEigenType |
Eric Kunze | be2e87c | 2023-08-07 15:16:18 +0000 | [diff] [blame] | 1192 | this->output->getTensor()(ob, oh, ow, ic * f_multiplier + cm) += |
| 1193 | (OutEigenType)((AccEigenType)input_extract_patches(ob, fh, fw, ow * out_height + oh, |
| 1194 | ic) * |
| 1195 | (AccEigenType)weight_val(fh, fw, ic, cm)); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1204 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1205 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1206 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1207 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | return GraphNode::eval(); |
| 1211 | } |
| 1212 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1213 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1214 | OpFullyConnected<InDtype, WeightDtype, OutDtype>::OpFullyConnected(SubgraphTraverser* sgt_, |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1215 | TosaAttributeBase* attribute_, |
| 1216 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1217 | : GraphNode(sgt_, Op_FULLY_CONNECTED, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1218 | { |
| 1219 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1220 | setRequiredRank(2, 2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1221 | |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1222 | INIT_ATTRIBUTE(FullyConnected); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1225 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1226 | OpFullyConnected<InDtype, WeightDtype, OutDtype>::~OpFullyConnected() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1227 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1228 | if (attribute) |
| 1229 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1232 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1233 | int OpFullyConnected<InDtype, WeightDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1234 | { |
| 1235 | if (validateRequiredOperands()) |
| 1236 | return 1; |
| 1237 | |
| 1238 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1239 | { |
| 1240 | return 1; |
| 1241 | } |
| 1242 | |
| 1243 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1244 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 1245 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
| 1246 | |
| 1247 | if (input->getShape()[1] != weight->getShape()[1]) |
| 1248 | { |
| 1249 | printNodeValidationError("OpFullyConnected operator input.shape[1] should match weight.shape[1]"); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
Jack Frankland | ac40bd1 | 2023-11-21 17:08:37 +0000 | [diff] [blame] | 1253 | if (weight->getShape()[0] != bias->getShape()[0] && bias->getShape()[0] != 1) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1254 | { |
Jack Frankland | ac40bd1 | 2023-11-21 17:08:37 +0000 | [diff] [blame] | 1255 | printNodeValidationError( |
| 1256 | "OpFullyConnected operator bias.shape[0] should match weight.shape[0] or be equal to 1"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1257 | return 1; |
| 1258 | } |
| 1259 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1260 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1261 | "OpFullyConnected: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1262 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1263 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1264 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1265 | ERROR_IF(InDtype != TOSA_REF_TYPE_INT8 && attribute->input_zp() != 0, |
| 1266 | "OpFullyConnected: Input zeropoint must be zero for non int8_t data"); |
| 1267 | ERROR_IF(WeightDtype != TOSA_REF_TYPE_INT8 && attribute->weight_zp() != 0, |
| 1268 | "OpFullyConnected: Weight zeropoint must be zero for non int8_t data"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1269 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1273 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1274 | int OpFullyConnected<InDtype, WeightDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1275 | { |
| 1276 | typedef Eigen::Tensor<int, 1>::DimensionPair DimPair; |
| 1277 | Eigen::array<DimPair, 1> dims{ { DimPair(1, 0) } }; |
| 1278 | |
| 1279 | Eigen::array<Eigen::Index, 2> weight_shuffle{ 1, 0 }; |
| 1280 | |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1281 | int b_out_channels = this->bias->getShape()[0]; |
| 1282 | int out_channels = this->output->getShape()[1]; |
| 1283 | |
| 1284 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, "OpFullyConnected: bias channels mismatch %d != %d", |
| 1285 | b_out_channels, out_channels); |
| 1286 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1287 | Eigen::array<Eigen::Index, 2> bias_reshape; |
| 1288 | bias_reshape[0] = 1; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1289 | bias_reshape[1] = b_out_channels; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1290 | |
| 1291 | Eigen::array<Eigen::Index, 2> bias_bcast; |
| 1292 | bias_bcast[0] = this->input->getShape()[0]; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1293 | bias_bcast[1] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1294 | |
| 1295 | TIn input_val = this->input->getTensor(); |
| 1296 | TWeight weight_val = this->weight->getTensor().shuffle(weight_shuffle); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1297 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1298 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1299 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 1300 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1303 | TBias bias_val = this->bias->getTensor(); |
| 1304 | |
| 1305 | if (g_func_config.abs_mode) |
| 1306 | { |
| 1307 | // in abs_mode: take abs values of conv operands |
| 1308 | input_val = input_val.abs(); |
| 1309 | weight_val = weight_val.abs(); |
| 1310 | bias_val = bias_val.abs(); |
| 1311 | } |
| 1312 | |
| 1313 | this->output->getTensor() = input_val.template cast<AccEigenType>() |
| 1314 | .contract(weight_val.template cast<AccEigenType>(), dims) |
| 1315 | .template cast<OutEigenType>() + |
| 1316 | bias_val.reshape(bias_reshape).broadcast(bias_bcast); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1317 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1318 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1319 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1320 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1321 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1322 | } |
| 1323 | return GraphNode::eval(); |
| 1324 | } |
| 1325 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1326 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1327 | OpMatMul<Dtype, OutDtype>::OpMatMul(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1328 | : GraphNode(sgt_, Op_MATMUL, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1329 | { |
| 1330 | setRequiredOperands(2, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1331 | setRequiredRank(3, 3); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1332 | |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1333 | INIT_ATTRIBUTE(MatMul); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1336 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1337 | OpMatMul<Dtype, OutDtype>::~OpMatMul() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1338 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1339 | if (attribute) |
| 1340 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1343 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1344 | int OpMatMul<Dtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1345 | { |
| 1346 | if (validateRequiredOperands()) |
| 1347 | return 1; |
| 1348 | |
| 1349 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1350 | { |
| 1351 | return 1; |
| 1352 | } |
| 1353 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1354 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1355 | "OpMatMul: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1356 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1357 | a = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1358 | b = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1359 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1360 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1361 | ASSERT_MEM(a && b && output); |
| 1362 | |
| 1363 | // a: [N, H, C] |
| 1364 | // b: [N, C, W] |
| 1365 | // c: [N, H, W] |
| 1366 | |
| 1367 | // Check N |
| 1368 | if (a->getShape()[0] != b->getShape()[0] || a->getShape()[0] != output->getShape()[0]) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1369 | { |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1370 | printNodeValidationError("OpMatMul operator a.shape[0], b.shape[0] and output.shape[0] should match"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1371 | return 1; |
| 1372 | } |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1373 | N = a->getShape()[0]; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1374 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1375 | // Check C |
| 1376 | if (a->getShape()[2] != b->getShape()[1]) |
| 1377 | { |
| 1378 | printNodeValidationError("OpMatMul operator a.shape[2] should match b.shape[1]"); |
| 1379 | return 1; |
| 1380 | } |
| 1381 | C = a->getShape()[2]; |
| 1382 | |
| 1383 | // Check H |
| 1384 | if (a->getShape()[1] != output->getShape()[1]) |
| 1385 | { |
| 1386 | printNodeValidationError("OpMatMul operator a.shape[1] should match output.shape[1]"); |
| 1387 | return 1; |
| 1388 | } |
| 1389 | H = a->getShape()[1]; |
| 1390 | |
| 1391 | // Check W |
| 1392 | if (b->getShape()[2] != output->getShape()[2]) |
| 1393 | { |
| 1394 | printNodeValidationError("OpMatMul operator output.shape[2] should match output.shape[2]"); |
| 1395 | return 1; |
| 1396 | } |
| 1397 | W = b->getShape()[2]; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1398 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1399 | ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->a_zp() != 0, |
| 1400 | "OpMatMul: A zeropoint must be zero for non int8_t data"); |
| 1401 | ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->b_zp() != 0, |
| 1402 | "OpMatMul: B zeropoint must be zero for non int8_t data"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1403 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1404 | return 0; |
| 1405 | } |
| 1406 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1407 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1408 | int OpMatMul<Dtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1409 | { |
| 1410 | typedef Eigen::Tensor<int, 1>::DimensionPair DimPair; |
| 1411 | Eigen::array<DimPair, 1> dims{ { DimPair(1, 0) } }; |
| 1412 | |
| 1413 | TIn a_val = this->a->getTensor(); |
| 1414 | TIn b_val = this->b->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1415 | if (Dtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1416 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1417 | a_val = a_val - (InEigenType)attribute->a_zp(); |
| 1418 | b_val = b_val - (InEigenType)attribute->b_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1421 | if (g_func_config.abs_mode) |
| 1422 | { |
| 1423 | // in abs_mode: take abs values of matmul operands |
| 1424 | a_val = a_val.abs(); |
| 1425 | b_val = b_val.abs(); |
| 1426 | } |
| 1427 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1428 | Eigen::array<Eigen::Index, 2> a_rank2_shape({ H, C }); |
| 1429 | Eigen::array<Eigen::Index, 2> b_rank2_shape({ C, W }); |
| 1430 | Eigen::array<Eigen::Index, 3> output_rank3_shape({ 1, H, W }); |
| 1431 | |
| 1432 | Eigen::array<Eigen::Index, 3> a_size_array({ 1, H, C }); |
| 1433 | Eigen::array<Eigen::Index, 3> b_size_array({ 1, C, W }); |
| 1434 | |
| 1435 | Eigen::array<Eigen::Index, 3> a_begin_array({ 0, 0, 0 }); |
| 1436 | Eigen::array<Eigen::Index, 3> b_begin_array({ 0, 0, 0 }); |
| 1437 | |
| 1438 | // Iterate N dimension. |
| 1439 | for (int i = 0; i < N; i++) |
| 1440 | { |
| 1441 | a_begin_array[0] = i; |
| 1442 | b_begin_array[0] = i; |
| 1443 | |
| 1444 | TInRank2 a_rank2_val = a_val.slice(a_begin_array, a_size_array).reshape(a_rank2_shape); |
| 1445 | TInRank2 b_rank2_val = b_val.slice(b_begin_array, b_size_array).reshape(b_rank2_shape); |
| 1446 | TAccRank2 output_rank2_val = |
| 1447 | a_rank2_val.template cast<AccEigenType>().contract(b_rank2_val.template cast<AccEigenType>(), dims); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1448 | TOut output_rank3_val = output_rank2_val.reshape(output_rank3_shape).template cast<OutEigenType>(); |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1449 | if (i == 0) |
| 1450 | { |
| 1451 | this->output->getTensor() = output_rank3_val; |
| 1452 | } |
| 1453 | else |
| 1454 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1455 | TOut temp = this->output->getTensor().concatenate(output_rank3_val, 0); |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1456 | this->output->getTensor() = temp; |
| 1457 | } |
| 1458 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1459 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1460 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1461 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1462 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1463 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | return GraphNode::eval(); |
| 1467 | } |
| 1468 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1469 | template <TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1470 | OpMaxPool2d<Dtype>::OpMaxPool2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1471 | : GraphNode(sgt_, Op_MAX_POOL2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1472 | { |
| 1473 | setRequiredOperands(1, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1474 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1475 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1476 | INIT_ATTRIBUTE(Pool); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1479 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1480 | OpMaxPool2d<Dtype>::~OpMaxPool2d() |
| 1481 | { |
| 1482 | if (attribute) |
| 1483 | delete attribute; |
| 1484 | } |
| 1485 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1486 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1487 | int OpMaxPool2d<Dtype>::checkTensorAttributes() |
| 1488 | { |
| 1489 | if (validateRequiredOperands()) |
| 1490 | return 1; |
| 1491 | |
| 1492 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) |
| 1493 | { |
| 1494 | return 1; |
| 1495 | } |
| 1496 | |
| 1497 | if (inputs[0]->matchType(*outputs[0])) |
| 1498 | { |
| 1499 | printNodeValidationError("OpMaxPool2d: input and output tensor type mismatch"); |
| 1500 | return 1; |
| 1501 | } |
| 1502 | |
| 1503 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1504 | out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 1505 | |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 1506 | std::string msg; |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1507 | if (check_pool2d_attribute(attribute, in->getShape(), out->getShape(), msg)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1508 | { |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 1509 | msg = "OpMaxPool2d: " + msg; |
| 1510 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1511 | return 1; |
| 1512 | } |
| 1513 | |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1517 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1518 | int OpMaxPool2d<Dtype>::eval() |
| 1519 | { |
| 1520 | int in_batch = this->in->getShape()[0]; |
| 1521 | int in_height = this->in->getShape()[1]; |
| 1522 | int in_width = this->in->getShape()[2]; |
| 1523 | int in_channels = this->in->getShape()[3]; |
| 1524 | |
| 1525 | int out_batch = this->out->getShape()[0]; |
| 1526 | int out_height = this->out->getShape()[1]; |
| 1527 | int out_width = this->out->getShape()[2]; |
| 1528 | int out_channels = this->out->getShape()[3]; |
| 1529 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1530 | ERROR_IF(in_batch != out_batch, "OpMaxPool2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 1531 | ERROR_IF(in_channels != out_channels, "OpMaxPool2d: tensor channel mismatch %d != %d", in_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1532 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1533 | int pad_top = this->attribute->pad()[0]; |
| 1534 | int pad_bottom = this->attribute->pad()[1]; |
| 1535 | int pad_left = this->attribute->pad()[2]; |
| 1536 | int pad_right = this->attribute->pad()[3]; |
| 1537 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1538 | int kernel_y = this->attribute->kernel()[0]; |
| 1539 | int kernel_x = this->attribute->kernel()[1]; |
| 1540 | int stride_y = this->attribute->stride()[0]; |
| 1541 | int stride_x = this->attribute->stride()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Check Tosa Level |
| 1544 | auto tosa_level = g_func_config.tosa_level; |
| 1545 | LEVEL_CHECK(kernel_y <= tosa_level.MAX_KERNEL, "kernel_y should be smaller than or equal to MAX_KERNEL"); |
| 1546 | LEVEL_CHECK(kernel_x <= tosa_level.MAX_KERNEL, "kernel_x should be smaller than or equal to MAX_KERNEL"); |
| 1547 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 1548 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 1549 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 1550 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 1551 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 1552 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1553 | |
| 1554 | DEBUG_INFO(OP, |
| 1555 | "perform MaxPool2d, input.shape=[%d,%d,%d,%d], output.shape=[%d,%d,%d,%d], kernel=[%d,%d], " |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1556 | "stride=[%d,%d], pad=[%d,%d,%d,%d]", |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1557 | in_batch, in_height, in_width, in_channels, out_batch, out_height, out_width, out_channels, kernel_y, |
| 1558 | kernel_x, stride_y, stride_x, pad_top, pad_bottom, pad_left, pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1559 | |
| 1560 | Eigen::array<Eigen::Index, 2> im2col_input_dims; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1561 | im2col_input_dims[0] = kernel_y * kernel_x; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1562 | im2col_input_dims[1] = out_batch * out_height * out_width * out_channels; |
| 1563 | |
| 1564 | Eigen::array<Eigen::Index, 4> col2im_output_dims; |
| 1565 | col2im_output_dims[0] = out_batch; |
| 1566 | col2im_output_dims[1] = out_height; |
| 1567 | col2im_output_dims[2] = out_width; |
| 1568 | col2im_output_dims[3] = out_channels; |
| 1569 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1570 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 1571 | pad[0] = std::make_pair(0, 0); |
| 1572 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 1573 | pad[2] = std::make_pair(pad_left, pad_right); |
| 1574 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1575 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1576 | ETensor4<InEigenType> input_padded = this->in->getTensor().pad(pad, std::numeric_limits<InEigenType>::lowest()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1577 | |
| 1578 | // extract_image_patches() output [N, KH, KW, H * W, C] |
| 1579 | // transpose to [KH, KW, N, H * W, C] |
| 1580 | // reshape to [KH * KW, N * H * W * C] |
| 1581 | // |
| 1582 | // Set the padding value to be the most negative value that can be |
| 1583 | // represented by the datatype to ensure that any padding values will be equal |
| 1584 | // to or smaller than the actual maximum in the KH x KW patch. |
| 1585 | ETensor2<InEigenType> input_extract_patches = |
| 1586 | input_padded |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1587 | .extract_image_patches(kernel_y, kernel_x, stride_y, stride_x, 1, 1, Eigen::PADDING_VALID, |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1588 | std::numeric_limits<InEigenType>::lowest()) |
| 1589 | .shuffle(Eigen::array<Eigen::Index, 5>{ 1, 2, 0, 3, 4 }) |
| 1590 | .reshape(im2col_input_dims); |
| 1591 | |
| 1592 | // Get the maximum of the KHxHW patches along axis 0 |
| 1593 | Eigen::Tensor<DenseIndex, 1> tensor_argmax = input_extract_patches.argmax(0); |
| 1594 | |
| 1595 | // 1D result with [N * H * W * C] |
| 1596 | ETensor1<OutEigenType> out_1d(this->out->getElementCount()); |
| 1597 | |
| 1598 | // index input_patches with argmax array should give the result |
| 1599 | for (size_t i = 0; i < this->out->getElementCount(); i++) |
| 1600 | { |
| 1601 | out_1d(i) = (OutEigenType)input_extract_patches(tensor_argmax(i), i); |
| 1602 | } |
| 1603 | |
| 1604 | // reshape result to [N, H, W, C] |
| 1605 | this->out->getTensor() = out_1d.reshape(col2im_output_dims); |
| 1606 | |
| 1607 | return GraphNode::eval(); |
| 1608 | } |
| 1609 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1610 | template <TOSA_REF_TYPE Dtype> |
| 1611 | OpFFT2d<Dtype>::OpFFT2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1612 | : GraphNode(sgt_, Op_FFT2D, id_) |
| 1613 | { |
| 1614 | setRequiredOperands(2, 2); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1615 | setRequiredRank(3, 3); |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1616 | |
| 1617 | INIT_ATTRIBUTE(FFT); |
| 1618 | } |
| 1619 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1620 | template <TOSA_REF_TYPE Dtype> |
| 1621 | OpFFT2d<Dtype>::~OpFFT2d() |
| 1622 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1623 | if (attribute) |
| 1624 | delete attribute; |
| 1625 | } |
| 1626 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1627 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1628 | int OpFFT2d<Dtype>::checkTensorAttributes() |
| 1629 | { |
| 1630 | if (validateRequiredOperands()) |
| 1631 | return 1; |
| 1632 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1633 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0]) || |
| 1634 | validateRequiredRank(outputs[1])) |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1635 | { |
| 1636 | return 1; |
| 1637 | } |
| 1638 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1639 | if (inputs[0]->matchType(*outputs[0]) || inputs[1]->matchType(*outputs[1]) || inputs[0]->matchType(*inputs[1])) |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1640 | { |
| 1641 | printNodeValidationError("OpFFT2d: input and output tensor type mismatch"); |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
| 1645 | in_real = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1646 | in_imag = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]); |
| 1647 | out_real = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 1648 | out_imag = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[1]); |
| 1649 | |
| 1650 | ASSERT_MEM(in_real && in_imag && out_real && out_imag); |
| 1651 | |
| 1652 | std::string msg; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1653 | if (check_fft_shape(in_real->getShape(), in_imag->getShape(), out_real->getShape(), out_imag->getShape(), msg)) |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1654 | { |
| 1655 | msg = "OpFFT2d: " + msg; |
| 1656 | printNodeValidationError(msg.c_str()); |
| 1657 | return 1; |
| 1658 | } |
| 1659 | |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1663 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1664 | int OpFFT2d<Dtype>::eval() |
| 1665 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1666 | int in_real_batch = this->in_real->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1667 | int in_real_height = this->in_real->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1668 | int in_real_width = this->in_real->getShape()[2]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1669 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1670 | int in_imag_batch = this->in_imag->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1671 | int in_imag_height = this->in_imag->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1672 | int in_imag_width = this->in_imag->getShape()[2]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1673 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1674 | int out_real_batch = this->out_real->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1675 | int out_real_height = this->out_real->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1676 | int out_real_width = this->out_real->getShape()[2]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1677 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1678 | int out_imag_batch = this->out_imag->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1679 | int out_imag_height = this->out_imag->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1680 | int out_imag_width = this->out_imag->getShape()[2]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1681 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1682 | // Check Tosa Level |
| 1683 | auto tosa_level = g_func_config.tosa_level; |
| 1684 | LEVEL_CHECK(in_real_height <= tosa_level.MAX_KERNEL, "H should be smaller than or equal to MAX_KERNEL"); |
| 1685 | LEVEL_CHECK(in_real_width <= tosa_level.MAX_KERNEL, "W should be smaller than or equal to MAX_KERNEL"); |
| 1686 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1687 | DEBUG_INFO(OP, "perform OpFFT2d, input.shapes=[[%d,%d,%d],[%d,%d,%d]], output.shapes=[[%d,%d,%d],[%d,%d,%d]]", |
| 1688 | in_real_batch, in_real_height, in_real_width, in_imag_batch, in_imag_height, in_imag_width, |
| 1689 | out_real_batch, out_real_height, out_real_width, out_imag_batch, out_imag_height, out_imag_width); |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1690 | |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1691 | OutEigenType sum_real, sum_imag, sign_val = 1.0; |
| 1692 | OutEigenType a, a_cos, a_sin, v_ir; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1693 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1694 | if (attribute->inverse()) |
| 1695 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1696 | sign_val = -1.0; |
| 1697 | } |
| 1698 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1699 | TIn in_real_val = this->in_real->getTensor(); |
| 1700 | TIn in_imag_val = this->in_imag->getTensor(); |
| 1701 | |
| 1702 | if (g_func_config.abs_mode) |
| 1703 | { |
| 1704 | // in abs_mode: take abs values of real and imag operands |
| 1705 | in_real_val = in_real_val.abs(); |
| 1706 | in_imag_val = in_imag_val.abs(); |
| 1707 | } |
| 1708 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1709 | for (int n = 0; n < in_real_batch; n++) |
| 1710 | { |
| 1711 | for (int oy = 0; oy < out_real_height; oy++) |
| 1712 | { |
| 1713 | for (int ox = 0; ox < out_real_width; ox++) |
| 1714 | { |
| 1715 | sum_real = 0.0; |
| 1716 | sum_imag = 0.0; |
| 1717 | for (int iy = 0; iy < in_real_height; iy++) |
| 1718 | { |
| 1719 | for (int ix = 0; ix < in_real_width; ix++) |
| 1720 | { |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1721 | OutEigenType val_real = in_real_val(n, iy, ix); |
| 1722 | OutEigenType val_imag = in_imag_val(n, iy, ix); |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1723 | // Perform the periodic calculation in integer maths to keep |
| 1724 | // the accuracy of the co-efficients similar for FP32 normal |
| 1725 | // and FP64 precise mode |
| 1726 | int32_t ay = (static_cast<int64_t>(iy) * static_cast<int64_t>(oy)) % in_real_height; |
| 1727 | int32_t ax = (static_cast<int64_t>(ix) * static_cast<int64_t>(ox)) % in_real_width; |
| 1728 | |
| 1729 | // Use explicit cast to ensure intermediate calculations are completed using OutEigenType |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1730 | a = sign_val * 2 * M_PI * |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1731 | ((OutEigenType)ay / in_real_height + (OutEigenType)ax / in_real_width); |
| 1732 | // Calculate weight values |
| 1733 | a_cos = cos(a); |
| 1734 | a_sin = sin(a); |
| 1735 | if (g_func_config.abs_mode) |
| 1736 | { |
| 1737 | // Bounded op - Use abs weight values |
| 1738 | a_cos = std::abs(a_cos); |
| 1739 | a_sin = std::abs(a_sin); |
| 1740 | // Bounded op - Use abs real value for imaginary calc |
| 1741 | v_ir = val_real; |
| 1742 | } |
| 1743 | else |
| 1744 | { |
| 1745 | // Normal op - Use negative real value for imaginary calc |
| 1746 | v_ir = -val_real; |
| 1747 | } |
| 1748 | sum_real += val_real * a_cos + val_imag * a_sin; |
| 1749 | sum_imag += v_ir * a_sin + val_imag * a_cos; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | this->out_real->getTensor()(n, oy, ox) = sum_real; |
| 1753 | this->out_imag->getTensor()(n, oy, ox) = sum_imag; |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | return GraphNode::eval(); |
| 1759 | } |
| 1760 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1761 | template <TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1762 | OpRFFT2d<Dtype>::OpRFFT2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1763 | : GraphNode(sgt_, Op_RFFT2D, id_) |
| 1764 | { |
| 1765 | setRequiredOperands(1, 2); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1766 | setRequiredRank(3, 3); |
Tai Ly | fd8fde8 | 2023-11-13 20:18:14 +0000 | [diff] [blame] | 1767 | |
| 1768 | INIT_ATTRIBUTE(RFFT); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1771 | template <TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1772 | OpRFFT2d<Dtype>::~OpRFFT2d() |
Tai Ly | fd8fde8 | 2023-11-13 20:18:14 +0000 | [diff] [blame] | 1773 | { |
| 1774 | if (attribute) |
| 1775 | delete attribute; |
| 1776 | } |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1777 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1778 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1779 | int OpRFFT2d<Dtype>::checkTensorAttributes() |
| 1780 | { |
| 1781 | if (validateRequiredOperands()) |
| 1782 | return 1; |
| 1783 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1784 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]) || validateRequiredRank(outputs[1])) |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1785 | { |
| 1786 | return 1; |
| 1787 | } |
| 1788 | |
| 1789 | if (inputs[0]->matchType(*outputs[0]) || inputs[0]->matchType(*outputs[1])) |
| 1790 | { |
| 1791 | printNodeValidationError("OpRFFT2d: input and output tensor type mismatch"); |
| 1792 | return 1; |
| 1793 | } |
| 1794 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1795 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1796 | out_real = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 1797 | out_imag = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[1]); |
| 1798 | |
| 1799 | ASSERT_MEM(in && out_real && out_imag); |
| 1800 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1801 | std::string msg; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1802 | if (check_fft_shape(in->getShape(), {}, out_real->getShape(), out_imag->getShape(), msg)) |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1803 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1804 | msg = "OpRFFT2d: " + msg; |
| 1805 | printNodeValidationError(msg.c_str()); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1806 | return 1; |
| 1807 | } |
| 1808 | |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1812 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1813 | int OpRFFT2d<Dtype>::eval() |
| 1814 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1815 | int32_t in_batch = in->getShape()[0]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1816 | int32_t in_height = in->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1817 | int32_t in_width = in->getShape()[2]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1818 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1819 | int32_t out_real_batch = out_real->getShape()[0]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1820 | int32_t out_real_height = out_real->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1821 | int32_t out_real_width = out_real->getShape()[2]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1822 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1823 | int32_t out_imag_batch = out_imag->getShape()[0]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1824 | int32_t out_imag_height = out_imag->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1825 | int32_t out_imag_width = out_imag->getShape()[2]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1826 | |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1827 | int32_t half_in_height = in_height / 2; |
| 1828 | int32_t half_in_width = in_width / 2; |
| 1829 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1830 | // Check Tosa Level |
| 1831 | auto tosa_level = g_func_config.tosa_level; |
| 1832 | LEVEL_CHECK(in_height <= tosa_level.MAX_KERNEL, "H should be smaller than or equal to MAX_KERNEL"); |
| 1833 | LEVEL_CHECK(in_width <= tosa_level.MAX_KERNEL, "W should be smaller than or equal to MAX_KERNEL"); |
| 1834 | |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1835 | DEBUG_INFO(OP, |
| 1836 | "perform OpRFFT2d, input.shape=[%d,%d,%d], output_real.shape=[%d,%d,%d], " |
| 1837 | "output_imag.shape=[%d,%d,%d]", |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1838 | in_batch, in_height, in_width, out_real_batch, out_real_height, out_real_width, out_imag_batch, |
| 1839 | out_imag_height, out_imag_width); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1840 | |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1841 | OutEigenType sum_real, sum_imag; |
| 1842 | OutEigenType a, a_cos, a_sin, v_ir; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1843 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1844 | TIn in_val = this->in->getTensor(); |
| 1845 | |
| 1846 | if (g_func_config.abs_mode) |
| 1847 | { |
| 1848 | // in abs_mode: take abs values of in operand |
| 1849 | in_val = in_val.abs(); |
| 1850 | } |
| 1851 | |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1852 | for (int n = 0; n < in_batch; n++) |
| 1853 | { |
| 1854 | for (int oy = 0; oy < out_real_height; oy++) |
| 1855 | { |
| 1856 | for (int ox = 0; ox < out_real_width; ox++) |
| 1857 | { |
| 1858 | sum_real = 0.0; |
| 1859 | sum_imag = 0.0; |
| 1860 | for (int iy = 0; iy < in_height; iy++) |
| 1861 | { |
| 1862 | for (int ix = 0; ix < in_width; ix++) |
| 1863 | { |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1864 | OutEigenType val = in_val(n, iy, ix); |
| 1865 | // Perform the periodic calculation in integer maths to keep |
| 1866 | // the accuracy of the co-efficients similar for FP32 normal |
| 1867 | // and FP64 precise mode |
| 1868 | int32_t ay = (static_cast<int64_t>(iy) * static_cast<int64_t>(oy)) % in_height; |
| 1869 | int32_t ax = (static_cast<int64_t>(ix) * static_cast<int64_t>(ox)) % in_width; |
| 1870 | |
| 1871 | // Use explicit cast to ensure intermediate calculations are completed using OutEigenType |
| 1872 | a = 2 * M_PI * ((OutEigenType)ay / in_height + (OutEigenType)ax / in_width); |
| 1873 | |
| 1874 | // Calculate weight values (co-efficients) |
| 1875 | a_cos = cos(a); |
| 1876 | a_sin = sin(a); |
| 1877 | |
| 1878 | if (g_func_config.abs_mode) |
| 1879 | { |
| 1880 | // Bounded op - Use abs weight values |
| 1881 | a_cos = std::abs(a_cos); |
| 1882 | a_sin = std::abs(a_sin); |
| 1883 | // Bounded op - Use abs real value for imaginary calc |
| 1884 | v_ir = val; |
| 1885 | } |
| 1886 | else |
| 1887 | { |
| 1888 | // Normal op - Use negative real value for imaginary calc |
| 1889 | v_ir = -val; |
| 1890 | } |
| 1891 | sum_real += val * a_cos; |
| 1892 | // Imaginary values with locations (0,0), (0,W/2), (H/2,0) and (H/2,W/2) are zero. |
| 1893 | // But due to sin(M_PI) not returning 0 because of M_PI being approximate, only |
| 1894 | // add to the imaginary sum when not processing these locations. |
Dmitriy Smirnov | 0306fb6 | 2024-04-12 14:18:11 +0100 | [diff] [blame] | 1895 | if ((in_height > 1 && (ay % (half_in_height)) > 0) || |
| 1896 | (in_width > 1 && (ax % (half_in_width)) > 0)) |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1897 | { |
| 1898 | sum_imag += v_ir * a_sin; |
| 1899 | } |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | this->out_real->getTensor()(n, oy, ox) = sum_real; |
| 1903 | this->out_imag->getTensor()(n, oy, ox) = sum_imag; |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | return GraphNode::eval(); |
| 1909 | } |
| 1910 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1911 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1912 | OpTransposeConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::OpTransposeConv2d(SubgraphTraverser* sgt_, |
| 1913 | TosaAttributeBase* attribute_, |
| 1914 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1915 | : GraphNode(sgt_, Op_TRANSPOSE_CONV2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1916 | { |
| 1917 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1918 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1919 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1920 | INIT_ATTRIBUTE(TransposeConv); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1923 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1924 | OpTransposeConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::~OpTransposeConv2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1925 | { |
| 1926 | if (attribute) |
| 1927 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 1930 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 1931 | int OpTransposeConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1932 | { |
| 1933 | if (validateRequiredOperands()) |
| 1934 | return 1; |
| 1935 | |
| 1936 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1937 | { |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1941 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1942 | "OpTransposeConv2d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1943 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1944 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1945 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 1946 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1947 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1948 | |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 1949 | if (attribute->out_pad().size() != 4) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1950 | { |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 1951 | printNodeValidationError("OpTransposeConv2d: illegal size for attribute out_pad"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1952 | return 1; |
| 1953 | } |
| 1954 | |
| 1955 | if (attribute->stride().size() != 2) |
| 1956 | { |
| 1957 | printNodeValidationError("OpTransposeConv2d: illegal size for attribute stride"); |
| 1958 | return 1; |
| 1959 | } |
| 1960 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1961 | for (int32_t i : attribute->stride()) |
| 1962 | { |
| 1963 | if (i < 1) |
| 1964 | { |
| 1965 | printNodeValidationError("OpTransposeConv2d: At least one stride is smaller than one"); |
| 1966 | return 1; |
| 1967 | } |
| 1968 | } |
| 1969 | |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 1970 | int32_t IH = input->getShape()[1]; |
| 1971 | int32_t IW = input->getShape()[2]; |
| 1972 | int32_t OH = output->getShape()[1]; |
| 1973 | int32_t OW = output->getShape()[2]; |
| 1974 | |
| 1975 | int32_t stride_y = attribute->stride()[0]; |
| 1976 | int32_t stride_x = attribute->stride()[1]; |
| 1977 | int32_t kernel_h = weight->getShape()[1]; |
| 1978 | int32_t kernel_w = weight->getShape()[2]; |
| 1979 | |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 1980 | int32_t out_pad_top = attribute->out_pad()[0]; |
| 1981 | int32_t out_pad_bottom = attribute->out_pad()[1]; |
| 1982 | int32_t out_pad_left = attribute->out_pad()[2]; |
| 1983 | int32_t out_pad_right = attribute->out_pad()[3]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 1984 | |
Eric Kunze | c1a9783 | 2022-07-01 16:56:09 -0700 | [diff] [blame] | 1985 | for (size_t i = 0; i < attribute->out_pad().size(); i++) |
| 1986 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1987 | ERROR_IF(attribute->out_pad()[i] <= -(weight->getShape()[(i / 2) + 1]), |
| 1988 | "OpTransposeConv2d: At least one out_pad value is larger than kernel size"); |
Eric Kunze | c1a9783 | 2022-07-01 16:56:09 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | int32_t H = (IH - 1) * stride_y + out_pad_top + out_pad_bottom + kernel_h; |
| 1992 | int32_t W = (IW - 1) * stride_x + out_pad_left + out_pad_right + kernel_w; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 1993 | |
| 1994 | if ((OH != H) || (OW != W)) |
| 1995 | { |
| 1996 | std::string msg = "OpTransposeConv2d: Mismatch between output shape provided and expected output shape (" + |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1997 | std::to_string(H) + "," + std::to_string(W) + ")"; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 1998 | printNodeValidationError(msg.c_str()); |
| 1999 | return 1; |
| 2000 | } |
| 2001 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2002 | ERROR_IF(InDtype != TOSA_REF_TYPE_INT8 && attribute->input_zp() != 0, |
| 2003 | "OpTransposeConv2d: Input zeropoint must be zero for non int8_t data"); |
| 2004 | ERROR_IF(WeightDtype != TOSA_REF_TYPE_INT8 && attribute->weight_zp() != 0, |
| 2005 | "OpTransposeConv2d: Weight zeropoint must be zero for non int8_t data"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 2006 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
| 2009 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 2010 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE AccDtype, TOSA_REF_TYPE OutDtype> |
| 2011 | int OpTransposeConv2d<InDtype, WeightDtype, AccDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2012 | { |
| 2013 | int in_batch = this->input->getShape()[0]; |
| 2014 | int in_height = this->input->getShape()[1]; |
| 2015 | int in_width = this->input->getShape()[2]; |
| 2016 | int in_channels = this->input->getShape()[3]; |
| 2017 | |
| 2018 | int f_out_channels = this->weight->getShape()[0]; |
| 2019 | int f_height = this->weight->getShape()[1]; |
| 2020 | int f_width = this->weight->getShape()[2]; |
| 2021 | int f_in_channels = this->weight->getShape()[3]; |
| 2022 | |
| 2023 | int b_out_channels = this->bias->getShape()[0]; |
| 2024 | |
| 2025 | int out_batch = this->output->getShape()[0]; |
| 2026 | int out_height = this->output->getShape()[1]; |
| 2027 | int out_width = this->output->getShape()[2]; |
| 2028 | int out_channels = this->output->getShape()[3]; |
| 2029 | |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 2030 | int out_pad_top = this->attribute->out_pad()[0]; |
| 2031 | int out_pad_bottom = this->attribute->out_pad()[1]; |
| 2032 | int out_pad_left = this->attribute->out_pad()[2]; |
| 2033 | int out_pad_right = this->attribute->out_pad()[3]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 2034 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2035 | int stride_y = this->attribute->stride()[0]; |
| 2036 | int stride_x = this->attribute->stride()[1]; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2037 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 2038 | ERROR_IF(in_batch != out_batch, "OpTransposeConv2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 2039 | ERROR_IF(f_in_channels != in_channels, "OpTransposeConv2d: tensor input channel mismatch %d != %d", f_in_channels, |
| 2040 | in_channels); |
| 2041 | ERROR_IF(f_out_channels != out_channels, "OpTransposeConv2d: tensor output channel mismatch %d != %d", |
| 2042 | f_out_channels, out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 2043 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, |
| 2044 | "OpTransposeConv2d: bias channels mismatch %d != %d", b_out_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2045 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2046 | // Check Tosa Level |
| 2047 | auto tosa_level = g_func_config.tosa_level; |
| 2048 | LEVEL_CHECK(f_height <= tosa_level.MAX_KERNEL, "KH should be smaller than or equal to MAX_KERNEL"); |
| 2049 | LEVEL_CHECK(f_width <= tosa_level.MAX_KERNEL, "KW should be smaller than or equal to MAX_KERNEL"); |
| 2050 | LEVEL_CHECK(out_pad_top <= tosa_level.MAX_KERNEL, "out_pad_top should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 2051 | LEVEL_CHECK(out_pad_bottom <= tosa_level.MAX_KERNEL, |
| 2052 | "out_pad_bottom should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2053 | LEVEL_CHECK(out_pad_left <= tosa_level.MAX_KERNEL, "out_pad_left should be smaller than or equal to MAX_KERNEL"); |
| 2054 | LEVEL_CHECK(out_pad_right <= tosa_level.MAX_KERNEL, "out_pad_right should be smaller than or equal to MAX_KERNEL"); |
| 2055 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 2056 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 2057 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2058 | DEBUG_INFO(OP, |
| 2059 | "perform OpTransposeConv2d, input.shape=[%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d], " |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2060 | "output.shape=[%d,%d,%d,%d], stride=[%d,%d], out_pad=[%d,%d,%d,%d]", |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 2061 | in_batch, in_height, in_width, in_channels, f_height, f_width, f_out_channels, f_in_channels, out_batch, |
| 2062 | out_height, out_width, out_channels, stride_y, stride_x, out_pad_top, out_pad_bottom, out_pad_left, |
| 2063 | out_pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2064 | |
| 2065 | TIn input_val = this->input->getTensor(); |
| 2066 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2067 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2068 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 2069 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 2070 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 2073 | TBias bias_val = this->bias->getTensor(); |
| 2074 | |
| 2075 | if (g_func_config.abs_mode) |
| 2076 | { |
| 2077 | // in abs_mode: take abs values of conv operands |
| 2078 | input_val = input_val.abs(); |
| 2079 | weight_val = weight_val.abs(); |
| 2080 | bias_val = bias_val.abs(); |
| 2081 | } |
| 2082 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2083 | Eigen::array<Eigen::Index, 4> reshape_dim; |
| 2084 | reshape_dim.fill(1); |
| 2085 | reshape_dim[3] = b_out_channels; |
| 2086 | |
| 2087 | Eigen::array<Eigen::Index, 4> bcast; |
| 2088 | bcast[0] = out_batch; |
| 2089 | bcast[1] = out_height; |
| 2090 | bcast[2] = out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 2091 | bcast[3] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2092 | |
| 2093 | // initialize with bias |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 2094 | this->output->getTensor() = bias_val.reshape(reshape_dim).broadcast(bcast); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2095 | |
| 2096 | int out_x_origin, out_y_origin; |
| 2097 | int out_x, out_y; |
| 2098 | |
| 2099 | // reference implementation from: tensorflow/tensorflow/lite/kernels/internal/reference/reference_ops.h |
| 2100 | for (int ob = 0; ob < out_batch; ob++) |
| 2101 | { |
| 2102 | for (int ih = 0; ih < in_height; ih++) |
| 2103 | { |
| 2104 | for (int iw = 0; iw < in_width; iw++) |
| 2105 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2106 | out_x_origin = iw * stride_x + out_pad_left; |
| 2107 | out_y_origin = ih * stride_y + out_pad_top; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2108 | for (int ic = 0; ic < in_channels; ic++) |
| 2109 | { |
| 2110 | for (int fh = 0; fh < f_height; fh++) |
| 2111 | { |
| 2112 | for (int fw = 0; fw < f_width; fw++) |
| 2113 | { |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 2114 | out_x = out_x_origin + fw; |
| 2115 | out_y = out_y_origin + fh; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2116 | for (int oc = 0; oc < out_channels; oc++) |
| 2117 | { |
| 2118 | if ((out_x >= 0 && out_x < out_width) && (out_y >= 0 && out_y < out_height)) |
| 2119 | { |
| 2120 | this->output->getTensor()(ob, out_y, out_x, oc) += |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 2121 | (OutEigenType)((AccEigenType)input_val(ob, ih, iw, ic) * |
| 2122 | (AccEigenType)weight_val(oc, fh, fw, ic)); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2132 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2133 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2134 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 2135 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | return GraphNode::eval(); |
| 2139 | } |
| 2140 | |
| 2141 | // template explicit instantiation |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2142 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP16); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 2143 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 2144 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2145 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2146 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, INT16); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2147 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2148 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP8E4M3); |
| 2149 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP8E5M2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2150 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2151 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP16, FP16); |
| 2152 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP16, FP32); |
| 2153 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, BF16, FP32); |
| 2154 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP32, FP32); |
| 2155 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, INT8, INT32); |
| 2156 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, INT16, INT32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2157 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2158 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP8E4M3, FP16); |
| 2159 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2160 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 2161 | // [in_t, weight_t, acc_t, out_t] |
| 2162 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, FP16, FP16, FP16, FP16); |
| 2163 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, FP16, FP16, FP32, FP16); |
| 2164 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, BF16, BF16, FP32, BF16); |
| 2165 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, FP32, FP32, FP32, FP32); |
| 2166 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, INT8, INT4, INT32, INT32); |
| 2167 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, INT8, INT8, INT32, INT32); |
| 2168 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, INT16, INT8, INT48, INT48); |
| 2169 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, FP64, FP64, FP64, FP64); |
| 2170 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, FP8E4M3, FP8E4M3, FP16, FP16); |
| 2171 | DEF_INSTANTIATE_FOUR_TYPE(OpConv2d, FP8E5M2, FP8E5M2, FP16, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2172 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 2173 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, FP16, FP16, FP16, FP16); |
| 2174 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, FP16, FP16, FP32, FP16); |
| 2175 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, BF16, BF16, FP32, BF16); |
| 2176 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, FP32, FP32, FP32, FP32); |
| 2177 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, INT8, INT4, INT32, INT32); |
| 2178 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, INT8, INT8, INT32, INT32); |
| 2179 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, INT16, INT8, INT48, INT48); |
| 2180 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, FP64, FP64, FP64, FP64); |
| 2181 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, FP8E4M3, FP8E4M3, FP16, FP16); |
| 2182 | DEF_INSTANTIATE_FOUR_TYPE(OpConv3d, FP8E5M2, FP8E5M2, FP16, FP16); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2183 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 2184 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, FP16, FP16, FP16, FP16); |
| 2185 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, FP16, FP16, FP32, FP16); |
| 2186 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, BF16, BF16, FP32, BF16); |
| 2187 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, FP32, FP32, FP32, FP32); |
| 2188 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, INT8, INT4, INT32, INT32); |
| 2189 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, INT8, INT8, INT32, INT32); |
| 2190 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, INT16, INT8, INT48, INT48); |
| 2191 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, FP64, FP64, FP64, FP64); |
| 2192 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, FP8E4M3, FP8E4M3, FP16, FP16); |
| 2193 | DEF_INSTANTIATE_FOUR_TYPE(OpDepthwiseConv2d, FP8E5M2, FP8E5M2, FP16, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2194 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 2195 | DEF_INSTANTIATE_ONE_TYPE(OpFFT2d, FP32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2196 | DEF_INSTANTIATE_ONE_TYPE(OpFFT2d, FP64); |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 2197 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2198 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP16, FP16, FP16); |
| 2199 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP16, FP16, FP32); |
| 2200 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, BF16, BF16, FP32); |
| 2201 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP32, FP32, FP32); |
| 2202 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, INT8, INT4, INT32); |
| 2203 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, INT8, INT8, INT32); |
| 2204 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, INT16, INT8, INT48); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2205 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP64, FP64, FP64); |
Won Jeon | e67115e | 2024-03-13 19:18:08 +0000 | [diff] [blame] | 2206 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP8E4M3, FP8E4M3, FP16); |
| 2207 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP8E5M2, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2208 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2209 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, INT8, INT32); |
| 2210 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, INT16, INT48); |
| 2211 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP16, FP16); |
| 2212 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP16, FP32); |
| 2213 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, BF16, FP32); |
| 2214 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP32, FP32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2215 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2216 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP8E4M3, FP16); |
| 2217 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2218 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2219 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP16); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 2220 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 2221 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2222 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2223 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, INT16); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2224 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2225 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP8E4M3); |
| 2226 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP8E5M2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2227 | |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 2228 | DEF_INSTANTIATE_ONE_TYPE(OpRFFT2d, FP32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2229 | DEF_INSTANTIATE_ONE_TYPE(OpRFFT2d, FP64); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 2230 | |
Tai Ly | f36f256 | 2024-03-14 16:21:29 +0000 | [diff] [blame] | 2231 | // [in_t, weight_t, acc_t, out_t] |
| 2232 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, FP16, FP16, FP16, FP16); |
| 2233 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, FP16, FP16, FP32, FP16); |
| 2234 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, BF16, BF16, FP32, BF16); |
| 2235 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, FP32, FP32, FP32, FP32); |
| 2236 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, INT8, INT4, INT32, INT32); |
| 2237 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, INT8, INT8, INT32, INT32); |
| 2238 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, INT16, INT8, INT48, INT48); |
| 2239 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, FP64, FP64, FP64, FP64); |
| 2240 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, FP8E4M3, FP8E4M3, FP16, FP16); |
| 2241 | DEF_INSTANTIATE_FOUR_TYPE(OpTransposeConv2d, FP8E5M2, FP8E5M2, FP16, FP16); |