| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "fuse_expression.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | #include "storezip.h" |
| 20 | |
| 21 | namespace pnnx { |
| 22 | |
| 23 | static bool operand_maybe_shape_tensor(const Operand* operand) |
| 24 | { |
| 25 | const Operator* op = operand->producer; |
| 26 | |
| 27 | if (op->type == "aten::size") |
| 28 | { |
| 29 | return op->inputs.size() == 1; |
| 30 | } |
| 31 | |
| 32 | if (op->type == "Tensor.to") |
| 33 | { |
| 34 | return operand_maybe_shape_tensor(op->inputs[0]); |
| 35 | } |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | static bool operand_maybe_tensor(const Operand* operand) |
| 41 | { |
| 42 | const Operator* op = operand->producer; |
| 43 | |
| 44 | if (op->type == "prim::Constant") |
| 45 | { |
| 46 | const Parameter& param = op->params.at("value"); |
| 47 | if (param.type == 0 || param.type == 1 || param.type == 2 || param.type == 3 || param.type == 4 || param.type == 10) |
| 48 | { |
| 49 | return false; |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (op->type == "prim::NumToTensor") |
| 58 | { |
| 59 | return operand_maybe_tensor(op->inputs[0]); |
| 60 | } |
| 61 | |
| 62 | if (op->type == "prim::ListConstruct") |
| 63 | { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (op->type == "torch.unbind" && op->inputs[0]->shape.size() == 1) |
| 68 | { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | if (op->type == "aten::size") |
| 73 | { |
| 74 | return op->inputs.size() == 1; |
| 75 | } |
| 76 | |
| 77 | if (op->type == "Tensor.slice") |
| 78 | { |
| 79 | // static slice |
| 80 | const size_t inputs_size = op->inputs.size(); |
| 81 | if (inputs_size != 3 && inputs_size != 4 && inputs_size != 5) |
| 82 | return true; |
| 83 | |
| 84 | for (size_t i = 0; i < inputs_size; i++) |
| 85 | { |
| 86 | if (op->inputs[i]->producer->type != "prim::Constant") |
| 87 | return true; |
| 88 | |
| 89 | if (op->inputs[i]->producer->params.at("value").type != 2) |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // dim=0 |
| 94 | if (inputs_size == 3 && op->params.at("dim").i != 0) |
| 95 | return true; |
| 96 | if ((inputs_size == 4 || inputs_size == 5) && op->inputs[0]->producer->params.at("value").i != 1) |
| 97 | return true; |
| 98 | |
| 99 | // step=1 |
| 100 | if ((inputs_size == 3 || inputs_size == 4) && op->params.at("step").i != 1) |
| 101 | return true; |
| 102 | if (inputs_size == 5 && op->inputs[4]->producer->params.at("value").i != 1) |
| 103 | return true; |
| 104 | |
| 105 | return !operand_maybe_shape_tensor(op->inputs[0]); |
| 106 | } |
| 107 | |
| 108 | if (op->type == "aten::Int") |
| 109 | { |
| 110 | return operand_maybe_tensor(op->inputs[0]); |
| 111 | } |
| 112 | |
| 113 | if (op->type == "Tensor.to" || op->type == "aten::detach") |
| 114 | { |
| 115 | return operand_maybe_tensor(op->inputs[0]); |
| 116 | } |
| 117 | |
| 118 | if (op->type == "aten::ScalarImplicit") |
| 119 | { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (op->type == "aten::abs" |
| 124 | || op->type == "aten::acos" |
| 125 | || op->type == "aten::acosh" |
| 126 | || op->type == "aten::asin" |
| 127 | || op->type == "aten::asinh" |
| 128 | || op->type == "aten::atan" |
| 129 | || op->type == "aten::atanh" |
| 130 | || op->type == "aten::ceil" |
| 131 | || op->type == "aten::cos" |
| 132 | || op->type == "aten::cosh" |
| 133 | || op->type == "aten::exp" |
| 134 | || op->type == "aten::floor" |
| 135 | || op->type == "aten::log" |
| 136 | || op->type == "aten::log10" |
| 137 | || op->type == "aten::neg" |
| 138 | || op->type == "aten::reciprocal" |
| 139 | || op->type == "aten::round" |
| 140 | || op->type == "aten::rsqrt" |
| 141 | || op->type == "aten::sign" |
| 142 | || op->type == "aten::sin" |
| 143 | || op->type == "aten::sinh" |
| 144 | || op->type == "aten::sqrt" |
| 145 | || op->type == "aten::square" |
| 146 | || op->type == "aten::tan" |
| 147 | || op->type == "aten::tanh" |
| 148 | || op->type == "aten::trunc") |
| 149 | { |
| 150 | return operand_maybe_tensor(op->inputs[0]); |
| 151 | } |
| 152 | |
| 153 | if (op->type == "aten::atan2" |
| 154 | || op->type == "aten::div" |
| 155 | || op->type == "aten::floor_divide" |
| 156 | || op->type == "aten::fmod" |
| 157 | || op->type == "aten::logaddexp" |
| 158 | || op->type == "aten::max" |
| 159 | || op->type == "aten::maximum" |
| 160 | || op->type == "aten::min" |
| 161 | || op->type == "aten::minimum" |
| 162 | || op->type == "aten::mul" |
| 163 | || op->type == "aten::pow" |
| 164 | || op->type == "aten::remainder") |
| 165 | { |
| 166 | return operand_maybe_tensor(op->inputs[0]) || operand_maybe_tensor(op->inputs[1]); |
| 167 | } |
| 168 | |
| 169 | if (op->type == "aten::__and__" || op->type == "aten::__or__" || op->type == "aten::__xor__" || op->type == "aten::__lshift__" || op->type == "aten::__rshift__") |
| 170 | { |
| 171 | return operand_maybe_tensor(op->inputs[0]) || operand_maybe_tensor(op->inputs[1]); |
| 172 | } |
| 173 | |
| 174 | if (op->type == "aten::add" || op->type == "aten::sub" || op->type == "aten::rsub") |
| 175 | { |
| 176 | if (op->inputs.size() == 2) |
| 177 | return operand_maybe_tensor(op->inputs[0]) || operand_maybe_tensor(op->inputs[1]); |
| 178 | else // if (op->inputs.size() == 3) |
| 179 | return operand_maybe_tensor(op->inputs[0]) || operand_maybe_tensor(op->inputs[1]) || operand_maybe_tensor(op->inputs[2]); |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | static void fuse_expression(Graph& graph, Operand* operand, std::string& expr, std::vector<Operand*>& inputs, const std::set<std::string>& foldable_constants, StoreZipReader& zip, bool checksubgraph = true) |
| 186 | { |
| 187 | Operator* op = operand->producer; |
| 188 | |
| 189 | // fprintf(stderr, "fuse_expression %s %s\n", op->type.c_str(), operand->name.c_str()); |
| 190 | |
| 191 | if (checksubgraph && operand_maybe_tensor(operand)) |
| 192 | { |
| 193 | if (op->outputs.size() > 1 || op->outputs[0]->consumers.size() > 1) |
| 194 | { |
| 195 | goto DEFAULT; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (op->type == "prim::Constant") |
| 200 | { |
| 201 | const Parameter& param = op->params["value"]; |
| 202 | // fprintf(stderr, "fuse_expression prim::Constant %d\n", param.type); |
| 203 | if (param.type == 0) |
| 204 | { |
| 205 | expr += "None"; |
| 206 | } |
| 207 | else if (param.type == 1) |
| 208 | { |
| 209 | expr += param.b ? "True" : "False"; |
| 210 | } |
| 211 | else if (param.type == 2) |
| 212 | { |
| 213 | char tmp[32]; |
| 214 | sprintf(tmp, "%d", param.i); |
| 215 | expr += tmp; |
| 216 | } |
| 217 | else if (param.type == 3) |
| 218 | { |
| 219 | char tmp[32]; |
| 220 | sprintf(tmp, "%e", param.f); |
| 221 | expr += tmp; |
| 222 | } |
| 223 | else if (param.type == 4) |
| 224 | { |
| 225 | expr += param.s; |
| 226 | } |
| 227 | else if (param.type == 5) |
| 228 | { |
| 229 | // ints |
| 230 | expr += "["; |
| 231 | for (int i = 0; i < (int)param.ai.size(); i++) |
| 232 | { |
| 233 | char tmp[32]; |
| 234 | sprintf(tmp, "%d", param.ai[i]); |
| 235 | expr += tmp; |
| 236 | if (i != (int)param.ai.size() - 1) |
| 237 | expr += ","; |
| 238 | } |
| 239 | expr += "]"; |
| 240 | } |
| 241 | else if (param.type == 6) |
| 242 | { |
| 243 | // floats |
| 244 | expr += "["; |
| 245 | for (int i = 0; i < (int)param.af.size(); i++) |
| 246 | { |
| 247 | char tmp[32]; |
| 248 | sprintf(tmp, "%e", param.af[i]); |
| 249 | expr += tmp; |
| 250 | if (i != (int)param.af.size() - 1) |
| 251 | expr += ","; |
| 252 | } |
| 253 | expr += "]"; |
| 254 | } |
| 255 | else if (param.type == 10) |
| 256 | { |
| 257 | char tmp[32]; |
| 258 | sprintf(tmp, "%e%+ej", param.c.real(), param.c.imag()); |
| 259 | expr += tmp; |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | goto DEFAULT; |
| 264 | } |
| 265 | } |
| 266 | else if (op->type == "pnnx.Attribute") |
| 267 | { |
| 268 | // fprintf(stderr, "operand pnnx.Attribute %s\n", operand->name.c_str()); |
| 269 | |
| 270 | const Attribute& data = op->attrs["data"]; |
| 271 | if (data.shape.size() == 1 && data.shape[0] == 1 && data.type != -1) |
| 272 | { |
| 273 | if (data.type == 0) |
| 274 | { |
| 275 | expr += "None"; |
| 276 | } |
| 277 | else if (data.type == 1) |
| 278 | { |
| 279 | char tmp[32]; |
| 280 | sprintf(tmp, "%e", ((const float*)data.data.data())[0]); |
| 281 | expr += tmp; |
| 282 | } |
| 283 | else if (data.type == 2) |
| 284 | { |
| 285 | char tmp[32]; |
| 286 | sprintf(tmp, "%e", ((const double*)data.data.data())[0]); |
| 287 | expr += tmp; |
| 288 | } |
| 289 | else if (data.type == 4) |
| 290 | { |
| 291 | char tmp[32]; |
| 292 | sprintf(tmp, "%d", ((const int*)data.data.data())[0]); |
| 293 | expr += tmp; |
| 294 | } |
| 295 | else if (data.type == 5) |
| 296 | { |
| 297 | int64_t v = ((const int64_t*)data.data.data())[0]; |
| 298 | if (v == std::numeric_limits<int64_t>::max()) v = INT_MAX; |
| 299 | if (v == std::numeric_limits<int64_t>::min()) v = INT_MIN; |
| 300 | |
| 301 | char tmp[32]; |
| 302 | sprintf(tmp, "%d", (int)v); |
| 303 | expr += tmp; |
| 304 | } |
| 305 | else if (data.type == 6) |
| 306 | { |
| 307 | char tmp[32]; |
| 308 | sprintf(tmp, "%d", ((const short*)data.data.data())[0]); |
| 309 | expr += tmp; |
| 310 | } |
| 311 | else if (data.type == 7) |
| 312 | { |
| 313 | char tmp[32]; |
| 314 | sprintf(tmp, "%d", ((const signed char*)data.data.data())[0]); |
| 315 | expr += tmp; |
| 316 | } |
| 317 | else if (data.type == 8) |
| 318 | { |
| 319 | char tmp[32]; |
| 320 | sprintf(tmp, "%u", ((const unsigned char*)data.data.data())[0]); |
| 321 | expr += tmp; |
| 322 | } |
| 323 | else if (data.type == 9) |
| 324 | { |
| 325 | expr += ((const char*)data.data.data())[0] ? "True" : "False"; |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | // unsupported type |
| 330 | fprintf(stderr, "fuse expression got unsupported scalar type %d\n", data.type); |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | goto DEFAULT; |
| 336 | } |
| 337 | } |
| 338 | else if (op->type == "torch.unbind") |
| 339 | { |
| 340 | // track chain |
| 341 | // pnnx.Attribute/foldable with 1-rank |
| 342 | // torch.unbind to constant scalar |
| 343 | Operand* operand2 = op->inputs[0]; |
| 344 | if (operand2->producer->type == "pnnx.Attribute") |
| 345 | { |
| 346 | const Attribute& data = operand2->producer->attrs["data"]; |
| 347 | |
| 348 | if (data.shape.size() == 1 && data.type != -1) |
| 349 | { |
| 350 | // resolve scalar i |
| 351 | int si = 0; |
| 352 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 353 | { |
| 354 | if (op->outputs[i] == operand) |
| 355 | { |
| 356 | si = (int)i; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (data.type == 0) |
| 362 | { |
| 363 | expr += "None"; |
| 364 | } |
| 365 | else if (data.type == 1) |
| 366 | { |
| 367 | char tmp[32]; |
| 368 | sprintf(tmp, "%e", ((const float*)data.data.data())[si]); |
| 369 | expr += tmp; |
| 370 | } |
| 371 | else if (data.type == 2) |
| 372 | { |
| 373 | char tmp[32]; |
| 374 | sprintf(tmp, "%e", ((const double*)data.data.data())[si]); |
| 375 | expr += tmp; |
| 376 | } |
| 377 | else if (data.type == 4) |
| 378 | { |
| 379 | char tmp[32]; |
| 380 | sprintf(tmp, "%d", ((const int*)data.data.data())[si]); |
| 381 | expr += tmp; |
| 382 | } |
| 383 | else if (data.type == 5) |
| 384 | { |
| 385 | int64_t v = ((const int64_t*)data.data.data())[si]; |
| 386 | if (v == std::numeric_limits<int64_t>::max()) v = INT_MAX; |
| 387 | if (v == std::numeric_limits<int64_t>::min()) v = INT_MIN; |
| 388 | |
| 389 | char tmp[32]; |
| 390 | sprintf(tmp, "%d", (int)v); |
| 391 | expr += tmp; |
| 392 | } |
| 393 | else if (data.type == 6) |
| 394 | { |
| 395 | char tmp[32]; |
| 396 | sprintf(tmp, "%d", ((const short*)data.data.data())[si]); |
| 397 | expr += tmp; |
| 398 | } |
| 399 | else if (data.type == 7) |
| 400 | { |
| 401 | char tmp[32]; |
| 402 | sprintf(tmp, "%d", ((const signed char*)data.data.data())[si]); |
| 403 | expr += tmp; |
| 404 | } |
| 405 | else if (data.type == 8) |
| 406 | { |
| 407 | char tmp[32]; |
| 408 | sprintf(tmp, "%u", ((const unsigned char*)data.data.data())[si]); |
| 409 | expr += tmp; |
| 410 | } |
| 411 | else if (data.type == 9) |
| 412 | { |
| 413 | expr += ((const char*)data.data.data())[si] ? "True" : "False"; |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | // unsupported type |
| 418 | fprintf(stderr, "fuse expression got unsupported scalar type %d\n", data.type); |
| 419 | goto DEFAULT; |
| 420 | } |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | goto DEFAULT; |
| 426 | } |
| 427 | else if (checksubgraph && operand_maybe_tensor(operand) && foldable_constants.find(operand->name) != foldable_constants.end()) |
| 428 | { |
| 429 | // fprintf(stderr, "operand_is_foldable %s\n", operand->name.c_str()); |
| 430 | |
| 431 | if (operand->shape.size() == 0 && operand->type != -1) |
| 432 | { |
| 433 | // fuse literal constant into expression |
| 434 | if (operand->type == 0) |
| 435 | { |
| 436 | expr += "None"; |
| 437 | } |
| 438 | else if (operand->type == 1) |
| 439 | { |
| 440 | float v; |
| 441 | zip.read_file(operand->name, (char*)&v); |
| 442 | |
| 443 | char tmp[32]; |
| 444 | sprintf(tmp, "%e", v); |
| 445 | expr += tmp; |
| 446 | } |
| 447 | else if (operand->type == 2) |
| 448 | { |
| 449 | double v; |
| 450 | zip.read_file(operand->name, (char*)&v); |
| 451 | |
| 452 | char tmp[32]; |
| 453 | sprintf(tmp, "%e", v); |
| 454 | expr += tmp; |
| 455 | } |
| 456 | else if (operand->type == 4) |
| 457 | { |
| 458 | int v; |
| 459 | zip.read_file(operand->name, (char*)&v); |
| 460 | |
| 461 | char tmp[32]; |
| 462 | sprintf(tmp, "%d", v); |
| 463 | expr += tmp; |
| 464 | } |
| 465 | else if (operand->type == 5) |
| 466 | { |
| 467 | int64_t v; |
| 468 | zip.read_file(operand->name, (char*)&v); |
| 469 | |
| 470 | if (v == std::numeric_limits<int64_t>::max()) v = INT_MAX; |
| 471 | if (v == std::numeric_limits<int64_t>::min()) v = INT_MIN; |
| 472 | |
| 473 | char tmp[32]; |
| 474 | sprintf(tmp, "%ld", v); |
| 475 | expr += tmp; |
| 476 | } |
| 477 | else if (operand->type == 6) |
| 478 | { |
| 479 | short v; |
| 480 | zip.read_file(operand->name, (char*)&v); |
| 481 | |
| 482 | char tmp[32]; |
| 483 | sprintf(tmp, "%d", v); |
| 484 | expr += tmp; |
| 485 | } |
| 486 | else if (operand->type == 7) |
| 487 | { |
| 488 | signed char v; |
| 489 | zip.read_file(operand->name, (char*)&v); |
| 490 | |
| 491 | char tmp[32]; |
| 492 | sprintf(tmp, "%d", v); |
| 493 | expr += tmp; |
| 494 | } |
| 495 | else if (operand->type == 8) |
| 496 | { |
| 497 | unsigned char v; |
| 498 | zip.read_file(operand->name, (char*)&v); |
| 499 | |
| 500 | char tmp[32]; |
| 501 | sprintf(tmp, "%u", v); |
| 502 | expr += tmp; |
| 503 | } |
| 504 | else if (operand->type == 9) |
| 505 | { |
| 506 | char v; |
| 507 | zip.read_file(operand->name, &v); |
| 508 | |
| 509 | expr += v ? "True" : "False"; |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | // fprintf(stderr, "unknown foldable literal %s %d\n", operand->name.c_str(), operand->type); |
| 514 | auto it = std::find(inputs.begin(), inputs.end(), operand); |
| 515 | if (it == inputs.end()) |
| 516 | { |
| 517 | // tensor |
| 518 | char tmp[32]; |
| 519 | sprintf(tmp, "@%d", (int)inputs.size()); |
| 520 | expr += tmp; |
| 521 | |
| 522 | inputs.push_back(operand); |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | // tensor |
| 527 | char tmp[32]; |
| 528 | sprintf(tmp, "@%d", (int)(it - inputs.begin())); |
| 529 | expr += tmp; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | goto DEFAULT; |
| 536 | } |
| 537 | } |
| 538 | else if (op->type == "prim::NumToTensor") |
| 539 | { |
| 540 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 541 | } |
| 542 | else if (op->type == "prim::ListConstruct") |
| 543 | { |
| 544 | expr += "["; |
| 545 | for (int i = 0; i < (int)op->inputs.size() - 1; i++) |
| 546 | { |
| 547 | fuse_expression(graph, op->inputs[i], expr, inputs, foldable_constants, zip); |
| 548 | expr += ","; |
| 549 | } |
| 550 | if (op->inputs.size() > 0) |
| 551 | { |
| 552 | fuse_expression(graph, op->inputs[op->inputs.size() - 1], expr, inputs, foldable_constants, zip); |
| 553 | } |
| 554 | expr += "]"; |
| 555 | } |
| 556 | else if (op->type == "aten::size") |
| 557 | { |
| 558 | if (op->inputs.size() == 1) |
| 559 | { |
| 560 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 561 | } |
| 562 | else // if (op->inputs.size() == 2) |
| 563 | { |
| 564 | expr += "size("; |
| 565 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 566 | expr += ","; |
| 567 | fuse_expression(graph, op->inputs[1], expr, inputs, foldable_constants, zip); |
| 568 | expr += ")"; |
| 569 | } |
| 570 | } |
| 571 | else if (op->type == "Tensor.slice" && !operand_maybe_tensor(operand)) |
| 572 | { |
| 573 | int start = op->inputs.size() == 3 ? op->inputs[1]->producer->params.at("value").i : op->inputs[2]->producer->params.at("value").i; |
| 574 | int end = op->inputs.size() == 3 ? op->inputs[2]->producer->params.at("value").i : op->inputs[3]->producer->params.at("value").i; |
| 575 | |
| 576 | // onnx style shape + slice chain |
| 577 | const Operator* op_shape = op->inputs[0]->producer; |
| 578 | for (int i = start; i < end; i++) |
| 579 | { |
| 580 | expr += "size("; |
| 581 | fuse_expression(graph, op_shape->inputs[0], expr, inputs, foldable_constants, zip); |
| 582 | expr += ","; |
| 583 | expr += std::to_string(i); |
| 584 | expr += ")"; |
| 585 | if (i + 1 != end) |
| 586 | expr += ","; |
| 587 | } |
| 588 | } |
| 589 | else if (op->type == "aten::Int") |
| 590 | { |
| 591 | expr += "int("; |
| 592 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 593 | expr += ")"; |
| 594 | } |
| 595 | else if (op->type == "Tensor.to") |
| 596 | { |
| 597 | bool noop_type_cast = (op->outputs[0]->type != -1) && (op->inputs[0]->type == op->outputs[0]->type); |
| 598 | if (noop_type_cast) |
| 599 | { |
| 600 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 601 | } |
| 602 | else if (!operand_maybe_tensor(operand)) |
| 603 | { |
| 604 | std::string dtype = op->params.at("dtype").s; |
| 605 | |
| 606 | // torch.xxx |
| 607 | expr += dtype + "("; |
| 608 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 609 | expr += ")"; |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | goto DEFAULT; |
| 614 | } |
| 615 | } |
| 616 | else if (op->type == "aten::detach" || op->type == "aten::ScalarImplicit") |
| 617 | { |
| 618 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 619 | } |
| 620 | else if (op->type == "aten::abs" |
| 621 | || op->type == "aten::acos" |
| 622 | || op->type == "aten::acosh" |
| 623 | || op->type == "aten::asin" |
| 624 | || op->type == "aten::asinh" |
| 625 | || op->type == "aten::atan" |
| 626 | || op->type == "aten::atanh" |
| 627 | || op->type == "aten::ceil" |
| 628 | || op->type == "aten::cos" |
| 629 | || op->type == "aten::cosh" |
| 630 | || op->type == "aten::exp" |
| 631 | || op->type == "aten::floor" |
| 632 | || op->type == "aten::log" |
| 633 | || op->type == "aten::log10" |
| 634 | || op->type == "aten::neg" |
| 635 | || op->type == "aten::reciprocal" |
| 636 | || op->type == "aten::round" |
| 637 | || op->type == "aten::rsqrt" |
| 638 | || op->type == "aten::sign" |
| 639 | || op->type == "aten::sin" |
| 640 | || op->type == "aten::sinh" |
| 641 | || op->type == "aten::sqrt" |
| 642 | || op->type == "aten::square" |
| 643 | || op->type == "aten::tan" |
| 644 | || op->type == "aten::tanh" |
| 645 | || op->type == "aten::trunc") |
| 646 | { |
| 647 | std::string mathop = op->type.substr(6); |
| 648 | |
| 649 | expr += mathop; |
| 650 | expr += "("; |
| 651 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 652 | expr += ")"; |
| 653 | } |
| 654 | else if (op->type == "aten::atan2" |
| 655 | || op->type == "aten::floor_divide" |
| 656 | || op->type == "aten::fmod" |
| 657 | || op->type == "aten::logaddexp" |
| 658 | || op->type == "aten::max" |
| 659 | || op->type == "aten::maximum" |
| 660 | || op->type == "aten::min" |
| 661 | || op->type == "aten::minimum" |
| 662 | || op->type == "aten::mul" |
| 663 | || op->type == "aten::pow" |
| 664 | || op->type == "aten::remainder") |
| 665 | { |
| 666 | std::string mathop = op->type.substr(6); |
| 667 | |
| 668 | expr += mathop; |
| 669 | expr += "("; |
| 670 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 671 | expr += ","; |
| 672 | fuse_expression(graph, op->inputs[1], expr, inputs, foldable_constants, zip); |
| 673 | expr += ")"; |
| 674 | } |
| 675 | else if (op->type == "aten::__and__" || op->type == "aten::__or__" || op->type == "aten::__xor__" || op->type == "aten::__lshift__" || op->type == "aten::__rshift__") |
| 676 | { |
| 677 | std::string mathop = op->type.substr(8, op->type.size() - 10); |
| 678 | |
| 679 | expr += mathop; |
| 680 | expr += "("; |
| 681 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 682 | expr += ","; |
| 683 | fuse_expression(graph, op->inputs[1], expr, inputs, foldable_constants, zip); |
| 684 | expr += ")"; |
| 685 | } |
| 686 | else if (op->type == "aten::add" || op->type == "aten::sub") |
| 687 | { |
| 688 | std::string mathop = op->type.substr(6); |
| 689 | |
| 690 | expr += mathop; |
| 691 | expr += "("; |
| 692 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 693 | expr += ","; |
| 694 | |
| 695 | std::string expr1; |
| 696 | fuse_expression(graph, op->inputs[1], expr1, inputs, foldable_constants, zip); |
| 697 | |
| 698 | if (op->inputs.size() == 2) |
| 699 | { |
| 700 | expr += expr1; |
| 701 | } |
| 702 | else // if (op->inputs.size() == 3) |
| 703 | { |
| 704 | std::string expr2; |
| 705 | fuse_expression(graph, op->inputs[2], expr2, inputs, foldable_constants, zip); |
| 706 | |
| 707 | if (expr2 == "1") |
| 708 | { |
| 709 | expr += expr1; |
| 710 | } |
| 711 | else |
| 712 | { |
| 713 | expr += ","; |
| 714 | expr += "mul("; |
| 715 | expr += expr1; |
| 716 | expr += ","; |
| 717 | expr += expr2; |
| 718 | expr += ")"; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | expr += ")"; |
| 723 | } |
| 724 | else if (op->type == "aten::rsub") |
| 725 | { |
| 726 | expr += "sub("; |
| 727 | std::string expr1; |
| 728 | fuse_expression(graph, op->inputs[1], expr1, inputs, foldable_constants, zip); |
| 729 | |
| 730 | if (op->inputs.size() == 2) |
| 731 | { |
| 732 | expr += expr1; |
| 733 | } |
| 734 | else // if (op->inputs.size() == 3) |
| 735 | { |
| 736 | std::string expr2; |
| 737 | fuse_expression(graph, op->inputs[2], expr2, inputs, foldable_constants, zip); |
| 738 | |
| 739 | if (expr2 == "1") |
| 740 | { |
| 741 | expr += expr1; |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | expr += ","; |
| 746 | expr += "mul("; |
| 747 | expr += expr1; |
| 748 | expr += ","; |
| 749 | expr += expr2; |
| 750 | expr += ")"; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | expr += ","; |
| 755 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 756 | expr += ")"; |
| 757 | } |
| 758 | else if (op->type == "aten::div") |
| 759 | { |
| 760 | std::string rounding_mode; |
| 761 | if (op->inputs.size() == 3) |
| 762 | fuse_expression(graph, op->inputs[2], rounding_mode, inputs, foldable_constants, zip); |
| 763 | |
| 764 | if (rounding_mode == "trunc") |
| 765 | { |
| 766 | expr += "floor_divide"; |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | expr += "div"; |
| 771 | } |
| 772 | |
| 773 | expr += "("; |
| 774 | fuse_expression(graph, op->inputs[0], expr, inputs, foldable_constants, zip); |
| 775 | expr += ","; |
| 776 | fuse_expression(graph, op->inputs[1], expr, inputs, foldable_constants, zip); |
| 777 | expr += ")"; |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | goto DEFAULT; |
| 782 | } |
| 783 | |
| 784 | return; |
| 785 | |
| 786 | DEFAULT: |
| 787 | auto it = std::find(inputs.begin(), inputs.end(), operand); |
| 788 | if (it == inputs.end()) |
| 789 | { |
| 790 | // tensor |
| 791 | char tmp[32]; |
| 792 | sprintf(tmp, "@%d", (int)inputs.size()); |
| 793 | expr += tmp; |
| 794 | |
| 795 | inputs.push_back(operand); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | // tensor |
| 800 | char tmp[32]; |
| 801 | sprintf(tmp, "@%d", (int)(it - inputs.begin())); |
| 802 | expr += tmp; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | void fuse_expression(Graph& graph, const std::set<std::string>& foldable_constants, const std::string& foldable_constants_zippath) |
| 807 | { |
| 808 | StoreZipReader zip; |
| 809 | zip.open(foldable_constants_zippath); |
| 810 | |
| 811 | int pnnx_expr_index = 0; |
| 812 | |
| 813 | for (;;) |
| 814 | { |
| 815 | bool need_fuse = false; |
| 816 | |
| 817 | // build expression via reverse order |
| 818 | for (int i = (int)graph.ops.size() - 1; i >= 0; i--) |
| 819 | { |
| 820 | Operator* op = graph.ops[i]; |
| 821 | |
| 822 | if (op->type == "prim::Constant") |
| 823 | { |
| 824 | need_fuse = true; |
| 825 | } |
| 826 | if (op->type == "prim::NumToTensor") |
| 827 | { |
| 828 | need_fuse = true; |
| 829 | } |
| 830 | if (op->type == "prim::ListConstruct") |
| 831 | { |
| 832 | need_fuse = true; |
| 833 | } |
| 834 | if (op->type == "aten::size") |
| 835 | { |
| 836 | need_fuse = true; |
| 837 | } |
| 838 | if (op->type == "aten::Int") |
| 839 | { |
| 840 | need_fuse = true; |
| 841 | } |
| 842 | if (op->type == "Tensor.to") |
| 843 | { |
| 844 | // fuse noop type cast only |
| 845 | bool noop_to = (op->outputs[0]->type != -1) && (op->inputs[0]->type == op->outputs[0]->type); |
| 846 | bool is_scalar = !operand_maybe_tensor(op->outputs[0]); |
| 847 | need_fuse = noop_to || is_scalar; |
| 848 | } |
| 849 | if (op->type == "aten::detach" || op->type == "aten::ScalarImplicit") |
| 850 | { |
| 851 | need_fuse = true; |
| 852 | } |
| 853 | if (op->type == "aten::abs" |
| 854 | || op->type == "aten::acos" |
| 855 | || op->type == "aten::acosh" |
| 856 | || op->type == "aten::add" |
| 857 | || op->type == "aten::asin" |
| 858 | || op->type == "aten::asinh" |
| 859 | || op->type == "aten::atan" |
| 860 | || op->type == "aten::atanh" |
| 861 | || op->type == "aten::atan2" |
| 862 | || op->type == "aten::ceil" |
| 863 | || op->type == "aten::cos" |
| 864 | || op->type == "aten::cosh" |
| 865 | || op->type == "aten::div" |
| 866 | || op->type == "aten::exp" |
| 867 | || op->type == "aten::floor" |
| 868 | || op->type == "aten::floor_divide" |
| 869 | || op->type == "aten::fmod" |
| 870 | || op->type == "aten::log" |
| 871 | || op->type == "aten::log10" |
| 872 | || op->type == "aten::logaddexp" |
| 873 | || op->type == "aten::max" |
| 874 | || op->type == "aten::maximum" |
| 875 | || op->type == "aten::min" |
| 876 | || op->type == "aten::minimum" |
| 877 | || op->type == "aten::mul" |
| 878 | || op->type == "aten::neg" |
| 879 | || op->type == "aten::pow" |
| 880 | || op->type == "aten::reciprocal" |
| 881 | || op->type == "aten::remainder" |
| 882 | || op->type == "aten::round" |
| 883 | || op->type == "aten::rsqrt" |
| 884 | || op->type == "aten::rsub" |
| 885 | || op->type == "aten::sign" |
| 886 | || op->type == "aten::sin" |
| 887 | || op->type == "aten::sinh" |
| 888 | || op->type == "aten::sqrt" |
| 889 | || op->type == "aten::square" |
| 890 | || op->type == "aten::sub" |
| 891 | || op->type == "aten::tan" |
| 892 | || op->type == "aten::tanh" |
| 893 | || op->type == "aten::trunc") |
| 894 | { |
| 895 | need_fuse = true; |
| 896 | } |
| 897 | if (op->type == "aten::__and__" || op->type == "aten::__or__" || op->type == "aten::__xor__" || op->type == "aten::__lshift__" || op->type == "aten::__rshift__") |
| 898 | { |
| 899 | need_fuse = true; |
| 900 | } |
| 901 | |
| 902 | if (need_fuse) |
| 903 | { |
| 904 | std::string expr; |
| 905 | std::vector<Operand*> inputs; |
| 906 | fuse_expression(graph, op->outputs[0], expr, inputs, foldable_constants, zip, false); |
| 907 | // fprintf(stderr, "expr = %s\n", expr.c_str()); |
| 908 | |
| 909 | // lets rewrite graph |
| 910 | char name[32]; |
| 911 | sprintf(name, "pnnx_expr_%d", pnnx_expr_index++); |
| 912 | |
| 913 | op->type = "pnnx.Expression"; |
| 914 | op->name = name; |
| 915 | |
| 916 | op->params.clear(); |
| 917 | op->attrs.clear(); |
| 918 | |
| 919 | op->params["expr"] = expr; |
| 920 | |
| 921 | // fix input output |
| 922 | for (Operand* operand : op->inputs) |
| 923 | { |
| 924 | operand->consumers.erase(std::find(operand->consumers.begin(), operand->consumers.end(), op)); |
| 925 | } |
| 926 | |
| 927 | op->inputs = inputs; |
| 928 | |
| 929 | for (Operand* operand : op->inputs) |
| 930 | { |
| 931 | operand->consumers.push_back(op); |
| 932 | } |
| 933 | |
| 934 | break; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | if (!need_fuse) |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | zip.close(); |
| 943 | } |
| 944 | |
| 945 | } // namespace pnnx |