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