| 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 "eval_expression.h" |
| 16 | |
| 17 | #include <fenv.h> |
| 18 | #include <float.h> |
| 19 | #include <math.h> |
| 20 | |
| 21 | #include <iostream> |
| 22 | #include <sstream> |
| 23 | #include <algorithm> |
| 24 | #include <stack> |
| 25 | #include <vector> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace pnnx { |
| 29 | |
| 30 | static bool token_is_argument(const std::string& t) |
| 31 | { |
| 32 | if (t[0] != '@' || t.size() < 2) |
| 33 | return false; |
| 34 | |
| 35 | for (size_t i = 1; i < t.size(); i++) |
| 36 | { |
| 37 | if (t[i] < '0' || t[i] > '9') |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | static bool token_is_complex(const std::string& t) |
| 45 | { |
| 46 | // 2.000000e+00+3.000000e+00j |
| 47 | if (t[t.size() - 1] != 'j') |
| 48 | return false; |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | static bool token_is_literal(const std::string& t) |
| 54 | { |
| 55 | if (token_is_complex(t)) |
| 56 | return true; |
| 57 | |
| 58 | std::istringstream iss(t); |
| 59 | float f; |
| 60 | iss >> std::noskipws >> f; |
| 61 | return iss.eof() && !iss.fail(); |
| 62 | } |
| 63 | |
| 64 | static bool token_is_interger_literal(const std::string& t) |
| 65 | { |
| 66 | std::istringstream iss(t); |
| 67 | int f; |
| 68 | iss >> std::noskipws >> f; |
| 69 | return iss.eof() && !iss.fail(); |
| 70 | } |
| 71 | |
| 72 | static std::string eval_expression(const Operator* op) |
| 73 | { |
| 74 | std::string expr = op->params.at("expr").s; |
| 75 | |
| 76 | // fprintf(stderr, "eval_expression %s\n", expr.c_str()); |
| 77 | |
| 78 | // split into tokens |
| 79 | std::vector<std::string> tokens; |
| 80 | { |
| 81 | std::string t; |
| 82 | for (size_t i = 0; i < expr.size(); i++) |
| 83 | { |
| 84 | char ch = expr[i]; |
| 85 | |
| 86 | if (ch == '[') // list |
| 87 | { |
| 88 | t += ch; |
| 89 | tokens.push_back(t); |
| 90 | t.clear(); |
| 91 | } |
| 92 | else if (ch == '(' || ch == ')' || ch == ',' || ch == ']') |
| 93 | { |
| 94 | if (!t.empty()) |
| 95 | { |
| 96 | tokens.push_back(t); |
| 97 | t.clear(); |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | t += ch; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!t.empty()) |
| 107 | { |
| 108 | tokens.push_back(t); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // scan and stack |
| 113 | std::stack<std::string> exprstack; |
| 114 | for (int i = (int)tokens.size() - 1; i >= 0; i--) |
| 115 | { |
| 116 | const std::string& t = tokens[i]; |
| 117 | |
| 118 | if (t == "size") |
| 119 | { |
| 120 | std::string a = exprstack.top(); |
| 121 | exprstack.pop(); |
| 122 | |
| 123 | if (exprstack.empty()) |
| 124 | { |
| 125 | std::string r = std::string("size(") + a + ")"; |
| 126 | exprstack.push(r); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | std::string b = exprstack.top(); |
| 131 | exprstack.pop(); |
| 132 | |
| 133 | if (token_is_argument(a) && token_is_literal(b)) |
| 134 | { |
| 135 | int input_index = std::stoi(a.substr(1)); |
| 136 | if (op->inputs[input_index]->shape.empty()) |
| 137 | { |
| 138 | std::string r = std::string("size(") + a + "," + b + ")"; |
| 139 | exprstack.push(r); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | int bi = std::stoi(b); |
| 144 | if (bi < 0) |
| 145 | bi = op->inputs[input_index]->shape.size() + bi; |
| 146 | int r = op->inputs[input_index]->shape[bi]; |
| 147 | if (r == -1) |
| 148 | { |
| 149 | // do not evaluate dynamic size info as -1 |
| 150 | // just keep the size expression |
| 151 | std::string r = std::string("size(") + a + "," + b + ")"; |
| 152 | exprstack.push(r); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | exprstack.push(std::to_string(r)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | std::string r = std::string("size(") + a + "," + b + ")"; |
| 163 | exprstack.push(r); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | else if (t == "int" |
| 168 | || t == "abs" |
| 169 | || t == "acos" |
| 170 | || t == "acosh" |
| 171 | || t == "asin" |
| 172 | || t == "asinh" |
| 173 | || t == "atan" |
| 174 | || t == "atanh" |
| 175 | || t == "ceil" |
| 176 | || t == "cos" |
| 177 | || t == "cosh" |
| 178 | || t == "exp" |
| 179 | || t == "floor" |
| 180 | || t == "log" |
| 181 | || t == "log10" |
| 182 | || t == "neg" |
| 183 | || t == "reciprocal" |
| 184 | || t == "round" |
| 185 | || t == "rsqrt" |
| 186 | || t == "sign" |
| 187 | || t == "sin" |
| 188 | || t == "sinh" |
| 189 | || t == "sqrt" |
| 190 | || t == "square" |
| 191 | || t == "tan" |
| 192 | || t == "tanh" |
| 193 | || t == "trunc" |
| 194 | || t == "torch.bool" |
| 195 | || t == "torch.float" |
| 196 | || t == "torch.long") |
| 197 | { |
| 198 | std::string a = exprstack.top(); |
| 199 | exprstack.pop(); |
| 200 | |
| 201 | if (token_is_literal(a)) |
| 202 | { |
| 203 | float af = std::stof(a); |
| 204 | |
| 205 | if (t == "int") |
| 206 | { |
| 207 | int r = int(af); |
| 208 | if (token_is_interger_literal(a)) |
| 209 | { |
| 210 | r = std::stoi(a); |
| 211 | } |
| 212 | |
| 213 | exprstack.push(std::to_string(r)); |
| 214 | } |
| 215 | if (t == "abs") |
| 216 | { |
| 217 | float r = abs(af); |
| 218 | exprstack.push(std::to_string(r)); |
| 219 | } |
| 220 | if (t == "acos") |
| 221 | { |
| 222 | float r = acos(af); |
| 223 | exprstack.push(std::to_string(r)); |
| 224 | } |
| 225 | if (t == "acosh") |
| 226 | { |
| 227 | float r = acosh(af); |
| 228 | exprstack.push(std::to_string(r)); |
| 229 | } |
| 230 | if (t == "asin") |
| 231 | { |
| 232 | float r = asin(af); |
| 233 | exprstack.push(std::to_string(r)); |
| 234 | } |
| 235 | if (t == "asinh") |
| 236 | { |
| 237 | float r = asinh(af); |
| 238 | exprstack.push(std::to_string(r)); |
| 239 | } |
| 240 | if (t == "atan") |
| 241 | { |
| 242 | float r = atan(af); |
| 243 | exprstack.push(std::to_string(r)); |
| 244 | } |
| 245 | if (t == "atanh") |
| 246 | { |
| 247 | float r = atanh(af); |
| 248 | exprstack.push(std::to_string(r)); |
| 249 | } |
| 250 | if (t == "ceil") |
| 251 | { |
| 252 | float r = ceil(af); |
| 253 | exprstack.push(std::to_string(r)); |
| 254 | } |
| 255 | if (t == "cos") |
| 256 | { |
| 257 | float r = cos(af); |
| 258 | exprstack.push(std::to_string(r)); |
| 259 | } |
| 260 | if (t == "cosh") |
| 261 | { |
| 262 | float r = cosh(af); |
| 263 | exprstack.push(std::to_string(r)); |
| 264 | } |
| 265 | if (t == "exp") |
| 266 | { |
| 267 | float r = exp(af); |
| 268 | exprstack.push(std::to_string(r)); |
| 269 | } |
| 270 | if (t == "floor") |
| 271 | { |
| 272 | float r = floor(af); |
| 273 | exprstack.push(std::to_string(r)); |
| 274 | } |
| 275 | if (t == "log") |
| 276 | { |
| 277 | float r = log(af); |
| 278 | exprstack.push(std::to_string(r)); |
| 279 | } |
| 280 | if (t == "log10") |
| 281 | { |
| 282 | float r = log10(af); |
| 283 | exprstack.push(std::to_string(r)); |
| 284 | } |
| 285 | if (t == "neg") |
| 286 | { |
| 287 | float r = -af; |
| 288 | exprstack.push(std::to_string(r)); |
| 289 | } |
| 290 | if (t == "reciprocal") |
| 291 | { |
| 292 | float r = 1.f / af; |
| 293 | exprstack.push(std::to_string(r)); |
| 294 | } |
| 295 | if (t == "round") |
| 296 | { |
| 297 | // round to nearest even |
| 298 | int old_rm = fegetround(); |
| 299 | fesetround(FE_TONEAREST); |
| 300 | float r = nearbyintf(af); |
| 301 | fesetround(old_rm); |
| 302 | exprstack.push(std::to_string(r)); |
| 303 | } |
| 304 | if (t == "rsqrt") |
| 305 | { |
| 306 | float r = 1.f / sqrt(af); |
| 307 | exprstack.push(std::to_string(r)); |
| 308 | } |
| 309 | if (t == "sign") |
| 310 | { |
| 311 | float r = af > 0.f ? 1.f : (af == 0.f ? 0.f : -1.f); |
| 312 | exprstack.push(std::to_string(r)); |
| 313 | } |
| 314 | if (t == "sin") |
| 315 | { |
| 316 | float r = sin(af); |
| 317 | exprstack.push(std::to_string(r)); |
| 318 | } |
| 319 | if (t == "sinh") |
| 320 | { |
| 321 | float r = sinh(af); |
| 322 | exprstack.push(std::to_string(r)); |
| 323 | } |
| 324 | if (t == "sqrt") |
| 325 | { |
| 326 | float r = sqrt(af); |
| 327 | exprstack.push(std::to_string(r)); |
| 328 | } |
| 329 | if (t == "square") |
| 330 | { |
| 331 | float r = af * af; |
| 332 | exprstack.push(std::to_string(r)); |
| 333 | } |
| 334 | if (t == "tan") |
| 335 | { |
| 336 | float r = tan(af); |
| 337 | exprstack.push(std::to_string(r)); |
| 338 | } |
| 339 | if (t == "tanh") |
| 340 | { |
| 341 | float r = tanh(af); |
| 342 | exprstack.push(std::to_string(r)); |
| 343 | } |
| 344 | if (t == "trunc") |
| 345 | { |
| 346 | float r = trunc(af); |
| 347 | exprstack.push(std::to_string(r)); |
| 348 | } |
| 349 | if (t == "torch.bool") |
| 350 | { |
| 351 | int r = int(af); |
| 352 | if (token_is_interger_literal(a)) |
| 353 | { |
| 354 | r = std::stoi(a); |
| 355 | } |
| 356 | |
| 357 | exprstack.push(r == 0 ? "False" : "True"); |
| 358 | } |
| 359 | if (t == "torch.float") |
| 360 | { |
| 361 | float r = af; |
| 362 | exprstack.push(std::to_string(r)); |
| 363 | } |
| 364 | if (t == "torch.long") |
| 365 | { |
| 366 | long r = long(af); |
| 367 | if (token_is_interger_literal(a)) |
| 368 | { |
| 369 | r = std::stol(a); |
| 370 | } |
| 371 | |
| 372 | exprstack.push(std::to_string(r)); |
| 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | std::string r = t + "(" + a + ")"; |
| 378 | exprstack.push(r); |
| 379 | } |
| 380 | } |
| 381 | else if (t == "atan2" |
| 382 | || t == "add" |
| 383 | || t == "sub" |
| 384 | || t == "max" |
| 385 | || t == "maximum" |
| 386 | || t == "min" |
| 387 | || t == "minimum" |
| 388 | || t == "mul" |
| 389 | || t == "div" |
| 390 | || t == "floor_divide" |
| 391 | || t == "fmod" |
| 392 | || t == "pow" |
| 393 | || t == "remainder" |
| 394 | || t == "logaddexp") |
| 395 | { |
| 396 | std::string a = exprstack.top(); |
| 397 | exprstack.pop(); |
| 398 | std::string b = exprstack.top(); |
| 399 | exprstack.pop(); |
| 400 | |
| 401 | if (token_is_literal(a) && token_is_literal(b)) |
| 402 | { |
| 403 | float af = std::stof(a); |
| 404 | float bf = std::stof(b); |
| 405 | |
| 406 | if (t == "atan2") |
| 407 | { |
| 408 | float r = atan2(af, bf); |
| 409 | exprstack.push(std::to_string(r)); |
| 410 | } |
| 411 | if (t == "add") |
| 412 | { |
| 413 | float r = af + bf; |
| 414 | exprstack.push(std::to_string(r)); |
| 415 | } |
| 416 | if (t == "sub") |
| 417 | { |
| 418 | float r = af - bf; |
| 419 | exprstack.push(std::to_string(r)); |
| 420 | } |
| 421 | if (t == "max" || t == "maximum") |
| 422 | { |
| 423 | float r = std::max(af, bf); |
| 424 | exprstack.push(std::to_string(r)); |
| 425 | } |
| 426 | if (t == "minimum") |
| 427 | { |
| 428 | float r = std::min(af, bf); |
| 429 | exprstack.push(std::to_string(r)); |
| 430 | } |
| 431 | if (t == "mul") |
| 432 | { |
| 433 | float r = af * bf; |
| 434 | exprstack.push(std::to_string(r)); |
| 435 | } |
| 436 | if (t == "div") |
| 437 | { |
| 438 | float r = af / bf; |
| 439 | exprstack.push(std::to_string(r)); |
| 440 | } |
| 441 | if (t == "fmod") |
| 442 | { |
| 443 | float r = fmod(af, bf); |
| 444 | exprstack.push(std::to_string(r)); |
| 445 | } |
| 446 | if (t == "floor_divide") |
| 447 | { |
| 448 | int r = (int)af / (int)bf; |
| 449 | exprstack.push(std::to_string(r)); |
| 450 | } |
| 451 | if (t == "pow") |
| 452 | { |
| 453 | float r = pow(af, bf); |
| 454 | exprstack.push(std::to_string(r)); |
| 455 | } |
| 456 | if (t == "remainder") |
| 457 | { |
| 458 | float r = fmod(af, bf); |
| 459 | if (af * bf < 0) |
| 460 | r += bf; |
| 461 | exprstack.push(std::to_string(r)); |
| 462 | } |
| 463 | if (t == "logaddexp") |
| 464 | { |
| 465 | float r = log(exp(af) + exp(bf)); |
| 466 | exprstack.push(std::to_string(r)); |
| 467 | } |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | std::string r = t + "(" + a + "," + b + ")"; |
| 472 | exprstack.push(r); |
| 473 | } |
| 474 | } |
| 475 | else if (t == "and" || t == "or" || t == "xor" || t == "lshift" || t == "rshift") |
| 476 | { |
| 477 | std::string a = exprstack.top(); |
| 478 | exprstack.pop(); |
| 479 | std::string b = exprstack.top(); |
| 480 | exprstack.pop(); |
| 481 | |
| 482 | if (token_is_interger_literal(a) && token_is_interger_literal(b)) |
| 483 | { |
| 484 | int ai = std::stoi(a); |
| 485 | int bi = std::stoi(b); |
| 486 | |
| 487 | if (t == "and") |
| 488 | { |
| 489 | int r = ai & bi; |
| 490 | exprstack.push(std::to_string(r)); |
| 491 | } |
| 492 | if (t == "or") |
| 493 | { |
| 494 | int r = ai | bi; |
| 495 | exprstack.push(std::to_string(r)); |
| 496 | } |
| 497 | if (t == "xor") |
| 498 | { |
| 499 | int r = ai ^ bi; |
| 500 | exprstack.push(std::to_string(r)); |
| 501 | } |
| 502 | if (t == "lshift") |
| 503 | { |
| 504 | int r = ai << bi; |
| 505 | exprstack.push(std::to_string(r)); |
| 506 | } |
| 507 | if (t == "rshift") |
| 508 | { |
| 509 | int r = ai >> bi; |
| 510 | exprstack.push(std::to_string(r)); |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | std::string r = t + "(" + a + "," + b + ")"; |
| 516 | exprstack.push(r); |
| 517 | } |
| 518 | } |
| 519 | else if (t == "[") // list |
| 520 | { |
| 521 | std::vector<std::string> elements; |
| 522 | while (!exprstack.empty()) |
| 523 | { |
| 524 | std::string a = exprstack.top(); |
| 525 | exprstack.pop(); |
| 526 | |
| 527 | elements.push_back(a); |
| 528 | } |
| 529 | |
| 530 | std::string r = "["; |
| 531 | for (int j = 0; j < (int)elements.size() - 1; j++) |
| 532 | { |
| 533 | r += elements[j]; |
| 534 | if (j + 1 != (int)elements.size()) |
| 535 | r += ","; |
| 536 | } |
| 537 | if (!elements.empty()) |
| 538 | { |
| 539 | r += elements[elements.size() - 1]; |
| 540 | } |
| 541 | r += "]"; |
| 542 | |
| 543 | exprstack.push(r); |
| 544 | } |
| 545 | else if (t[0] == '@') |
| 546 | { |
| 547 | exprstack.push(t); |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | // literal |
| 552 | exprstack.push(t); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | std::string r = exprstack.top(); |
| 557 | exprstack.pop(); |
| 558 | while (!exprstack.empty()) |
| 559 | { |
| 560 | r += std::string(",") + exprstack.top(); |
| 561 | exprstack.pop(); |
| 562 | } |
| 563 | |
| 564 | // fprintf(stderr, "eval_expression return %s\n", r.c_str()); |
| 565 | |
| 566 | return r; |
| 567 | } |
| 568 | |
| 569 | static std::string canonicalize_arguments(const Operator* op, std::vector<Operand*>& inputs) |
| 570 | { |
| 571 | std::string expr = op->params.at("expr").s; |
| 572 | |
| 573 | // split into tokens |
| 574 | std::vector<std::string> tokens; |
| 575 | { |
| 576 | std::string t; |
| 577 | for (size_t i = 0; i < expr.size(); i++) |
| 578 | { |
| 579 | char ch = expr[i]; |
| 580 | |
| 581 | if (ch == '[') // list |
| 582 | { |
| 583 | t += ch; |
| 584 | tokens.push_back(t); |
| 585 | t.clear(); |
| 586 | } |
| 587 | else if (ch == '(' || ch == ')' || ch == ',' || ch == ']') |
| 588 | { |
| 589 | if (!t.empty()) |
| 590 | { |
| 591 | tokens.push_back(t); |
| 592 | t.clear(); |
| 593 | } |
| 594 | |
| 595 | t += ch; |
| 596 | tokens.push_back(t); |
| 597 | t.clear(); |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | t += ch; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (!t.empty()) |
| 606 | { |
| 607 | tokens.push_back(t); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | std::string r; |
| 612 | for (auto t : tokens) |
| 613 | { |
| 614 | if (t[0] == '@') |
| 615 | { |
| 616 | int input_index = std::stoi(t.substr(1)); |
| 617 | Operand* operand = op->inputs[input_index]; |
| 618 | |
| 619 | int new_input_index; |
| 620 | |
| 621 | auto it = std::find(inputs.begin(), inputs.end(), operand); |
| 622 | if (it == inputs.end()) |
| 623 | { |
| 624 | new_input_index = inputs.size(); |
| 625 | inputs.push_back(operand); |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | new_input_index = it - inputs.begin(); |
| 630 | } |
| 631 | r += std::string("@") + std::to_string(new_input_index); |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | r += t; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | // fprintf(stderr, "canonicalize_arguments return %s\n", r.c_str()); |
| 640 | |
| 641 | return r; |
| 642 | } |
| 643 | |
| 644 | void eval_expression(Graph& graph) |
| 645 | { |
| 646 | for (Operator* op : graph.ops) |
| 647 | { |
| 648 | if (op->type != "pnnx.Expression") |
| 649 | continue; |
| 650 | |
| 651 | std::string expr_eval = eval_expression(op); |
| 652 | |
| 653 | op->params["expr"] = expr_eval; |
| 654 | |
| 655 | std::vector<Operand*> inputs; |
| 656 | std::string expr_canonicalize = canonicalize_arguments(op, inputs); |
| 657 | |
| 658 | op->params["expr"] = expr_canonicalize; |
| 659 | |
| 660 | for (auto r : op->inputs) |
| 661 | { |
| 662 | r->remove_consumer(op); |
| 663 | } |
| 664 | |
| 665 | for (auto r : inputs) |
| 666 | { |
| 667 | r->consumers.push_back(op); |
| 668 | } |
| 669 | |
| 670 | op->inputs = inputs; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | } // namespace pnnx |