| 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 "onnx.pb.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <float.h> |
| 19 | #include <fstream> |
| 20 | #include <google/protobuf/io/coded_stream.h> |
| 21 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 22 | #include <google/protobuf/message.h> |
| 23 | #include <google/protobuf/text_format.h> |
| 24 | #include <iostream> |
| 25 | #include <limits.h> |
| 26 | #include <limits> |
| 27 | #include <set> |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | static bool read_proto_from_binary(const char* filepath, onnx::ModelProto* message) |
| 31 | { |
| 32 | std::ifstream fs(filepath, std::ifstream::in | std::ifstream::binary); |
| 33 | if (!fs.is_open()) |
| 34 | { |
| 35 | fprintf(stderr, "open failed %s\n", filepath); |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | google::protobuf::io::IstreamInputStream input(&fs); |
| 40 | google::protobuf::io::CodedInputStream codedstr(&input); |
| 41 | |
| 42 | #if GOOGLE_PROTOBUF_VERSION >= 3011000 |
| 43 | codedstr.SetTotalBytesLimit(INT_MAX); |
| 44 | #else |
| 45 | codedstr.SetTotalBytesLimit(INT_MAX, INT_MAX / 2); |
| 46 | #endif |
| 47 | |
| 48 | bool success = message->ParseFromCodedStream(&codedstr); |
| 49 | |
| 50 | fs.close(); |
| 51 | |
| 52 | return success; |
| 53 | } |
| 54 | |
| 55 | static std::vector<int> get_node_attr_ai(const onnx::NodeProto& node, const char* key) |
| 56 | { |
| 57 | std::vector<int> v; |
| 58 | |
| 59 | for (int i = 0; i < node.attribute_size(); i++) |
| 60 | { |
| 61 | const onnx::AttributeProto& attr = node.attribute(i); |
| 62 | if (attr.name() == key) |
| 63 | { |
| 64 | v.resize(attr.ints_size()); |
| 65 | for (int j = 0; j < attr.ints_size(); j++) |
| 66 | { |
| 67 | v[j] = std::max(std::min(attr.ints(j), (::google::protobuf::int64)INT_MAX), (::google::protobuf::int64)INT_MIN); |
| 68 | } |
| 69 | |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return v; |
| 75 | } |
| 76 | |
| 77 | static void set_node_attr_ai(onnx::NodeProto& node, const char* key, |
| 78 | const std::vector<int>& value) |
| 79 | { |
| 80 | onnx::AttributeProto* attr_group = node.add_attribute(); |
| 81 | attr_group->set_name(key); |
| 82 | for (auto v : value) |
| 83 | { |
| 84 | attr_group->add_ints(v); |
| 85 | } |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | static std::vector<float> get_node_attr_af(const onnx::NodeProto& node, const char* key) |
| 91 | { |
| 92 | std::vector<float> v; |
| 93 | |
| 94 | for (int i = 0; i < node.attribute_size(); i++) |
| 95 | { |
| 96 | const onnx::AttributeProto& attr = node.attribute(i); |
| 97 | if (attr.name() == key) |
| 98 | { |
| 99 | v.resize(attr.floats_size()); |
| 100 | for (int j = 0; j < attr.floats_size(); j++) |
| 101 | { |
| 102 | v[j] = attr.floats(j); |
| 103 | } |
| 104 | |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return v; |
| 110 | } |
| 111 | |
| 112 | static int get_node_attr_i(const onnx::NodeProto& node, const char* key, int def = 0) |
| 113 | { |
| 114 | for (int i = 0; i < node.attribute_size(); i++) |
| 115 | { |
| 116 | const onnx::AttributeProto& attr = node.attribute(i); |
| 117 | if (attr.name() == key) |
| 118 | { |
| 119 | return std::max(std::min(attr.i(), (::google::protobuf::int64)INT_MAX), (::google::protobuf::int64)INT_MIN); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return def; |
| 124 | } |
| 125 | |
| 126 | static float get_node_attr_f(const onnx::NodeProto& node, const char* key, float def = 0.f) |
| 127 | { |
| 128 | for (int i = 0; i < node.attribute_size(); i++) |
| 129 | { |
| 130 | const onnx::AttributeProto& attr = node.attribute(i); |
| 131 | if (attr.name() == key) |
| 132 | { |
| 133 | return attr.f(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return def; |
| 138 | } |
| 139 | |
| 140 | static std::string get_node_attr_s(const onnx::NodeProto& node, const char* key, const std::string& def = std::string()) |
| 141 | { |
| 142 | for (int i = 0; i < node.attribute_size(); i++) |
| 143 | { |
| 144 | const onnx::AttributeProto& attr = node.attribute(i); |
| 145 | if (attr.name() == key) |
| 146 | { |
| 147 | return attr.s(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return def; |
| 152 | } |
| 153 | |
| 154 | static onnx::TensorProto get_node_attr_tensor(const onnx::NodeProto& node, const char* key) |
| 155 | { |
| 156 | for (int i = 0; i < node.attribute_size(); i++) |
| 157 | { |
| 158 | const onnx::AttributeProto& attr = node.attribute(i); |
| 159 | if (attr.name() == key) |
| 160 | { |
| 161 | return attr.t(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return onnx::TensorProto(); |
| 166 | } |
| 167 | |
| 168 | static float get_node_attr_from_input_f(const onnx::TensorProto& tp) |
| 169 | { |
| 170 | float v = 0.f; |
| 171 | |
| 172 | // float |
| 173 | if (tp.data_type() == 1) |
| 174 | { |
| 175 | const float* shape_data = 0; |
| 176 | if (tp.has_raw_data()) |
| 177 | { |
| 178 | shape_data = (const float*)tp.raw_data().data(); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | shape_data = tp.float_data().data(); |
| 183 | } |
| 184 | v = shape_data[0]; |
| 185 | } |
| 186 | // double |
| 187 | else if (tp.data_type() == 11) |
| 188 | { |
| 189 | const double* shape_data = 0; |
| 190 | if (tp.has_raw_data()) |
| 191 | { |
| 192 | shape_data = (const double*)tp.raw_data().data(); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | shape_data = tp.double_data().data(); |
| 197 | } |
| 198 | v = shape_data[0]; |
| 199 | } |
| 200 | // int64 |
| 201 | else if (tp.data_type() == 7) |
| 202 | { |
| 203 | const int64_t* shape_data = 0; |
| 204 | if (tp.has_raw_data()) |
| 205 | { |
| 206 | shape_data = (const int64_t*)tp.raw_data().data(); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | shape_data = tp.int64_data().data(); |
| 211 | } |
| 212 | v = std::max(std::min(shape_data[0], (::google::protobuf::int64)INT_MAX), (::google::protobuf::int64)INT_MIN); |
| 213 | } |
| 214 | // int32 |
| 215 | else if (tp.data_type() == 6) |
| 216 | { |
| 217 | const int32_t* shape_data = 0; |
| 218 | if (tp.has_raw_data()) |
| 219 | { |
| 220 | shape_data = (const int32_t*)tp.raw_data().data(); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | shape_data = tp.int32_data().data(); |
| 225 | } |
| 226 | v = shape_data[0]; |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | fprintf(stderr, "Unknown data type %d\n", tp.data_type()); |
| 231 | abort(); |
| 232 | } |
| 233 | |
| 234 | return v; |
| 235 | } |
| 236 | |
| 237 | static std::vector<int> get_node_attr_from_input_ai(const onnx::TensorProto& tp) |
| 238 | { |
| 239 | int size = 0; |
| 240 | |
| 241 | std::vector<int> v; |
| 242 | |
| 243 | // int64 |
| 244 | if (tp.data_type() == 7) |
| 245 | { |
| 246 | const int64_t* shape_data = 0; |
| 247 | if (tp.has_raw_data()) |
| 248 | { |
| 249 | shape_data = (const int64_t*)tp.raw_data().data(); |
| 250 | size = (int)(tp.raw_data().size() / 8); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | shape_data = tp.int64_data().data(); |
| 255 | size = tp.int64_data_size(); |
| 256 | } |
| 257 | for (int j = 0; j < size; j++) |
| 258 | { |
| 259 | int vi = std::max(std::min(shape_data[j], (::google::protobuf::int64)INT_MAX), (::google::protobuf::int64)INT_MIN); |
| 260 | v.push_back(vi); |
| 261 | } |
| 262 | } |
| 263 | // int32 |
| 264 | else if (tp.data_type() == 6) |
| 265 | { |
| 266 | const int32_t* shape_data = 0; |
| 267 | if (tp.has_raw_data()) |
| 268 | { |
| 269 | shape_data = (const int32_t*)tp.raw_data().data(); |
| 270 | size = (int)(tp.raw_data().size() / 4); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | shape_data = tp.int32_data().data(); |
| 275 | size = tp.int32_data_size(); |
| 276 | } |
| 277 | for (int j = 0; j < size; j++) |
| 278 | { |
| 279 | v.push_back(shape_data[j]); |
| 280 | } |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | fprintf(stderr, "Unknown data type %d\n", tp.data_type()); |
| 285 | } |
| 286 | |
| 287 | return v; |
| 288 | } |
| 289 | |
| 290 | static std::vector<float> get_node_attr_from_input_af(const onnx::TensorProto& tp) |
| 291 | { |
| 292 | int size = 0; |
| 293 | |
| 294 | std::vector<float> v; |
| 295 | |
| 296 | // float |
| 297 | if (tp.data_type() == 1) |
| 298 | { |
| 299 | const float* shape_data = 0; |
| 300 | if (tp.has_raw_data()) |
| 301 | { |
| 302 | shape_data = (const float*)tp.raw_data().data(); |
| 303 | size = (int)(tp.raw_data().size() / 4); |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | shape_data = tp.float_data().data(); |
| 308 | size = tp.float_data_size(); |
| 309 | } |
| 310 | for (int j = 0; j < size; j++) |
| 311 | { |
| 312 | v.push_back(shape_data[j]); |
| 313 | } |
| 314 | } |
| 315 | // double |
| 316 | else if (tp.data_type() == 11) |
| 317 | { |
| 318 | const double* shape_data = 0; |
| 319 | if (tp.has_raw_data()) |
| 320 | { |
| 321 | shape_data = (const double*)tp.raw_data().data(); |
| 322 | size = (int)(tp.raw_data().size() / 8); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | shape_data = tp.double_data().data(); |
| 327 | size = tp.double_data_size(); |
| 328 | } |
| 329 | for (int j = 0; j < size; j++) |
| 330 | { |
| 331 | v.push_back((float)shape_data[j]); |
| 332 | } |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | fprintf(stderr, "Unknown data type %d\n", tp.data_type()); |
| 337 | } |
| 338 | |
| 339 | return v; |
| 340 | } |
| 341 | |
| 342 | static int get_tensor_proto_data_size(const onnx::TensorProto& tp) |
| 343 | { |
| 344 | if (tp.has_raw_data()) |
| 345 | { |
| 346 | const std::string& raw_data = tp.raw_data(); |
| 347 | int size = (int)raw_data.size() / 4; |
| 348 | return size; |
| 349 | } |
| 350 | else if (tp.data_type() == 1) |
| 351 | { |
| 352 | return tp.float_data_size(); |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static void fwrite_tensor_proto_data(const onnx::TensorProto& tp, FILE* bp) |
| 359 | { |
| 360 | int size = get_tensor_proto_data_size(tp); |
| 361 | |
| 362 | if (tp.has_raw_data()) |
| 363 | { |
| 364 | const std::string& raw_data = tp.raw_data(); |
| 365 | fwrite(raw_data.data(), sizeof(float), size, bp); |
| 366 | } |
| 367 | else if (tp.data_type() == 1) |
| 368 | { |
| 369 | fwrite(tp.float_data().data(), sizeof(float), size, bp); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static void fuse_rewrite_gather(onnx::GraphProto* mutable_graph, |
| 374 | std::map<std::string, onnx::TensorProto>& weights, |
| 375 | std::map<std::string, int>& node_reference, |
| 376 | std::set<std::string>& blob_names, int& reduced_node_count) |
| 377 | { |
| 378 | const int node_count = mutable_graph->node_size(); |
| 379 | for (int i = 0; i < node_count; ++i) |
| 380 | { |
| 381 | onnx::NodeProto* gather = mutable_graph->mutable_node(i); |
| 382 | if (gather->op_type() != "Gather") |
| 383 | { |
| 384 | continue; |
| 385 | } |
| 386 | auto indices = get_node_attr_from_input_ai(weights[gather->input(1)]); |
| 387 | if (indices.size() != 1) |
| 388 | { |
| 389 | continue; |
| 390 | } |
| 391 | |
| 392 | { |
| 393 | // reconstruct node connections |
| 394 | node_reference[gather->input(1)] -= 1; |
| 395 | std::string origin_inp = gather->input(0); |
| 396 | gather->clear_input(); |
| 397 | gather->add_input(origin_inp); |
| 398 | } |
| 399 | |
| 400 | { |
| 401 | // update axis, starts and ends |
| 402 | int axis = get_node_attr_i(*gather, "axis", 1) - 1; |
| 403 | |
| 404 | gather->set_op_type("Crop"); |
| 405 | gather->clear_attribute(); |
| 406 | |
| 407 | int indice = indices[0]; |
| 408 | set_node_attr_ai(*gather, "starts", std::vector<int> {indice}); |
| 409 | set_node_attr_ai(*gather, "ends", std::vector<int> {indice + 1}); |
| 410 | set_node_attr_ai(*gather, "axis", std::vector<int> {axis}); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static void fuse_weight_reshape(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 416 | { |
| 417 | int node_count = mutable_graph->node_size(); |
| 418 | for (int i = 0; i < node_count; i++) |
| 419 | { |
| 420 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 421 | |
| 422 | // weight <= Reshape(weight) |
| 423 | if (node->op_type() == "Reshape") |
| 424 | { |
| 425 | // check weight |
| 426 | if (weights.find(node->input(0)) == weights.end()) |
| 427 | continue; |
| 428 | |
| 429 | weights[node->output(0)] = weights[node->input(0)]; |
| 430 | |
| 431 | // set weight shape directly |
| 432 | std::vector<int> shape; |
| 433 | if (node->input_size() == 1) |
| 434 | { |
| 435 | shape = get_node_attr_ai(*node, "shape"); |
| 436 | } |
| 437 | else if (node->input_size() == 2) |
| 438 | { |
| 439 | // opset 5 |
| 440 | shape = get_node_attr_from_input_ai(weights[node->input(1)]); |
| 441 | } |
| 442 | |
| 443 | weights[node->output(0)].clear_dims(); |
| 444 | for (int j = 0; j < shape.size(); j++) |
| 445 | { |
| 446 | weights[node->output(0)].add_dims(shape[j]); |
| 447 | } |
| 448 | |
| 449 | // reduce |
| 450 | node->set_op_type("noop_reducedncnn"); |
| 451 | |
| 452 | node_reference[node->input(0)] -= 1; |
| 453 | if (node->input_size() == 2) |
| 454 | { |
| 455 | node_reference[node->input(1)] -= 1; |
| 456 | } |
| 457 | |
| 458 | reduced_node_count += 1; |
| 459 | i += 1; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | static void fuse_weight_transpose(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 465 | { |
| 466 | int node_count = mutable_graph->node_size(); |
| 467 | for (int i = 0; i < node_count; i++) |
| 468 | { |
| 469 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 470 | |
| 471 | // weight <= Transpose(weight) |
| 472 | if (node->op_type() == "Transpose") |
| 473 | { |
| 474 | // check weight |
| 475 | if (weights.find(node->input(0)) == weights.end()) |
| 476 | continue; |
| 477 | |
| 478 | if (weights[node->input(0)].dims_size() != 2) |
| 479 | continue; |
| 480 | |
| 481 | // perm = (1, 0) |
| 482 | std::vector<int> perm = get_node_attr_ai(*node, "perm"); |
| 483 | if (perm.size() != 2) |
| 484 | continue; |
| 485 | if (perm[0] != 1 || perm[1] != 0) |
| 486 | continue; |
| 487 | |
| 488 | weights[node->output(0)] = weights[node->input(0)]; |
| 489 | |
| 490 | // permute weight |
| 491 | { |
| 492 | onnx::TensorProto& B = weights[node->output(0)]; |
| 493 | |
| 494 | const int h = B.dims(0); |
| 495 | const int w = B.dims(1); |
| 496 | |
| 497 | std::vector<float> permuted_data; |
| 498 | permuted_data.reserve((size_t)h * w); |
| 499 | const float* bptr = B.has_raw_data() ? (const float*)B.raw_data().data() : B.float_data().data(); |
| 500 | |
| 501 | for (int j = 0; j < w; j++) |
| 502 | { |
| 503 | for (int k = 0; k < h; k++) |
| 504 | { |
| 505 | float vb = bptr[k * w + j]; |
| 506 | permuted_data.push_back(vb); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | B.set_dims(0, w); |
| 511 | B.set_dims(1, h); |
| 512 | |
| 513 | if (B.has_raw_data()) |
| 514 | { |
| 515 | B.set_raw_data(permuted_data.data(), permuted_data.size() * sizeof(float)); |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | for (int j = 0; j < (int)permuted_data.size(); j++) |
| 520 | B.set_float_data(j, permuted_data[j]); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // reduce |
| 525 | node->set_op_type("noop_reducedncnn"); |
| 526 | |
| 527 | node_reference[node->input(0)] -= 1; |
| 528 | |
| 529 | reduced_node_count += 1; |
| 530 | i += 1; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | static void fuse_shufflechannel(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 536 | { |
| 537 | int node_count = mutable_graph->node_size(); |
| 538 | for (int i = 0; i < node_count; i++) |
| 539 | { |
| 540 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 541 | |
| 542 | // ShuffleChannel <= Reshape - Transpose - Reshape |
| 543 | // ShuffleChannel <= Reshape - Transpose - Constant - Reshape |
| 544 | if (node->op_type() == "Reshape") |
| 545 | { |
| 546 | if (node_reference[node->output(0)] != 1) |
| 547 | continue; |
| 548 | |
| 549 | std::vector<int> shape; |
| 550 | if (node->input_size() == 1) |
| 551 | { |
| 552 | shape = get_node_attr_ai(*node, "shape"); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | // skip weight reshape |
| 557 | if (weights.find(node->input(1)) == weights.end()) |
| 558 | continue; |
| 559 | |
| 560 | shape = get_node_attr_from_input_ai(weights[node->input(1)]); |
| 561 | } |
| 562 | |
| 563 | // 1 groups channels_per_group, height, width |
| 564 | // reverse style = channels_per_group, groups, height * width |
| 565 | if (shape.size() != 5 && shape.size() != 3) |
| 566 | continue; |
| 567 | |
| 568 | if (shape.size() == 5 && shape[0] != 1) |
| 569 | continue; |
| 570 | |
| 571 | if (i + 2 >= node_count) |
| 572 | continue; |
| 573 | |
| 574 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 575 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 576 | |
| 577 | if (node3->op_type() == "Constant") |
| 578 | { |
| 579 | if (i + 3 >= node_count) |
| 580 | continue; |
| 581 | |
| 582 | node3 = mutable_graph->mutable_node(i + 3); |
| 583 | } |
| 584 | |
| 585 | if (node2->op_type() != "Transpose" || node3->op_type() != "Reshape") |
| 586 | continue; |
| 587 | |
| 588 | if (node_reference[node2->output(0)] != 1) |
| 589 | continue; |
| 590 | |
| 591 | // 0 2 1 3 4 |
| 592 | // reverse style = 1 0 2 |
| 593 | std::vector<int> perm = get_node_attr_ai(*node2, "perm"); |
| 594 | if (perm.size() != 5 && perm.size() != 3) |
| 595 | continue; |
| 596 | |
| 597 | if (perm.size() == 5 && (perm[0] != 0 || perm[1] != 2 || perm[2] != 1 || perm[3] != 3 || perm[4] != 4)) |
| 598 | continue; |
| 599 | |
| 600 | if (perm.size() == 3 && (perm[0] != 1 || perm[1] != 0 || perm[2] != 2)) |
| 601 | continue; |
| 602 | |
| 603 | std::vector<int> shape3; |
| 604 | if (node3->input_size() == 1) |
| 605 | { |
| 606 | shape3 = get_node_attr_ai(*node3, "shape"); |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | // skip weight reshape |
| 611 | if (weights.find(node3->input(1)) == weights.end()) |
| 612 | continue; |
| 613 | |
| 614 | shape3 = get_node_attr_from_input_ai(weights[node3->input(1)]); |
| 615 | } |
| 616 | |
| 617 | // 1, -1, height, width |
| 618 | // reverse style = group, -1, channels_per_group, height, width |
| 619 | if (shape3.size() != 4 && shape3.size() != 5) |
| 620 | continue; |
| 621 | |
| 622 | if (shape3.size() == 4 && (shape3[0] != 1 || (shape3[1] != -1 && shape3[1] != shape[1] * shape[2]))) |
| 623 | continue; |
| 624 | |
| 625 | if (shape3.size() == 5 && (shape3[0] != shape[1] || shape3[2] != shape[0] || shape3[3] * shape3[4] != shape[2])) |
| 626 | continue; |
| 627 | |
| 628 | // reduce |
| 629 | node->set_op_type("noop_reducedncnn"); |
| 630 | node2->set_op_type("noop_reducedncnn"); |
| 631 | |
| 632 | if (node->input_size() == 2) |
| 633 | { |
| 634 | node_reference[node->input(1)] -= 1; |
| 635 | } |
| 636 | node_reference[node->output(0)] -= 1; |
| 637 | node_reference[node2->output(0)] -= 1; |
| 638 | if (node3->input_size() == 2) |
| 639 | { |
| 640 | node_reference[node3->input(1)] -= 1; |
| 641 | } |
| 642 | |
| 643 | blob_names.erase(node->output(0)); |
| 644 | blob_names.erase(node2->output(0)); |
| 645 | |
| 646 | node3->set_op_type("ShuffleChannel"); |
| 647 | node3->set_input(0, node->input(0)); |
| 648 | |
| 649 | onnx::AttributeProto* attr_group = node3->add_attribute(); |
| 650 | attr_group->set_name("group"); |
| 651 | attr_group->set_i(shape[1]); |
| 652 | |
| 653 | onnx::AttributeProto* attr_reverse = node3->add_attribute(); |
| 654 | attr_reverse->set_name("reverse"); |
| 655 | attr_reverse->set_i(shape.size() == 3); |
| 656 | |
| 657 | reduced_node_count += 2; |
| 658 | i += 2; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | static void fuse_shufflechannel_split(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 664 | { |
| 665 | int node_count = mutable_graph->node_size(); |
| 666 | for (int i = 0; i < node_count; i++) |
| 667 | { |
| 668 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 669 | |
| 670 | // Split <= ShuffleChannel(reverse type) - Gather(0) - Gather(1) |
| 671 | if (node->op_type() == "ShuffleChannel") |
| 672 | { |
| 673 | // reverse = 1 |
| 674 | int reverse = get_node_attr_i(*node, "reverse"); |
| 675 | if (reverse != 1) |
| 676 | continue; |
| 677 | |
| 678 | if (i + 2 >= node_count) |
| 679 | continue; |
| 680 | |
| 681 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 682 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 683 | |
| 684 | if (node2->op_type() != "Gather" || node3->op_type() != "Gather") |
| 685 | continue; |
| 686 | |
| 687 | if (node2->input(0) != node->output(0) || node3->input(0) != node->output(0)) |
| 688 | continue; |
| 689 | |
| 690 | // axis = 0 |
| 691 | int gather2_axis = get_node_attr_i(*node2, "axis"); |
| 692 | if (gather2_axis != 0) |
| 693 | continue; |
| 694 | |
| 695 | // indices = 0 |
| 696 | if (weights.find(node2->input(1)) == weights.end()) |
| 697 | continue; |
| 698 | |
| 699 | std::vector<int> gather2_indices = get_node_attr_from_input_ai(weights[node2->input(1)]); |
| 700 | if (gather2_indices.size() != 1 || gather2_indices[0] != 0) |
| 701 | continue; |
| 702 | |
| 703 | // axis = 0 |
| 704 | int gather3_axis = get_node_attr_i(*node3, "axis"); |
| 705 | if (gather3_axis != 0) |
| 706 | continue; |
| 707 | |
| 708 | // indices = 1 |
| 709 | if (weights.find(node3->input(1)) == weights.end()) |
| 710 | continue; |
| 711 | |
| 712 | std::vector<int> gather3_indices = get_node_attr_from_input_ai(weights[node3->input(1)]); |
| 713 | if (gather3_indices.size() != 1 || gather3_indices[0] != 1) |
| 714 | continue; |
| 715 | |
| 716 | // reduce |
| 717 | node2->set_op_type("noop_reducedncnn"); |
| 718 | |
| 719 | node_reference[node->output(0)] -= 2; |
| 720 | node_reference[node2->input(1)] -= 1; |
| 721 | node_reference[node3->input(1)] -= 1; |
| 722 | |
| 723 | node3->set_op_type("Split"); |
| 724 | node3->clear_input(); |
| 725 | node3->add_input(node->output(0)); |
| 726 | node3->add_output(node3->output(0)); |
| 727 | node3->set_output(0, node2->output(0)); |
| 728 | |
| 729 | node3->clear_attribute(); |
| 730 | onnx::AttributeProto* attr_axis = node3->add_attribute(); |
| 731 | attr_axis->set_name("axis"); |
| 732 | attr_axis->set_i(1); |
| 733 | |
| 734 | reduced_node_count += 1; |
| 735 | i += 1; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | static void fuse_hardswish(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 741 | { |
| 742 | int node_count = mutable_graph->node_size(); |
| 743 | for (int i = 0; i < node_count; i++) |
| 744 | { |
| 745 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 746 | |
| 747 | // HardSwish <= Add(+3) - Clip(0,6) - Mul(X,) - Div(/6) |
| 748 | // HardSwish <= Add(+3) - Clip(0,6) - Mul(X,) - Mul(*(1/6)) |
| 749 | // HardSwish <= Add(+3) - Clip(0,6) - Mul(X,) - Constant - Div(/6) |
| 750 | // HardSwish <= Add(+3) - Clip(0,6) - Mul(X,) - Constant - Mul(*(1/6)) |
| 751 | // out = x * F.relu6(x + 3, inplace=True) / 6 |
| 752 | if (node->op_type() == "Add") |
| 753 | { |
| 754 | if (node_reference[node->output(0)] != 1) |
| 755 | continue; |
| 756 | |
| 757 | if (i + 3 >= node_count) |
| 758 | continue; |
| 759 | |
| 760 | if (weights.find(node->input(1)) == weights.end()) |
| 761 | continue; |
| 762 | |
| 763 | const onnx::TensorProto& add_three = weights[node->input(1)]; |
| 764 | if (add_three.dims_size() != 0 || get_tensor_proto_data_size(add_three) != 1) |
| 765 | continue; |
| 766 | |
| 767 | float constant_add_three = get_node_attr_from_input_f(add_three); |
| 768 | if (constant_add_three != 3.f) |
| 769 | continue; |
| 770 | |
| 771 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 772 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 773 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 774 | |
| 775 | if (node4->op_type() == "Constant") |
| 776 | { |
| 777 | if (i + 4 >= node_count) |
| 778 | continue; |
| 779 | |
| 780 | node4 = mutable_graph->mutable_node(i + 4); |
| 781 | } |
| 782 | |
| 783 | if (node2->op_type() != "Clip" || node3->op_type() != "Mul" || (node4->op_type() != "Div" && node4->op_type() != "Mul")) |
| 784 | continue; |
| 785 | |
| 786 | if (node_reference[node2->output(0)] != 1) |
| 787 | continue; |
| 788 | |
| 789 | float relu6_min; |
| 790 | float relu6_max; |
| 791 | if (node2->input_size() == 1) |
| 792 | { |
| 793 | relu6_min = get_node_attr_f(*node2, "min", -FLT_MAX); |
| 794 | relu6_max = get_node_attr_f(*node2, "max", FLT_MAX); |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | const onnx::TensorProto& min_tp = weights[node2->input(1)]; |
| 799 | const onnx::TensorProto& max_tp = weights[node2->input(2)]; |
| 800 | |
| 801 | relu6_min = get_node_attr_from_input_f(min_tp); |
| 802 | relu6_max = get_node_attr_from_input_f(max_tp); |
| 803 | } |
| 804 | if (relu6_min != 0.f || relu6_max != 6.f) |
| 805 | continue; |
| 806 | |
| 807 | if (node_reference[node3->output(0)] != 1) |
| 808 | continue; |
| 809 | |
| 810 | if (node3->input(0) != node->input(0) || node3->input(1) != node2->output(0)) |
| 811 | continue; |
| 812 | |
| 813 | if (weights.find(node4->input(1)) == weights.end()) |
| 814 | continue; |
| 815 | |
| 816 | const onnx::TensorProto& div_six = weights[node4->input(1)]; |
| 817 | if (div_six.dims_size() != 0 || get_tensor_proto_data_size(div_six) != 1) |
| 818 | continue; |
| 819 | |
| 820 | float constant_div_six = get_node_attr_from_input_f(div_six); |
| 821 | if (node4->op_type() == "Div" && constant_div_six != 6.f) |
| 822 | continue; |
| 823 | if (node4->op_type() == "Mul" && constant_div_six != 1 / 6.f) |
| 824 | continue; |
| 825 | |
| 826 | // reduce |
| 827 | node->set_op_type("noop_reducedncnn"); |
| 828 | node2->set_op_type("noop_reducedncnn"); |
| 829 | node3->set_op_type("noop_reducedncnn"); |
| 830 | |
| 831 | node_reference[node->input(0)] -= 1; |
| 832 | node_reference[node->input(1)] -= 1; |
| 833 | node_reference[node->output(0)] -= 1; |
| 834 | if (node2->input_size() == 3) |
| 835 | { |
| 836 | node_reference[node2->input(1)] -= 1; |
| 837 | node_reference[node2->input(2)] -= 1; |
| 838 | } |
| 839 | node_reference[node2->output(0)] -= 1; |
| 840 | node_reference[node3->output(0)] -= 1; |
| 841 | node_reference[node4->input(1)] -= 1; |
| 842 | |
| 843 | blob_names.erase(node->output(0)); |
| 844 | blob_names.erase(node2->output(0)); |
| 845 | blob_names.erase(node3->output(0)); |
| 846 | |
| 847 | node4->set_op_type("HardSwish"); |
| 848 | node4->clear_input(); |
| 849 | node4->add_input(node->input(0)); |
| 850 | |
| 851 | onnx::AttributeProto* attr_alpha = node4->add_attribute(); |
| 852 | attr_alpha->set_name("alpha"); |
| 853 | attr_alpha->set_f(1.f / 6.f); |
| 854 | |
| 855 | onnx::AttributeProto* attr_beta = node4->add_attribute(); |
| 856 | attr_beta->set_name("beta"); |
| 857 | attr_beta->set_f(3.f / 6.f); |
| 858 | |
| 859 | reduced_node_count += 3; |
| 860 | i += 3; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | for (int i = 0; i < node_count; i++) |
| 865 | { |
| 866 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 867 | |
| 868 | // HardSwish <= HardSigmoid - Mul |
| 869 | // out = x * hsigmoid(x) |
| 870 | if (node->op_type() == "HardSigmoid") |
| 871 | { |
| 872 | if (node_reference[node->output(0)] != 1) |
| 873 | continue; |
| 874 | |
| 875 | float alpha = get_node_attr_f(*node, "alpha", 0.2f); |
| 876 | float beta = get_node_attr_f(*node, "beta", 0.5f); |
| 877 | |
| 878 | if (i + 1 >= node_count) |
| 879 | continue; |
| 880 | |
| 881 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 882 | |
| 883 | if (node2->op_type() != "Mul") |
| 884 | continue; |
| 885 | |
| 886 | if (node2->input(0) != node->input(0) || node2->input(1) != node->output(0)) |
| 887 | continue; |
| 888 | |
| 889 | // reduce |
| 890 | node->set_op_type("noop_reducedncnn"); |
| 891 | |
| 892 | node_reference[node->input(0)] -= 1; |
| 893 | node_reference[node->output(0)] -= 1; |
| 894 | |
| 895 | blob_names.erase(node->output(0)); |
| 896 | |
| 897 | node2->set_op_type("HardSwish"); |
| 898 | node2->clear_input(); |
| 899 | node2->add_input(node->input(0)); |
| 900 | |
| 901 | onnx::AttributeProto* attr_alpha = node2->add_attribute(); |
| 902 | attr_alpha->set_name("alpha"); |
| 903 | attr_alpha->set_f(alpha); |
| 904 | |
| 905 | onnx::AttributeProto* attr_beta = node2->add_attribute(); |
| 906 | attr_beta->set_name("beta"); |
| 907 | attr_beta->set_f(beta); |
| 908 | |
| 909 | reduced_node_count += 1; |
| 910 | i += 1; |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | static void fuse_hardsigmoid(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 916 | { |
| 917 | int node_count = mutable_graph->node_size(); |
| 918 | for (int i = 0; i < node_count; i++) |
| 919 | { |
| 920 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 921 | |
| 922 | // HardSigmoid <= Add(+3) - Clip(0,6) - Div(/6) |
| 923 | // HardSigmoid <= Add(+3) - Clip(0,6) - Mul(*(1/6)) |
| 924 | // HardSigmoid <= Add(+3) - Clip(0,6) - Constant - Div(/6) |
| 925 | // HardSigmoid <= Add(+3) - Clip(0,6) - Constant - Mul(*(1/6)) |
| 926 | // out = F.relu6(x + 3, inplace=True) / 6 |
| 927 | if (node->op_type() == "Add") |
| 928 | { |
| 929 | if (node_reference[node->output(0)] != 1) |
| 930 | continue; |
| 931 | |
| 932 | if (i + 2 >= node_count) |
| 933 | continue; |
| 934 | |
| 935 | if (weights.find(node->input(1)) == weights.end()) |
| 936 | continue; |
| 937 | |
| 938 | const onnx::TensorProto& add_three = weights[node->input(1)]; |
| 939 | if (add_three.dims_size() != 0 || get_tensor_proto_data_size(add_three) != 1) |
| 940 | continue; |
| 941 | |
| 942 | float constant_add_three = get_node_attr_from_input_f(add_three); |
| 943 | if (constant_add_three != 3.f) |
| 944 | continue; |
| 945 | |
| 946 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 947 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 948 | |
| 949 | if (node3->op_type() == "Constant") |
| 950 | { |
| 951 | if (i + 3 >= node_count) |
| 952 | continue; |
| 953 | |
| 954 | node3 = mutable_graph->mutable_node(i + 3); |
| 955 | } |
| 956 | |
| 957 | if (node2->op_type() != "Clip" || (node3->op_type() != "Div" && node3->op_type() != "Mul")) |
| 958 | continue; |
| 959 | |
| 960 | if (node_reference[node2->output(0)] != 1) |
| 961 | continue; |
| 962 | |
| 963 | float relu6_min; |
| 964 | float relu6_max; |
| 965 | if (node2->input_size() == 1) |
| 966 | { |
| 967 | relu6_min = get_node_attr_f(*node2, "min", -FLT_MAX); |
| 968 | relu6_max = get_node_attr_f(*node2, "max", FLT_MAX); |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | const onnx::TensorProto& min_tp = weights[node2->input(1)]; |
| 973 | const onnx::TensorProto& max_tp = weights[node2->input(2)]; |
| 974 | |
| 975 | relu6_min = get_node_attr_from_input_f(min_tp); |
| 976 | relu6_max = get_node_attr_from_input_f(max_tp); |
| 977 | } |
| 978 | if (relu6_min != 0.f || relu6_max != 6.f) |
| 979 | continue; |
| 980 | |
| 981 | if (weights.find(node3->input(1)) == weights.end()) |
| 982 | continue; |
| 983 | |
| 984 | const onnx::TensorProto& div_six = weights[node3->input(1)]; |
| 985 | if (div_six.dims_size() != 0 || get_tensor_proto_data_size(div_six) != 1) |
| 986 | continue; |
| 987 | |
| 988 | float constant_div_six = get_node_attr_from_input_f(div_six); |
| 989 | if (node3->op_type() == "Div" && constant_div_six != 6.f) |
| 990 | continue; |
| 991 | if (node3->op_type() == "Mul" && constant_div_six != 1 / 6.f) |
| 992 | continue; |
| 993 | |
| 994 | // reduce |
| 995 | node->set_op_type("noop_reducedncnn"); |
| 996 | node2->set_op_type("noop_reducedncnn"); |
| 997 | |
| 998 | node_reference[node->input(1)] -= 1; |
| 999 | node_reference[node->output(0)] -= 1; |
| 1000 | if (node2->input_size() == 3) |
| 1001 | { |
| 1002 | node_reference[node2->input(1)] -= 1; |
| 1003 | node_reference[node2->input(2)] -= 1; |
| 1004 | } |
| 1005 | node_reference[node2->output(0)] -= 1; |
| 1006 | node_reference[node3->input(1)] -= 1; |
| 1007 | |
| 1008 | blob_names.erase(node->output(0)); |
| 1009 | blob_names.erase(node2->output(0)); |
| 1010 | |
| 1011 | node3->set_op_type("HardSigmoid"); |
| 1012 | node3->clear_input(); |
| 1013 | node3->add_input(node->input(0)); |
| 1014 | |
| 1015 | onnx::AttributeProto* attr_alpha = node3->add_attribute(); |
| 1016 | attr_alpha->set_name("alpha"); |
| 1017 | attr_alpha->set_f(1.f / 6.f); |
| 1018 | |
| 1019 | onnx::AttributeProto* attr_beta = node3->add_attribute(); |
| 1020 | attr_beta->set_name("beta"); |
| 1021 | attr_beta->set_f(3.f / 6.f); |
| 1022 | |
| 1023 | reduced_node_count += 2; |
| 1024 | i += 2; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | static void fuse_swish(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1030 | { |
| 1031 | int node_count = mutable_graph->node_size(); |
| 1032 | for (int i = 0; i < node_count; i++) |
| 1033 | { |
| 1034 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1035 | |
| 1036 | // Swish <= Sigmoid - Mul |
| 1037 | // x * torch.sigmoid(x) |
| 1038 | if (node->op_type() == "Sigmoid") |
| 1039 | { |
| 1040 | if (node_reference[node->output(0)] != 1) |
| 1041 | continue; |
| 1042 | |
| 1043 | if (i + 1 >= node_count) |
| 1044 | continue; |
| 1045 | |
| 1046 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1047 | |
| 1048 | if (node2->op_type() != "Mul") |
| 1049 | continue; |
| 1050 | |
| 1051 | if (node2->input(0) != node->input(0) || node2->input(1) != node->output(0)) |
| 1052 | continue; |
| 1053 | |
| 1054 | // reduce |
| 1055 | node->set_op_type("noop_reducedncnn"); |
| 1056 | |
| 1057 | node_reference[node->input(0)] -= 1; |
| 1058 | node_reference[node->output(0)] -= 1; |
| 1059 | |
| 1060 | blob_names.erase(node->output(0)); |
| 1061 | |
| 1062 | node2->set_op_type("Swish"); |
| 1063 | node2->clear_input(); |
| 1064 | node2->add_input(node->input(0)); |
| 1065 | |
| 1066 | reduced_node_count += 1; |
| 1067 | i += 1; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | static void fuse_batchnorm1d_squeeze_unsqueeze(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1073 | { |
| 1074 | int node_count = mutable_graph->node_size(); |
| 1075 | for (int i = 0; i < node_count; i++) |
| 1076 | { |
| 1077 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1078 | |
| 1079 | // BatchNormalization <= Unsqueeze - BatchNormalization - Squeeze |
| 1080 | if (node->op_type() == "Unsqueeze") |
| 1081 | { |
| 1082 | if (node_reference[node->output(0)] != 1) |
| 1083 | continue; |
| 1084 | |
| 1085 | if (i + 2 >= node_count) |
| 1086 | continue; |
| 1087 | |
| 1088 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1089 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1090 | |
| 1091 | if (node2->op_type() != "BatchNormalization" || node3->op_type() != "Squeeze") |
| 1092 | continue; |
| 1093 | |
| 1094 | if (node_reference[node2->output(0)] != 1) |
| 1095 | continue; |
| 1096 | |
| 1097 | if (node2->input(0) != node->output(0) || node3->input(0) != node2->output(0)) |
| 1098 | continue; |
| 1099 | |
| 1100 | // reduce |
| 1101 | node->set_op_type("noop_reducedncnn"); |
| 1102 | node3->set_op_type("noop_reducedncnn"); |
| 1103 | |
| 1104 | node_reference[node->output(0)] -= 1; |
| 1105 | node_reference[node2->output(0)] -= 1; |
| 1106 | |
| 1107 | blob_names.erase(node->output(0)); |
| 1108 | blob_names.erase(node2->output(0)); |
| 1109 | |
| 1110 | node2->set_input(0, node->input(0)); |
| 1111 | node2->set_output(0, node3->output(0)); |
| 1112 | |
| 1113 | reduced_node_count += 2; |
| 1114 | i += 2; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | static void fuse_unsqueeze_prelu(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1120 | { |
| 1121 | int node_count = mutable_graph->node_size(); |
| 1122 | for (int i = 0; i < node_count; i++) |
| 1123 | { |
| 1124 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1125 | |
| 1126 | // PReLU <= Unsqueeze - PReLU |
| 1127 | if (node->op_type() == "Unsqueeze") |
| 1128 | { |
| 1129 | // check weight |
| 1130 | if (weights.find(node->input(0)) == weights.end()) |
| 1131 | continue; |
| 1132 | |
| 1133 | onnx::TensorProto& B = weights[node->input(0)]; |
| 1134 | if (B.dims_size() != 1) |
| 1135 | continue; |
| 1136 | |
| 1137 | if (node_reference[node->output(0)] != 1) |
| 1138 | continue; |
| 1139 | |
| 1140 | // axes = (1, 2) |
| 1141 | std::vector<int> axes = get_node_attr_ai(*node, "axes"); |
| 1142 | if (axes.size() != 2) |
| 1143 | continue; |
| 1144 | if (axes[0] != 1 || axes[1] != 2) |
| 1145 | continue; |
| 1146 | |
| 1147 | if (i + 1 >= node_count) |
| 1148 | continue; |
| 1149 | |
| 1150 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1151 | |
| 1152 | if (node2->op_type() != "PRelu") |
| 1153 | continue; |
| 1154 | |
| 1155 | if (node2->input(1) != node->output(0)) |
| 1156 | continue; |
| 1157 | |
| 1158 | // reduce |
| 1159 | node->set_op_type("noop_reducedncnn"); |
| 1160 | |
| 1161 | node_reference[node->output(0)] -= 1; |
| 1162 | |
| 1163 | blob_names.erase(node->output(0)); |
| 1164 | |
| 1165 | node2->set_input(1, node->input(0)); |
| 1166 | |
| 1167 | reduced_node_count += 1; |
| 1168 | i += 1; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | static void fuse_normalize(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1174 | { |
| 1175 | int node_count = mutable_graph->node_size(); |
| 1176 | for (int i = 0; i < node_count; i++) |
| 1177 | { |
| 1178 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1179 | |
| 1180 | // Normalize <= X - ReduceL2 - Clip - Expand - Div |
| 1181 | // Normalize <= X - ReduceL2 - Clip - Shape - Expand - Div |
| 1182 | if (node->op_type() == "ReduceL2") |
| 1183 | { |
| 1184 | if (node_reference[node->output(0)] != 1) |
| 1185 | continue; |
| 1186 | |
| 1187 | // axes = (1) |
| 1188 | std::vector<int> axes = get_node_attr_ai(*node, "axes"); |
| 1189 | if (axes.size() != 1) |
| 1190 | continue; |
| 1191 | if (axes[0] != 1) |
| 1192 | continue; |
| 1193 | |
| 1194 | if (i + 3 >= node_count) |
| 1195 | continue; |
| 1196 | |
| 1197 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1198 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1199 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 1200 | |
| 1201 | bool has_shape_node = node3->op_type() == "Shape"; |
| 1202 | onnx::NodeProto* node_shape = 0; |
| 1203 | if (has_shape_node) |
| 1204 | { |
| 1205 | if (i + 4 >= node_count) |
| 1206 | continue; |
| 1207 | |
| 1208 | node_shape = node3; |
| 1209 | node3 = mutable_graph->mutable_node(i + 3); |
| 1210 | node4 = mutable_graph->mutable_node(i + 4); |
| 1211 | } |
| 1212 | |
| 1213 | if (node2->op_type() != "Clip" || node3->op_type() != "Expand" || node4->op_type() != "Div") |
| 1214 | continue; |
| 1215 | |
| 1216 | if (node_reference[node2->output(0)] != 1) |
| 1217 | continue; |
| 1218 | |
| 1219 | if (node_reference[node3->output(0)] != 1) |
| 1220 | continue; |
| 1221 | |
| 1222 | if (node2->input(0) != node->output(0) || node3->input(0) != node2->output(0) |
| 1223 | || node4->input(0) != node->input(0) || node4->input(1) != node3->output(0)) |
| 1224 | continue; |
| 1225 | |
| 1226 | if (has_shape_node) |
| 1227 | { |
| 1228 | if (node_shape->input(0) != node->input(0) || node3->input(1) != node_shape->output(0)) |
| 1229 | continue; |
| 1230 | } |
| 1231 | |
| 1232 | // +eps |
| 1233 | float clip_min; |
| 1234 | if (node2->input_size() == 1) |
| 1235 | { |
| 1236 | clip_min = get_node_attr_f(*node2, "min", -FLT_MAX); |
| 1237 | } |
| 1238 | else |
| 1239 | { |
| 1240 | const onnx::TensorProto& min_tp = weights[node2->input(1)]; |
| 1241 | |
| 1242 | clip_min = get_node_attr_from_input_f(min_tp); |
| 1243 | } |
| 1244 | |
| 1245 | // reduce |
| 1246 | node->set_op_type("noop_reducedncnn"); |
| 1247 | node2->set_op_type("noop_reducedncnn"); |
| 1248 | if (has_shape_node) |
| 1249 | { |
| 1250 | node_shape->set_op_type("noop_reducedncnn"); |
| 1251 | } |
| 1252 | node3->set_op_type("noop_reducedncnn"); |
| 1253 | |
| 1254 | node_reference[node->input(0)] -= has_shape_node ? 2 : 1; |
| 1255 | node_reference[node->output(0)] -= 1; |
| 1256 | node_reference[node2->output(0)] -= 1; |
| 1257 | if (has_shape_node) |
| 1258 | { |
| 1259 | node_reference[node_shape->output(0)] -= 1; |
| 1260 | } |
| 1261 | node_reference[node3->output(0)] -= 1; |
| 1262 | if (node3->input_size() == 2) |
| 1263 | { |
| 1264 | node_reference[node3->input(1)] -= 1; |
| 1265 | } |
| 1266 | |
| 1267 | blob_names.erase(node->output(0)); |
| 1268 | blob_names.erase(node2->output(0)); |
| 1269 | if (has_shape_node) |
| 1270 | { |
| 1271 | blob_names.erase(node_shape->output(0)); |
| 1272 | } |
| 1273 | blob_names.erase(node3->output(0)); |
| 1274 | |
| 1275 | node4->set_op_type("Normalize"); |
| 1276 | node4->clear_input(); |
| 1277 | node4->add_input(node->input(0)); |
| 1278 | |
| 1279 | onnx::AttributeProto* attr_alpha = node4->add_attribute(); |
| 1280 | attr_alpha->set_name("eps"); |
| 1281 | attr_alpha->set_f(clip_min); |
| 1282 | |
| 1283 | reduced_node_count += has_shape_node ? 4 : 3; |
| 1284 | i += has_shape_node ? 4 : 3; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | static void fuse_groupnorm(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1290 | { |
| 1291 | int node_count = mutable_graph->node_size(); |
| 1292 | for (int i = 0; i < node_count; i++) |
| 1293 | { |
| 1294 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1295 | |
| 1296 | // GroupNorm <= X - Reshape - InstanceNormalization - Reshape - Mul - Add |
| 1297 | if (node->op_type() == "Reshape") |
| 1298 | { |
| 1299 | if (node_reference[node->output(0)] != 1) |
| 1300 | continue; |
| 1301 | |
| 1302 | std::vector<int> shape; |
| 1303 | if (node->input_size() == 1) |
| 1304 | { |
| 1305 | shape = get_node_attr_ai(*node, "shape"); |
| 1306 | } |
| 1307 | else |
| 1308 | { |
| 1309 | // skip weight reshape |
| 1310 | if (weights.find(node->input(1)) == weights.end()) |
| 1311 | continue; |
| 1312 | |
| 1313 | shape = get_node_attr_from_input_ai(weights[node->input(1)]); |
| 1314 | } |
| 1315 | |
| 1316 | // 0, group, -1 |
| 1317 | if (shape.size() != 3) |
| 1318 | continue; |
| 1319 | |
| 1320 | if (shape[0] != 0 || shape[2] != -1) |
| 1321 | continue; |
| 1322 | |
| 1323 | int groups = shape[1]; |
| 1324 | |
| 1325 | if (i + 4 >= node_count) |
| 1326 | continue; |
| 1327 | |
| 1328 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1329 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1330 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 1331 | onnx::NodeProto* node5 = mutable_graph->mutable_node(i + 4); |
| 1332 | |
| 1333 | if (node2->op_type() != "InstanceNormalization" || node3->op_type() != "Reshape" || node4->op_type() != "Mul" || node5->op_type() != "Add") |
| 1334 | continue; |
| 1335 | |
| 1336 | if (node_reference[node2->output(0)] != 1) |
| 1337 | continue; |
| 1338 | |
| 1339 | if (node_reference[node3->output(0)] != 1) |
| 1340 | continue; |
| 1341 | |
| 1342 | if (node_reference[node4->output(0)] != 1) |
| 1343 | continue; |
| 1344 | |
| 1345 | if (node2->input(0) != node->output(0) || node3->input(0) != node2->output(0) |
| 1346 | || node4->input(0) != node3->output(0) || node5->input(0) != node4->output(0)) |
| 1347 | continue; |
| 1348 | |
| 1349 | // +eps |
| 1350 | float eps = get_node_attr_f(*node2, "epsilon", 1e-05f); |
| 1351 | |
| 1352 | // InstanceNormalization S=1 B=0 |
| 1353 | std::vector<float> S = get_node_attr_from_input_af(weights[node2->input(1)]); |
| 1354 | std::vector<float> B = get_node_attr_from_input_af(weights[node2->input(2)]); |
| 1355 | if ((int)S.size() != groups || (int)B.size() != groups) |
| 1356 | continue; |
| 1357 | |
| 1358 | bool instancenorm_affine = false; |
| 1359 | for (int j = 0; j < groups; j++) |
| 1360 | { |
| 1361 | if (S[j] != 1.f || B[j] != 0.f) |
| 1362 | { |
| 1363 | instancenorm_affine = true; |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | if (instancenorm_affine) |
| 1369 | continue; |
| 1370 | |
| 1371 | std::vector<int> shape2; |
| 1372 | if (node3->input_size() == 1) |
| 1373 | { |
| 1374 | shape2 = get_node_attr_ai(*node3, "shape"); |
| 1375 | } |
| 1376 | else |
| 1377 | { |
| 1378 | // skip weight reshape |
| 1379 | if (weights.find(node3->input(1)) == weights.end()) |
| 1380 | continue; |
| 1381 | |
| 1382 | shape2 = get_node_attr_from_input_ai(weights[node3->input(1)]); |
| 1383 | } |
| 1384 | |
| 1385 | // 1, channels, w, h |
| 1386 | if (shape2.size() != 4) |
| 1387 | continue; |
| 1388 | |
| 1389 | if (shape2[0] != 1) |
| 1390 | continue; |
| 1391 | |
| 1392 | int channels = shape2[1]; |
| 1393 | |
| 1394 | // affine |
| 1395 | std::vector<float> affine_S = get_node_attr_from_input_af(weights[node4->input(1)]); |
| 1396 | std::vector<float> affine_B = get_node_attr_from_input_af(weights[node5->input(1)]); |
| 1397 | if (affine_S.size() == 1 && affine_S[0] == 1.f && affine_B.size() == 1 && affine_B[0] == 0.f) |
| 1398 | { |
| 1399 | // no affine |
| 1400 | } |
| 1401 | else if ((int)affine_S.size() != channels && (int)affine_B.size() != channels) |
| 1402 | { |
| 1403 | // we only allow per-channel affine |
| 1404 | continue; |
| 1405 | } |
| 1406 | |
| 1407 | // reduce |
| 1408 | node->set_op_type("noop_reducedncnn"); |
| 1409 | node2->set_op_type("noop_reducedncnn"); |
| 1410 | node3->set_op_type("noop_reducedncnn"); |
| 1411 | node4->set_op_type("noop_reducedncnn"); |
| 1412 | |
| 1413 | if (node->input_size() == 2) |
| 1414 | { |
| 1415 | node_reference[node->input(1)] -= 1; |
| 1416 | } |
| 1417 | node_reference[node->output(0)] -= 1; |
| 1418 | node_reference[node2->input(1)] -= 1; |
| 1419 | node_reference[node2->input(2)] -= 1; |
| 1420 | node_reference[node2->output(0)] -= 1; |
| 1421 | if (node3->input_size() == 2) |
| 1422 | { |
| 1423 | node_reference[node3->input(1)] -= 1; |
| 1424 | } |
| 1425 | node_reference[node3->output(0)] -= 1; |
| 1426 | node_reference[node4->output(0)] -= 1; |
| 1427 | |
| 1428 | blob_names.erase(node->output(0)); |
| 1429 | blob_names.erase(node2->output(0)); |
| 1430 | blob_names.erase(node3->output(0)); |
| 1431 | blob_names.erase(node4->output(0)); |
| 1432 | |
| 1433 | std::string affine_scale = node4->input(1); |
| 1434 | std::string affine_bias = node5->input(1); |
| 1435 | |
| 1436 | node5->set_op_type("GroupNorm"); |
| 1437 | node5->clear_input(); |
| 1438 | node5->add_input(node->input(0)); |
| 1439 | node5->add_input(affine_scale); |
| 1440 | node5->add_input(affine_bias); |
| 1441 | |
| 1442 | onnx::AttributeProto* attr_groups = node5->add_attribute(); |
| 1443 | attr_groups->set_name("groups"); |
| 1444 | attr_groups->set_i(groups); |
| 1445 | |
| 1446 | onnx::AttributeProto* attr_channels = node5->add_attribute(); |
| 1447 | attr_channels->set_name("channels"); |
| 1448 | attr_channels->set_i(channels); |
| 1449 | |
| 1450 | onnx::AttributeProto* attr_eps = node5->add_attribute(); |
| 1451 | attr_eps->set_name("epsilon"); |
| 1452 | attr_eps->set_f(eps); |
| 1453 | |
| 1454 | onnx::AttributeProto* attr_affine = node5->add_attribute(); |
| 1455 | attr_affine->set_name("affine"); |
| 1456 | attr_affine->set_i(1); |
| 1457 | |
| 1458 | reduced_node_count += 4; |
| 1459 | i += 4; |
| 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | static void fuse_layernorm(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1465 | { |
| 1466 | int node_count = mutable_graph->node_size(); |
| 1467 | for (int i = 0; i < node_count; i++) |
| 1468 | { |
| 1469 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1470 | |
| 1471 | // LayerNorm <= X - ReduceMean - Sub - Pow - ReduceMean - Add - Sqrt - Div |
| 1472 | // LayerNorm <= X - ReduceMean - Sub - Pow - ReduceMean - Add - Sqrt - Div - Mul - Add |
| 1473 | if (node->op_type() == "ReduceMean") |
| 1474 | { |
| 1475 | if (node_reference[node->output(0)] != 1) |
| 1476 | continue; |
| 1477 | |
| 1478 | std::vector<int> axes = get_node_attr_ai(*node, "axes"); |
| 1479 | |
| 1480 | // -1 |
| 1481 | // -2 -1 |
| 1482 | if (axes.size() != 1 && axes.size() != 2) |
| 1483 | continue; |
| 1484 | |
| 1485 | int normed_axes = (int)axes.size(); |
| 1486 | if (normed_axes == 1 && axes[0] != -1) |
| 1487 | continue; |
| 1488 | if (normed_axes == 2 && (axes[0] != -2 || axes[1] != -1)) |
| 1489 | continue; |
| 1490 | |
| 1491 | if (i + 6 >= node_count) |
| 1492 | continue; |
| 1493 | |
| 1494 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1495 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1496 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 1497 | onnx::NodeProto* node5 = mutable_graph->mutable_node(i + 4); |
| 1498 | onnx::NodeProto* node6 = mutable_graph->mutable_node(i + 5); |
| 1499 | onnx::NodeProto* node7 = mutable_graph->mutable_node(i + 6); |
| 1500 | |
| 1501 | if (node2->op_type() != "Sub" || node3->op_type() != "Pow" || node4->op_type() != "ReduceMean" || node5->op_type() != "Add" || node6->op_type() != "Sqrt" || node7->op_type() != "Div") |
| 1502 | continue; |
| 1503 | |
| 1504 | if (node_reference[node2->output(0)] != 2) |
| 1505 | continue; |
| 1506 | |
| 1507 | if (node_reference[node3->output(0)] != 1) |
| 1508 | continue; |
| 1509 | |
| 1510 | if (node_reference[node4->output(0)] != 1) |
| 1511 | continue; |
| 1512 | |
| 1513 | if (node_reference[node5->output(0)] != 1) |
| 1514 | continue; |
| 1515 | |
| 1516 | if (node_reference[node6->output(0)] != 1) |
| 1517 | continue; |
| 1518 | |
| 1519 | if (node2->input(0) != node->input(0) || node2->input(1) != node->output(0) |
| 1520 | || node3->input(0) != node2->output(0) || node4->input(0) != node3->output(0) |
| 1521 | || node5->input(0) != node4->output(0) || node6->input(0) != node5->output(0) |
| 1522 | || node7->input(0) != node2->output(0) || node7->input(1) != node6->output(0)) |
| 1523 | continue; |
| 1524 | |
| 1525 | if (weights.find(node3->input(1)) == weights.end()) |
| 1526 | continue; |
| 1527 | |
| 1528 | const onnx::TensorProto& pow_two = weights[node3->input(1)]; |
| 1529 | if (pow_two.dims_size() != 0 || get_tensor_proto_data_size(pow_two) != 1) |
| 1530 | continue; |
| 1531 | |
| 1532 | float constant_pow_two = get_node_attr_from_input_f(pow_two); |
| 1533 | if (constant_pow_two != 2.f) |
| 1534 | continue; |
| 1535 | |
| 1536 | std::vector<int> axes4 = get_node_attr_ai(*node4, "axes"); |
| 1537 | |
| 1538 | // -1 |
| 1539 | // -2 -1 |
| 1540 | if ((int)axes4.size() != normed_axes) |
| 1541 | continue; |
| 1542 | |
| 1543 | if (normed_axes == 1 && axes4[0] != -1) |
| 1544 | continue; |
| 1545 | if (normed_axes == 2 && (axes4[0] != -2 || axes4[1] != -1)) |
| 1546 | continue; |
| 1547 | |
| 1548 | if (weights.find(node5->input(1)) == weights.end()) |
| 1549 | continue; |
| 1550 | |
| 1551 | const onnx::TensorProto& add_eps = weights[node5->input(1)]; |
| 1552 | if (add_eps.dims_size() != 0 || get_tensor_proto_data_size(add_eps) != 1) |
| 1553 | continue; |
| 1554 | |
| 1555 | float eps = get_node_attr_from_input_f(add_eps); |
| 1556 | |
| 1557 | int affine = 0; |
| 1558 | while (i + 8 < node_count) |
| 1559 | { |
| 1560 | onnx::NodeProto* node8 = mutable_graph->mutable_node(i + 7); |
| 1561 | onnx::NodeProto* node9 = mutable_graph->mutable_node(i + 8); |
| 1562 | |
| 1563 | if (node8->op_type() != "Mul" || node9->op_type() != "Add") |
| 1564 | break; |
| 1565 | |
| 1566 | if (node_reference[node7->output(0)] != 1) |
| 1567 | break; |
| 1568 | |
| 1569 | if (node_reference[node8->output(0)] != 1) |
| 1570 | break; |
| 1571 | |
| 1572 | if (node8->input(0) != node7->output(0) || node9->input(0) != node8->output(0)) |
| 1573 | break; |
| 1574 | |
| 1575 | // affine |
| 1576 | std::vector<float> affine_S = get_node_attr_from_input_af(weights[node8->input(1)]); |
| 1577 | std::vector<float> affine_B = get_node_attr_from_input_af(weights[node9->input(1)]); |
| 1578 | if (affine_S.size() != affine_B.size()) |
| 1579 | break; |
| 1580 | |
| 1581 | affine = 1; |
| 1582 | break; |
| 1583 | } |
| 1584 | |
| 1585 | // reduce |
| 1586 | node->set_op_type("noop_reducedncnn"); |
| 1587 | node2->set_op_type("noop_reducedncnn"); |
| 1588 | node3->set_op_type("noop_reducedncnn"); |
| 1589 | node4->set_op_type("noop_reducedncnn"); |
| 1590 | node5->set_op_type("noop_reducedncnn"); |
| 1591 | node6->set_op_type("noop_reducedncnn"); |
| 1592 | |
| 1593 | node_reference[node->input(0)] -= 1; |
| 1594 | node_reference[node2->input(0)] -= 1; |
| 1595 | node_reference[node2->input(1)] -= 1; |
| 1596 | node_reference[node3->input(0)] -= 1; |
| 1597 | node_reference[node3->input(1)] -= 1; |
| 1598 | node_reference[node4->input(0)] -= 1; |
| 1599 | node_reference[node5->input(0)] -= 1; |
| 1600 | node_reference[node5->input(1)] -= 1; |
| 1601 | node_reference[node6->input(0)] -= 1; |
| 1602 | node_reference[node7->input(0)] -= 1; |
| 1603 | node_reference[node7->input(1)] -= 1; |
| 1604 | |
| 1605 | blob_names.erase(node->output(0)); |
| 1606 | blob_names.erase(node2->output(0)); |
| 1607 | blob_names.erase(node3->output(0)); |
| 1608 | blob_names.erase(node4->output(0)); |
| 1609 | blob_names.erase(node5->output(0)); |
| 1610 | blob_names.erase(node6->output(0)); |
| 1611 | |
| 1612 | node_reference[node->input(0)] += 1; |
| 1613 | |
| 1614 | if (affine == 0) |
| 1615 | { |
| 1616 | node7->set_op_type("LayerNorm"); |
| 1617 | node7->clear_input(); |
| 1618 | node7->add_input(node->input(0)); |
| 1619 | |
| 1620 | onnx::AttributeProto* attr_eps = node7->add_attribute(); |
| 1621 | attr_eps->set_name("epsilon"); |
| 1622 | attr_eps->set_f(eps); |
| 1623 | |
| 1624 | onnx::AttributeProto* attr_affine = node7->add_attribute(); |
| 1625 | attr_affine->set_name("affine"); |
| 1626 | attr_affine->set_i(affine); |
| 1627 | |
| 1628 | reduced_node_count += 6; |
| 1629 | i += 6; |
| 1630 | } |
| 1631 | else // if (affine == 1) |
| 1632 | { |
| 1633 | onnx::NodeProto* node8 = mutable_graph->mutable_node(i + 7); |
| 1634 | onnx::NodeProto* node9 = mutable_graph->mutable_node(i + 8); |
| 1635 | |
| 1636 | node7->set_op_type("noop_reducedncnn"); |
| 1637 | node8->set_op_type("noop_reducedncnn"); |
| 1638 | |
| 1639 | node_reference[node8->input(0)] -= 1; |
| 1640 | node_reference[node9->input(0)] -= 1; |
| 1641 | |
| 1642 | blob_names.erase(node7->output(0)); |
| 1643 | blob_names.erase(node8->output(0)); |
| 1644 | |
| 1645 | std::string affine_scale = node8->input(1); |
| 1646 | std::string affine_bias = node9->input(1); |
| 1647 | |
| 1648 | node9->set_op_type("LayerNorm"); |
| 1649 | node9->clear_input(); |
| 1650 | node9->add_input(node->input(0)); |
| 1651 | node9->add_input(affine_scale); |
| 1652 | node9->add_input(affine_bias); |
| 1653 | |
| 1654 | onnx::AttributeProto* attr_eps = node9->add_attribute(); |
| 1655 | attr_eps->set_name("epsilon"); |
| 1656 | attr_eps->set_f(eps); |
| 1657 | |
| 1658 | onnx::AttributeProto* attr_affine = node9->add_attribute(); |
| 1659 | attr_affine->set_name("affine"); |
| 1660 | attr_affine->set_i(affine); |
| 1661 | |
| 1662 | reduced_node_count += 8; |
| 1663 | i += 8; |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | static void fuse_flatten(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1670 | { |
| 1671 | int node_count = mutable_graph->node_size(); |
| 1672 | for (int i = 0; i < node_count; i++) |
| 1673 | { |
| 1674 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1675 | |
| 1676 | // Flatten <= X - Shape - Gather - Constant - Unsqueeze - Unsqueeze - Concat - Reshape |
| 1677 | if (node->op_type() == "Shape") |
| 1678 | { |
| 1679 | if (node_reference[node->output(0)] != 1) |
| 1680 | continue; |
| 1681 | |
| 1682 | if (i + 6 >= node_count) |
| 1683 | continue; |
| 1684 | |
| 1685 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1686 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1687 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 1688 | onnx::NodeProto* node5 = mutable_graph->mutable_node(i + 4); |
| 1689 | onnx::NodeProto* node6 = mutable_graph->mutable_node(i + 5); |
| 1690 | onnx::NodeProto* node7 = mutable_graph->mutable_node(i + 6); |
| 1691 | |
| 1692 | if (node2->op_type() != "Gather" || node3->op_type() != "Constant" || node4->op_type() != "Unsqueeze" || node5->op_type() != "Unsqueeze" |
| 1693 | || node6->op_type() != "Concat" || node7->op_type() != "Reshape") |
| 1694 | continue; |
| 1695 | |
| 1696 | if (node_reference[node2->output(0)] != 1) |
| 1697 | continue; |
| 1698 | |
| 1699 | // if (node_reference[node3->output(0)] != 1) |
| 1700 | // continue; |
| 1701 | |
| 1702 | if (node_reference[node4->output(0)] != 1) |
| 1703 | continue; |
| 1704 | |
| 1705 | if (node_reference[node5->output(0)] != 1) |
| 1706 | continue; |
| 1707 | |
| 1708 | if (node_reference[node6->output(0)] != 1) |
| 1709 | continue; |
| 1710 | |
| 1711 | if (node2->input(0) != node->output(0) || node4->input(0) != node2->output(0) || node5->input(0) != node3->output(0) |
| 1712 | || node6->input(0) != node4->output(0) || node6->input(1) != node5->output(0) |
| 1713 | || node7->input(0) != node->input(0) || node7->input(1) != node6->output(0)) |
| 1714 | continue; |
| 1715 | |
| 1716 | // axis = 0 |
| 1717 | int gather_axis = get_node_attr_i(*node2, "axis"); |
| 1718 | if (gather_axis != 0) |
| 1719 | continue; |
| 1720 | |
| 1721 | // indices = 0 |
| 1722 | if (weights.find(node2->input(1)) == weights.end()) |
| 1723 | continue; |
| 1724 | |
| 1725 | std::vector<int> gather_indices = get_node_attr_from_input_ai(weights[node2->input(1)]); |
| 1726 | if (gather_indices.size() != 1 || gather_indices[0] != 0) |
| 1727 | continue; |
| 1728 | |
| 1729 | // axes = (0) |
| 1730 | std::vector<int> unsqueeze_axes = get_node_attr_ai(*node4, "axes"); |
| 1731 | if (unsqueeze_axes.size() != 1) |
| 1732 | continue; |
| 1733 | if (unsqueeze_axes[0] != 0) |
| 1734 | continue; |
| 1735 | |
| 1736 | // axes = (0) |
| 1737 | std::vector<int> unsqueeze2_axes = get_node_attr_ai(*node5, "axes"); |
| 1738 | if (unsqueeze2_axes.size() != 1) |
| 1739 | continue; |
| 1740 | if (unsqueeze2_axes[0] != 0) |
| 1741 | continue; |
| 1742 | |
| 1743 | // data = -1 |
| 1744 | if (weights.find(node5->input(0)) == weights.end()) |
| 1745 | continue; |
| 1746 | |
| 1747 | std::vector<int> unsqueeze2_data = get_node_attr_from_input_ai(weights[node5->input(0)]); |
| 1748 | if (unsqueeze2_data.size() != 1 || unsqueeze2_data[0] != -1) |
| 1749 | continue; |
| 1750 | |
| 1751 | // axis = 0 |
| 1752 | int concat_axis = get_node_attr_i(*node6, "axis"); |
| 1753 | if (concat_axis != 0) |
| 1754 | continue; |
| 1755 | |
| 1756 | // reduce |
| 1757 | node->set_op_type("noop_reducedncnn"); |
| 1758 | node2->set_op_type("noop_reducedncnn"); |
| 1759 | // node3->set_op_type("noop_reducedncnn"); |
| 1760 | node4->set_op_type("noop_reducedncnn"); |
| 1761 | node5->set_op_type("noop_reducedncnn"); |
| 1762 | node6->set_op_type("noop_reducedncnn"); |
| 1763 | |
| 1764 | node_reference[node->input(0)] -= 1; |
| 1765 | node_reference[node->output(0)] -= 1; |
| 1766 | node_reference[node2->input(1)] -= 1; |
| 1767 | node_reference[node2->output(0)] -= 1; |
| 1768 | // node_reference[node3->output(0)] -= 1; |
| 1769 | node_reference[node4->output(0)] -= 1; |
| 1770 | node_reference[node5->input(0)] -= 1; |
| 1771 | node_reference[node5->output(0)] -= 1; |
| 1772 | node_reference[node6->output(0)] -= 1; |
| 1773 | |
| 1774 | blob_names.erase(node->output(0)); |
| 1775 | blob_names.erase(node2->output(0)); |
| 1776 | // blob_names.erase(node3->output(0)); |
| 1777 | blob_names.erase(node4->output(0)); |
| 1778 | blob_names.erase(node5->output(0)); |
| 1779 | blob_names.erase(node6->output(0)); |
| 1780 | |
| 1781 | node7->set_op_type("Flatten"); |
| 1782 | node7->clear_input(); |
| 1783 | node7->add_input(node->input(0)); |
| 1784 | |
| 1785 | reduced_node_count += 5; |
| 1786 | i += 5; |
| 1787 | } |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | static void fuse_pixelshuffle(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1792 | { |
| 1793 | int node_count = mutable_graph->node_size(); |
| 1794 | for (int i = 0; i < node_count; i++) |
| 1795 | { |
| 1796 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1797 | |
| 1798 | // PixelShuffle <= Reshape - Transpose - Reshape |
| 1799 | // PixelShuffle <= Reshape - Transpose - Constant - Reshape |
| 1800 | if (node->op_type() == "Reshape") |
| 1801 | { |
| 1802 | if (node_reference[node->output(0)] != 1) |
| 1803 | continue; |
| 1804 | |
| 1805 | std::vector<int> shape; |
| 1806 | if (node->input_size() == 1) |
| 1807 | { |
| 1808 | shape = get_node_attr_ai(*node, "shape"); |
| 1809 | } |
| 1810 | else |
| 1811 | { |
| 1812 | // skip weight reshape |
| 1813 | if (weights.find(node->input(1)) == weights.end()) |
| 1814 | continue; |
| 1815 | |
| 1816 | shape = get_node_attr_from_input_ai(weights[node->input(1)]); |
| 1817 | } |
| 1818 | |
| 1819 | // -1, 3, upscale_factor, upscale_factor, height, width |
| 1820 | if (shape.size() != 6) |
| 1821 | continue; |
| 1822 | |
| 1823 | if (shape[0] != 1 && shape[0] != -1) |
| 1824 | continue; |
| 1825 | |
| 1826 | if (shape[2] != shape[3]) |
| 1827 | continue; |
| 1828 | |
| 1829 | if (i + 2 >= node_count) |
| 1830 | continue; |
| 1831 | |
| 1832 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1833 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1834 | |
| 1835 | if (node3->op_type() == "Constant") |
| 1836 | { |
| 1837 | if (i + 3 >= node_count) |
| 1838 | continue; |
| 1839 | |
| 1840 | node3 = mutable_graph->mutable_node(i + 3); |
| 1841 | } |
| 1842 | |
| 1843 | if (node2->op_type() != "Transpose" || node3->op_type() != "Reshape") |
| 1844 | continue; |
| 1845 | |
| 1846 | if (node_reference[node2->output(0)] != 1) |
| 1847 | continue; |
| 1848 | |
| 1849 | // 0 1 4 2 5 3 |
| 1850 | std::vector<int> perm = get_node_attr_ai(*node2, "perm"); |
| 1851 | if (perm.size() != 6) |
| 1852 | continue; |
| 1853 | |
| 1854 | if (perm[0] != 0 || perm[1] != 1 || perm[2] != 4 || perm[3] != 2 || perm[4] != 5 || perm[5] != 3) |
| 1855 | continue; |
| 1856 | |
| 1857 | std::vector<int> shape3; |
| 1858 | if (node3->input_size() == 1) |
| 1859 | { |
| 1860 | shape3 = get_node_attr_ai(*node3, "shape"); |
| 1861 | } |
| 1862 | else |
| 1863 | { |
| 1864 | // skip weight reshape |
| 1865 | if (weights.find(node3->input(1)) == weights.end()) |
| 1866 | continue; |
| 1867 | |
| 1868 | shape3 = get_node_attr_from_input_ai(weights[node3->input(1)]); |
| 1869 | } |
| 1870 | |
| 1871 | // -1, 3, height, width |
| 1872 | if (shape3.size() != 4) |
| 1873 | continue; |
| 1874 | |
| 1875 | if (shape3[0] != 1 && shape3[0] != -1) |
| 1876 | continue; |
| 1877 | |
| 1878 | if (shape3[1] != shape[1] || shape3[2] != shape[2] * shape[4] || shape3[3] != shape[3] * shape[5]) |
| 1879 | continue; |
| 1880 | |
| 1881 | // reduce |
| 1882 | node->set_op_type("noop_reducedncnn"); |
| 1883 | node2->set_op_type("noop_reducedncnn"); |
| 1884 | |
| 1885 | if (node->input_size() == 2) |
| 1886 | { |
| 1887 | node_reference[node->input(1)] -= 1; |
| 1888 | } |
| 1889 | node_reference[node->output(0)] -= 1; |
| 1890 | node_reference[node2->output(0)] -= 1; |
| 1891 | if (node3->input_size() == 2) |
| 1892 | { |
| 1893 | node_reference[node3->input(1)] -= 1; |
| 1894 | } |
| 1895 | |
| 1896 | blob_names.erase(node->output(0)); |
| 1897 | blob_names.erase(node2->output(0)); |
| 1898 | |
| 1899 | node3->set_op_type("PixelShuffle"); |
| 1900 | node3->set_input(0, node->input(0)); |
| 1901 | |
| 1902 | onnx::AttributeProto* attr_group = node3->add_attribute(); |
| 1903 | attr_group->set_name("scale_factor"); |
| 1904 | attr_group->set_i(shape[2]); |
| 1905 | |
| 1906 | reduced_node_count += 2; |
| 1907 | i += 2; |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | static void fuse_reorg(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 1913 | { |
| 1914 | int node_count = mutable_graph->node_size(); |
| 1915 | for (int i = 0; i < node_count; i++) |
| 1916 | { |
| 1917 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 1918 | |
| 1919 | // PixelShuffle <= Reshape - Transpose - Reshape |
| 1920 | // PixelShuffle <= Reshape - Transpose - Constant - Reshape |
| 1921 | if (node->op_type() == "Reshape") |
| 1922 | { |
| 1923 | if (node_reference[node->output(0)] != 1) |
| 1924 | continue; |
| 1925 | |
| 1926 | std::vector<int> shape; |
| 1927 | if (node->input_size() == 1) |
| 1928 | { |
| 1929 | shape = get_node_attr_ai(*node, "shape"); |
| 1930 | } |
| 1931 | else |
| 1932 | { |
| 1933 | // skip weight reshape |
| 1934 | if (weights.find(node->input(1)) == weights.end()) |
| 1935 | continue; |
| 1936 | |
| 1937 | shape = get_node_attr_from_input_ai(weights[node->input(1)]); |
| 1938 | } |
| 1939 | |
| 1940 | // -1, 3, out_height, block_size, out_width, block_size |
| 1941 | if (shape.size() != 6) |
| 1942 | continue; |
| 1943 | |
| 1944 | if (shape[0] != 1 && shape[0] != -1) |
| 1945 | continue; |
| 1946 | |
| 1947 | if (shape[3] != shape[5]) |
| 1948 | continue; |
| 1949 | |
| 1950 | if (i + 2 >= node_count) |
| 1951 | continue; |
| 1952 | |
| 1953 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 1954 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 1955 | |
| 1956 | if (node3->op_type() == "Constant") |
| 1957 | { |
| 1958 | if (i + 3 >= node_count) |
| 1959 | continue; |
| 1960 | |
| 1961 | node3 = mutable_graph->mutable_node(i + 3); |
| 1962 | } |
| 1963 | |
| 1964 | if (node2->op_type() != "Transpose" || node3->op_type() != "Reshape") |
| 1965 | continue; |
| 1966 | |
| 1967 | if (node_reference[node2->output(0)] != 1) |
| 1968 | continue; |
| 1969 | |
| 1970 | // 0 1 3 5 2 4 |
| 1971 | std::vector<int> perm = get_node_attr_ai(*node2, "perm"); |
| 1972 | if (perm.size() != 6) |
| 1973 | continue; |
| 1974 | |
| 1975 | if (perm[0] != 0 || perm[1] != 1 || perm[2] != 3 || perm[3] != 5 || perm[4] != 2 || perm[5] != 4) |
| 1976 | continue; |
| 1977 | |
| 1978 | std::vector<int> shape3; |
| 1979 | if (node3->input_size() == 1) |
| 1980 | { |
| 1981 | shape3 = get_node_attr_ai(*node3, "shape"); |
| 1982 | } |
| 1983 | else |
| 1984 | { |
| 1985 | // skip weight reshape |
| 1986 | if (weights.find(node3->input(1)) == weights.end()) |
| 1987 | continue; |
| 1988 | |
| 1989 | shape3 = get_node_attr_from_input_ai(weights[node3->input(1)]); |
| 1990 | } |
| 1991 | |
| 1992 | // -1, out_channels, out_height, out_width |
| 1993 | if (shape3.size() != 4) |
| 1994 | continue; |
| 1995 | |
| 1996 | if (shape3[0] != 1 && shape3[0] != -1) |
| 1997 | continue; |
| 1998 | |
| 1999 | if (shape3[1] != shape[1] * shape[3] * shape[5] || shape3[2] != shape[2] || shape3[3] != shape[4]) |
| 2000 | continue; |
| 2001 | |
| 2002 | // reduce |
| 2003 | node->set_op_type("noop_reducedncnn"); |
| 2004 | node2->set_op_type("noop_reducedncnn"); |
| 2005 | |
| 2006 | if (node->input_size() == 2) |
| 2007 | { |
| 2008 | node_reference[node->input(1)] -= 1; |
| 2009 | } |
| 2010 | node_reference[node->output(0)] -= 1; |
| 2011 | node_reference[node2->output(0)] -= 1; |
| 2012 | if (node3->input_size() == 2) |
| 2013 | { |
| 2014 | node_reference[node3->input(1)] -= 1; |
| 2015 | } |
| 2016 | |
| 2017 | blob_names.erase(node->output(0)); |
| 2018 | blob_names.erase(node2->output(0)); |
| 2019 | |
| 2020 | node3->set_op_type("Reorg"); |
| 2021 | node3->set_input(0, node->input(0)); |
| 2022 | |
| 2023 | onnx::AttributeProto* attr_group = node3->add_attribute(); |
| 2024 | attr_group->set_name("stride"); |
| 2025 | attr_group->set_i(shape[3]); |
| 2026 | |
| 2027 | reduced_node_count += 2; |
| 2028 | i += 2; |
| 2029 | } |
| 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | static void fuse_expand_broadcast(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 2034 | { |
| 2035 | int node_count = mutable_graph->node_size(); |
| 2036 | for (int i = 0; i < node_count; i++) |
| 2037 | { |
| 2038 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2039 | |
| 2040 | // Add/Sub/Mul/Div/Min/Max <= Expand - Add/Sub/Mul/Div/Min/Max |
| 2041 | if (node->op_type() == "Expand") |
| 2042 | { |
| 2043 | if (node_reference[node->output(0)] != 1) |
| 2044 | continue; |
| 2045 | |
| 2046 | if (i + 1 >= node_count) |
| 2047 | continue; |
| 2048 | |
| 2049 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 2050 | |
| 2051 | if (node2->op_type() != "Add" && node2->op_type() != "Sub" && node2->op_type() != "Mul" && node2->op_type() != "Div" && node2->op_type() != "Min" && node2->op_type() != "Max") |
| 2052 | continue; |
| 2053 | |
| 2054 | if (node2->input(1) != node->output(0) && node2->input(0) != node->output(0)) |
| 2055 | continue; |
| 2056 | |
| 2057 | // reduce |
| 2058 | node->set_op_type("noop_reducedncnn"); |
| 2059 | |
| 2060 | node_reference[node->output(0)] -= 1; |
| 2061 | if (node->input_size() == 2) |
| 2062 | { |
| 2063 | node_reference[node->input(1)] -= 1; |
| 2064 | } |
| 2065 | |
| 2066 | blob_names.erase(node->output(0)); |
| 2067 | |
| 2068 | if (node2->input(0) == node->output(0)) |
| 2069 | { |
| 2070 | node2->set_input(0, node->input(0)); |
| 2071 | } |
| 2072 | else |
| 2073 | { |
| 2074 | node2->set_input(1, node->input(0)); |
| 2075 | } |
| 2076 | |
| 2077 | reduced_node_count += 1; |
| 2078 | i += 1; |
| 2079 | } |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | static void fuse_lstm_gru_rnn(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 2084 | { |
| 2085 | int node_count = mutable_graph->node_size(); |
| 2086 | for (int i = 0; i < node_count; i++) |
| 2087 | { |
| 2088 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2089 | |
| 2090 | // LSTM(bi) <= LSTM(bi) - Transpose - Reshape - Transpose |
| 2091 | if (node->op_type() == "LSTM" || node->op_type() == "GRU" || node->op_type() == "RNN") |
| 2092 | { |
| 2093 | if (node_reference[node->output(0)] != 1) |
| 2094 | continue; |
| 2095 | |
| 2096 | if (i + 2 >= node_count) |
| 2097 | continue; |
| 2098 | |
| 2099 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 2100 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 2101 | |
| 2102 | if (node2->op_type() != "Transpose" || node3->op_type() != "Reshape") |
| 2103 | continue; |
| 2104 | |
| 2105 | if (node_reference[node2->output(0)] != 1) |
| 2106 | continue; |
| 2107 | |
| 2108 | if (node2->input(0) != node->output(0) || node3->input(0) != node2->output(0)) |
| 2109 | continue; |
| 2110 | |
| 2111 | std::string direction = get_node_attr_s(*node, "direction"); |
| 2112 | if (direction != "bidirectional") |
| 2113 | continue; |
| 2114 | |
| 2115 | // 0 2 1 3 |
| 2116 | std::vector<int> perm = get_node_attr_ai(*node2, "perm"); |
| 2117 | if (perm.size() != 4) |
| 2118 | continue; |
| 2119 | |
| 2120 | if (perm[0] != 0 || perm[1] != 2 || perm[2] != 1 || perm[3] != 3) |
| 2121 | continue; |
| 2122 | |
| 2123 | std::vector<int> shape; |
| 2124 | if (node3->input_size() == 1) |
| 2125 | { |
| 2126 | shape = get_node_attr_ai(*node3, "shape"); |
| 2127 | } |
| 2128 | else |
| 2129 | { |
| 2130 | // skip weight reshape |
| 2131 | if (weights.find(node3->input(1)) == weights.end()) |
| 2132 | continue; |
| 2133 | |
| 2134 | shape = get_node_attr_from_input_ai(weights[node3->input(1)]); |
| 2135 | } |
| 2136 | |
| 2137 | // 0 0 -1 |
| 2138 | if (shape.size() != 3) |
| 2139 | continue; |
| 2140 | |
| 2141 | if (shape[0] != 0 || shape[1] != 0 || shape[2] != -1) |
| 2142 | continue; |
| 2143 | |
| 2144 | // reduce |
| 2145 | node2->set_op_type("noop_reducedncnn"); |
| 2146 | node3->set_op_type("noop_reducedncnn"); |
| 2147 | |
| 2148 | node_reference[node->output(0)] -= 1; |
| 2149 | node_reference[node2->output(0)] -= 1; |
| 2150 | if (node3->input_size() == 2) |
| 2151 | { |
| 2152 | node_reference[node3->input(1)] -= 1; |
| 2153 | } |
| 2154 | |
| 2155 | blob_names.erase(node->output(0)); |
| 2156 | blob_names.erase(node2->output(0)); |
| 2157 | |
| 2158 | node->set_output(0, node3->output(0)); |
| 2159 | |
| 2160 | reduced_node_count += 2; |
| 2161 | i += 2; |
| 2162 | |
| 2163 | if (i + 1 < node_count) |
| 2164 | { |
| 2165 | if (node_reference[node3->output(0)] != 1) |
| 2166 | continue; |
| 2167 | |
| 2168 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 1); |
| 2169 | |
| 2170 | if (node4->op_type() != "Transpose") |
| 2171 | continue; |
| 2172 | |
| 2173 | if (node4->input(0) != node->output(0)) |
| 2174 | continue; |
| 2175 | |
| 2176 | // 1 0 2 |
| 2177 | std::vector<int> perm4 = get_node_attr_ai(*node4, "perm"); |
| 2178 | if (perm4.size() != 3) |
| 2179 | continue; |
| 2180 | |
| 2181 | if (perm4[0] != 1 || perm4[1] != 0 || perm4[2] != 2) |
| 2182 | continue; |
| 2183 | |
| 2184 | // reduce |
| 2185 | node4->set_op_type("noop_reducedncnn"); |
| 2186 | |
| 2187 | node_reference[node->output(0)] -= 1; |
| 2188 | |
| 2189 | blob_names.erase(node->output(0)); |
| 2190 | |
| 2191 | node->set_output(0, node4->output(0)); |
| 2192 | |
| 2193 | reduced_node_count += 1; |
| 2194 | i += 1; |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | for (int i = 0; i < node_count; i++) |
| 2200 | { |
| 2201 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2202 | |
| 2203 | // LSTM(uni) <= LSTM(uni) - Squeeze - Transpose |
| 2204 | if (node->op_type() == "LSTM" || node->op_type() == "GRU" || node->op_type() == "RNN") |
| 2205 | { |
| 2206 | if (node_reference[node->output(0)] != 1) |
| 2207 | continue; |
| 2208 | |
| 2209 | if (i + 1 >= node_count) |
| 2210 | continue; |
| 2211 | |
| 2212 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 2213 | |
| 2214 | if (node2->op_type() != "Squeeze") |
| 2215 | continue; |
| 2216 | |
| 2217 | if (node2->input(0) != node->output(0)) |
| 2218 | continue; |
| 2219 | |
| 2220 | std::string direction = get_node_attr_s(*node, "direction"); |
| 2221 | if (direction == "bidirectional") |
| 2222 | continue; |
| 2223 | |
| 2224 | // 1 |
| 2225 | std::vector<int> axes = get_node_attr_ai(*node2, "axes"); |
| 2226 | if (axes.size() != 1) |
| 2227 | continue; |
| 2228 | |
| 2229 | if (axes[0] != 1) |
| 2230 | continue; |
| 2231 | |
| 2232 | // reduce |
| 2233 | node2->set_op_type("noop_reducedncnn"); |
| 2234 | |
| 2235 | node_reference[node->output(0)] -= 1; |
| 2236 | |
| 2237 | blob_names.erase(node->output(0)); |
| 2238 | |
| 2239 | node->set_output(0, node2->output(0)); |
| 2240 | |
| 2241 | reduced_node_count += 1; |
| 2242 | i += 1; |
| 2243 | |
| 2244 | if (i + 1 < node_count) |
| 2245 | { |
| 2246 | if (node_reference[node2->output(0)] != 1) |
| 2247 | continue; |
| 2248 | |
| 2249 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 1); |
| 2250 | |
| 2251 | if (node3->op_type() != "Transpose") |
| 2252 | continue; |
| 2253 | |
| 2254 | if (node3->input(0) != node->output(0)) |
| 2255 | continue; |
| 2256 | |
| 2257 | // 1 0 2 |
| 2258 | std::vector<int> perm4 = get_node_attr_ai(*node3, "perm"); |
| 2259 | if (perm4.size() != 3) |
| 2260 | continue; |
| 2261 | |
| 2262 | if (perm4[0] != 1 || perm4[1] != 0 || perm4[2] != 2) |
| 2263 | continue; |
| 2264 | |
| 2265 | // reduce |
| 2266 | node3->set_op_type("noop_reducedncnn"); |
| 2267 | |
| 2268 | node_reference[node->output(0)] -= 1; |
| 2269 | |
| 2270 | blob_names.erase(node->output(0)); |
| 2271 | |
| 2272 | node->set_output(0, node3->output(0)); |
| 2273 | |
| 2274 | reduced_node_count += 1; |
| 2275 | i += 1; |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | for (int i = 0; i < node_count; i++) |
| 2281 | { |
| 2282 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2283 | |
| 2284 | // LSTM <= Transpose - LSTM |
| 2285 | if (node->op_type() == "Transpose") |
| 2286 | { |
| 2287 | if (node_reference[node->output(0)] != 1) |
| 2288 | continue; |
| 2289 | |
| 2290 | // 1 0 2 |
| 2291 | std::vector<int> perm = get_node_attr_ai(*node, "perm"); |
| 2292 | if (perm.size() != 3) |
| 2293 | continue; |
| 2294 | |
| 2295 | if (perm[0] != 1 || perm[1] != 0 || perm[2] != 2) |
| 2296 | continue; |
| 2297 | |
| 2298 | if (i + 1 >= node_count) |
| 2299 | continue; |
| 2300 | |
| 2301 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 2302 | |
| 2303 | if (node2->op_type() != "LSTM" && node->op_type() != "GRU" && node->op_type() != "RNN") |
| 2304 | continue; |
| 2305 | |
| 2306 | if (node2->input(0) != node->output(0)) |
| 2307 | continue; |
| 2308 | |
| 2309 | // reduce |
| 2310 | node->set_op_type("noop_reducedncnn"); |
| 2311 | |
| 2312 | node_reference[node->output(0)] -= 1; |
| 2313 | |
| 2314 | blob_names.erase(node->output(0)); |
| 2315 | |
| 2316 | node2->set_input(0, node->input(0)); |
| 2317 | |
| 2318 | reduced_node_count += 1; |
| 2319 | i += 1; |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | static void fuse_multiheadattention(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 2325 | { |
| 2326 | int node_count = mutable_graph->node_size(); |
| 2327 | for (int i = 0; i < node_count; i++) |
| 2328 | { |
| 2329 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2330 | |
| 2331 | // MultiHeadAttention <= MatMul(q) - Add |
| 2332 | // - MatMul(k) - Add |
| 2333 | // - MatMul(v) - Add |
| 2334 | // - Mul |
| 2335 | // - Reshape - Transpose |
| 2336 | // - Reshape - Reshape - Transpose - Transpose |
| 2337 | // - Gemm - Softmax - Gemm - Transpose - Reshape - MatMul - Add |
| 2338 | if (node->op_type() == "MatMul") |
| 2339 | { |
| 2340 | if (i + 19 >= node_count) |
| 2341 | continue; |
| 2342 | |
| 2343 | if (node_reference[node->output(0)] != 1) |
| 2344 | continue; |
| 2345 | |
| 2346 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 2347 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 2348 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 2349 | onnx::NodeProto* node5 = mutable_graph->mutable_node(i + 4); |
| 2350 | onnx::NodeProto* node6 = mutable_graph->mutable_node(i + 5); |
| 2351 | onnx::NodeProto* node7 = mutable_graph->mutable_node(i + 6); |
| 2352 | onnx::NodeProto* node8 = mutable_graph->mutable_node(i + 7); |
| 2353 | onnx::NodeProto* node9 = mutable_graph->mutable_node(i + 8); |
| 2354 | onnx::NodeProto* node10 = mutable_graph->mutable_node(i + 9); |
| 2355 | onnx::NodeProto* node11 = mutable_graph->mutable_node(i + 10); |
| 2356 | onnx::NodeProto* node12 = mutable_graph->mutable_node(i + 11); |
| 2357 | onnx::NodeProto* node13 = mutable_graph->mutable_node(i + 12); |
| 2358 | onnx::NodeProto* node14 = mutable_graph->mutable_node(i + 13); |
| 2359 | onnx::NodeProto* node15 = mutable_graph->mutable_node(i + 14); |
| 2360 | onnx::NodeProto* node16 = mutable_graph->mutable_node(i + 15); |
| 2361 | onnx::NodeProto* node17 = mutable_graph->mutable_node(i + 16); |
| 2362 | onnx::NodeProto* node18 = mutable_graph->mutable_node(i + 17); |
| 2363 | onnx::NodeProto* node19 = mutable_graph->mutable_node(i + 18); |
| 2364 | onnx::NodeProto* node20 = mutable_graph->mutable_node(i + 19); |
| 2365 | |
| 2366 | if (node2->op_type() != "Add" || node3->op_type() != "MatMul" || node4->op_type() != "Add" || node5->op_type() != "MatMul" || node6->op_type() != "Add" || node7->op_type() != "Mul" || node8->op_type() != "Reshape" || node9->op_type() != "Transpose" || node10->op_type() != "Reshape" || node11->op_type() != "Reshape" || node12->op_type() != "Transpose" || node13->op_type() != "Transpose" || node14->op_type() != "MatMul" || node15->op_type() != "Softmax" || node16->op_type() != "MatMul" || node17->op_type() != "Transpose" || node18->op_type() != "Reshape" || node19->op_type() != "MatMul" || node20->op_type() != "Add") |
| 2367 | continue; |
| 2368 | |
| 2369 | if (node_reference[node2->output(0)] != 1 || node_reference[node3->output(0)] != 1 || node_reference[node4->output(0)] != 1 || node_reference[node5->output(0)] != 1 || node_reference[node6->output(0)] != 1 || node_reference[node7->output(0)] != 1 || node_reference[node8->output(0)] != 1 || node_reference[node9->output(0)] != 1 || node_reference[node10->output(0)] != 1 || node_reference[node11->output(0)] != 1 || node_reference[node12->output(0)] != 1 || node_reference[node13->output(0)] != 1 || node_reference[node14->output(0)] != 1 || node_reference[node15->output(0)] != 1 || node_reference[node16->output(0)] != 1 || node_reference[node17->output(0)] != 1 || node_reference[node18->output(0)] != 1 || node_reference[node19->output(0)] != 1) |
| 2370 | continue; |
| 2371 | |
| 2372 | if (node2->input(0) != node->output(0) || node4->input(0) != node3->output(0) || node6->input(0) != node5->output(0) || node7->input(0) != node2->output(0) || node8->input(0) != node7->output(0) || node9->input(0) != node8->output(0) || node10->input(0) != node4->output(0) || node11->input(0) != node6->output(0) || node12->input(0) != node11->output(0) || node13->input(0) != node10->output(0) || node14->input(0) != node9->output(0) || node14->input(1) != node13->output(0) || node15->input(0) != node14->output(0) || node16->input(0) != node15->output(0) || node16->input(1) != node12->output(0) || node17->input(0) != node16->output(0) || node18->input(0) != node17->output(0) || node19->input(0) != node18->output(0) || node20->input(0) != node19->output(0)) |
| 2373 | continue; |
| 2374 | |
| 2375 | std::vector<float> q_B = get_node_attr_from_input_af(weights[node2->input(1)]); |
| 2376 | std::vector<float> k_B = get_node_attr_from_input_af(weights[node4->input(1)]); |
| 2377 | std::vector<float> v_B = get_node_attr_from_input_af(weights[node6->input(1)]); |
| 2378 | std::vector<float> o_B = get_node_attr_from_input_af(weights[node20->input(1)]); |
| 2379 | |
| 2380 | if (q_B.size() != k_B.size() || q_B.size() != v_B.size() || q_B.size() != o_B.size()) |
| 2381 | continue; |
| 2382 | |
| 2383 | int embed_dim = q_B.size(); |
| 2384 | |
| 2385 | // 1 0 2 |
| 2386 | std::vector<int> perm9 = get_node_attr_ai(*node9, "perm"); |
| 2387 | std::vector<int> perm12 = get_node_attr_ai(*node12, "perm"); |
| 2388 | if (perm9.size() != 3 || perm12.size() != 3) |
| 2389 | continue; |
| 2390 | |
| 2391 | if (perm9[0] != 1 || perm9[1] != 0 || perm9[2] != 2 || perm12[0] != 1 || perm12[1] != 0 || perm12[2] != 2) |
| 2392 | continue; |
| 2393 | |
| 2394 | // 1 2 0 |
| 2395 | std::vector<int> perm13 = get_node_attr_ai(*node13, "perm"); |
| 2396 | if (perm13.size() != 3) |
| 2397 | continue; |
| 2398 | |
| 2399 | if (perm13[0] != 1 || perm13[1] != 2 || perm13[2] != 0) |
| 2400 | continue; |
| 2401 | |
| 2402 | // 1 0 2 |
| 2403 | std::vector<int> perm17 = get_node_attr_ai(*node17, "perm"); |
| 2404 | if (perm17.size() != 3) |
| 2405 | continue; |
| 2406 | |
| 2407 | if (perm17[0] != 1 || perm17[1] != 0 || perm17[2] != 2) |
| 2408 | continue; |
| 2409 | |
| 2410 | int softmax_axis = get_node_attr_i(*node15, "axis"); |
| 2411 | if (softmax_axis != 2) |
| 2412 | continue; |
| 2413 | |
| 2414 | // 1/-1, seqlen * num_heads, embed_dim / num_heads |
| 2415 | std::vector<int> shape8; |
| 2416 | std::vector<int> shape10; |
| 2417 | std::vector<int> shape11; |
| 2418 | if (node8->input_size() == 1) |
| 2419 | { |
| 2420 | shape8 = get_node_attr_ai(*node8, "shape"); |
| 2421 | } |
| 2422 | else |
| 2423 | { |
| 2424 | // skip weight reshape |
| 2425 | if (weights.find(node8->input(1)) == weights.end()) |
| 2426 | continue; |
| 2427 | |
| 2428 | shape8 = get_node_attr_from_input_ai(weights[node8->input(1)]); |
| 2429 | } |
| 2430 | if (node10->input_size() == 1) |
| 2431 | { |
| 2432 | shape10 = get_node_attr_ai(*node10, "shape"); |
| 2433 | } |
| 2434 | else |
| 2435 | { |
| 2436 | // skip weight reshape |
| 2437 | if (weights.find(node10->input(1)) == weights.end()) |
| 2438 | continue; |
| 2439 | |
| 2440 | shape10 = get_node_attr_from_input_ai(weights[node10->input(1)]); |
| 2441 | } |
| 2442 | if (node11->input_size() == 1) |
| 2443 | { |
| 2444 | shape11 = get_node_attr_ai(*node11, "shape"); |
| 2445 | } |
| 2446 | else |
| 2447 | { |
| 2448 | // skip weight reshape |
| 2449 | if (weights.find(node11->input(1)) == weights.end()) |
| 2450 | continue; |
| 2451 | |
| 2452 | shape11 = get_node_attr_from_input_ai(weights[node11->input(1)]); |
| 2453 | } |
| 2454 | |
| 2455 | if (shape8.size() != 3 || shape10.size() != 3 || shape11.size() != 3) |
| 2456 | continue; |
| 2457 | |
| 2458 | if (shape8[1] != shape10[1] || shape8[1] != shape11[1] || shape8[2] != shape10[2] || shape8[2] != shape11[2]) |
| 2459 | continue; |
| 2460 | |
| 2461 | int num_heads = embed_dim / shape8[2]; |
| 2462 | |
| 2463 | // 1, seqlen, embed_dim |
| 2464 | std::vector<int> shape18; |
| 2465 | if (node18->input_size() == 1) |
| 2466 | { |
| 2467 | shape18 = get_node_attr_ai(*node18, "shape"); |
| 2468 | } |
| 2469 | else |
| 2470 | { |
| 2471 | // skip weight reshape |
| 2472 | if (weights.find(node18->input(1)) == weights.end()) |
| 2473 | continue; |
| 2474 | |
| 2475 | shape18 = get_node_attr_from_input_ai(weights[node18->input(1)]); |
| 2476 | } |
| 2477 | |
| 2478 | if (shape18.size() != 3) |
| 2479 | continue; |
| 2480 | |
| 2481 | if (shape18[2] != embed_dim || shape18[1] * num_heads != shape8[1]) |
| 2482 | continue; |
| 2483 | |
| 2484 | // reduce |
| 2485 | node->set_op_type("noop_reducedncnn"); |
| 2486 | node2->set_op_type("noop_reducedncnn"); |
| 2487 | node3->set_op_type("noop_reducedncnn"); |
| 2488 | node4->set_op_type("noop_reducedncnn"); |
| 2489 | node5->set_op_type("noop_reducedncnn"); |
| 2490 | node6->set_op_type("noop_reducedncnn"); |
| 2491 | node7->set_op_type("noop_reducedncnn"); |
| 2492 | node8->set_op_type("noop_reducedncnn"); |
| 2493 | node9->set_op_type("noop_reducedncnn"); |
| 2494 | node10->set_op_type("noop_reducedncnn"); |
| 2495 | node11->set_op_type("noop_reducedncnn"); |
| 2496 | node12->set_op_type("noop_reducedncnn"); |
| 2497 | node13->set_op_type("noop_reducedncnn"); |
| 2498 | node14->set_op_type("noop_reducedncnn"); |
| 2499 | node15->set_op_type("noop_reducedncnn"); |
| 2500 | node16->set_op_type("noop_reducedncnn"); |
| 2501 | node17->set_op_type("noop_reducedncnn"); |
| 2502 | node18->set_op_type("noop_reducedncnn"); |
| 2503 | node19->set_op_type("noop_reducedncnn"); |
| 2504 | |
| 2505 | node_reference[node2->input(0)] -= 1; |
| 2506 | node_reference[node4->input(0)] -= 1; |
| 2507 | node_reference[node6->input(0)] -= 1; |
| 2508 | node_reference[node7->input(0)] -= 1; |
| 2509 | node_reference[node7->input(1)] -= 1; |
| 2510 | node_reference[node8->input(0)] -= 1; |
| 2511 | if (node8->input_size() == 2) |
| 2512 | { |
| 2513 | node_reference[node8->input(1)] -= 1; |
| 2514 | } |
| 2515 | node_reference[node9->input(0)] -= 1; |
| 2516 | node_reference[node10->input(0)] -= 1; |
| 2517 | if (node10->input_size() == 2) |
| 2518 | { |
| 2519 | node_reference[node10->input(1)] -= 1; |
| 2520 | } |
| 2521 | node_reference[node11->input(0)] -= 1; |
| 2522 | if (node11->input_size() == 2) |
| 2523 | { |
| 2524 | node_reference[node11->input(1)] -= 1; |
| 2525 | } |
| 2526 | node_reference[node12->input(0)] -= 1; |
| 2527 | node_reference[node13->input(0)] -= 1; |
| 2528 | node_reference[node14->input(0)] -= 1; |
| 2529 | node_reference[node14->input(1)] -= 1; |
| 2530 | node_reference[node15->input(0)] -= 1; |
| 2531 | node_reference[node16->input(0)] -= 1; |
| 2532 | node_reference[node16->input(1)] -= 1; |
| 2533 | node_reference[node17->input(0)] -= 1; |
| 2534 | node_reference[node18->input(0)] -= 1; |
| 2535 | if (node18->input_size() == 2) |
| 2536 | { |
| 2537 | node_reference[node18->input(1)] -= 1; |
| 2538 | } |
| 2539 | node_reference[node19->input(0)] -= 1; |
| 2540 | node_reference[node20->input(0)] -= 1; |
| 2541 | |
| 2542 | blob_names.erase(node->output(0)); |
| 2543 | blob_names.erase(node2->output(0)); |
| 2544 | blob_names.erase(node3->output(0)); |
| 2545 | blob_names.erase(node4->output(0)); |
| 2546 | blob_names.erase(node5->output(0)); |
| 2547 | blob_names.erase(node6->output(0)); |
| 2548 | blob_names.erase(node7->output(0)); |
| 2549 | blob_names.erase(node8->output(0)); |
| 2550 | blob_names.erase(node9->output(0)); |
| 2551 | blob_names.erase(node10->output(0)); |
| 2552 | blob_names.erase(node11->output(0)); |
| 2553 | blob_names.erase(node12->output(0)); |
| 2554 | blob_names.erase(node13->output(0)); |
| 2555 | blob_names.erase(node14->output(0)); |
| 2556 | blob_names.erase(node15->output(0)); |
| 2557 | blob_names.erase(node16->output(0)); |
| 2558 | blob_names.erase(node17->output(0)); |
| 2559 | blob_names.erase(node18->output(0)); |
| 2560 | blob_names.erase(node19->output(0)); |
| 2561 | |
| 2562 | std::string qw = node->input(1); |
| 2563 | std::string qb = node2->input(1); |
| 2564 | std::string kw = node3->input(1); |
| 2565 | std::string kb = node4->input(1); |
| 2566 | std::string vw = node5->input(1); |
| 2567 | std::string vb = node6->input(1); |
| 2568 | std::string ow = node19->input(1); |
| 2569 | std::string ob = node20->input(1); |
| 2570 | |
| 2571 | node20->set_op_type("MultiHeadAttention"); |
| 2572 | node20->clear_input(); |
| 2573 | node20->add_input(node->input(0)); |
| 2574 | node20->add_input(node3->input(0)); |
| 2575 | node20->add_input(node5->input(0)); |
| 2576 | // q |
| 2577 | node20->add_input(qw); |
| 2578 | node20->add_input(qb); |
| 2579 | // k |
| 2580 | node20->add_input(kw); |
| 2581 | node20->add_input(kb); |
| 2582 | // v |
| 2583 | node20->add_input(vw); |
| 2584 | node20->add_input(vb); |
| 2585 | // out linear |
| 2586 | node20->add_input(ow); |
| 2587 | node20->add_input(ob); |
| 2588 | |
| 2589 | onnx::AttributeProto* attr_embed_dim = node20->add_attribute(); |
| 2590 | attr_embed_dim->set_name("embed_dim"); |
| 2591 | attr_embed_dim->set_i(embed_dim); |
| 2592 | |
| 2593 | onnx::AttributeProto* attr_num_heads = node20->add_attribute(); |
| 2594 | attr_num_heads->set_name("num_heads"); |
| 2595 | attr_num_heads->set_i(num_heads); |
| 2596 | |
| 2597 | reduced_node_count += 19; |
| 2598 | i += 19; |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | for (int i = 0; i < node_count; i++) |
| 2603 | { |
| 2604 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2605 | |
| 2606 | // MultiHeadAttention <= MatMul(qkv) - Add - Split |
| 2607 | // - Mul |
| 2608 | // - Reshape - Transpose |
| 2609 | // - Reshape - Reshape - Transpose - Transpose |
| 2610 | // - Gemm - Softmax - Gemm - Transpose - Reshape - MatMul - Add |
| 2611 | if (node->op_type() == "MatMul") |
| 2612 | { |
| 2613 | if (i + 16 >= node_count) |
| 2614 | continue; |
| 2615 | |
| 2616 | if (node_reference[node->output(0)] != 1) |
| 2617 | continue; |
| 2618 | |
| 2619 | onnx::NodeProto* node2 = mutable_graph->mutable_node(i + 1); |
| 2620 | onnx::NodeProto* node3 = mutable_graph->mutable_node(i + 2); |
| 2621 | onnx::NodeProto* node4 = mutable_graph->mutable_node(i + 3); |
| 2622 | onnx::NodeProto* node5 = mutable_graph->mutable_node(i + 4); |
| 2623 | onnx::NodeProto* node6 = mutable_graph->mutable_node(i + 5); |
| 2624 | onnx::NodeProto* node7 = mutable_graph->mutable_node(i + 6); |
| 2625 | onnx::NodeProto* node8 = mutable_graph->mutable_node(i + 7); |
| 2626 | onnx::NodeProto* node9 = mutable_graph->mutable_node(i + 8); |
| 2627 | onnx::NodeProto* node10 = mutable_graph->mutable_node(i + 9); |
| 2628 | onnx::NodeProto* node11 = mutable_graph->mutable_node(i + 10); |
| 2629 | onnx::NodeProto* node12 = mutable_graph->mutable_node(i + 11); |
| 2630 | onnx::NodeProto* node13 = mutable_graph->mutable_node(i + 12); |
| 2631 | onnx::NodeProto* node14 = mutable_graph->mutable_node(i + 13); |
| 2632 | onnx::NodeProto* node15 = mutable_graph->mutable_node(i + 14); |
| 2633 | onnx::NodeProto* node16 = mutable_graph->mutable_node(i + 15); |
| 2634 | onnx::NodeProto* node17 = mutable_graph->mutable_node(i + 16); |
| 2635 | |
| 2636 | if (node2->op_type() != "Add" || node3->op_type() != "Split" || node4->op_type() != "Mul" || node5->op_type() != "Reshape" || node6->op_type() != "Transpose" || node7->op_type() != "Reshape" || node8->op_type() != "Reshape" || node9->op_type() != "Transpose" || node10->op_type() != "Transpose" || node11->op_type() != "MatMul" || node12->op_type() != "Softmax" || node13->op_type() != "MatMul" || node14->op_type() != "Transpose" || node15->op_type() != "Reshape" || node16->op_type() != "MatMul" || node17->op_type() != "Add") |
| 2637 | continue; |
| 2638 | |
| 2639 | if (node_reference[node2->output(0)] != 1 || node_reference[node3->output(0)] != 1 || node_reference[node3->output(1)] != 1 || node_reference[node3->output(2)] != 1 || node_reference[node4->output(0)] != 1 || node_reference[node5->output(0)] != 1 || node_reference[node6->output(0)] != 1 || node_reference[node7->output(0)] != 1 || node_reference[node8->output(0)] != 1 || node_reference[node9->output(0)] != 1 || node_reference[node10->output(0)] != 1 || node_reference[node11->output(0)] != 1 || node_reference[node12->output(0)] != 1 || node_reference[node13->output(0)] != 1 || node_reference[node14->output(0)] != 1 || node_reference[node15->output(0)] != 1 || node_reference[node16->output(0)] != 1) |
| 2640 | continue; |
| 2641 | |
| 2642 | if (node2->input(0) != node->output(0) || node3->input(0) != node2->output(0) || node4->input(0) != node3->output(0) || node5->input(0) != node4->output(0) || node6->input(0) != node5->output(0) || node7->input(0) != node3->output(1) || node8->input(0) != node3->output(2) || node9->input(0) != node8->output(0) || node10->input(0) != node7->output(0) || node11->input(0) != node6->output(0) || node11->input(1) != node10->output(0) || node12->input(0) != node11->output(0) || node13->input(0) != node12->output(0) || node13->input(1) != node9->output(0) || node14->input(0) != node13->output(0) || node15->input(0) != node14->output(0) || node16->input(0) != node15->output(0) || node17->input(0) != node16->output(0)) |
| 2643 | continue; |
| 2644 | |
| 2645 | std::vector<float> qkv_B = get_node_attr_from_input_af(weights[node2->input(1)]); |
| 2646 | std::vector<float> o_B = get_node_attr_from_input_af(weights[node17->input(1)]); |
| 2647 | |
| 2648 | if (qkv_B.size() != o_B.size() * 3) |
| 2649 | continue; |
| 2650 | |
| 2651 | int embed_dim = o_B.size(); |
| 2652 | |
| 2653 | // 1 0 2 |
| 2654 | std::vector<int> perm6 = get_node_attr_ai(*node6, "perm"); |
| 2655 | std::vector<int> perm9 = get_node_attr_ai(*node9, "perm"); |
| 2656 | if (perm6.size() != 3 || perm9.size() != 3) |
| 2657 | continue; |
| 2658 | |
| 2659 | if (perm6[0] != 1 || perm6[1] != 0 || perm6[2] != 2 || perm9[0] != 1 || perm9[1] != 0 || perm9[2] != 2) |
| 2660 | continue; |
| 2661 | |
| 2662 | // 1 2 0 |
| 2663 | std::vector<int> perm10 = get_node_attr_ai(*node10, "perm"); |
| 2664 | if (perm10.size() != 3) |
| 2665 | continue; |
| 2666 | |
| 2667 | if (perm10[0] != 1 || perm10[1] != 2 || perm10[2] != 0) |
| 2668 | continue; |
| 2669 | |
| 2670 | // 1 0 2 |
| 2671 | std::vector<int> perm14 = get_node_attr_ai(*node14, "perm"); |
| 2672 | if (perm14.size() != 3) |
| 2673 | continue; |
| 2674 | |
| 2675 | if (perm14[0] != 1 || perm14[1] != 0 || perm14[2] != 2) |
| 2676 | continue; |
| 2677 | |
| 2678 | int softmax_axis = get_node_attr_i(*node12, "axis"); |
| 2679 | if (softmax_axis != 2) |
| 2680 | continue; |
| 2681 | |
| 2682 | // 1/-1, seqlen * num_heads, embed_dim / num_heads |
| 2683 | std::vector<int> shape5; |
| 2684 | std::vector<int> shape7; |
| 2685 | std::vector<int> shape8; |
| 2686 | if (node5->input_size() == 1) |
| 2687 | { |
| 2688 | shape5 = get_node_attr_ai(*node5, "shape"); |
| 2689 | } |
| 2690 | else |
| 2691 | { |
| 2692 | // skip weight reshape |
| 2693 | if (weights.find(node5->input(1)) == weights.end()) |
| 2694 | continue; |
| 2695 | |
| 2696 | shape5 = get_node_attr_from_input_ai(weights[node5->input(1)]); |
| 2697 | } |
| 2698 | if (node7->input_size() == 1) |
| 2699 | { |
| 2700 | shape7 = get_node_attr_ai(*node7, "shape"); |
| 2701 | } |
| 2702 | else |
| 2703 | { |
| 2704 | // skip weight reshape |
| 2705 | if (weights.find(node7->input(1)) == weights.end()) |
| 2706 | continue; |
| 2707 | |
| 2708 | shape7 = get_node_attr_from_input_ai(weights[node7->input(1)]); |
| 2709 | } |
| 2710 | if (node8->input_size() == 1) |
| 2711 | { |
| 2712 | shape8 = get_node_attr_ai(*node8, "shape"); |
| 2713 | } |
| 2714 | else |
| 2715 | { |
| 2716 | // skip weight reshape |
| 2717 | if (weights.find(node8->input(1)) == weights.end()) |
| 2718 | continue; |
| 2719 | |
| 2720 | shape8 = get_node_attr_from_input_ai(weights[node8->input(1)]); |
| 2721 | } |
| 2722 | |
| 2723 | if (shape5.size() != 3 || shape7.size() != 3 || shape8.size() != 3) |
| 2724 | continue; |
| 2725 | |
| 2726 | if (shape5[1] != shape7[1] || shape5[1] != shape8[1] || shape5[2] != shape7[2] || shape5[2] != shape8[2]) |
| 2727 | continue; |
| 2728 | |
| 2729 | int num_heads = embed_dim / shape5[2]; |
| 2730 | |
| 2731 | // 1, seqlen, embed_dim |
| 2732 | std::vector<int> shape15; |
| 2733 | if (node15->input_size() == 1) |
| 2734 | { |
| 2735 | shape15 = get_node_attr_ai(*node15, "shape"); |
| 2736 | } |
| 2737 | else |
| 2738 | { |
| 2739 | // skip weight reshape |
| 2740 | if (weights.find(node15->input(1)) == weights.end()) |
| 2741 | continue; |
| 2742 | |
| 2743 | shape15 = get_node_attr_from_input_ai(weights[node15->input(1)]); |
| 2744 | } |
| 2745 | |
| 2746 | if (shape15.size() != 3) |
| 2747 | continue; |
| 2748 | |
| 2749 | if (shape15[2] != embed_dim || shape15[1] * num_heads != shape8[1]) |
| 2750 | continue; |
| 2751 | |
| 2752 | // reduce |
| 2753 | node->set_op_type("noop_reducedncnn"); |
| 2754 | node2->set_op_type("noop_reducedncnn"); |
| 2755 | node3->set_op_type("noop_reducedncnn"); |
| 2756 | node4->set_op_type("noop_reducedncnn"); |
| 2757 | node5->set_op_type("noop_reducedncnn"); |
| 2758 | node6->set_op_type("noop_reducedncnn"); |
| 2759 | node7->set_op_type("noop_reducedncnn"); |
| 2760 | node8->set_op_type("noop_reducedncnn"); |
| 2761 | node9->set_op_type("noop_reducedncnn"); |
| 2762 | node10->set_op_type("noop_reducedncnn"); |
| 2763 | node11->set_op_type("noop_reducedncnn"); |
| 2764 | node12->set_op_type("noop_reducedncnn"); |
| 2765 | node13->set_op_type("noop_reducedncnn"); |
| 2766 | node14->set_op_type("noop_reducedncnn"); |
| 2767 | node15->set_op_type("noop_reducedncnn"); |
| 2768 | node16->set_op_type("noop_reducedncnn"); |
| 2769 | |
| 2770 | node_reference[node2->input(0)] -= 1; |
| 2771 | node_reference[node3->input(0)] -= 1; |
| 2772 | node_reference[node4->input(0)] -= 1; |
| 2773 | node_reference[node4->input(1)] -= 1; |
| 2774 | node_reference[node5->input(0)] -= 1; |
| 2775 | if (node5->input_size() == 2) |
| 2776 | { |
| 2777 | node_reference[node5->input(1)] -= 1; |
| 2778 | } |
| 2779 | node_reference[node6->input(0)] -= 1; |
| 2780 | node_reference[node7->input(0)] -= 1; |
| 2781 | if (node7->input_size() == 2) |
| 2782 | { |
| 2783 | node_reference[node7->input(1)] -= 1; |
| 2784 | } |
| 2785 | node_reference[node8->input(0)] -= 1; |
| 2786 | if (node8->input_size() == 2) |
| 2787 | { |
| 2788 | node_reference[node8->input(1)] -= 1; |
| 2789 | } |
| 2790 | node_reference[node9->input(0)] -= 1; |
| 2791 | node_reference[node10->input(0)] -= 1; |
| 2792 | node_reference[node11->input(0)] -= 1; |
| 2793 | node_reference[node11->input(1)] -= 1; |
| 2794 | node_reference[node12->input(0)] -= 1; |
| 2795 | node_reference[node13->input(0)] -= 1; |
| 2796 | node_reference[node13->input(1)] -= 1; |
| 2797 | node_reference[node14->input(0)] -= 1; |
| 2798 | node_reference[node15->input(0)] -= 1; |
| 2799 | if (node15->input_size() == 2) |
| 2800 | { |
| 2801 | node_reference[node15->input(1)] -= 1; |
| 2802 | } |
| 2803 | node_reference[node16->input(0)] -= 1; |
| 2804 | node_reference[node17->input(0)] -= 1; |
| 2805 | |
| 2806 | blob_names.erase(node->output(0)); |
| 2807 | blob_names.erase(node2->output(0)); |
| 2808 | blob_names.erase(node3->output(0)); |
| 2809 | blob_names.erase(node3->output(1)); |
| 2810 | blob_names.erase(node3->output(2)); |
| 2811 | blob_names.erase(node4->output(0)); |
| 2812 | blob_names.erase(node5->output(0)); |
| 2813 | blob_names.erase(node6->output(0)); |
| 2814 | blob_names.erase(node7->output(0)); |
| 2815 | blob_names.erase(node8->output(0)); |
| 2816 | blob_names.erase(node9->output(0)); |
| 2817 | blob_names.erase(node10->output(0)); |
| 2818 | blob_names.erase(node11->output(0)); |
| 2819 | blob_names.erase(node12->output(0)); |
| 2820 | blob_names.erase(node13->output(0)); |
| 2821 | blob_names.erase(node14->output(0)); |
| 2822 | blob_names.erase(node15->output(0)); |
| 2823 | blob_names.erase(node16->output(0)); |
| 2824 | |
| 2825 | std::string qkvw = node->input(1); |
| 2826 | std::string qkvb = node2->input(1); |
| 2827 | std::string ow = node16->input(1); |
| 2828 | std::string ob = node17->input(1); |
| 2829 | |
| 2830 | node17->set_op_type("MultiHeadAttention"); |
| 2831 | node17->clear_input(); |
| 2832 | node17->add_input(node->input(0)); |
| 2833 | // qkv |
| 2834 | node17->add_input(qkvw); |
| 2835 | node17->add_input(qkvb); |
| 2836 | // out linear |
| 2837 | node17->add_input(ow); |
| 2838 | node17->add_input(ob); |
| 2839 | |
| 2840 | onnx::AttributeProto* attr_embed_dim = node17->add_attribute(); |
| 2841 | attr_embed_dim->set_name("embed_dim"); |
| 2842 | attr_embed_dim->set_i(embed_dim); |
| 2843 | |
| 2844 | onnx::AttributeProto* attr_num_heads = node17->add_attribute(); |
| 2845 | attr_num_heads->set_name("num_heads"); |
| 2846 | attr_num_heads->set_i(num_heads); |
| 2847 | |
| 2848 | reduced_node_count += 16; |
| 2849 | i += 16; |
| 2850 | } |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | static void fuse_binaryop_with_scalar(onnx::GraphProto* mutable_graph, std::map<std::string, onnx::TensorProto>& weights, std::map<std::string, int>& node_reference, std::set<std::string>& blob_names, int& reduced_node_count) |
| 2855 | { |
| 2856 | int node_count = mutable_graph->node_size(); |
| 2857 | for (int i = 0; i < node_count; i++) |
| 2858 | { |
| 2859 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2860 | |
| 2861 | // Add/Sub/Mul/Div/Min/Max/Pow(a, x) |
| 2862 | if (node->op_type() == "Add" || node->op_type() == "Sub" || node->op_type() == "Mul" || node->op_type() == "Div" || node->op_type() == "Max" || node->op_type() == "Min" || node->op_type() == "Pow") |
| 2863 | { |
| 2864 | if (weights.find(node->input(0)) == weights.end()) |
| 2865 | continue; |
| 2866 | |
| 2867 | const onnx::TensorProto& scalar_b = weights[node->input(0)]; |
| 2868 | if (scalar_b.dims_size() != 0 || get_tensor_proto_data_size(scalar_b) != 1) |
| 2869 | continue; |
| 2870 | |
| 2871 | if (node->op_type() == "Sub") |
| 2872 | { |
| 2873 | node->set_op_type("RSub"); |
| 2874 | } |
| 2875 | else if (node->op_type() == "Div") |
| 2876 | { |
| 2877 | node->set_op_type("RDiv"); |
| 2878 | } |
| 2879 | |
| 2880 | float b = get_node_attr_from_input_f(scalar_b); |
| 2881 | |
| 2882 | node_reference[node->input(0)] -= 1; |
| 2883 | |
| 2884 | std::string input = node->input(1); |
| 2885 | |
| 2886 | node->clear_input(); |
| 2887 | node->add_input(input); |
| 2888 | |
| 2889 | onnx::AttributeProto* attr_with_scalar = node->add_attribute(); |
| 2890 | attr_with_scalar->set_name("with_scalar"); |
| 2891 | attr_with_scalar->set_i(1); |
| 2892 | |
| 2893 | onnx::AttributeProto* attr_b = node->add_attribute(); |
| 2894 | attr_b->set_name("b"); |
| 2895 | attr_b->set_f(b); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | for (int i = 0; i < node_count; i++) |
| 2900 | { |
| 2901 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 2902 | |
| 2903 | // Add/Sub/Mul/Div/Min/Max/Pow(x, b) |
| 2904 | if (node->op_type() == "Add" || node->op_type() == "Sub" || node->op_type() == "Mul" || node->op_type() == "Div" || node->op_type() == "Max" || node->op_type() == "Min" || node->op_type() == "Pow") |
| 2905 | { |
| 2906 | if (weights.find(node->input(1)) == weights.end()) |
| 2907 | continue; |
| 2908 | |
| 2909 | const onnx::TensorProto& scalar_b = weights[node->input(1)]; |
| 2910 | if (scalar_b.dims_size() != 0 || get_tensor_proto_data_size(scalar_b) != 1) |
| 2911 | continue; |
| 2912 | |
| 2913 | float b = get_node_attr_from_input_f(scalar_b); |
| 2914 | |
| 2915 | node_reference[node->input(1)] -= 1; |
| 2916 | |
| 2917 | std::string input = node->input(0); |
| 2918 | |
| 2919 | node->clear_input(); |
| 2920 | node->add_input(input); |
| 2921 | |
| 2922 | onnx::AttributeProto* attr_with_scalar = node->add_attribute(); |
| 2923 | attr_with_scalar->set_name("with_scalar"); |
| 2924 | attr_with_scalar->set_i(1); |
| 2925 | |
| 2926 | onnx::AttributeProto* attr_b = node->add_attribute(); |
| 2927 | attr_b->set_name("b"); |
| 2928 | attr_b->set_f(b); |
| 2929 | } |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | // truncate layer/blob names when they exceed 255, which is the upper length limit when parsing param in src/net.cpp |
| 2934 | static std::string trunc_name(std::string name) |
| 2935 | { |
| 2936 | static int trunc_idx = 0; |
| 2937 | static std::map<std::string, std::string> name_trunc_map; |
| 2938 | |
| 2939 | const int max_len = 255; |
| 2940 | if (name.size() <= max_len) |
| 2941 | { |
| 2942 | return name; |
| 2943 | } |
| 2944 | if (name_trunc_map.count(name)) |
| 2945 | { |
| 2946 | return name_trunc_map[name]; |
| 2947 | } |
| 2948 | |
| 2949 | std::string concat_name = name + "_t" + std::to_string(trunc_idx); |
| 2950 | std::string trunc_name = concat_name.substr(concat_name.size() - max_len); |
| 2951 | trunc_idx += 1; |
| 2952 | name_trunc_map[name] = trunc_name; |
| 2953 | |
| 2954 | return trunc_name; |
| 2955 | } |
| 2956 | |
| 2957 | int main(int argc, char** argv) |
| 2958 | { |
| 2959 | fprintf(stderr, "onnx2ncnn may not fully meet your needs. For more accurate and elegant\n\ |
| 2960 | conversion results, please use PNNX. PyTorch Neural Network eXchange (PNNX) is\n\ |
| 2961 | an open standard for PyTorch model interoperability. PNNX provides an open model\n\ |
| 2962 | format for PyTorch. It defines computation graph as well as high level operators\n\ |
| 2963 | strictly matches PyTorch. You can obtain pnnx through the following ways:\n\ |
| 2964 | 1. Install via python\n\ |
| 2965 | pip3 install pnnx\n\ |
| 2966 | 2. Get the executable from https://github.com/pnnx/pnnx\n\ |
| 2967 | For more information, please refer to https://github.com/pnnx/pnnx\n"); |
| 2968 | if (!(argc == 2 || argc == 4)) |
| 2969 | { |
| 2970 | fprintf(stderr, "Usage: %s [onnxpb] [ncnnparam] [ncnnbin]\n", argv[0]); |
| 2971 | return -1; |
| 2972 | } |
| 2973 | |
| 2974 | const char* onnxpb = argv[1]; |
| 2975 | const char* ncnn_prototxt = argc == 4 ? argv[2] : "ncnn.param"; |
| 2976 | const char* ncnn_modelbin = argc == 4 ? argv[3] : "ncnn.bin"; |
| 2977 | |
| 2978 | onnx::ModelProto model; |
| 2979 | |
| 2980 | // load |
| 2981 | bool s1 = read_proto_from_binary(onnxpb, &model); |
| 2982 | if (!s1) |
| 2983 | { |
| 2984 | fprintf(stderr, "read_proto_from_binary failed\n"); |
| 2985 | return -1; |
| 2986 | } |
| 2987 | |
| 2988 | FILE* pp = fopen(ncnn_prototxt, "wb"); |
| 2989 | FILE* bp = fopen(ncnn_modelbin, "wb"); |
| 2990 | |
| 2991 | // magic |
| 2992 | fprintf(pp, "7767517\n"); |
| 2993 | |
| 2994 | const onnx::GraphProto& graph = model.graph(); |
| 2995 | onnx::GraphProto* mutable_graph = model.mutable_graph(); |
| 2996 | |
| 2997 | int node_count = graph.node_size(); |
| 2998 | |
| 2999 | // node reference |
| 3000 | std::map<std::string, int> node_reference; |
| 3001 | |
| 3002 | // weight node and weight reshape node |
| 3003 | std::map<std::string, onnx::TensorProto> weights; |
| 3004 | |
| 3005 | for (int j = 0; j < graph.initializer_size(); j++) |
| 3006 | { |
| 3007 | const onnx::TensorProto& initializer = graph.initializer(j); |
| 3008 | |
| 3009 | // fprintf(stderr, "weight = %s %d\n", initializer.name().c_str(), initializer.data_type()); |
| 3010 | |
| 3011 | weights[initializer.name()] = initializer; |
| 3012 | } |
| 3013 | |
| 3014 | // topological sort |
| 3015 | { |
| 3016 | // name -> producer node index |
| 3017 | std::set<std::string> producers; |
| 3018 | for (int j = 0; j < graph.input_size(); j++) |
| 3019 | { |
| 3020 | const std::string& input_name = graph.input(j).name(); |
| 3021 | producers.insert(input_name); |
| 3022 | } |
| 3023 | |
| 3024 | for (int i = 0; i < node_count;) |
| 3025 | { |
| 3026 | onnx::NodeProto* node = mutable_graph->mutable_node(i); |
| 3027 | |
| 3028 | bool swapnode = false; |
| 3029 | std::string missing_input_name; |
| 3030 | for (int j = 0; j < (int)node->input_size(); j++) |
| 3031 | { |
| 3032 | const std::string& input_name = node->input(j); |
| 3033 | if (input_name.empty()) |
| 3034 | continue; |
| 3035 | |
| 3036 | if (producers.find(input_name) == producers.end() && weights.find(input_name) == weights.end()) |
| 3037 | { |
| 3038 | swapnode = true; |
| 3039 | missing_input_name = input_name; |
| 3040 | break; |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | if (!swapnode) |
| 3045 | { |
| 3046 | for (int j = 0; j < (int)node->output_size(); j++) |
| 3047 | { |
| 3048 | const std::string& output_name = node->output(j); |
| 3049 | if (output_name.empty()) |
| 3050 | continue; |
| 3051 | |
| 3052 | producers.insert(output_name); |
| 3053 | } |
| 3054 | |
| 3055 | i++; |
| 3056 | continue; |
| 3057 | } |
| 3058 | |
| 3059 | // find node that produce missing_input_name |
| 3060 | int q = i + 1; |
| 3061 | for (; q < node_count; q++) |
| 3062 | { |
| 3063 | onnx::NodeProto* nodeq = mutable_graph->mutable_node(q); |
| 3064 | bool found = false; |
| 3065 | for (int j = 0; j < (int)nodeq->output_size(); j++) |
| 3066 | { |
| 3067 | const std::string& output_name = nodeq->output(j); |
| 3068 | if (output_name == missing_input_name) |
| 3069 | { |
| 3070 | found = true; |
| 3071 | break; |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | if (found) |
| 3076 | break; |
| 3077 | } |
| 3078 | |
| 3079 | if (q == node_count) |
| 3080 | { |
| 3081 | fprintf(stderr, "cannot find node produces %s but node %d requires it\n", missing_input_name.c_str(), i); |
| 3082 | return -1; |
| 3083 | } |
| 3084 | |
| 3085 | // fprintf(stderr, "swap %d %d\n", i, q); |
| 3086 | // swap this node with q |
| 3087 | onnx::NodeProto* nodeq = mutable_graph->mutable_node(q); |
| 3088 | onnx::NodeProto tmp = *node; |
| 3089 | *node = *nodeq; |
| 3090 | *nodeq = tmp; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | // global definition line |
| 3095 | // [layer count] [blob count] |
| 3096 | std::set<std::string> blob_names; |
| 3097 | for (int i = 0; i < node_count; i++) |
| 3098 | { |
| 3099 | const onnx::NodeProto& node = graph.node(i); |
| 3100 | |
| 3101 | const std::string& op = node.op_type(); |
| 3102 | |
| 3103 | std::string name = node.name(); |
| 3104 | if (name.empty()) |
| 3105 | { |
| 3106 | name = node.output(0); |
| 3107 | } |
| 3108 | |
| 3109 | if (op == "Constant") |
| 3110 | { |
| 3111 | onnx::TensorProto tensor = get_node_attr_tensor(node, "value"); |
| 3112 | weights[node.output(0)] = tensor; |
| 3113 | } |
| 3114 | |
| 3115 | for (int j = 0; j < (int)node.input_size(); j++) |
| 3116 | { |
| 3117 | const std::string& input_name = node.input(j); |
| 3118 | |
| 3119 | blob_names.insert(input_name); |
| 3120 | |
| 3121 | if (node_reference.find(input_name) == node_reference.end()) |
| 3122 | { |
| 3123 | node_reference[input_name] = 1; |
| 3124 | } |
| 3125 | else |
| 3126 | { |
| 3127 | node_reference[input_name] = node_reference[input_name] + 1; |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | if (op == "Dropout") |
| 3132 | { |
| 3133 | const std::string& output_name = node.output(0); |
| 3134 | blob_names.insert(output_name); |
| 3135 | node_reference[output_name] = 0; |
| 3136 | continue; |
| 3137 | } |
| 3138 | |
| 3139 | for (int j = 0; j < (int)node.output_size(); j++) |
| 3140 | { |
| 3141 | const std::string& output_name = node.output(j); |
| 3142 | |
| 3143 | blob_names.insert(output_name); |
| 3144 | |
| 3145 | node_reference[output_name] = 0; |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | // include Input node |
| 3150 | int input_node_count = 0; |
| 3151 | for (int j = 0; j < graph.input_size(); j++) |
| 3152 | { |
| 3153 | const std::string& input_name = graph.input(j).name(); |
| 3154 | |
| 3155 | // check weight |
| 3156 | if (weights.find(input_name) != weights.end()) |
| 3157 | continue; |
| 3158 | |
| 3159 | blob_names.insert(input_name); |
| 3160 | |
| 3161 | input_node_count++; |
| 3162 | } |
| 3163 | |
| 3164 | // for (auto a: node_reference) |
| 3165 | // { |
| 3166 | // fprintf(stderr, "a = %s %d\n", a.first.c_str(), a.second); |
| 3167 | // } |
| 3168 | |
| 3169 | // op chain fusion |
| 3170 | int reduced_node_count = 0; |
| 3171 | fuse_weight_reshape(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3172 | fuse_weight_transpose(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3173 | fuse_shufflechannel(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3174 | fuse_shufflechannel_split(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3175 | fuse_hardsigmoid(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3176 | fuse_hardswish(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3177 | fuse_swish(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3178 | fuse_batchnorm1d_squeeze_unsqueeze(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3179 | fuse_unsqueeze_prelu(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3180 | fuse_normalize(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3181 | fuse_groupnorm(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3182 | fuse_layernorm(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3183 | fuse_flatten(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3184 | fuse_pixelshuffle(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3185 | fuse_reorg(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3186 | fuse_expand_broadcast(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3187 | fuse_lstm_gru_rnn(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3188 | fuse_multiheadattention(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3189 | fuse_binaryop_with_scalar(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3190 | fuse_rewrite_gather(mutable_graph, weights, node_reference, blob_names, reduced_node_count); |
| 3191 | |
| 3192 | // reduce common const weight node_reference |
| 3193 | for (int i = 0; i < node_count; i++) |
| 3194 | { |
| 3195 | const onnx::NodeProto& node = graph.node(i); |
| 3196 | |
| 3197 | const std::string& op = node.op_type(); |
| 3198 | |
| 3199 | if (op == "BatchNormalization") |
| 3200 | { |
| 3201 | node_reference[node.input(1)] -= 1; |
| 3202 | node_reference[node.input(2)] -= 1; |
| 3203 | node_reference[node.input(3)] -= 1; |
| 3204 | node_reference[node.input(4)] -= 1; |
| 3205 | } |
| 3206 | else if (op == "BiasGelu") |
| 3207 | { |
| 3208 | node_reference[node.input(1)] -= 1; |
| 3209 | } |
| 3210 | else if (op == "Clip") |
| 3211 | { |
| 3212 | if (node.input_size() == 3) |
| 3213 | { |
| 3214 | node_reference[node.input(1)] -= 1; |
| 3215 | node_reference[node.input(2)] -= 1; |
| 3216 | } |
| 3217 | } |
| 3218 | else if (op == "Conv") |
| 3219 | { |
| 3220 | node_reference[node.input(1)] -= 1; |
| 3221 | if (node.input_size() == 3) |
| 3222 | { |
| 3223 | node_reference[node.input(2)] -= 1; |
| 3224 | } |
| 3225 | } |
| 3226 | else if (op == "ConvTranspose") |
| 3227 | { |
| 3228 | node_reference[node.input(1)] -= 1; |
| 3229 | if (node.input_size() == 3) |
| 3230 | { |
| 3231 | node_reference[node.input(2)] -= 1; |
| 3232 | } |
| 3233 | } |
| 3234 | else if (op == "EmbedLayerNormalization") |
| 3235 | { |
| 3236 | node_reference[node.input(1)] -= 1; |
| 3237 | node_reference[node.input(2)] -= 1; |
| 3238 | node_reference[node.input(3)] -= 1; |
| 3239 | node_reference[node.input(4)] -= 1; |
| 3240 | node_reference[node.input(5)] -= 1; |
| 3241 | node_reference[node.input(6)] -= 1; |
| 3242 | } |
| 3243 | else if (op == "Gemm") |
| 3244 | { |
| 3245 | float alpha = get_node_attr_f(node, "alpha", 1.f); |
| 3246 | float beta = get_node_attr_f(node, "beta", 1.f); |
| 3247 | int transA = get_node_attr_i(node, "transA", 0); |
| 3248 | int transB = get_node_attr_i(node, "transB", 0); |
| 3249 | |
| 3250 | if (alpha == 1.f && beta == 1.f && transA == 0 && transB == 1) |
| 3251 | { |
| 3252 | // InnerProduct-like A * B + C |
| 3253 | node_reference[node.input(1)] -= 1; |
| 3254 | node_reference[node.input(2)] -= 1; |
| 3255 | } |
| 3256 | } |
| 3257 | else if (op == "GroupNorm") |
| 3258 | { |
| 3259 | int affine = get_node_attr_i(node, "affine", 1); |
| 3260 | if (affine) |
| 3261 | { |
| 3262 | node_reference[node.input(1)] -= 1; |
| 3263 | node_reference[node.input(2)] -= 1; |
| 3264 | } |
| 3265 | } |
| 3266 | else if (op == "GRU") |
| 3267 | { |
| 3268 | for (int j = 1; j < node.input_size(); j++) |
| 3269 | { |
| 3270 | node_reference[node.input(j)] -= 1; |
| 3271 | } |
| 3272 | } |
| 3273 | else if (op == "InstanceNormalization") |
| 3274 | { |
| 3275 | node_reference[node.input(1)] -= 1; |
| 3276 | node_reference[node.input(2)] -= 1; |
| 3277 | } |
| 3278 | else if (op == "LayerNorm") |
| 3279 | { |
| 3280 | int affine = get_node_attr_i(node, "affine", 1); |
| 3281 | if (affine) |
| 3282 | { |
| 3283 | node_reference[node.input(1)] -= 1; |
| 3284 | node_reference[node.input(2)] -= 1; |
| 3285 | } |
| 3286 | } |
| 3287 | else if (op == "LSTM") |
| 3288 | { |
| 3289 | for (int j = 1; j < node.input_size(); j++) |
| 3290 | { |
| 3291 | node_reference[node.input(j)] -= 1; |
| 3292 | } |
| 3293 | } |
| 3294 | else if (op == "MatMul") |
| 3295 | { |
| 3296 | if (weights.find(node.input(1)) != weights.end() && weights[node.input(1)].dims_size() == 2) |
| 3297 | { |
| 3298 | // InnerProduct |
| 3299 | node_reference[node.input(1)] -= 1; |
| 3300 | } |
| 3301 | } |
| 3302 | else if (op == "MultiHeadAttention") |
| 3303 | { |
| 3304 | if (node.input_size() == 5) |
| 3305 | { |
| 3306 | node_reference[node.input(1)] -= 1; |
| 3307 | node_reference[node.input(2)] -= 1; |
| 3308 | node_reference[node.input(3)] -= 1; |
| 3309 | node_reference[node.input(4)] -= 1; |
| 3310 | } |
| 3311 | else |
| 3312 | { |
| 3313 | node_reference[node.input(3)] -= 1; |
| 3314 | node_reference[node.input(4)] -= 1; |
| 3315 | node_reference[node.input(5)] -= 1; |
| 3316 | node_reference[node.input(6)] -= 1; |
| 3317 | node_reference[node.input(7)] -= 1; |
| 3318 | node_reference[node.input(8)] -= 1; |
| 3319 | node_reference[node.input(9)] -= 1; |
| 3320 | node_reference[node.input(10)] -= 1; |
| 3321 | } |
| 3322 | } |
| 3323 | else if (op == "Pad") |
| 3324 | { |
| 3325 | if (node.input_size() >= 2) |
| 3326 | { |
| 3327 | node_reference[node.input(1)] -= 1; |
| 3328 | } |
| 3329 | } |
| 3330 | else if (op == "PRelu") |
| 3331 | { |
| 3332 | node_reference[node.input(1)] -= 1; |
| 3333 | } |
| 3334 | else if (op == "Reshape") |
| 3335 | { |
| 3336 | if (node.input_size() >= 2) |
| 3337 | { |
| 3338 | node_reference[node.input(1)] -= 1; |
| 3339 | } |
| 3340 | } |
| 3341 | else if (op == "Resize") |
| 3342 | { |
| 3343 | if (node.input_size() == 2) |
| 3344 | { |
| 3345 | // opset 10 |
| 3346 | node_reference[node.input(1)] -= 1; |
| 3347 | } |
| 3348 | else |
| 3349 | { |
| 3350 | // opset 11+ |
| 3351 | node_reference[node.input(1)] -= 1; |
| 3352 | node_reference[node.input(2)] -= 1; |
| 3353 | if (node.input_size() >= 4) |
| 3354 | { |
| 3355 | node_reference[node.input(3)] -= 1; |
| 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | else if (op == "RNN") |
| 3360 | { |
| 3361 | for (int j = 1; j < node.input_size(); j++) |
| 3362 | { |
| 3363 | node_reference[node.input(j)] -= 1; |
| 3364 | } |
| 3365 | } |
| 3366 | else if (op == "SkipLayerNormalization") |
| 3367 | { |
| 3368 | node_reference[node.input(2)] -= 1; |
| 3369 | node_reference[node.input(3)] -= 1; |
| 3370 | node_reference[node.input(4)] -= 1; |
| 3371 | } |
| 3372 | else if (op == "Slice") |
| 3373 | { |
| 3374 | if (node.input_size() >= 2) |
| 3375 | { |
| 3376 | node_reference[node.input(1)] -= 1; |
| 3377 | node_reference[node.input(2)] -= 1; |
| 3378 | if (node.input_size() >= 4) |
| 3379 | node_reference[node.input(3)] -= 1; |
| 3380 | if (node.input_size() >= 5) |
| 3381 | node_reference[node.input(4)] -= 1; |
| 3382 | } |
| 3383 | } |
| 3384 | else if (op == "Upsample") |
| 3385 | { |
| 3386 | if (node.input_size() >= 2) |
| 3387 | { |
| 3388 | node_reference[node.input(1)] -= 1; |
| 3389 | } |
| 3390 | } |
| 3391 | else if (op == "adaptive_avg_pool2d" || op == "adaptive_max_pool2d") |
| 3392 | { |
| 3393 | if (node.input_size() >= 2) |
| 3394 | { |
| 3395 | node_reference[node.input(1)] -= 1; |
| 3396 | } |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | // for (auto a: node_reference) |
| 3401 | // { |
| 3402 | // fprintf(stderr, "b = %s %d\n", a.first.c_str(), a.second); |
| 3403 | // } |
| 3404 | |
| 3405 | // count all weight node with zero reference |
| 3406 | int zero_reference_weight_node_count = 0; |
| 3407 | for (std::map<std::string, onnx::TensorProto>::iterator it = weights.begin(); it != weights.end(); it++) |
| 3408 | { |
| 3409 | const std::string& input_name = it->first; |
| 3410 | |
| 3411 | // there may be some weight nodes in initializer but none of the graph node use them |
| 3412 | // add them to blob_names so we could get proper blob count later |
| 3413 | blob_names.insert(input_name); |
| 3414 | |
| 3415 | int refcount = node_reference[input_name]; |
| 3416 | if (refcount == 0) |
| 3417 | zero_reference_weight_node_count++; |
| 3418 | } |
| 3419 | |
| 3420 | // we always treat constant node as weight or binaryop_weights |
| 3421 | // do not count it twice for layer_count |
| 3422 | int constant_node_count_moved_to_weight = 0; |
| 3423 | for (int i = 0; i < node_count; i++) |
| 3424 | { |
| 3425 | const onnx::NodeProto& node = graph.node(i); |
| 3426 | |
| 3427 | const std::string& op = node.op_type(); |
| 3428 | |
| 3429 | if (op == "Constant") |
| 3430 | { |
| 3431 | constant_node_count_moved_to_weight++; |
| 3432 | } |
| 3433 | } |
| 3434 | |
| 3435 | // some op may have anonymous input |
| 3436 | // LSTM sequence_lens |
| 3437 | blob_names.erase(""); |
| 3438 | node_reference.erase(""); |
| 3439 | |
| 3440 | // remove node_reference entry with reference equals to one |
| 3441 | int split_layer_count = 0; |
| 3442 | int splitncnn_blob_count = 0; |
| 3443 | // split node reference |
| 3444 | std::map<std::string, int> split_node_reference; |
| 3445 | for (std::map<std::string, int>::iterator it = node_reference.begin(); it != node_reference.end(); it++) |
| 3446 | { |
| 3447 | if (it->second > 1) |
| 3448 | { |
| 3449 | split_layer_count++; |
| 3450 | splitncnn_blob_count += it->second; |
| 3451 | |
| 3452 | split_node_reference[it->first] = it->second; |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | fprintf(pp, "%zu %zu\n", node_count - constant_node_count_moved_to_weight + weights.size() - zero_reference_weight_node_count - reduced_node_count + input_node_count + split_layer_count, blob_names.size() - zero_reference_weight_node_count + splitncnn_blob_count); |
| 3457 | |
| 3458 | int internal_split = 0; |
| 3459 | |
| 3460 | // place Input at the beginning |
| 3461 | for (int j = 0; j < graph.input_size(); j++) |
| 3462 | { |
| 3463 | const std::string& input_name = graph.input(j).name(); |
| 3464 | |
| 3465 | // check weight |
| 3466 | if (weights.find(input_name) != weights.end()) |
| 3467 | continue; |
| 3468 | |
| 3469 | fprintf(pp, "%-16s %-24s 0 1 %s\n", "Input", trunc_name(input_name).c_str(), trunc_name(input_name).c_str()); |
| 3470 | |
| 3471 | int refcount = node_reference[input_name]; |
| 3472 | if (refcount <= 1) |
| 3473 | { |
| 3474 | continue; |
| 3475 | } |
| 3476 | |
| 3477 | char splitname[256]; |
| 3478 | sprintf(splitname, "splitncnn_input%d", j); |
| 3479 | fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount); |
| 3480 | fprintf(pp, " %s", trunc_name(input_name).c_str()); |
| 3481 | |
| 3482 | for (int k = 0; k < refcount; k++) |
| 3483 | { |
| 3484 | std::string split_name = input_name + "_splitncnn_" + std::to_string(k); |
| 3485 | fprintf(pp, " %s", trunc_name(split_name).c_str()); |
| 3486 | } |
| 3487 | fprintf(pp, "\n"); |
| 3488 | } |
| 3489 | |
| 3490 | // place MemoryData next |
| 3491 | for (std::map<std::string, onnx::TensorProto>::iterator weight_it = weights.begin(); weight_it != weights.end(); weight_it++) |
| 3492 | { |
| 3493 | const std::string& input_name = weight_it->first; |
| 3494 | |
| 3495 | int refcount = node_reference[input_name]; |
| 3496 | if (refcount == 0) |
| 3497 | { |
| 3498 | continue; |
| 3499 | } |
| 3500 | |
| 3501 | fprintf(pp, "%-16s %-24s 0 1 %s", "MemoryData", trunc_name(input_name).c_str(), trunc_name(input_name).c_str()); |
| 3502 | |
| 3503 | const onnx::TensorProto& M = weights[input_name]; |
| 3504 | |
| 3505 | if (M.dims_size() == 0) |
| 3506 | { |
| 3507 | fprintf(pp, " 0=%d", get_tensor_proto_data_size(M)); |
| 3508 | } |
| 3509 | else if (M.dims_size() == 1) |
| 3510 | { |
| 3511 | fprintf(pp, " 0=%d", (int)M.dims(0)); |
| 3512 | } |
| 3513 | else if (M.dims_size() == 2) |
| 3514 | { |
| 3515 | fprintf(pp, " 0=%d", (int)M.dims(1)); |
| 3516 | if (M.dims(0) != 1) |
| 3517 | { |
| 3518 | fprintf(pp, " 1=%d", (int)M.dims(0)); |
| 3519 | } |
| 3520 | } |
| 3521 | else if (M.dims_size() == 3) |
| 3522 | { |
| 3523 | fprintf(pp, " 0=%d", (int)M.dims(2)); |
| 3524 | fprintf(pp, " 1=%d", (int)M.dims(1)); |
| 3525 | if (M.dims(0) != 1) |
| 3526 | { |
| 3527 | fprintf(pp, " 2=%d", (int)M.dims(0)); |
| 3528 | } |
| 3529 | } |
| 3530 | else if (M.dims_size() == 4) |
| 3531 | { |
| 3532 | fprintf(pp, " 0=%d", (int)M.dims(3)); |
| 3533 | fprintf(pp, " 1=%d", (int)M.dims(2)); |
| 3534 | fprintf(pp, " 2=%d", (int)M.dims(1)); |
| 3535 | } |
| 3536 | |
| 3537 | fprintf(pp, "\n"); |
| 3538 | |
| 3539 | fwrite_tensor_proto_data(M, bp); |
| 3540 | |
| 3541 | if (refcount <= 1) |
| 3542 | { |
| 3543 | continue; |
| 3544 | } |
| 3545 | |
| 3546 | char splitname[256]; |
| 3547 | sprintf(splitname, "splitncnn_%d", internal_split); |
| 3548 | fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount); |
| 3549 | |
| 3550 | fprintf(pp, " %s", trunc_name(input_name).c_str()); |
| 3551 | |
| 3552 | for (int k = 0; k < refcount; k++) |
| 3553 | { |
| 3554 | std::string split_name = input_name + "_splitncnn_" + std::to_string(k); |
| 3555 | fprintf(pp, " %s", trunc_name(split_name).c_str()); |
| 3556 | } |
| 3557 | fprintf(pp, "\n"); |
| 3558 | |
| 3559 | internal_split++; |
| 3560 | } |
| 3561 | |
| 3562 | for (int i = 0; i < node_count; i++) |
| 3563 | { |
| 3564 | const onnx::NodeProto& node = graph.node(i); |
| 3565 | |
| 3566 | const std::string& op = node.op_type(); |
| 3567 | |
| 3568 | // fprintf(stderr, "op = %s\n", op.c_str()); |
| 3569 | |
| 3570 | if (op == "noop_reducedncnn") |
| 3571 | { |
| 3572 | continue; |
| 3573 | } |
| 3574 | |
| 3575 | std::string name = node.name(); |
| 3576 | if (name.empty()) |
| 3577 | { |
| 3578 | name = node.output(0); |
| 3579 | } |
| 3580 | |
| 3581 | int input_size = node.input_size(); |
| 3582 | int output_size = node.output_size(); |
| 3583 | |
| 3584 | for (int j = 0; j < (int)node.input_size(); j++) |
| 3585 | { |
| 3586 | const std::string& input_name = node.input(j); |
| 3587 | |
| 3588 | // check weight |
| 3589 | if (weights.find(input_name) != weights.end() && node_reference[input_name] == 0) |
| 3590 | { |
| 3591 | input_size--; |
| 3592 | } |
| 3593 | |
| 3594 | if (input_name.empty()) |
| 3595 | { |
| 3596 | input_size--; |
| 3597 | } |
| 3598 | |
| 3599 | // fprintf(stderr, " input = %s\n", input_name.c_str()); |
| 3600 | } |
| 3601 | /* |
| 3602 | for (int j=0; j<(int)node.output_size(); j++) |
| 3603 | { |
| 3604 | const std::string& output_name = node.output(j); |
| 3605 | fprintf(stderr, " output = %s\n", output_name.c_str()); |
| 3606 | } |
| 3607 | */ |
| 3608 | |
| 3609 | if (op == "Abs") |
| 3610 | { |
| 3611 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3612 | } |
| 3613 | else if (op == "Acos") |
| 3614 | { |
| 3615 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3616 | } |
| 3617 | else if (op == "Add") |
| 3618 | { |
| 3619 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3620 | } |
| 3621 | else if (op == "Asin") |
| 3622 | { |
| 3623 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3624 | } |
| 3625 | else if (op == "Atan") |
| 3626 | { |
| 3627 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3628 | } |
| 3629 | else if (op == "AveragePool" || op == "MaxPool") |
| 3630 | { |
| 3631 | std::vector<int> kernel_shape = get_node_attr_ai(node, "kernel_shape"); |
| 3632 | if (kernel_shape.size() == 1) |
| 3633 | { |
| 3634 | fprintf(pp, "%-16s", "Pooling1D"); |
| 3635 | } |
| 3636 | else |
| 3637 | { |
| 3638 | fprintf(pp, "%-16s", "Pooling"); |
| 3639 | } |
| 3640 | } |
| 3641 | else if (op == "BatchNormalization") |
| 3642 | { |
| 3643 | fprintf(pp, "%-16s", "BatchNorm"); |
| 3644 | } |
| 3645 | else if (op == "BiasGelu") |
| 3646 | { |
| 3647 | fprintf(pp, "%-16s", "BiasGelu"); |
| 3648 | } |
| 3649 | else if (op == "Ceil") |
| 3650 | { |
| 3651 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3652 | } |
| 3653 | else if (op == "Celu") |
| 3654 | { |
| 3655 | fprintf(pp, "%-16s", "CELU"); |
| 3656 | } |
| 3657 | else if (op == "Clip") |
| 3658 | { |
| 3659 | fprintf(pp, "%-16s", "Clip"); |
| 3660 | } |
| 3661 | else if (op == "Concat") |
| 3662 | { |
| 3663 | fprintf(pp, "%-16s", "Concat"); |
| 3664 | } |
| 3665 | else if (op == "Constant") |
| 3666 | { |
| 3667 | continue; |
| 3668 | } |
| 3669 | else if (op == "Conv") |
| 3670 | { |
| 3671 | std::vector<int> kernel_shape = get_node_attr_ai(node, "kernel_shape"); |
| 3672 | if (kernel_shape.size() == 1) |
| 3673 | { |
| 3674 | fprintf(pp, "%-16s", "Convolution1D"); |
| 3675 | } |
| 3676 | else |
| 3677 | { |
| 3678 | int group = get_node_attr_i(node, "group", 1); |
| 3679 | if (group > 1) |
| 3680 | { |
| 3681 | fprintf(pp, "%-16s", "ConvolutionDepthWise"); |
| 3682 | } |
| 3683 | else |
| 3684 | { |
| 3685 | fprintf(pp, "%-16s", "Convolution"); |
| 3686 | } |
| 3687 | } |
| 3688 | } |
| 3689 | else if (op == "ConvTranspose") |
| 3690 | { |
| 3691 | int group = get_node_attr_i(node, "group", 1); |
| 3692 | if (group > 1) |
| 3693 | { |
| 3694 | fprintf(pp, "%-16s", "DeconvolutionDepthWise"); |
| 3695 | } |
| 3696 | else |
| 3697 | { |
| 3698 | fprintf(pp, "%-16s", "Deconvolution"); |
| 3699 | } |
| 3700 | } |
| 3701 | else if (op == "Cos") |
| 3702 | { |
| 3703 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3704 | } |
| 3705 | else if (op == "Crop") |
| 3706 | { |
| 3707 | fprintf(pp, "%-16s", "Crop"); |
| 3708 | } |
| 3709 | else if (op == "DepthToSpace") |
| 3710 | { |
| 3711 | fprintf(pp, "%-16s", "PixelShuffle"); |
| 3712 | } |
| 3713 | else if (op == "Div") |
| 3714 | { |
| 3715 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3716 | } |
| 3717 | else if (op == "Dropout") |
| 3718 | { |
| 3719 | fprintf(pp, "%-16s", "Dropout"); |
| 3720 | output_size = 1; |
| 3721 | } |
| 3722 | else if (op == "Elu") |
| 3723 | { |
| 3724 | fprintf(pp, "%-16s", "ELU"); |
| 3725 | } |
| 3726 | else if (op == "EmbedLayerNormalization") |
| 3727 | { |
| 3728 | fprintf(pp, "%-16s", "EmbedLayerNormalization"); |
| 3729 | } |
| 3730 | else if (op == "Erf") |
| 3731 | { |
| 3732 | fprintf(pp, "%-16s", "Erf"); |
| 3733 | } |
| 3734 | else if (op == "Exp") |
| 3735 | { |
| 3736 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3737 | } |
| 3738 | else if (op == "Flatten") |
| 3739 | { |
| 3740 | fprintf(pp, "%-16s", "Flatten"); |
| 3741 | } |
| 3742 | else if (op == "Floor") |
| 3743 | { |
| 3744 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3745 | } |
| 3746 | else if (op == "Gelu") |
| 3747 | { |
| 3748 | fprintf(pp, "%-16s", "GELU"); |
| 3749 | } |
| 3750 | else if (op == "Gemm") |
| 3751 | { |
| 3752 | float alpha = get_node_attr_f(node, "alpha", 1.f); |
| 3753 | float beta = get_node_attr_f(node, "beta", 1.f); |
| 3754 | int transA = get_node_attr_i(node, "transA", 0); |
| 3755 | int transB = get_node_attr_i(node, "transB", 0); |
| 3756 | |
| 3757 | if (alpha == 1.f && beta == 1.f && transA == 0 && transB == 1) |
| 3758 | { |
| 3759 | // InnerProduct-like A * B + C |
| 3760 | fprintf(pp, "%-16s", "InnerProduct"); |
| 3761 | } |
| 3762 | else |
| 3763 | { |
| 3764 | fprintf(pp, "%-16s", "Gemm"); |
| 3765 | } |
| 3766 | } |
| 3767 | else if (op == "GlobalAveragePool") |
| 3768 | { |
| 3769 | fprintf(pp, "%-16s", "Pooling"); |
| 3770 | } |
| 3771 | else if (op == "GlobalMaxPool") |
| 3772 | { |
| 3773 | fprintf(pp, "%-16s", "Pooling"); |
| 3774 | } |
| 3775 | else if (op == "adaptive_avg_pool2d" || op == "adaptive_max_pool2d") |
| 3776 | { |
| 3777 | fprintf(pp, "%-16s", "Pooling"); |
| 3778 | } |
| 3779 | else if (op == "GroupNorm") |
| 3780 | { |
| 3781 | fprintf(pp, "%-16s", "GroupNorm"); |
| 3782 | } |
| 3783 | else if (op == "GRU") |
| 3784 | { |
| 3785 | fprintf(pp, "%-16s", "GRU"); |
| 3786 | } |
| 3787 | else if (op == "HardSigmoid") |
| 3788 | { |
| 3789 | fprintf(pp, "%-16s", "HardSigmoid"); |
| 3790 | } |
| 3791 | else if (op == "HardSwish") |
| 3792 | { |
| 3793 | fprintf(pp, "%-16s", "HardSwish"); |
| 3794 | } |
| 3795 | else if (op == "ImageScaler") |
| 3796 | { |
| 3797 | fprintf(pp, "%-16s", "Scale"); |
| 3798 | } |
| 3799 | else if (op == "InstanceNormalization") |
| 3800 | { |
| 3801 | fprintf(pp, "%-16s", "InstanceNorm"); |
| 3802 | } |
| 3803 | else if (op == "LayerNorm") |
| 3804 | { |
| 3805 | fprintf(pp, "%-16s", "LayerNorm"); |
| 3806 | } |
| 3807 | else if (op == "LeakyRelu") |
| 3808 | { |
| 3809 | fprintf(pp, "%-16s", "ReLU"); |
| 3810 | } |
| 3811 | else if (op == "Log") |
| 3812 | { |
| 3813 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3814 | } |
| 3815 | else if (op == "LRN") |
| 3816 | { |
| 3817 | fprintf(pp, "%-16s", "LRN"); |
| 3818 | } |
| 3819 | else if (op == "LSTM") |
| 3820 | { |
| 3821 | fprintf(pp, "%-16s", "LSTM"); |
| 3822 | } |
| 3823 | else if (op == "MatMul") |
| 3824 | { |
| 3825 | if (weights.find(node.input(1)) != weights.end() && weights[node.input(1)].dims_size() == 2) |
| 3826 | { |
| 3827 | fprintf(pp, "%-16s", "InnerProduct"); |
| 3828 | } |
| 3829 | else |
| 3830 | { |
| 3831 | fprintf(pp, "%-16s", "Gemm"); |
| 3832 | } |
| 3833 | } |
| 3834 | else if (op == "Max") |
| 3835 | { |
| 3836 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3837 | } |
| 3838 | else if (op == "Min") |
| 3839 | { |
| 3840 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3841 | } |
| 3842 | else if (op == "Mul") |
| 3843 | { |
| 3844 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3845 | } |
| 3846 | else if (op == "MultiHeadAttention") |
| 3847 | { |
| 3848 | fprintf(pp, "%-16s", "MultiHeadAttention"); |
| 3849 | } |
| 3850 | else if (op == "Neg") |
| 3851 | { |
| 3852 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3853 | } |
| 3854 | else if (op == "Normalize") |
| 3855 | { |
| 3856 | fprintf(pp, "%-16s", "Normalize"); |
| 3857 | } |
| 3858 | else if (op == "Pad") |
| 3859 | { |
| 3860 | fprintf(pp, "%-16s", "Padding"); |
| 3861 | } |
| 3862 | else if (op == "PixelShuffle") |
| 3863 | { |
| 3864 | fprintf(pp, "%-16s", "PixelShuffle"); |
| 3865 | } |
| 3866 | else if (op == "Pow") |
| 3867 | { |
| 3868 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3869 | } |
| 3870 | else if (op == "PRelu") |
| 3871 | { |
| 3872 | fprintf(pp, "%-16s", "PReLU"); |
| 3873 | } |
| 3874 | else if (op == "Reciprocal") |
| 3875 | { |
| 3876 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3877 | } |
| 3878 | else if (op == "ReduceMax" || op == "ReduceMin" || op == "ReduceMean" || op == "ReduceProd" || op == "ReduceSum" || op == "ReduceSumSquare" || op == "ReduceL1" || op == "ReduceL2" || op == "ReduceLogSum" || op == "ReduceLogSumExp") |
| 3879 | { |
| 3880 | fprintf(pp, "%-16s", "Reduction"); |
| 3881 | } |
| 3882 | else if (op == "Relu") |
| 3883 | { |
| 3884 | fprintf(pp, "%-16s", "ReLU"); |
| 3885 | } |
| 3886 | else if (op == "Reorg") |
| 3887 | { |
| 3888 | fprintf(pp, "%-16s", "Reorg"); |
| 3889 | } |
| 3890 | else if (op == "Reshape") |
| 3891 | { |
| 3892 | fprintf(pp, "%-16s", "Reshape"); |
| 3893 | } |
| 3894 | else if (op == "RNN") |
| 3895 | { |
| 3896 | fprintf(pp, "%-16s", "RNN"); |
| 3897 | } |
| 3898 | else if (op == "RDiv") |
| 3899 | { |
| 3900 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3901 | } |
| 3902 | else if (op == "RSub") |
| 3903 | { |
| 3904 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3905 | } |
| 3906 | else if (op == "Shrink") |
| 3907 | { |
| 3908 | fprintf(pp, "%-16s", "Shrink"); |
| 3909 | } |
| 3910 | else if (op == "ShuffleChannel") |
| 3911 | { |
| 3912 | fprintf(pp, "%-16s", "ShuffleChannel"); |
| 3913 | } |
| 3914 | else if (op == "Sigmoid") |
| 3915 | { |
| 3916 | fprintf(pp, "%-16s", "Sigmoid"); |
| 3917 | } |
| 3918 | else if (op == "Sin") |
| 3919 | { |
| 3920 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3921 | } |
| 3922 | else if (op == "SkipLayerNormalization") |
| 3923 | { |
| 3924 | fprintf(pp, "%-16s", "SkipLayerNormalization"); |
| 3925 | } |
| 3926 | else if (op == "Slice") |
| 3927 | { |
| 3928 | fprintf(pp, "%-16s", "Crop"); |
| 3929 | } |
| 3930 | else if (op == "Softmax") |
| 3931 | { |
| 3932 | fprintf(pp, "%-16s", "Softmax"); |
| 3933 | } |
| 3934 | else if (op == "Softplus") |
| 3935 | { |
| 3936 | fprintf(pp, "%-16s", "Softplus"); |
| 3937 | } |
| 3938 | else if (op == "Split") |
| 3939 | { |
| 3940 | fprintf(pp, "%-16s", "Slice"); |
| 3941 | } |
| 3942 | else if (op == "Sqrt") |
| 3943 | { |
| 3944 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3945 | } |
| 3946 | else if (op == "Squeeze") |
| 3947 | { |
| 3948 | fprintf(pp, "%-16s", "Squeeze"); |
| 3949 | } |
| 3950 | else if (op == "Sub") |
| 3951 | { |
| 3952 | fprintf(pp, "%-16s", "BinaryOp"); |
| 3953 | } |
| 3954 | else if (op == "Sum") |
| 3955 | { |
| 3956 | fprintf(pp, "%-16s", "Eltwise"); |
| 3957 | } |
| 3958 | else if (op == "Swish") |
| 3959 | { |
| 3960 | fprintf(pp, "%-16s", "Swish"); |
| 3961 | } |
| 3962 | else if (op == "Tan") |
| 3963 | { |
| 3964 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3965 | } |
| 3966 | else if (op == "Tanh") |
| 3967 | { |
| 3968 | fprintf(pp, "%-16s", "UnaryOp"); |
| 3969 | } |
| 3970 | else if (op == "Transpose") |
| 3971 | { |
| 3972 | fprintf(pp, "%-16s", "Permute"); |
| 3973 | } |
| 3974 | else if (op == "Upsample" || op == "Resize") |
| 3975 | { |
| 3976 | fprintf(pp, "%-16s", "Interp"); |
| 3977 | } |
| 3978 | else if (op == "Unsqueeze") |
| 3979 | { |
| 3980 | fprintf(pp, "%-16s", "ExpandDims"); |
| 3981 | } |
| 3982 | else |
| 3983 | { |
| 3984 | // TODO |
| 3985 | fprintf(stderr, "%s not supported yet!\n", op.c_str()); |
| 3986 | fprintf(pp, "%-16s", op.c_str()); |
| 3987 | } |
| 3988 | |
| 3989 | fprintf(pp, " %-24s %d %d", trunc_name(name).c_str(), input_size, output_size); |
| 3990 | |
| 3991 | for (int j = 0; j < (int)node.input_size(); j++) |
| 3992 | { |
| 3993 | std::string input_name = node.input(j); |
| 3994 | |
| 3995 | // check weight |
| 3996 | if (weights.find(input_name) != weights.end() && node_reference[input_name] == 0) |
| 3997 | { |
| 3998 | continue; |
| 3999 | } |
| 4000 | |
| 4001 | if (input_name.empty()) |
| 4002 | { |
| 4003 | continue; |
| 4004 | } |
| 4005 | |
| 4006 | if (split_node_reference.find(input_name) != split_node_reference.end()) |
| 4007 | { |
| 4008 | int refidx = split_node_reference[input_name] - 1; |
| 4009 | split_node_reference[input_name] = refidx; |
| 4010 | |
| 4011 | char splitsuffix[256]; |
| 4012 | sprintf(splitsuffix, "_splitncnn_%d", refidx); |
| 4013 | input_name = input_name + splitsuffix; |
| 4014 | } |
| 4015 | |
| 4016 | fprintf(pp, " %s", trunc_name(input_name).c_str()); |
| 4017 | } |
| 4018 | |
| 4019 | for (int j = 0; j < output_size; j++) |
| 4020 | { |
| 4021 | const std::string& output_name = node.output(j); |
| 4022 | |
| 4023 | fprintf(pp, " %s", trunc_name(output_name).c_str()); |
| 4024 | } |
| 4025 | |
| 4026 | if (op == "Abs") |
| 4027 | { |
| 4028 | int op_type = 0; |
| 4029 | fprintf(pp, " 0=%d", op_type); |
| 4030 | } |
| 4031 | else if (op == "Acos") |
| 4032 | { |
| 4033 | int op_type = 13; |
| 4034 | fprintf(pp, " 0=%d", op_type); |
| 4035 | } |
| 4036 | else if (op == "Add") |
| 4037 | { |
| 4038 | int op_type = 0; |
| 4039 | fprintf(pp, " 0=%d", op_type); |
| 4040 | |
| 4041 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 4042 | float b = get_node_attr_f(node, "b", 0.f); |
| 4043 | if (with_scalar) |
| 4044 | { |
| 4045 | fprintf(pp, " 1=%d", with_scalar); |
| 4046 | fprintf(pp, " 2=%e", b); |
| 4047 | } |
| 4048 | } |
| 4049 | else if (op == "Asin") |
| 4050 | { |
| 4051 | int op_type = 12; |
| 4052 | fprintf(pp, " 0=%d", op_type); |
| 4053 | } |
| 4054 | else if (op == "Atan") |
| 4055 | { |
| 4056 | int op_type = 14; |
| 4057 | fprintf(pp, " 0=%d", op_type); |
| 4058 | } |
| 4059 | else if (op == "AveragePool" || op == "MaxPool") |
| 4060 | { |
| 4061 | std::string auto_pad = get_node_attr_s(node, "auto_pad"); |
| 4062 | int ceil_mode = get_node_attr_i(node, "ceil_mode", 0); |
| 4063 | std::vector<int> kernel_shape = get_node_attr_ai(node, "kernel_shape"); |
| 4064 | std::vector<int> strides = get_node_attr_ai(node, "strides"); |
| 4065 | std::vector<int> pads = get_node_attr_ai(node, "pads"); |
| 4066 | |
| 4067 | int pool = op == "AveragePool" ? 1 : 0; |
| 4068 | int pad_mode = 1; |
| 4069 | |
| 4070 | if (auto_pad == "SAME_UPPER") |
| 4071 | { |
| 4072 | pad_mode = 2; |
| 4073 | } |
| 4074 | else if (auto_pad == "SAME_LOWER") |
| 4075 | { |
| 4076 | pad_mode = 3; |
| 4077 | } |
| 4078 | |
| 4079 | if (ceil_mode == 1) |
| 4080 | { |
| 4081 | pad_mode = 0; |
| 4082 | } |
| 4083 | |
| 4084 | fprintf(pp, " 0=%d", pool); |
| 4085 | |
| 4086 | if (kernel_shape.size() == 1) |
| 4087 | { |
| 4088 | fprintf(pp, " 1=%d", kernel_shape[0]); |
| 4089 | } |
| 4090 | else if (kernel_shape.size() == 2) |
| 4091 | { |
| 4092 | fprintf(pp, " 1=%d", kernel_shape[1]); |
| 4093 | fprintf(pp, " 11=%d", kernel_shape[0]); |
| 4094 | } |
| 4095 | |
| 4096 | if (strides.size() == 1) |
| 4097 | { |
| 4098 | fprintf(pp, " 2=%d", strides[0]); |
| 4099 | } |
| 4100 | else if (strides.size() == 2) |
| 4101 | { |
| 4102 | fprintf(pp, " 2=%d", strides[1]); |
| 4103 | fprintf(pp, " 12=%d", strides[0]); |
| 4104 | } |
| 4105 | |
| 4106 | if (pads.size() == 1) |
| 4107 | { |
| 4108 | fprintf(pp, " 3=%d", pads[0]); |
| 4109 | } |
| 4110 | else if (pads.size() == 2) |
| 4111 | { |
| 4112 | fprintf(pp, " 3=%d", pads[1]); |
| 4113 | fprintf(pp, " 13=%d", pads[0]); |
| 4114 | } |
| 4115 | else if (pads.size() == 4) |
| 4116 | { |
| 4117 | fprintf(pp, " 3=%d", pads[1]); |
| 4118 | fprintf(pp, " 13=%d", pads[0]); |
| 4119 | fprintf(pp, " 14=%d", pads[3]); |
| 4120 | fprintf(pp, " 15=%d", pads[2]); |
| 4121 | } |
| 4122 | |
| 4123 | fprintf(pp, " 5=%d", pad_mode); |
| 4124 | |
| 4125 | if (op == "AveragePool") |
| 4126 | { |
| 4127 | int avgpool_count_include_pad = get_node_attr_i(node, "count_include_pad", 0); |
| 4128 | fprintf(pp, " 6=%d", avgpool_count_include_pad); |
| 4129 | } |
| 4130 | } |
| 4131 | else if (op == "BatchNormalization") |
| 4132 | { |
| 4133 | float epsilon = get_node_attr_f(node, "epsilon", 1e-5f); |
| 4134 | |
| 4135 | const onnx::TensorProto& scale = weights[node.input(1)]; |
| 4136 | const onnx::TensorProto& B = weights[node.input(2)]; |
| 4137 | const onnx::TensorProto& mean = weights[node.input(3)]; |
| 4138 | const onnx::TensorProto& var = weights[node.input(4)]; |
| 4139 | |
| 4140 | int channels = get_tensor_proto_data_size(scale); |
| 4141 | |
| 4142 | fprintf(pp, " 0=%d", channels); |
| 4143 | |
| 4144 | fwrite_tensor_proto_data(scale, bp); |
| 4145 | fwrite_tensor_proto_data(mean, bp); |
| 4146 | // apply epsilon to var |
| 4147 | { |
| 4148 | const float* v = var.has_raw_data() ? (const float*)var.raw_data().data() : var.float_data().data(); |
| 4149 | |
| 4150 | for (int j = 0; j < channels; j++) |
| 4151 | { |
| 4152 | float ve = v[j] + epsilon; |
| 4153 | fwrite(&ve, sizeof(float), 1, bp); |
| 4154 | } |
| 4155 | } |
| 4156 | fwrite_tensor_proto_data(B, bp); |
| 4157 | } |
| 4158 | else if (op == "BiasGelu") |
| 4159 | { |
| 4160 | const onnx::TensorProto& B = weights[node.input(1)]; |
| 4161 | |
| 4162 | fprintf(pp, " 0=%d", get_tensor_proto_data_size(B)); |
| 4163 | |
| 4164 | int quantize_tag = 0; |
| 4165 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4166 | |
| 4167 | fwrite_tensor_proto_data(B, bp); |
| 4168 | } |
| 4169 | else if (op == "Ceil") |
| 4170 | { |
| 4171 | int op_type = 3; |
| 4172 | fprintf(pp, " 0=%d", op_type); |
| 4173 | } |
| 4174 | else if (op == "CeLU") |
| 4175 | { |
| 4176 | float alpha = get_node_attr_f(node, "alpha", 1.0f); |
| 4177 | |
| 4178 | fprintf(pp, " 0=%e", alpha); |
| 4179 | } |
| 4180 | else if (op == "Clip") |
| 4181 | { |
| 4182 | float min; |
| 4183 | float max; |
| 4184 | if (node.input_size() == 1) |
| 4185 | { |
| 4186 | min = get_node_attr_f(node, "min", -FLT_MAX); |
| 4187 | max = get_node_attr_f(node, "max", FLT_MAX); |
| 4188 | } |
| 4189 | else |
| 4190 | { |
| 4191 | min = weights.find(node.input(1)) != weights.end() ? get_node_attr_from_input_f(weights[node.input(1)]) : -FLT_MAX; |
| 4192 | max = weights.find(node.input(2)) != weights.end() ? get_node_attr_from_input_f(weights[node.input(2)]) : FLT_MAX; |
| 4193 | } |
| 4194 | |
| 4195 | fprintf(pp, " 0=%e", min); |
| 4196 | fprintf(pp, " 1=%e", max); |
| 4197 | } |
| 4198 | else if (op == "Concat") |
| 4199 | { |
| 4200 | int axis = get_node_attr_i(node, "axis", 1); |
| 4201 | fprintf(pp, " 0=%d", axis > 0 ? axis - 1 : axis); |
| 4202 | } |
| 4203 | else if (op == "Constant") |
| 4204 | { |
| 4205 | // never reach here |
| 4206 | } |
| 4207 | else if (op == "Conv") |
| 4208 | { |
| 4209 | const onnx::TensorProto& W = weights[node.input(1)]; |
| 4210 | |
| 4211 | int num_filter = W.dims(0); |
| 4212 | int has_bias = node.input_size() == 3 ? 1 : 0; |
| 4213 | |
| 4214 | std::string auto_pad = get_node_attr_s(node, "auto_pad"); |
| 4215 | std::vector<int> kernel_shape = get_node_attr_ai(node, "kernel_shape"); |
| 4216 | std::vector<int> dilations = get_node_attr_ai(node, "dilations"); |
| 4217 | std::vector<int> strides = get_node_attr_ai(node, "strides"); |
| 4218 | std::vector<int> pads = get_node_attr_ai(node, "pads"); |
| 4219 | int group = get_node_attr_i(node, "group", 1); |
| 4220 | |
| 4221 | fprintf(pp, " 0=%d", num_filter); |
| 4222 | |
| 4223 | if (kernel_shape.size() == 1) |
| 4224 | { |
| 4225 | fprintf(pp, " 1=%d", kernel_shape[0]); |
| 4226 | } |
| 4227 | else if (kernel_shape.size() == 2) |
| 4228 | { |
| 4229 | fprintf(pp, " 1=%d", kernel_shape[1]); |
| 4230 | fprintf(pp, " 11=%d", kernel_shape[0]); |
| 4231 | } |
| 4232 | |
| 4233 | if (dilations.size() == 1) |
| 4234 | { |
| 4235 | fprintf(pp, " 2=%d", dilations[0]); |
| 4236 | } |
| 4237 | else if (dilations.size() == 2) |
| 4238 | { |
| 4239 | fprintf(pp, " 2=%d", dilations[1]); |
| 4240 | fprintf(pp, " 12=%d", dilations[0]); |
| 4241 | } |
| 4242 | |
| 4243 | if (strides.size() == 1) |
| 4244 | { |
| 4245 | fprintf(pp, " 3=%d", strides[0]); |
| 4246 | } |
| 4247 | else if (strides.size() == 2) |
| 4248 | { |
| 4249 | fprintf(pp, " 3=%d", strides[1]); |
| 4250 | fprintf(pp, " 13=%d", strides[0]); |
| 4251 | } |
| 4252 | |
| 4253 | if (auto_pad == "SAME_UPPER") |
| 4254 | { |
| 4255 | fprintf(pp, " 4=-233"); |
| 4256 | } |
| 4257 | else if (auto_pad == "SAME_LOWER") |
| 4258 | { |
| 4259 | fprintf(pp, " 4=-234"); |
| 4260 | } |
| 4261 | else |
| 4262 | { |
| 4263 | if (pads.size() == 1) |
| 4264 | { |
| 4265 | fprintf(pp, " 4=%d", pads[0]); |
| 4266 | } |
| 4267 | else if (pads.size() == 2) |
| 4268 | { |
| 4269 | fprintf(pp, " 4=%d", pads[1]); |
| 4270 | fprintf(pp, " 14=%d", pads[0]); |
| 4271 | } |
| 4272 | else if (pads.size() == 4) |
| 4273 | { |
| 4274 | fprintf(pp, " 4=%d", pads[1]); |
| 4275 | fprintf(pp, " 14=%d", pads[0]); |
| 4276 | fprintf(pp, " 15=%d", pads[3]); |
| 4277 | fprintf(pp, " 16=%d", pads[2]); |
| 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | fprintf(pp, " 5=%d", has_bias); |
| 4282 | |
| 4283 | fprintf(pp, " 6=%d", get_tensor_proto_data_size(W)); |
| 4284 | |
| 4285 | if (group > 1) |
| 4286 | { |
| 4287 | fprintf(pp, " 7=%d", group); |
| 4288 | } |
| 4289 | |
| 4290 | int quantize_tag = 0; |
| 4291 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4292 | |
| 4293 | fwrite_tensor_proto_data(W, bp); |
| 4294 | |
| 4295 | if (has_bias) |
| 4296 | { |
| 4297 | const onnx::TensorProto& B = weights[node.input(2)]; |
| 4298 | fwrite_tensor_proto_data(B, bp); |
| 4299 | } |
| 4300 | } |
| 4301 | else if (op == "ConvTranspose") |
| 4302 | { |
| 4303 | const onnx::TensorProto& W = weights[node.input(1)]; |
| 4304 | |
| 4305 | int has_bias = node.input_size() == 3 ? 1 : 0; |
| 4306 | |
| 4307 | std::string auto_pad = get_node_attr_s(node, "auto_pad"); |
| 4308 | std::vector<int> kernel_shape = get_node_attr_ai(node, "kernel_shape"); |
| 4309 | std::vector<int> dilations = get_node_attr_ai(node, "dilations"); |
| 4310 | std::vector<int> strides = get_node_attr_ai(node, "strides"); |
| 4311 | std::vector<int> output_padding = get_node_attr_ai(node, "output_padding"); |
| 4312 | std::vector<int> output_shape = get_node_attr_ai(node, "output_shape"); |
| 4313 | std::vector<int> pads = get_node_attr_ai(node, "pads"); |
| 4314 | int group = get_node_attr_i(node, "group", 1); |
| 4315 | int num_filter = W.dims(1) * group; |
| 4316 | |
| 4317 | fprintf(pp, " 0=%d", num_filter); |
| 4318 | |
| 4319 | if (kernel_shape.size() == 1) |
| 4320 | { |
| 4321 | fprintf(pp, " 1=%d", kernel_shape[0]); |
| 4322 | } |
| 4323 | else if (kernel_shape.size() == 2) |
| 4324 | { |
| 4325 | fprintf(pp, " 1=%d", kernel_shape[1]); |
| 4326 | fprintf(pp, " 11=%d", kernel_shape[0]); |
| 4327 | } |
| 4328 | |
| 4329 | if (dilations.size() == 1) |
| 4330 | { |
| 4331 | fprintf(pp, " 2=%d", dilations[0]); |
| 4332 | } |
| 4333 | else if (dilations.size() == 2) |
| 4334 | { |
| 4335 | fprintf(pp, " 2=%d", dilations[1]); |
| 4336 | fprintf(pp, " 12=%d", dilations[0]); |
| 4337 | } |
| 4338 | |
| 4339 | if (strides.size() == 1) |
| 4340 | { |
| 4341 | fprintf(pp, " 3=%d", strides[0]); |
| 4342 | } |
| 4343 | else if (strides.size() == 2) |
| 4344 | { |
| 4345 | fprintf(pp, " 3=%d", strides[1]); |
| 4346 | fprintf(pp, " 13=%d", strides[0]); |
| 4347 | } |
| 4348 | |
| 4349 | if (auto_pad == "SAME_UPPER") |
| 4350 | { |
| 4351 | fprintf(pp, " 4=-233"); |
| 4352 | } |
| 4353 | else if (auto_pad == "SAME_LOWER") |
| 4354 | { |
| 4355 | fprintf(pp, " 4=-234"); |
| 4356 | } |
| 4357 | else |
| 4358 | { |
| 4359 | if (pads.size() == 1) |
| 4360 | { |
| 4361 | fprintf(pp, " 4=%d", pads[0]); |
| 4362 | } |
| 4363 | else if (pads.size() == 2) |
| 4364 | { |
| 4365 | fprintf(pp, " 4=%d", pads[1]); |
| 4366 | fprintf(pp, " 14=%d", pads[0]); |
| 4367 | } |
| 4368 | else if (pads.size() == 4) |
| 4369 | { |
| 4370 | fprintf(pp, " 4=%d", pads[1]); |
| 4371 | fprintf(pp, " 14=%d", pads[0]); |
| 4372 | fprintf(pp, " 15=%d", pads[3]); |
| 4373 | fprintf(pp, " 16=%d", pads[2]); |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | if (output_padding.size() == 1) |
| 4378 | { |
| 4379 | fprintf(pp, " 18=%d", output_padding[0]); |
| 4380 | } |
| 4381 | else if (output_padding.size() == 2) |
| 4382 | { |
| 4383 | fprintf(pp, " 18=%d", output_padding[1]); |
| 4384 | fprintf(pp, " 19=%d", output_padding[0]); |
| 4385 | } |
| 4386 | |
| 4387 | if (output_shape.size() == 1) |
| 4388 | { |
| 4389 | fprintf(pp, " 20=%d", output_shape[0]); |
| 4390 | } |
| 4391 | else if (output_shape.size() == 2) |
| 4392 | { |
| 4393 | fprintf(pp, " 20=%d", output_shape[1]); |
| 4394 | fprintf(pp, " 21=%d", output_shape[0]); |
| 4395 | } |
| 4396 | |
| 4397 | fprintf(pp, " 5=%d", has_bias); |
| 4398 | |
| 4399 | fprintf(pp, " 6=%d", get_tensor_proto_data_size(W)); |
| 4400 | |
| 4401 | if (group > 1) |
| 4402 | { |
| 4403 | fprintf(pp, " 7=%d", group); |
| 4404 | } |
| 4405 | |
| 4406 | int quantize_tag = 0; |
| 4407 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4408 | |
| 4409 | int maxk = 0; |
| 4410 | if (kernel_shape.size() == 2) |
| 4411 | { |
| 4412 | maxk = kernel_shape[1] * kernel_shape[0]; |
| 4413 | } |
| 4414 | else |
| 4415 | { |
| 4416 | maxk = kernel_shape[0] * kernel_shape[0]; |
| 4417 | } |
| 4418 | int weight_data_size = get_tensor_proto_data_size(W); |
| 4419 | const float* weight_data = 0; |
| 4420 | if (W.has_raw_data()) |
| 4421 | { |
| 4422 | weight_data = (const float*)W.raw_data().data(); |
| 4423 | } |
| 4424 | else if (W.data_type() == 1) |
| 4425 | { |
| 4426 | weight_data = W.float_data().data(); |
| 4427 | } |
| 4428 | for (int g = 0; g < group; g++) |
| 4429 | { |
| 4430 | // reorder weight from inch-outch to outch-inch |
| 4431 | int num_filter_g = num_filter / group; |
| 4432 | int num_input = weight_data_size / maxk / num_filter_g / group; |
| 4433 | const float* weight_data_ptr = weight_data + g * maxk * num_filter_g * num_input; |
| 4434 | for (int k = 0; k < num_filter_g; k++) |
| 4435 | { |
| 4436 | for (int j = 0; j < num_input; j++) |
| 4437 | { |
| 4438 | fwrite(weight_data_ptr + (j * num_filter_g + k) * maxk, sizeof(float), maxk, bp); |
| 4439 | } |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | if (has_bias) |
| 4444 | { |
| 4445 | const onnx::TensorProto& B = weights[node.input(2)]; |
| 4446 | fwrite_tensor_proto_data(B, bp); |
| 4447 | } |
| 4448 | } |
| 4449 | else if (op == "Cos") |
| 4450 | { |
| 4451 | int op_type = 10; |
| 4452 | fprintf(pp, " 0=%d", op_type); |
| 4453 | } |
| 4454 | else if (op == "Crop") |
| 4455 | { |
| 4456 | auto starts = get_node_attr_ai(node, "starts"); |
| 4457 | fprintf(pp, " -23309=%zu", starts.size()); |
| 4458 | for (size_t j = 0; j < starts.size(); ++j) |
| 4459 | { |
| 4460 | fprintf(pp, ",%i", starts[j]); |
| 4461 | } |
| 4462 | auto ends = get_node_attr_ai(node, "ends"); |
| 4463 | fprintf(pp, " -23310=%zu", ends.size()); |
| 4464 | for (size_t j = 0; j < ends.size(); ++j) |
| 4465 | { |
| 4466 | fprintf(pp, ",%i", ends[j]); |
| 4467 | } |
| 4468 | auto axis = get_node_attr_ai(node, "axis"); |
| 4469 | fprintf(pp, " -23311=%zu", axis.size()); |
| 4470 | for (size_t j = 0; j < axis.size(); ++j) |
| 4471 | { |
| 4472 | fprintf(pp, ",%i", axis[j]); |
| 4473 | } |
| 4474 | } |
| 4475 | else if (op == "DepthToSpace") |
| 4476 | { |
| 4477 | // pixelshuffle |
| 4478 | int scale_factor = get_node_attr_i(node, "blocksize", 1); |
| 4479 | std::string mode = get_node_attr_s(node, "mode"); |
| 4480 | fprintf(pp, " 0=%d", scale_factor); |
| 4481 | if (mode == "CRD") |
| 4482 | { |
| 4483 | fprintf(pp, " 1=0"); |
| 4484 | } |
| 4485 | else if (mode == "DCR") |
| 4486 | { |
| 4487 | fprintf(pp, " 1=1"); |
| 4488 | } |
| 4489 | } |
| 4490 | else if (op == "Div") |
| 4491 | { |
| 4492 | int op_type = 3; |
| 4493 | fprintf(pp, " 0=%d", op_type); |
| 4494 | |
| 4495 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 4496 | float b = get_node_attr_f(node, "b", 0.f); |
| 4497 | if (with_scalar) |
| 4498 | { |
| 4499 | fprintf(pp, " 1=%d", with_scalar); |
| 4500 | fprintf(pp, " 2=%e", b); |
| 4501 | } |
| 4502 | } |
| 4503 | else if (op == "Dropout") |
| 4504 | { |
| 4505 | // no-op |
| 4506 | } |
| 4507 | else if (op == "Elu") |
| 4508 | { |
| 4509 | float alpha = get_node_attr_f(node, "alpha", 1.f); |
| 4510 | fprintf(pp, " 0=%e", alpha); |
| 4511 | } |
| 4512 | else if (op == "EmbedLayerNormalization") |
| 4513 | { |
| 4514 | const onnx::TensorProto& words = weights[node.input(2)]; |
| 4515 | const onnx::TensorProto& positions = weights[node.input(3)]; |
| 4516 | const onnx::TensorProto& W = weights[node.input(5)]; |
| 4517 | const onnx::TensorProto& B = weights[node.input(6)]; |
| 4518 | |
| 4519 | fprintf(pp, " 0=%d", get_tensor_proto_data_size(B)); |
| 4520 | fprintf(pp, " 1=%d", get_tensor_proto_data_size(words)); |
| 4521 | fprintf(pp, " 2=%d", get_tensor_proto_data_size(positions)); |
| 4522 | |
| 4523 | int quantize_tag = 0; |
| 4524 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4525 | |
| 4526 | fwrite_tensor_proto_data(words, bp); |
| 4527 | |
| 4528 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4529 | |
| 4530 | fwrite_tensor_proto_data(positions, bp); |
| 4531 | |
| 4532 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4533 | |
| 4534 | fwrite_tensor_proto_data(W, bp); |
| 4535 | |
| 4536 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4537 | |
| 4538 | fwrite_tensor_proto_data(B, bp); |
| 4539 | } |
| 4540 | else if (op == "Erf") |
| 4541 | { |
| 4542 | // no-op |
| 4543 | } |
| 4544 | else if (op == "Exp") |
| 4545 | { |
| 4546 | int op_type = 7; |
| 4547 | fprintf(pp, " 0=%d", op_type); |
| 4548 | } |
| 4549 | else if (op == "Flatten") |
| 4550 | { |
| 4551 | int axis = get_node_attr_i(node, "axis", 1); |
| 4552 | if (axis != 1) |
| 4553 | { |
| 4554 | fprintf(stderr, "Unsupported Flatten axis %d!\n", axis); |
| 4555 | } |
| 4556 | } |
| 4557 | else if (op == "Floor") |
| 4558 | { |
| 4559 | int op_type = 2; |
| 4560 | fprintf(pp, " 0=%d", op_type); |
| 4561 | } |
| 4562 | else if (op == "Gelu") |
| 4563 | { |
| 4564 | fprintf(pp, " 0=1"); |
| 4565 | } |
| 4566 | else if (op == "Gemm") |
| 4567 | { |
| 4568 | float alpha = get_node_attr_f(node, "alpha", 1.f); |
| 4569 | float beta = get_node_attr_f(node, "beta", 1.f); |
| 4570 | int transA = get_node_attr_i(node, "transA", 0); |
| 4571 | int transB = get_node_attr_i(node, "transB", 0); |
| 4572 | |
| 4573 | if (alpha == 1.f && beta == 1.f && transA == 0 && transB == 1) |
| 4574 | { |
| 4575 | // InnerProduct-like A * B + C |
| 4576 | const onnx::TensorProto& B = weights[node.input(1)]; |
| 4577 | const onnx::TensorProto& C = weights[node.input(2)]; |
| 4578 | |
| 4579 | fprintf(pp, " 0=%d", get_tensor_proto_data_size(C)); |
| 4580 | fprintf(pp, " 1=1"); |
| 4581 | fprintf(pp, " 2=%d", get_tensor_proto_data_size(B)); |
| 4582 | |
| 4583 | int quantize_tag = 0; |
| 4584 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4585 | |
| 4586 | fwrite_tensor_proto_data(B, bp); |
| 4587 | fwrite_tensor_proto_data(C, bp); |
| 4588 | } |
| 4589 | else |
| 4590 | { |
| 4591 | // gemm |
| 4592 | fprintf(pp, " 0=%e", alpha); |
| 4593 | fprintf(pp, " 1=%e", beta); |
| 4594 | fprintf(pp, " 2=%d", transA); |
| 4595 | fprintf(pp, " 3=%d", transB); |
| 4596 | } |
| 4597 | } |
| 4598 | else if (op == "GlobalAveragePool") |
| 4599 | { |
| 4600 | int pool = 1; |
| 4601 | int global_pool = 1; |
| 4602 | |
| 4603 | fprintf(pp, " 0=%d", pool); |
| 4604 | fprintf(pp, " 4=%d", global_pool); |
| 4605 | } |
| 4606 | else if (op == "GlobalMaxPool") |
| 4607 | { |
| 4608 | int pool = 0; |
| 4609 | int global_pool = 1; |
| 4610 | |
| 4611 | fprintf(pp, " 0=%d", pool); |
| 4612 | fprintf(pp, " 4=%d", global_pool); |
| 4613 | } |
| 4614 | else if (op == "adaptive_avg_pool2d" || op == "adaptive_max_pool2d") |
| 4615 | { |
| 4616 | int pool = 0; |
| 4617 | if (op == "adaptive_avg_pool2d") |
| 4618 | { |
| 4619 | pool = 1; |
| 4620 | } |
| 4621 | int adaptive_pooling = 1; |
| 4622 | const onnx::TensorProto& out_shape_tp = weights[node.input(1)]; |
| 4623 | std::vector<int> out_shape = get_node_attr_from_input_ai(out_shape_tp); |
| 4624 | |
| 4625 | fprintf(pp, " 0=%d", pool); |
| 4626 | fprintf(pp, " 7=%d", adaptive_pooling); |
| 4627 | if (out_shape.size() == 1) |
| 4628 | { |
| 4629 | fprintf(pp, " 8=%d", out_shape[0]); |
| 4630 | } |
| 4631 | else if (out_shape.size() == 2) |
| 4632 | { |
| 4633 | // out_w |
| 4634 | fprintf(pp, " 8=%d", out_shape[1]); |
| 4635 | // out_h |
| 4636 | fprintf(pp, " 18=%d", out_shape[0]); |
| 4637 | } |
| 4638 | } |
| 4639 | else if (op == "GroupNorm") |
| 4640 | { |
| 4641 | int groups = get_node_attr_i(node, "groups", 1); |
| 4642 | int channels = get_node_attr_i(node, "channels", 1); |
| 4643 | float eps = get_node_attr_f(node, "epsilon", 1e-5f); |
| 4644 | int affine = get_node_attr_i(node, "affine", 1); |
| 4645 | |
| 4646 | if (affine) |
| 4647 | { |
| 4648 | // discard affine-less S=1 B=0 |
| 4649 | std::vector<float> affine_S = get_node_attr_from_input_af(weights[node.input(1)]); |
| 4650 | std::vector<float> affine_B = get_node_attr_from_input_af(weights[node.input(2)]); |
| 4651 | if (affine_S.size() == 1 && affine_S[0] == 1.f && affine_B.size() == 1 && affine_B[0] == 0.f) |
| 4652 | { |
| 4653 | affine = 0; |
| 4654 | } |
| 4655 | else |
| 4656 | { |
| 4657 | affine = 0; |
| 4658 | { |
| 4659 | for (int j = 0; j < channels; j++) |
| 4660 | { |
| 4661 | if (affine_S[j] != 1.f || affine_B[j] != 0.f) |
| 4662 | { |
| 4663 | affine = 1; |
| 4664 | break; |
| 4665 | } |
| 4666 | } |
| 4667 | } |
| 4668 | } |
| 4669 | } |
| 4670 | |
| 4671 | fprintf(pp, " 0=%d", groups); |
| 4672 | fprintf(pp, " 1=%d", channels); |
| 4673 | fprintf(pp, " 2=%e", eps); |
| 4674 | fprintf(pp, " 3=%d", affine); |
| 4675 | if (affine) |
| 4676 | { |
| 4677 | const onnx::TensorProto& scale = weights[node.input(1)]; |
| 4678 | const onnx::TensorProto& B = weights[node.input(2)]; |
| 4679 | |
| 4680 | fwrite_tensor_proto_data(scale, bp); |
| 4681 | fwrite_tensor_proto_data(B, bp); |
| 4682 | } |
| 4683 | } |
| 4684 | else if (op == "GRU") |
| 4685 | { |
| 4686 | const onnx::TensorProto& W = weights[node.input(1)]; |
| 4687 | const onnx::TensorProto& R = weights[node.input(2)]; |
| 4688 | const onnx::TensorProto& B = weights[node.input(3)]; |
| 4689 | |
| 4690 | int hidden_size = get_node_attr_i(node, "hidden_size", 0); |
| 4691 | std::string direction = get_node_attr_s(node, "direction"); |
| 4692 | |
| 4693 | int direction_type = 0; |
| 4694 | if (direction == "forward") |
| 4695 | { |
| 4696 | direction_type = 0; |
| 4697 | } |
| 4698 | else if (direction == "reverse") |
| 4699 | { |
| 4700 | direction_type = 1; |
| 4701 | } |
| 4702 | else if (direction == "bidirectional") |
| 4703 | { |
| 4704 | direction_type = 2; |
| 4705 | } |
| 4706 | |
| 4707 | int weight_data_size = get_tensor_proto_data_size(W); |
| 4708 | |
| 4709 | fprintf(pp, " 0=%d", hidden_size); |
| 4710 | fprintf(pp, " 1=%d", weight_data_size); |
| 4711 | fprintf(pp, " 2=%d", direction_type); |
| 4712 | |
| 4713 | int num_directions = direction_type == 2 ? 2 : 1; |
| 4714 | |
| 4715 | int quantize_tag = 0; |
| 4716 | |
| 4717 | // reorder num_directions-URN-hidden-size to num_directions-RUN-hidden-size |
| 4718 | { |
| 4719 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4720 | |
| 4721 | int weight_data_size_g = get_tensor_proto_data_size(W) / 3 / num_directions; |
| 4722 | const float* wptr = W.has_raw_data() ? (const float*)W.raw_data().data() : W.float_data().data(); |
| 4723 | |
| 4724 | const float* uptr = wptr; |
| 4725 | const float* rptr = wptr + weight_data_size_g; |
| 4726 | const float* nptr = wptr + weight_data_size_g * 2; |
| 4727 | fwrite(rptr, sizeof(float), weight_data_size_g, bp); |
| 4728 | fwrite(uptr, sizeof(float), weight_data_size_g, bp); |
| 4729 | fwrite(nptr, sizeof(float), weight_data_size_g, bp); |
| 4730 | |
| 4731 | if (direction_type == 2) |
| 4732 | { |
| 4733 | uptr += weight_data_size_g * 3; |
| 4734 | rptr += weight_data_size_g * 3; |
| 4735 | nptr += weight_data_size_g * 3; |
| 4736 | fwrite(rptr, sizeof(float), weight_data_size_g, bp); |
| 4737 | fwrite(uptr, sizeof(float), weight_data_size_g, bp); |
| 4738 | fwrite(nptr, sizeof(float), weight_data_size_g, bp); |
| 4739 | } |
| 4740 | } |
| 4741 | |
| 4742 | // reduce U and R bias except N |
| 4743 | // reorder num_directions-URN-hidden to num_directions-RUN-hidden |
| 4744 | { |
| 4745 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4746 | |
| 4747 | int bias_data_size_g = get_tensor_proto_data_size(B) / 2 / 3 / num_directions; |
| 4748 | const float* bptr = B.has_raw_data() ? (const float*)B.raw_data().data() : B.float_data().data(); |
| 4749 | const float* wuptr = bptr; |
| 4750 | const float* wrptr = bptr + bias_data_size_g; |
| 4751 | const float* wnptr = bptr + bias_data_size_g * 2; |
| 4752 | const float* buptr = bptr + bias_data_size_g * 3; |
| 4753 | const float* brptr = bptr + bias_data_size_g * 4; |
| 4754 | const float* bnptr = bptr + bias_data_size_g * 5; |
| 4755 | |
| 4756 | for (int j = 0; j < bias_data_size_g; j++) |
| 4757 | { |
| 4758 | float vb = wrptr[j] + brptr[j]; |
| 4759 | fwrite(&vb, sizeof(float), 1, bp); |
| 4760 | } |
| 4761 | for (int j = 0; j < bias_data_size_g; j++) |
| 4762 | { |
| 4763 | float vb = wuptr[j] + buptr[j]; |
| 4764 | fwrite(&vb, sizeof(float), 1, bp); |
| 4765 | } |
| 4766 | fwrite(wnptr, sizeof(float), bias_data_size_g, bp); |
| 4767 | fwrite(bnptr, sizeof(float), bias_data_size_g, bp); |
| 4768 | |
| 4769 | if (direction_type == 2) |
| 4770 | { |
| 4771 | wuptr += bias_data_size_g * 6; |
| 4772 | wrptr += bias_data_size_g * 6; |
| 4773 | wnptr += bias_data_size_g * 6; |
| 4774 | buptr += bias_data_size_g * 6; |
| 4775 | brptr += bias_data_size_g * 6; |
| 4776 | bnptr += bias_data_size_g * 6; |
| 4777 | |
| 4778 | for (int j = 0; j < bias_data_size_g; j++) |
| 4779 | { |
| 4780 | float vb = wrptr[j] + brptr[j]; |
| 4781 | fwrite(&vb, sizeof(float), 1, bp); |
| 4782 | } |
| 4783 | for (int j = 0; j < bias_data_size_g; j++) |
| 4784 | { |
| 4785 | float vb = wuptr[j] + buptr[j]; |
| 4786 | fwrite(&vb, sizeof(float), 1, bp); |
| 4787 | } |
| 4788 | fwrite(wnptr, sizeof(float), bias_data_size_g, bp); |
| 4789 | fwrite(bnptr, sizeof(float), bias_data_size_g, bp); |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | // reorder num_directions-URN-hidden-hidden to num_directions-RUN-hidden-hidden |
| 4794 | { |
| 4795 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4796 | |
| 4797 | int weight_data_size_g = get_tensor_proto_data_size(R) / 3 / num_directions; |
| 4798 | const float* Rptr = R.has_raw_data() ? (const float*)R.raw_data().data() : R.float_data().data(); |
| 4799 | |
| 4800 | const float* uptr = Rptr; |
| 4801 | const float* rptr = Rptr + weight_data_size_g; |
| 4802 | const float* nptr = Rptr + weight_data_size_g * 2; |
| 4803 | fwrite(rptr, sizeof(float), weight_data_size_g, bp); |
| 4804 | fwrite(uptr, sizeof(float), weight_data_size_g, bp); |
| 4805 | fwrite(nptr, sizeof(float), weight_data_size_g, bp); |
| 4806 | |
| 4807 | if (direction_type == 2) |
| 4808 | { |
| 4809 | uptr += weight_data_size_g * 3; |
| 4810 | rptr += weight_data_size_g * 3; |
| 4811 | nptr += weight_data_size_g * 3; |
| 4812 | fwrite(rptr, sizeof(float), weight_data_size_g, bp); |
| 4813 | fwrite(uptr, sizeof(float), weight_data_size_g, bp); |
| 4814 | fwrite(nptr, sizeof(float), weight_data_size_g, bp); |
| 4815 | } |
| 4816 | } |
| 4817 | } |
| 4818 | else if (op == "HardSigmoid") |
| 4819 | { |
| 4820 | float alpha = get_node_attr_f(node, "alpha", 0.2f); |
| 4821 | float beta = get_node_attr_f(node, "beta", 0.5f); |
| 4822 | |
| 4823 | fprintf(pp, " 0=%e", alpha); |
| 4824 | fprintf(pp, " 1=%e", beta); |
| 4825 | } |
| 4826 | else if (op == "HardSwish") |
| 4827 | { |
| 4828 | float alpha = get_node_attr_f(node, "alpha", 0.2f); |
| 4829 | float beta = get_node_attr_f(node, "beta", 0.5f); |
| 4830 | |
| 4831 | fprintf(pp, " 0=%e", alpha); |
| 4832 | fprintf(pp, " 1=%e", beta); |
| 4833 | } |
| 4834 | else if (op == "ImageScaler") |
| 4835 | { |
| 4836 | std::vector<float> bias = get_node_attr_af(node, "bias"); |
| 4837 | float scale = get_node_attr_f(node, "scale", 1.f); |
| 4838 | |
| 4839 | int channels = (int)bias.size(); |
| 4840 | |
| 4841 | fprintf(pp, " 0=%d", channels); |
| 4842 | fprintf(pp, " 1=1"); |
| 4843 | |
| 4844 | for (int j = 0; j < channels; j++) |
| 4845 | { |
| 4846 | fwrite(&scale, sizeof(float), 1, bp); |
| 4847 | } |
| 4848 | fwrite(&bias[0], sizeof(float), channels, bp); |
| 4849 | } |
| 4850 | else if (op == "InstanceNormalization") |
| 4851 | { |
| 4852 | float eps = get_node_attr_f(node, "epsilon", 1e-5f); |
| 4853 | |
| 4854 | // discard affine-less S=1 B=0 |
| 4855 | std::vector<float> affine_S = get_node_attr_from_input_af(weights[node.input(1)]); |
| 4856 | std::vector<float> affine_B = get_node_attr_from_input_af(weights[node.input(2)]); |
| 4857 | int channels = (int)affine_S.size(); |
| 4858 | int affine = 0; |
| 4859 | { |
| 4860 | for (int j = 0; j < channels; j++) |
| 4861 | { |
| 4862 | if (affine_S[j] != 1.f || affine_B[j] != 0.f) |
| 4863 | { |
| 4864 | affine = 1; |
| 4865 | break; |
| 4866 | } |
| 4867 | } |
| 4868 | } |
| 4869 | |
| 4870 | fprintf(pp, " 0=%d", channels); |
| 4871 | fprintf(pp, " 1=%e", eps); |
| 4872 | fprintf(pp, " 2=%d", affine); |
| 4873 | if (affine) |
| 4874 | { |
| 4875 | const onnx::TensorProto& scale = weights[node.input(1)]; |
| 4876 | const onnx::TensorProto& B = weights[node.input(2)]; |
| 4877 | |
| 4878 | fwrite_tensor_proto_data(scale, bp); |
| 4879 | fwrite_tensor_proto_data(B, bp); |
| 4880 | } |
| 4881 | } |
| 4882 | else if (op == "LayerNorm") |
| 4883 | { |
| 4884 | float eps = get_node_attr_f(node, "epsilon", 1e-5f); |
| 4885 | int affine = get_node_attr_i(node, "affine", 1); |
| 4886 | |
| 4887 | if (affine) |
| 4888 | { |
| 4889 | // discard affine-less S=1 B=0 |
| 4890 | std::vector<float> affine_S = get_node_attr_from_input_af(weights[node.input(1)]); |
| 4891 | std::vector<float> affine_B = get_node_attr_from_input_af(weights[node.input(2)]); |
| 4892 | int affine_size = (int)affine_S.size(); |
| 4893 | affine = 0; |
| 4894 | { |
| 4895 | for (int j = 0; j < affine_size; j++) |
| 4896 | { |
| 4897 | if (affine_S[j] != 1.f || affine_B[j] != 0.f) |
| 4898 | { |
| 4899 | affine = 1; |
| 4900 | break; |
| 4901 | } |
| 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | if (affine) |
| 4906 | { |
| 4907 | fprintf(pp, " 0=%d", affine_size); |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | fprintf(pp, " 1=%e", eps); |
| 4912 | fprintf(pp, " 2=%d", affine); |
| 4913 | |
| 4914 | if (affine) |
| 4915 | { |
| 4916 | const onnx::TensorProto& scale = weights[node.input(1)]; |
| 4917 | const onnx::TensorProto& B = weights[node.input(2)]; |
| 4918 | |
| 4919 | fwrite_tensor_proto_data(scale, bp); |
| 4920 | fwrite_tensor_proto_data(B, bp); |
| 4921 | } |
| 4922 | } |
| 4923 | else if (op == "LeakyRelu") |
| 4924 | { |
| 4925 | float alpha = get_node_attr_f(node, "alpha", 0.01f); |
| 4926 | |
| 4927 | fprintf(pp, " 0=%e", alpha); |
| 4928 | } |
| 4929 | else if (op == "Log") |
| 4930 | { |
| 4931 | int op_type = 8; |
| 4932 | fprintf(pp, " 0=%d", op_type); |
| 4933 | } |
| 4934 | else if (op == "LRN") |
| 4935 | { |
| 4936 | float alpha = get_node_attr_f(node, "alpha", 1.f); |
| 4937 | float beta = get_node_attr_f(node, "beta", 0.5f); |
| 4938 | float bias = get_node_attr_f(node, "bias", 1.f); |
| 4939 | int size = get_node_attr_i(node, "size", 1); |
| 4940 | |
| 4941 | int norm_region = 0; |
| 4942 | |
| 4943 | fprintf(pp, " 0=%d", norm_region); |
| 4944 | fprintf(pp, " 1=%d", size); |
| 4945 | fprintf(pp, " 2=%e", alpha); |
| 4946 | fprintf(pp, " 3=%e", beta); |
| 4947 | fprintf(pp, " 4=%e", bias); |
| 4948 | } |
| 4949 | else if (op == "LSTM") |
| 4950 | { |
| 4951 | const onnx::TensorProto& W = weights[node.input(1)]; |
| 4952 | const onnx::TensorProto& R = weights[node.input(2)]; |
| 4953 | const onnx::TensorProto& B = weights[node.input(3)]; |
| 4954 | |
| 4955 | int hidden_size = get_node_attr_i(node, "hidden_size", 0); |
| 4956 | std::string direction = get_node_attr_s(node, "direction"); |
| 4957 | |
| 4958 | int direction_type = 0; |
| 4959 | if (direction == "forward") |
| 4960 | { |
| 4961 | direction_type = 0; |
| 4962 | } |
| 4963 | else if (direction == "reverse") |
| 4964 | { |
| 4965 | direction_type = 1; |
| 4966 | } |
| 4967 | else if (direction == "bidirectional") |
| 4968 | { |
| 4969 | direction_type = 2; |
| 4970 | } |
| 4971 | |
| 4972 | int weight_data_size = get_tensor_proto_data_size(W); |
| 4973 | |
| 4974 | fprintf(pp, " 0=%d", hidden_size); |
| 4975 | fprintf(pp, " 1=%d", weight_data_size); |
| 4976 | fprintf(pp, " 2=%d", direction_type); |
| 4977 | |
| 4978 | int num_directions = direction_type == 2 ? 2 : 1; |
| 4979 | |
| 4980 | int quantize_tag = 0; |
| 4981 | |
| 4982 | // reorder num_directions-IOFG-hidden-size to num_directions-IFOG-hidden-size |
| 4983 | { |
| 4984 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 4985 | |
| 4986 | int weight_data_size_g = get_tensor_proto_data_size(W) / 4 / num_directions; |
| 4987 | const float* wptr = W.has_raw_data() ? (const float*)W.raw_data().data() : W.float_data().data(); |
| 4988 | |
| 4989 | const float* iptr = wptr; |
| 4990 | const float* optr = wptr + weight_data_size_g; |
| 4991 | const float* fptr = wptr + weight_data_size_g * 2; |
| 4992 | const float* gptr = wptr + weight_data_size_g * 3; |
| 4993 | fwrite(iptr, sizeof(float), weight_data_size_g, bp); |
| 4994 | fwrite(fptr, sizeof(float), weight_data_size_g, bp); |
| 4995 | fwrite(optr, sizeof(float), weight_data_size_g, bp); |
| 4996 | fwrite(gptr, sizeof(float), weight_data_size_g, bp); |
| 4997 | |
| 4998 | if (direction_type == 2) |
| 4999 | { |
| 5000 | iptr += weight_data_size_g * 4; |
| 5001 | optr += weight_data_size_g * 4; |
| 5002 | fptr += weight_data_size_g * 4; |
| 5003 | gptr += weight_data_size_g * 4; |
| 5004 | fwrite(iptr, sizeof(float), weight_data_size_g, bp); |
| 5005 | fwrite(fptr, sizeof(float), weight_data_size_g, bp); |
| 5006 | fwrite(optr, sizeof(float), weight_data_size_g, bp); |
| 5007 | fwrite(gptr, sizeof(float), weight_data_size_g, bp); |
| 5008 | } |
| 5009 | } |
| 5010 | |
| 5011 | // reduce xc and hc bias |
| 5012 | // reorder num_directions-IOFG-hidden to num_directions-IFOG-hidden |
| 5013 | { |
| 5014 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5015 | |
| 5016 | int bias_data_size_g = get_tensor_proto_data_size(B) / 2 / 4 / num_directions; |
| 5017 | const float* xcbptr = B.has_raw_data() ? (const float*)B.raw_data().data() : B.float_data().data(); |
| 5018 | const float* xiptr = xcbptr; |
| 5019 | const float* xoptr = xcbptr + bias_data_size_g; |
| 5020 | const float* xfptr = xcbptr + bias_data_size_g * 2; |
| 5021 | const float* xgptr = xcbptr + bias_data_size_g * 3; |
| 5022 | const float* hiptr = xcbptr + bias_data_size_g * 4; |
| 5023 | const float* hoptr = xcbptr + bias_data_size_g * 5; |
| 5024 | const float* hfptr = xcbptr + bias_data_size_g * 6; |
| 5025 | const float* hgptr = xcbptr + bias_data_size_g * 7; |
| 5026 | |
| 5027 | for (int j = 0; j < bias_data_size_g; j++) |
| 5028 | { |
| 5029 | float vb = xiptr[j] + hiptr[j]; |
| 5030 | fwrite(&vb, sizeof(float), 1, bp); |
| 5031 | } |
| 5032 | for (int j = 0; j < bias_data_size_g; j++) |
| 5033 | { |
| 5034 | float vb = xfptr[j] + hfptr[j]; |
| 5035 | fwrite(&vb, sizeof(float), 1, bp); |
| 5036 | } |
| 5037 | for (int j = 0; j < bias_data_size_g; j++) |
| 5038 | { |
| 5039 | float vb = xoptr[j] + hoptr[j]; |
| 5040 | fwrite(&vb, sizeof(float), 1, bp); |
| 5041 | } |
| 5042 | for (int j = 0; j < bias_data_size_g; j++) |
| 5043 | { |
| 5044 | float vb = xgptr[j] + hgptr[j]; |
| 5045 | fwrite(&vb, sizeof(float), 1, bp); |
| 5046 | } |
| 5047 | |
| 5048 | if (direction_type == 2) |
| 5049 | { |
| 5050 | xiptr += bias_data_size_g * 8; |
| 5051 | xoptr += bias_data_size_g * 8; |
| 5052 | xfptr += bias_data_size_g * 8; |
| 5053 | xgptr += bias_data_size_g * 8; |
| 5054 | hiptr += bias_data_size_g * 8; |
| 5055 | hoptr += bias_data_size_g * 8; |
| 5056 | hfptr += bias_data_size_g * 8; |
| 5057 | hgptr += bias_data_size_g * 8; |
| 5058 | |
| 5059 | for (int j = 0; j < bias_data_size_g; j++) |
| 5060 | { |
| 5061 | float vb = xiptr[j] + hiptr[j]; |
| 5062 | fwrite(&vb, sizeof(float), 1, bp); |
| 5063 | } |
| 5064 | for (int j = 0; j < bias_data_size_g; j++) |
| 5065 | { |
| 5066 | float vb = xfptr[j] + hfptr[j]; |
| 5067 | fwrite(&vb, sizeof(float), 1, bp); |
| 5068 | } |
| 5069 | for (int j = 0; j < bias_data_size_g; j++) |
| 5070 | { |
| 5071 | float vb = xoptr[j] + hoptr[j]; |
| 5072 | fwrite(&vb, sizeof(float), 1, bp); |
| 5073 | } |
| 5074 | for (int j = 0; j < bias_data_size_g; j++) |
| 5075 | { |
| 5076 | float vb = xgptr[j] + hgptr[j]; |
| 5077 | fwrite(&vb, sizeof(float), 1, bp); |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | // reorder num_directions-IOFG-hidden-hidden to num_directions-IFOG-hidden-hidden |
| 5083 | { |
| 5084 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5085 | |
| 5086 | int weight_data_size_g = get_tensor_proto_data_size(R) / 4 / num_directions; |
| 5087 | const float* rptr = R.has_raw_data() ? (const float*)R.raw_data().data() : R.float_data().data(); |
| 5088 | |
| 5089 | const float* iptr = rptr; |
| 5090 | const float* optr = rptr + weight_data_size_g; |
| 5091 | const float* fptr = rptr + weight_data_size_g * 2; |
| 5092 | const float* gptr = rptr + weight_data_size_g * 3; |
| 5093 | fwrite(iptr, sizeof(float), weight_data_size_g, bp); |
| 5094 | fwrite(fptr, sizeof(float), weight_data_size_g, bp); |
| 5095 | fwrite(optr, sizeof(float), weight_data_size_g, bp); |
| 5096 | fwrite(gptr, sizeof(float), weight_data_size_g, bp); |
| 5097 | |
| 5098 | if (direction_type == 2) |
| 5099 | { |
| 5100 | iptr += weight_data_size_g * 4; |
| 5101 | optr += weight_data_size_g * 4; |
| 5102 | fptr += weight_data_size_g * 4; |
| 5103 | gptr += weight_data_size_g * 4; |
| 5104 | fwrite(iptr, sizeof(float), weight_data_size_g, bp); |
| 5105 | fwrite(fptr, sizeof(float), weight_data_size_g, bp); |
| 5106 | fwrite(optr, sizeof(float), weight_data_size_g, bp); |
| 5107 | fwrite(gptr, sizeof(float), weight_data_size_g, bp); |
| 5108 | } |
| 5109 | } |
| 5110 | } |
| 5111 | else if (op == "MatMul") |
| 5112 | { |
| 5113 | if (weights.find(node.input(1)) != weights.end() && weights[node.input(1)].dims_size() == 2) |
| 5114 | { |
| 5115 | // InnerProduct |
| 5116 | const onnx::TensorProto& B = weights[node.input(1)]; |
| 5117 | |
| 5118 | int weight_data_size = get_tensor_proto_data_size(B); |
| 5119 | |
| 5120 | int num_output = B.dims(B.dims_size() - 1); |
| 5121 | int num_input = weight_data_size / num_output; |
| 5122 | |
| 5123 | fprintf(pp, " 0=%d", num_output); |
| 5124 | fprintf(pp, " 1=0"); |
| 5125 | fprintf(pp, " 2=%d", weight_data_size); |
| 5126 | |
| 5127 | int quantize_tag = 0; |
| 5128 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5129 | |
| 5130 | // reorder num_input-num_output to num_output-num_input |
| 5131 | { |
| 5132 | const float* bptr = B.has_raw_data() ? (const float*)B.raw_data().data() : B.float_data().data(); |
| 5133 | |
| 5134 | for (int j = 0; j < num_output; j++) |
| 5135 | { |
| 5136 | for (int k = 0; k < num_input; k++) |
| 5137 | { |
| 5138 | float vb = bptr[k * num_output + j]; |
| 5139 | fwrite(&vb, sizeof(float), 1, bp); |
| 5140 | } |
| 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | // fwrite_tensor_proto_data(B, bp) |
| 5145 | } |
| 5146 | else |
| 5147 | { |
| 5148 | // default matrix multiplication |
| 5149 | } |
| 5150 | } |
| 5151 | else if (op == "Max") |
| 5152 | { |
| 5153 | int op_type = 4; |
| 5154 | fprintf(pp, " 0=%d", op_type); |
| 5155 | |
| 5156 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5157 | float b = get_node_attr_f(node, "b", 0.f); |
| 5158 | if (with_scalar) |
| 5159 | { |
| 5160 | fprintf(pp, " 1=%d", with_scalar); |
| 5161 | fprintf(pp, " 2=%e", b); |
| 5162 | } |
| 5163 | } |
| 5164 | else if (op == "Min") |
| 5165 | { |
| 5166 | int op_type = 5; |
| 5167 | fprintf(pp, " 0=%d", op_type); |
| 5168 | |
| 5169 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5170 | float b = get_node_attr_f(node, "b", 0.f); |
| 5171 | if (with_scalar) |
| 5172 | { |
| 5173 | fprintf(pp, " 1=%d", with_scalar); |
| 5174 | fprintf(pp, " 2=%e", b); |
| 5175 | } |
| 5176 | } |
| 5177 | else if (op == "Mul") |
| 5178 | { |
| 5179 | int op_type = 2; |
| 5180 | fprintf(pp, " 0=%d", op_type); |
| 5181 | |
| 5182 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5183 | float b = get_node_attr_f(node, "b", 0.f); |
| 5184 | if (with_scalar) |
| 5185 | { |
| 5186 | fprintf(pp, " 1=%d", with_scalar); |
| 5187 | fprintf(pp, " 2=%e", b); |
| 5188 | } |
| 5189 | } |
| 5190 | else if (op == "MultiHeadAttention") |
| 5191 | { |
| 5192 | int embed_dim = get_node_attr_i(node, "embed_dim", 0); |
| 5193 | int num_heads = get_node_attr_i(node, "num_heads", 0); |
| 5194 | |
| 5195 | fprintf(pp, " 0=%d", embed_dim); |
| 5196 | fprintf(pp, " 1=%d", num_heads); |
| 5197 | |
| 5198 | if (node.input_size() == 5) |
| 5199 | { |
| 5200 | const onnx::TensorProto& qkvw = weights[node.input(1)]; |
| 5201 | const onnx::TensorProto& qkvb = weights[node.input(2)]; |
| 5202 | const onnx::TensorProto& ow = weights[node.input(3)]; |
| 5203 | const onnx::TensorProto& ob = weights[node.input(4)]; |
| 5204 | |
| 5205 | int weight_data_size = get_tensor_proto_data_size(ow); |
| 5206 | |
| 5207 | fprintf(pp, " 2=%d", weight_data_size); |
| 5208 | |
| 5209 | int quantize_tag = 0; |
| 5210 | |
| 5211 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5212 | // transpose qw |
| 5213 | { |
| 5214 | const float* wptr = qkvw.has_raw_data() ? (const float*)qkvw.raw_data().data() : qkvw.float_data().data(); |
| 5215 | const float* bptr = qkvb.has_raw_data() ? (const float*)qkvb.raw_data().data() : qkvb.float_data().data(); |
| 5216 | |
| 5217 | for (int j = 0; j < embed_dim; j++) |
| 5218 | { |
| 5219 | for (int k = 0; k < embed_dim; k++) |
| 5220 | { |
| 5221 | float vb = wptr[k * embed_dim * 3 + j]; |
| 5222 | fwrite(&vb, sizeof(float), 1, bp); |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | fwrite(bptr, sizeof(float), embed_dim, bp); |
| 5227 | } |
| 5228 | |
| 5229 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5230 | // transpose kw |
| 5231 | { |
| 5232 | const float* wptr = qkvw.has_raw_data() ? (const float*)qkvw.raw_data().data() : qkvw.float_data().data(); |
| 5233 | const float* bptr = qkvb.has_raw_data() ? (const float*)qkvb.raw_data().data() : qkvb.float_data().data(); |
| 5234 | bptr += embed_dim; |
| 5235 | |
| 5236 | for (int j = 0; j < embed_dim; j++) |
| 5237 | { |
| 5238 | for (int k = 0; k < embed_dim; k++) |
| 5239 | { |
| 5240 | float vb = wptr[k * embed_dim * 3 + j + embed_dim]; |
| 5241 | fwrite(&vb, sizeof(float), 1, bp); |
| 5242 | } |
| 5243 | } |
| 5244 | |
| 5245 | fwrite(bptr, sizeof(float), embed_dim, bp); |
| 5246 | } |
| 5247 | |
| 5248 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5249 | // transpose vw |
| 5250 | { |
| 5251 | const float* wptr = qkvw.has_raw_data() ? (const float*)qkvw.raw_data().data() : qkvw.float_data().data(); |
| 5252 | const float* bptr = qkvb.has_raw_data() ? (const float*)qkvb.raw_data().data() : qkvb.float_data().data(); |
| 5253 | bptr += embed_dim * 2; |
| 5254 | |
| 5255 | for (int j = 0; j < embed_dim; j++) |
| 5256 | { |
| 5257 | for (int k = 0; k < embed_dim; k++) |
| 5258 | { |
| 5259 | float vb = wptr[k * embed_dim * 3 + j + embed_dim * 2]; |
| 5260 | fwrite(&vb, sizeof(float), 1, bp); |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | fwrite(bptr, sizeof(float), embed_dim, bp); |
| 5265 | } |
| 5266 | |
| 5267 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5268 | // transpose ow |
| 5269 | { |
| 5270 | const float* wptr = ow.has_raw_data() ? (const float*)ow.raw_data().data() : ow.float_data().data(); |
| 5271 | |
| 5272 | for (int j = 0; j < embed_dim; j++) |
| 5273 | { |
| 5274 | for (int k = 0; k < embed_dim; k++) |
| 5275 | { |
| 5276 | float vb = wptr[k * embed_dim + j]; |
| 5277 | fwrite(&vb, sizeof(float), 1, bp); |
| 5278 | } |
| 5279 | } |
| 5280 | } |
| 5281 | fwrite_tensor_proto_data(ob, bp); |
| 5282 | } |
| 5283 | else |
| 5284 | { |
| 5285 | const onnx::TensorProto& qw = weights[node.input(3)]; |
| 5286 | const onnx::TensorProto& qb = weights[node.input(4)]; |
| 5287 | const onnx::TensorProto& kw = weights[node.input(5)]; |
| 5288 | const onnx::TensorProto& kb = weights[node.input(6)]; |
| 5289 | const onnx::TensorProto& vw = weights[node.input(7)]; |
| 5290 | const onnx::TensorProto& vb = weights[node.input(8)]; |
| 5291 | const onnx::TensorProto& ow = weights[node.input(9)]; |
| 5292 | const onnx::TensorProto& ob = weights[node.input(10)]; |
| 5293 | |
| 5294 | int weight_data_size = get_tensor_proto_data_size(qw); |
| 5295 | |
| 5296 | fprintf(pp, " 2=%d", weight_data_size); |
| 5297 | |
| 5298 | int quantize_tag = 0; |
| 5299 | |
| 5300 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5301 | // transpose qw |
| 5302 | { |
| 5303 | const float* wptr = qw.has_raw_data() ? (const float*)qw.raw_data().data() : qw.float_data().data(); |
| 5304 | |
| 5305 | for (int j = 0; j < embed_dim; j++) |
| 5306 | { |
| 5307 | for (int k = 0; k < embed_dim; k++) |
| 5308 | { |
| 5309 | float vb = wptr[k * embed_dim + j]; |
| 5310 | fwrite(&vb, sizeof(float), 1, bp); |
| 5311 | } |
| 5312 | } |
| 5313 | } |
| 5314 | fwrite_tensor_proto_data(qb, bp); |
| 5315 | |
| 5316 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5317 | // transpose kw |
| 5318 | { |
| 5319 | const float* wptr = kw.has_raw_data() ? (const float*)kw.raw_data().data() : kw.float_data().data(); |
| 5320 | |
| 5321 | for (int j = 0; j < embed_dim; j++) |
| 5322 | { |
| 5323 | for (int k = 0; k < embed_dim; k++) |
| 5324 | { |
| 5325 | float vb = wptr[k * embed_dim + j]; |
| 5326 | fwrite(&vb, sizeof(float), 1, bp); |
| 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | fwrite_tensor_proto_data(kb, bp); |
| 5331 | |
| 5332 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5333 | // transpose vw |
| 5334 | { |
| 5335 | const float* wptr = vw.has_raw_data() ? (const float*)vw.raw_data().data() : vw.float_data().data(); |
| 5336 | |
| 5337 | for (int j = 0; j < embed_dim; j++) |
| 5338 | { |
| 5339 | for (int k = 0; k < embed_dim; k++) |
| 5340 | { |
| 5341 | float vb = wptr[k * embed_dim + j]; |
| 5342 | fwrite(&vb, sizeof(float), 1, bp); |
| 5343 | } |
| 5344 | } |
| 5345 | } |
| 5346 | fwrite_tensor_proto_data(vb, bp); |
| 5347 | |
| 5348 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5349 | // transpose ow |
| 5350 | { |
| 5351 | const float* wptr = ow.has_raw_data() ? (const float*)ow.raw_data().data() : ow.float_data().data(); |
| 5352 | |
| 5353 | for (int j = 0; j < embed_dim; j++) |
| 5354 | { |
| 5355 | for (int k = 0; k < embed_dim; k++) |
| 5356 | { |
| 5357 | float vb = wptr[k * embed_dim + j]; |
| 5358 | fwrite(&vb, sizeof(float), 1, bp); |
| 5359 | } |
| 5360 | } |
| 5361 | } |
| 5362 | fwrite_tensor_proto_data(ob, bp); |
| 5363 | } |
| 5364 | } |
| 5365 | else if (op == "Neg") |
| 5366 | { |
| 5367 | int op_type = 1; |
| 5368 | fprintf(pp, " 0=%d", op_type); |
| 5369 | } |
| 5370 | else if (op == "Normalize") |
| 5371 | { |
| 5372 | float eps = get_node_attr_f(node, "eps", 0.f); |
| 5373 | int scale_data_size = 1; |
| 5374 | |
| 5375 | fprintf(pp, " 1=1"); // channel_shared |
| 5376 | fprintf(pp, " 2=%e", eps); |
| 5377 | fprintf(pp, " 3=%d", scale_data_size); |
| 5378 | fprintf(pp, " 9=1"); // TODO hardcode pytorch style |
| 5379 | |
| 5380 | const float scale_data[1] = {1.f}; |
| 5381 | fwrite(scale_data, sizeof(float), 1, bp); |
| 5382 | } |
| 5383 | else if (op == "Pad") |
| 5384 | { |
| 5385 | std::string mode = get_node_attr_s(node, "mode"); |
| 5386 | float value = get_node_attr_f(node, "value", 0.f); |
| 5387 | |
| 5388 | std::vector<int> pads; |
| 5389 | if (node.input_size() == 1) |
| 5390 | { |
| 5391 | pads = get_node_attr_ai(node, "pads"); |
| 5392 | } |
| 5393 | else |
| 5394 | { |
| 5395 | pads = get_node_attr_from_input_ai(weights[node.input(1)]); |
| 5396 | } |
| 5397 | |
| 5398 | int type = 0; |
| 5399 | if (mode == "constant") |
| 5400 | { |
| 5401 | type = 0; |
| 5402 | } |
| 5403 | else if (mode == "edge") |
| 5404 | { |
| 5405 | type = 1; |
| 5406 | } |
| 5407 | else if (mode == "reflect") |
| 5408 | { |
| 5409 | type = 2; |
| 5410 | } |
| 5411 | |
| 5412 | int pad_size = (int)pads.size(); |
| 5413 | int top = 0; |
| 5414 | int bottom = 0; |
| 5415 | int left = 0; |
| 5416 | int right = 0; |
| 5417 | int front = 0; |
| 5418 | int behind = 0; |
| 5419 | if (pad_size == 8) |
| 5420 | { |
| 5421 | //NCHW |
| 5422 | top = pads[2]; |
| 5423 | bottom = pads[6]; |
| 5424 | left = pads[3]; |
| 5425 | right = pads[7]; |
| 5426 | front = pads[1]; |
| 5427 | behind = pads[5]; |
| 5428 | } |
| 5429 | else if (pad_size == 6) |
| 5430 | { |
| 5431 | //NHW |
| 5432 | top = pads[1]; |
| 5433 | bottom = pads[4]; |
| 5434 | left = pads[2]; |
| 5435 | right = pads[5]; |
| 5436 | } |
| 5437 | else |
| 5438 | { |
| 5439 | //NW |
| 5440 | left = pads[1]; |
| 5441 | right = pads[3]; |
| 5442 | } |
| 5443 | |
| 5444 | fprintf(pp, " 0=%d", top); |
| 5445 | fprintf(pp, " 1=%d", bottom); |
| 5446 | fprintf(pp, " 2=%d", left); |
| 5447 | fprintf(pp, " 3=%d", right); |
| 5448 | fprintf(pp, " 4=%d", type); |
| 5449 | fprintf(pp, " 5=%e", value); |
| 5450 | fprintf(pp, " 7=%d", front); |
| 5451 | fprintf(pp, " 8=%d", behind); |
| 5452 | } |
| 5453 | else if (op == "Pow") |
| 5454 | { |
| 5455 | int op_type = 6; |
| 5456 | fprintf(pp, " 0=%d", op_type); |
| 5457 | |
| 5458 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5459 | float b = get_node_attr_f(node, "b", 0.f); |
| 5460 | if (with_scalar) |
| 5461 | { |
| 5462 | fprintf(pp, " 1=%d", with_scalar); |
| 5463 | fprintf(pp, " 2=%e", b); |
| 5464 | } |
| 5465 | } |
| 5466 | else if (op == "PixelShuffle") |
| 5467 | { |
| 5468 | int scale_factor = get_node_attr_i(node, "scale_factor", 1); |
| 5469 | fprintf(pp, " 0=%d", scale_factor); |
| 5470 | } |
| 5471 | else if (op == "PRelu") |
| 5472 | { |
| 5473 | const onnx::TensorProto& slope = weights[node.input(1)]; |
| 5474 | |
| 5475 | int num_slope = get_tensor_proto_data_size(slope); |
| 5476 | |
| 5477 | fprintf(pp, " 0=%d", num_slope); |
| 5478 | |
| 5479 | fwrite_tensor_proto_data(slope, bp); |
| 5480 | } |
| 5481 | else if (op == "Reciprocal") |
| 5482 | { |
| 5483 | int op_type = 15; |
| 5484 | fprintf(pp, " 0=%d", op_type); |
| 5485 | } |
| 5486 | else if (op == "ReduceMax" || op == "ReduceMin" || op == "ReduceMean" || op == "ReduceProd" || op == "ReduceSum" || op == "ReduceSumSquare" || op == "ReduceL1" || op == "ReduceL2" || op == "ReduceLogSum" || op == "ReduceLogSumExp") |
| 5487 | { |
| 5488 | int op_type = -233; |
| 5489 | if (op == "ReduceSum") |
| 5490 | op_type = 0; |
| 5491 | else if (op == "ReduceSumSquare") |
| 5492 | op_type = 2; |
| 5493 | else if (op == "ReduceMean") |
| 5494 | op_type = 3; |
| 5495 | else if (op == "ReduceMax") |
| 5496 | op_type = 4; |
| 5497 | else if (op == "ReduceMin") |
| 5498 | op_type = 5; |
| 5499 | else if (op == "ReduceProd") |
| 5500 | op_type = 6; |
| 5501 | else if (op == "ReduceL1") |
| 5502 | op_type = 7; |
| 5503 | else if (op == "ReduceL2") |
| 5504 | op_type = 8; |
| 5505 | else if (op == "ReduceLogSum") |
| 5506 | op_type = 9; |
| 5507 | else if (op == "ReduceLogSumExp") |
| 5508 | op_type = 10; |
| 5509 | fprintf(pp, " 0=%d", op_type); |
| 5510 | |
| 5511 | std::vector<int> axes = get_node_attr_ai(node, "axes"); |
| 5512 | int keepdims = get_node_attr_i(node, "keepdims", 1); |
| 5513 | |
| 5514 | if (axes.size() > 0) |
| 5515 | { |
| 5516 | // if axes set, reduce according to axes |
| 5517 | fprintf(pp, " 1=%d", 0); |
| 5518 | fprintf(pp, " -23303=%zu", axes.size()); |
| 5519 | for (size_t j = 0; j < axes.size(); j++) |
| 5520 | { |
| 5521 | if (axes[j] == 0 || axes[j] > 4 || axes[j] < -3) |
| 5522 | fprintf(stderr, "Unsupported reduction axes !\n"); |
| 5523 | fprintf(pp, ",%d", axes[j] > 0 ? axes[j] - 1 : axes[j]); |
| 5524 | } |
| 5525 | } |
| 5526 | else |
| 5527 | { |
| 5528 | // if axes not set, reduce all axes by default |
| 5529 | fprintf(pp, " 1=%d", 1); |
| 5530 | } |
| 5531 | fprintf(pp, " 4=%d", keepdims); |
| 5532 | fprintf(pp, " 5=1"); |
| 5533 | } |
| 5534 | else if (op == "Reorg") |
| 5535 | { |
| 5536 | int stride = get_node_attr_i(node, "stride", 1); |
| 5537 | fprintf(pp, " 0=%d", stride); |
| 5538 | } |
| 5539 | else if (op == "Reshape") |
| 5540 | { |
| 5541 | std::vector<int> shape; |
| 5542 | |
| 5543 | if (node.input_size() == 1) |
| 5544 | { |
| 5545 | shape = get_node_attr_ai(node, "shape"); |
| 5546 | } |
| 5547 | else |
| 5548 | { |
| 5549 | shape = get_node_attr_from_input_ai(weights[node.input(1)]); |
| 5550 | } |
| 5551 | |
| 5552 | if (shape.size() == 1) |
| 5553 | { |
| 5554 | fprintf(pp, " 0=%d", shape[0]); // should never reach here |
| 5555 | } |
| 5556 | else if (shape.size() == 2) |
| 5557 | { |
| 5558 | fprintf(pp, " 0=%d", shape[1]); |
| 5559 | } |
| 5560 | else if (shape.size() == 3) |
| 5561 | { |
| 5562 | fprintf(pp, " 0=%d", shape[2]); |
| 5563 | fprintf(pp, " 1=%d", shape[1]); |
| 5564 | } |
| 5565 | else if (shape.size() == 4) |
| 5566 | { |
| 5567 | fprintf(pp, " 0=%d", shape[3]); |
| 5568 | fprintf(pp, " 1=%d", shape[2]); |
| 5569 | fprintf(pp, " 2=%d", shape[1]); |
| 5570 | } |
| 5571 | else if (shape.size() == 5) |
| 5572 | { |
| 5573 | fprintf(pp, " 0=%d", shape[4] * shape[3]); |
| 5574 | fprintf(pp, " 1=%d", shape[2]); |
| 5575 | fprintf(pp, " 2=%d", shape[1]); |
| 5576 | } |
| 5577 | } |
| 5578 | else if (op == "Resize") |
| 5579 | { |
| 5580 | std::string mode = get_node_attr_s(node, "mode"); |
| 5581 | std::string align = get_node_attr_s(node, "coordinate_transformation_mode"); |
| 5582 | |
| 5583 | std::vector<float> scales; |
| 5584 | std::vector<int> sizes; |
| 5585 | if (node.input_size() == 2) |
| 5586 | { |
| 5587 | // opset 10 |
| 5588 | scales = get_node_attr_from_input_af(weights[node.input(1)]); |
| 5589 | } |
| 5590 | else |
| 5591 | { |
| 5592 | // opset 11+ |
| 5593 | scales = get_node_attr_from_input_af(weights[node.input(2)]); |
| 5594 | if (node.input_size() >= 4) |
| 5595 | { |
| 5596 | sizes = get_node_attr_from_input_ai(weights[node.input(3)]); |
| 5597 | } |
| 5598 | } |
| 5599 | |
| 5600 | int resize_type = 1; |
| 5601 | if (mode == "nearest") |
| 5602 | { |
| 5603 | resize_type = 1; |
| 5604 | } |
| 5605 | else if (mode == "linear") |
| 5606 | { |
| 5607 | resize_type = 2; |
| 5608 | } |
| 5609 | else if (mode == "cubic") |
| 5610 | { |
| 5611 | resize_type = 3; |
| 5612 | } |
| 5613 | |
| 5614 | if (scales.empty() && sizes.empty()) |
| 5615 | { |
| 5616 | fprintf(stderr, "Unsupported Resize scales and sizes are all empty!\n"); |
| 5617 | } |
| 5618 | |
| 5619 | float h_scale = 1.f; |
| 5620 | float w_scale = 1.f; |
| 5621 | if (scales.size() == 2) |
| 5622 | { |
| 5623 | w_scale = scales[1]; |
| 5624 | } |
| 5625 | else if (scales.size() == 3) |
| 5626 | { |
| 5627 | h_scale = scales[1]; |
| 5628 | w_scale = scales[2]; |
| 5629 | } |
| 5630 | else if (scales.size() == 4) |
| 5631 | { |
| 5632 | h_scale = scales[2]; |
| 5633 | w_scale = scales[3]; |
| 5634 | |
| 5635 | if (scales[1] != 1.f) |
| 5636 | fprintf(stderr, "Unsupported Resize scales !\n"); |
| 5637 | } |
| 5638 | |
| 5639 | int output_height = 0; |
| 5640 | int output_width = 0; |
| 5641 | if (sizes.size() == 2) |
| 5642 | { |
| 5643 | output_width = sizes[1]; |
| 5644 | } |
| 5645 | else if (sizes.size() == 3) |
| 5646 | { |
| 5647 | output_height = sizes[1]; |
| 5648 | output_width = sizes[2]; |
| 5649 | } |
| 5650 | else if (sizes.size() == 4) |
| 5651 | { |
| 5652 | output_height = sizes[2]; |
| 5653 | output_width = sizes[3]; |
| 5654 | } |
| 5655 | |
| 5656 | int align_corner = 0; |
| 5657 | if (align == "align_corners") |
| 5658 | { |
| 5659 | align_corner = 1; |
| 5660 | } |
| 5661 | |
| 5662 | fprintf(pp, " 0=%d", resize_type); |
| 5663 | fprintf(pp, " 1=%e", h_scale); |
| 5664 | fprintf(pp, " 2=%e", w_scale); |
| 5665 | fprintf(pp, " 3=%d", output_height); |
| 5666 | fprintf(pp, " 4=%d", output_width); |
| 5667 | fprintf(pp, " 6=%d", align_corner); |
| 5668 | } |
| 5669 | else if (op == "RNN") |
| 5670 | { |
| 5671 | const onnx::TensorProto& W = weights[node.input(1)]; |
| 5672 | const onnx::TensorProto& R = weights[node.input(2)]; |
| 5673 | const onnx::TensorProto& B = weights[node.input(3)]; |
| 5674 | |
| 5675 | int hidden_size = get_node_attr_i(node, "hidden_size", 0); |
| 5676 | std::string direction = get_node_attr_s(node, "direction"); |
| 5677 | |
| 5678 | int direction_type = 0; |
| 5679 | if (direction == "forward") |
| 5680 | { |
| 5681 | direction_type = 0; |
| 5682 | } |
| 5683 | else if (direction == "reverse") |
| 5684 | { |
| 5685 | direction_type = 1; |
| 5686 | } |
| 5687 | else if (direction == "bidirectional") |
| 5688 | { |
| 5689 | direction_type = 2; |
| 5690 | } |
| 5691 | |
| 5692 | int weight_data_size = get_tensor_proto_data_size(W); |
| 5693 | |
| 5694 | fprintf(pp, " 0=%d", hidden_size); |
| 5695 | fprintf(pp, " 1=%d", weight_data_size); |
| 5696 | fprintf(pp, " 2=%d", direction_type); |
| 5697 | |
| 5698 | int num_directions = direction_type == 2 ? 2 : 1; |
| 5699 | |
| 5700 | int quantize_tag = 0; |
| 5701 | |
| 5702 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5703 | fwrite_tensor_proto_data(W, bp); |
| 5704 | |
| 5705 | // reduce xc and hc bias |
| 5706 | { |
| 5707 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5708 | |
| 5709 | int bias_data_size_g = get_tensor_proto_data_size(B) / 2 / num_directions; |
| 5710 | const float* bptr = B.has_raw_data() ? (const float*)B.raw_data().data() : B.float_data().data(); |
| 5711 | const float* xiptr = bptr; |
| 5712 | const float* hiptr = bptr + bias_data_size_g; |
| 5713 | |
| 5714 | for (int j = 0; j < bias_data_size_g; j++) |
| 5715 | { |
| 5716 | float vb = xiptr[j] + hiptr[j]; |
| 5717 | fwrite(&vb, sizeof(float), 1, bp); |
| 5718 | } |
| 5719 | |
| 5720 | if (direction_type == 2) |
| 5721 | { |
| 5722 | xiptr += bias_data_size_g * 2; |
| 5723 | hiptr += bias_data_size_g * 2; |
| 5724 | |
| 5725 | for (int j = 0; j < bias_data_size_g; j++) |
| 5726 | { |
| 5727 | float vb = xiptr[j] + hiptr[j]; |
| 5728 | fwrite(&vb, sizeof(float), 1, bp); |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5734 | fwrite_tensor_proto_data(R, bp); |
| 5735 | } |
| 5736 | else if (op == "RDiv") |
| 5737 | { |
| 5738 | int op_type = 8; |
| 5739 | fprintf(pp, " 0=%d", op_type); |
| 5740 | |
| 5741 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5742 | float b = get_node_attr_f(node, "b", 0.f); |
| 5743 | if (with_scalar) |
| 5744 | { |
| 5745 | fprintf(pp, " 1=%d", with_scalar); |
| 5746 | fprintf(pp, " 2=%e", b); |
| 5747 | } |
| 5748 | } |
| 5749 | else if (op == "RSub") |
| 5750 | { |
| 5751 | int op_type = 7; |
| 5752 | fprintf(pp, " 0=%d", op_type); |
| 5753 | |
| 5754 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5755 | float b = get_node_attr_f(node, "b", 0.f); |
| 5756 | if (with_scalar) |
| 5757 | { |
| 5758 | fprintf(pp, " 1=%d", with_scalar); |
| 5759 | fprintf(pp, " 2=%e", b); |
| 5760 | } |
| 5761 | } |
| 5762 | else if (op == "Shrink") |
| 5763 | { |
| 5764 | float bias = get_node_attr_f(node, "bias", 0.0f); |
| 5765 | float lambd = get_node_attr_f(node, "lambd", 0.5f); |
| 5766 | fprintf(pp, " 0=%e", bias); |
| 5767 | fprintf(pp, " 1=%e", lambd); |
| 5768 | } |
| 5769 | else if (op == "ShuffleChannel") |
| 5770 | { |
| 5771 | int group = get_node_attr_i(node, "group", 1); |
| 5772 | int reverse = get_node_attr_i(node, "reverse", 0); |
| 5773 | fprintf(pp, " 0=%d", group); |
| 5774 | fprintf(pp, " 1=%d", reverse); |
| 5775 | } |
| 5776 | else if (op == "Sigmoid") |
| 5777 | { |
| 5778 | // no param |
| 5779 | } |
| 5780 | else if (op == "Sin") |
| 5781 | { |
| 5782 | int op_type = 9; |
| 5783 | fprintf(pp, " 0=%d", op_type); |
| 5784 | } |
| 5785 | else if (op == "SkipLayerNormalization") |
| 5786 | { |
| 5787 | const onnx::TensorProto& W = weights[node.input(2)]; |
| 5788 | const onnx::TensorProto& B = weights[node.input(3)]; |
| 5789 | const onnx::TensorProto& B2 = weights[node.input(4)]; |
| 5790 | |
| 5791 | fprintf(pp, " 0=%d", get_tensor_proto_data_size(B)); |
| 5792 | |
| 5793 | int quantize_tag = 0; |
| 5794 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5795 | |
| 5796 | fwrite_tensor_proto_data(W, bp); |
| 5797 | |
| 5798 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5799 | |
| 5800 | fwrite_tensor_proto_data(B, bp); |
| 5801 | |
| 5802 | fwrite(&quantize_tag, sizeof(int), 1, bp); |
| 5803 | |
| 5804 | fwrite_tensor_proto_data(B2, bp); |
| 5805 | } |
| 5806 | else if (op == "Slice") |
| 5807 | { |
| 5808 | std::vector<int> starts; |
| 5809 | std::vector<int> ends; |
| 5810 | std::vector<int> axes; |
| 5811 | std::vector<int> steps; |
| 5812 | if (node.input_size() == 1) |
| 5813 | { |
| 5814 | starts = get_node_attr_ai(node, "starts"); |
| 5815 | ends = get_node_attr_ai(node, "ends"); |
| 5816 | axes = get_node_attr_ai(node, "axes"); |
| 5817 | steps = get_node_attr_ai(node, "steps"); // TODO |
| 5818 | } |
| 5819 | else |
| 5820 | { |
| 5821 | starts = get_node_attr_from_input_ai(weights[node.input(1)]); |
| 5822 | ends = get_node_attr_from_input_ai(weights[node.input(2)]); |
| 5823 | if (node.input_size() >= 4) |
| 5824 | axes = get_node_attr_from_input_ai(weights[node.input(3)]); |
| 5825 | if (node.input_size() >= 5) |
| 5826 | steps = get_node_attr_from_input_ai(weights[node.input(4)]); |
| 5827 | } |
| 5828 | |
| 5829 | // assert step == 1 |
| 5830 | for (int i = 0; i < (int)steps.size(); i++) |
| 5831 | { |
| 5832 | if (steps[i] != 1) |
| 5833 | fprintf(stderr, "Unsupported slice step !\n"); |
| 5834 | } |
| 5835 | |
| 5836 | // filter out N-dim axis |
| 5837 | if (!axes.empty()) |
| 5838 | { |
| 5839 | for (int i = 0; i < (int)axes.size(); i++) |
| 5840 | { |
| 5841 | int axis = axes[i]; |
| 5842 | if (axis == 0) |
| 5843 | { |
| 5844 | starts.erase(starts.begin() + i); |
| 5845 | ends.erase(ends.begin() + i); |
| 5846 | axes.erase(axes.begin() + i); |
| 5847 | break; |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | fprintf(pp, " -23309=%d", (int)starts.size()); |
| 5853 | for (int i = 0; i < (int)starts.size(); i++) |
| 5854 | { |
| 5855 | fprintf(pp, ",%d", starts[i]); |
| 5856 | } |
| 5857 | fprintf(pp, " -23310=%d", (int)ends.size()); |
| 5858 | for (int i = 0; i < (int)ends.size(); i++) |
| 5859 | { |
| 5860 | fprintf(pp, ",%d", ends[i]); |
| 5861 | } |
| 5862 | if (!axes.empty()) |
| 5863 | { |
| 5864 | fprintf(pp, " -23311=%d", (int)axes.size()); |
| 5865 | for (int i = 0; i < (int)axes.size(); i++) |
| 5866 | { |
| 5867 | int axis = axes[i]; |
| 5868 | if (axis == 0 || axis > 3 || axis < -3) |
| 5869 | fprintf(stderr, "Unsupported slice axes !\n"); |
| 5870 | |
| 5871 | if (axis > 0) |
| 5872 | axis = axis - 1; // -1 for skip N-dim |
| 5873 | |
| 5874 | fprintf(pp, ",%d", axis); |
| 5875 | } |
| 5876 | } |
| 5877 | } |
| 5878 | else if (op == "Softmax") |
| 5879 | { |
| 5880 | int axis = get_node_attr_i(node, "axis", 1); |
| 5881 | fprintf(pp, " 0=%d", axis - 1); |
| 5882 | fprintf(pp, " 1=1"); |
| 5883 | } |
| 5884 | else if (op == "Split") |
| 5885 | { |
| 5886 | int axis = get_node_attr_i(node, "axis", 0); |
| 5887 | std::vector<int> split = get_node_attr_ai(node, "split"); |
| 5888 | if (axis < 1) |
| 5889 | fprintf(stderr, "Unsupported split axis !\n"); |
| 5890 | |
| 5891 | fprintf(pp, " -23300=%d", output_size); |
| 5892 | if (split.empty()) |
| 5893 | { |
| 5894 | for (int i = 0; i < output_size; i++) |
| 5895 | { |
| 5896 | fprintf(pp, ",-233"); |
| 5897 | } |
| 5898 | } |
| 5899 | else |
| 5900 | { |
| 5901 | for (size_t i = 0; i < split.size() - 1; i++) |
| 5902 | { |
| 5903 | fprintf(pp, ",%d", split[i]); |
| 5904 | } |
| 5905 | fprintf(pp, ",-233"); |
| 5906 | } |
| 5907 | fprintf(pp, " 1=%d", axis - 1); |
| 5908 | } |
| 5909 | else if (op == "Sqrt") |
| 5910 | { |
| 5911 | int op_type = 5; |
| 5912 | fprintf(pp, " 0=%d", op_type); |
| 5913 | } |
| 5914 | else if (op == "Squeeze") |
| 5915 | { |
| 5916 | std::vector<int> axes = get_node_attr_ai(node, "axes"); |
| 5917 | |
| 5918 | if (axes.empty()) |
| 5919 | { |
| 5920 | fprintf(pp, " 0=1"); |
| 5921 | fprintf(pp, " 1=1"); |
| 5922 | fprintf(pp, " 2=1"); |
| 5923 | } |
| 5924 | else |
| 5925 | { |
| 5926 | fprintf(pp, " -23303=%zu", axes.size()); |
| 5927 | for (int i = 0; i < (int)axes.size(); i++) |
| 5928 | { |
| 5929 | if (axes[i] == 0 || axes[i] > 4 || axes[i] < -3) |
| 5930 | fprintf(stderr, "Unsupported squeeze axes !\n"); |
| 5931 | fprintf(pp, ",%d", axes[i] > 0 ? axes[i] - 1 : axes[i]); |
| 5932 | } |
| 5933 | } |
| 5934 | } |
| 5935 | else if (op == "Sub") |
| 5936 | { |
| 5937 | int op_type = 1; |
| 5938 | fprintf(pp, " 0=%d", op_type); |
| 5939 | |
| 5940 | int with_scalar = get_node_attr_i(node, "with_scalar", 0); |
| 5941 | float b = get_node_attr_f(node, "b", 0.f); |
| 5942 | if (with_scalar) |
| 5943 | { |
| 5944 | fprintf(pp, " 1=%d", with_scalar); |
| 5945 | fprintf(pp, " 2=%e", b); |
| 5946 | } |
| 5947 | } |
| 5948 | else if (op == "Sum") |
| 5949 | { |
| 5950 | int op_type = 1; |
| 5951 | fprintf(pp, " 0=%d", op_type); |
| 5952 | } |
| 5953 | else if (op == "Swish") |
| 5954 | { |
| 5955 | // no param |
| 5956 | } |
| 5957 | else if (op == "Tan") |
| 5958 | { |
| 5959 | int op_type = 11; |
| 5960 | fprintf(pp, " 0=%d", op_type); |
| 5961 | } |
| 5962 | else if (op == "Tanh") |
| 5963 | { |
| 5964 | int op_type = 16; |
| 5965 | fprintf(pp, " 0=%d", op_type); |
| 5966 | } |
| 5967 | else if (op == "Transpose") |
| 5968 | { |
| 5969 | std::vector<int> perm = get_node_attr_ai(node, "perm"); |
| 5970 | |
| 5971 | if (perm.size() == 3) |
| 5972 | { |
| 5973 | if (perm[1] == 1 && perm[2] == 2) |
| 5974 | fprintf(pp, " 0=0"); // w h |
| 5975 | else if (perm[1] == 2 && perm[2] == 1) |
| 5976 | fprintf(pp, " 0=1"); // h w |
| 5977 | else if (perm[0] == 1 && perm[1] == 0 && perm[2] == 2) |
| 5978 | fprintf(pp, " 0=0"); // w h |
| 5979 | else if (perm[0] == 2 && perm[1] == 0 && perm[2] == 1) |
| 5980 | fprintf(pp, " 0=1"); // h w |
| 5981 | } |
| 5982 | else if (perm.size() == 4) |
| 5983 | { |
| 5984 | if (perm[1] == 1 && perm[2] == 2 && perm[3] == 3) |
| 5985 | fprintf(pp, " 0=0"); // w h c |
| 5986 | else if (perm[1] == 1 && perm[2] == 3 && perm[3] == 2) |
| 5987 | fprintf(pp, " 0=1"); // h w c |
| 5988 | else if (perm[1] == 2 && perm[2] == 1 && perm[3] == 3) |
| 5989 | fprintf(pp, " 0=2"); // w c h |
| 5990 | else if (perm[1] == 2 && perm[2] == 3 && perm[3] == 1) |
| 5991 | fprintf(pp, " 0=3"); // c w h |
| 5992 | else if (perm[1] == 3 && perm[2] == 1 && perm[3] == 2) |
| 5993 | fprintf(pp, " 0=4"); // h c w |
| 5994 | else if (perm[1] == 3 && perm[2] == 2 && perm[3] == 1) |
| 5995 | fprintf(pp, " 0=5"); // c h w |
| 5996 | else |
| 5997 | fprintf(stderr, "Unsupported transpose type !\n"); |
| 5998 | } |
| 5999 | else if (perm.size() == 5) |
| 6000 | { |
| 6001 | if (perm[1] == 1 && perm[2] == 2 && perm[3] == 3 && perm[4] == 4) |
| 6002 | fprintf(pp, " 0=0"); // wx h c |
| 6003 | else if (perm[1] == 1 && perm[2] == 3 && perm[3] == 4 && perm[4] == 2) |
| 6004 | fprintf(pp, " 0=1"); // h wx c |
| 6005 | else if (perm[1] == 2 && perm[2] == 1 && perm[3] == 3 && perm[4] == 4) |
| 6006 | fprintf(pp, " 0=2"); // wx c h |
| 6007 | else if (perm[1] == 2 && perm[2] == 3 && perm[3] == 4 && perm[4] == 1) |
| 6008 | fprintf(pp, " 0=3"); // c wx h |
| 6009 | else if (perm[1] == 3 && perm[2] == 4 && perm[3] == 1 && perm[4] == 2) |
| 6010 | fprintf(pp, " 0=4"); // h c wx |
| 6011 | else if (perm[1] == 3 && perm[2] == 4 && perm[3] == 2 && perm[4] == 1) |
| 6012 | fprintf(pp, " 0=5"); // c h wx |
| 6013 | else |
| 6014 | fprintf(stderr, "Unsupported transpose type !\n"); |
| 6015 | } |
| 6016 | } |
| 6017 | else if (op == "Upsample") |
| 6018 | { |
| 6019 | std::string mode = get_node_attr_s(node, "mode"); |
| 6020 | std::string align = get_node_attr_s(node, "coordinate_transformation_mode"); |
| 6021 | |
| 6022 | std::vector<float> scales; |
| 6023 | |
| 6024 | if (node.input_size() == 1) |
| 6025 | { |
| 6026 | scales = get_node_attr_af(node, "scales"); |
| 6027 | } |
| 6028 | else |
| 6029 | { |
| 6030 | scales = get_node_attr_from_input_af(weights[node.input(1)]); |
| 6031 | } |
| 6032 | |
| 6033 | int resize_type = 1; |
| 6034 | if (mode == "nearest") |
| 6035 | { |
| 6036 | resize_type = 1; |
| 6037 | } |
| 6038 | else if (mode == "bilinear" || mode == "linear") |
| 6039 | { |
| 6040 | resize_type = 2; |
| 6041 | } |
| 6042 | else if (mode == "trilinear") |
| 6043 | { |
| 6044 | fprintf(stderr, "Unsupported Upsample mode !\n"); |
| 6045 | } |
| 6046 | |
| 6047 | float h_scale = 1.f; |
| 6048 | float w_scale = 1.f; |
| 6049 | if (scales.size() == 2) |
| 6050 | { |
| 6051 | w_scale = scales[1]; |
| 6052 | } |
| 6053 | else if (scales.size() == 3) |
| 6054 | { |
| 6055 | h_scale = scales[1]; |
| 6056 | w_scale = scales[2]; |
| 6057 | } |
| 6058 | else if (scales.size() == 4) |
| 6059 | { |
| 6060 | h_scale = scales[2]; |
| 6061 | w_scale = scales[3]; |
| 6062 | |
| 6063 | if (scales[1] != 1.f) |
| 6064 | fprintf(stderr, "Unsupported Upsample scales !\n"); |
| 6065 | } |
| 6066 | else |
| 6067 | { |
| 6068 | fprintf(stderr, "Unsupported Upsample scales !\n"); |
| 6069 | } |
| 6070 | |
| 6071 | int align_corner = 0; |
| 6072 | if (align == "align_corners") |
| 6073 | { |
| 6074 | align_corner = 1; |
| 6075 | } |
| 6076 | |
| 6077 | fprintf(pp, " 0=%d", resize_type); |
| 6078 | fprintf(pp, " 1=%e", h_scale); |
| 6079 | fprintf(pp, " 2=%e", w_scale); |
| 6080 | fprintf(pp, " 6=%d", align_corner); |
| 6081 | } |
| 6082 | else if (op == "Unsqueeze") |
| 6083 | { |
| 6084 | std::vector<int> axes = get_node_attr_ai(node, "axes"); |
| 6085 | |
| 6086 | fprintf(pp, " -23303=%zu", axes.size()); |
| 6087 | for (int i = 0; i < (int)axes.size(); i++) |
| 6088 | { |
| 6089 | if (axes[i] == 0 || axes[i] > 4 || axes[i] < -4) |
| 6090 | fprintf(stderr, "Unsupported unsqueeze axes !\n"); |
| 6091 | fprintf(pp, ",%d", axes[i] > 0 ? axes[i] - 1 : axes[i]); |
| 6092 | } |
| 6093 | } |
| 6094 | else |
| 6095 | { |
| 6096 | // TODO op specific param |
| 6097 | for (int j = 0; j < node.attribute_size(); j++) |
| 6098 | { |
| 6099 | const onnx::AttributeProto& attr = node.attribute(j); |
| 6100 | if (attr.type() == 1) |
| 6101 | { |
| 6102 | fprintf(stderr, " # %s=%g\n", attr.name().c_str(), attr.f()); |
| 6103 | } |
| 6104 | else if (attr.type() == 2) |
| 6105 | { |
| 6106 | fprintf(stderr, " # %s=%lld\n", attr.name().c_str(), (long long)attr.i()); |
| 6107 | } |
| 6108 | else if (attr.type() == 3) |
| 6109 | { |
| 6110 | fprintf(stderr, " # %s=%s\n", attr.name().c_str(), attr.s().c_str()); |
| 6111 | } |
| 6112 | else |
| 6113 | { |
| 6114 | fprintf(stderr, " # %s %d\n", attr.name().c_str(), attr.type()); |
| 6115 | } |
| 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | fprintf(pp, "\n"); |
| 6120 | |
| 6121 | for (int j = 0; j < output_size; j++) |
| 6122 | { |
| 6123 | const std::string& output_name = node.output(j); |
| 6124 | if (node_reference.find(output_name) != node_reference.end()) |
| 6125 | { |
| 6126 | int refcount = node_reference[output_name]; |
| 6127 | if (refcount > 1) |
| 6128 | { |
| 6129 | char splitname[256]; |
| 6130 | sprintf(splitname, "splitncnn_%d", internal_split); |
| 6131 | fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount); |
| 6132 | |
| 6133 | fprintf(pp, " %s", trunc_name(output_name).c_str()); |
| 6134 | |
| 6135 | for (int k = 0; k < refcount; k++) |
| 6136 | { |
| 6137 | std::string split_name = output_name + "_splitncnn_" + std::to_string(k); |
| 6138 | fprintf(pp, " %s", trunc_name(split_name).c_str()); |
| 6139 | } |
| 6140 | fprintf(pp, "\n"); |
| 6141 | |
| 6142 | internal_split++; |
| 6143 | } |
| 6144 | } |
| 6145 | } |
| 6146 | } |
| 6147 | |
| 6148 | fclose(pp); |
| 6149 | fclose(bp); |
| 6150 | |
| 6151 | return 0; |
| 6152 | } |