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 | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 589 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE OutDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 590 | OpConv2d<InDtype, WeightDtype, OutDtype>::OpConv2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 591 | : GraphNode(sgt_, Op_CONV2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 592 | { |
| 593 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 594 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 595 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 596 | INIT_ATTRIBUTE(Conv); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 599 | 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] | 600 | OpConv2d<InDtype, WeightDtype, OutDtype>::~OpConv2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 601 | { |
| 602 | if (attribute) |
| 603 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 606 | 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] | 607 | int OpConv2d<InDtype, WeightDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 608 | { |
| 609 | if (validateRequiredOperands()) |
| 610 | return 1; |
| 611 | |
| 612 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 613 | { |
| 614 | return 1; |
| 615 | } |
| 616 | |
| 617 | // 'bias' checked separatedly since it doens't make sense to make required rank ranging from 1 to 4 |
| 618 | if (inputs[2]->getRank() != 1) |
| 619 | { |
| 620 | printNodeValidationError("OpConv2d: bias tensor must be rank 1"); |
| 621 | } |
| 622 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 623 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 624 | "OpConv2d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 625 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 626 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 627 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 628 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 629 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 630 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 631 | std::string msg; |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 632 | if (check_conv_attribute(attribute, 2 /* conv_dimension */, input->getShape(), output->getShape(), |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 633 | weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 634 | { |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 635 | msg = "OpConv2d: " + msg; |
| 636 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 637 | return 1; |
| 638 | } |
| 639 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 640 | return 0; |
| 641 | } |
| 642 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 643 | 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] | 644 | int OpConv2d<InDtype, WeightDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 645 | { |
| 646 | int in_batch = this->input->getShape()[0]; |
| 647 | int in_height = this->input->getShape()[1]; |
| 648 | int in_width = this->input->getShape()[2]; |
| 649 | int in_channels = this->input->getShape()[3]; |
| 650 | |
| 651 | int f_out_channels = this->weight->getShape()[0]; |
| 652 | int f_height = this->weight->getShape()[1]; |
| 653 | int f_width = this->weight->getShape()[2]; |
| 654 | int f_in_channels = this->weight->getShape()[3]; |
| 655 | |
| 656 | int b_out_channels = this->bias->getShape()[0]; |
| 657 | |
| 658 | int out_batch = this->output->getShape()[0]; |
| 659 | int out_height = this->output->getShape()[1]; |
| 660 | int out_width = this->output->getShape()[2]; |
| 661 | int out_channels = this->output->getShape()[3]; |
| 662 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 663 | ERROR_IF(in_batch != out_batch, "OpConv2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 664 | ERROR_IF(f_in_channels != in_channels, "OpConv2d: tensor input channel mismatch %d != %d", f_in_channels, |
| 665 | in_channels); |
| 666 | ERROR_IF(f_out_channels != out_channels, "OpConv2d: tensor output channel mismatch %d != %d", f_out_channels, |
| 667 | out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 668 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, "OpConv2d: bias channel mismatch %d != %d", |
| 669 | b_out_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 670 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 671 | int pad_top = this->attribute->pad()[0]; |
| 672 | int pad_bottom = this->attribute->pad()[1]; |
| 673 | int pad_left = this->attribute->pad()[2]; |
| 674 | int pad_right = this->attribute->pad()[3]; |
| 675 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 676 | int stride_y = this->attribute->stride()[0]; |
| 677 | int stride_x = this->attribute->stride()[1]; |
| 678 | int dilation_y = this->attribute->dilation()[0]; |
| 679 | int dilation_x = this->attribute->dilation()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 680 | |
| 681 | // Check Tosa Level |
| 682 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 683 | LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, |
| 684 | "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); |
| 685 | LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, |
| 686 | "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 687 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 688 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 689 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 690 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
| 691 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 692 | 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] | 693 | |
| 694 | DEBUG_INFO(OP, |
| 695 | "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] | 696 | "stride=[%d,%d], dilation=[%d,%d], pad=[%d,%d,%d,%d]", |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 697 | 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] | 698 | out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, pad_bottom, |
| 699 | pad_left, pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 700 | |
| 701 | // GEMM-conv2d, left matrix is input, right matrix is weight |
| 702 | Eigen::array<Eigen::Index, 2> im2col_input_dims; |
| 703 | im2col_input_dims[0] = out_batch * out_height * out_width; |
| 704 | im2col_input_dims[1] = f_height * f_width * f_in_channels; |
| 705 | |
| 706 | Eigen::array<Eigen::Index, 2> im2col_weight_dims; |
| 707 | im2col_weight_dims[0] = f_height * f_width * f_in_channels; |
| 708 | im2col_weight_dims[1] = f_out_channels; |
| 709 | |
| 710 | Eigen::array<Eigen::Index, 2> bias_reshaped_dims; |
| 711 | bias_reshaped_dims[0] = 1; |
| 712 | bias_reshaped_dims[1] = b_out_channels; |
| 713 | |
| 714 | Eigen::array<Eigen::Index, 4> weight_zp_bcast_dims; |
| 715 | weight_zp_bcast_dims[0] = f_height; |
| 716 | weight_zp_bcast_dims[1] = f_width; |
| 717 | weight_zp_bcast_dims[2] = f_in_channels; |
| 718 | |
| 719 | Eigen::array<Eigen::Index, 2> bias_bcast_dims; |
| 720 | bias_bcast_dims[0] = out_batch * out_height * out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 721 | bias_bcast_dims[1] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 722 | |
| 723 | Eigen::array<Eigen::Index, 4> col2im_output_dims; |
| 724 | col2im_output_dims[0] = out_batch; |
| 725 | col2im_output_dims[1] = out_height; |
| 726 | col2im_output_dims[2] = out_width; |
| 727 | col2im_output_dims[3] = out_channels; |
| 728 | |
| 729 | Eigen::array<Eigen::IndexPair<Eigen::Index>, 1> contract_dims = { Eigen::IndexPair<Eigen::Index>(1, 0) }; |
| 730 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 731 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 732 | pad[0] = std::make_pair(0, 0); |
| 733 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 734 | pad[2] = std::make_pair(pad_left, pad_right); |
| 735 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 736 | |
| 737 | TIn input_val = this->input->getTensor(); |
| 738 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 739 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 740 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 741 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 742 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 743 | } |
| 744 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 745 | ETensor4<InEigenType> input_padded = input_val.pad(pad); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 746 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 747 | TBias bias_val = this->bias->getTensor(); |
| 748 | |
| 749 | if (g_func_config.abs_mode) |
| 750 | { |
| 751 | // in abs_mode: take abs values of conv operands |
| 752 | input_padded = input_padded.abs(); |
| 753 | weight_val = weight_val.abs(); |
| 754 | bias_val = bias_val.abs(); |
| 755 | } |
| 756 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 757 | // extract_image_patches() output [N, KH, KW, H * W, C] |
| 758 | // need to transpose to [N, H * W, KH, KW, C] |
| 759 | ETensor5<InEigenType> input_extract_patches = |
| 760 | input_padded |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 761 | .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] | 762 | .shuffle(Eigen::array<Eigen::Index, 5>{ 0, 3, 1, 2, 4 }); |
| 763 | |
| 764 | // reshape input to [N * H * W, KH * KW * C] |
| 765 | ETensor2<InEigenType> im2col_input = input_extract_patches.reshape(im2col_input_dims); |
| 766 | |
| 767 | // transpose and reshape weight from [OC, H, W, IC] to [H * W * IC, OC] |
| 768 | ETensor2<WeightEigenType> im2col_weight = |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 769 | 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] | 770 | |
| 771 | // don't need to apply bias_multiplier ( * bias_scale and >> bias_shift) since tflite already scale it |
| 772 | // 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] | 773 | ETensor2<OutEigenType> bias_2d = |
| 774 | (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] | 775 | |
| 776 | // output matrix is [N * H * W, C] |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 777 | ETensor2<OutEigenType> contracted_result = (im2col_input.template cast<AccEigenType>().contract( |
| 778 | im2col_weight.template cast<AccEigenType>(), contract_dims)) |
| 779 | .template cast<OutEigenType>(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 780 | |
| 781 | // adding bias |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 782 | ETensor2<OutEigenType> biased_output = contracted_result + bias_2d; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 783 | |
| 784 | // reshape back to [N, H, W, C] |
| 785 | this->output->getTensor() = biased_output.reshape(col2im_output_dims); |
| 786 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 787 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 788 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 789 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 790 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | return GraphNode::eval(); |
| 794 | } |
| 795 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 796 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE WeightDtype, TOSA_REF_TYPE OutDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 797 | OpConv3d<InDtype, WeightDtype, OutDtype>::OpConv3d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 798 | : GraphNode(sgt_, Op_CONV3D, id_) |
| 799 | { |
| 800 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 801 | setRequiredRank(5, 5); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 802 | |
| 803 | INIT_ATTRIBUTE(Conv); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 806 | 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] | 807 | OpConv3d<InDtype, WeightDtype, OutDtype>::~OpConv3d() |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 808 | { |
| 809 | if (attribute) |
| 810 | delete attribute; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 813 | 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] | 814 | int OpConv3d<InDtype, WeightDtype, OutDtype>::checkTensorAttributes() |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 815 | { |
| 816 | if (validateRequiredOperands()) |
| 817 | return 1; |
| 818 | |
| 819 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 820 | { |
| 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | // 'bias' checked separatedly since it doens't make sense to make required rank ranging from 1 to 4 |
| 825 | if (inputs[2]->getRank() != 1) |
| 826 | { |
| 827 | printNodeValidationError("OpConv3d: bias tensor must be rank 1"); |
| 828 | } |
| 829 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 830 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 831 | "OpConv3d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 832 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 833 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 834 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 835 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 836 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 837 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 838 | std::string msg; |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 839 | if (check_conv_attribute(attribute, 3 /* conv_dimension */, input->getShape(), output->getShape(), |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 840 | weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) |
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 | msg = "OpConv3d: " + msg; |
| 843 | printNodeValidationError(msg.c_str()); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 844 | return 1; |
| 845 | } |
| 846 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 850 | 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] | 851 | int OpConv3d<InDtype, WeightDtype, OutDtype>::eval() |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 852 | { |
| 853 | int in_batch = this->input->getShape()[0]; |
| 854 | int in_depth = this->input->getShape()[1]; |
| 855 | int in_height = this->input->getShape()[2]; |
| 856 | int in_width = this->input->getShape()[3]; |
| 857 | int in_channels = this->input->getShape()[4]; |
| 858 | |
| 859 | int f_out_channels = this->weight->getShape()[0]; |
| 860 | int f_depth = this->weight->getShape()[1]; |
| 861 | int f_height = this->weight->getShape()[2]; |
| 862 | int f_width = this->weight->getShape()[3]; |
| 863 | int f_in_channels = this->weight->getShape()[4]; |
| 864 | |
| 865 | int b_out_channels = this->bias->getShape()[0]; |
| 866 | |
| 867 | int out_batch = this->output->getShape()[0]; |
| 868 | int out_depth = this->output->getShape()[1]; |
| 869 | int out_height = this->output->getShape()[2]; |
| 870 | int out_width = this->output->getShape()[3]; |
| 871 | int out_channels = this->output->getShape()[4]; |
| 872 | |
| 873 | ERROR_IF(in_batch != out_batch, "OpConv3d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 874 | ERROR_IF(f_in_channels != in_channels, "OpConv3d: tensor input channel mismatch %d != %d", f_in_channels, |
| 875 | in_channels); |
| 876 | ERROR_IF(f_out_channels != out_channels, "OpConv3d: tensor output channel mismatch %d != %d", f_out_channels, |
| 877 | out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 878 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, "OpConv3d: bias channel mismatch %d != %d", |
| 879 | b_out_channels, out_channels); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 880 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 881 | int pad_d0 = this->attribute->pad()[0]; |
| 882 | int pad_d1 = this->attribute->pad()[1]; |
| 883 | int pad_top = this->attribute->pad()[2]; |
| 884 | int pad_bottom = this->attribute->pad()[3]; |
| 885 | int pad_left = this->attribute->pad()[4]; |
| 886 | int pad_right = this->attribute->pad()[5]; |
| 887 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 888 | int stride_d = this->attribute->stride()[0]; |
| 889 | int stride_y = this->attribute->stride()[1]; |
| 890 | int stride_x = this->attribute->stride()[2]; |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 891 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 892 | int dilation_d = this->attribute->dilation()[0]; |
| 893 | int dilation_y = this->attribute->dilation()[1]; |
| 894 | int dilation_x = this->attribute->dilation()[2]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 895 | |
| 896 | // Check Tosa Level |
| 897 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 898 | LEVEL_CHECK(dilation_d * f_depth <= tosa_level.MAX_KERNEL, |
| 899 | "dilation_d * KD should be smaller than or equal to MAX_KERNEL"); |
| 900 | LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, |
| 901 | "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); |
| 902 | LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, |
| 903 | "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 904 | LEVEL_CHECK(pad_d0 <= tosa_level.MAX_KERNEL, "pad_d0 should be smaller than or equal to MAX_KERNEL"); |
| 905 | LEVEL_CHECK(pad_d1 <= tosa_level.MAX_KERNEL, "pad_d1 should be smaller than or equal to MAX_KERNEL"); |
| 906 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 907 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 908 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 909 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
| 910 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 911 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 912 | 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] | 913 | |
| 914 | DEBUG_INFO( |
| 915 | OP, |
| 916 | "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] | 917 | "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] | 918 | 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] | 919 | out_batch, out_depth, out_height, out_width, out_channels, stride_d, stride_y, stride_x, dilation_d, dilation_y, |
| 920 | 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] | 921 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 922 | Eigen::array<std::pair<int32_t, int32_t>, 5> pad; |
| 923 | pad[0] = std::make_pair(0, 0); |
| 924 | pad[1] = std::make_pair(pad_d0, pad_d1); |
| 925 | pad[2] = std::make_pair(pad_top, pad_bottom); |
| 926 | pad[3] = std::make_pair(pad_left, pad_right); |
| 927 | pad[4] = std::make_pair(0, 0); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 928 | |
| 929 | TIn input_val = this->input->getTensor(); |
| 930 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 931 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 932 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 933 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 934 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 935 | } |
| 936 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 937 | ETensor5<InEigenType> input_padded = input_val.pad(pad); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 938 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 939 | TBias bias_val = this->bias->getTensor(); |
| 940 | |
| 941 | if (g_func_config.abs_mode) |
| 942 | { |
| 943 | // in abs_mode: take abs values of conv operands |
| 944 | input_padded = input_padded.abs(); |
| 945 | weight_val = weight_val.abs(); |
| 946 | bias_val = bias_val.abs(); |
| 947 | } |
| 948 | |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 949 | // 1. initialize with bias |
| 950 | Eigen::array<Eigen::Index, 5> reshape_dim; |
| 951 | reshape_dim.fill(1); |
| 952 | reshape_dim[4] = b_out_channels; |
| 953 | |
| 954 | Eigen::array<Eigen::Index, 5> bcast; |
| 955 | bcast[0] = out_batch; |
| 956 | bcast[1] = out_depth; |
| 957 | bcast[2] = out_height; |
| 958 | bcast[3] = out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 959 | bcast[4] = (b_out_channels == 1) ? out_channels : 1; |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 960 | this->output->getTensor() = bias_val.reshape(reshape_dim).broadcast(bcast); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 961 | |
| 962 | // 2. direct convolution |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 963 | AccEigenType acc(0.0); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 964 | int d_idx, h_idx, w_idx; |
| 965 | |
| 966 | for (int ob = 0; ob < out_batch; ob++) |
| 967 | { |
| 968 | for (int od = 0; od < out_depth; od++) |
| 969 | { |
| 970 | for (int oh = 0; oh < out_height; oh++) |
| 971 | { |
| 972 | for (int ow = 0; ow < out_width; ow++) |
| 973 | { |
| 974 | for (int oc = 0; oc < out_channels; oc++) |
| 975 | { |
Eric Kunze | 7edb34c | 2022-05-16 17:34:40 -0700 | [diff] [blame] | 976 | // Initialize accumulator with bias value |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 977 | acc = (AccEigenType)this->output->getTensor()(ob, od, oh, ow, oc); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 978 | for (int fd = 0; fd < f_depth; fd++) |
| 979 | { |
| 980 | d_idx = od * stride_d + fd * dilation_d; |
| 981 | for (int fh = 0; fh < f_height; fh++) |
| 982 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 983 | h_idx = oh * stride_y + fh * dilation_y; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 984 | for (int fw = 0; fw < f_width; fw++) |
| 985 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 986 | w_idx = ow * stride_x + fw * dilation_x; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 987 | for (int ic = 0; ic < in_channels; ic++) |
| 988 | { |
| 989 | acc += ((AccEigenType)input_padded(ob, d_idx, h_idx, w_idx, ic) * |
| 990 | (AccEigenType)weight_val(oc, fd, fh, fw, ic)); |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | } |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 995 | this->output->getTensor()(ob, od, oh, ow, oc) = (OutEigenType)acc; |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1002 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1003 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1004 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1005 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | return GraphNode::eval(); |
| 1009 | } |
| 1010 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1011 | 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] | 1012 | OpDepthwiseConv2d<InDtype, WeightDtype, OutDtype>::OpDepthwiseConv2d(SubgraphTraverser* sgt_, |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1013 | TosaAttributeBase* attribute_, |
| 1014 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1015 | : GraphNode(sgt_, Op_DEPTHWISE_CONV2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1016 | { |
| 1017 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1018 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1019 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1020 | INIT_ATTRIBUTE(Conv); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1023 | 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] | 1024 | OpDepthwiseConv2d<InDtype, WeightDtype, OutDtype>::~OpDepthwiseConv2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1025 | { |
| 1026 | if (attribute) |
| 1027 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1030 | 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] | 1031 | int OpDepthwiseConv2d<InDtype, WeightDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1032 | { |
| 1033 | if (validateRequiredOperands()) |
| 1034 | return 1; |
| 1035 | |
| 1036 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1037 | { |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | // 'bias' checked separatedly since it doens't make sense to make required rank ranging from 1 to 4 |
| 1042 | if (inputs[2]->getRank() != 1) |
| 1043 | { |
| 1044 | printNodeValidationError("OpDepthwiseConv2d: bias tensor must be rank 1"); |
| 1045 | } |
| 1046 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1047 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1048 | "OpDepthwiseConv2d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1049 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1050 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1051 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 1052 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1053 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1054 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1055 | std::string msg; |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1056 | if (check_conv_attribute(attribute, 2 /* conv_dimension */, input->getShape(), output->getShape(), |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1057 | weight->getShape(), 0 /* offset_kernel */, InDtype, WeightDtype, msg)) |
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 | msg = "OpDepthwiseConv2d: " + msg; |
| 1060 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1061 | return 1; |
| 1062 | } |
| 1063 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
| 1066 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1067 | 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] | 1068 | int OpDepthwiseConv2d<InDtype, WeightDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1069 | { |
| 1070 | int in_batch = this->input->getShape()[0]; |
| 1071 | int in_height = this->input->getShape()[1]; |
| 1072 | int in_width = this->input->getShape()[2]; |
| 1073 | int in_channels = this->input->getShape()[3]; |
| 1074 | |
| 1075 | int f_height = this->weight->getShape()[0]; |
| 1076 | int f_width = this->weight->getShape()[1]; |
| 1077 | int f_in_channels = this->weight->getShape()[2]; |
| 1078 | int f_multiplier = this->weight->getShape()[3]; |
| 1079 | |
| 1080 | int b_out_channels = this->bias->getShape()[0]; |
| 1081 | |
| 1082 | int out_batch = this->output->getShape()[0]; |
| 1083 | int out_height = this->output->getShape()[1]; |
| 1084 | int out_width = this->output->getShape()[2]; |
| 1085 | int out_channels = this->output->getShape()[3]; |
| 1086 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1087 | ERROR_IF(in_batch != out_batch, "OpDepthwiseConv2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 1088 | ERROR_IF(f_in_channels != in_channels, "OpDepthwiseConv2d: tensor input channel mismatch %d != %d", f_in_channels, |
| 1089 | in_channels); |
| 1090 | ERROR_IF(in_channels * f_multiplier != out_channels, "OpDepthwiseConv2d: tensor output channel mismatch %d != %d", |
| 1091 | in_channels * f_multiplier, out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1092 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, |
| 1093 | "OpDepthwiseConv2d: bias channels mismatch %d != %d", b_out_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1094 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1095 | int pad_top = this->attribute->pad()[0]; |
| 1096 | int pad_bottom = this->attribute->pad()[1]; |
| 1097 | int pad_left = this->attribute->pad()[2]; |
| 1098 | int pad_right = this->attribute->pad()[3]; |
| 1099 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1100 | int stride_y = this->attribute->stride()[0]; |
| 1101 | int stride_x = this->attribute->stride()[1]; |
| 1102 | int dilation_y = this->attribute->dilation()[0]; |
| 1103 | int dilation_x = this->attribute->dilation()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Check Tosa Level |
| 1106 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1107 | LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, |
| 1108 | "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); |
| 1109 | LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, |
| 1110 | "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1111 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 1112 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 1113 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 1114 | LEVEL_CHECK(pad_right <= tosa_level.MAX_KERNEL, "pad_right should be smaller than or equal to MAX_KERNEL"); |
| 1115 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 1116 | 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] | 1117 | |
| 1118 | DEBUG_INFO(OP, |
| 1119 | "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] | 1120 | "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] | 1121 | 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] | 1122 | out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, pad_bottom, |
| 1123 | pad_left, pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1124 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1125 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 1126 | pad[0] = std::make_pair(0, 0); |
| 1127 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 1128 | pad[2] = std::make_pair(pad_left, pad_right); |
| 1129 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1130 | |
| 1131 | TIn input_val = this->input->getTensor(); |
| 1132 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1133 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1134 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1135 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 1136 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1139 | ETensor4<InEigenType> input_padded = input_val.pad(pad); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1140 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1141 | TBias bias_val = this->bias->getTensor(); |
| 1142 | |
| 1143 | if (g_func_config.abs_mode) |
| 1144 | { |
| 1145 | // in abs_mode: take abs values of conv operands |
| 1146 | input_padded = input_padded.abs(); |
| 1147 | weight_val = weight_val.abs(); |
| 1148 | bias_val = bias_val.abs(); |
| 1149 | } |
| 1150 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1151 | // GEMM doesn't fit well with DepthwiseConv2d |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1152 | // 1. use extract_image_patches() to handle stride/dilation/pad |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1153 | // 2. perform direct convolution |
| 1154 | |
| 1155 | // 1. extract_image_patches() output [N, KH, KW, OH * OW, IC] |
| 1156 | ETensor5<InEigenType> input_extract_patches = input_padded.extract_image_patches( |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1157 | 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] | 1158 | |
| 1159 | Eigen::array<Eigen::Index, 4> reshape_dim; |
| 1160 | reshape_dim.fill(1); |
| 1161 | reshape_dim[3] = b_out_channels; |
| 1162 | |
| 1163 | Eigen::array<Eigen::Index, 4> bcast; |
| 1164 | bcast[0] = out_batch; |
| 1165 | bcast[1] = out_height; |
| 1166 | bcast[2] = out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1167 | bcast[3] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1168 | |
| 1169 | // initialize with bias |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1170 | this->output->getTensor() = bias_val.reshape(reshape_dim).broadcast(bcast); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1171 | |
| 1172 | // 2. direct depthwise convolution |
| 1173 | for (int ob = 0; ob < out_batch; ob++) |
| 1174 | { |
| 1175 | for (int oh = 0; oh < out_height; oh++) |
| 1176 | { |
| 1177 | for (int ow = 0; ow < out_width; ow++) |
| 1178 | { |
| 1179 | for (int ic = 0; ic < in_channels; ic++) |
| 1180 | { |
| 1181 | for (int cm = 0; cm < f_multiplier; cm++) |
| 1182 | { |
| 1183 | for (int fh = 0; fh < f_height; fh++) |
| 1184 | { |
| 1185 | for (int fw = 0; fw < f_width; fw++) |
| 1186 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1187 | // Perform multiplication in AccEigenType then cast to OutEigenType |
Eric Kunze | be2e87c | 2023-08-07 15:16:18 +0000 | [diff] [blame] | 1188 | this->output->getTensor()(ob, oh, ow, ic * f_multiplier + cm) += |
| 1189 | (OutEigenType)((AccEigenType)input_extract_patches(ob, fh, fw, ow * out_height + oh, |
| 1190 | ic) * |
| 1191 | (AccEigenType)weight_val(fh, fw, ic, cm)); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1200 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1201 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1202 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1203 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | return GraphNode::eval(); |
| 1207 | } |
| 1208 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1209 | 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] | 1210 | OpFullyConnected<InDtype, WeightDtype, OutDtype>::OpFullyConnected(SubgraphTraverser* sgt_, |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1211 | TosaAttributeBase* attribute_, |
| 1212 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1213 | : GraphNode(sgt_, Op_FULLY_CONNECTED, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1214 | { |
| 1215 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1216 | setRequiredRank(2, 2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1217 | |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1218 | INIT_ATTRIBUTE(FullyConnected); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1221 | 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] | 1222 | OpFullyConnected<InDtype, WeightDtype, OutDtype>::~OpFullyConnected() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1223 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1224 | if (attribute) |
| 1225 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1228 | 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] | 1229 | int OpFullyConnected<InDtype, WeightDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1230 | { |
| 1231 | if (validateRequiredOperands()) |
| 1232 | return 1; |
| 1233 | |
| 1234 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1235 | { |
| 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1240 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 1241 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
| 1242 | |
| 1243 | if (input->getShape()[1] != weight->getShape()[1]) |
| 1244 | { |
| 1245 | printNodeValidationError("OpFullyConnected operator input.shape[1] should match weight.shape[1]"); |
| 1246 | return 1; |
| 1247 | } |
| 1248 | |
Jack Frankland | ac40bd1 | 2023-11-21 17:08:37 +0000 | [diff] [blame] | 1249 | if (weight->getShape()[0] != bias->getShape()[0] && bias->getShape()[0] != 1) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1250 | { |
Jack Frankland | ac40bd1 | 2023-11-21 17:08:37 +0000 | [diff] [blame] | 1251 | printNodeValidationError( |
| 1252 | "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] | 1253 | return 1; |
| 1254 | } |
| 1255 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1256 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1257 | "OpFullyConnected: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1258 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1259 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1260 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1261 | ERROR_IF(InDtype != TOSA_REF_TYPE_INT8 && attribute->input_zp() != 0, |
| 1262 | "OpFullyConnected: Input zeropoint must be zero for non int8_t data"); |
| 1263 | ERROR_IF(WeightDtype != TOSA_REF_TYPE_INT8 && attribute->weight_zp() != 0, |
| 1264 | "OpFullyConnected: Weight zeropoint must be zero for non int8_t data"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1265 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1266 | return 0; |
| 1267 | } |
| 1268 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1269 | 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] | 1270 | int OpFullyConnected<InDtype, WeightDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1271 | { |
| 1272 | typedef Eigen::Tensor<int, 1>::DimensionPair DimPair; |
| 1273 | Eigen::array<DimPair, 1> dims{ { DimPair(1, 0) } }; |
| 1274 | |
| 1275 | Eigen::array<Eigen::Index, 2> weight_shuffle{ 1, 0 }; |
| 1276 | |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1277 | int b_out_channels = this->bias->getShape()[0]; |
| 1278 | int out_channels = this->output->getShape()[1]; |
| 1279 | |
| 1280 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, "OpFullyConnected: bias channels mismatch %d != %d", |
| 1281 | b_out_channels, out_channels); |
| 1282 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1283 | Eigen::array<Eigen::Index, 2> bias_reshape; |
| 1284 | bias_reshape[0] = 1; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1285 | bias_reshape[1] = b_out_channels; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1286 | |
| 1287 | Eigen::array<Eigen::Index, 2> bias_bcast; |
| 1288 | bias_bcast[0] = this->input->getShape()[0]; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 1289 | bias_bcast[1] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1290 | |
| 1291 | TIn input_val = this->input->getTensor(); |
| 1292 | TWeight weight_val = this->weight->getTensor().shuffle(weight_shuffle); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1293 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1294 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1295 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 1296 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1299 | TBias bias_val = this->bias->getTensor(); |
| 1300 | |
| 1301 | if (g_func_config.abs_mode) |
| 1302 | { |
| 1303 | // in abs_mode: take abs values of conv operands |
| 1304 | input_val = input_val.abs(); |
| 1305 | weight_val = weight_val.abs(); |
| 1306 | bias_val = bias_val.abs(); |
| 1307 | } |
| 1308 | |
| 1309 | this->output->getTensor() = input_val.template cast<AccEigenType>() |
| 1310 | .contract(weight_val.template cast<AccEigenType>(), dims) |
| 1311 | .template cast<OutEigenType>() + |
| 1312 | bias_val.reshape(bias_reshape).broadcast(bias_bcast); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1313 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1314 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1315 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1316 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1317 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1318 | } |
| 1319 | return GraphNode::eval(); |
| 1320 | } |
| 1321 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1322 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1323 | OpMatMul<Dtype, OutDtype>::OpMatMul(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1324 | : GraphNode(sgt_, Op_MATMUL, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1325 | { |
| 1326 | setRequiredOperands(2, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1327 | setRequiredRank(3, 3); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1328 | |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1329 | INIT_ATTRIBUTE(MatMul); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1332 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1333 | OpMatMul<Dtype, OutDtype>::~OpMatMul() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1334 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1335 | if (attribute) |
| 1336 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1339 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1340 | int OpMatMul<Dtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1341 | { |
| 1342 | if (validateRequiredOperands()) |
| 1343 | return 1; |
| 1344 | |
| 1345 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1346 | { |
| 1347 | return 1; |
| 1348 | } |
| 1349 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1350 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1351 | "OpMatMul: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1352 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1353 | a = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1354 | b = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1355 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1356 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1357 | ASSERT_MEM(a && b && output); |
| 1358 | |
| 1359 | // a: [N, H, C] |
| 1360 | // b: [N, C, W] |
| 1361 | // c: [N, H, W] |
| 1362 | |
| 1363 | // Check N |
| 1364 | 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] | 1365 | { |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1366 | 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] | 1367 | return 1; |
| 1368 | } |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1369 | N = a->getShape()[0]; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1370 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1371 | // Check C |
| 1372 | if (a->getShape()[2] != b->getShape()[1]) |
| 1373 | { |
| 1374 | printNodeValidationError("OpMatMul operator a.shape[2] should match b.shape[1]"); |
| 1375 | return 1; |
| 1376 | } |
| 1377 | C = a->getShape()[2]; |
| 1378 | |
| 1379 | // Check H |
| 1380 | if (a->getShape()[1] != output->getShape()[1]) |
| 1381 | { |
| 1382 | printNodeValidationError("OpMatMul operator a.shape[1] should match output.shape[1]"); |
| 1383 | return 1; |
| 1384 | } |
| 1385 | H = a->getShape()[1]; |
| 1386 | |
| 1387 | // Check W |
| 1388 | if (b->getShape()[2] != output->getShape()[2]) |
| 1389 | { |
| 1390 | printNodeValidationError("OpMatMul operator output.shape[2] should match output.shape[2]"); |
| 1391 | return 1; |
| 1392 | } |
| 1393 | W = b->getShape()[2]; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1394 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1395 | ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->a_zp() != 0, |
| 1396 | "OpMatMul: A zeropoint must be zero for non int8_t data"); |
| 1397 | ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->b_zp() != 0, |
| 1398 | "OpMatMul: B zeropoint must be zero for non int8_t data"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1399 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1403 | template <TOSA_REF_TYPE Dtype, TOSA_REF_TYPE OutDtype> |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1404 | int OpMatMul<Dtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1405 | { |
| 1406 | typedef Eigen::Tensor<int, 1>::DimensionPair DimPair; |
| 1407 | Eigen::array<DimPair, 1> dims{ { DimPair(1, 0) } }; |
| 1408 | |
| 1409 | TIn a_val = this->a->getTensor(); |
| 1410 | TIn b_val = this->b->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1411 | if (Dtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1412 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 1413 | a_val = a_val - (InEigenType)attribute->a_zp(); |
| 1414 | b_val = b_val - (InEigenType)attribute->b_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1417 | if (g_func_config.abs_mode) |
| 1418 | { |
| 1419 | // in abs_mode: take abs values of matmul operands |
| 1420 | a_val = a_val.abs(); |
| 1421 | b_val = b_val.abs(); |
| 1422 | } |
| 1423 | |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1424 | Eigen::array<Eigen::Index, 2> a_rank2_shape({ H, C }); |
| 1425 | Eigen::array<Eigen::Index, 2> b_rank2_shape({ C, W }); |
| 1426 | Eigen::array<Eigen::Index, 3> output_rank3_shape({ 1, H, W }); |
| 1427 | |
| 1428 | Eigen::array<Eigen::Index, 3> a_size_array({ 1, H, C }); |
| 1429 | Eigen::array<Eigen::Index, 3> b_size_array({ 1, C, W }); |
| 1430 | |
| 1431 | Eigen::array<Eigen::Index, 3> a_begin_array({ 0, 0, 0 }); |
| 1432 | Eigen::array<Eigen::Index, 3> b_begin_array({ 0, 0, 0 }); |
| 1433 | |
| 1434 | // Iterate N dimension. |
| 1435 | for (int i = 0; i < N; i++) |
| 1436 | { |
| 1437 | a_begin_array[0] = i; |
| 1438 | b_begin_array[0] = i; |
| 1439 | |
| 1440 | TInRank2 a_rank2_val = a_val.slice(a_begin_array, a_size_array).reshape(a_rank2_shape); |
| 1441 | TInRank2 b_rank2_val = b_val.slice(b_begin_array, b_size_array).reshape(b_rank2_shape); |
| 1442 | TAccRank2 output_rank2_val = |
| 1443 | 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] | 1444 | 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] | 1445 | if (i == 0) |
| 1446 | { |
| 1447 | this->output->getTensor() = output_rank3_val; |
| 1448 | } |
| 1449 | else |
| 1450 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1451 | TOut temp = this->output->getTensor().concatenate(output_rank3_val, 0); |
Kevin Cheng | 2d60f00 | 2021-06-09 14:18:32 -0700 | [diff] [blame] | 1452 | this->output->getTensor() = temp; |
| 1453 | } |
| 1454 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1455 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1456 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1457 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1458 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 1459 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | return GraphNode::eval(); |
| 1463 | } |
| 1464 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1465 | template <TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1466 | OpMaxPool2d<Dtype>::OpMaxPool2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1467 | : GraphNode(sgt_, Op_MAX_POOL2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1468 | { |
| 1469 | setRequiredOperands(1, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1470 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1471 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1472 | INIT_ATTRIBUTE(Pool); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1475 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1476 | OpMaxPool2d<Dtype>::~OpMaxPool2d() |
| 1477 | { |
| 1478 | if (attribute) |
| 1479 | delete attribute; |
| 1480 | } |
| 1481 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1482 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1483 | int OpMaxPool2d<Dtype>::checkTensorAttributes() |
| 1484 | { |
| 1485 | if (validateRequiredOperands()) |
| 1486 | return 1; |
| 1487 | |
| 1488 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) |
| 1489 | { |
| 1490 | return 1; |
| 1491 | } |
| 1492 | |
| 1493 | if (inputs[0]->matchType(*outputs[0])) |
| 1494 | { |
| 1495 | printNodeValidationError("OpMaxPool2d: input and output tensor type mismatch"); |
| 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1500 | out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 1501 | |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 1502 | std::string msg; |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1503 | if (check_pool2d_attribute(attribute, in->getShape(), out->getShape(), msg)) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1504 | { |
Kevin Cheng | 7eb93d7 | 2021-10-09 01:26:08 +0000 | [diff] [blame] | 1505 | msg = "OpMaxPool2d: " + msg; |
| 1506 | printNodeValidationError(msg.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1507 | return 1; |
| 1508 | } |
| 1509 | |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1513 | template <TOSA_REF_TYPE Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1514 | int OpMaxPool2d<Dtype>::eval() |
| 1515 | { |
| 1516 | int in_batch = this->in->getShape()[0]; |
| 1517 | int in_height = this->in->getShape()[1]; |
| 1518 | int in_width = this->in->getShape()[2]; |
| 1519 | int in_channels = this->in->getShape()[3]; |
| 1520 | |
| 1521 | int out_batch = this->out->getShape()[0]; |
| 1522 | int out_height = this->out->getShape()[1]; |
| 1523 | int out_width = this->out->getShape()[2]; |
| 1524 | int out_channels = this->out->getShape()[3]; |
| 1525 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1526 | ERROR_IF(in_batch != out_batch, "OpMaxPool2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 1527 | 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] | 1528 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1529 | int pad_top = this->attribute->pad()[0]; |
| 1530 | int pad_bottom = this->attribute->pad()[1]; |
| 1531 | int pad_left = this->attribute->pad()[2]; |
| 1532 | int pad_right = this->attribute->pad()[3]; |
| 1533 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1534 | int kernel_y = this->attribute->kernel()[0]; |
| 1535 | int kernel_x = this->attribute->kernel()[1]; |
| 1536 | int stride_y = this->attribute->stride()[0]; |
| 1537 | int stride_x = this->attribute->stride()[1]; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1538 | |
| 1539 | // Check Tosa Level |
| 1540 | auto tosa_level = g_func_config.tosa_level; |
| 1541 | LEVEL_CHECK(kernel_y <= tosa_level.MAX_KERNEL, "kernel_y should be smaller than or equal to MAX_KERNEL"); |
| 1542 | LEVEL_CHECK(kernel_x <= tosa_level.MAX_KERNEL, "kernel_x should be smaller than or equal to MAX_KERNEL"); |
| 1543 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 1544 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 1545 | LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); |
| 1546 | LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); |
| 1547 | LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); |
| 1548 | 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] | 1549 | |
| 1550 | DEBUG_INFO(OP, |
| 1551 | "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] | 1552 | "stride=[%d,%d], pad=[%d,%d,%d,%d]", |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1553 | in_batch, in_height, in_width, in_channels, out_batch, out_height, out_width, out_channels, kernel_y, |
| 1554 | 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] | 1555 | |
| 1556 | Eigen::array<Eigen::Index, 2> im2col_input_dims; |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1557 | im2col_input_dims[0] = kernel_y * kernel_x; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1558 | im2col_input_dims[1] = out_batch * out_height * out_width * out_channels; |
| 1559 | |
| 1560 | Eigen::array<Eigen::Index, 4> col2im_output_dims; |
| 1561 | col2im_output_dims[0] = out_batch; |
| 1562 | col2im_output_dims[1] = out_height; |
| 1563 | col2im_output_dims[2] = out_width; |
| 1564 | col2im_output_dims[3] = out_channels; |
| 1565 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1566 | Eigen::array<std::pair<int32_t, int32_t>, 4> pad; |
| 1567 | pad[0] = std::make_pair(0, 0); |
| 1568 | pad[1] = std::make_pair(pad_top, pad_bottom); |
| 1569 | pad[2] = std::make_pair(pad_left, pad_right); |
| 1570 | pad[3] = std::make_pair(0, 0); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1571 | |
TatWai Chong | 86c403b | 2022-06-06 20:46:01 -0700 | [diff] [blame] | 1572 | 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] | 1573 | |
| 1574 | // extract_image_patches() output [N, KH, KW, H * W, C] |
| 1575 | // transpose to [KH, KW, N, H * W, C] |
| 1576 | // reshape to [KH * KW, N * H * W * C] |
| 1577 | // |
| 1578 | // Set the padding value to be the most negative value that can be |
| 1579 | // represented by the datatype to ensure that any padding values will be equal |
| 1580 | // to or smaller than the actual maximum in the KH x KW patch. |
| 1581 | ETensor2<InEigenType> input_extract_patches = |
| 1582 | input_padded |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1583 | .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] | 1584 | std::numeric_limits<InEigenType>::lowest()) |
| 1585 | .shuffle(Eigen::array<Eigen::Index, 5>{ 1, 2, 0, 3, 4 }) |
| 1586 | .reshape(im2col_input_dims); |
| 1587 | |
| 1588 | // Get the maximum of the KHxHW patches along axis 0 |
| 1589 | Eigen::Tensor<DenseIndex, 1> tensor_argmax = input_extract_patches.argmax(0); |
| 1590 | |
| 1591 | // 1D result with [N * H * W * C] |
| 1592 | ETensor1<OutEigenType> out_1d(this->out->getElementCount()); |
| 1593 | |
| 1594 | // index input_patches with argmax array should give the result |
| 1595 | for (size_t i = 0; i < this->out->getElementCount(); i++) |
| 1596 | { |
| 1597 | out_1d(i) = (OutEigenType)input_extract_patches(tensor_argmax(i), i); |
| 1598 | } |
| 1599 | |
| 1600 | // reshape result to [N, H, W, C] |
| 1601 | this->out->getTensor() = out_1d.reshape(col2im_output_dims); |
| 1602 | |
| 1603 | return GraphNode::eval(); |
| 1604 | } |
| 1605 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1606 | template <TOSA_REF_TYPE Dtype> |
| 1607 | OpFFT2d<Dtype>::OpFFT2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1608 | : GraphNode(sgt_, Op_FFT2D, id_) |
| 1609 | { |
| 1610 | setRequiredOperands(2, 2); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1611 | setRequiredRank(3, 3); |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1612 | |
| 1613 | INIT_ATTRIBUTE(FFT); |
| 1614 | } |
| 1615 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1616 | template <TOSA_REF_TYPE Dtype> |
| 1617 | OpFFT2d<Dtype>::~OpFFT2d() |
| 1618 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1619 | if (attribute) |
| 1620 | delete attribute; |
| 1621 | } |
| 1622 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1623 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1624 | int OpFFT2d<Dtype>::checkTensorAttributes() |
| 1625 | { |
| 1626 | if (validateRequiredOperands()) |
| 1627 | return 1; |
| 1628 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1629 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0]) || |
| 1630 | validateRequiredRank(outputs[1])) |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1631 | { |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1635 | 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] | 1636 | { |
| 1637 | printNodeValidationError("OpFFT2d: input and output tensor type mismatch"); |
| 1638 | return 1; |
| 1639 | } |
| 1640 | |
| 1641 | in_real = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1642 | in_imag = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]); |
| 1643 | out_real = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 1644 | out_imag = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[1]); |
| 1645 | |
| 1646 | ASSERT_MEM(in_real && in_imag && out_real && out_imag); |
| 1647 | |
| 1648 | std::string msg; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1649 | 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] | 1650 | { |
| 1651 | msg = "OpFFT2d: " + msg; |
| 1652 | printNodeValidationError(msg.c_str()); |
| 1653 | return 1; |
| 1654 | } |
| 1655 | |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1659 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1660 | int OpFFT2d<Dtype>::eval() |
| 1661 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1662 | int in_real_batch = this->in_real->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1663 | int in_real_height = this->in_real->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1664 | int in_real_width = this->in_real->getShape()[2]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1665 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1666 | int in_imag_batch = this->in_imag->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1667 | int in_imag_height = this->in_imag->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1668 | int in_imag_width = this->in_imag->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 out_real_batch = this->out_real->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1671 | int out_real_height = this->out_real->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1672 | int out_real_width = this->out_real->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_imag_batch = this->out_imag->getShape()[0]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1675 | int out_imag_height = this->out_imag->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1676 | int out_imag_width = this->out_imag->getShape()[2]; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1677 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1678 | // Check Tosa Level |
| 1679 | auto tosa_level = g_func_config.tosa_level; |
| 1680 | LEVEL_CHECK(in_real_height <= tosa_level.MAX_KERNEL, "H should be smaller than or equal to MAX_KERNEL"); |
| 1681 | LEVEL_CHECK(in_real_width <= tosa_level.MAX_KERNEL, "W should be smaller than or equal to MAX_KERNEL"); |
| 1682 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1683 | DEBUG_INFO(OP, "perform OpFFT2d, input.shapes=[[%d,%d,%d],[%d,%d,%d]], output.shapes=[[%d,%d,%d],[%d,%d,%d]]", |
| 1684 | in_real_batch, in_real_height, in_real_width, in_imag_batch, in_imag_height, in_imag_width, |
| 1685 | 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] | 1686 | |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1687 | OutEigenType sum_real, sum_imag, sign_val = 1.0; |
| 1688 | OutEigenType a, a_cos, a_sin, v_ir; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1689 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1690 | if (attribute->inverse()) |
| 1691 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1692 | sign_val = -1.0; |
| 1693 | } |
| 1694 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1695 | TIn in_real_val = this->in_real->getTensor(); |
| 1696 | TIn in_imag_val = this->in_imag->getTensor(); |
| 1697 | |
| 1698 | if (g_func_config.abs_mode) |
| 1699 | { |
| 1700 | // in abs_mode: take abs values of real and imag operands |
| 1701 | in_real_val = in_real_val.abs(); |
| 1702 | in_imag_val = in_imag_val.abs(); |
| 1703 | } |
| 1704 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1705 | for (int n = 0; n < in_real_batch; n++) |
| 1706 | { |
| 1707 | for (int oy = 0; oy < out_real_height; oy++) |
| 1708 | { |
| 1709 | for (int ox = 0; ox < out_real_width; ox++) |
| 1710 | { |
| 1711 | sum_real = 0.0; |
| 1712 | sum_imag = 0.0; |
| 1713 | for (int iy = 0; iy < in_real_height; iy++) |
| 1714 | { |
| 1715 | for (int ix = 0; ix < in_real_width; ix++) |
| 1716 | { |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1717 | OutEigenType val_real = in_real_val(n, iy, ix); |
| 1718 | OutEigenType val_imag = in_imag_val(n, iy, ix); |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1719 | // Perform the periodic calculation in integer maths to keep |
| 1720 | // the accuracy of the co-efficients similar for FP32 normal |
| 1721 | // and FP64 precise mode |
| 1722 | int32_t ay = (static_cast<int64_t>(iy) * static_cast<int64_t>(oy)) % in_real_height; |
| 1723 | int32_t ax = (static_cast<int64_t>(ix) * static_cast<int64_t>(ox)) % in_real_width; |
| 1724 | |
| 1725 | // Use explicit cast to ensure intermediate calculations are completed using OutEigenType |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1726 | a = sign_val * 2 * M_PI * |
Jeremy Johnson | c833081 | 2024-01-18 16:57:28 +0000 | [diff] [blame] | 1727 | ((OutEigenType)ay / in_real_height + (OutEigenType)ax / in_real_width); |
| 1728 | // Calculate weight values |
| 1729 | a_cos = cos(a); |
| 1730 | a_sin = sin(a); |
| 1731 | if (g_func_config.abs_mode) |
| 1732 | { |
| 1733 | // Bounded op - Use abs weight values |
| 1734 | a_cos = std::abs(a_cos); |
| 1735 | a_sin = std::abs(a_sin); |
| 1736 | // Bounded op - Use abs real value for imaginary calc |
| 1737 | v_ir = val_real; |
| 1738 | } |
| 1739 | else |
| 1740 | { |
| 1741 | // Normal op - Use negative real value for imaginary calc |
| 1742 | v_ir = -val_real; |
| 1743 | } |
| 1744 | sum_real += val_real * a_cos + val_imag * a_sin; |
| 1745 | sum_imag += v_ir * a_sin + val_imag * a_cos; |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | this->out_real->getTensor()(n, oy, ox) = sum_real; |
| 1749 | this->out_imag->getTensor()(n, oy, ox) = sum_imag; |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | return GraphNode::eval(); |
| 1755 | } |
| 1756 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1757 | template <TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1758 | OpRFFT2d<Dtype>::OpRFFT2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1759 | : GraphNode(sgt_, Op_RFFT2D, id_) |
| 1760 | { |
| 1761 | setRequiredOperands(1, 2); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1762 | setRequiredRank(3, 3); |
Tai Ly | fd8fde8 | 2023-11-13 20:18:14 +0000 | [diff] [blame] | 1763 | |
| 1764 | INIT_ATTRIBUTE(RFFT); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1767 | template <TOSA_REF_TYPE Dtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1768 | OpRFFT2d<Dtype>::~OpRFFT2d() |
Tai Ly | fd8fde8 | 2023-11-13 20:18:14 +0000 | [diff] [blame] | 1769 | { |
| 1770 | if (attribute) |
| 1771 | delete attribute; |
| 1772 | } |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1773 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1774 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1775 | int OpRFFT2d<Dtype>::checkTensorAttributes() |
| 1776 | { |
| 1777 | if (validateRequiredOperands()) |
| 1778 | return 1; |
| 1779 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1780 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]) || validateRequiredRank(outputs[1])) |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1781 | { |
| 1782 | return 1; |
| 1783 | } |
| 1784 | |
| 1785 | if (inputs[0]->matchType(*outputs[0]) || inputs[0]->matchType(*outputs[1])) |
| 1786 | { |
| 1787 | printNodeValidationError("OpRFFT2d: input and output tensor type mismatch"); |
| 1788 | return 1; |
| 1789 | } |
| 1790 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1791 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1792 | out_real = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 1793 | out_imag = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[1]); |
| 1794 | |
| 1795 | ASSERT_MEM(in && out_real && out_imag); |
| 1796 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1797 | std::string msg; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1798 | 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] | 1799 | { |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 1800 | msg = "OpRFFT2d: " + msg; |
| 1801 | printNodeValidationError(msg.c_str()); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1802 | return 1; |
| 1803 | } |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1808 | template <TOSA_REF_TYPE Dtype> |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1809 | int OpRFFT2d<Dtype>::eval() |
| 1810 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1811 | int32_t in_batch = in->getShape()[0]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1812 | int32_t in_height = in->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1813 | int32_t in_width = in->getShape()[2]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1814 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1815 | int32_t out_real_batch = out_real->getShape()[0]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1816 | int32_t out_real_height = out_real->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1817 | int32_t out_real_width = out_real->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_imag_batch = out_imag->getShape()[0]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1820 | int32_t out_imag_height = out_imag->getShape()[1]; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1821 | int32_t out_imag_width = out_imag->getShape()[2]; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1822 | |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1823 | int32_t half_in_height = in_height / 2; |
| 1824 | int32_t half_in_width = in_width / 2; |
| 1825 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 1826 | // Check Tosa Level |
| 1827 | auto tosa_level = g_func_config.tosa_level; |
| 1828 | LEVEL_CHECK(in_height <= tosa_level.MAX_KERNEL, "H should be smaller than or equal to MAX_KERNEL"); |
| 1829 | LEVEL_CHECK(in_width <= tosa_level.MAX_KERNEL, "W should be smaller than or equal to MAX_KERNEL"); |
| 1830 | |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1831 | DEBUG_INFO(OP, |
| 1832 | "perform OpRFFT2d, input.shape=[%d,%d,%d], output_real.shape=[%d,%d,%d], " |
| 1833 | "output_imag.shape=[%d,%d,%d]", |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1834 | in_batch, in_height, in_width, out_real_batch, out_real_height, out_real_width, out_imag_batch, |
| 1835 | out_imag_height, out_imag_width); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1836 | |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1837 | OutEigenType sum_real, sum_imag; |
| 1838 | OutEigenType a, a_cos, a_sin, v_ir; |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1839 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 1840 | TIn in_val = this->in->getTensor(); |
| 1841 | |
| 1842 | if (g_func_config.abs_mode) |
| 1843 | { |
| 1844 | // in abs_mode: take abs values of in operand |
| 1845 | in_val = in_val.abs(); |
| 1846 | } |
| 1847 | |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1848 | for (int n = 0; n < in_batch; n++) |
| 1849 | { |
| 1850 | for (int oy = 0; oy < out_real_height; oy++) |
| 1851 | { |
| 1852 | for (int ox = 0; ox < out_real_width; ox++) |
| 1853 | { |
| 1854 | sum_real = 0.0; |
| 1855 | sum_imag = 0.0; |
| 1856 | for (int iy = 0; iy < in_height; iy++) |
| 1857 | { |
| 1858 | for (int ix = 0; ix < in_width; ix++) |
| 1859 | { |
Jeremy Johnson | 6f57e6e | 2024-01-30 16:10:50 +0000 | [diff] [blame] | 1860 | OutEigenType val = in_val(n, iy, ix); |
| 1861 | // Perform the periodic calculation in integer maths to keep |
| 1862 | // the accuracy of the co-efficients similar for FP32 normal |
| 1863 | // and FP64 precise mode |
| 1864 | int32_t ay = (static_cast<int64_t>(iy) * static_cast<int64_t>(oy)) % in_height; |
| 1865 | int32_t ax = (static_cast<int64_t>(ix) * static_cast<int64_t>(ox)) % in_width; |
| 1866 | |
| 1867 | // Use explicit cast to ensure intermediate calculations are completed using OutEigenType |
| 1868 | a = 2 * M_PI * ((OutEigenType)ay / in_height + (OutEigenType)ax / in_width); |
| 1869 | |
| 1870 | // Calculate weight values (co-efficients) |
| 1871 | a_cos = cos(a); |
| 1872 | a_sin = sin(a); |
| 1873 | |
| 1874 | if (g_func_config.abs_mode) |
| 1875 | { |
| 1876 | // Bounded op - Use abs weight values |
| 1877 | a_cos = std::abs(a_cos); |
| 1878 | a_sin = std::abs(a_sin); |
| 1879 | // Bounded op - Use abs real value for imaginary calc |
| 1880 | v_ir = val; |
| 1881 | } |
| 1882 | else |
| 1883 | { |
| 1884 | // Normal op - Use negative real value for imaginary calc |
| 1885 | v_ir = -val; |
| 1886 | } |
| 1887 | sum_real += val * a_cos; |
| 1888 | // Imaginary values with locations (0,0), (0,W/2), (H/2,0) and (H/2,W/2) are zero. |
| 1889 | // But due to sin(M_PI) not returning 0 because of M_PI being approximate, only |
| 1890 | // add to the imaginary sum when not processing these locations. |
| 1891 | if ((ay % (half_in_height)) + (ax % (half_in_width)) > 0) |
| 1892 | { |
| 1893 | sum_imag += v_ir * a_sin; |
| 1894 | } |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | this->out_real->getTensor()(n, oy, ox) = sum_real; |
| 1898 | this->out_imag->getTensor()(n, oy, ox) = sum_imag; |
| 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | return GraphNode::eval(); |
| 1904 | } |
| 1905 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1906 | 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] | 1907 | OpTransposeConv2d<InDtype, WeightDtype, OutDtype>::OpTransposeConv2d(SubgraphTraverser* sgt_, |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1908 | TosaAttributeBase* attribute_, |
| 1909 | uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 1910 | : GraphNode(sgt_, Op_TRANSPOSE_CONV2D, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1911 | { |
| 1912 | setRequiredOperands(3, 1); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 1913 | setRequiredRank(4, 4); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1914 | |
Kevin Cheng | 93a1628 | 2021-08-31 16:14:03 -0700 | [diff] [blame] | 1915 | INIT_ATTRIBUTE(TransposeConv); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1918 | 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] | 1919 | OpTransposeConv2d<InDtype, WeightDtype, OutDtype>::~OpTransposeConv2d() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1920 | { |
| 1921 | if (attribute) |
| 1922 | delete attribute; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1925 | 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] | 1926 | int OpTransposeConv2d<InDtype, WeightDtype, OutDtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1927 | { |
| 1928 | if (validateRequiredOperands()) |
| 1929 | return 1; |
| 1930 | |
| 1931 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0])) |
| 1932 | { |
| 1933 | return 1; |
| 1934 | } |
| 1935 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 1936 | ERROR_IF(outputs[0]->getDtype() != OutDtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1937 | "OpTransposeConv2d: Output data type not supported for this configuration of operator"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 1938 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1939 | input = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 1940 | weight = dynamic_cast<TosaReference::TensorTemplate<TWeight>*>(inputs[1]); |
| 1941 | bias = dynamic_cast<TosaReference::TensorTemplate<TBias>*>(inputs[2]); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 1942 | output = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1943 | |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 1944 | if (attribute->out_pad().size() != 4) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1945 | { |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 1946 | printNodeValidationError("OpTransposeConv2d: illegal size for attribute out_pad"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1947 | return 1; |
| 1948 | } |
| 1949 | |
| 1950 | if (attribute->stride().size() != 2) |
| 1951 | { |
| 1952 | printNodeValidationError("OpTransposeConv2d: illegal size for attribute stride"); |
| 1953 | return 1; |
| 1954 | } |
| 1955 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1956 | if (attribute->output_shape().size() != 4) |
| 1957 | { |
| 1958 | printNodeValidationError("OpTransposeConv2d: illegal size for attribute output_shape"); |
| 1959 | return 1; |
| 1960 | } |
| 1961 | |
Kevin Cheng | 9fe1724 | 2021-11-10 01:04:39 +0000 | [diff] [blame] | 1962 | for (int32_t i : attribute->stride()) |
| 1963 | { |
| 1964 | if (i < 1) |
| 1965 | { |
| 1966 | printNodeValidationError("OpTransposeConv2d: At least one stride is smaller than one"); |
| 1967 | return 1; |
| 1968 | } |
| 1969 | } |
| 1970 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1971 | for (int d = 0; d < 4; d++) |
| 1972 | { |
| 1973 | if (attribute->output_shape()[d] != this->output->getShape()[d]) |
| 1974 | { |
| 1975 | printNodeValidationError("OpTransposeConv2d: illegal size for attribute output_shape"); |
| 1976 | return 1; |
| 1977 | } |
| 1978 | } |
| 1979 | |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 1980 | int32_t IH = input->getShape()[1]; |
| 1981 | int32_t IW = input->getShape()[2]; |
| 1982 | int32_t OH = output->getShape()[1]; |
| 1983 | int32_t OW = output->getShape()[2]; |
| 1984 | |
| 1985 | int32_t stride_y = attribute->stride()[0]; |
| 1986 | int32_t stride_x = attribute->stride()[1]; |
| 1987 | int32_t kernel_h = weight->getShape()[1]; |
| 1988 | int32_t kernel_w = weight->getShape()[2]; |
| 1989 | |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 1990 | int32_t out_pad_top = attribute->out_pad()[0]; |
| 1991 | int32_t out_pad_bottom = attribute->out_pad()[1]; |
| 1992 | int32_t out_pad_left = attribute->out_pad()[2]; |
| 1993 | int32_t out_pad_right = attribute->out_pad()[3]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 1994 | |
Eric Kunze | c1a9783 | 2022-07-01 16:56:09 -0700 | [diff] [blame] | 1995 | for (size_t i = 0; i < attribute->out_pad().size(); i++) |
| 1996 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 1997 | ERROR_IF(attribute->out_pad()[i] <= -(weight->getShape()[(i / 2) + 1]), |
| 1998 | "OpTransposeConv2d: At least one out_pad value is larger than kernel size"); |
Eric Kunze | c1a9783 | 2022-07-01 16:56:09 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | int32_t H = (IH - 1) * stride_y + out_pad_top + out_pad_bottom + kernel_h; |
| 2002 | 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] | 2003 | |
| 2004 | if ((OH != H) || (OW != W)) |
| 2005 | { |
| 2006 | 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] | 2007 | std::to_string(H) + "," + std::to_string(W) + ")"; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 2008 | printNodeValidationError(msg.c_str()); |
| 2009 | return 1; |
| 2010 | } |
| 2011 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2012 | ERROR_IF(InDtype != TOSA_REF_TYPE_INT8 && attribute->input_zp() != 0, |
| 2013 | "OpTransposeConv2d: Input zeropoint must be zero for non int8_t data"); |
| 2014 | ERROR_IF(WeightDtype != TOSA_REF_TYPE_INT8 && attribute->weight_zp() != 0, |
| 2015 | "OpTransposeConv2d: Weight zeropoint must be zero for non int8_t data"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 2016 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2017 | return 0; |
| 2018 | } |
| 2019 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2020 | 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] | 2021 | int OpTransposeConv2d<InDtype, WeightDtype, OutDtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2022 | { |
| 2023 | int in_batch = this->input->getShape()[0]; |
| 2024 | int in_height = this->input->getShape()[1]; |
| 2025 | int in_width = this->input->getShape()[2]; |
| 2026 | int in_channels = this->input->getShape()[3]; |
| 2027 | |
| 2028 | int f_out_channels = this->weight->getShape()[0]; |
| 2029 | int f_height = this->weight->getShape()[1]; |
| 2030 | int f_width = this->weight->getShape()[2]; |
| 2031 | int f_in_channels = this->weight->getShape()[3]; |
| 2032 | |
| 2033 | int b_out_channels = this->bias->getShape()[0]; |
| 2034 | |
| 2035 | int out_batch = this->output->getShape()[0]; |
| 2036 | int out_height = this->output->getShape()[1]; |
| 2037 | int out_width = this->output->getShape()[2]; |
| 2038 | int out_channels = this->output->getShape()[3]; |
| 2039 | |
TatWai Chong | 24594f5 | 2022-06-08 00:48:04 -0700 | [diff] [blame] | 2040 | int out_pad_top = this->attribute->out_pad()[0]; |
| 2041 | int out_pad_bottom = this->attribute->out_pad()[1]; |
| 2042 | int out_pad_left = this->attribute->out_pad()[2]; |
| 2043 | int out_pad_right = this->attribute->out_pad()[3]; |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 2044 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2045 | int stride_y = this->attribute->stride()[0]; |
| 2046 | int stride_x = this->attribute->stride()[1]; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2047 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 2048 | ERROR_IF(in_batch != out_batch, "OpTransposeConv2d: tensor batch mismatch %d != %d", in_batch, out_batch); |
| 2049 | ERROR_IF(f_in_channels != in_channels, "OpTransposeConv2d: tensor input channel mismatch %d != %d", f_in_channels, |
| 2050 | in_channels); |
| 2051 | ERROR_IF(f_out_channels != out_channels, "OpTransposeConv2d: tensor output channel mismatch %d != %d", |
| 2052 | f_out_channels, out_channels); |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 2053 | ERROR_IF(b_out_channels != out_channels && b_out_channels != 1, |
| 2054 | "OpTransposeConv2d: bias channels mismatch %d != %d", b_out_channels, out_channels); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2055 | |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2056 | // Check Tosa Level |
| 2057 | auto tosa_level = g_func_config.tosa_level; |
| 2058 | LEVEL_CHECK(f_height <= tosa_level.MAX_KERNEL, "KH should be smaller than or equal to MAX_KERNEL"); |
| 2059 | LEVEL_CHECK(f_width <= tosa_level.MAX_KERNEL, "KW should be smaller than or equal to MAX_KERNEL"); |
| 2060 | 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] | 2061 | LEVEL_CHECK(out_pad_bottom <= tosa_level.MAX_KERNEL, |
| 2062 | "out_pad_bottom should be smaller than or equal to MAX_KERNEL"); |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2063 | LEVEL_CHECK(out_pad_left <= tosa_level.MAX_KERNEL, "out_pad_left should be smaller than or equal to MAX_KERNEL"); |
| 2064 | LEVEL_CHECK(out_pad_right <= tosa_level.MAX_KERNEL, "out_pad_right should be smaller than or equal to MAX_KERNEL"); |
| 2065 | LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); |
| 2066 | LEVEL_CHECK(stride_x <= tosa_level.MAX_STRIDE, "stride_x should be smaller than or equal to MAX_STRIDE"); |
| 2067 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2068 | DEBUG_INFO(OP, |
| 2069 | "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] | 2070 | "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] | 2071 | in_batch, in_height, in_width, in_channels, f_height, f_width, f_out_channels, f_in_channels, out_batch, |
| 2072 | out_height, out_width, out_channels, stride_y, stride_x, out_pad_top, out_pad_bottom, out_pad_left, |
| 2073 | out_pad_right); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2074 | |
| 2075 | TIn input_val = this->input->getTensor(); |
| 2076 | TWeight weight_val = this->weight->getTensor(); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2077 | if (InDtype == TOSA_REF_TYPE_INT8 || WeightDtype == TOSA_REF_TYPE_INT8) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2078 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 2079 | input_val = input_val - (InEigenType)attribute->input_zp(); |
| 2080 | weight_val = weight_val - (WeightEigenType)attribute->weight_zp(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 2083 | TBias bias_val = this->bias->getTensor(); |
| 2084 | |
| 2085 | if (g_func_config.abs_mode) |
| 2086 | { |
| 2087 | // in abs_mode: take abs values of conv operands |
| 2088 | input_val = input_val.abs(); |
| 2089 | weight_val = weight_val.abs(); |
| 2090 | bias_val = bias_val.abs(); |
| 2091 | } |
| 2092 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2093 | Eigen::array<Eigen::Index, 4> reshape_dim; |
| 2094 | reshape_dim.fill(1); |
| 2095 | reshape_dim[3] = b_out_channels; |
| 2096 | |
| 2097 | Eigen::array<Eigen::Index, 4> bcast; |
| 2098 | bcast[0] = out_batch; |
| 2099 | bcast[1] = out_height; |
| 2100 | bcast[2] = out_width; |
Tai Ly | a641dd5 | 2023-08-11 19:58:50 +0000 | [diff] [blame] | 2101 | bcast[3] = (b_out_channels == 1) ? out_channels : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2102 | |
| 2103 | // initialize with bias |
Tai Ly | 307392a | 2023-05-12 21:42:19 +0000 | [diff] [blame] | 2104 | this->output->getTensor() = bias_val.reshape(reshape_dim).broadcast(bcast); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2105 | |
| 2106 | int out_x_origin, out_y_origin; |
| 2107 | int out_x, out_y; |
| 2108 | |
| 2109 | // reference implementation from: tensorflow/tensorflow/lite/kernels/internal/reference/reference_ops.h |
| 2110 | for (int ob = 0; ob < out_batch; ob++) |
| 2111 | { |
| 2112 | for (int ih = 0; ih < in_height; ih++) |
| 2113 | { |
| 2114 | for (int iw = 0; iw < in_width; iw++) |
| 2115 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 2116 | out_x_origin = iw * stride_x + out_pad_left; |
| 2117 | out_y_origin = ih * stride_y + out_pad_top; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2118 | for (int ic = 0; ic < in_channels; ic++) |
| 2119 | { |
| 2120 | for (int fh = 0; fh < f_height; fh++) |
| 2121 | { |
| 2122 | for (int fw = 0; fw < f_width; fw++) |
| 2123 | { |
Jeremy Johnson | 4a6fb9b | 2022-04-26 15:47:21 +0100 | [diff] [blame] | 2124 | out_x = out_x_origin + fw; |
| 2125 | out_y = out_y_origin + fh; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2126 | for (int oc = 0; oc < out_channels; oc++) |
| 2127 | { |
| 2128 | if ((out_x >= 0 && out_x < out_width) && (out_y >= 0 && out_y < out_height)) |
| 2129 | { |
| 2130 | this->output->getTensor()(ob, out_y, out_x, oc) += |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 2131 | (OutEigenType)((AccEigenType)input_val(ob, ih, iw, ic) * |
| 2132 | (AccEigenType)weight_val(oc, fh, fw, ic)); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2142 | if (OutDtype == TOSA_REF_TYPE_INT48) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2143 | { |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2144 | this->output->getTensor() = this->output->getTensor().cwiseMax((OutEigenType)AccQMin); |
| 2145 | this->output->getTensor() = this->output->getTensor().cwiseMin((OutEigenType)AccQMax); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | return GraphNode::eval(); |
| 2149 | } |
| 2150 | |
| 2151 | // template explicit instantiation |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2152 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP16); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 2153 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 2154 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2155 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2156 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, INT16); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2157 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2158 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP8E4M3); |
| 2159 | DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpArgMax, FP8E5M2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2160 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2161 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP16, FP16); |
| 2162 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP16, FP32); |
| 2163 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, BF16, FP32); |
| 2164 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP32, FP32); |
| 2165 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, INT8, INT32); |
| 2166 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, INT16, INT32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2167 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2168 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP8E4M3, FP16); |
| 2169 | DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2170 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 2171 | // [in_t, weight_t, out_t] |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2172 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP16, FP16, FP16); |
| 2173 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP16, FP16, FP32); |
| 2174 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, BF16, BF16, FP32); |
| 2175 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP32, FP32, FP32); |
| 2176 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, INT8, INT4, INT32); |
| 2177 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, INT8, INT8, INT32); |
| 2178 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, INT16, INT8, INT48); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2179 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP64, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2180 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP8E4M3, FP8E4M3, FP16); |
| 2181 | DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP8E5M2, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2182 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2183 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, FP16, FP16, FP16); |
| 2184 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, FP16, FP16, FP32); |
| 2185 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, BF16, BF16, FP32); |
| 2186 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, FP32, FP32, FP32); |
| 2187 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, INT8, INT4, INT32); |
| 2188 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, INT8, INT8, INT32); |
| 2189 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, INT16, INT8, INT48); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2190 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, FP64, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2191 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, FP8E4M3, FP8E4M3, FP16); |
| 2192 | DEF_INSTANTIATE_THREE_TYPE(OpConv3d, FP8E5M2, FP8E5M2, FP16); |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 2193 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2194 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, FP16, FP16, FP16); |
| 2195 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, FP16, FP16, FP32); |
| 2196 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, BF16, BF16, FP32); |
| 2197 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, FP32, FP32, FP32); |
| 2198 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, INT8, INT4, INT32); |
| 2199 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, INT8, INT8, INT32); |
| 2200 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, INT16, INT8, INT48); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2201 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, FP64, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2202 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, FP8E4M3, FP8E4M3, FP16); |
| 2203 | DEF_INSTANTIATE_THREE_TYPE(OpDepthwiseConv2d, FP8E5M2, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2204 | |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 2205 | DEF_INSTANTIATE_ONE_TYPE(OpFFT2d, FP32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2206 | DEF_INSTANTIATE_ONE_TYPE(OpFFT2d, FP64); |
Luke Hutton | 5728713 | 2023-02-06 14:54:18 +0000 | [diff] [blame] | 2207 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2208 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP16, FP16, FP16); |
| 2209 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP16, FP16, FP32); |
| 2210 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, BF16, BF16, FP32); |
| 2211 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP32, FP32, FP32); |
| 2212 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, INT8, INT4, INT32); |
| 2213 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, INT8, INT8, INT32); |
| 2214 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, INT16, INT8, INT48); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2215 | DEF_INSTANTIATE_THREE_TYPE(OpFullyConnected, FP64, FP64, FP64); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2216 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2217 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, INT8, INT32); |
| 2218 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, INT16, INT48); |
| 2219 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP16, FP16); |
| 2220 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP16, FP32); |
| 2221 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, BF16, FP32); |
| 2222 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP32, FP32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2223 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2224 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP8E4M3, FP16); |
| 2225 | DEF_INSTANTIATE_TWO_TYPE(OpMatMul, FP8E5M2, FP16); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2226 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2227 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP16); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 2228 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 2229 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2230 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2231 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, INT16); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2232 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2233 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP8E4M3); |
| 2234 | DEF_INSTANTIATE_ONE_TYPE(OpMaxPool2d, FP8E5M2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2235 | |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 2236 | DEF_INSTANTIATE_ONE_TYPE(OpRFFT2d, FP32); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2237 | DEF_INSTANTIATE_ONE_TYPE(OpRFFT2d, FP64); |
Luke Hutton | 261b7b6 | 2023-01-10 14:50:31 +0000 | [diff] [blame] | 2238 | |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 2239 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, FP16, FP16, FP16); |
| 2240 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, FP16, FP16, FP32); |
| 2241 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, BF16, BF16, FP32); |
| 2242 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, FP32, FP32, FP32); |
| 2243 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, INT8, INT4, INT32); |
| 2244 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, INT8, INT8, INT32); |
| 2245 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, INT16, INT8, INT48); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2246 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, FP64, FP64, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 2247 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, FP8E4M3, FP8E4M3, FP16); |
| 2248 | DEF_INSTANTIATE_THREE_TYPE(OpTransposeConv2d, FP8E5M2, FP8E5M2, FP16); |