| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 <limits.h> |
| 16 | #include <map> |
| 17 | #include <set> |
| 18 | #include <stdint.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | class MXNetParam; |
| 25 | class MXNetNode |
| 26 | { |
| 27 | public: |
| 28 | bool has_attr(const char* key) const; |
| 29 | bool is_attr_scalar(const char* key) const; |
| 30 | |
| 31 | class AttrProxy |
| 32 | { |
| 33 | MXNetNode const* _n; |
| 34 | const char* const _key; |
| 35 | |
| 36 | public: |
| 37 | AttrProxy(MXNetNode const* n, const char* key) |
| 38 | : _n(n), _key(key) |
| 39 | { |
| 40 | } |
| 41 | operator int() const |
| 42 | { |
| 43 | return _n->attr_i(_key); |
| 44 | } |
| 45 | operator float() const |
| 46 | { |
| 47 | return _n->attr_f(_key); |
| 48 | } |
| 49 | operator std::string() const |
| 50 | { |
| 51 | return _n->attr_s(_key); |
| 52 | } |
| 53 | operator std::vector<int>() const |
| 54 | { |
| 55 | return _n->attr_ai(_key); |
| 56 | } |
| 57 | operator std::vector<float>() const |
| 58 | { |
| 59 | return _n->attr_af(_key); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | AttrProxy attr(const char* key) const |
| 64 | { |
| 65 | return AttrProxy(this, key); |
| 66 | } |
| 67 | |
| 68 | int attr_i(const char* key) const; |
| 69 | float attr_f(const char* key) const; |
| 70 | std::string attr_s(const char* key) const; |
| 71 | std::vector<int> attr_ai(const char* key) const; |
| 72 | std::vector<float> attr_af(const char* key) const; |
| 73 | |
| 74 | public: |
| 75 | bool is_weight() const; |
| 76 | bool has_weight(int i) const; |
| 77 | std::vector<float> weight(int i, int init_len = 0) const; |
| 78 | |
| 79 | std::vector<MXNetNode>* nodes; // reference |
| 80 | std::vector<MXNetParam>* params; // reference |
| 81 | |
| 82 | public: |
| 83 | std::string op; |
| 84 | std::string name; |
| 85 | int output_size; |
| 86 | std::map<std::string, std::string> attrs; |
| 87 | std::vector<int> inputs; |
| 88 | std::vector<int> subinputs; |
| 89 | std::vector<int> weights; |
| 90 | }; |
| 91 | |
| 92 | class MXNetParam |
| 93 | { |
| 94 | public: |
| 95 | std::string name; |
| 96 | std::vector<float> data; |
| 97 | std::string init; |
| 98 | }; |
| 99 | |
| 100 | bool MXNetNode::has_attr(const char* key) const |
| 101 | { |
| 102 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 103 | return it != attrs.end(); |
| 104 | } |
| 105 | |
| 106 | bool MXNetNode::is_attr_scalar(const char* key) const |
| 107 | { |
| 108 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 109 | if (it == attrs.end()) |
| 110 | return false; |
| 111 | |
| 112 | if (it->second.empty()) |
| 113 | return false; |
| 114 | |
| 115 | return it->second[0] != '('; |
| 116 | } |
| 117 | |
| 118 | int MXNetNode::attr_i(const char* key) const |
| 119 | { |
| 120 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 121 | if (it == attrs.end()) |
| 122 | return 0; |
| 123 | |
| 124 | if (it->second == "False") |
| 125 | return 0; |
| 126 | |
| 127 | if (it->second == "True") |
| 128 | return 1; |
| 129 | |
| 130 | int i = 0; |
| 131 | int nscan = sscanf(it->second.c_str(), "%d", &i); |
| 132 | if (nscan != 1) |
| 133 | return 0; |
| 134 | |
| 135 | return i; |
| 136 | } |
| 137 | |
| 138 | float MXNetNode::attr_f(const char* key) const |
| 139 | { |
| 140 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 141 | if (it == attrs.end()) |
| 142 | return 0.f; |
| 143 | |
| 144 | float f = 0; |
| 145 | int nscan = sscanf(it->second.c_str(), "%f", &f); |
| 146 | if (nscan != 1) |
| 147 | return 0.f; |
| 148 | |
| 149 | return f; |
| 150 | } |
| 151 | |
| 152 | std::string MXNetNode::attr_s(const char* key) const |
| 153 | { |
| 154 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 155 | if (it == attrs.end()) |
| 156 | return std::string(); |
| 157 | |
| 158 | return it->second; |
| 159 | } |
| 160 | |
| 161 | std::vector<int> MXNetNode::attr_ai(const char* key) const |
| 162 | { |
| 163 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 164 | if (it == attrs.end()) |
| 165 | return std::vector<int>(); |
| 166 | |
| 167 | // (1,2,3) |
| 168 | std::vector<int> list; |
| 169 | |
| 170 | if (is_attr_scalar(key)) |
| 171 | { |
| 172 | list.push_back(attr_i(key)); |
| 173 | return list; |
| 174 | } |
| 175 | |
| 176 | int i = 0; |
| 177 | int c = 0; |
| 178 | int nconsumed = 0; |
| 179 | int nscan = sscanf(it->second.c_str() + c, "%*[\\[(,]%d%n", &i, &nconsumed); |
| 180 | if (nscan != 1) |
| 181 | { |
| 182 | // (None |
| 183 | if (strncmp(it->second.c_str() + c, "(None", 5) == 0) |
| 184 | { |
| 185 | i = -233; |
| 186 | nconsumed = 5; |
| 187 | nscan = 1; |
| 188 | } |
| 189 | } |
| 190 | while (nscan == 1) |
| 191 | { |
| 192 | list.push_back(i); |
| 193 | // fprintf(stderr, "%d\n", i); |
| 194 | |
| 195 | i = 0; |
| 196 | c += nconsumed; |
| 197 | nscan = sscanf(it->second.c_str() + c, "%*[(,]%d%n", &i, &nconsumed); |
| 198 | if (nscan != 1) |
| 199 | { |
| 200 | // , None |
| 201 | if (strncmp(it->second.c_str() + c, ", None", 6) == 0) |
| 202 | { |
| 203 | i = -233; |
| 204 | nconsumed = 6; |
| 205 | nscan = 1; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return list; |
| 211 | } |
| 212 | |
| 213 | std::vector<float> MXNetNode::attr_af(const char* key) const |
| 214 | { |
| 215 | const std::map<std::string, std::string>::const_iterator it = attrs.find(key); |
| 216 | if (it == attrs.end()) |
| 217 | return std::vector<float>(); |
| 218 | |
| 219 | // (0.1,0.2,0.3) |
| 220 | std::vector<float> list; |
| 221 | |
| 222 | if (is_attr_scalar(key)) |
| 223 | { |
| 224 | list.push_back(attr_f(key)); |
| 225 | return list; |
| 226 | } |
| 227 | |
| 228 | float i = 0.f; |
| 229 | int c = 0; |
| 230 | int nconsumed = 0; |
| 231 | int nscan = sscanf(it->second.c_str() + c, "%*[(,]%f%n", &i, &nconsumed); |
| 232 | while (nscan == 1) |
| 233 | { |
| 234 | list.push_back(i); |
| 235 | // fprintf(stderr, "%f\n", i); |
| 236 | |
| 237 | i = 0.f; |
| 238 | c += nconsumed; |
| 239 | nscan = sscanf(it->second.c_str() + c, "%*[(,]%f%n", &i, &nconsumed); |
| 240 | } |
| 241 | |
| 242 | return list; |
| 243 | } |
| 244 | |
| 245 | bool MXNetNode::is_weight() const |
| 246 | { |
| 247 | for (int i = 0; i < (int)(*params).size(); i++) |
| 248 | { |
| 249 | const MXNetParam& p = (*params)[i]; |
| 250 | if (p.name == name) |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | bool MXNetNode::has_weight(int i) const |
| 258 | { |
| 259 | if (i < 0 || i >= (int)weights.size()) |
| 260 | return false; |
| 261 | |
| 262 | const std::string& node_name = (*nodes)[weights[i]].name; |
| 263 | |
| 264 | for (int j = 0; j < (int)(*params).size(); j++) |
| 265 | { |
| 266 | const MXNetParam& p = (*params)[j]; |
| 267 | if (p.name == node_name) |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | std::vector<float> MXNetNode::weight(int i, int init_len) const |
| 275 | { |
| 276 | if (i < 0 || i >= (int)weights.size()) |
| 277 | return std::vector<float>(); |
| 278 | |
| 279 | const std::string& node_name = (*nodes)[weights[i]].name; |
| 280 | |
| 281 | for (int j = 0; j < (int)(*params).size(); j++) |
| 282 | { |
| 283 | const MXNetParam& p = (*params)[j]; |
| 284 | if (p.name != node_name) |
| 285 | continue; |
| 286 | |
| 287 | if (!p.data.empty()) |
| 288 | return p.data; |
| 289 | |
| 290 | std::vector<float> data; |
| 291 | |
| 292 | if (!p.init.empty() && init_len != 0) |
| 293 | { |
| 294 | if (p.init == "[\\$zero\\$, {}]" || p.init == "[\\\"zero\\\", {}]" || p.init == "zeros") |
| 295 | { |
| 296 | data.resize(init_len, 0.f); |
| 297 | } |
| 298 | else if (p.init == "[\\$one\\$, {}]" || p.init == "[\\\"one\\\", {}]" || p.init == "ones") |
| 299 | { |
| 300 | data.resize(init_len, 1.f); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return data; |
| 305 | } |
| 306 | |
| 307 | return std::vector<float>(); |
| 308 | } |
| 309 | |
| 310 | static void replace_backslash_doublequote_dollar(char* s) |
| 311 | { |
| 312 | char* a = s; |
| 313 | char* b = s + 1; |
| 314 | while (*a && *b) |
| 315 | { |
| 316 | if (*a == '\\' && *b == '\"') |
| 317 | { |
| 318 | *b = '$'; |
| 319 | } |
| 320 | |
| 321 | a++; |
| 322 | b++; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static void parse_input_list(const char* s, std::vector<int>& inputs, std::vector<int>& subinputs) |
| 327 | { |
| 328 | inputs.clear(); |
| 329 | subinputs.clear(); |
| 330 | |
| 331 | if (memcmp(s, "[]", 2) == 0) |
| 332 | return; |
| 333 | |
| 334 | int nscan = 0; |
| 335 | int nconsumed = 0; |
| 336 | |
| 337 | int id; |
| 338 | int subid; |
| 339 | |
| 340 | int c = 1; // skip leading [ |
| 341 | nscan = sscanf(s + c, "[%d, %d%n", &id, &subid, &nconsumed); |
| 342 | while (nscan == 2) |
| 343 | { |
| 344 | inputs.push_back(id); |
| 345 | subinputs.push_back(subid); |
| 346 | // fprintf(stderr, "%d %d\n", id, subid); |
| 347 | |
| 348 | c += nconsumed; |
| 349 | nscan = sscanf(s + c, "%*[^[][%d, %d%n", &id, &subid, &nconsumed); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static bool read_mxnet_json(const char* jsonpath, std::vector<MXNetNode>& nodes) |
| 354 | { |
| 355 | FILE* fp = fopen(jsonpath, "rb"); |
| 356 | if (!fp) |
| 357 | { |
| 358 | fprintf(stderr, "fopen %s failed\n", jsonpath); |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | int internal_unknown = 0; |
| 363 | int internal_underscore = 0; |
| 364 | |
| 365 | char line[1024]; |
| 366 | |
| 367 | //{ |
| 368 | char* s = fgets(line, 1024, fp); |
| 369 | if (!s) |
| 370 | { |
| 371 | fprintf(stderr, "fgets %s failed\n", jsonpath); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | MXNetNode n; |
| 376 | |
| 377 | bool in_nodes_list = false; |
| 378 | bool in_node_block = false; |
| 379 | bool in_attr_block = false; |
| 380 | bool in_inputs_block = false; |
| 381 | while (!feof(fp)) |
| 382 | { |
| 383 | char* t = fgets(line, 1024, fp); |
| 384 | if (!t) |
| 385 | break; |
| 386 | |
| 387 | if (in_inputs_block) |
| 388 | { |
| 389 | // ] |
| 390 | if (memcmp(line, " ]", 7) == 0) |
| 391 | { |
| 392 | in_inputs_block = false; |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | // [439, 0, 0], |
| 397 | int id; |
| 398 | int subid; |
| 399 | int nscan = sscanf(line, " [%d, %d", &id, &subid); |
| 400 | if (nscan == 2) |
| 401 | { |
| 402 | n.inputs.push_back(id); |
| 403 | n.subinputs.push_back(subid); |
| 404 | continue; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (in_attr_block) |
| 409 | { |
| 410 | // }, |
| 411 | if (memcmp(line, " }", 7) == 0) |
| 412 | { |
| 413 | in_attr_block = false; |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | // replace \" with \$ |
| 418 | replace_backslash_doublequote_dollar(line); |
| 419 | |
| 420 | // "kernel": "(7,7)", |
| 421 | char key[256] = {0}; |
| 422 | char value[256] = {0}; |
| 423 | int nscan = sscanf(line, " \"%255[^\"]\": \"%255[^\"]\"", key, value); |
| 424 | if (nscan == 2) |
| 425 | { |
| 426 | n.attrs[key] = value; |
| 427 | // fprintf(stderr, "# %s = %s\n", key, value); |
| 428 | continue; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (in_node_block) |
| 433 | { |
| 434 | // }, |
| 435 | if (memcmp(line, " }", 5) == 0) |
| 436 | { |
| 437 | // new node |
| 438 | if (n.name.empty()) |
| 439 | { |
| 440 | // assign default unknown name |
| 441 | char unknownname[256]; |
| 442 | sprintf(unknownname, "unknownncnn_%d", internal_unknown); |
| 443 | |
| 444 | n.name = unknownname; |
| 445 | |
| 446 | internal_unknown++; |
| 447 | } |
| 448 | if (n.name[0] == '_') |
| 449 | { |
| 450 | // workaround for potential duplicated _plus0 |
| 451 | char underscorename[256]; |
| 452 | sprintf(underscorename, "underscorencnn_%d%s", internal_underscore, n.name.c_str()); |
| 453 | |
| 454 | n.name = underscorename; |
| 455 | |
| 456 | internal_underscore++; |
| 457 | } |
| 458 | nodes.push_back(n); |
| 459 | |
| 460 | in_node_block = false; |
| 461 | continue; |
| 462 | } |
| 463 | |
| 464 | int nscan; |
| 465 | |
| 466 | // "op": "Convolution", |
| 467 | char op[256] = {0}; |
| 468 | nscan = sscanf(line, " \"op\": \"%255[^\"]\",", op); |
| 469 | if (nscan == 1) |
| 470 | { |
| 471 | n.op = op; |
| 472 | // fprintf(stderr, "op = %s\n", op); |
| 473 | continue; |
| 474 | } |
| 475 | |
| 476 | // "name": "conv0", |
| 477 | char name[256] = {0}; |
| 478 | nscan = sscanf(line, " \"name\": \"%255[^\"]\",", name); |
| 479 | if (nscan == 1) |
| 480 | { |
| 481 | n.name = name; |
| 482 | // fprintf(stderr, "name = %s\n", name); |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | // "inputs": [ |
| 487 | if (memcmp(line, " \"inputs\": [\n", 18) == 0) |
| 488 | { |
| 489 | in_inputs_block = true; |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | // "inputs": [] |
| 494 | char inputs[256] = {0}; |
| 495 | nscan = sscanf(line, " \"inputs\": %255[^\n]", inputs); |
| 496 | if (nscan == 1) |
| 497 | { |
| 498 | parse_input_list(inputs, n.inputs, n.subinputs); |
| 499 | // fprintf(stderr, "inputs = %s\n", inputs); |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | // "param": {}, |
| 504 | if (memcmp(line, " \"param\": {}", 17) == 0) |
| 505 | { |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | // replace \" with \$ |
| 510 | replace_backslash_doublequote_dollar(line); |
| 511 | |
| 512 | // "attr": {"__init__": "[\"zero\", {}]"}, |
| 513 | char key[256] = {0}; |
| 514 | char value[256] = {0}; |
| 515 | nscan = sscanf(line, " \"attr\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value); |
| 516 | if (nscan == 2) |
| 517 | { |
| 518 | n.attrs[key] = value; |
| 519 | // fprintf(stderr, "# %s = %s\n", key, value); |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | // "attrs": {"__init__": "[\"zero\", {}]"}, |
| 524 | nscan = sscanf(line, " \"attrs\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value); |
| 525 | if (nscan == 2) |
| 526 | { |
| 527 | n.attrs[key] = value; |
| 528 | // fprintf(stderr, "# %s = %s\n", key, value); |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | // "param": {"p": "0.5"}, |
| 533 | nscan = sscanf(line, " \"param\": {\"%255[^\"]\": \"%255[^\"]\"}", key, value); |
| 534 | if (nscan == 2) |
| 535 | { |
| 536 | n.attrs[key] = value; |
| 537 | // fprintf(stderr, "# %s = %s\n", key, value); |
| 538 | continue; |
| 539 | } |
| 540 | |
| 541 | // "attr": { |
| 542 | if (memcmp(line, " \"attr\": {", 15) == 0) |
| 543 | { |
| 544 | in_attr_block = true; |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | // "attrs": { |
| 549 | if (memcmp(line, " \"attrs\": {", 16) == 0) |
| 550 | { |
| 551 | in_attr_block = true; |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | // "param": { |
| 556 | if (memcmp(line, " \"param\": {", 16) == 0) |
| 557 | { |
| 558 | in_attr_block = true; |
| 559 | continue; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (in_nodes_list) |
| 564 | { |
| 565 | // ], |
| 566 | if (memcmp(line, " ],", 4) == 0) |
| 567 | { |
| 568 | in_nodes_list = false; |
| 569 | // all nodes parsed |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | // { |
| 574 | if (memcmp(line, " {", 5) == 0) |
| 575 | { |
| 576 | n = MXNetNode(); |
| 577 | |
| 578 | in_node_block = true; |
| 579 | continue; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | // "nodes": [ |
| 584 | if (memcmp(line, " \"nodes\": [", 12) == 0) |
| 585 | { |
| 586 | in_nodes_list = true; |
| 587 | continue; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | fclose(fp); |
| 592 | |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | static bool read_mxnet_param(const char* parampath, std::vector<MXNetParam>& params) |
| 597 | { |
| 598 | FILE* fp = fopen(parampath, "rb"); |
| 599 | if (!fp) |
| 600 | { |
| 601 | fprintf(stderr, "fopen %s failed\n", parampath); |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | size_t nread; |
| 606 | uint64_t header; |
| 607 | uint64_t reserved; |
| 608 | nread = fread(&header, sizeof(uint64_t), 1, fp); |
| 609 | if (nread != 1) |
| 610 | { |
| 611 | fprintf(stderr, "read header failed %zd\n", nread); |
| 612 | return false; |
| 613 | } |
| 614 | nread = fread(&reserved, sizeof(uint64_t), 1, fp); |
| 615 | if (nread != 1) |
| 616 | { |
| 617 | fprintf(stderr, "read reserved failed %zd\n", nread); |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | // NDArray vec |
| 622 | |
| 623 | // each data |
| 624 | uint64_t data_count; |
| 625 | nread = fread(&data_count, sizeof(uint64_t), 1, fp); |
| 626 | if (nread != 1) |
| 627 | { |
| 628 | fprintf(stderr, "read data_count failed %zd\n", nread); |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | // fprintf(stderr, "data count = %d\n", (int)data_count); |
| 633 | |
| 634 | for (int i = 0; i < (int)data_count; i++) |
| 635 | { |
| 636 | uint32_t magic; // 0xF993FAC9 |
| 637 | nread = fread(&magic, sizeof(uint32_t), 1, fp); |
| 638 | if (nread != 1) |
| 639 | { |
| 640 | fprintf(stderr, "read magic failed %zd\n", nread); |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | // shape |
| 645 | uint32_t ndim; |
| 646 | std::vector<int64_t> shape; |
| 647 | |
| 648 | if (magic == 0xF993FAC9) |
| 649 | { |
| 650 | int32_t stype; |
| 651 | nread = fread(&stype, sizeof(int32_t), 1, fp); |
| 652 | if (nread != 1) |
| 653 | { |
| 654 | fprintf(stderr, "read stype failed %zd\n", nread); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | nread = fread(&ndim, sizeof(uint32_t), 1, fp); |
| 659 | if (nread != 1) |
| 660 | { |
| 661 | fprintf(stderr, "read ndim failed %zd\n", nread); |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | shape.resize(ndim); |
| 666 | nread = fread(&shape[0], ndim * sizeof(int64_t), 1, fp); |
| 667 | if (nread != 1) |
| 668 | { |
| 669 | fprintf(stderr, "read shape failed %zd\n", nread); |
| 670 | return false; |
| 671 | } |
| 672 | } |
| 673 | else if (magic == 0xF993FAC8) |
| 674 | { |
| 675 | nread = fread(&ndim, sizeof(uint32_t), 1, fp); |
| 676 | if (nread != 1) |
| 677 | { |
| 678 | fprintf(stderr, "read ndim failed %zd\n", nread); |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | shape.resize(ndim); |
| 683 | nread = fread(&shape[0], ndim * sizeof(int64_t), 1, fp); |
| 684 | if (nread != 1) |
| 685 | { |
| 686 | fprintf(stderr, "read shape failed %zd\n", nread); |
| 687 | return false; |
| 688 | } |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | ndim = magic; |
| 693 | |
| 694 | shape.resize(ndim); |
| 695 | |
| 696 | std::vector<uint32_t> shape32; |
| 697 | shape32.resize(ndim); |
| 698 | nread = fread(&shape32[0], ndim * sizeof(uint32_t), 1, fp); |
| 699 | if (nread != 1) |
| 700 | { |
| 701 | fprintf(stderr, "read shape failed %zd\n", nread); |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | for (int j = 0; j < (int)ndim; j++) |
| 706 | { |
| 707 | shape[j] = shape32[j]; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // context |
| 712 | int32_t dev_type; |
| 713 | int32_t dev_id; |
| 714 | nread = fread(&dev_type, sizeof(int32_t), 1, fp); |
| 715 | if (nread != 1) |
| 716 | { |
| 717 | fprintf(stderr, "read dev_type failed %zd\n", nread); |
| 718 | return false; |
| 719 | } |
| 720 | nread = fread(&dev_id, sizeof(int32_t), 1, fp); |
| 721 | if (nread != 1) |
| 722 | { |
| 723 | fprintf(stderr, "read dev_id failed %zd\n", nread); |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | int32_t type_flag; |
| 728 | nread = fread(&type_flag, sizeof(int32_t), 1, fp); |
| 729 | if (nread != 1) |
| 730 | { |
| 731 | fprintf(stderr, "read type_flag failed %zd\n", nread); |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | // data |
| 736 | size_t len = 0; |
| 737 | if (shape.size() == 1) len = shape[0]; |
| 738 | if (shape.size() == 2) len = shape[0] * shape[1]; |
| 739 | if (shape.size() == 3) len = shape[0] * shape[1] * shape[2]; |
| 740 | if (shape.size() == 4) len = shape[0] * shape[1] * shape[2] * shape[3]; |
| 741 | |
| 742 | MXNetParam p; |
| 743 | |
| 744 | p.data.resize(len); |
| 745 | nread = fread(&p.data[0], len * sizeof(float), 1, fp); |
| 746 | if (nread != 1) |
| 747 | { |
| 748 | fprintf(stderr, "read MXNetParam data failed %zd\n", nread); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | params.push_back(p); |
| 753 | |
| 754 | // fprintf(stderr, "%u read\n", len); |
| 755 | } |
| 756 | |
| 757 | // each name |
| 758 | uint64_t name_count; |
| 759 | nread = fread(&name_count, sizeof(uint64_t), 1, fp); |
| 760 | if (nread != 1) |
| 761 | { |
| 762 | fprintf(stderr, "read name_count failed %zd\n", nread); |
| 763 | return false; |
| 764 | } |
| 765 | |
| 766 | // fprintf(stderr, "name count = %d\n", (int)name_count); |
| 767 | |
| 768 | for (int i = 0; i < (int)name_count; i++) |
| 769 | { |
| 770 | uint64_t len; |
| 771 | nread = fread(&len, sizeof(uint64_t), 1, fp); |
| 772 | if (nread != 1) |
| 773 | { |
| 774 | fprintf(stderr, "read name length failed %zd\n", nread); |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | MXNetParam& p = params[i]; |
| 779 | |
| 780 | p.name.resize(len); |
| 781 | nread = fread((char*)p.name.data(), len, 1, fp); |
| 782 | if (nread != 1) |
| 783 | { |
| 784 | fprintf(stderr, "read MXNetParam name failed %zd\n", nread); |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | // cut leading arg: |
| 789 | if (memcmp(p.name.c_str(), "arg:", 4) == 0) |
| 790 | { |
| 791 | p.name = std::string(p.name.c_str() + 4); |
| 792 | } |
| 793 | if (memcmp(p.name.c_str(), "aux:", 4) == 0) |
| 794 | { |
| 795 | p.name = std::string(p.name.c_str() + 4); |
| 796 | } |
| 797 | |
| 798 | // fprintf(stderr, "%s read\n", p.name.c_str()); |
| 799 | } |
| 800 | |
| 801 | fclose(fp); |
| 802 | |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | static void fuse_shufflechannel(std::vector<MXNetNode>& nodes, std::vector<MXNetParam>& params, std::map<size_t, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 807 | { |
| 808 | size_t node_count = nodes.size(); |
| 809 | for (size_t i = 0; i < node_count; i++) |
| 810 | { |
| 811 | const MXNetNode& n = nodes[i]; |
| 812 | |
| 813 | if (n.is_weight()) |
| 814 | continue; |
| 815 | |
| 816 | // ShuffleChannel <= Reshape - SwapAxis - Reshape |
| 817 | if (n.op == "Reshape") |
| 818 | { |
| 819 | if (node_reference.find(i) == node_reference.end() || node_reference[i] != 1) |
| 820 | continue; |
| 821 | |
| 822 | // "shape": "(0, -4, X, -1, -2)" |
| 823 | std::vector<int> shape = n.attr("shape"); |
| 824 | if (shape.size() != 5) |
| 825 | continue; |
| 826 | if (shape[0] != 0 || shape[1] != -4 || shape[3] != -1 || shape[4] != -2) |
| 827 | continue; |
| 828 | |
| 829 | if (i + 2 >= node_count) |
| 830 | continue; |
| 831 | |
| 832 | const MXNetNode& n2 = nodes[i + 1]; |
| 833 | const MXNetNode& n3 = nodes[i + 2]; |
| 834 | |
| 835 | if (n2.op != "SwapAxis" || n3.op != "Reshape") |
| 836 | continue; |
| 837 | |
| 838 | if (node_reference.find(i + 1) == node_reference.end() || node_reference[i + 1] != 1) |
| 839 | continue; |
| 840 | |
| 841 | // "dim1": "1", "dim2": "2" |
| 842 | int dim1 = n2.attr("dim1"); |
| 843 | int dim2 = n2.attr("dim2"); |
| 844 | if (dim1 != 1 || dim2 != 2) |
| 845 | continue; |
| 846 | |
| 847 | // "shape": "(0, -3, -2)" |
| 848 | std::vector<int> shape3 = n3.attr("shape"); |
| 849 | if (shape3.size() != 3) |
| 850 | continue; |
| 851 | if (shape3[0] != 0 || shape3[1] != -3 || shape3[2] != -2) |
| 852 | continue; |
| 853 | |
| 854 | // reduce |
| 855 | nodes[i].op = "noop_reducedncnn"; |
| 856 | nodes[i + 1].op = "noop_reducedncnn"; |
| 857 | |
| 858 | node_reference.erase(node_reference.find(i)); |
| 859 | node_reference.erase(node_reference.find(i + 1)); |
| 860 | blob_names.erase(n.name); |
| 861 | blob_names.erase(n2.name); |
| 862 | |
| 863 | MXNetNode new_node; |
| 864 | new_node.nodes = &nodes; |
| 865 | new_node.params = ¶ms; |
| 866 | new_node.op = "ShuffleChannel"; |
| 867 | // new_node.name = n.name + "_" + n2.name + "_" + n3.name; |
| 868 | new_node.name = n3.name; |
| 869 | new_node.output_size = n3.output_size; |
| 870 | char group[16]; |
| 871 | sprintf(group, "%d", shape[2]); |
| 872 | new_node.attrs["group"] = group; |
| 873 | new_node.inputs = n.inputs; |
| 874 | new_node.subinputs = n.subinputs; |
| 875 | |
| 876 | nodes[i + 2] = new_node; |
| 877 | |
| 878 | reduced_node_count += 2; |
| 879 | i += 2; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | static void fuse_hardsigmoid_hardswish(std::vector<MXNetNode>& nodes, std::vector<MXNetParam>& params, std::map<size_t, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 885 | { |
| 886 | size_t node_count = nodes.size(); |
| 887 | for (size_t i = 0; i < node_count; i++) |
| 888 | { |
| 889 | const MXNetNode& n = nodes[i]; |
| 890 | |
| 891 | if (n.is_weight()) |
| 892 | continue; |
| 893 | |
| 894 | if (n.op == "_plus_scalar") |
| 895 | { |
| 896 | // HardSigmoid <= _plus_scalar(+3) - clip(0,6) - _div_scalar(/6) |
| 897 | const MXNetNode& n1 = nodes[i + 1]; |
| 898 | const MXNetNode& n2 = nodes[i + 2]; |
| 899 | const MXNetNode& n3 = nodes[i + 3]; |
| 900 | |
| 901 | if ((float)n.attr("scalar") != 3.f) |
| 902 | continue; |
| 903 | |
| 904 | if (n1.op != "clip" || (float)n1.attr("a_min") != 0.f || (float)n1.attr("a_max") != 6.f) |
| 905 | continue; |
| 906 | |
| 907 | if (n2.op != "_div_scalar" || (float)n2.attr("scalar") != 6.f) |
| 908 | continue; |
| 909 | |
| 910 | // reduce |
| 911 | nodes[i].op = "noop_reducedncnn"; |
| 912 | nodes[i + 1].op = "noop_reducedncnn"; |
| 913 | |
| 914 | node_reference.erase(node_reference.find(i)); |
| 915 | node_reference.erase(node_reference.find(i + 1)); |
| 916 | blob_names.erase(n.name); |
| 917 | blob_names.erase(n1.name); |
| 918 | |
| 919 | if (n3.op != "elemwise_mul" || n3.inputs[0] != n.inputs[0]) |
| 920 | { |
| 921 | MXNetNode new_node; |
| 922 | new_node.nodes = &nodes; |
| 923 | new_node.params = ¶ms; |
| 924 | new_node.op = "HardSigmoid"; |
| 925 | new_node.name = n2.name; |
| 926 | new_node.output_size = n2.output_size; |
| 927 | char alpha[16], beta[16]; |
| 928 | sprintf(alpha, "%f", 1.f / 6.f); |
| 929 | sprintf(beta, "%f", 3.f / 6.f); |
| 930 | new_node.attrs["alpha"] = alpha; |
| 931 | new_node.attrs["beta"] = beta; |
| 932 | new_node.inputs = n.inputs; |
| 933 | new_node.subinputs = n.subinputs; |
| 934 | |
| 935 | nodes[i + 2] = new_node; |
| 936 | |
| 937 | reduced_node_count += 2; |
| 938 | i += 2; |
| 939 | } |
| 940 | else // HardSwish <= HardSigmoid - Mul |
| 941 | { |
| 942 | nodes[i + 2].op = "noop_reducedncnn"; |
| 943 | node_reference[i - 1]--; |
| 944 | node_reference.erase(node_reference.find(i + 2)); |
| 945 | blob_names.erase(n2.name); |
| 946 | |
| 947 | MXNetNode new_node; |
| 948 | new_node.nodes = &nodes; |
| 949 | new_node.params = ¶ms; |
| 950 | new_node.op = "HardSwish"; |
| 951 | new_node.name = n3.name; |
| 952 | new_node.output_size = n3.output_size; |
| 953 | char alpha[16], beta[16]; |
| 954 | sprintf(alpha, "%f", 1.f / 6.f); |
| 955 | sprintf(beta, "%f", 3.f / 6.f); |
| 956 | new_node.attrs["alpha"] = alpha; |
| 957 | new_node.attrs["beta"] = beta; |
| 958 | new_node.inputs = n.inputs; |
| 959 | new_node.subinputs = n.subinputs; |
| 960 | |
| 961 | nodes[i + 3] = new_node; |
| 962 | |
| 963 | reduced_node_count += 3; |
| 964 | i += 3; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | int main(int argc, char** argv) |
| 971 | { |
| 972 | if (!(argc == 3 || argc == 5)) |
| 973 | { |
| 974 | fprintf(stderr, "Usage: %s [mxnetjson] [mxnetparam] [ncnnparam] [ncnnbin]\n", argv[0]); |
| 975 | return -1; |
| 976 | } |
| 977 | |
| 978 | const char* jsonpath = argv[1]; |
| 979 | const char* parampath = argv[2]; |
| 980 | const char* ncnn_prototxt = argc == 5 ? argv[3] : "ncnn.param"; |
| 981 | const char* ncnn_modelbin = argc == 5 ? argv[4] : "ncnn.bin"; |
| 982 | |
| 983 | std::vector<MXNetNode> nodes; |
| 984 | std::vector<MXNetParam> params; |
| 985 | |
| 986 | read_mxnet_json(jsonpath, nodes); |
| 987 | read_mxnet_param(parampath, params); |
| 988 | |
| 989 | FILE* pp = fopen(ncnn_prototxt, "wb"); |
| 990 | FILE* bp = fopen(ncnn_modelbin, "wb"); |
| 991 | |
| 992 | // magic |
| 993 | fprintf(pp, "7767517\n"); |
| 994 | |
| 995 | size_t node_count = nodes.size(); |
| 996 | |
| 997 | // node reference |
| 998 | std::map<size_t, int> node_reference; |
| 999 | |
| 1000 | // weight node |
| 1001 | std::vector<int> weight_nodes; |
| 1002 | |
| 1003 | // sometimes mxnet produce non-unique name for activation op |
| 1004 | { |
| 1005 | std::set<std::string> known_names; |
| 1006 | for (size_t i = 0; i < node_count; i++) |
| 1007 | { |
| 1008 | MXNetNode& n = nodes[i]; |
| 1009 | |
| 1010 | if (known_names.find(n.name) == known_names.end()) |
| 1011 | { |
| 1012 | known_names.insert(n.name); |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| 1016 | // non-unique name detected, append index as suffix |
| 1017 | char suffix[32]; |
| 1018 | sprintf(suffix, "_%d", (int)i); |
| 1019 | n.name = n.name + std::string(suffix); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | // global definition line |
| 1024 | // [layer count] [blob count] |
| 1025 | std::set<std::string> blob_names; |
| 1026 | for (size_t i = 0; i < node_count; i++) |
| 1027 | { |
| 1028 | MXNetNode& n = nodes[i]; |
| 1029 | |
| 1030 | // assign global param reference |
| 1031 | n.nodes = &nodes; |
| 1032 | n.params = ¶ms; |
| 1033 | |
| 1034 | const std::string& output_name = n.name; |
| 1035 | n.output_size = 1; |
| 1036 | |
| 1037 | if (n.op == "null") |
| 1038 | { |
| 1039 | if (n.is_weight()) |
| 1040 | { |
| 1041 | weight_nodes.push_back(i); |
| 1042 | } |
| 1043 | else |
| 1044 | { |
| 1045 | if (n.has_attr("__init__")) |
| 1046 | { |
| 1047 | // init weight param |
| 1048 | MXNetParam pi; |
| 1049 | pi.name = n.name; |
| 1050 | pi.init = (std::string)n.attr("__init__"); |
| 1051 | params.push_back(pi); |
| 1052 | |
| 1053 | weight_nodes.push_back(i); |
| 1054 | } |
| 1055 | else |
| 1056 | { |
| 1057 | // null node without data, treat it as network input |
| 1058 | } |
| 1059 | } |
| 1060 | continue; |
| 1061 | } |
| 1062 | else if (n.op == "_contrib_MultiBoxTarget") |
| 1063 | { |
| 1064 | n.output_size = 3; |
| 1065 | } |
| 1066 | else if (n.op == "SliceChannel") |
| 1067 | { |
| 1068 | n.output_size = n.attr("num_outputs"); |
| 1069 | } |
| 1070 | |
| 1071 | // distinguish weights and inputs |
| 1072 | std::vector<int> weights; |
| 1073 | std::vector<int> inputs; |
| 1074 | for (int j = 0; j < (int)n.inputs.size(); j++) |
| 1075 | { |
| 1076 | int input_index = n.inputs[j]; |
| 1077 | if (nodes[input_index].is_weight()) |
| 1078 | { |
| 1079 | weights.push_back(input_index); |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
| 1083 | inputs.push_back(input_index); |
| 1084 | } |
| 1085 | n.inputs = inputs; |
| 1086 | n.weights = weights; |
| 1087 | |
| 1088 | if (n.op == "_contrib_MultiBoxDetection") |
| 1089 | { |
| 1090 | // reorder input blob |
| 1091 | int temp = n.inputs[0]; |
| 1092 | n.inputs[0] = n.inputs[1]; |
| 1093 | n.inputs[1] = temp; |
| 1094 | } |
| 1095 | |
| 1096 | // input |
| 1097 | for (int j = 0; j < (int)n.inputs.size(); j++) |
| 1098 | { |
| 1099 | int input_index = n.inputs[j]; |
| 1100 | int subinput_index = n.subinputs[j]; |
| 1101 | |
| 1102 | std::string input_name = nodes[input_index].name; |
| 1103 | // fprintf(stderr, "input = %s\n", input_name.c_str()); |
| 1104 | |
| 1105 | if (subinput_index != 0) |
| 1106 | { |
| 1107 | char subinputsuffix[256]; |
| 1108 | sprintf(subinputsuffix, "_subncnn_%d", subinput_index); |
| 1109 | input_name = input_name + subinputsuffix; |
| 1110 | } |
| 1111 | |
| 1112 | blob_names.insert(input_name); |
| 1113 | |
| 1114 | int input_uid = input_index | (subinput_index << 16); |
| 1115 | if (node_reference.find(input_uid) == node_reference.end()) |
| 1116 | { |
| 1117 | node_reference[input_uid] = 1; |
| 1118 | } |
| 1119 | else |
| 1120 | { |
| 1121 | node_reference[input_uid] = node_reference[input_uid] + 1; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | // output |
| 1126 | // fprintf(stderr, "output = %s\n", output_name.c_str()); |
| 1127 | blob_names.insert(output_name); |
| 1128 | |
| 1129 | for (int j = 1; j < n.output_size; j++) |
| 1130 | { |
| 1131 | char subinputsuffix[256]; |
| 1132 | sprintf(subinputsuffix, "_%d", j); |
| 1133 | std::string output_name_j = output_name + subinputsuffix; |
| 1134 | blob_names.insert(output_name_j); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | // for (std::map<int, int>::iterator it = node_reference.begin(); it != node_reference.end(); it++) |
| 1139 | // { |
| 1140 | // fprintf(stderr, "ref %d %d\n", it->first, it->second); |
| 1141 | // } |
| 1142 | |
| 1143 | // op chain fusion |
| 1144 | int reduced_node_count = 0; |
| 1145 | fuse_shufflechannel(nodes, params, node_reference, blob_names, reduced_node_count); |
| 1146 | fuse_hardsigmoid_hardswish(nodes, params, node_reference, blob_names, reduced_node_count); |
| 1147 | |
| 1148 | // remove node_reference entry with reference equals to one |
| 1149 | int splitncnn_blob_count = 0; |
| 1150 | std::map<size_t, int>::iterator it = node_reference.begin(); |
| 1151 | while (it != node_reference.end()) |
| 1152 | { |
| 1153 | if (it->second == 1) |
| 1154 | { |
| 1155 | node_reference.erase(it++); |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | splitncnn_blob_count += it->second; |
| 1160 | // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second); |
| 1161 | ++it; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | // fprintf(stderr, "%d %d %d %d, %d %d\n", node_count, reduced_node_count, node_reference.size(), weight_nodes.size(), blob_names.size(), splitncnn_blob_count); |
| 1166 | |
| 1167 | fprintf(pp, "%zu %zu\n", node_count - reduced_node_count + node_reference.size() - weight_nodes.size(), blob_names.size() + splitncnn_blob_count); |
| 1168 | |
| 1169 | int internal_split = 0; |
| 1170 | |
| 1171 | for (size_t i = 0; i < node_count; i++) |
| 1172 | { |
| 1173 | const MXNetNode& n = nodes[i]; |
| 1174 | |
| 1175 | if (n.op == "noop_reducedncnn") |
| 1176 | { |
| 1177 | continue; |
| 1178 | } |
| 1179 | |
| 1180 | if (n.op == "null") |
| 1181 | { |
| 1182 | if (n.is_weight()) |
| 1183 | { |
| 1184 | continue; |
| 1185 | } |
| 1186 | |
| 1187 | fprintf(pp, "%-16s", "Input"); |
| 1188 | } |
| 1189 | else if (n.op == "_contrib_BilinearResize2D") |
| 1190 | { |
| 1191 | fprintf(pp, "%-16s", "Interp"); |
| 1192 | } |
| 1193 | else if (n.op == "_contrib_MultiBoxDetection") |
| 1194 | { |
| 1195 | fprintf(pp, "%-16s", "DetectionOutput"); |
| 1196 | } |
| 1197 | else if (n.op == "_contrib_MultiBoxPrior") |
| 1198 | { |
| 1199 | fprintf(pp, "%-16s", "PriorBox"); |
| 1200 | } |
| 1201 | else if (n.op == "_copy") |
| 1202 | { |
| 1203 | fprintf(pp, "%-16s", "Noop"); |
| 1204 | } |
| 1205 | else if (n.op == "_div_scalar") |
| 1206 | { |
| 1207 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1208 | } |
| 1209 | else if (n.op == "_maximum_scalar") |
| 1210 | { |
| 1211 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1212 | } |
| 1213 | else if (n.op == "_minimum_scalar") |
| 1214 | { |
| 1215 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1216 | } |
| 1217 | else if (n.op == "_minus_scalar") |
| 1218 | { |
| 1219 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1220 | } |
| 1221 | else if (n.op == "_mul_scalar") |
| 1222 | { |
| 1223 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1224 | } |
| 1225 | else if (n.op == "_plus_scalar") |
| 1226 | { |
| 1227 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1228 | } |
| 1229 | else if (n.op == "_power_scalar") |
| 1230 | { |
| 1231 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1232 | } |
| 1233 | else if (n.op == "_rdiv_scalar") |
| 1234 | { |
| 1235 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1236 | } |
| 1237 | else if (n.op == "_rminus_scalar") |
| 1238 | { |
| 1239 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1240 | } |
| 1241 | else if (n.op == "abs") |
| 1242 | { |
| 1243 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1244 | } |
| 1245 | else if (n.op == "Activation") |
| 1246 | { |
| 1247 | std::string type = n.attr("act_type"); |
| 1248 | if (type == "relu") |
| 1249 | { |
| 1250 | fprintf(pp, "%-16s", "ReLU"); |
| 1251 | } |
| 1252 | else if (type == "sigmoid") |
| 1253 | { |
| 1254 | fprintf(pp, "%-16s", "Sigmoid"); |
| 1255 | } |
| 1256 | else if (type == "tanh") |
| 1257 | { |
| 1258 | fprintf(pp, "%-16s", "TanH"); |
| 1259 | } |
| 1260 | } |
| 1261 | else if (n.op == "add_n" || n.op == "ElementWiseSum") |
| 1262 | { |
| 1263 | fprintf(pp, "%-16s", "Eltwise"); |
| 1264 | } |
| 1265 | else if (n.op == "arccos") |
| 1266 | { |
| 1267 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1268 | } |
| 1269 | else if (n.op == "arcsin") |
| 1270 | { |
| 1271 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1272 | } |
| 1273 | else if (n.op == "arctan") |
| 1274 | { |
| 1275 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1276 | } |
| 1277 | else if (n.op == "BatchNorm") |
| 1278 | { |
| 1279 | fprintf(pp, "%-16s", "BatchNorm"); |
| 1280 | } |
| 1281 | else if (n.op == "broadcast_add") |
| 1282 | { |
| 1283 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1284 | } |
| 1285 | else if (n.op == "broadcast_div") |
| 1286 | { |
| 1287 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1288 | } |
| 1289 | else if (n.op == "broadcast_mul") |
| 1290 | { |
| 1291 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1292 | } |
| 1293 | else if (n.op == "broadcast_sub") |
| 1294 | { |
| 1295 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1296 | } |
| 1297 | else if (n.op == "ceil") |
| 1298 | { |
| 1299 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1300 | } |
| 1301 | else if (n.op == "clip") |
| 1302 | { |
| 1303 | fprintf(pp, "%-16s", "Clip"); |
| 1304 | } |
| 1305 | else if (n.op == "Concat") |
| 1306 | { |
| 1307 | fprintf(pp, "%-16s", "Concat"); |
| 1308 | } |
| 1309 | else if (n.op == "Convolution") |
| 1310 | { |
| 1311 | int num_group = n.attr("num_group"); |
| 1312 | if (num_group > 1) |
| 1313 | { |
| 1314 | fprintf(pp, "%-16s", "ConvolutionDepthWise"); |
| 1315 | } |
| 1316 | else |
| 1317 | { |
| 1318 | fprintf(pp, "%-16s", "Convolution"); |
| 1319 | } |
| 1320 | } |
| 1321 | else if (n.op == "cos") |
| 1322 | { |
| 1323 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1324 | } |
| 1325 | else if (n.op == "Crop") |
| 1326 | { |
| 1327 | fprintf(pp, "%-16s", "Crop"); |
| 1328 | } |
| 1329 | else if (n.op == "Deconvolution") |
| 1330 | { |
| 1331 | int num_group = n.attr("num_group"); |
| 1332 | if (num_group > 1) |
| 1333 | { |
| 1334 | fprintf(pp, "%-16s", "DeconvolutionDepthWise"); |
| 1335 | } |
| 1336 | else |
| 1337 | { |
| 1338 | fprintf(pp, "%-16s", "Deconvolution"); |
| 1339 | } |
| 1340 | } |
| 1341 | else if (n.op == "dot") |
| 1342 | { |
| 1343 | fprintf(pp, "%-16s", "Gemm"); |
| 1344 | } |
| 1345 | else if (n.op == "Dropout") |
| 1346 | { |
| 1347 | fprintf(pp, "%-16s", "Dropout"); |
| 1348 | } |
| 1349 | else if (n.op == "elemwise_add" || n.op == "_add" || n.op == "_plus" || n.op == "_Plus") |
| 1350 | { |
| 1351 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1352 | } |
| 1353 | else if (n.op == "elemwise_div" || n.op == "_div" || n.op == "_Div") |
| 1354 | { |
| 1355 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1356 | } |
| 1357 | else if (n.op == "elemwise_mul" || n.op == "_mul" || n.op == "_Mul") |
| 1358 | { |
| 1359 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1360 | } |
| 1361 | else if (n.op == "elemwise_sub" || n.op == "_sub" || n.op == "_minus" || n.op == "_Minus") |
| 1362 | { |
| 1363 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1364 | } |
| 1365 | else if (n.op == "Embedding") |
| 1366 | { |
| 1367 | fprintf(pp, "%-16s", "Embed"); |
| 1368 | } |
| 1369 | else if (n.op == "exp") |
| 1370 | { |
| 1371 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1372 | } |
| 1373 | else if (n.op == "expand_dims") |
| 1374 | { |
| 1375 | fprintf(pp, "%-16s", "ExpandDims"); |
| 1376 | } |
| 1377 | else if (n.op == "Flatten") |
| 1378 | { |
| 1379 | fprintf(pp, "%-16s", "Flatten"); |
| 1380 | } |
| 1381 | else if (n.op == "floor") |
| 1382 | { |
| 1383 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1384 | } |
| 1385 | else if (n.op == "FullyConnected") |
| 1386 | { |
| 1387 | fprintf(pp, "%-16s", "InnerProduct"); |
| 1388 | } |
| 1389 | else if (n.op == "HardSigmoid") |
| 1390 | { |
| 1391 | fprintf(pp, "%-16s", "HardSigmoid"); |
| 1392 | } |
| 1393 | else if (n.op == "HardSwish") |
| 1394 | { |
| 1395 | fprintf(pp, "%-16s", "HardSwish"); |
| 1396 | } |
| 1397 | else if (n.op == "InstanceNorm") |
| 1398 | { |
| 1399 | fprintf(pp, "%-16s", "InstanceNorm"); |
| 1400 | } |
| 1401 | else if (n.op == "L2Normalization") |
| 1402 | { |
| 1403 | fprintf(pp, "%-16s", "Normalize"); |
| 1404 | } |
| 1405 | else if (n.op == "LeakyReLU") |
| 1406 | { |
| 1407 | std::string type = n.attr("act_type"); |
| 1408 | if (type == "elu") |
| 1409 | { |
| 1410 | fprintf(pp, "%-16s", "ELU"); |
| 1411 | } |
| 1412 | else if (type == "leaky" || type.empty()) |
| 1413 | { |
| 1414 | fprintf(pp, "%-16s", "ReLU"); |
| 1415 | } |
| 1416 | else if (type == "prelu") |
| 1417 | { |
| 1418 | fprintf(pp, "%-16s", "PReLU"); |
| 1419 | } |
| 1420 | } |
| 1421 | else if (n.op == "LinearRegressionOutput") |
| 1422 | { |
| 1423 | fprintf(pp, "%-16s", "Noop"); |
| 1424 | } |
| 1425 | else if (n.op == "log") |
| 1426 | { |
| 1427 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1428 | } |
| 1429 | else if (n.op == "LogisticRegressionOutput") |
| 1430 | { |
| 1431 | fprintf(pp, "%-16s", "Sigmoid"); |
| 1432 | } |
| 1433 | else if (n.op == "MAERegressionOutput") |
| 1434 | { |
| 1435 | fprintf(pp, "%-16s", "Noop"); |
| 1436 | } |
| 1437 | else if (n.op == "max" || n.op == "mean" || n.op == "min" || n.op == "prod" || n.op == "sum") |
| 1438 | { |
| 1439 | fprintf(pp, "%-16s", "Reduction"); |
| 1440 | } |
| 1441 | else if (n.op == "maximum") |
| 1442 | { |
| 1443 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1444 | } |
| 1445 | else if (n.op == "minimum") |
| 1446 | { |
| 1447 | fprintf(pp, "%-16s", "BinaryOp"); |
| 1448 | } |
| 1449 | else if (n.op == "negative") |
| 1450 | { |
| 1451 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1452 | } |
| 1453 | else if (n.op == "Pad") |
| 1454 | { |
| 1455 | fprintf(pp, "%-16s", "Padding"); |
| 1456 | } |
| 1457 | else if (n.op == "Pooling") |
| 1458 | { |
| 1459 | fprintf(pp, "%-16s", "Pooling"); |
| 1460 | } |
| 1461 | else if (n.op == "reciprocal") |
| 1462 | { |
| 1463 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1464 | } |
| 1465 | else if (n.op == "relu") |
| 1466 | { |
| 1467 | fprintf(pp, "%-16s", "ReLU"); |
| 1468 | } |
| 1469 | else if (n.op == "Reshape") |
| 1470 | { |
| 1471 | fprintf(pp, "%-16s", "Reshape"); |
| 1472 | } |
| 1473 | else if (n.op == "ShuffleChannel") |
| 1474 | { |
| 1475 | fprintf(pp, "%-16s", "ShuffleChannel"); |
| 1476 | } |
| 1477 | else if (n.op == "sigmoid") |
| 1478 | { |
| 1479 | fprintf(pp, "%-16s", "Sigmoid"); |
| 1480 | } |
| 1481 | else if (n.op == "sin") |
| 1482 | { |
| 1483 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1484 | } |
| 1485 | else if (n.op == "slice") |
| 1486 | { |
| 1487 | fprintf(pp, "%-16s", "Crop"); |
| 1488 | } |
| 1489 | else if (n.op == "slice_axis") |
| 1490 | { |
| 1491 | fprintf(pp, "%-16s", "Crop"); |
| 1492 | } |
| 1493 | else if (n.op == "SliceChannel") |
| 1494 | { |
| 1495 | fprintf(pp, "%-16s", "Slice"); |
| 1496 | } |
| 1497 | else if (n.op == "SoftmaxActivation") |
| 1498 | { |
| 1499 | fprintf(pp, "%-16s", "Softmax"); |
| 1500 | } |
| 1501 | else if (n.op == "SoftmaxOutput") |
| 1502 | { |
| 1503 | fprintf(pp, "%-16s", "Softmax"); |
| 1504 | } |
| 1505 | else if (n.op == "softmax") |
| 1506 | { |
| 1507 | fprintf(pp, "%-16s", "Softmax"); |
| 1508 | } |
| 1509 | else if (n.op == "sqrt") |
| 1510 | { |
| 1511 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1512 | } |
| 1513 | else if (n.op == "square") |
| 1514 | { |
| 1515 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1516 | } |
| 1517 | else if (n.op == "squeeze") |
| 1518 | { |
| 1519 | fprintf(pp, "%-16s", "Squeeze"); |
| 1520 | } |
| 1521 | else if (n.op == "tan") |
| 1522 | { |
| 1523 | fprintf(pp, "%-16s", "UnaryOp"); |
| 1524 | } |
| 1525 | else if (n.op == "tanh") |
| 1526 | { |
| 1527 | fprintf(pp, "%-16s", "TanH"); |
| 1528 | } |
| 1529 | else if (n.op == "Transpose" || n.op == "transpose") |
| 1530 | { |
| 1531 | fprintf(pp, "%-16s", "Permute"); |
| 1532 | } |
| 1533 | else if (n.op == "UpSampling") |
| 1534 | { |
| 1535 | std::string sample_type = n.attr("sample_type"); |
| 1536 | if (sample_type == "nearest") |
| 1537 | { |
| 1538 | fprintf(pp, "%-16s", "Interp"); |
| 1539 | } |
| 1540 | else if (sample_type == "bilinear") |
| 1541 | { |
| 1542 | fprintf(pp, "%-16s", "DeconvolutionDepthWise"); |
| 1543 | } |
| 1544 | } |
| 1545 | else |
| 1546 | { |
| 1547 | fprintf(stderr, "%s not supported yet!\n", n.op.c_str()); |
| 1548 | fprintf(pp, "%-16s", n.op.c_str()); |
| 1549 | } |
| 1550 | |
| 1551 | size_t input_size = n.inputs.size(); |
| 1552 | for (int j = 0; j < (int)n.inputs.size(); j++) |
| 1553 | { |
| 1554 | int input_index = n.inputs[j]; |
| 1555 | if (nodes[input_index].is_weight()) |
| 1556 | { |
| 1557 | input_size--; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | if (n.op == "SoftmaxOutput" || n.op == "LogisticRegressionOutput") |
| 1562 | { |
| 1563 | // drop label |
| 1564 | input_size--; |
| 1565 | } |
| 1566 | |
| 1567 | fprintf(pp, " %-32s %zd %d", n.name.c_str(), input_size, n.output_size); |
| 1568 | |
| 1569 | for (int j = 0; j < (int)n.inputs.size(); j++) |
| 1570 | { |
| 1571 | int input_index = n.inputs[j]; |
| 1572 | int subinput_index = n.subinputs[j]; |
| 1573 | if (nodes[input_index].is_weight()) |
| 1574 | { |
| 1575 | continue; |
| 1576 | } |
| 1577 | |
| 1578 | if (n.op == "SoftmaxOutput" || n.op == "LogisticRegressionOutput") |
| 1579 | { |
| 1580 | // drop label |
| 1581 | if (j == 1) |
| 1582 | continue; |
| 1583 | } |
| 1584 | |
| 1585 | std::string input_name = nodes[input_index].name; |
| 1586 | |
| 1587 | if (subinput_index != 0) |
| 1588 | { |
| 1589 | char subinputsuffix[256]; |
| 1590 | sprintf(subinputsuffix, "_subncnn_%d", subinput_index); |
| 1591 | input_name = input_name + subinputsuffix; |
| 1592 | } |
| 1593 | |
| 1594 | int input_uid = input_index | (subinput_index << 16); |
| 1595 | if (node_reference.find(input_uid) != node_reference.end()) |
| 1596 | { |
| 1597 | int refidx = node_reference[input_uid] - 1; |
| 1598 | node_reference[input_uid] = refidx; |
| 1599 | |
| 1600 | char splitsuffix[256]; |
| 1601 | sprintf(splitsuffix, "_splitncnn_%d", refidx); |
| 1602 | input_name = input_name + splitsuffix; |
| 1603 | } |
| 1604 | |
| 1605 | fprintf(pp, " %s", input_name.c_str()); |
| 1606 | } |
| 1607 | |
| 1608 | fprintf(pp, " %s", n.name.c_str()); |
| 1609 | for (int j = 1; j < n.output_size; j++) |
| 1610 | { |
| 1611 | fprintf(pp, " %s_subncnn_%d", n.name.c_str(), j); |
| 1612 | } |
| 1613 | |
| 1614 | if (n.op == "null") |
| 1615 | { |
| 1616 | // dummy input shape |
| 1617 | // fprintf(pp, " 0 0 0"); |
| 1618 | } |
| 1619 | else if (n.op == "_contrib_BilinearResize2D") |
| 1620 | { |
| 1621 | float scale_height = n.has_attr("scale_height") ? n.attr("scale_height") : 1.f; |
| 1622 | float scale_width = n.has_attr("scale_width") ? n.attr("scale_width") : 1.f; |
| 1623 | int height = n.has_attr("scale_height") ? 0 : n.attr("height"); |
| 1624 | int width = n.has_attr("scale_width") ? 0 : n.attr("width"); |
| 1625 | |
| 1626 | fprintf(pp, " 0=2"); |
| 1627 | fprintf(pp, " 1=%e", scale_height); |
| 1628 | fprintf(pp, " 2=%e", scale_width); |
| 1629 | fprintf(pp, " 3=%d", height); |
| 1630 | fprintf(pp, " 4=%d", width); |
| 1631 | } |
| 1632 | else if (n.op == "_contrib_MultiBoxDetection") |
| 1633 | { |
| 1634 | float threshold = n.has_attr("threshold") ? n.attr("threshold") : 0.01f; |
| 1635 | float nms_threshold = n.has_attr("nms_threshold") ? n.attr("nms_threshold") : 0.5f; |
| 1636 | int nms_topk = n.has_attr("nms_topk") ? n.attr("nms_topk") : 300; |
| 1637 | |
| 1638 | fprintf(pp, " 0=-233"); |
| 1639 | fprintf(pp, " 1=%e", nms_threshold); |
| 1640 | fprintf(pp, " 2=%d", nms_topk); |
| 1641 | |
| 1642 | int keep_top_k = 100; |
| 1643 | fprintf(pp, " 3=%d", keep_top_k); |
| 1644 | fprintf(pp, " 4=%e", threshold); |
| 1645 | |
| 1646 | std::vector<float> variances = n.attr("variances"); |
| 1647 | if (variances.empty()) |
| 1648 | { |
| 1649 | fprintf(pp, " 5=0.1"); |
| 1650 | fprintf(pp, " 6=0.1"); |
| 1651 | fprintf(pp, " 7=0.2"); |
| 1652 | fprintf(pp, " 8=0.2"); |
| 1653 | } |
| 1654 | else |
| 1655 | { |
| 1656 | fprintf(pp, " 5=%e", variances[0]); |
| 1657 | fprintf(pp, " 6=%e", variances[1]); |
| 1658 | fprintf(pp, " 7=%e", variances[2]); |
| 1659 | fprintf(pp, " 8=%e", variances[3]); |
| 1660 | } |
| 1661 | } |
| 1662 | else if (n.op == "_contrib_MultiBoxPrior") |
| 1663 | { |
| 1664 | // mxnet-ssd encode size as scale factor, fill min_size |
| 1665 | std::vector<float> sizes = n.attr("sizes"); |
| 1666 | fprintf(pp, " -23300=%d", (int)sizes.size()); |
| 1667 | for (int j = 0; j < (int)sizes.size(); j++) |
| 1668 | { |
| 1669 | fprintf(pp, ",%e", sizes[j]); |
| 1670 | } |
| 1671 | |
| 1672 | std::vector<float> aspect_ratios = n.attr("ratios"); |
| 1673 | fprintf(pp, " -23302=%d", (int)aspect_ratios.size()); |
| 1674 | for (int j = 0; j < (int)aspect_ratios.size(); j++) |
| 1675 | { |
| 1676 | fprintf(pp, ",%e", aspect_ratios[j]); |
| 1677 | } |
| 1678 | |
| 1679 | int flip = 0; |
| 1680 | fprintf(pp, " 7=%d", flip); |
| 1681 | |
| 1682 | int clip = n.attr("clip"); |
| 1683 | fprintf(pp, " 8=%d", clip); |
| 1684 | |
| 1685 | // auto image size |
| 1686 | fprintf(pp, " 9=-233"); |
| 1687 | fprintf(pp, " 10=-233"); |
| 1688 | |
| 1689 | std::vector<float> steps = n.attr("steps"); |
| 1690 | if (steps.empty() || (steps[0] == -1.f && steps[1] == -1.f)) |
| 1691 | { |
| 1692 | // auto step |
| 1693 | fprintf(pp, " 11=-233.0"); |
| 1694 | fprintf(pp, " 12=-233.0"); |
| 1695 | } |
| 1696 | else |
| 1697 | { |
| 1698 | fprintf(pp, " 11=%e", steps[1]); |
| 1699 | fprintf(pp, " 12=%e", steps[0]); |
| 1700 | } |
| 1701 | |
| 1702 | std::vector<float> offsets = n.attr("offsets"); |
| 1703 | if (offsets.empty() || (offsets[0] == 0.5f && offsets[1] == 0.5f)) |
| 1704 | { |
| 1705 | fprintf(pp, " 13=0.5"); |
| 1706 | } |
| 1707 | else |
| 1708 | { |
| 1709 | fprintf(stderr, "Unsupported offsets param! %g %g\n", offsets[0], offsets[1]); |
| 1710 | } |
| 1711 | } |
| 1712 | else if (n.op == "_copy") |
| 1713 | { |
| 1714 | // noop |
| 1715 | } |
| 1716 | else if (n.op == "_div_scalar") |
| 1717 | { |
| 1718 | int op_type = 3; |
| 1719 | int with_scalar = 1; |
| 1720 | float scalar = n.attr("scalar"); |
| 1721 | fprintf(pp, " 0=%d", op_type); |
| 1722 | fprintf(pp, " 1=%d", with_scalar); |
| 1723 | fprintf(pp, " 2=%e", scalar); |
| 1724 | } |
| 1725 | else if (n.op == "_maximum_scalar") |
| 1726 | { |
| 1727 | int op_type = 4; |
| 1728 | int with_scalar = 1; |
| 1729 | float scalar = n.attr("scalar"); |
| 1730 | fprintf(pp, " 0=%d", op_type); |
| 1731 | fprintf(pp, " 1=%d", with_scalar); |
| 1732 | fprintf(pp, " 2=%e", scalar); |
| 1733 | } |
| 1734 | else if (n.op == "_minimum_scalar") |
| 1735 | { |
| 1736 | int op_type = 5; |
| 1737 | int with_scalar = 1; |
| 1738 | float scalar = n.attr("scalar"); |
| 1739 | fprintf(pp, " 0=%d", op_type); |
| 1740 | fprintf(pp, " 1=%d", with_scalar); |
| 1741 | fprintf(pp, " 2=%e", scalar); |
| 1742 | } |
| 1743 | else if (n.op == "_minus_scalar") |
| 1744 | { |
| 1745 | int op_type = 1; |
| 1746 | int with_scalar = 1; |
| 1747 | float scalar = n.attr("scalar"); |
| 1748 | fprintf(pp, " 0=%d", op_type); |
| 1749 | fprintf(pp, " 1=%d", with_scalar); |
| 1750 | fprintf(pp, " 2=%e", scalar); |
| 1751 | } |
| 1752 | else if (n.op == "_mul_scalar") |
| 1753 | { |
| 1754 | int op_type = 2; |
| 1755 | int with_scalar = 1; |
| 1756 | float scalar = n.attr("scalar"); |
| 1757 | fprintf(pp, " 0=%d", op_type); |
| 1758 | fprintf(pp, " 1=%d", with_scalar); |
| 1759 | fprintf(pp, " 2=%e", scalar); |
| 1760 | } |
| 1761 | else if (n.op == "_plus_scalar") |
| 1762 | { |
| 1763 | int op_type = 0; |
| 1764 | int with_scalar = 1; |
| 1765 | float scalar = n.attr("scalar"); |
| 1766 | fprintf(pp, " 0=%d", op_type); |
| 1767 | fprintf(pp, " 1=%d", with_scalar); |
| 1768 | fprintf(pp, " 2=%e", scalar); |
| 1769 | } |
| 1770 | else if (n.op == "_power_scalar") |
| 1771 | { |
| 1772 | int op_type = 6; |
| 1773 | int with_scalar = 1; |
| 1774 | float scalar = n.attr("scalar"); |
| 1775 | fprintf(pp, " 0=%d", op_type); |
| 1776 | fprintf(pp, " 1=%d", with_scalar); |
| 1777 | fprintf(pp, " 2=%e", scalar); |
| 1778 | } |
| 1779 | else if (n.op == "_rdiv_scalar") |
| 1780 | { |
| 1781 | int op_type = 8; |
| 1782 | int with_scalar = 1; |
| 1783 | float scalar = n.attr("scalar"); |
| 1784 | fprintf(pp, " 0=%d", op_type); |
| 1785 | fprintf(pp, " 1=%d", with_scalar); |
| 1786 | fprintf(pp, " 2=%e", scalar); |
| 1787 | } |
| 1788 | else if (n.op == "_rminus_scalar") |
| 1789 | { |
| 1790 | int op_type = 7; |
| 1791 | int with_scalar = 1; |
| 1792 | float scalar = n.attr("scalar"); |
| 1793 | fprintf(pp, " 0=%d", op_type); |
| 1794 | fprintf(pp, " 1=%d", with_scalar); |
| 1795 | fprintf(pp, " 2=%e", scalar); |
| 1796 | } |
| 1797 | else if (n.op == "abs") |
| 1798 | { |
| 1799 | int op_type = 0; |
| 1800 | fprintf(pp, " 0=%d", op_type); |
| 1801 | } |
| 1802 | else if (n.op == "Activation") |
| 1803 | { |
| 1804 | std::string type = n.attr("act_type"); |
| 1805 | if (type == "relu") |
| 1806 | { |
| 1807 | // fprintf(pp, " 0=%e", 0.f); |
| 1808 | } |
| 1809 | } |
| 1810 | else if (n.op == "add_n" || n.op == "ElementWiseSum") |
| 1811 | { |
| 1812 | int op_type = 1; |
| 1813 | fprintf(pp, " 0=%d", op_type); |
| 1814 | } |
| 1815 | else if (n.op == "arccos") |
| 1816 | { |
| 1817 | int op_type = 13; |
| 1818 | fprintf(pp, " 0=%d", op_type); |
| 1819 | } |
| 1820 | else if (n.op == "arcsin") |
| 1821 | { |
| 1822 | int op_type = 12; |
| 1823 | fprintf(pp, " 0=%d", op_type); |
| 1824 | } |
| 1825 | else if (n.op == "arctan") |
| 1826 | { |
| 1827 | int op_type = 14; |
| 1828 | fprintf(pp, " 0=%d", op_type); |
| 1829 | } |
| 1830 | else if (n.op == "BatchNorm") |
| 1831 | { |
| 1832 | float eps = 1e-3f; |
| 1833 | if (n.has_attr("eps")) |
| 1834 | { |
| 1835 | eps = n.attr("eps"); |
| 1836 | } |
| 1837 | |
| 1838 | std::vector<float> slope_data = n.weight(0); |
| 1839 | std::vector<float> bias_data = n.weight(1); |
| 1840 | |
| 1841 | int channels = static_cast<int>(slope_data.size()); |
| 1842 | |
| 1843 | std::vector<float> mean_data = n.weight(2, channels); |
| 1844 | std::vector<float> var_data = n.weight(3, channels); |
| 1845 | |
| 1846 | for (int j = 0; j < (int)var_data.size(); j++) |
| 1847 | { |
| 1848 | var_data[j] += eps; |
| 1849 | } |
| 1850 | |
| 1851 | fprintf(pp, " 0=%d", channels); |
| 1852 | |
| 1853 | int fix_gamma = n.has_attr("fix_gamma") ? n.attr("fix_gamma") : 0; |
| 1854 | if (fix_gamma) |
| 1855 | { |
| 1856 | // slope data are all 0 here, force set 1 |
| 1857 | for (int j = 0; j < channels; j++) |
| 1858 | { |
| 1859 | slope_data[j] = 1.f; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | fwrite(slope_data.data(), sizeof(float), slope_data.size(), bp); |
| 1864 | fwrite(mean_data.data(), sizeof(float), mean_data.size(), bp); |
| 1865 | fwrite(var_data.data(), sizeof(float), var_data.size(), bp); |
| 1866 | fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp); |
| 1867 | } |
| 1868 | else if (n.op == "broadcast_add") |
| 1869 | { |
| 1870 | int op_type = 0; |
| 1871 | fprintf(pp, " 0=%d", op_type); |
| 1872 | } |
| 1873 | else if (n.op == "broadcast_div") |
| 1874 | { |
| 1875 | int op_type = 3; |
| 1876 | fprintf(pp, " 0=%d", op_type); |
| 1877 | } |
| 1878 | else if (n.op == "broadcast_mul") |
| 1879 | { |
| 1880 | int op_type = 2; |
| 1881 | fprintf(pp, " 0=%d", op_type); |
| 1882 | } |
| 1883 | else if (n.op == "broadcast_sub") |
| 1884 | { |
| 1885 | int op_type = 1; |
| 1886 | fprintf(pp, " 0=%d", op_type); |
| 1887 | } |
| 1888 | else if (n.op == "ceil") |
| 1889 | { |
| 1890 | int op_type = 3; |
| 1891 | fprintf(pp, " 0=%d", op_type); |
| 1892 | } |
| 1893 | else if (n.op == "clip") |
| 1894 | { |
| 1895 | float min = n.attr("a_min"); |
| 1896 | float max = n.attr("a_max"); |
| 1897 | fprintf(pp, " 0=%e", min); |
| 1898 | fprintf(pp, " 1=%e", max); |
| 1899 | } |
| 1900 | else if (n.op == "Concat") |
| 1901 | { |
| 1902 | int dim = n.has_attr("dim") ? n.attr("dim") : 1; |
| 1903 | fprintf(pp, " 0=%d", dim - 1); |
| 1904 | } |
| 1905 | else if (n.op == "Convolution") |
| 1906 | { |
| 1907 | int num_filter = n.attr("num_filter"); |
| 1908 | std::vector<int> kernel = n.attr("kernel"); |
| 1909 | std::vector<int> dilate = n.attr("dilate"); |
| 1910 | std::vector<int> stride = n.attr("stride"); |
| 1911 | std::vector<int> pad = n.attr("pad"); |
| 1912 | int no_bias = n.attr("no_bias"); |
| 1913 | int num_group = n.attr("num_group"); |
| 1914 | |
| 1915 | std::vector<float> weight_data = n.weight(0); |
| 1916 | std::vector<float> bias_data = n.weight(1); |
| 1917 | |
| 1918 | fprintf(pp, " 0=%d", num_filter); |
| 1919 | if (kernel.size() == 1) |
| 1920 | { |
| 1921 | fprintf(pp, " 1=%d", kernel[0]); |
| 1922 | } |
| 1923 | else if (kernel.size() == 2) |
| 1924 | { |
| 1925 | fprintf(pp, " 1=%d", kernel[1]); |
| 1926 | fprintf(pp, " 11=%d", kernel[0]); |
| 1927 | } |
| 1928 | |
| 1929 | if (dilate.size() == 1) |
| 1930 | { |
| 1931 | fprintf(pp, " 2=%d", dilate[0]); |
| 1932 | } |
| 1933 | else if (dilate.size() == 2) |
| 1934 | { |
| 1935 | fprintf(pp, " 2=%d", dilate[1]); |
| 1936 | fprintf(pp, " 12=%d", dilate[0]); |
| 1937 | } |
| 1938 | |
| 1939 | if (stride.size() == 1) |
| 1940 | { |
| 1941 | fprintf(pp, " 3=%d", stride[0]); |
| 1942 | } |
| 1943 | else if (stride.size() == 2) |
| 1944 | { |
| 1945 | fprintf(pp, " 3=%d", stride[1]); |
| 1946 | fprintf(pp, " 13=%d", stride[0]); |
| 1947 | } |
| 1948 | |
| 1949 | if (pad.size() == 1) |
| 1950 | { |
| 1951 | fprintf(pp, " 4=%d", pad[0]); |
| 1952 | } |
| 1953 | else if (pad.size() == 2) |
| 1954 | { |
| 1955 | fprintf(pp, " 4=%d", pad[1]); |
| 1956 | fprintf(pp, " 14=%d", pad[0]); |
| 1957 | } |
| 1958 | |
| 1959 | fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1); |
| 1960 | fprintf(pp, " 6=%d", (int)weight_data.size()); |
| 1961 | if (num_group > 1) |
| 1962 | { |
| 1963 | fprintf(pp, " 7=%d", num_group); |
| 1964 | } |
| 1965 | |
| 1966 | int quantize_tag = 0; |
| 1967 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 1968 | fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp); |
| 1969 | fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp); |
| 1970 | } |
| 1971 | else if (n.op == "cos") |
| 1972 | { |
| 1973 | int op_type = 10; |
| 1974 | fprintf(pp, " 0=%d", op_type); |
| 1975 | } |
| 1976 | else if (n.op == "Crop") |
| 1977 | { |
| 1978 | int num_args = n.attr("num_args"); |
| 1979 | std::vector<int> offset = n.attr("offset"); |
| 1980 | |
| 1981 | int woffset = 0; |
| 1982 | int hoffset = 0; |
| 1983 | if (offset.size() == 2) |
| 1984 | { |
| 1985 | woffset = offset[1]; |
| 1986 | hoffset = offset[0]; |
| 1987 | } |
| 1988 | |
| 1989 | fprintf(pp, " 0=%d", woffset); |
| 1990 | fprintf(pp, " 1=%d", hoffset); |
| 1991 | fprintf(pp, " 2=0"); |
| 1992 | |
| 1993 | if (num_args == 1) |
| 1994 | { |
| 1995 | std::vector<int> h_w = n.attr("h_w"); |
| 1996 | fprintf(pp, " 3=%d", h_w[1]); |
| 1997 | fprintf(pp, " 4=%d", h_w[0]); |
| 1998 | fprintf(pp, " 5=0"); |
| 1999 | } |
| 2000 | } |
| 2001 | else if (n.op == "Deconvolution") |
| 2002 | { |
| 2003 | int num_filter = n.attr("num_filter"); |
| 2004 | std::vector<int> kernel = n.attr("kernel"); |
| 2005 | std::vector<int> dilate = n.attr("dilate"); |
| 2006 | std::vector<int> stride = n.attr("stride"); |
| 2007 | std::vector<int> pad = n.attr("pad"); |
| 2008 | std::vector<int> adj = n.attr("adj"); |
| 2009 | std::vector<int> target_shape = n.attr("target_shape"); |
| 2010 | int no_bias = n.attr("no_bias"); |
| 2011 | int num_group = n.attr("num_group"); |
| 2012 | |
| 2013 | std::vector<float> weight_data = n.weight(0); |
| 2014 | std::vector<float> bias_data = n.weight(1); |
| 2015 | |
| 2016 | fprintf(pp, " 0=%d", num_filter); |
| 2017 | if (kernel.size() == 1) |
| 2018 | { |
| 2019 | fprintf(pp, " 1=%d", kernel[0]); |
| 2020 | } |
| 2021 | else if (kernel.size() == 2) |
| 2022 | { |
| 2023 | fprintf(pp, " 1=%d", kernel[1]); |
| 2024 | fprintf(pp, " 11=%d", kernel[0]); |
| 2025 | } |
| 2026 | |
| 2027 | if (dilate.size() == 1) |
| 2028 | { |
| 2029 | fprintf(pp, " 2=%d", dilate[0]); |
| 2030 | } |
| 2031 | else if (dilate.size() == 2) |
| 2032 | { |
| 2033 | fprintf(pp, " 2=%d", dilate[1]); |
| 2034 | fprintf(pp, " 12=%d", dilate[0]); |
| 2035 | } |
| 2036 | |
| 2037 | if (stride.size() == 1) |
| 2038 | { |
| 2039 | fprintf(pp, " 3=%d", stride[0]); |
| 2040 | } |
| 2041 | else if (stride.size() == 2) |
| 2042 | { |
| 2043 | fprintf(pp, " 3=%d", stride[1]); |
| 2044 | fprintf(pp, " 13=%d", stride[0]); |
| 2045 | } |
| 2046 | |
| 2047 | if (target_shape.size() == 0) |
| 2048 | { |
| 2049 | if (pad.size() == 1) |
| 2050 | { |
| 2051 | fprintf(pp, " 4=%d", pad[0]); |
| 2052 | } |
| 2053 | else if (pad.size() == 2) |
| 2054 | { |
| 2055 | fprintf(pp, " 4=%d", pad[1]); |
| 2056 | fprintf(pp, " 14=%d", pad[0]); |
| 2057 | } |
| 2058 | |
| 2059 | if (adj.size() == 1) |
| 2060 | { |
| 2061 | fprintf(pp, " 18=%d", adj[0]); |
| 2062 | } |
| 2063 | else if (adj.size() == 2) |
| 2064 | { |
| 2065 | fprintf(pp, " 18=%d", adj[1]); |
| 2066 | fprintf(pp, " 19=%d", adj[0]); |
| 2067 | } |
| 2068 | } |
| 2069 | else |
| 2070 | { |
| 2071 | fprintf(pp, " 4=-233"); |
| 2072 | |
| 2073 | if (target_shape.size() == 1) |
| 2074 | { |
| 2075 | fprintf(pp, " 20=%d", target_shape[0]); |
| 2076 | } |
| 2077 | else if (target_shape.size() == 2) |
| 2078 | { |
| 2079 | fprintf(pp, " 20=%d", target_shape[1]); |
| 2080 | fprintf(pp, " 21=%d", target_shape[0]); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | fprintf(pp, " 5=%d", no_bias == 1 ? 0 : 1); |
| 2085 | fprintf(pp, " 6=%d", (int)weight_data.size()); |
| 2086 | if (num_group > 1) |
| 2087 | { |
| 2088 | fprintf(pp, " 7=%d", num_group); |
| 2089 | } |
| 2090 | |
| 2091 | int quantize_tag = 0; |
| 2092 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 2093 | |
| 2094 | int maxk = 0; |
| 2095 | if (kernel.size() == 2) |
| 2096 | { |
| 2097 | maxk = kernel[1] * kernel[0]; |
| 2098 | } |
| 2099 | else |
| 2100 | { |
| 2101 | maxk = kernel[0] * kernel[0]; |
| 2102 | } |
| 2103 | for (int g = 0; g < num_group; g++) |
| 2104 | { |
| 2105 | // reorder weight from inch-outch to outch-inch |
| 2106 | int num_filter_g = num_filter / num_group; |
| 2107 | int num_input = static_cast<int>(weight_data.size() / maxk / num_filter_g / num_group); |
| 2108 | const float* weight_data_ptr = weight_data.data() + g * maxk * num_filter_g * num_input; |
| 2109 | for (int k = 0; k < num_filter_g; k++) |
| 2110 | { |
| 2111 | for (int j = 0; j < num_input; j++) |
| 2112 | { |
| 2113 | fwrite(weight_data_ptr + (j * num_filter_g + k) * maxk, sizeof(float), maxk, bp); |
| 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp); |
| 2119 | } |
| 2120 | else if (n.op == "dot") |
| 2121 | { |
| 2122 | int transpose_a = n.attr("transpose_a"); |
| 2123 | int transpose_b = n.attr("transpose_b"); |
| 2124 | fprintf(pp, " 0=1.0"); // alpha |
| 2125 | fprintf(pp, " 1=1.0"); // beta |
| 2126 | fprintf(pp, " 2=%d", transpose_a); |
| 2127 | fprintf(pp, " 3=%d", transpose_b); |
| 2128 | } |
| 2129 | else if (n.op == "Dropout") |
| 2130 | { |
| 2131 | // float p = n.attr("p"); |
| 2132 | // fprintf(pp, " 0=%d", p); |
| 2133 | } |
| 2134 | else if (n.op == "elemwise_add" || n.op == "_add" || n.op == "_plus" || n.op == "_Plus") |
| 2135 | { |
| 2136 | int op_type = 0; |
| 2137 | fprintf(pp, " 0=%d", op_type); |
| 2138 | } |
| 2139 | else if (n.op == "elemwise_div" || n.op == "_div" || n.op == "_Div") |
| 2140 | { |
| 2141 | int op_type = 3; |
| 2142 | fprintf(pp, " 0=%d", op_type); |
| 2143 | } |
| 2144 | else if (n.op == "elemwise_mul" || n.op == "_mul" || n.op == "_Mul") |
| 2145 | { |
| 2146 | int op_type = 2; |
| 2147 | fprintf(pp, " 0=%d", op_type); |
| 2148 | } |
| 2149 | else if (n.op == "elemwise_sub" || n.op == "_sub" || n.op == "_minus" || n.op == "_Minus") |
| 2150 | { |
| 2151 | int op_type = 1; |
| 2152 | fprintf(pp, " 0=%d", op_type); |
| 2153 | } |
| 2154 | else if (n.op == "Embedding") |
| 2155 | { |
| 2156 | int input_dim = n.attr("input_dim"); |
| 2157 | int output_dim = n.attr("output_dim"); |
| 2158 | |
| 2159 | std::vector<float> weight_data = n.weight(0); |
| 2160 | |
| 2161 | fprintf(pp, " 0=%d", output_dim); |
| 2162 | fprintf(pp, " 1=%d", input_dim); |
| 2163 | fprintf(pp, " 3=%d", (int)weight_data.size()); |
| 2164 | |
| 2165 | int quantize_tag = 0; |
| 2166 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 2167 | fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp); |
| 2168 | } |
| 2169 | else if (n.op == "exp") |
| 2170 | { |
| 2171 | int op_type = 7; |
| 2172 | fprintf(pp, " 0=%d", op_type); |
| 2173 | } |
| 2174 | else if (n.op == "expand_dims") |
| 2175 | { |
| 2176 | int axis = n.attr("axis"); |
| 2177 | |
| 2178 | fprintf(pp, " -23303=1,%d", axis); |
| 2179 | } |
| 2180 | else if (n.op == "Flatten") |
| 2181 | { |
| 2182 | // no param |
| 2183 | } |
| 2184 | else if (n.op == "floor") |
| 2185 | { |
| 2186 | int op_type = 2; |
| 2187 | fprintf(pp, " 0=%d", op_type); |
| 2188 | } |
| 2189 | else if (n.op == "FullyConnected") |
| 2190 | { |
| 2191 | int num_hidden = n.attr("num_hidden"); |
| 2192 | int no_bias = n.attr("no_bias"); |
| 2193 | // int flatten = n.attr("flatten"); |
| 2194 | |
| 2195 | // TODO flatten |
| 2196 | |
| 2197 | std::vector<float> weight_data = n.weight(0); |
| 2198 | std::vector<float> bias_data = n.weight(1); |
| 2199 | |
| 2200 | fprintf(pp, " 0=%d", num_hidden); |
| 2201 | fprintf(pp, " 1=%d", no_bias == 1 ? 0 : 1); |
| 2202 | fprintf(pp, " 2=%d", (int)weight_data.size()); |
| 2203 | |
| 2204 | int quantize_tag = 0; |
| 2205 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 2206 | fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp); |
| 2207 | fwrite(bias_data.data(), sizeof(float), bias_data.size(), bp); |
| 2208 | } |
| 2209 | else if (n.op == "HardSigmoid") |
| 2210 | { |
| 2211 | float alpha = n.attr("alpha"); |
| 2212 | float beta = n.attr("beta"); |
| 2213 | |
| 2214 | fprintf(pp, " 0=%e", alpha); |
| 2215 | fprintf(pp, " 1=%e", beta); |
| 2216 | } |
| 2217 | else if (n.op == "HardSwish") |
| 2218 | { |
| 2219 | float alpha = n.attr("alpha"); |
| 2220 | float beta = n.attr("beta"); |
| 2221 | |
| 2222 | fprintf(pp, " 0=%e", alpha); |
| 2223 | fprintf(pp, " 1=%e", beta); |
| 2224 | } |
| 2225 | else if (n.op == "InstanceNorm") |
| 2226 | { |
| 2227 | float eps = n.has_attr("eps") ? n.attr("eps") : 0.001f; |
| 2228 | |
| 2229 | std::vector<float> gamma_data = n.weight(0); |
| 2230 | std::vector<float> beta_data = n.weight(1); |
| 2231 | |
| 2232 | fprintf(pp, " 0=%d", (int)gamma_data.size()); |
| 2233 | fprintf(pp, " 1=%e", eps); |
| 2234 | |
| 2235 | fwrite(gamma_data.data(), sizeof(float), gamma_data.size(), bp); |
| 2236 | fwrite(beta_data.data(), sizeof(float), beta_data.size(), bp); |
| 2237 | } |
| 2238 | else if (n.op == "L2Normalization") |
| 2239 | { |
| 2240 | std::string mode = n.attr("mode"); |
| 2241 | float eps = n.has_attr("eps") ? n.attr("eps") : 1e-10f; |
| 2242 | |
| 2243 | int across_spatial = 0; |
| 2244 | int across_channel = 1; |
| 2245 | int channel_shared = 1; |
| 2246 | int scale_data_size = 1; |
| 2247 | |
| 2248 | if (mode == "instance") |
| 2249 | { |
| 2250 | across_spatial = 1; |
| 2251 | across_channel = 1; |
| 2252 | } |
| 2253 | else if (mode == "channel") |
| 2254 | { |
| 2255 | across_spatial = 0; |
| 2256 | across_channel = 1; |
| 2257 | } |
| 2258 | else if (mode == "spatial") |
| 2259 | { |
| 2260 | across_spatial = 1; |
| 2261 | across_channel = 0; |
| 2262 | } |
| 2263 | |
| 2264 | fprintf(pp, " 0=%d", across_spatial); |
| 2265 | fprintf(pp, " 4=%d", across_channel); |
| 2266 | fprintf(pp, " 1=%d", channel_shared); |
| 2267 | fprintf(pp, " 2=%e", eps); |
| 2268 | fprintf(pp, " 3=%d", scale_data_size); |
| 2269 | |
| 2270 | const float scale_data[1] = {1.f}; |
| 2271 | fwrite(scale_data, sizeof(float), 1, bp); |
| 2272 | } |
| 2273 | else if (n.op == "LeakyReLU") |
| 2274 | { |
| 2275 | std::string type = n.attr("act_type"); |
| 2276 | if (type == "elu") |
| 2277 | { |
| 2278 | float slope = n.has_attr("slope") ? n.attr("slope") : 0.25f; |
| 2279 | fprintf(pp, " 0=%e", slope); |
| 2280 | } |
| 2281 | else if (type == "leaky" || type.empty()) |
| 2282 | { |
| 2283 | float slope = n.has_attr("slope") ? n.attr("slope") : 0.25f; |
| 2284 | fprintf(pp, " 0=%e", slope); |
| 2285 | } |
| 2286 | else if (type == "prelu") |
| 2287 | { |
| 2288 | std::vector<float> weight_data = n.weight(0); |
| 2289 | |
| 2290 | fprintf(pp, " 0=%d", (int)weight_data.size()); |
| 2291 | |
| 2292 | fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp); |
| 2293 | } |
| 2294 | } |
| 2295 | else if (n.op == "LinearRegressionOutput") |
| 2296 | { |
| 2297 | // noop |
| 2298 | } |
| 2299 | else if (n.op == "log") |
| 2300 | { |
| 2301 | int op_type = 8; |
| 2302 | fprintf(pp, " 0=%d", op_type); |
| 2303 | } |
| 2304 | else if (n.op == "LogisticRegressionOutput") |
| 2305 | { |
| 2306 | // noop |
| 2307 | } |
| 2308 | else if (n.op == "MAERegressionOutput") |
| 2309 | { |
| 2310 | // noop |
| 2311 | } |
| 2312 | else if (n.op == "max" || n.op == "mean" || n.op == "min" || n.op == "prod" || n.op == "sum") |
| 2313 | { |
| 2314 | int operation = -233; |
| 2315 | if (n.op == "max") operation = 4; |
| 2316 | if (n.op == "mean") operation = 3; |
| 2317 | if (n.op == "min") operation = 5; |
| 2318 | if (n.op == "prod") operation = 6; |
| 2319 | if (n.op == "sum") operation = 0; |
| 2320 | |
| 2321 | std::vector<int> axis = n.attr("axis"); |
| 2322 | int keepdims = n.attr("keepdims"); |
| 2323 | |
| 2324 | fprintf(pp, " 0=%d", operation); |
| 2325 | if (axis.empty()) |
| 2326 | { |
| 2327 | // if axis not set, reduce all axis by default |
| 2328 | fprintf(pp, " 1=%d", 1); |
| 2329 | } |
| 2330 | else |
| 2331 | { |
| 2332 | // if axis set, reduce according to axis |
| 2333 | fprintf(pp, " 1=%d", 0); |
| 2334 | fprintf(pp, " -23303=%zd", axis.size()); |
| 2335 | for (size_t j = 0; j < axis.size(); j++) |
| 2336 | { |
| 2337 | if (axis[j] == 0 || axis[j] > 4 || axis[j] < -3) |
| 2338 | fprintf(stderr, "Unsupported reduction axis !\n"); |
| 2339 | fprintf(pp, ",%d", axis[j] > 0 ? axis[j] - 1 : axis[j]); |
| 2340 | } |
| 2341 | } |
| 2342 | fprintf(pp, " 4=%d", keepdims); |
| 2343 | fprintf(pp, " 5=1"); |
| 2344 | } |
| 2345 | else if (n.op == "maximum") |
| 2346 | { |
| 2347 | int op_type = 4; |
| 2348 | fprintf(pp, " 0=%d", op_type); |
| 2349 | } |
| 2350 | else if (n.op == "minimum") |
| 2351 | { |
| 2352 | int op_type = 5; |
| 2353 | fprintf(pp, " 0=%d", op_type); |
| 2354 | } |
| 2355 | else if (n.op == "negative") |
| 2356 | { |
| 2357 | int op_type = 1; |
| 2358 | fprintf(pp, " 0=%d", op_type); |
| 2359 | } |
| 2360 | else if (n.op == "Pad") |
| 2361 | { |
| 2362 | std::string mode = n.attr("mode"); |
| 2363 | std::vector<int> pad_width = n.attr("pad_width"); |
| 2364 | float constant_value = n.attr("constant_value"); |
| 2365 | |
| 2366 | int type = 0; |
| 2367 | if (mode == "constant") |
| 2368 | { |
| 2369 | type = 0; |
| 2370 | } |
| 2371 | else if (mode == "edge") |
| 2372 | { |
| 2373 | type = 1; |
| 2374 | } |
| 2375 | else if (mode == "reflect") |
| 2376 | { |
| 2377 | type = 2; |
| 2378 | } |
| 2379 | |
| 2380 | if (pad_width.size() != 8) |
| 2381 | { |
| 2382 | fprintf(stderr, "Unsupported pad_width !\n"); |
| 2383 | } |
| 2384 | |
| 2385 | int channel_before = pad_width[2]; |
| 2386 | int channel_after = pad_width[3]; |
| 2387 | int top = pad_width[4]; |
| 2388 | int bottom = pad_width[5]; |
| 2389 | int left = pad_width[6]; |
| 2390 | int right = pad_width[7]; |
| 2391 | |
| 2392 | fprintf(pp, " 0=%d", top); |
| 2393 | fprintf(pp, " 1=%d", bottom); |
| 2394 | fprintf(pp, " 2=%d", left); |
| 2395 | fprintf(pp, " 3=%d", right); |
| 2396 | fprintf(pp, " 4=%d", type); |
| 2397 | fprintf(pp, " 5=%e", constant_value); |
| 2398 | fprintf(pp, " 7=%d", channel_before); |
| 2399 | fprintf(pp, " 8=%d", channel_after); |
| 2400 | } |
| 2401 | else if (n.op == "Pooling") |
| 2402 | { |
| 2403 | std::string pool_type = n.attr("pool_type"); |
| 2404 | std::vector<int> kernel = n.attr("kernel"); |
| 2405 | std::vector<int> stride = n.attr("stride"); |
| 2406 | std::vector<int> pad = n.attr("pad"); |
| 2407 | std::string pooling_convention = n.attr("pooling_convention"); |
| 2408 | int global_pool = n.attr("global_pool"); |
| 2409 | |
| 2410 | int pool = 0; |
| 2411 | if (pool_type == "max") |
| 2412 | { |
| 2413 | pool = 0; |
| 2414 | } |
| 2415 | else if (pool_type == "avg") |
| 2416 | { |
| 2417 | pool = 1; |
| 2418 | } |
| 2419 | |
| 2420 | int pad_mode = 1; |
| 2421 | if (pooling_convention == "valid") |
| 2422 | { |
| 2423 | pad_mode = 1; |
| 2424 | } |
| 2425 | else if (pooling_convention == "full") |
| 2426 | { |
| 2427 | pad_mode = 0; |
| 2428 | } |
| 2429 | |
| 2430 | fprintf(pp, " 0=%d", pool); |
| 2431 | |
| 2432 | if (kernel.size() == 1) |
| 2433 | { |
| 2434 | fprintf(pp, " 1=%d", kernel[0]); |
| 2435 | } |
| 2436 | else if (kernel.size() == 2) |
| 2437 | { |
| 2438 | fprintf(pp, " 1=%d", kernel[1]); |
| 2439 | fprintf(pp, " 11=%d", kernel[0]); |
| 2440 | } |
| 2441 | |
| 2442 | if (stride.size() == 1) |
| 2443 | { |
| 2444 | fprintf(pp, " 2=%d", stride[0]); |
| 2445 | } |
| 2446 | else if (stride.size() == 2) |
| 2447 | { |
| 2448 | fprintf(pp, " 2=%d", stride[1]); |
| 2449 | fprintf(pp, " 12=%d", stride[0]); |
| 2450 | } |
| 2451 | |
| 2452 | if (pad.size() == 1) |
| 2453 | { |
| 2454 | fprintf(pp, " 3=%d", pad[0]); |
| 2455 | } |
| 2456 | else if (pad.size() == 2) |
| 2457 | { |
| 2458 | fprintf(pp, " 3=%d", pad[1]); |
| 2459 | fprintf(pp, " 13=%d", pad[0]); |
| 2460 | } |
| 2461 | |
| 2462 | fprintf(pp, " 4=%d", global_pool); |
| 2463 | fprintf(pp, " 5=%d", pad_mode); |
| 2464 | |
| 2465 | if (pool_type == "avg") |
| 2466 | { |
| 2467 | int avgpool_count_include_pad = n.has_attr("count_include_pad") ? n.attr("count_include_pad") : 0; |
| 2468 | fprintf(pp, " 6=%d", avgpool_count_include_pad); |
| 2469 | } |
| 2470 | } |
| 2471 | else if (n.op == "reciprocal") |
| 2472 | { |
| 2473 | int op_type = 15; |
| 2474 | fprintf(pp, " 0=%d", op_type); |
| 2475 | } |
| 2476 | else if (n.op == "relu") |
| 2477 | { |
| 2478 | // no param |
| 2479 | } |
| 2480 | else if (n.op == "Reshape") |
| 2481 | { |
| 2482 | std::vector<int> shape = n.attr("shape"); |
| 2483 | |
| 2484 | if (shape.size() == 1) |
| 2485 | { |
| 2486 | fprintf(pp, " 0=%d", shape[0]); // should never reach here |
| 2487 | } |
| 2488 | else if (shape.size() == 2) |
| 2489 | { |
| 2490 | fprintf(pp, " 0=%d", shape[1]); |
| 2491 | } |
| 2492 | else if (shape.size() == 3) |
| 2493 | { |
| 2494 | fprintf(pp, " 0=%d", shape[2]); |
| 2495 | fprintf(pp, " 1=%d", shape[1]); |
| 2496 | } |
| 2497 | else if (shape.size() == 4) |
| 2498 | { |
| 2499 | fprintf(pp, " 0=%d", shape[3]); |
| 2500 | fprintf(pp, " 1=%d", shape[2]); |
| 2501 | fprintf(pp, " 2=%d", shape[1]); |
| 2502 | } |
| 2503 | else if (shape.size() == 5) |
| 2504 | { |
| 2505 | fprintf(pp, " 0=%d", shape[4] * shape[3]); |
| 2506 | fprintf(pp, " 1=%d", shape[2]); |
| 2507 | fprintf(pp, " 2=%d", shape[1]); |
| 2508 | } |
| 2509 | } |
| 2510 | else if (n.op == "ShuffleChannel") |
| 2511 | { |
| 2512 | int group = n.attr("group"); |
| 2513 | fprintf(pp, " 0=%d", group); |
| 2514 | } |
| 2515 | else if (n.op == "sigmoid") |
| 2516 | { |
| 2517 | // no param |
| 2518 | } |
| 2519 | else if (n.op == "sin") |
| 2520 | { |
| 2521 | int op_type = 9; |
| 2522 | fprintf(pp, " 0=%d", op_type); |
| 2523 | } |
| 2524 | else if (n.op == "slice") |
| 2525 | { |
| 2526 | std::vector<int> begin = n.attr("begin"); |
| 2527 | std::vector<int> end = n.attr("end"); |
| 2528 | std::vector<int> step = n.attr("step"); // TODO |
| 2529 | |
| 2530 | // skip N-dim |
| 2531 | begin.erase(begin.begin()); |
| 2532 | end.erase(end.begin()); |
| 2533 | if (step.size() != 0) |
| 2534 | step.erase(step.begin()); |
| 2535 | |
| 2536 | // assert step == 1 |
| 2537 | for (size_t j = 0; j < step.size(); j++) |
| 2538 | { |
| 2539 | if (step[j] != 1) |
| 2540 | fprintf(stderr, "Unsupported slice step !\n"); |
| 2541 | } |
| 2542 | |
| 2543 | fprintf(pp, " -23309=%d", (int)begin.size()); |
| 2544 | for (size_t j = 0; j < begin.size(); j++) |
| 2545 | { |
| 2546 | fprintf(pp, ",%d", begin[j]); |
| 2547 | } |
| 2548 | fprintf(pp, " -23310=%d", (int)end.size()); |
| 2549 | for (size_t j = 0; j < end.size(); j++) |
| 2550 | { |
| 2551 | fprintf(pp, ",%d", end[j]); |
| 2552 | } |
| 2553 | } |
| 2554 | else if (n.op == "slice_axis") |
| 2555 | { |
| 2556 | int axis = n.attr("axis"); |
| 2557 | int begin = n.attr("begin"); |
| 2558 | int end = n.has_attr("end") ? n.attr("end") : INT_MAX; |
| 2559 | |
| 2560 | if (axis == 0 || axis > 3 || axis < -3) |
| 2561 | fprintf(stderr, "Unsupported slice_axis axes !\n"); |
| 2562 | |
| 2563 | if (axis > 0) |
| 2564 | axis = axis - 1; // -1 for skip N-dim |
| 2565 | |
| 2566 | fprintf(pp, " -23309=1,%d", begin); |
| 2567 | fprintf(pp, " -23310=1,%d", end); |
| 2568 | fprintf(pp, " -23311=1,%d", axis); |
| 2569 | } |
| 2570 | else if (n.op == "SliceChannel") |
| 2571 | { |
| 2572 | int num_outputs = n.attr("num_outputs"); |
| 2573 | int squeeze_axis = n.attr("squeeze_axis"); // TODO |
| 2574 | if (squeeze_axis) |
| 2575 | { |
| 2576 | fprintf(stderr, "Unsupported SliceChannel squeeze_axis !\n"); |
| 2577 | } |
| 2578 | |
| 2579 | fprintf(pp, " -23300=%d", num_outputs); |
| 2580 | for (int j = 0; j < num_outputs; j++) |
| 2581 | { |
| 2582 | fprintf(pp, ",-233"); |
| 2583 | } |
| 2584 | } |
| 2585 | else if (n.op == "SoftmaxActivation") |
| 2586 | { |
| 2587 | std::string mode = n.attr("mode"); |
| 2588 | if (mode != "channel") |
| 2589 | { |
| 2590 | fprintf(stderr, "Unsupported SoftmaxActivation mode !\n"); |
| 2591 | } |
| 2592 | fprintf(pp, " 1=1"); |
| 2593 | } |
| 2594 | else if (n.op == "SoftmaxOutput") |
| 2595 | { |
| 2596 | fprintf(pp, " 1=1"); |
| 2597 | } |
| 2598 | else if (n.op == "softmax") |
| 2599 | { |
| 2600 | fprintf(pp, " 1=1"); |
| 2601 | } |
| 2602 | else if (n.op == "sqrt") |
| 2603 | { |
| 2604 | int op_type = 5; |
| 2605 | fprintf(pp, " 0=%d", op_type); |
| 2606 | } |
| 2607 | else if (n.op == "square") |
| 2608 | { |
| 2609 | int op_type = 4; |
| 2610 | fprintf(pp, " 0=%d", op_type); |
| 2611 | } |
| 2612 | else if (n.op == "squeeze") |
| 2613 | { |
| 2614 | std::vector<int> axis = n.attr("axis"); |
| 2615 | |
| 2616 | if (axis.empty()) |
| 2617 | { |
| 2618 | fprintf(pp, " 0=1"); |
| 2619 | fprintf(pp, " 1=1"); |
| 2620 | fprintf(pp, " 2=1"); |
| 2621 | } |
| 2622 | else |
| 2623 | { |
| 2624 | fprintf(pp, " -23303=%zd", axis.size()); |
| 2625 | for (size_t j = 0; j < axis.size(); j++) |
| 2626 | { |
| 2627 | fprintf(pp, ",%d", axis[j]); |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | else if (n.op == "tan") |
| 2632 | { |
| 2633 | int op_type = 11; |
| 2634 | fprintf(pp, " 0=%d", op_type); |
| 2635 | } |
| 2636 | else if (n.op == "tanh") |
| 2637 | { |
| 2638 | // no param |
| 2639 | } |
| 2640 | else if (n.op == "Transpose" || n.op == "transpose") |
| 2641 | { |
| 2642 | std::vector<int> axes = n.attr("axes"); |
| 2643 | |
| 2644 | if (axes.size() == 3) |
| 2645 | { |
| 2646 | if (axes[1] == 2 && axes[2] == 1) |
| 2647 | fprintf(pp, " 0=1"); // h w c |
| 2648 | else |
| 2649 | fprintf(stderr, "Unsupported transpose type !\n"); |
| 2650 | } |
| 2651 | else if (axes.size() == 4) |
| 2652 | { |
| 2653 | if (axes[1] == 1 && axes[2] == 2 && axes[3] == 3) |
| 2654 | fprintf(pp, " 0=0"); // w h c |
| 2655 | else if (axes[1] == 1 && axes[2] == 3 && axes[3] == 2) |
| 2656 | fprintf(pp, " 0=1"); // h w c |
| 2657 | else if (axes[1] == 2 && axes[2] == 1 && axes[3] == 3) |
| 2658 | fprintf(pp, " 0=2"); // w c h |
| 2659 | else if (axes[1] == 2 && axes[2] == 3 && axes[3] == 1) |
| 2660 | fprintf(pp, " 0=3"); // c w h |
| 2661 | else if (axes[1] == 3 && axes[2] == 1 && axes[3] == 2) |
| 2662 | fprintf(pp, " 0=4"); // h c w |
| 2663 | else if (axes[1] == 3 && axes[2] == 2 && axes[3] == 1) |
| 2664 | fprintf(pp, " 0=5"); // c h w |
| 2665 | } |
| 2666 | else if (axes.size() == 5) |
| 2667 | { |
| 2668 | if (axes[1] == 1 && axes[2] == 2 && axes[3] == 3 && axes[4] == 4) |
| 2669 | fprintf(pp, " 0=0"); // wx h c |
| 2670 | else if (axes[1] == 1 && axes[2] == 3 && axes[3] == 4 && axes[4] == 2) |
| 2671 | fprintf(pp, " 0=1"); // h wx c |
| 2672 | else if (axes[1] == 2 && axes[2] == 1 && axes[3] == 3 && axes[4] == 4) |
| 2673 | fprintf(pp, " 0=2"); // wx c h |
| 2674 | else if (axes[1] == 2 && axes[2] == 3 && axes[3] == 4 && axes[4] == 1) |
| 2675 | fprintf(pp, " 0=3"); // c wx h |
| 2676 | else if (axes[1] == 3 && axes[2] == 4 && axes[3] == 1 && axes[4] == 2) |
| 2677 | fprintf(pp, " 0=4"); // h c wx |
| 2678 | else if (axes[1] == 3 && axes[2] == 4 && axes[3] == 2 && axes[4] == 1) |
| 2679 | fprintf(pp, " 0=5"); // c h wx |
| 2680 | else |
| 2681 | fprintf(stderr, "Unsupported transpose type !\n"); |
| 2682 | } |
| 2683 | else |
| 2684 | { |
| 2685 | fprintf(stderr, "Unsupported transpose type !\n"); |
| 2686 | } |
| 2687 | } |
| 2688 | else if (n.op == "UpSampling") |
| 2689 | { |
| 2690 | int scale = n.attr("scale"); |
| 2691 | std::string sample_type = n.attr("sample_type"); |
| 2692 | |
| 2693 | if (sample_type == "nearest") |
| 2694 | { |
| 2695 | fprintf(pp, " 0=1"); |
| 2696 | fprintf(pp, " 1=%e", (float)scale); |
| 2697 | fprintf(pp, " 2=%e", (float)scale); |
| 2698 | } |
| 2699 | else if (sample_type == "bilinear") |
| 2700 | { |
| 2701 | // DeconvolutionDepthWise |
| 2702 | int num_filter = n.attr("num_filter"); |
| 2703 | |
| 2704 | std::vector<float> weight_data = n.weight(0); |
| 2705 | |
| 2706 | int kernel = scale * 2 - scale % 2; |
| 2707 | int stride = scale; |
| 2708 | int pad = (scale - 1) / 2; |
| 2709 | |
| 2710 | fprintf(pp, " 0=%d", num_filter); |
| 2711 | fprintf(pp, " 1=%d", kernel); |
| 2712 | fprintf(pp, " 2=1"); |
| 2713 | fprintf(pp, " 3=%d", stride); |
| 2714 | fprintf(pp, " 4=%d", pad); |
| 2715 | fprintf(pp, " 5=0"); |
| 2716 | fprintf(pp, " 6=%d", (int)weight_data.size()); |
| 2717 | fprintf(pp, " 7=%d", num_filter); |
| 2718 | |
| 2719 | int quantize_tag = 0; |
| 2720 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 2721 | |
| 2722 | fwrite(weight_data.data(), sizeof(float), weight_data.size(), bp); |
| 2723 | } |
| 2724 | } |
| 2725 | else |
| 2726 | { |
| 2727 | // TODO op specific params |
| 2728 | std::map<std::string, std::string>::const_iterator attr_it = n.attrs.begin(); |
| 2729 | for (; attr_it != n.attrs.end(); attr_it++) |
| 2730 | { |
| 2731 | fprintf(stderr, "# %s=%s\n", attr_it->first.c_str(), attr_it->second.c_str()); |
| 2732 | // fprintf(pp, " %s=%s", attr_it->first.c_str(), attr_it->second.c_str()); |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | fprintf(pp, "\n"); |
| 2737 | |
| 2738 | for (int j = 0; j < n.output_size; j++) |
| 2739 | { |
| 2740 | int input_uid = i | (j << 16); |
| 2741 | if (node_reference.find(input_uid) != node_reference.end()) |
| 2742 | { |
| 2743 | int refcount = node_reference[input_uid]; |
| 2744 | if (refcount > 1) |
| 2745 | { |
| 2746 | std::string output_name = n.name; |
| 2747 | |
| 2748 | char splitname[256]; |
| 2749 | sprintf(splitname, "splitncnn_%d", internal_split); |
| 2750 | fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount); |
| 2751 | if (j == 0) |
| 2752 | { |
| 2753 | fprintf(pp, " %s", output_name.c_str()); |
| 2754 | } |
| 2755 | else |
| 2756 | { |
| 2757 | fprintf(pp, " %s_subncnn_%d", output_name.c_str(), j); |
| 2758 | } |
| 2759 | |
| 2760 | for (int k = 0; k < refcount; k++) |
| 2761 | { |
| 2762 | if (j == 0) |
| 2763 | { |
| 2764 | fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), k); |
| 2765 | } |
| 2766 | else |
| 2767 | { |
| 2768 | fprintf(pp, " %s_subncnn_%d_splitncnn_%d", output_name.c_str(), j, k); |
| 2769 | } |
| 2770 | } |
| 2771 | fprintf(pp, "\n"); |
| 2772 | |
| 2773 | internal_split++; |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | fclose(pp); |
| 2780 | fclose(bp); |
| 2781 | |
| 2782 | return 0; |
| 2783 | } |