| 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 "ir.h" |
| 16 | |
| 17 | #include <limits.h> |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <algorithm> |
| 21 | #include <fstream> |
| 22 | #include <sstream> |
| 23 | #include <string> |
| 24 | #include <stack> |
| 25 | |
| 26 | #include "storezip.h" |
| 27 | #include "utils.h" |
| 28 | |
| 29 | namespace pnnx { |
| 30 | |
| 31 | static bool type_is_integer(int type) |
| 32 | { |
| 33 | if (type == 1) return false; |
| 34 | if (type == 2) return false; |
| 35 | if (type == 3) return false; |
| 36 | if (type == 4) return true; |
| 37 | if (type == 5) return true; |
| 38 | if (type == 6) return true; |
| 39 | if (type == 7) return true; |
| 40 | if (type == 8) return true; |
| 41 | if (type == 9) return true; |
| 42 | if (type == 10) return false; |
| 43 | if (type == 11) return false; |
| 44 | if (type == 12) return false; |
| 45 | if (type == 13) return false; |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | static const char* type_to_string(int type) |
| 50 | { |
| 51 | if (type == 1) return "f32"; |
| 52 | if (type == 2) return "f64"; |
| 53 | if (type == 3) return "f16"; |
| 54 | if (type == 4) return "i32"; |
| 55 | if (type == 5) return "i64"; |
| 56 | if (type == 6) return "i16"; |
| 57 | if (type == 7) return "i8"; |
| 58 | if (type == 8) return "u8"; |
| 59 | if (type == 9) return "bool"; |
| 60 | if (type == 10) return "c64"; |
| 61 | if (type == 11) return "c128"; |
| 62 | if (type == 12) return "c32"; |
| 63 | if (type == 13) return "bf16"; |
| 64 | return "null"; |
| 65 | } |
| 66 | |
| 67 | static const char* type_to_numpy_string(int type) |
| 68 | { |
| 69 | if (type == 1) return "float32"; |
| 70 | if (type == 2) return "float64"; |
| 71 | if (type == 3) return "float16"; |
| 72 | if (type == 4) return "int32"; |
| 73 | if (type == 5) return "int64"; |
| 74 | if (type == 6) return "int16"; |
| 75 | if (type == 7) return "int8"; |
| 76 | if (type == 8) return "uint8"; |
| 77 | if (type == 9) return "bool8"; |
| 78 | if (type == 10) return "csingle"; |
| 79 | if (type == 11) return "cdouble"; |
| 80 | if (type == 12) return "chalf"; |
| 81 | if (type == 13) return "bfloat16"; |
| 82 | return "null"; |
| 83 | } |
| 84 | |
| 85 | static const char* type_to_dtype_string(int type) |
| 86 | { |
| 87 | if (type == 1) return "torch.float"; |
| 88 | if (type == 2) return "torch.double"; |
| 89 | if (type == 3) return "torch.half"; |
| 90 | if (type == 4) return "torch.int"; |
| 91 | if (type == 5) return "torch.long"; |
| 92 | if (type == 6) return "torch.short"; |
| 93 | if (type == 7) return "torch.int8"; |
| 94 | if (type == 8) return "torch.uint8"; |
| 95 | if (type == 9) return "torch.bool"; |
| 96 | if (type == 10) return "torch.complex64"; |
| 97 | if (type == 11) return "torch.complex128"; |
| 98 | if (type == 12) return "torch.complex32"; |
| 99 | if (type == 13) return "torch.bfloat16"; |
| 100 | return "null"; |
| 101 | } |
| 102 | |
| 103 | static size_t type_to_elemsize(int type) |
| 104 | { |
| 105 | if (type == 1) return 4; |
| 106 | if (type == 2) return 8; |
| 107 | if (type == 3) return 2; |
| 108 | if (type == 4) return 4; |
| 109 | if (type == 5) return 8; |
| 110 | if (type == 6) return 2; |
| 111 | if (type == 7) return 1; |
| 112 | if (type == 8) return 1; |
| 113 | if (type == 9) return 1; |
| 114 | if (type == 10) return 8; |
| 115 | if (type == 11) return 16; |
| 116 | if (type == 12) return 4; |
| 117 | if (type == 13) return 2; |
| 118 | return 0; // null |
| 119 | } |
| 120 | |
| 121 | static int string_to_type(const char* s) |
| 122 | { |
| 123 | if (strcmp(s, "f32") == 0) return 1; |
| 124 | if (strcmp(s, "f64") == 0) return 2; |
| 125 | if (strcmp(s, "f16") == 0) return 3; |
| 126 | if (strcmp(s, "i32") == 0) return 4; |
| 127 | if (strcmp(s, "i64") == 0) return 5; |
| 128 | if (strcmp(s, "i16") == 0) return 6; |
| 129 | if (strcmp(s, "i8") == 0) return 7; |
| 130 | if (strcmp(s, "u8") == 0) return 8; |
| 131 | if (strcmp(s, "bool") == 0) return 9; |
| 132 | if (strcmp(s, "c64") == 0) return 10; |
| 133 | if (strcmp(s, "c128") == 0) return 11; |
| 134 | if (strcmp(s, "c32") == 0) return 12; |
| 135 | if (strcmp(s, "bf16") == 0) return 13; |
| 136 | return 0; // null |
| 137 | } |
| 138 | |
| 139 | bool operator==(const Parameter& lhs, const Parameter& rhs) |
| 140 | { |
| 141 | if (lhs.type != rhs.type) |
| 142 | return false; |
| 143 | |
| 144 | if (lhs.type == 0) |
| 145 | return true; |
| 146 | |
| 147 | if (lhs.type == 1 && lhs.b == rhs.b) |
| 148 | return true; |
| 149 | |
| 150 | if (lhs.type == 2 && lhs.i == rhs.i) |
| 151 | return true; |
| 152 | |
| 153 | if (lhs.type == 3 && lhs.f == rhs.f) |
| 154 | return true; |
| 155 | |
| 156 | if (lhs.type == 4 && lhs.s == rhs.s) |
| 157 | return true; |
| 158 | |
| 159 | if (lhs.type == 5 && lhs.ai == rhs.ai) |
| 160 | return true; |
| 161 | |
| 162 | if (lhs.type == 6 && lhs.af == rhs.af) |
| 163 | return true; |
| 164 | |
| 165 | if (lhs.type == 7 && lhs.as == rhs.as) |
| 166 | return true; |
| 167 | |
| 168 | if (lhs.type == 10 && lhs.c == rhs.c) |
| 169 | return true; |
| 170 | |
| 171 | if (lhs.type == 11 && lhs.ac == rhs.ac) |
| 172 | return true; |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | Attribute::Attribute(const std::initializer_list<int>& _shape, const std::vector<float>& t) |
| 178 | { |
| 179 | type = 1; |
| 180 | shape = _shape; |
| 181 | |
| 182 | if (shape.size() > 0) |
| 183 | { |
| 184 | data.resize(elemcount() * type_to_elemsize(type)); |
| 185 | memcpy((void*)data.data(), (const void*)t.data(), data.size()); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | size_t Attribute::elemsize() const |
| 190 | { |
| 191 | return type_to_elemsize(type); |
| 192 | } |
| 193 | |
| 194 | int Attribute::elemcount() const |
| 195 | { |
| 196 | if (shape.empty()) |
| 197 | return 0; |
| 198 | |
| 199 | int size = shape[0]; |
| 200 | for (size_t i = 1; i < shape.size(); i++) |
| 201 | { |
| 202 | size *= shape[i]; |
| 203 | } |
| 204 | |
| 205 | return size; |
| 206 | } |
| 207 | |
| 208 | std::vector<float> Attribute::get_float32_data() const |
| 209 | { |
| 210 | std::vector<float> v(elemcount()); |
| 211 | |
| 212 | if (type == 1) |
| 213 | { |
| 214 | memcpy((void*)v.data(), (const void*)data.data(), data.size()); |
| 215 | } |
| 216 | else if (type == 2) |
| 217 | { |
| 218 | // f64 |
| 219 | const double* p = (const double*)data.data(); |
| 220 | for (size_t i = 0; i < v.size(); i++) |
| 221 | { |
| 222 | v[i] = float(p[i]); |
| 223 | } |
| 224 | } |
| 225 | else if (type == 3) |
| 226 | { |
| 227 | // f16 |
| 228 | const unsigned short* p = (const unsigned short*)data.data(); |
| 229 | for (size_t i = 0; i < v.size(); i++) |
| 230 | { |
| 231 | v[i] = float16_to_float32(p[i]); |
| 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | fprintf(stderr, "cannot convert type %d to float32 data\n", type); |
| 237 | } |
| 238 | |
| 239 | return v; |
| 240 | } |
| 241 | |
| 242 | void Attribute::set_float32_data(const std::vector<float>& newdata) |
| 243 | { |
| 244 | data.resize(newdata.size() * elemsize()); |
| 245 | |
| 246 | if (type == 1) |
| 247 | { |
| 248 | memcpy((void*)data.data(), (const void*)newdata.data(), data.size()); |
| 249 | } |
| 250 | else if (type == 2) |
| 251 | { |
| 252 | // f64 |
| 253 | double* p = (double*)data.data(); |
| 254 | for (size_t i = 0; i < newdata.size(); i++) |
| 255 | { |
| 256 | p[i] = newdata[i]; |
| 257 | } |
| 258 | } |
| 259 | else if (type == 3) |
| 260 | { |
| 261 | // f16 |
| 262 | unsigned short* p = (unsigned short*)data.data(); |
| 263 | for (size_t i = 0; i < newdata.size(); i++) |
| 264 | { |
| 265 | p[i] = float32_to_float16(newdata[i]); |
| 266 | } |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | fprintf(stderr, "cannot convert float32 data to type %d\n", type); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | bool operator==(const Attribute& lhs, const Attribute& rhs) |
| 275 | { |
| 276 | if (lhs.type != rhs.type) |
| 277 | return false; |
| 278 | |
| 279 | if (lhs.type == 0) |
| 280 | return true; |
| 281 | |
| 282 | if (lhs.shape != rhs.shape) |
| 283 | return false; |
| 284 | |
| 285 | if (lhs.data != rhs.data) |
| 286 | return false; |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | Attribute operator+(const Attribute& a, const Attribute& b) |
| 292 | { |
| 293 | Attribute c; |
| 294 | |
| 295 | if (a.type != b.type) |
| 296 | { |
| 297 | fprintf(stderr, "concat attribute type mismatch\n"); |
| 298 | return c; |
| 299 | } |
| 300 | |
| 301 | if (a.shape.size() != b.shape.size()) |
| 302 | { |
| 303 | fprintf(stderr, "concat attribute shape rank mismatch\n"); |
| 304 | return c; |
| 305 | } |
| 306 | |
| 307 | for (int i = 1; i < (int)a.shape.size(); i++) |
| 308 | { |
| 309 | if (a.shape[i] != b.shape[i]) |
| 310 | { |
| 311 | fprintf(stderr, "concat attribute shape mismatch\n"); |
| 312 | return c; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | c.type = a.type; |
| 317 | c.shape = a.shape; |
| 318 | c.shape[0] += b.shape[0]; // concat the first dim |
| 319 | |
| 320 | c.data.resize(a.data.size() + b.data.size()); |
| 321 | memcpy(c.data.data(), a.data.data(), a.data.size()); |
| 322 | memcpy(c.data.data() + a.data.size(), b.data.data(), b.data.size()); |
| 323 | |
| 324 | return c; |
| 325 | } |
| 326 | |
| 327 | Parameter Parameter::parse_from_string(const std::string& value) |
| 328 | { |
| 329 | if (value.find('%') != std::string::npos) |
| 330 | { |
| 331 | Parameter p; |
| 332 | p.type = 4; |
| 333 | p.s = value; |
| 334 | return p; |
| 335 | } |
| 336 | |
| 337 | Parameter p; |
| 338 | p.type = 0; |
| 339 | |
| 340 | if (value == "None" || value == "()" || value == "[]") |
| 341 | { |
| 342 | return p; |
| 343 | } |
| 344 | |
| 345 | if (value == "True" || value == "False") |
| 346 | { |
| 347 | // bool |
| 348 | p.type = 1; |
| 349 | p.b = value == "True"; |
| 350 | return p; |
| 351 | } |
| 352 | |
| 353 | if (value[0] == '(' || value[0] == '[') |
| 354 | { |
| 355 | // list |
| 356 | std::string lc = value.substr(1, value.size() - 2); |
| 357 | std::istringstream lcss(lc); |
| 358 | |
| 359 | while (!lcss.eof()) |
| 360 | { |
| 361 | std::string elem; |
| 362 | std::getline(lcss, elem, ','); |
| 363 | |
| 364 | if ((elem[0] != '-' && (elem[0] < '0' || elem[0] > '9')) || (elem[0] == '-' && (elem[1] < '0' || elem[1] > '9'))) |
| 365 | { |
| 366 | // string |
| 367 | p.type = 7; |
| 368 | p.as.push_back(elem); |
| 369 | } |
| 370 | else if (elem.find('.') != std::string::npos || elem.find('e') != std::string::npos) |
| 371 | { |
| 372 | // float |
| 373 | p.type = 6; |
| 374 | p.af.push_back(std::stof(elem)); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | // integer |
| 379 | p.type = 5; |
| 380 | p.ai.push_back(std::stoi(elem)); |
| 381 | } |
| 382 | } |
| 383 | return p; |
| 384 | } |
| 385 | |
| 386 | if ((value[0] != '-' && (value[0] < '0' || value[0] > '9')) || (value[0] == '-' && (value[1] < '0' || value[1] > '9'))) |
| 387 | { |
| 388 | // string |
| 389 | p.type = 4; |
| 390 | p.s = value; |
| 391 | return p; |
| 392 | } |
| 393 | |
| 394 | if (value.find('.') != std::string::npos || value.find('e') != std::string::npos) |
| 395 | { |
| 396 | // float |
| 397 | p.type = 3; |
| 398 | p.f = std::stof(value); |
| 399 | return p; |
| 400 | } |
| 401 | |
| 402 | // integer |
| 403 | p.type = 2; |
| 404 | p.i = std::stoi(value); |
| 405 | return p; |
| 406 | } |
| 407 | |
| 408 | std::string Parameter::encode_to_string(const Parameter& param) |
| 409 | { |
| 410 | if (param.type == 0) |
| 411 | { |
| 412 | return std::string("None"); |
| 413 | } |
| 414 | if (param.type == 1) |
| 415 | { |
| 416 | if (param.b) |
| 417 | return std::string("True"); |
| 418 | else |
| 419 | return std::string("False"); |
| 420 | } |
| 421 | if (param.type == 2) |
| 422 | { |
| 423 | return std::to_string(param.i); |
| 424 | } |
| 425 | if (param.type == 3) |
| 426 | { |
| 427 | char buf[64]; |
| 428 | sprintf(buf, "%e", param.f); |
| 429 | return std::string(buf); |
| 430 | } |
| 431 | if (param.type == 4) |
| 432 | { |
| 433 | return param.s; |
| 434 | } |
| 435 | if (param.type == 5) |
| 436 | { |
| 437 | std::string s("("); |
| 438 | for (size_t i = 0; i < param.ai.size(); i++) |
| 439 | { |
| 440 | s += std::to_string(param.ai[i]); |
| 441 | if (i + 1 != param.ai.size()) |
| 442 | s += std::string(","); |
| 443 | } |
| 444 | s += std::string(")"); |
| 445 | return s; |
| 446 | } |
| 447 | if (param.type == 6) |
| 448 | { |
| 449 | std::string s("("); |
| 450 | for (size_t i = 0; i < param.af.size(); i++) |
| 451 | { |
| 452 | char buf[64]; |
| 453 | sprintf(buf, "%e", param.af[i]); |
| 454 | s += std::string(buf); |
| 455 | if (i + 1 != param.af.size()) |
| 456 | s += std::string(","); |
| 457 | } |
| 458 | s += std::string(")"); |
| 459 | return s; |
| 460 | } |
| 461 | if (param.type == 7) |
| 462 | { |
| 463 | std::string s("("); |
| 464 | for (size_t i = 0; i < param.as.size(); i++) |
| 465 | { |
| 466 | s += param.as[i]; |
| 467 | if (i + 1 != param.as.size()) |
| 468 | s += std::string(","); |
| 469 | } |
| 470 | s += std::string(")"); |
| 471 | return s; |
| 472 | } |
| 473 | if (param.type == 10) |
| 474 | { |
| 475 | char buf[128]; |
| 476 | sprintf(buf, "%e+%ej", param.c.real(), param.c.imag()); |
| 477 | return std::string(buf); |
| 478 | } |
| 479 | if (param.type == 11) |
| 480 | { |
| 481 | std::string s("("); |
| 482 | for (size_t i = 0; i < param.ac.size(); i++) |
| 483 | { |
| 484 | char buf[128]; |
| 485 | sprintf(buf, "%e+%ej", param.ac[i].real(), param.ac[i].imag()); |
| 486 | s += std::string(buf); |
| 487 | if (i + 1 != param.ac.size()) |
| 488 | s += std::string(","); |
| 489 | } |
| 490 | s += std::string(")"); |
| 491 | return s; |
| 492 | } |
| 493 | |
| 494 | fprintf(stderr, "unknown parameter type %d\n", param.type); |
| 495 | return std::string(); |
| 496 | } |
| 497 | |
| 498 | bool Operator::has_param(const std::string& key) const |
| 499 | { |
| 500 | return params.find(key) != params.end(); |
| 501 | } |
| 502 | |
| 503 | bool Operator::has_attr(const std::string& key) const |
| 504 | { |
| 505 | return attrs.find(key) != attrs.end(); |
| 506 | } |
| 507 | |
| 508 | bool Operator::has_input(const std::string& key) const |
| 509 | { |
| 510 | return std::find(inputnames.begin(), inputnames.end(), key) != inputnames.end(); |
| 511 | } |
| 512 | |
| 513 | Operand* Operator::named_input(const std::string& key) |
| 514 | { |
| 515 | for (size_t i = 0; i < inputnames.size(); i++) |
| 516 | { |
| 517 | if (inputnames[i] == key) |
| 518 | return inputs[i]; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | const Operand* Operator::named_input(const std::string& key) const |
| 525 | { |
| 526 | for (size_t i = 0; i < inputnames.size(); i++) |
| 527 | { |
| 528 | if (inputnames[i] == key) |
| 529 | return inputs[i]; |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | Graph::Graph() |
| 536 | { |
| 537 | } |
| 538 | |
| 539 | Graph::~Graph() |
| 540 | { |
| 541 | for (auto x : ops) |
| 542 | delete x; |
| 543 | |
| 544 | for (auto x : operands) |
| 545 | delete x; |
| 546 | |
| 547 | ops.clear(); |
| 548 | operands.clear(); |
| 549 | } |
| 550 | |
| 551 | Graph::Graph(const Graph& /*rhs*/) |
| 552 | { |
| 553 | } |
| 554 | |
| 555 | Graph& Graph::operator=(const Graph& /*rhs*/) |
| 556 | { |
| 557 | return *this; |
| 558 | } |
| 559 | |
| 560 | static void load_parameter(Operator* op, const std::string& key, const std::string& value) |
| 561 | { |
| 562 | op->params[key] = Parameter::parse_from_string(value); |
| 563 | } |
| 564 | |
| 565 | static void load_input_key(Operator* op, const std::string& key, const std::string& value) |
| 566 | { |
| 567 | op->inputnames.resize(op->inputs.size()); |
| 568 | |
| 569 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 570 | { |
| 571 | const Operand* oprand = op->inputs[i]; |
| 572 | if (oprand->name == value) |
| 573 | { |
| 574 | op->inputnames[i] = key; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void load_shape(Operator* op, const std::string& key, const std::string& value) |
| 581 | { |
| 582 | Operand* operand = 0; |
| 583 | for (auto r : op->inputs) |
| 584 | { |
| 585 | if (r->name == key) |
| 586 | { |
| 587 | operand = r; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (!operand) |
| 593 | { |
| 594 | for (auto r : op->outputs) |
| 595 | { |
| 596 | if (r->name == key) |
| 597 | { |
| 598 | operand = r; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if (!operand) |
| 605 | { |
| 606 | fprintf(stderr, "no such operand %s for operator %s\n", key.c_str(), op->name.c_str()); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | // type |
| 611 | std::string typestr = value.substr(value.find_last_of(')') + 1); |
| 612 | operand->type = string_to_type(typestr.c_str()); |
| 613 | |
| 614 | // shape |
| 615 | std::string lc = value.substr(1, value.find_last_of(')') - 1); |
| 616 | std::istringstream lcss(lc); |
| 617 | |
| 618 | operand->shape.clear(); |
| 619 | while (!lcss.eof()) |
| 620 | { |
| 621 | std::string elem; |
| 622 | std::getline(lcss, elem, ','); |
| 623 | |
| 624 | if (elem == "?") |
| 625 | { |
| 626 | operand->shape.push_back(-1); |
| 627 | } |
| 628 | else if (elem[0] == '%') |
| 629 | { |
| 630 | // encode %abc as symbolic tag |
| 631 | operand->shape.push_back(-233); |
| 632 | int index = operand->shape.size() - 1; |
| 633 | std::string key = elem.substr(1); |
| 634 | operand->params[std::string("__shape__") + std::to_string(index)] = key; |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | int i = std::stoi(elem); |
| 639 | operand->shape.push_back(i); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | static void load_attribute(Operator* op, const std::string& key, const std::string& value, StoreZipReader& szr) |
| 645 | { |
| 646 | Attribute& a = op->attrs[key]; |
| 647 | |
| 648 | // type |
| 649 | std::string typestr = value.substr(value.find_last_of(')') + 1); |
| 650 | a.type = string_to_type(typestr.c_str()); |
| 651 | |
| 652 | if (a.type == 0) |
| 653 | return; |
| 654 | |
| 655 | // shape |
| 656 | std::string lc = value.substr(1, value.find_last_of(')') - 1); |
| 657 | std::istringstream lcss(lc); |
| 658 | |
| 659 | a.shape.clear(); |
| 660 | while (!lcss.eof()) |
| 661 | { |
| 662 | std::string elem; |
| 663 | std::getline(lcss, elem, ','); |
| 664 | |
| 665 | int i = std::stoi(elem); |
| 666 | a.shape.push_back(i); |
| 667 | } |
| 668 | |
| 669 | if (a.shape.empty()) |
| 670 | return; |
| 671 | |
| 672 | // data |
| 673 | size_t size = 1; |
| 674 | for (int i : a.shape) |
| 675 | { |
| 676 | size *= i; |
| 677 | } |
| 678 | |
| 679 | size_t bytesize = size * type_to_elemsize(a.type); |
| 680 | |
| 681 | std::string filename = op->name + "." + key; |
| 682 | |
| 683 | size_t filesize = szr.get_file_size(filename); |
| 684 | |
| 685 | if (filesize == 0) |
| 686 | { |
| 687 | // no such file |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | if (filesize != bytesize) |
| 692 | { |
| 693 | fprintf(stderr, "file size not match expect %lu but got %lu\n", bytesize, filesize); |
| 694 | } |
| 695 | |
| 696 | a.data.resize(bytesize); |
| 697 | szr.read_file(filename, (char*)a.data.data()); |
| 698 | } |
| 699 | |
| 700 | int Graph::load(const std::string& parampath, const std::string& binpath) |
| 701 | { |
| 702 | std::ifstream is(parampath, std::ios::in | std::ios::binary); |
| 703 | if (!is.good()) |
| 704 | { |
| 705 | fprintf(stderr, "open failed\n"); |
| 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | StoreZipReader szr; |
| 710 | if (szr.open(binpath) != 0) |
| 711 | { |
| 712 | fprintf(stderr, "open failed\n"); |
| 713 | return -1; |
| 714 | } |
| 715 | |
| 716 | int magic = 0; |
| 717 | { |
| 718 | std::string line; |
| 719 | std::getline(is, line); |
| 720 | std::istringstream iss(line); |
| 721 | |
| 722 | iss >> magic; |
| 723 | } |
| 724 | |
| 725 | int operator_count = 0; |
| 726 | int operand_count = 0; |
| 727 | { |
| 728 | std::string line; |
| 729 | std::getline(is, line); |
| 730 | std::istringstream iss(line); |
| 731 | |
| 732 | iss >> operator_count >> operand_count; |
| 733 | } |
| 734 | |
| 735 | for (int i = 0; i < operator_count; i++) |
| 736 | { |
| 737 | std::string line; |
| 738 | std::getline(is, line); |
| 739 | std::istringstream iss(line); |
| 740 | |
| 741 | std::string type; |
| 742 | std::string name; |
| 743 | int input_count = 0; |
| 744 | int output_count = 0; |
| 745 | |
| 746 | iss >> type >> name >> input_count >> output_count; |
| 747 | |
| 748 | Operator* op = new_operator(type, name); |
| 749 | |
| 750 | for (int j = 0; j < input_count; j++) |
| 751 | { |
| 752 | std::string operand_name; |
| 753 | iss >> operand_name; |
| 754 | |
| 755 | Operand* r = get_operand(operand_name); |
| 756 | r->consumers.push_back(op); |
| 757 | op->inputs.push_back(r); |
| 758 | } |
| 759 | |
| 760 | for (int j = 0; j < output_count; j++) |
| 761 | { |
| 762 | std::string operand_name; |
| 763 | iss >> operand_name; |
| 764 | |
| 765 | Operand* r = new_operand(operand_name); |
| 766 | r->producer = op; |
| 767 | op->outputs.push_back(r); |
| 768 | } |
| 769 | |
| 770 | // key=value |
| 771 | while (!iss.eof()) |
| 772 | { |
| 773 | std::string param; |
| 774 | iss >> param; |
| 775 | |
| 776 | std::string key; |
| 777 | std::string value; |
| 778 | std::istringstream pss(param); |
| 779 | std::getline(pss, key, '='); |
| 780 | std::getline(pss, value); |
| 781 | |
| 782 | if (key[0] == '@') |
| 783 | { |
| 784 | // attribute |
| 785 | load_attribute(op, key.substr(1), value, szr); |
| 786 | } |
| 787 | else if (key[0] == '$') |
| 788 | { |
| 789 | // operand input key |
| 790 | load_input_key(op, key.substr(1), value); |
| 791 | } |
| 792 | else if (key[0] == '#') |
| 793 | { |
| 794 | // operand shape |
| 795 | load_shape(op, key.substr(1), value); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | // parameter |
| 800 | load_parameter(op, key, value); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | int Graph::save(const std::string& parampath, const std::string& binpath) |
| 809 | { |
| 810 | FILE* paramfp = fopen(parampath.c_str(), "wb"); |
| 811 | if (!paramfp) |
| 812 | { |
| 813 | fprintf(stderr, "fopen %s failed\n", parampath.c_str()); |
| 814 | return -1; |
| 815 | } |
| 816 | |
| 817 | StoreZipWriter szw; |
| 818 | if (szw.open(binpath) != 0) |
| 819 | { |
| 820 | fprintf(stderr, "open failed\n"); |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | // magic |
| 825 | fprintf(paramfp, "7767517\n"); |
| 826 | |
| 827 | // op count and oprand count |
| 828 | fprintf(paramfp, "%d %d\n", (int)ops.size(), (int)operands.size()); |
| 829 | |
| 830 | for (const Operator* op : ops) |
| 831 | { |
| 832 | fprintf(paramfp, "%-24s %-24s %d %d", op->type.c_str(), op->name.c_str(), (int)op->inputs.size(), (int)op->outputs.size()); |
| 833 | |
| 834 | for (const Operand* oprand : op->inputs) |
| 835 | { |
| 836 | fprintf(paramfp, " %s", oprand->name.c_str()); |
| 837 | } |
| 838 | |
| 839 | for (const Operand* oprand : op->outputs) |
| 840 | { |
| 841 | fprintf(paramfp, " %s", oprand->name.c_str()); |
| 842 | } |
| 843 | |
| 844 | for (const auto& it : op->params) |
| 845 | { |
| 846 | fprintf(paramfp, " %s=", it.first.c_str()); |
| 847 | |
| 848 | const Parameter& param = it.second; |
| 849 | std::string s = Parameter::encode_to_string(param); |
| 850 | fprintf(paramfp, "%s", s.c_str()); |
| 851 | } |
| 852 | |
| 853 | for (const auto& it : op->attrs) |
| 854 | { |
| 855 | fprintf(paramfp, " @%s=", it.first.c_str()); |
| 856 | |
| 857 | const Attribute& attr = it.second; |
| 858 | fprintf(paramfp, "("); |
| 859 | for (int i = 0; i < (int)attr.shape.size() - 1; i++) |
| 860 | { |
| 861 | fprintf(paramfp, "%d,", attr.shape[i]); |
| 862 | } |
| 863 | if (attr.shape.size() > 0) |
| 864 | fprintf(paramfp, "%d", attr.shape[attr.shape.size() - 1]); |
| 865 | fprintf(paramfp, ")"); |
| 866 | |
| 867 | fprintf(paramfp, type_to_string(attr.type)); |
| 868 | |
| 869 | std::string filename = op->name + "." + it.first; |
| 870 | szw.write_file(filename, attr.data.data(), attr.data.size()); |
| 871 | } |
| 872 | |
| 873 | if (op->inputnames.size() == op->inputs.size()) |
| 874 | { |
| 875 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 876 | { |
| 877 | if (op->inputnames[i].empty()) |
| 878 | continue; |
| 879 | |
| 880 | const Operand* oprand = op->inputs[i]; |
| 881 | fprintf(paramfp, " $%s=%s", op->inputnames[i].c_str(), oprand->name.c_str()); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | for (const Operand* oprand : op->inputs) |
| 886 | { |
| 887 | if (oprand->shape.empty()) |
| 888 | continue; |
| 889 | |
| 890 | fprintf(paramfp, " #%s=", oprand->name.c_str()); |
| 891 | |
| 892 | fprintf(paramfp, "("); |
| 893 | for (int i = 0; i < (int)oprand->shape.size() - 1; i++) |
| 894 | { |
| 895 | if (oprand->shape[i] == -1) |
| 896 | fprintf(paramfp, "?,"); |
| 897 | else |
| 898 | fprintf(paramfp, "%d,", oprand->shape[i]); |
| 899 | } |
| 900 | if (oprand->shape.size() > 0) |
| 901 | { |
| 902 | if (oprand->shape[oprand->shape.size() - 1] == -1) |
| 903 | fprintf(paramfp, "?"); |
| 904 | else |
| 905 | fprintf(paramfp, "%d", oprand->shape[oprand->shape.size() - 1]); |
| 906 | } |
| 907 | fprintf(paramfp, ")"); |
| 908 | |
| 909 | fprintf(paramfp, type_to_string(oprand->type)); |
| 910 | } |
| 911 | |
| 912 | for (const Operand* oprand : op->outputs) |
| 913 | { |
| 914 | if (oprand->shape.empty()) |
| 915 | continue; |
| 916 | |
| 917 | fprintf(paramfp, " #%s=", oprand->name.c_str()); |
| 918 | |
| 919 | fprintf(paramfp, "("); |
| 920 | for (int i = 0; i < (int)oprand->shape.size() - 1; i++) |
| 921 | { |
| 922 | if (oprand->shape[i] == -1) |
| 923 | fprintf(paramfp, "?,"); |
| 924 | else |
| 925 | fprintf(paramfp, "%d,", oprand->shape[i]); |
| 926 | } |
| 927 | if (oprand->shape.size() > 0) |
| 928 | { |
| 929 | if (oprand->shape[oprand->shape.size() - 1] == -1) |
| 930 | fprintf(paramfp, "?"); |
| 931 | else |
| 932 | fprintf(paramfp, "%d", oprand->shape[oprand->shape.size() - 1]); |
| 933 | } |
| 934 | fprintf(paramfp, ")"); |
| 935 | |
| 936 | fprintf(paramfp, type_to_string(oprand->type)); |
| 937 | } |
| 938 | |
| 939 | fprintf(paramfp, "\n"); |
| 940 | } |
| 941 | |
| 942 | fclose(paramfp); |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static std::string sanitize_identifier(const std::string& s) |
| 948 | { |
| 949 | std::string ss = s; |
| 950 | for (size_t i = 0; i < ss.size(); i++) |
| 951 | { |
| 952 | if (ss[i] == '.' || ss[i] == ':' || ss[i] == '/') |
| 953 | ss[i] = '_'; |
| 954 | } |
| 955 | |
| 956 | return ss; |
| 957 | } |
| 958 | |
| 959 | static std::string expand_expression(const Operator* op) |
| 960 | { |
| 961 | std::string expr = op->params.at("expr").s; |
| 962 | |
| 963 | // split into tokens |
| 964 | std::vector<std::string> tokens; |
| 965 | { |
| 966 | std::string t; |
| 967 | for (size_t i = 0; i < expr.size(); i++) |
| 968 | { |
| 969 | char ch = expr[i]; |
| 970 | |
| 971 | if (ch == '[') // list |
| 972 | { |
| 973 | t += ch; |
| 974 | tokens.push_back(t); |
| 975 | t.clear(); |
| 976 | } |
| 977 | else if (ch == '(' || ch == ')' || ch == ',' || ch == ']') |
| 978 | { |
| 979 | if (!t.empty()) |
| 980 | { |
| 981 | tokens.push_back(t); |
| 982 | t.clear(); |
| 983 | } |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | t += ch; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | if (!t.empty()) |
| 992 | { |
| 993 | tokens.push_back(t); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // scan and stack |
| 998 | std::stack<std::string> exprstack; |
| 999 | for (int i = (int)tokens.size() - 1; i >= 0; i--) |
| 1000 | { |
| 1001 | const std::string& t = tokens[i]; |
| 1002 | |
| 1003 | if (t == "size") |
| 1004 | { |
| 1005 | std::string a = exprstack.top(); |
| 1006 | exprstack.pop(); |
| 1007 | |
| 1008 | if (exprstack.empty()) |
| 1009 | { |
| 1010 | std::string r = a + ".shape"; |
| 1011 | exprstack.push(r); |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | std::string b = exprstack.top(); |
| 1016 | exprstack.pop(); |
| 1017 | |
| 1018 | std::string r = a + ".size(" + b + ")"; |
| 1019 | exprstack.push(r); |
| 1020 | } |
| 1021 | } |
| 1022 | else if (t == "int" |
| 1023 | || t == "abs" |
| 1024 | || t == "acos" |
| 1025 | || t == "acosh" |
| 1026 | || t == "asin" |
| 1027 | || t == "asinh" |
| 1028 | || t == "atan" |
| 1029 | || t == "atanh" |
| 1030 | || t == "ceil" |
| 1031 | || t == "cos" |
| 1032 | || t == "cosh" |
| 1033 | || t == "exp" |
| 1034 | || t == "floor" |
| 1035 | || t == "log" |
| 1036 | || t == "log10" |
| 1037 | || t == "neg" |
| 1038 | || t == "reciprocal" |
| 1039 | || t == "round" |
| 1040 | || t == "rsqrt" |
| 1041 | || t == "sign" |
| 1042 | || t == "sin" |
| 1043 | || t == "sinh" |
| 1044 | || t == "sqrt" |
| 1045 | || t == "square" |
| 1046 | || t == "tan" |
| 1047 | || t == "tanh" |
| 1048 | || t == "trunc" |
| 1049 | || t == "torch.bool" |
| 1050 | || t == "torch.float" |
| 1051 | || t == "torch.long") |
| 1052 | { |
| 1053 | std::string unaryop = t; |
| 1054 | if (t == "int") unaryop = "int"; |
| 1055 | if (t == "abs") unaryop = "torch.abs"; |
| 1056 | if (t == "acos") unaryop = "torch.acos"; |
| 1057 | if (t == "acosh") unaryop = "torch.acosh"; |
| 1058 | if (t == "asin") unaryop = "torch.asin"; |
| 1059 | if (t == "asinh") unaryop = "torch.asinh"; |
| 1060 | if (t == "atan") unaryop = "torch.atan"; |
| 1061 | if (t == "atanh") unaryop = "torch.atanh"; |
| 1062 | if (t == "ceil") unaryop = "torch.ceil"; |
| 1063 | if (t == "cos") unaryop = "torch.cos"; |
| 1064 | if (t == "cosh") unaryop = "torch.cosh"; |
| 1065 | if (t == "exp") unaryop = "torch.exp"; |
| 1066 | if (t == "floor") unaryop = "torch.floor"; |
| 1067 | if (t == "log") unaryop = "torch.log"; |
| 1068 | if (t == "log10") unaryop = "torch.log10"; |
| 1069 | if (t == "neg") unaryop = "-"; |
| 1070 | if (t == "reciprocal") unaryop = "torch.reciprocal"; |
| 1071 | if (t == "round") unaryop = "torch.round"; |
| 1072 | if (t == "rsqrt") unaryop = "torch.rsqrt"; |
| 1073 | if (t == "sign") unaryop = "torch.sign"; |
| 1074 | if (t == "sin") unaryop = "torch.sin"; |
| 1075 | if (t == "sinh") unaryop = "torch.sinh"; |
| 1076 | if (t == "sqrt") unaryop = "torch.sqrt"; |
| 1077 | if (t == "square") unaryop = "torch.square"; |
| 1078 | if (t == "tan") unaryop = "torch.tan"; |
| 1079 | if (t == "tanh") unaryop = "torch.tanh"; |
| 1080 | if (t == "trunc") unaryop = "torch.trunc"; |
| 1081 | |
| 1082 | std::string a = exprstack.top(); |
| 1083 | exprstack.pop(); |
| 1084 | |
| 1085 | std::string r = unaryop + "(" + a + ")"; |
| 1086 | exprstack.push(r); |
| 1087 | } |
| 1088 | else if (t == "atan2" |
| 1089 | || t == "fmod" |
| 1090 | || t == "max" |
| 1091 | || t == "maximum" |
| 1092 | || t == "min" |
| 1093 | || t == "minimum" |
| 1094 | || t == "pow" |
| 1095 | || t == "logaddexp") |
| 1096 | { |
| 1097 | std::string binaryop; |
| 1098 | if (t == "atan2") binaryop = "torch.atan2"; |
| 1099 | if (t == "fmod") binaryop = "torch.fmod"; |
| 1100 | if (t == "max") binaryop = "torch.max"; |
| 1101 | if (t == "maximum") binaryop = "torch.maximum"; |
| 1102 | if (t == "min") binaryop = "torch.min"; |
| 1103 | if (t == "minimum") binaryop = "torch.minimum"; |
| 1104 | if (t == "pow") binaryop = "torch.pow"; |
| 1105 | if (t == "logaddexp") binaryop = "torch.logaddexp"; |
| 1106 | |
| 1107 | std::string a = exprstack.top(); |
| 1108 | exprstack.pop(); |
| 1109 | std::string b = exprstack.top(); |
| 1110 | exprstack.pop(); |
| 1111 | |
| 1112 | std::string r = binaryop + "(" + a + ", " + b + ")"; |
| 1113 | exprstack.push(r); |
| 1114 | } |
| 1115 | else if (t == "add" |
| 1116 | || t == "sub" |
| 1117 | || t == "mul" |
| 1118 | || t == "div" |
| 1119 | || t == "floor_divide" |
| 1120 | || t == "remainder" |
| 1121 | || t == "and" |
| 1122 | || t == "or" |
| 1123 | || t == "xor" |
| 1124 | || t == "lshift" |
| 1125 | || t == "rshift") |
| 1126 | { |
| 1127 | std::string binaryop; |
| 1128 | if (t == "add") binaryop = "+"; |
| 1129 | if (t == "sub") binaryop = "-"; |
| 1130 | if (t == "mul") binaryop = "*"; |
| 1131 | if (t == "div") binaryop = "/"; |
| 1132 | if (t == "floor_divide") binaryop = "//"; |
| 1133 | if (t == "remainder") binaryop = "%"; |
| 1134 | if (t == "and") binaryop = "&"; |
| 1135 | if (t == "or") binaryop = "|"; |
| 1136 | if (t == "xor") binaryop = "^"; |
| 1137 | if (t == "lshift") binaryop = "<<"; |
| 1138 | if (t == "rshift") binaryop = ">>"; |
| 1139 | |
| 1140 | std::string a = exprstack.top(); |
| 1141 | exprstack.pop(); |
| 1142 | std::string b = exprstack.top(); |
| 1143 | exprstack.pop(); |
| 1144 | |
| 1145 | std::string r = std::string("(") + a + " " + binaryop + " " + b + ")"; |
| 1146 | exprstack.push(r); |
| 1147 | } |
| 1148 | else if (t == "[") // list |
| 1149 | { |
| 1150 | std::vector<std::string> elements; |
| 1151 | while (!exprstack.empty()) |
| 1152 | { |
| 1153 | std::string a = exprstack.top(); |
| 1154 | exprstack.pop(); |
| 1155 | |
| 1156 | elements.push_back(a); |
| 1157 | } |
| 1158 | |
| 1159 | std::string r = "["; |
| 1160 | for (int j = 0; j < (int)elements.size() - 1; j++) |
| 1161 | { |
| 1162 | r += elements[j]; |
| 1163 | if (j + 1 != (int)elements.size()) |
| 1164 | r += ", "; |
| 1165 | } |
| 1166 | if (!elements.empty()) |
| 1167 | { |
| 1168 | r += elements[elements.size() - 1]; |
| 1169 | } |
| 1170 | r += "]"; |
| 1171 | |
| 1172 | exprstack.push(r); |
| 1173 | } |
| 1174 | else if (t[0] == '@') |
| 1175 | { |
| 1176 | int input_index = std::stoi(t.substr(1)); |
| 1177 | std::string varid = std::string("v_") + sanitize_identifier(op->inputs[input_index]->name); |
| 1178 | exprstack.push(varid); |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | // literal |
| 1183 | if (t[t.size() - 1] == 'j') |
| 1184 | { |
| 1185 | // complex |
| 1186 | std::string r = std::string("(") + t + ")"; |
| 1187 | exprstack.push(r); |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | exprstack.push(t); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | std::string r = exprstack.top(); |
| 1197 | exprstack.pop(); |
| 1198 | |
| 1199 | return r; |
| 1200 | } |
| 1201 | |
| 1202 | static std::string make_slice_expression(const Operator* op) |
| 1203 | { |
| 1204 | // for (size_t j = 0; j < op->inputnames.size(); j++) |
| 1205 | // { |
| 1206 | // fprintf(stderr, "make_slice_expression %s %s\n", op->inputnames[j].c_str(), op->inputs[j]->name.c_str()); |
| 1207 | // } |
| 1208 | |
| 1209 | std::vector<int> dims; |
| 1210 | if (op->has_param("dims")) |
| 1211 | { |
| 1212 | dims = op->params.at("dims").ai; |
| 1213 | } |
| 1214 | else |
| 1215 | { |
| 1216 | dims.push_back(op->params.at("dim").i); |
| 1217 | } |
| 1218 | |
| 1219 | std::string pr; |
| 1220 | std::string nr; |
| 1221 | |
| 1222 | int last_dim = -1; |
| 1223 | const int ndim = (int)dims.size(); |
| 1224 | for (int i = 0; i < ndim; i++) |
| 1225 | { |
| 1226 | int dim = dims[i]; |
| 1227 | std::string& r = dim < 0 ? nr : pr; |
| 1228 | |
| 1229 | for (int j = last_dim + 1; j < dim; j++) |
| 1230 | { |
| 1231 | r += ":,"; |
| 1232 | } |
| 1233 | last_dim = dim; |
| 1234 | |
| 1235 | bool is_select = false; |
| 1236 | if (op->has_param("select")) |
| 1237 | { |
| 1238 | int select = op->params.at("select").i; |
| 1239 | if (select != INT_MAX) |
| 1240 | { |
| 1241 | r += std::to_string(select); |
| 1242 | is_select = true; |
| 1243 | } |
| 1244 | } |
| 1245 | if (op->has_param("selects")) |
| 1246 | { |
| 1247 | std::vector<int> selects = op->params.at("selects").ai; |
| 1248 | int select = selects[i]; |
| 1249 | if (select != INT_MAX) |
| 1250 | { |
| 1251 | r += std::to_string(select); |
| 1252 | is_select = true; |
| 1253 | } |
| 1254 | } |
| 1255 | if (op->has_input("select")) |
| 1256 | { |
| 1257 | r += std::string("v_") + sanitize_identifier(op->named_input("select")->name); |
| 1258 | is_select = true; |
| 1259 | } |
| 1260 | if (op->has_input("selects")) |
| 1261 | { |
| 1262 | // must be pnnx.SliceIndexes |
| 1263 | const Operator* op_sliceindexes = op->named_input("selects")->producer; |
| 1264 | const std::string& index = op_sliceindexes->params.at("indexes").as[i]; |
| 1265 | if (index[0] == '@') |
| 1266 | { |
| 1267 | int selecti = std::stoi(index.substr(1)); |
| 1268 | r += std::string("v_") + sanitize_identifier(op_sliceindexes->inputs[selecti]->name); |
| 1269 | is_select = true; |
| 1270 | } |
| 1271 | else |
| 1272 | { |
| 1273 | int select = std::stoi(index); |
| 1274 | if (select != INT_MAX) |
| 1275 | { |
| 1276 | r += std::to_string(select); |
| 1277 | is_select = true; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | if (is_select) |
| 1283 | { |
| 1284 | if (i + 1 != ndim) |
| 1285 | r += ','; |
| 1286 | continue; |
| 1287 | } |
| 1288 | |
| 1289 | if (op->has_param("start")) |
| 1290 | { |
| 1291 | int start = op->params.at("start").i; |
| 1292 | if (start != 0) |
| 1293 | r += std::to_string(start); |
| 1294 | } |
| 1295 | else if (op->has_param("starts")) |
| 1296 | { |
| 1297 | std::vector<int> starts = op->params.at("starts").ai; |
| 1298 | int start = starts[i]; |
| 1299 | if (start != 0) |
| 1300 | r += std::to_string(start); |
| 1301 | } |
| 1302 | else if (op->has_input("start")) |
| 1303 | { |
| 1304 | r += std::string("v_") + sanitize_identifier(op->named_input("start")->name); |
| 1305 | } |
| 1306 | else // if (op->has_input("starts")) |
| 1307 | { |
| 1308 | // must be pnnx.SliceIndexes |
| 1309 | const Operator* op_sliceindexes = op->named_input("starts")->producer; |
| 1310 | const std::string& index = op_sliceindexes->params.at("indexes").as[i]; |
| 1311 | if (index[0] == '@') |
| 1312 | { |
| 1313 | int starti = std::stoi(index.substr(1)); |
| 1314 | r += std::string("v_") + sanitize_identifier(op_sliceindexes->inputs[starti]->name); |
| 1315 | } |
| 1316 | else |
| 1317 | { |
| 1318 | int start = std::stoi(index); |
| 1319 | if (start != 0) |
| 1320 | r += std::to_string(start); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | r += ':'; |
| 1325 | |
| 1326 | if (op->has_param("end")) |
| 1327 | { |
| 1328 | int end = op->params.at("end").i; |
| 1329 | if (end != INT_MAX) |
| 1330 | r += std::to_string(end); |
| 1331 | } |
| 1332 | else if (op->has_param("ends")) |
| 1333 | { |
| 1334 | std::vector<int> ends = op->params.at("ends").ai; |
| 1335 | int end = ends[i]; |
| 1336 | if (end != INT_MAX) |
| 1337 | r += std::to_string(end); |
| 1338 | } |
| 1339 | else if (op->has_input("end")) |
| 1340 | { |
| 1341 | r += std::string("v_") + sanitize_identifier(op->named_input("end")->name); |
| 1342 | } |
| 1343 | else // if (op->has_input("ends")) |
| 1344 | { |
| 1345 | // must be pnnx.SliceIndexes |
| 1346 | const Operator* op_sliceindexes = op->named_input("ends")->producer; |
| 1347 | const std::string& index = op_sliceindexes->params.at("indexes").as[i]; |
| 1348 | if (index[0] == '@') |
| 1349 | { |
| 1350 | int endi = std::stoi(index.substr(1)); |
| 1351 | r += std::string("v_") + sanitize_identifier(op_sliceindexes->inputs[endi]->name); |
| 1352 | } |
| 1353 | else |
| 1354 | { |
| 1355 | int end = std::stoi(index); |
| 1356 | if (end != INT_MAX) |
| 1357 | r += std::to_string(end); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | if (op->has_param("step")) |
| 1362 | { |
| 1363 | int step = op->params.at("step").i; |
| 1364 | if (step != 1) |
| 1365 | { |
| 1366 | r += ':'; |
| 1367 | r += std::to_string(step); |
| 1368 | } |
| 1369 | } |
| 1370 | else if (op->has_param("steps")) |
| 1371 | { |
| 1372 | std::vector<int> steps = op->params.at("steps").ai; |
| 1373 | int step = steps[i]; |
| 1374 | if (step != 1) |
| 1375 | { |
| 1376 | r += ':'; |
| 1377 | r += std::to_string(step); |
| 1378 | } |
| 1379 | } |
| 1380 | else if (op->has_input("step")) |
| 1381 | { |
| 1382 | r += ':'; |
| 1383 | r += std::string("v_") + sanitize_identifier(op->named_input("step")->name); |
| 1384 | } |
| 1385 | else // if (op->has_input("steps")) |
| 1386 | { |
| 1387 | // must be pnnx.SliceIndexes |
| 1388 | const Operator* op_sliceindexes = op->named_input("steps")->producer; |
| 1389 | const std::string& index = op_sliceindexes->params.at("indexes").as[i]; |
| 1390 | if (index[0] == '@') |
| 1391 | { |
| 1392 | int stepi = std::stoi(index.substr(1)); |
| 1393 | r += ':'; |
| 1394 | r += std::string("v_") + sanitize_identifier(op_sliceindexes->inputs[stepi]->name); |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | int step = std::stoi(index); |
| 1399 | if (step != 1) |
| 1400 | { |
| 1401 | r += ':'; |
| 1402 | r += std::to_string(step); |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | if (i + 1 != ndim) |
| 1408 | r += ','; |
| 1409 | } |
| 1410 | |
| 1411 | if (!pr.empty() && !nr.empty()) |
| 1412 | return pr + "...," + nr; |
| 1413 | |
| 1414 | if (pr.empty() && !nr.empty()) |
| 1415 | return std::string("...,") + nr; |
| 1416 | |
| 1417 | return pr + nr; |
| 1418 | } |
| 1419 | |
| 1420 | static std::string make_index_expression(const Operator* op) |
| 1421 | { |
| 1422 | fprintf(stderr, "make_index_expression %s\n", op->name.c_str()); |
| 1423 | |
| 1424 | std::string index_expr = op->params.at("expr").s; |
| 1425 | |
| 1426 | // strip out-most [ ] pair |
| 1427 | index_expr = index_expr.substr(1, index_expr.size() - 2); |
| 1428 | |
| 1429 | // None,None, -> ..., |
| 1430 | bool leading_none = false; |
| 1431 | while (index_expr.substr(0, 5) == "None,") |
| 1432 | { |
| 1433 | leading_none = true; |
| 1434 | index_expr = index_expr.substr(5); |
| 1435 | } |
| 1436 | if (leading_none) |
| 1437 | { |
| 1438 | index_expr = "...," + index_expr; |
| 1439 | } |
| 1440 | |
| 1441 | return index_expr; |
| 1442 | } |
| 1443 | |
| 1444 | int Graph::python(const std::string& pypath, const std::string& pnnxbinpath) |
| 1445 | { |
| 1446 | FILE* pyfp = fopen(pypath.c_str(), "wb"); |
| 1447 | if (!pyfp) |
| 1448 | { |
| 1449 | fprintf(stderr, "fopen %s failed\n", pypath.c_str()); |
| 1450 | return -1; |
| 1451 | } |
| 1452 | |
| 1453 | fprintf(pyfp, "import os\n"); |
| 1454 | fprintf(pyfp, "import numpy as np\n"); |
| 1455 | fprintf(pyfp, "import tempfile, zipfile\n"); |
| 1456 | fprintf(pyfp, "import torch\n"); |
| 1457 | fprintf(pyfp, "import torch.nn as nn\n"); |
| 1458 | fprintf(pyfp, "import torch.nn.functional as F\n"); |
| 1459 | fprintf(pyfp, "try:\n"); |
| 1460 | fprintf(pyfp, " import torchvision\n"); |
| 1461 | fprintf(pyfp, "except:\n"); |
| 1462 | fprintf(pyfp, " pass\n"); |
| 1463 | |
| 1464 | fprintf(pyfp, "\n"); |
| 1465 | |
| 1466 | fprintf(pyfp, "class Model(nn.Module):\n"); |
| 1467 | fprintf(pyfp, " def __init__(self):\n"); |
| 1468 | fprintf(pyfp, " super(Model, self).__init__()\n"); |
| 1469 | |
| 1470 | fprintf(pyfp, "\n"); |
| 1471 | |
| 1472 | // module |
| 1473 | { |
| 1474 | for (const Operator* op : ops) |
| 1475 | { |
| 1476 | if (op->type.substr(0, 3) != "nn." && op->type.substr(0, 16) != "torchvision.ops.") |
| 1477 | continue; |
| 1478 | |
| 1479 | fprintf(pyfp, " self.%s = %s(", sanitize_identifier(op->name).c_str(), op->type.c_str()); |
| 1480 | |
| 1481 | int param_count = op->params.size(); |
| 1482 | if (op->type == "nn.quantized.Conv2d" || op->type == "nn.quantized.Linear") |
| 1483 | { |
| 1484 | param_count -= 2; // ignore scale and zero_point |
| 1485 | } |
| 1486 | |
| 1487 | int param_index = 0; |
| 1488 | for (const auto& it : op->params) |
| 1489 | { |
| 1490 | if (op->type == "nn.quantized.Conv2d" || op->type == "nn.quantized.Linear") |
| 1491 | { |
| 1492 | if (it.first == "scale" || it.first == "zero_point") |
| 1493 | continue; |
| 1494 | } |
| 1495 | |
| 1496 | fprintf(pyfp, "%s=", it.first.c_str()); |
| 1497 | |
| 1498 | const Parameter& param = it.second; |
| 1499 | if (param.type == 0) |
| 1500 | { |
| 1501 | fprintf(pyfp, "None"); |
| 1502 | } |
| 1503 | if (param.type == 1) |
| 1504 | { |
| 1505 | if (param.b) |
| 1506 | fprintf(pyfp, "True"); |
| 1507 | else |
| 1508 | fprintf(pyfp, "False"); |
| 1509 | } |
| 1510 | if (param.type == 2) |
| 1511 | { |
| 1512 | fprintf(pyfp, "%d", param.i); |
| 1513 | } |
| 1514 | if (param.type == 3) |
| 1515 | { |
| 1516 | fprintf(pyfp, "%f", param.f); |
| 1517 | } |
| 1518 | if (param.type == 4) |
| 1519 | { |
| 1520 | if (param.s.substr(0, 6) == "torch.") |
| 1521 | { |
| 1522 | fprintf(pyfp, "%s", param.s.c_str()); |
| 1523 | } |
| 1524 | else |
| 1525 | { |
| 1526 | fprintf(pyfp, "\'%s\'", param.s.c_str()); |
| 1527 | } |
| 1528 | } |
| 1529 | if (param.type == 5) |
| 1530 | { |
| 1531 | fprintf(pyfp, "("); |
| 1532 | for (size_t i = 0; i < param.ai.size(); i++) |
| 1533 | { |
| 1534 | if ((op->type == "nn.AdaptiveAvgPool2d" |
| 1535 | || op->type == "nn.AdaptiveAvgPool3d" |
| 1536 | || op->type == "nn.AdaptiveMaxPool2d" |
| 1537 | || op->type == "nn.AdaptiveMaxPool3d") |
| 1538 | && it.first == "output_size" && param.ai[i] == 0) |
| 1539 | { |
| 1540 | fprintf(pyfp, "None"); |
| 1541 | } |
| 1542 | else |
| 1543 | { |
| 1544 | fprintf(pyfp, "%d", param.ai[i]); |
| 1545 | } |
| 1546 | if (i + 1 != param.ai.size() || param.ai.size() == 1) |
| 1547 | fprintf(pyfp, ","); |
| 1548 | } |
| 1549 | fprintf(pyfp, ")"); |
| 1550 | } |
| 1551 | if (param.type == 6) |
| 1552 | { |
| 1553 | fprintf(pyfp, "("); |
| 1554 | for (size_t i = 0; i < param.af.size(); i++) |
| 1555 | { |
| 1556 | fprintf(pyfp, "%f", param.af[i]); |
| 1557 | if (i + 1 != param.af.size() || param.af.size() == 1) |
| 1558 | fprintf(pyfp, ","); |
| 1559 | } |
| 1560 | fprintf(pyfp, ")"); |
| 1561 | } |
| 1562 | if (param.type == 7) |
| 1563 | { |
| 1564 | fprintf(pyfp, "("); |
| 1565 | for (size_t i = 0; i < param.as.size(); i++) |
| 1566 | { |
| 1567 | if (param.as[i].substr(0, 6) == "torch.") |
| 1568 | { |
| 1569 | fprintf(pyfp, "%s", param.as[i].c_str()); |
| 1570 | } |
| 1571 | else |
| 1572 | { |
| 1573 | fprintf(pyfp, "\'%s\'", param.as[i].c_str()); |
| 1574 | } |
| 1575 | if (i + 1 != param.as.size() || param.as.size() == 1) |
| 1576 | fprintf(pyfp, ","); |
| 1577 | } |
| 1578 | fprintf(pyfp, ")"); |
| 1579 | } |
| 1580 | |
| 1581 | param_index++; |
| 1582 | if (param_index != param_count) |
| 1583 | fprintf(pyfp, ", "); |
| 1584 | } |
| 1585 | |
| 1586 | fprintf(pyfp, ")\n"); |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | fprintf(pyfp, "\n"); |
| 1591 | |
| 1592 | // load weights |
| 1593 | { |
| 1594 | fprintf(pyfp, " archive = zipfile.ZipFile('%s', 'r')\n", pnnxbinpath.c_str()); |
| 1595 | |
| 1596 | for (const Operator* op : ops) |
| 1597 | { |
| 1598 | if (op->type.substr(0, 3) != "nn." && op->type.substr(0, 16) != "torchvision.ops.") |
| 1599 | continue; |
| 1600 | |
| 1601 | if (op->type == "nn.quantized.Conv2d" || op->type == "nn.quantized.Linear") |
| 1602 | { |
| 1603 | for (const auto& it : op->attrs) |
| 1604 | { |
| 1605 | if (it.first == "weight" || it.first == "bias") |
| 1606 | { |
| 1607 | fprintf(pyfp, " self_%s_%s = self.load_pnnx_bin_as_parameter(archive, '%s.%s', (", sanitize_identifier(op->name).c_str(), it.first.c_str(), op->name.c_str(), it.first.c_str()); |
| 1608 | } |
| 1609 | else |
| 1610 | { |
| 1611 | // unknown attr |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
| 1615 | const Attribute& attr = it.second; |
| 1616 | for (size_t i = 0; i < attr.shape.size(); i++) |
| 1617 | { |
| 1618 | fprintf(pyfp, "%d", attr.shape[i]); |
| 1619 | if (i + 1 != attr.shape.size()) |
| 1620 | fprintf(pyfp, ","); |
| 1621 | } |
| 1622 | |
| 1623 | fprintf(pyfp, "), '%s', requires_grad=False)\n", type_to_numpy_string(attr.type)); |
| 1624 | } |
| 1625 | |
| 1626 | fprintf(pyfp, " self.%s.set_weight_bias(self_%s_weight, self_%s_bias)\n", sanitize_identifier(op->name).c_str(), sanitize_identifier(op->name).c_str(), sanitize_identifier(op->name).c_str()); |
| 1627 | fprintf(pyfp, " self.%s.scale = %f\n", sanitize_identifier(op->name).c_str(), op->params.at("scale").f); |
| 1628 | fprintf(pyfp, " self.%s.zero_point = %d\n", sanitize_identifier(op->name).c_str(), op->params.at("zero_point").i); |
| 1629 | |
| 1630 | continue; |
| 1631 | } |
| 1632 | |
| 1633 | for (const auto& it : op->attrs) |
| 1634 | { |
| 1635 | if (it.first == "running_mean" || it.first == "running_var") |
| 1636 | { |
| 1637 | fprintf(pyfp, " self.%s.%s = self.load_pnnx_bin_as_tensor(archive, '%s.%s', (", sanitize_identifier(op->name).c_str(), it.first.c_str(), op->name.c_str(), it.first.c_str()); |
| 1638 | } |
| 1639 | else |
| 1640 | { |
| 1641 | fprintf(pyfp, " self.%s.%s = self.load_pnnx_bin_as_parameter(archive, '%s.%s', (", sanitize_identifier(op->name).c_str(), it.first.c_str(), op->name.c_str(), it.first.c_str()); |
| 1642 | } |
| 1643 | |
| 1644 | const Attribute& attr = it.second; |
| 1645 | for (size_t i = 0; i < attr.shape.size(); i++) |
| 1646 | { |
| 1647 | fprintf(pyfp, "%d", attr.shape[i]); |
| 1648 | if (i + 1 != attr.shape.size()) |
| 1649 | fprintf(pyfp, ","); |
| 1650 | } |
| 1651 | |
| 1652 | if (attr.type == 1 || attr.type == 2 || attr.type == 3) |
| 1653 | { |
| 1654 | fprintf(pyfp, "), '%s')\n", type_to_numpy_string(attr.type)); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | fprintf(pyfp, "), '%s', requires_grad=False)\n", type_to_numpy_string(attr.type)); |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | for (const Operator* op : ops) |
| 1664 | { |
| 1665 | if (op->type != "pnnx.Attribute") |
| 1666 | continue; |
| 1667 | |
| 1668 | const std::string& key = op->attrs.begin()->first; |
| 1669 | const Attribute& attr = op->attrs.begin()->second; |
| 1670 | |
| 1671 | bool is_running_mean_var = false; |
| 1672 | { |
| 1673 | const Operand* r = op->outputs[0]; |
| 1674 | if (r->consumers.size() == 1) |
| 1675 | { |
| 1676 | const Operator* op2 = r->consumers[0]; |
| 1677 | if (op2->type == "F.batch_norm" || op2->type == "F.instance_norm") |
| 1678 | { |
| 1679 | if (r == op2->inputs[1] || r == op2->inputs[2]) |
| 1680 | { |
| 1681 | is_running_mean_var = true; |
| 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | bool is_empty = false; |
| 1688 | for (size_t i = 0; i < attr.shape.size(); i++) |
| 1689 | { |
| 1690 | if (attr.shape[i] == 0) |
| 1691 | is_empty = true; |
| 1692 | } |
| 1693 | |
| 1694 | if (is_empty) |
| 1695 | { |
| 1696 | fprintf(pyfp, " self.%s_%s = torch.from_numpy(np.empty((", sanitize_identifier(op->name).c_str(), sanitize_identifier(key).c_str()); |
| 1697 | |
| 1698 | for (size_t i = 0; i < attr.shape.size(); i++) |
| 1699 | { |
| 1700 | fprintf(pyfp, "%d,", attr.shape[i]); |
| 1701 | } |
| 1702 | |
| 1703 | fprintf(pyfp, "), dtype='%s'))\n", type_to_numpy_string(attr.type)); |
| 1704 | } |
| 1705 | else |
| 1706 | { |
| 1707 | if (is_running_mean_var) |
| 1708 | { |
| 1709 | fprintf(pyfp, " self.%s_%s = self.load_pnnx_bin_as_tensor(archive, '%s.%s', (", sanitize_identifier(op->name).c_str(), sanitize_identifier(key).c_str(), op->name.c_str(), key.c_str()); |
| 1710 | } |
| 1711 | else |
| 1712 | { |
| 1713 | fprintf(pyfp, " self.%s_%s = self.load_pnnx_bin_as_parameter(archive, '%s.%s', (", sanitize_identifier(op->name).c_str(), sanitize_identifier(key).c_str(), op->name.c_str(), key.c_str()); |
| 1714 | } |
| 1715 | |
| 1716 | for (size_t i = 0; i < attr.shape.size(); i++) |
| 1717 | { |
| 1718 | fprintf(pyfp, "%d,", attr.shape[i]); |
| 1719 | } |
| 1720 | |
| 1721 | if (attr.type == 1 || attr.type == 2 || attr.type == 3) |
| 1722 | { |
| 1723 | fprintf(pyfp, "), '%s')\n", type_to_numpy_string(attr.type)); |
| 1724 | } |
| 1725 | else |
| 1726 | { |
| 1727 | fprintf(pyfp, "), '%s', requires_grad=False)\n", type_to_numpy_string(attr.type)); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | fprintf(pyfp, " archive.close()\n"); |
| 1733 | } |
| 1734 | |
| 1735 | fprintf(pyfp, "\n"); |
| 1736 | |
| 1737 | // utility function |
| 1738 | { |
| 1739 | fprintf(pyfp, " def load_pnnx_bin_as_parameter(self, archive, key, shape, dtype, requires_grad=True):\n"); |
| 1740 | fprintf(pyfp, " return nn.Parameter(self.load_pnnx_bin_as_tensor(archive, key, shape, dtype), requires_grad)\n"); |
| 1741 | fprintf(pyfp, "\n"); |
| 1742 | fprintf(pyfp, " def load_pnnx_bin_as_tensor(self, archive, key, shape, dtype):\n"); |
| 1743 | fprintf(pyfp, " fd, tmppath = tempfile.mkstemp()\n"); |
| 1744 | fprintf(pyfp, " with os.fdopen(fd, 'wb') as tmpf, archive.open(key) as keyfile:\n"); |
| 1745 | fprintf(pyfp, " tmpf.write(keyfile.read())\n"); |
| 1746 | fprintf(pyfp, " m = np.memmap(tmppath, dtype=dtype, mode='r', shape=shape).copy()\n"); |
| 1747 | fprintf(pyfp, " os.remove(tmppath)\n"); |
| 1748 | fprintf(pyfp, " return torch.from_numpy(m)\n"); |
| 1749 | } |
| 1750 | |
| 1751 | fprintf(pyfp, "\n"); |
| 1752 | |
| 1753 | // def forward |
| 1754 | { |
| 1755 | fprintf(pyfp, " def forward(self"); |
| 1756 | |
| 1757 | for (const Operator* op : ops) |
| 1758 | { |
| 1759 | if (op->type != "pnnx.Input") |
| 1760 | continue; |
| 1761 | |
| 1762 | fprintf(pyfp, ", v_%s", sanitize_identifier(op->outputs[0]->name).c_str()); |
| 1763 | } |
| 1764 | |
| 1765 | fprintf(pyfp, "):\n"); |
| 1766 | } |
| 1767 | |
| 1768 | // forward body |
| 1769 | { |
| 1770 | for (const Operator* op : ops) |
| 1771 | { |
| 1772 | if (op->type == "pnnx.Input" || op->type == "pnnx.Output") |
| 1773 | continue; |
| 1774 | |
| 1775 | if (op->type == "pnnx.SliceIndexes") |
| 1776 | continue; |
| 1777 | |
| 1778 | fprintf(pyfp, " "); |
| 1779 | |
| 1780 | if (op->type == "pnnx.Expression") |
| 1781 | { |
| 1782 | // expr |
| 1783 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 1784 | { |
| 1785 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[i]->name).c_str()); |
| 1786 | if (i + 1 != op->outputs.size()) |
| 1787 | fprintf(pyfp, ", "); |
| 1788 | } |
| 1789 | std::string expanded_expr = expand_expression(op); |
| 1790 | fprintf(pyfp, " = %s\n", expanded_expr.c_str()); |
| 1791 | } |
| 1792 | else if (op->type == "pnnx.Attribute") |
| 1793 | { |
| 1794 | const std::string& key = op->attrs.begin()->first; |
| 1795 | fprintf(pyfp, "v_%s = self.%s_%s\n", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->name).c_str(), sanitize_identifier(key).c_str()); |
| 1796 | } |
| 1797 | else if (op->type == "Tensor.slice") |
| 1798 | { |
| 1799 | // slice expr |
| 1800 | std::string slice_expr = make_slice_expression(op); |
| 1801 | fprintf(pyfp, "v_%s = v_%s[%s]\n", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str(), slice_expr.c_str()); |
| 1802 | } |
| 1803 | else if (op->type == "Tensor.slice_copy") |
| 1804 | { |
| 1805 | // slice copy expr |
| 1806 | std::string slice_expr = make_slice_expression(op); |
| 1807 | fprintf(pyfp, "v_%s = v_%s\n", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str()); |
| 1808 | fprintf(pyfp, " v_%s[%s] = v_%s\n", sanitize_identifier(op->outputs[0]->name).c_str(), slice_expr.c_str(), sanitize_identifier(op->inputs[1]->name).c_str()); |
| 1809 | } |
| 1810 | else if (op->type == "Tensor.index") |
| 1811 | { |
| 1812 | // index expr |
| 1813 | if (op->inputs.size() == 2) |
| 1814 | { |
| 1815 | std::string expanded_expr = expand_expression(op->inputs[1]->producer); |
| 1816 | fprintf(pyfp, "v_%s = v_%s[%s]\n", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str(), expanded_expr.c_str()); |
| 1817 | } |
| 1818 | else |
| 1819 | { |
| 1820 | std::string index_expr = make_index_expression(op); |
| 1821 | fprintf(pyfp, "v_%s = v_%s[%s]\n", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str(), index_expr.c_str()); |
| 1822 | } |
| 1823 | } |
| 1824 | else if (op->type == "Tensor.expand") |
| 1825 | { |
| 1826 | // expand |
| 1827 | fprintf(pyfp, "v_%s = v_%s.%s(", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str(), op->type.substr(7).c_str()); |
| 1828 | if (op->inputs.size() == 2) |
| 1829 | { |
| 1830 | fprintf(pyfp, "*v_%s", sanitize_identifier(op->inputs[1]->name).c_str()); |
| 1831 | } |
| 1832 | else |
| 1833 | { |
| 1834 | const std::vector<int>& shape = op->params.at("shape").ai; |
| 1835 | for (size_t i = 0; i < shape.size(); i++) |
| 1836 | { |
| 1837 | fprintf(pyfp, "%d", shape[i]); |
| 1838 | if (i + 1 != shape.size()) |
| 1839 | fprintf(pyfp, ", "); |
| 1840 | } |
| 1841 | } |
| 1842 | fprintf(pyfp, ")\n"); |
| 1843 | } |
| 1844 | else if (op->type == "Tensor.view" || op->type == "Tensor.reshape") |
| 1845 | { |
| 1846 | // view reshape |
| 1847 | fprintf(pyfp, "v_%s = v_%s.%s(", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str(), op->type.substr(7).c_str()); |
| 1848 | if (op->inputs.size() == 2) |
| 1849 | { |
| 1850 | fprintf(pyfp, "*v_%s", sanitize_identifier(op->inputs[1]->name).c_str()); |
| 1851 | } |
| 1852 | else |
| 1853 | { |
| 1854 | const std::vector<int>& shape = op->params.at("shape").ai; |
| 1855 | for (size_t i = 0; i < shape.size(); i++) |
| 1856 | { |
| 1857 | fprintf(pyfp, "%d", shape[i]); |
| 1858 | if (i + 1 != shape.size()) |
| 1859 | fprintf(pyfp, ", "); |
| 1860 | } |
| 1861 | } |
| 1862 | fprintf(pyfp, ")\n"); |
| 1863 | } |
| 1864 | else if (op->type == "Tensor.repeat") |
| 1865 | { |
| 1866 | // view reshape |
| 1867 | fprintf(pyfp, "v_%s = v_%s.%s(", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->inputs[0]->name).c_str(), op->type.substr(7).c_str()); |
| 1868 | if (op->inputs.size() == 2) |
| 1869 | { |
| 1870 | fprintf(pyfp, "*v_%s", sanitize_identifier(op->inputs[1]->name).c_str()); |
| 1871 | } |
| 1872 | else |
| 1873 | { |
| 1874 | const std::vector<int>& sizes = op->params.at("sizes").ai; |
| 1875 | for (size_t i = 0; i < sizes.size(); i++) |
| 1876 | { |
| 1877 | fprintf(pyfp, "%d", sizes[i]); |
| 1878 | if (i + 1 != sizes.size()) |
| 1879 | fprintf(pyfp, ", "); |
| 1880 | } |
| 1881 | } |
| 1882 | fprintf(pyfp, ")\n"); |
| 1883 | } |
| 1884 | else if (op->type == "torch.cat" || op->type == "torch.stack") |
| 1885 | { |
| 1886 | // cat |
| 1887 | fprintf(pyfp, "v_%s = %s(", sanitize_identifier(op->outputs[0]->name).c_str(), op->type.c_str()); |
| 1888 | if (op->inputs.size() == 1) |
| 1889 | { |
| 1890 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 1891 | } |
| 1892 | else |
| 1893 | { |
| 1894 | fprintf(pyfp, "("); |
| 1895 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 1896 | { |
| 1897 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 1898 | if (i + 1 != op->inputs.size()) |
| 1899 | fprintf(pyfp, ", "); |
| 1900 | } |
| 1901 | fprintf(pyfp, ")"); |
| 1902 | } |
| 1903 | fprintf(pyfp, ", dim=%d", op->params.at("dim").i); |
| 1904 | fprintf(pyfp, ")\n"); |
| 1905 | } |
| 1906 | else if (op->type == "torch.einsum") |
| 1907 | { |
| 1908 | // einsum |
| 1909 | fprintf(pyfp, "v_%s = %s(", sanitize_identifier(op->outputs[0]->name).c_str(), op->type.c_str()); |
| 1910 | |
| 1911 | fprintf(pyfp, "\'%s\'", op->params.at("equation").s.c_str()); |
| 1912 | |
| 1913 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 1914 | { |
| 1915 | fprintf(pyfp, ", v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 1916 | } |
| 1917 | fprintf(pyfp, ")\n"); |
| 1918 | } |
| 1919 | else if (op->type == "prim::TupleUnpack") |
| 1920 | { |
| 1921 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 1922 | { |
| 1923 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[i]->name).c_str()); |
| 1924 | if (i + 1 != op->outputs.size()) |
| 1925 | fprintf(pyfp, ", "); |
| 1926 | } |
| 1927 | fprintf(pyfp, " = v_%s\n", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 1928 | } |
| 1929 | else if (op->type == "prim::TupleConstruct") |
| 1930 | { |
| 1931 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[0]->name).c_str()); |
| 1932 | fprintf(pyfp, " = ("); |
| 1933 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 1934 | { |
| 1935 | fprintf(pyfp, "v_%s, ", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 1936 | } |
| 1937 | fprintf(pyfp, ")\n"); |
| 1938 | } |
| 1939 | else if (op->type == "prim::ListUnpack") |
| 1940 | { |
| 1941 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 1942 | { |
| 1943 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[i]->name).c_str()); |
| 1944 | if (i + 1 != op->outputs.size()) |
| 1945 | fprintf(pyfp, ", "); |
| 1946 | } |
| 1947 | fprintf(pyfp, " = v_%s\n", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 1948 | } |
| 1949 | else if (op->type == "prim::ListConstruct") |
| 1950 | { |
| 1951 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[0]->name).c_str()); |
| 1952 | fprintf(pyfp, " = ["); |
| 1953 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 1954 | { |
| 1955 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 1956 | if (i + 1 != op->inputs.size()) |
| 1957 | fprintf(pyfp, ", "); |
| 1958 | } |
| 1959 | fprintf(pyfp, "]\n"); |
| 1960 | } |
| 1961 | else if (op->type == "nn.GRU" || op->type == "nn.RNN") |
| 1962 | { |
| 1963 | if (op->outputs.size() == 1) |
| 1964 | { |
| 1965 | fprintf(pyfp, "v_%s, _", sanitize_identifier(op->outputs[0]->name).c_str()); |
| 1966 | } |
| 1967 | else |
| 1968 | { |
| 1969 | fprintf(pyfp, "v_%s, v_%s", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->outputs[1]->name).c_str()); |
| 1970 | } |
| 1971 | fprintf(pyfp, " = self.%s(", sanitize_identifier(op->name).c_str()); |
| 1972 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 1973 | if (op->inputs.size() == 2) |
| 1974 | { |
| 1975 | fprintf(pyfp, ", v_%s", sanitize_identifier(op->inputs[1]->name).c_str()); |
| 1976 | } |
| 1977 | fprintf(pyfp, ")\n"); |
| 1978 | } |
| 1979 | else if (op->type == "nn.LSTM") |
| 1980 | { |
| 1981 | if (op->outputs.size() == 1) |
| 1982 | { |
| 1983 | fprintf(pyfp, "v_%s, _", sanitize_identifier(op->outputs[0]->name).c_str()); |
| 1984 | } |
| 1985 | else |
| 1986 | { |
| 1987 | fprintf(pyfp, "v_%s, (v_%s, v_%s)", sanitize_identifier(op->outputs[0]->name).c_str(), sanitize_identifier(op->outputs[1]->name).c_str(), sanitize_identifier(op->outputs[2]->name).c_str()); |
| 1988 | } |
| 1989 | fprintf(pyfp, " = self.%s(", sanitize_identifier(op->name).c_str()); |
| 1990 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 1991 | if (op->inputs.size() == 3) |
| 1992 | { |
| 1993 | fprintf(pyfp, ", (v_%s, v_%s)", sanitize_identifier(op->inputs[1]->name).c_str(), sanitize_identifier(op->inputs[2]->name).c_str()); |
| 1994 | } |
| 1995 | fprintf(pyfp, ")\n"); |
| 1996 | } |
| 1997 | else if (op->type == "nn.MultiheadAttention") |
| 1998 | { |
| 1999 | bool need_weights = true; |
| 2000 | if (op->outputs.size() == 1) |
| 2001 | { |
| 2002 | fprintf(pyfp, "v_%s, _", sanitize_identifier(op->outputs[0]->name).c_str()); |
| 2003 | need_weights = false; |
| 2004 | } |
| 2005 | else |
| 2006 | { |
| 2007 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 2008 | { |
| 2009 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[i]->name).c_str()); |
| 2010 | if (i + 1 != op->outputs.size()) |
| 2011 | fprintf(pyfp, ", "); |
| 2012 | } |
| 2013 | } |
| 2014 | fprintf(pyfp, " = self.%s(", sanitize_identifier(op->name).c_str()); |
| 2015 | if (op->inputs.size() == 1) |
| 2016 | { |
| 2017 | std::string in0 = sanitize_identifier(op->inputs[0]->name); |
| 2018 | fprintf(pyfp, "v_%s, v_%s, v_%s", in0.c_str(), in0.c_str(), in0.c_str()); |
| 2019 | } |
| 2020 | else if (op->inputs.size() == 2) |
| 2021 | { |
| 2022 | std::string in0 = sanitize_identifier(op->inputs[0]->name); |
| 2023 | std::string in1 = sanitize_identifier(op->inputs[1]->name); |
| 2024 | if (op->inputnames.size() == 2 && op->inputnames[1] == "attn_mask") |
| 2025 | { |
| 2026 | fprintf(pyfp, "v_%s, v_%s, v_%s, attn_mask=v_%s", in0.c_str(), in0.c_str(), in0.c_str(), in1.c_str()); |
| 2027 | } |
| 2028 | else |
| 2029 | { |
| 2030 | fprintf(pyfp, "v_%s, v_%s, v_%s", in0.c_str(), in1.c_str(), in1.c_str()); |
| 2031 | } |
| 2032 | } |
| 2033 | else if (op->inputs.size() == 3) |
| 2034 | { |
| 2035 | std::string in0 = sanitize_identifier(op->inputs[0]->name); |
| 2036 | std::string in1 = sanitize_identifier(op->inputs[1]->name); |
| 2037 | std::string in2 = sanitize_identifier(op->inputs[2]->name); |
| 2038 | if (op->inputnames.size() == 3 && op->inputnames[2] == "attn_mask") |
| 2039 | { |
| 2040 | fprintf(pyfp, "v_%s, v_%s, v_%s, attn_mask=v_%s", in0.c_str(), in1.c_str(), in1.c_str(), in2.c_str()); |
| 2041 | } |
| 2042 | else |
| 2043 | { |
| 2044 | fprintf(pyfp, "v_%s, v_%s, v_%s", in0.c_str(), in1.c_str(), in2.c_str()); |
| 2045 | } |
| 2046 | } |
| 2047 | else if (op->inputs.size() == 4) |
| 2048 | { |
| 2049 | std::string in0 = sanitize_identifier(op->inputs[0]->name); |
| 2050 | std::string in1 = sanitize_identifier(op->inputs[1]->name); |
| 2051 | std::string in2 = sanitize_identifier(op->inputs[2]->name); |
| 2052 | std::string in3 = sanitize_identifier(op->inputs[3]->name); |
| 2053 | if (op->inputnames.size() == 4 && op->inputnames[3] == "attn_mask") |
| 2054 | { |
| 2055 | fprintf(pyfp, "v_%s, v_%s, v_%s, attn_mask=v_%s", in0.c_str(), in1.c_str(), in2.c_str(), in3.c_str()); |
| 2056 | } |
| 2057 | else |
| 2058 | { |
| 2059 | fprintf(pyfp, "v_%s, v_%s, v_%s, v_%s", in0.c_str(), in1.c_str(), in2.c_str(), in3.c_str()); |
| 2060 | } |
| 2061 | } |
| 2062 | else |
| 2063 | { |
| 2064 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 2065 | { |
| 2066 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2067 | if (i + 1 != op->inputs.size()) |
| 2068 | fprintf(pyfp, ", "); |
| 2069 | } |
| 2070 | } |
| 2071 | if (need_weights) |
| 2072 | { |
| 2073 | fprintf(pyfp, ", need_weights=True"); |
| 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | fprintf(pyfp, ", need_weights=False"); |
| 2078 | } |
| 2079 | fprintf(pyfp, ")\n"); |
| 2080 | } |
| 2081 | else if (op->type.substr(0, 3) == "nn." || op->type.substr(0, 16) == "torchvision.ops.") |
| 2082 | { |
| 2083 | // self.xxx() |
| 2084 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 2085 | { |
| 2086 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[i]->name).c_str()); |
| 2087 | if (i + 1 != op->outputs.size()) |
| 2088 | fprintf(pyfp, ", "); |
| 2089 | } |
| 2090 | fprintf(pyfp, " = self.%s(", sanitize_identifier(op->name).c_str()); |
| 2091 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 2092 | { |
| 2093 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2094 | if (i + 1 != op->inputs.size()) |
| 2095 | fprintf(pyfp, ", "); |
| 2096 | } |
| 2097 | fprintf(pyfp, ")\n"); |
| 2098 | } |
| 2099 | else |
| 2100 | { |
| 2101 | if (op->type.find("::") == std::string::npos && op->type.find(".") == std::string::npos) |
| 2102 | { |
| 2103 | fprintf(stderr, "todo %s\n", op->type.c_str()); |
| 2104 | } |
| 2105 | |
| 2106 | // direct |
| 2107 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 2108 | { |
| 2109 | fprintf(pyfp, "v_%s", sanitize_identifier(op->outputs[i]->name).c_str()); |
| 2110 | if (i + 1 != op->outputs.size()) |
| 2111 | fprintf(pyfp, ", "); |
| 2112 | } |
| 2113 | |
| 2114 | if (op->type == "torch.max" || op->type == "torch.max") |
| 2115 | { |
| 2116 | if (op->has_param("dim") && op->outputs.size() == 1) |
| 2117 | { |
| 2118 | // torch.max and torch.min with dim returns tuple |
| 2119 | fprintf(pyfp, ", _"); |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | if (op->type.substr(0, 7) == "Tensor.") |
| 2124 | { |
| 2125 | if (op->type == "Tensor.fill") |
| 2126 | { |
| 2127 | fprintf(pyfp, " = v_%s.fill_(", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 2128 | } |
| 2129 | else |
| 2130 | { |
| 2131 | fprintf(pyfp, " = v_%s.%s(", sanitize_identifier(op->inputs[0]->name).c_str(), op->type.substr(7).c_str()); |
| 2132 | } |
| 2133 | |
| 2134 | if (op->inputnames.size() == op->inputs.size()) |
| 2135 | { |
| 2136 | for (size_t i = 1; i < op->inputs.size(); i++) |
| 2137 | { |
| 2138 | if (!op->inputnames[i].empty()) |
| 2139 | continue; |
| 2140 | |
| 2141 | fprintf(pyfp, "v_%s, ", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2142 | } |
| 2143 | |
| 2144 | for (size_t i = 1; i < op->inputs.size(); i++) |
| 2145 | { |
| 2146 | if (op->inputnames[i].empty()) |
| 2147 | continue; |
| 2148 | |
| 2149 | fprintf(pyfp, "%s=v_%s, ", op->inputnames[i].c_str(), sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2150 | } |
| 2151 | } |
| 2152 | else |
| 2153 | { |
| 2154 | for (size_t i = 1; i < op->inputs.size(); i++) |
| 2155 | { |
| 2156 | fprintf(pyfp, "v_%s, ", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | else |
| 2161 | { |
| 2162 | fprintf(pyfp, " = %s(", op->type.c_str()); |
| 2163 | |
| 2164 | if (op->inputnames.size() == op->inputs.size()) |
| 2165 | { |
| 2166 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 2167 | { |
| 2168 | if (!op->inputnames[i].empty()) |
| 2169 | continue; |
| 2170 | |
| 2171 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2172 | if (i + 1 != op->inputs.size()) |
| 2173 | fprintf(pyfp, ", "); |
| 2174 | } |
| 2175 | |
| 2176 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 2177 | { |
| 2178 | if (op->inputnames[i].empty()) |
| 2179 | continue; |
| 2180 | |
| 2181 | fprintf(pyfp, "%s=v_%s", op->inputnames[i].c_str(), sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2182 | if (i + 1 != op->inputs.size()) |
| 2183 | fprintf(pyfp, ", "); |
| 2184 | } |
| 2185 | } |
| 2186 | else |
| 2187 | { |
| 2188 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 2189 | { |
| 2190 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[i]->name).c_str()); |
| 2191 | if (i + 1 != op->inputs.size()) |
| 2192 | fprintf(pyfp, ", "); |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | int i = 0; |
| 2198 | for (const auto& it : op->params) |
| 2199 | { |
| 2200 | if (op->type.substr(0, 7) == "Tensor." && i == 0) |
| 2201 | { |
| 2202 | fprintf(pyfp, "%s=", it.first.c_str()); |
| 2203 | } |
| 2204 | else if (op->inputs.empty() && i == 0) |
| 2205 | { |
| 2206 | fprintf(pyfp, "%s=", it.first.c_str()); |
| 2207 | } |
| 2208 | else |
| 2209 | { |
| 2210 | fprintf(pyfp, ", %s=", it.first.c_str()); |
| 2211 | } |
| 2212 | |
| 2213 | i++; |
| 2214 | |
| 2215 | const Parameter& param = it.second; |
| 2216 | if (param.type == 0) |
| 2217 | { |
| 2218 | if (op->type == "Tensor.index_put" && it.first == "values") |
| 2219 | { |
| 2220 | fprintf(pyfp, "torch.tensor(False)"); |
| 2221 | } |
| 2222 | else |
| 2223 | { |
| 2224 | fprintf(pyfp, "None"); |
| 2225 | } |
| 2226 | } |
| 2227 | if (param.type == 1) |
| 2228 | { |
| 2229 | if (param.b) |
| 2230 | fprintf(pyfp, "True"); |
| 2231 | else |
| 2232 | fprintf(pyfp, "False"); |
| 2233 | } |
| 2234 | if (param.type == 2) |
| 2235 | { |
| 2236 | if (op->type == "Tensor.index_put" && it.first == "values") |
| 2237 | { |
| 2238 | fprintf(pyfp, "torch.tensor(%d)", param.i); |
| 2239 | } |
| 2240 | else |
| 2241 | { |
| 2242 | fprintf(pyfp, "%d", param.i); |
| 2243 | } |
| 2244 | } |
| 2245 | if (param.type == 3) |
| 2246 | { |
| 2247 | if (op->type == "Tensor.index_put" && it.first == "values") |
| 2248 | { |
| 2249 | fprintf(pyfp, "torch.tensor(%f)", param.f); |
| 2250 | } |
| 2251 | else |
| 2252 | { |
| 2253 | fprintf(pyfp, "%f", param.f); |
| 2254 | } |
| 2255 | } |
| 2256 | if (param.type == 4) |
| 2257 | { |
| 2258 | if (param.s.substr(0, 6) == "torch.") |
| 2259 | { |
| 2260 | fprintf(pyfp, "%s", param.s.c_str()); |
| 2261 | } |
| 2262 | else if (op->type == "Tensor.index_put" && it.first == "values") |
| 2263 | { |
| 2264 | if (param.s == "inf" || param.s == "-inf") |
| 2265 | { |
| 2266 | fprintf(pyfp, "torch.tensor(float(\'%s\'))", param.s.c_str()); |
| 2267 | } |
| 2268 | else |
| 2269 | { |
| 2270 | fprintf(pyfp, "torch.tensor(\'%s\')", param.s.c_str()); |
| 2271 | } |
| 2272 | } |
| 2273 | else |
| 2274 | { |
| 2275 | if (param.s == "inf" || param.s == "-inf") |
| 2276 | { |
| 2277 | fprintf(pyfp, "float(\'%s\')", param.s.c_str()); |
| 2278 | } |
| 2279 | else |
| 2280 | { |
| 2281 | fprintf(pyfp, "\'%s\'", param.s.c_str()); |
| 2282 | } |
| 2283 | } |
| 2284 | } |
| 2285 | if (param.type == 5) |
| 2286 | { |
| 2287 | fprintf(pyfp, "("); |
| 2288 | for (size_t i = 0; i < param.ai.size(); i++) |
| 2289 | { |
| 2290 | if ((op->type == "F.adaptive_avg_pool2d" |
| 2291 | || op->type == "F.adaptive_avg_pool3d" |
| 2292 | || op->type == "F.adaptive_max_pool2d" |
| 2293 | || op->type == "F.adaptive_max_pool3d") |
| 2294 | && it.first == "output_size" && param.ai[i] == 0) |
| 2295 | { |
| 2296 | fprintf(pyfp, "None"); |
| 2297 | } |
| 2298 | else |
| 2299 | { |
| 2300 | fprintf(pyfp, "%d", param.ai[i]); |
| 2301 | } |
| 2302 | if (i + 1 != param.ai.size() || param.ai.size() == 1) |
| 2303 | fprintf(pyfp, ","); |
| 2304 | } |
| 2305 | fprintf(pyfp, ")"); |
| 2306 | } |
| 2307 | if (param.type == 6) |
| 2308 | { |
| 2309 | fprintf(pyfp, "("); |
| 2310 | for (size_t i = 0; i < param.af.size(); i++) |
| 2311 | { |
| 2312 | fprintf(pyfp, "%f", param.af[i]); |
| 2313 | if (i + 1 != param.af.size() || param.af.size() == 1) |
| 2314 | fprintf(pyfp, ","); |
| 2315 | } |
| 2316 | fprintf(pyfp, ")"); |
| 2317 | } |
| 2318 | if (param.type == 7) |
| 2319 | { |
| 2320 | fprintf(pyfp, "("); |
| 2321 | for (size_t i = 0; i < param.as.size(); i++) |
| 2322 | { |
| 2323 | if (param.as[i].substr(0, 6) == "torch.") |
| 2324 | { |
| 2325 | fprintf(pyfp, "%s", param.as[i].c_str()); |
| 2326 | } |
| 2327 | else |
| 2328 | { |
| 2329 | fprintf(pyfp, "\'%s\'", param.as[i].c_str()); |
| 2330 | } |
| 2331 | if (i + 1 != param.as.size() || param.as.size() == 1) |
| 2332 | fprintf(pyfp, ","); |
| 2333 | } |
| 2334 | fprintf(pyfp, ")"); |
| 2335 | } |
| 2336 | if (param.type == 10) |
| 2337 | { |
| 2338 | fprintf(pyfp, "(%f%+fj)", param.c.real(), param.c.imag()); |
| 2339 | } |
| 2340 | if (param.type == 11) |
| 2341 | { |
| 2342 | fprintf(pyfp, "("); |
| 2343 | for (size_t i = 0; i < param.ac.size(); i++) |
| 2344 | { |
| 2345 | fprintf(pyfp, "(%f%+fj)", param.ac[i].real(), param.ac[i].imag()); |
| 2346 | if (i + 1 != param.ac.size() || param.ac.size() == 1) |
| 2347 | fprintf(pyfp, ","); |
| 2348 | } |
| 2349 | fprintf(pyfp, ")"); |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | fprintf(pyfp, ")\n"); |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | // return |
| 2359 | { |
| 2360 | fprintf(pyfp, " return "); |
| 2361 | |
| 2362 | int output_count = 0; |
| 2363 | { |
| 2364 | for (const Operator* op : ops) |
| 2365 | { |
| 2366 | if (op->type == "pnnx.Output") |
| 2367 | output_count++; |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | int output_index = 0; |
| 2372 | for (const Operator* op : ops) |
| 2373 | { |
| 2374 | if (op->type != "pnnx.Output") |
| 2375 | continue; |
| 2376 | |
| 2377 | fprintf(pyfp, "v_%s", sanitize_identifier(op->inputs[0]->name).c_str()); |
| 2378 | if (output_index + 1 != output_count) |
| 2379 | fprintf(pyfp, ", "); |
| 2380 | |
| 2381 | output_index++; |
| 2382 | } |
| 2383 | |
| 2384 | fprintf(pyfp, "\n"); |
| 2385 | } |
| 2386 | |
| 2387 | fprintf(pyfp, "\n"); |
| 2388 | |
| 2389 | // export torchscript |
| 2390 | { |
| 2391 | fprintf(pyfp, "def export_torchscript():\n"); |
| 2392 | fprintf(pyfp, " net = Model()\n"); |
| 2393 | fprintf(pyfp, " net.eval()\n"); |
| 2394 | fprintf(pyfp, "\n"); |
| 2395 | fprintf(pyfp, " torch.manual_seed(0)\n"); |
| 2396 | |
| 2397 | std::vector<std::string> input_names; |
| 2398 | for (const Operator* op : ops) |
| 2399 | { |
| 2400 | if (op->type != "pnnx.Input") |
| 2401 | continue; |
| 2402 | |
| 2403 | const Operand* r = op->outputs[0]; |
| 2404 | std::string input_name = std::string("v_") + sanitize_identifier(r->name); |
| 2405 | if (type_is_integer(r->type)) |
| 2406 | { |
| 2407 | fprintf(pyfp, " %s = torch.randint(10, (", input_name.c_str()); |
| 2408 | for (size_t i = 0; i < r->shape.size(); i++) |
| 2409 | { |
| 2410 | fprintf(pyfp, "%d", r->shape[i]); |
| 2411 | if (i + 1 != r->shape.size() || r->shape.size() == 1) |
| 2412 | fprintf(pyfp, ", "); |
| 2413 | } |
| 2414 | fprintf(pyfp, "), dtype=%s)\n", type_to_dtype_string(r->type)); |
| 2415 | } |
| 2416 | else |
| 2417 | { |
| 2418 | fprintf(pyfp, " %s = torch.rand(", input_name.c_str()); |
| 2419 | for (size_t i = 0; i < r->shape.size(); i++) |
| 2420 | { |
| 2421 | fprintf(pyfp, "%d, ", r->shape[i]); |
| 2422 | } |
| 2423 | fprintf(pyfp, "dtype=%s)\n", type_to_dtype_string(r->type)); |
| 2424 | } |
| 2425 | |
| 2426 | input_names.push_back(input_name); |
| 2427 | } |
| 2428 | |
| 2429 | fprintf(pyfp, "\n"); |
| 2430 | |
| 2431 | if (input_names.size() == 1) |
| 2432 | { |
| 2433 | fprintf(pyfp, " mod = torch.jit.trace(net, %s)\n", input_names[0].c_str()); |
| 2434 | } |
| 2435 | else |
| 2436 | { |
| 2437 | fprintf(pyfp, " mod = torch.jit.trace(net, ("); |
| 2438 | |
| 2439 | for (size_t i = 0; i < input_names.size(); i++) |
| 2440 | { |
| 2441 | fprintf(pyfp, "%s", input_names[i].c_str()); |
| 2442 | if (i + 1 != input_names.size()) |
| 2443 | fprintf(pyfp, ", "); |
| 2444 | } |
| 2445 | |
| 2446 | fprintf(pyfp, "))\n"); |
| 2447 | } |
| 2448 | |
| 2449 | fprintf(pyfp, " mod.save(\"%s.pt\")\n", pypath.c_str()); |
| 2450 | } |
| 2451 | |
| 2452 | fprintf(pyfp, "\n"); |
| 2453 | |
| 2454 | // export onnx |
| 2455 | { |
| 2456 | fprintf(pyfp, "def export_onnx():\n"); |
| 2457 | fprintf(pyfp, " net = Model()\n"); |
| 2458 | fprintf(pyfp, " net.eval()\n"); |
| 2459 | fprintf(pyfp, "\n"); |
| 2460 | fprintf(pyfp, " torch.manual_seed(0)\n"); |
| 2461 | |
| 2462 | std::vector<std::string> input_names; |
| 2463 | for (const Operator* op : ops) |
| 2464 | { |
| 2465 | if (op->type != "pnnx.Input") |
| 2466 | continue; |
| 2467 | |
| 2468 | const Operand* r = op->outputs[0]; |
| 2469 | std::string input_name = std::string("v_") + sanitize_identifier(r->name); |
| 2470 | if (type_is_integer(r->type)) |
| 2471 | { |
| 2472 | fprintf(pyfp, " %s = torch.randint(10, (", input_name.c_str()); |
| 2473 | for (size_t i = 0; i < r->shape.size(); i++) |
| 2474 | { |
| 2475 | fprintf(pyfp, "%d", r->shape[i]); |
| 2476 | if (i + 1 != r->shape.size() || r->shape.size() == 1) |
| 2477 | fprintf(pyfp, ", "); |
| 2478 | } |
| 2479 | fprintf(pyfp, "), dtype=%s)\n", type_to_dtype_string(r->type)); |
| 2480 | } |
| 2481 | else |
| 2482 | { |
| 2483 | fprintf(pyfp, " %s = torch.rand(", input_name.c_str()); |
| 2484 | for (size_t i = 0; i < r->shape.size(); i++) |
| 2485 | { |
| 2486 | fprintf(pyfp, "%d, ", r->shape[i]); |
| 2487 | } |
| 2488 | fprintf(pyfp, "dtype=%s)\n", type_to_dtype_string(r->type)); |
| 2489 | } |
| 2490 | |
| 2491 | input_names.push_back(input_name); |
| 2492 | } |
| 2493 | |
| 2494 | fprintf(pyfp, "\n"); |
| 2495 | |
| 2496 | // torch.onnx._export(net, v_0, "test_swin_t.onnx", export_params=True, opset_version=14, input_names=['in0'], output_names=['out0']) |
| 2497 | |
| 2498 | if (input_names.size() == 1) |
| 2499 | { |
| 2500 | fprintf(pyfp, " torch.onnx._export(net, %s", input_names[0].c_str()); |
| 2501 | } |
| 2502 | else |
| 2503 | { |
| 2504 | fprintf(pyfp, " torch.onnx._export(net, ("); |
| 2505 | |
| 2506 | for (size_t i = 0; i < input_names.size(); i++) |
| 2507 | { |
| 2508 | fprintf(pyfp, "%s", input_names[i].c_str()); |
| 2509 | if (i + 1 != input_names.size()) |
| 2510 | fprintf(pyfp, ", "); |
| 2511 | } |
| 2512 | |
| 2513 | fprintf(pyfp, ")"); |
| 2514 | } |
| 2515 | |
| 2516 | fprintf(pyfp, ", \"%s.onnx\", export_params=True, operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK, opset_version=13", pypath.c_str()); |
| 2517 | |
| 2518 | fprintf(pyfp, ", input_names=["); |
| 2519 | { |
| 2520 | int input_count = 0; |
| 2521 | { |
| 2522 | for (const Operator* op : ops) |
| 2523 | { |
| 2524 | if (op->type == "pnnx.Input") |
| 2525 | input_count++; |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | int input_index = 0; |
| 2530 | for (const Operator* op : ops) |
| 2531 | { |
| 2532 | if (op->type != "pnnx.Input") |
| 2533 | continue; |
| 2534 | |
| 2535 | fprintf(pyfp, "'in%d'", input_index); |
| 2536 | if (input_index + 1 != input_count) |
| 2537 | fprintf(pyfp, ", "); |
| 2538 | |
| 2539 | input_index++; |
| 2540 | } |
| 2541 | } |
| 2542 | fprintf(pyfp, "]"); |
| 2543 | |
| 2544 | fprintf(pyfp, ", output_names=["); |
| 2545 | { |
| 2546 | int output_count = 0; |
| 2547 | { |
| 2548 | for (const Operator* op : ops) |
| 2549 | { |
| 2550 | if (op->type == "pnnx.Output") |
| 2551 | output_count++; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | int output_index = 0; |
| 2556 | for (const Operator* op : ops) |
| 2557 | { |
| 2558 | if (op->type != "pnnx.Output") |
| 2559 | continue; |
| 2560 | |
| 2561 | fprintf(pyfp, "'out%d'", output_index); |
| 2562 | if (output_index + 1 != output_count) |
| 2563 | fprintf(pyfp, ", "); |
| 2564 | |
| 2565 | output_index++; |
| 2566 | } |
| 2567 | } |
| 2568 | fprintf(pyfp, "]"); |
| 2569 | |
| 2570 | fprintf(pyfp, ")\n"); |
| 2571 | } |
| 2572 | |
| 2573 | fprintf(pyfp, "\n"); |
| 2574 | |
| 2575 | // test inference |
| 2576 | { |
| 2577 | fprintf(pyfp, "def test_inference():\n"); |
| 2578 | fprintf(pyfp, " net = Model()\n"); |
| 2579 | fprintf(pyfp, " net.eval()\n"); |
| 2580 | fprintf(pyfp, "\n"); |
| 2581 | fprintf(pyfp, " torch.manual_seed(0)\n"); |
| 2582 | |
| 2583 | std::vector<std::string> input_names; |
| 2584 | for (const Operator* op : ops) |
| 2585 | { |
| 2586 | if (op->type != "pnnx.Input") |
| 2587 | continue; |
| 2588 | |
| 2589 | const Operand* r = op->outputs[0]; |
| 2590 | std::string input_name = std::string("v_") + sanitize_identifier(r->name); |
| 2591 | if (type_is_integer(r->type)) |
| 2592 | { |
| 2593 | fprintf(pyfp, " %s = torch.randint(10, (", input_name.c_str()); |
| 2594 | for (size_t i = 0; i < r->shape.size(); i++) |
| 2595 | { |
| 2596 | fprintf(pyfp, "%d", r->shape[i]); |
| 2597 | if (i + 1 != r->shape.size() || r->shape.size() == 1) |
| 2598 | fprintf(pyfp, ", "); |
| 2599 | } |
| 2600 | fprintf(pyfp, "), dtype=%s)\n", type_to_dtype_string(r->type)); |
| 2601 | } |
| 2602 | else |
| 2603 | { |
| 2604 | fprintf(pyfp, " %s = torch.rand(", input_name.c_str()); |
| 2605 | for (size_t i = 0; i < r->shape.size(); i++) |
| 2606 | { |
| 2607 | fprintf(pyfp, "%d, ", r->shape[i]); |
| 2608 | } |
| 2609 | fprintf(pyfp, "dtype=%s)\n", type_to_dtype_string(r->type)); |
| 2610 | } |
| 2611 | |
| 2612 | input_names.push_back(input_name); |
| 2613 | } |
| 2614 | |
| 2615 | fprintf(pyfp, "\n"); |
| 2616 | |
| 2617 | if (input_names.size() == 1) |
| 2618 | { |
| 2619 | fprintf(pyfp, " return net(%s)\n", input_names[0].c_str()); |
| 2620 | } |
| 2621 | else |
| 2622 | { |
| 2623 | fprintf(pyfp, " return net("); |
| 2624 | |
| 2625 | for (size_t i = 0; i < input_names.size(); i++) |
| 2626 | { |
| 2627 | fprintf(pyfp, "%s", input_names[i].c_str()); |
| 2628 | if (i + 1 != input_names.size()) |
| 2629 | fprintf(pyfp, ", "); |
| 2630 | } |
| 2631 | |
| 2632 | fprintf(pyfp, ")\n"); |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | fprintf(pyfp, "\n"); |
| 2637 | |
| 2638 | // main |
| 2639 | { |
| 2640 | fprintf(pyfp, "if __name__ == \"__main__\":\n"); |
| 2641 | fprintf(pyfp, " print(test_inference())\n"); |
| 2642 | } |
| 2643 | |
| 2644 | fclose(pyfp); |
| 2645 | |
| 2646 | return 0; |
| 2647 | } |
| 2648 | |
| 2649 | int Graph::parse(const std::string& param) |
| 2650 | { |
| 2651 | std::istringstream is(param); |
| 2652 | if (!is.good()) |
| 2653 | { |
| 2654 | fprintf(stderr, "open failed\n"); |
| 2655 | return -1; |
| 2656 | } |
| 2657 | |
| 2658 | int magic = 0; |
| 2659 | { |
| 2660 | std::string line; |
| 2661 | std::getline(is, line); |
| 2662 | std::istringstream iss(line); |
| 2663 | |
| 2664 | iss >> magic; |
| 2665 | } |
| 2666 | |
| 2667 | int operator_count = 0; |
| 2668 | int operand_count = 0; |
| 2669 | { |
| 2670 | std::string line; |
| 2671 | std::getline(is, line); |
| 2672 | std::istringstream iss(line); |
| 2673 | |
| 2674 | iss >> operator_count >> operand_count; |
| 2675 | } |
| 2676 | |
| 2677 | for (int i = 0; i < operator_count; i++) |
| 2678 | { |
| 2679 | std::string line; |
| 2680 | std::getline(is, line); |
| 2681 | std::istringstream iss(line); |
| 2682 | |
| 2683 | std::string type; |
| 2684 | std::string name; |
| 2685 | int input_count = 0; |
| 2686 | int output_count = 0; |
| 2687 | |
| 2688 | iss >> type >> name >> input_count >> output_count; |
| 2689 | |
| 2690 | Operator* op = new_operator(type, name); |
| 2691 | |
| 2692 | for (int j = 0; j < input_count; j++) |
| 2693 | { |
| 2694 | std::string operand_name; |
| 2695 | iss >> operand_name; |
| 2696 | |
| 2697 | Operand* r = get_operand(operand_name); |
| 2698 | r->consumers.push_back(op); |
| 2699 | op->inputs.push_back(r); |
| 2700 | } |
| 2701 | |
| 2702 | for (int j = 0; j < output_count; j++) |
| 2703 | { |
| 2704 | std::string operand_name; |
| 2705 | iss >> operand_name; |
| 2706 | |
| 2707 | Operand* r = new_operand(operand_name); |
| 2708 | r->producer = op; |
| 2709 | op->outputs.push_back(r); |
| 2710 | } |
| 2711 | |
| 2712 | // key=value |
| 2713 | while (!iss.eof()) |
| 2714 | { |
| 2715 | std::string param; |
| 2716 | iss >> param; |
| 2717 | |
| 2718 | std::string key; |
| 2719 | std::string value; |
| 2720 | std::istringstream pss(param); |
| 2721 | std::getline(pss, key, '='); |
| 2722 | std::getline(pss, value); |
| 2723 | |
| 2724 | if (key[0] == '@') |
| 2725 | { |
| 2726 | // attribute |
| 2727 | // load_attribute(op, key.substr(1), value, szr); |
| 2728 | op->attrs[key.substr(1)] = Attribute(); |
| 2729 | |
| 2730 | Attribute& attr = op->attrs[key.substr(1)]; |
| 2731 | |
| 2732 | attr.type = 0; |
| 2733 | if (value.empty()) |
| 2734 | continue; |
| 2735 | |
| 2736 | if (value[0] == '%') |
| 2737 | { |
| 2738 | // @data=%op1.data |
| 2739 | attr.data = std::vector<char>(value.begin(), value.end()); |
| 2740 | } |
| 2741 | |
| 2742 | if (value[0] == '(') |
| 2743 | { |
| 2744 | // @data=(1,%c,?,4)f32 |
| 2745 | |
| 2746 | // type |
| 2747 | std::string typestr = value.substr(value.find_last_of(')') + 1); |
| 2748 | attr.type = string_to_type(typestr.c_str()); |
| 2749 | |
| 2750 | // shape |
| 2751 | std::string lc = value.substr(1, value.find_last_of(')') - 1); |
| 2752 | std::istringstream lcss(lc); |
| 2753 | |
| 2754 | attr.shape.clear(); |
| 2755 | while (!lcss.eof()) |
| 2756 | { |
| 2757 | std::string elem; |
| 2758 | std::getline(lcss, elem, ','); |
| 2759 | |
| 2760 | if (elem == "?") |
| 2761 | { |
| 2762 | attr.shape.push_back(-1); |
| 2763 | } |
| 2764 | else if (elem[0] == '%') |
| 2765 | { |
| 2766 | // encode %abc as symbolic tag |
| 2767 | attr.shape.push_back(-233); |
| 2768 | int index = attr.shape.size() - 1; |
| 2769 | std::string key = elem.substr(1); |
| 2770 | attr.params[std::string("__shape__") + std::to_string(index)] = key; |
| 2771 | } |
| 2772 | else |
| 2773 | { |
| 2774 | int i = std::stoi(elem); |
| 2775 | attr.shape.push_back(i); |
| 2776 | } |
| 2777 | } |
| 2778 | } |
| 2779 | } |
| 2780 | else if (key[0] == '$') |
| 2781 | { |
| 2782 | // operand input key |
| 2783 | load_input_key(op, key.substr(1), value); |
| 2784 | } |
| 2785 | else if (key[0] == '#') |
| 2786 | { |
| 2787 | // operand shape |
| 2788 | load_shape(op, key.substr(1), value); |
| 2789 | } |
| 2790 | else |
| 2791 | { |
| 2792 | // parameter |
| 2793 | load_parameter(op, key, value); |
| 2794 | } |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
| 2801 | void Operand::remove_consumer(const Operator* c) |
| 2802 | { |
| 2803 | auto it = std::find(consumers.begin(), consumers.end(), c); |
| 2804 | if (it != consumers.end()) |
| 2805 | consumers.erase(it); |
| 2806 | } |
| 2807 | |
| 2808 | Operator* Graph::new_operator(const std::string& type, const std::string& name) |
| 2809 | { |
| 2810 | Operator* op = new Operator; |
| 2811 | op->type = type; |
| 2812 | op->name = name; |
| 2813 | ops.push_back(op); |
| 2814 | return op; |
| 2815 | } |
| 2816 | |
| 2817 | Operator* Graph::new_operator_before(const std::string& type, const std::string& name, const Operator* cur) |
| 2818 | { |
| 2819 | Operator* op = new Operator; |
| 2820 | op->type = type; |
| 2821 | op->name = name; |
| 2822 | ops.insert(std::find(ops.begin(), ops.end(), cur), op); |
| 2823 | return op; |
| 2824 | } |
| 2825 | |
| 2826 | Operator* Graph::new_operator_after(const std::string& type, const std::string& name, const Operator* cur) |
| 2827 | { |
| 2828 | Operator* op = new Operator; |
| 2829 | op->type = type; |
| 2830 | op->name = name; |
| 2831 | ops.insert(std::find(ops.begin(), ops.end(), cur) + 1, op); |
| 2832 | return op; |
| 2833 | } |
| 2834 | |
| 2835 | Operand* Graph::new_operand(const std::string& name) |
| 2836 | { |
| 2837 | Operand* r = new Operand; |
| 2838 | r->name = name; |
| 2839 | operands.push_back(r); |
| 2840 | return r; |
| 2841 | } |
| 2842 | |
| 2843 | Operand* Graph::get_operand(const std::string& name) |
| 2844 | { |
| 2845 | for (Operand* r : operands) |
| 2846 | { |
| 2847 | if (r->name == name) |
| 2848 | return r; |
| 2849 | } |
| 2850 | |
| 2851 | return 0; |
| 2852 | } |
| 2853 | |
| 2854 | const Operand* Graph::get_operand(const std::string& name) const |
| 2855 | { |
| 2856 | for (const Operand* r : operands) |
| 2857 | { |
| 2858 | if (r->name == name) |
| 2859 | return r; |
| 2860 | } |
| 2861 | |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | } // namespace pnnx |