| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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 | #ifdef _MSC_VER |
| 16 | #define _CRT_SECURE_NO_DEPRECATE |
| 17 | #endif |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <algorithm> |
| 21 | #include <map> |
| 22 | #include <set> |
| 23 | #include <vector> |
| 24 | |
| 25 | // ncnn public header |
| 26 | #include "datareader.h" |
| 27 | #include "layer.h" |
| 28 | #include "layer_type.h" |
| 29 | #include "net.h" |
| 30 | |
| 31 | // ncnn private header |
| 32 | #include "layer/batchnorm.h" |
| 33 | #include "layer/bias.h" |
| 34 | #include "layer/binaryop.h" |
| 35 | #include "layer/celu.h" |
| 36 | #include "layer/clip.h" |
| 37 | #include "layer/concat.h" |
| 38 | #include "layer/convolution.h" |
| 39 | #include "layer/convolution1d.h" |
| 40 | #include "layer/convolution3d.h" |
| 41 | #include "layer/convolutiondepthwise.h" |
| 42 | #include "layer/convolutiondepthwise1d.h" |
| 43 | #include "layer/convolutiondepthwise3d.h" |
| 44 | #include "layer/copyto.h" |
| 45 | #include "layer/crop.h" |
| 46 | #include "layer/cumulativesum.h" |
| 47 | #include "layer/deconvolution.h" |
| 48 | #include "layer/deconvolution1d.h" |
| 49 | #include "layer/deconvolution3d.h" |
| 50 | #include "layer/deconvolutiondepthwise.h" |
| 51 | #include "layer/deconvolutiondepthwise1d.h" |
| 52 | #include "layer/deconvolutiondepthwise3d.h" |
| 53 | #include "layer/deformableconv2d.h" |
| 54 | #include "layer/detectionoutput.h" |
| 55 | #include "layer/diag.h" |
| 56 | #include "layer/dropout.h" |
| 57 | #include "layer/eltwise.h" |
| 58 | #include "layer/elu.h" |
| 59 | #include "layer/embed.h" |
| 60 | #include "layer/exp.h" |
| 61 | #include "layer/expanddims.h" |
| 62 | #include "layer/flatten.h" |
| 63 | #include "layer/fold.h" |
| 64 | #include "layer/gelu.h" |
| 65 | #include "layer/gemm.h" |
| 66 | #include "layer/glu.h" |
| 67 | #include "layer/gridsample.h" |
| 68 | #include "layer/groupnorm.h" |
| 69 | #include "layer/gru.h" |
| 70 | #include "layer/hardsigmoid.h" |
| 71 | #include "layer/hardswish.h" |
| 72 | #include "layer/innerproduct.h" |
| 73 | #include "layer/input.h" |
| 74 | #include "layer/instancenorm.h" |
| 75 | #include "layer/interp.h" |
| 76 | #include "layer/layernorm.h" |
| 77 | #include "layer/log.h" |
| 78 | #include "layer/lrn.h" |
| 79 | #include "layer/lstm.h" |
| 80 | #include "layer/matmul.h" |
| 81 | #include "layer/memorydata.h" |
| 82 | #include "layer/mvn.h" |
| 83 | #include "layer/multiheadattention.h" |
| 84 | #include "layer/normalize.h" |
| 85 | #include "layer/padding.h" |
| 86 | #include "layer/permute.h" |
| 87 | #include "layer/pixelshuffle.h" |
| 88 | #include "layer/pooling.h" |
| 89 | #include "layer/pooling1d.h" |
| 90 | #include "layer/pooling3d.h" |
| 91 | #include "layer/power.h" |
| 92 | #include "layer/prelu.h" |
| 93 | #include "layer/priorbox.h" |
| 94 | #include "layer/proposal.h" |
| 95 | #include "layer/psroipooling.h" |
| 96 | #include "layer/quantize.h" |
| 97 | #include "layer/reduction.h" |
| 98 | #include "layer/relu.h" |
| 99 | #include "layer/reorg.h" |
| 100 | #include "layer/requantize.h" |
| 101 | #include "layer/reshape.h" |
| 102 | #include "layer/rmsnorm.h" |
| 103 | #include "layer/rnn.h" |
| 104 | #include "layer/roialign.h" |
| 105 | #include "layer/roipooling.h" |
| 106 | #include "layer/scale.h" |
| 107 | #include "layer/shufflechannel.h" |
| 108 | #include "layer/slice.h" |
| 109 | #include "layer/softmax.h" |
| 110 | #include "layer/split.h" |
| 111 | #include "layer/squeeze.h" |
| 112 | #include "layer/threshold.h" |
| 113 | #include "layer/unaryop.h" |
| 114 | #include "layer/unfold.h" |
| 115 | #include "layer/yolodetectionoutput.h" |
| 116 | #include "layer/yolov3detectionoutput.h" |
| 117 | |
| 118 | // for gen_random_weight |
| 119 | #include "../tests/prng.h" |
| 120 | |
| 121 | static struct prng_rand_t g_prng_rand_state; |
| 122 | #define SRAND(seed) prng_srand(seed, &g_prng_rand_state) |
| 123 | #define RAND() prng_rand(&g_prng_rand_state) |
| 124 | |
| 125 | class MemoryFootprintAllocator : public ncnn::Allocator |
| 126 | { |
| 127 | public: |
| 128 | MemoryFootprintAllocator() |
| 129 | { |
| 130 | current_memory_usage = 0; |
| 131 | memory_footprint = 0; |
| 132 | } |
| 133 | |
| 134 | virtual void* fastMalloc(size_t size) |
| 135 | { |
| 136 | ncnn::MutexLockGuard g(lock); |
| 137 | void* ptr = ncnn::fastMalloc(size); |
| 138 | bookkeeper[ptr] = size; |
| 139 | current_memory_usage += size; |
| 140 | memory_footprint = std::max(memory_footprint, current_memory_usage); |
| 141 | return ptr; |
| 142 | } |
| 143 | |
| 144 | virtual void fastFree(void* ptr) |
| 145 | { |
| 146 | ncnn::MutexLockGuard g(lock); |
| 147 | size_t size = bookkeeper[ptr]; |
| 148 | current_memory_usage -= size; |
| 149 | bookkeeper.erase(bookkeeper.find(ptr)); |
| 150 | ncnn::fastFree(ptr); |
| 151 | } |
| 152 | |
| 153 | public: |
| 154 | int current_memory_usage; |
| 155 | int memory_footprint; |
| 156 | ncnn::Mutex lock; |
| 157 | std::map<void*, size_t> bookkeeper; |
| 158 | }; |
| 159 | |
| 160 | class CustomLayer : public ncnn::Layer |
| 161 | { |
| 162 | public: |
| 163 | virtual int load_param(const ncnn::ParamDict& pd) |
| 164 | { |
| 165 | mpd = pd; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void write_param(FILE* pp) |
| 170 | { |
| 171 | for (int i = 0; i < NCNN_MAX_PARAM_COUNT; i++) |
| 172 | { |
| 173 | int type = mpd.type(i); |
| 174 | if (type == 0) |
| 175 | continue; |
| 176 | |
| 177 | if (type == 2) |
| 178 | { |
| 179 | fprintf(pp, " %d=%d", i, mpd.get(i, 0)); |
| 180 | } |
| 181 | if (type == 3) |
| 182 | { |
| 183 | fprintf(pp, " %d=%e", i, mpd.get(i, 0.f)); |
| 184 | } |
| 185 | if (type == 5) |
| 186 | { |
| 187 | ncnn::Mat v = mpd.get(i, ncnn::Mat()); |
| 188 | int len = v.w; |
| 189 | fprintf(pp, " %d=%d", -i - 23300, len); |
| 190 | const int* p = v; |
| 191 | for (int j = 0; j < len; j++) |
| 192 | { |
| 193 | fprintf(pp, ",%d", p[j]); |
| 194 | } |
| 195 | } |
| 196 | if (type == 6) |
| 197 | { |
| 198 | ncnn::Mat v = mpd.get(i, ncnn::Mat()); |
| 199 | int len = v.w; |
| 200 | fprintf(pp, " %d=%d", -i - 23300, len); |
| 201 | const float* p = v; |
| 202 | for (int j = 0; j < len; j++) |
| 203 | { |
| 204 | fprintf(pp, ",%e", p[j]); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | public: |
| 211 | ncnn::ParamDict mpd; |
| 212 | }; |
| 213 | |
| 214 | DEFINE_LAYER_CREATOR(CustomLayer) |
| 215 | |
| 216 | class ModelWriter : public ncnn::Net |
| 217 | { |
| 218 | public: |
| 219 | ModelWriter(); |
| 220 | |
| 221 | virtual ncnn::Layer* create_custom_layer(const char* type); |
| 222 | |
| 223 | std::vector<ncnn::Blob>& blobs; |
| 224 | std::vector<ncnn::Layer*>& layers; |
| 225 | |
| 226 | bool has_custom_layer; |
| 227 | |
| 228 | public: |
| 229 | // 0=fp32 1=fp16 |
| 230 | int storage_type; |
| 231 | |
| 232 | int gen_random_weight; |
| 233 | |
| 234 | // Cut param and bin -1=no cut |
| 235 | int cutstart; |
| 236 | int cutend; |
| 237 | |
| 238 | public: |
| 239 | int set_cutparam(const char* cutstartname, const char* cutendname); |
| 240 | |
| 241 | int shape_inference(); |
| 242 | int estimate_memory_footprint(); |
| 243 | |
| 244 | public: |
| 245 | int fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp); |
| 246 | int fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp); |
| 247 | |
| 248 | int fwrite_weight_tag_data(const ncnn::Mat& data, FILE* bp, float a = -1.2f, float b = 1.2f); |
| 249 | int fwrite_weight_data(const ncnn::Mat& data, FILE* bp, float a = -1.2f, float b = 1.2f); |
| 250 | |
| 251 | int save(const char* parampath, const char* binpath); |
| 252 | }; |
| 253 | |
| 254 | ModelWriter::ModelWriter() |
| 255 | : blobs(mutable_blobs()), layers(mutable_layers()) |
| 256 | { |
| 257 | opt.lightmode = false; |
| 258 | has_custom_layer = false; |
| 259 | gen_random_weight = false; |
| 260 | cutstart = -1; |
| 261 | cutend = -1; |
| 262 | |
| 263 | SRAND(7767517); |
| 264 | } |
| 265 | |
| 266 | ncnn::Layer* ModelWriter::create_custom_layer(const char* type) |
| 267 | { |
| 268 | ncnn::Layer* layer = Net::create_custom_layer(type); |
| 269 | if (layer) |
| 270 | return layer; |
| 271 | |
| 272 | fprintf(stderr, "create_custom_layer %s\n", type); |
| 273 | |
| 274 | register_custom_layer(type, CustomLayer_layer_creator); |
| 275 | |
| 276 | has_custom_layer = true; |
| 277 | |
| 278 | return Net::create_custom_layer(type); |
| 279 | } |
| 280 | |
| 281 | int ModelWriter::set_cutparam(const char* cutstartname, const char* cutendname) |
| 282 | { |
| 283 | if (cutstartname != nullptr) |
| 284 | { |
| 285 | int layindex = find_layer_index_by_name(cutstartname); |
| 286 | if (layindex >= 0) |
| 287 | { |
| 288 | cutstart = layindex; |
| 289 | fprintf(stderr, "cutstart layer %d:%s\n", layindex, cutstartname); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | fprintf(stderr, "not find target cutstart layer %s\n", cutstartname); |
| 294 | return -1; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (cutendname != nullptr) |
| 299 | { |
| 300 | int layindex = find_layer_index_by_name(cutendname); |
| 301 | if (layindex >= 0) |
| 302 | { |
| 303 | cutend = layindex; |
| 304 | fprintf(stderr, "cutend layer %d:%s\n", layindex, cutendname); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | fprintf(stderr, "not find target cutend layer %s\n", cutendname); |
| 309 | return -1; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int ModelWriter::shape_inference() |
| 317 | { |
| 318 | if (has_custom_layer) |
| 319 | { |
| 320 | fprintf(stderr, "model has custom layer, shape_inference skipped\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | const size_t layer_count = layers.size(); |
| 325 | const size_t blob_count = blobs.size(); |
| 326 | |
| 327 | // recreate layer pipeline for param and weight changes |
| 328 | for (size_t i = 0; i < layer_count; i++) |
| 329 | { |
| 330 | ncnn::Layer* layer = layers[i]; |
| 331 | |
| 332 | layer->destroy_pipeline(opt); |
| 333 | |
| 334 | int cret = layer->create_pipeline(opt); |
| 335 | if (cret != 0) |
| 336 | { |
| 337 | NCNN_LOGE("layer create_pipeline %d %s failed", (int)i, layer->name.c_str()); |
| 338 | return -1; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | ncnn::Extractor ex = create_extractor(); |
| 343 | ex.set_light_mode(true); |
| 344 | |
| 345 | // prepare Input blobs |
| 346 | for (size_t i = 0; i < layer_count; i++) |
| 347 | { |
| 348 | const ncnn::Layer* layer = layers[i]; |
| 349 | if (layer->type == "ncnnfused") |
| 350 | continue; |
| 351 | |
| 352 | if (layer->type != "Input") |
| 353 | continue; |
| 354 | |
| 355 | ncnn::Input* input = (ncnn::Input*)layer; |
| 356 | |
| 357 | int w = input->w; |
| 358 | int h = input->h; |
| 359 | int c = input->c; |
| 360 | |
| 361 | int dims = 0; |
| 362 | if (w == 0 && h == 0 && c == 0) dims = 0; |
| 363 | if (w != 0 && h == 0 && c == 0) dims = 1; |
| 364 | if (w != 0 && h != 0 && c == 0) dims = 2; |
| 365 | if (w != 0 && h != 0 && c != 0) dims = 3; |
| 366 | |
| 367 | if (dims == 0) |
| 368 | { |
| 369 | fprintf(stderr, "Input layer %s without shape info, shape_inference skipped\n", layer->name.c_str()); |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | ncnn::Mat m; |
| 374 | if (dims == 1) m.create(w); |
| 375 | if (dims == 2) m.create(w, h); |
| 376 | if (dims == 3) m.create(w, h, c); |
| 377 | |
| 378 | ex.input(layer->tops[0], m); |
| 379 | } |
| 380 | |
| 381 | // prepare blobs with predefined shape |
| 382 | for (size_t i = 0; i < blob_count; i++) |
| 383 | { |
| 384 | const ncnn::Blob& blob = blobs[i]; |
| 385 | |
| 386 | int dims = blob.shape.dims; |
| 387 | int w = blob.shape.w; |
| 388 | int h = blob.shape.h; |
| 389 | int c = blob.shape.c; |
| 390 | |
| 391 | if (dims == 0) |
| 392 | continue; |
| 393 | |
| 394 | ncnn::Mat m; |
| 395 | if (dims == 1) m.create(w); |
| 396 | if (dims == 2) m.create(w, h); |
| 397 | if (dims == 3) m.create(w, h, c); |
| 398 | |
| 399 | m.fill(0.f); |
| 400 | |
| 401 | ex.input(int(i), m); |
| 402 | } |
| 403 | |
| 404 | fprintf(stderr, "shape_inference\n"); |
| 405 | |
| 406 | // resolve all layer output blob shape |
| 407 | for (size_t i = 0; i < layer_count; i++) |
| 408 | { |
| 409 | const ncnn::Layer* layer = layers[i]; |
| 410 | if (layer->type == "ncnnfused") |
| 411 | continue; |
| 412 | |
| 413 | for (size_t j = 0; j < layer->tops.size(); j++) |
| 414 | { |
| 415 | int top_blob_index = layer->tops[j]; |
| 416 | |
| 417 | ncnn::Mat m; |
| 418 | ex.extract(top_blob_index, m); |
| 419 | |
| 420 | blobs[top_blob_index].shape = m; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // assign all layer blob shape |
| 425 | for (size_t i = 0; i < layer_count; i++) |
| 426 | { |
| 427 | ncnn::Layer* layer = layers[i]; |
| 428 | if (layer->type == "ncnnfused") |
| 429 | continue; |
| 430 | |
| 431 | layer->bottom_shapes.resize(layer->bottoms.size()); |
| 432 | for (size_t j = 0; j < layer->bottoms.size(); j++) |
| 433 | { |
| 434 | int bottom_blob_index = layer->bottoms[j]; |
| 435 | |
| 436 | layer->bottom_shapes[j] = blobs[bottom_blob_index].shape; |
| 437 | } |
| 438 | |
| 439 | layer->top_shapes.resize(layer->tops.size()); |
| 440 | for (size_t j = 0; j < layer->tops.size(); j++) |
| 441 | { |
| 442 | int top_blob_index = layer->tops[j]; |
| 443 | |
| 444 | layer->top_shapes[j] = blobs[top_blob_index].shape; |
| 445 | |
| 446 | // fprintf(stderr, "%d %4d %4d %4d | %2d %s\n", blobs[top_blob_index].shape.dims, blobs[top_blob_index].shape.w, blobs[top_blob_index].shape.h, blobs[top_blob_index].shape.c, top_blob_index, blobs[top_blob_index].name.c_str()); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | int ModelWriter::estimate_memory_footprint() |
| 454 | { |
| 455 | if (has_custom_layer) |
| 456 | { |
| 457 | fprintf(stderr, "model has custom layer, estimate_memory_footprint skipped\n"); |
| 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | const size_t layer_count = layers.size(); |
| 462 | const size_t blob_count = blobs.size(); |
| 463 | |
| 464 | MemoryFootprintAllocator allocator; |
| 465 | |
| 466 | ncnn::Extractor ex = create_extractor(); |
| 467 | ex.set_light_mode(true); |
| 468 | |
| 469 | ex.set_blob_allocator(&allocator); |
| 470 | ex.set_workspace_allocator(&allocator); |
| 471 | |
| 472 | // prepare Input blobs |
| 473 | for (size_t i = 0; i < layer_count; i++) |
| 474 | { |
| 475 | const ncnn::Layer* layer = layers[i]; |
| 476 | if (layer->type == "ncnnfused") |
| 477 | continue; |
| 478 | |
| 479 | if (layer->type != "Input") |
| 480 | continue; |
| 481 | |
| 482 | ncnn::Input* input = (ncnn::Input*)layer; |
| 483 | |
| 484 | int w = input->w; |
| 485 | int h = input->h; |
| 486 | int c = input->c; |
| 487 | |
| 488 | int dims = 0; |
| 489 | if (w == 0 && h == 0 && c == 0) dims = 0; |
| 490 | if (w != 0 && h == 0 && c == 0) dims = 1; |
| 491 | if (w != 0 && h != 0 && c == 0) dims = 2; |
| 492 | if (w != 0 && h != 0 && c != 0) dims = 3; |
| 493 | |
| 494 | if (dims == 0) |
| 495 | { |
| 496 | fprintf(stderr, "Input layer %s without shape info, estimate_memory_footprint skipped\n", layer->name.c_str()); |
| 497 | return -1; |
| 498 | } |
| 499 | |
| 500 | ncnn::Mat m; |
| 501 | if (dims == 1) m.create(w, 4u, &allocator); |
| 502 | if (dims == 2) m.create(w, h, 4u, &allocator); |
| 503 | if (dims == 3) m.create(w, h, c, 4u, &allocator); |
| 504 | |
| 505 | ex.input(layer->tops[0], m); |
| 506 | |
| 507 | fprintf(stderr, "input = %s\n", blobs[layer->tops[0]].name.c_str()); |
| 508 | } |
| 509 | |
| 510 | // find output blobs and do inference |
| 511 | std::vector<ncnn::Mat> outputs; |
| 512 | for (size_t i = 0; i < blob_count; i++) |
| 513 | { |
| 514 | const ncnn::Blob& blob = blobs[i]; |
| 515 | |
| 516 | if (blob.producer == -1 || blob.consumer != -1) |
| 517 | continue; |
| 518 | |
| 519 | if (layers[blob.producer]->type == "ncnnfused") |
| 520 | continue; |
| 521 | |
| 522 | // treat blob without any consumers as output |
| 523 | ncnn::Mat m; |
| 524 | ex.extract(int(i), m); |
| 525 | outputs.push_back(m); |
| 526 | |
| 527 | fprintf(stderr, "extract = %s\n", blob.name.c_str()); |
| 528 | } |
| 529 | |
| 530 | fprintf(stderr, "estimated memory footprint = %.2f KB = %.2f MB\n", allocator.memory_footprint / 1024.f, allocator.memory_footprint / 1024.f / 1024.f); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | int ModelWriter::fprintf_param_int_array(int id, const ncnn::Mat& m, FILE* pp) |
| 536 | { |
| 537 | const int count = m.w; |
| 538 | const int* ptr = m; |
| 539 | |
| 540 | fprintf(pp, " -%d=%d", 23300 + id, count); |
| 541 | for (int i = 0; i < count; i++) |
| 542 | { |
| 543 | fprintf(pp, ",%d", ptr[i]); |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | int ModelWriter::fprintf_param_float_array(int id, const ncnn::Mat& m, FILE* pp) |
| 550 | { |
| 551 | const int count = m.w; |
| 552 | const float* ptr = m; |
| 553 | |
| 554 | fprintf(pp, " -%d=%d", 23300 + id, count); |
| 555 | for (int i = 0; i < count; i++) |
| 556 | { |
| 557 | fprintf(pp, ",%e", ptr[i]); |
| 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static inline size_t alignSize(size_t sz, int n) |
| 564 | { |
| 565 | return (sz + n - 1) & -n; |
| 566 | } |
| 567 | |
| 568 | static void replace_denormals_with_zero(float* data, size_t data_length) |
| 569 | { |
| 570 | const int total = static_cast<int>(data_length); |
| 571 | for (size_t i = 0; i < data_length; ++i) |
| 572 | { |
| 573 | float value = data[i]; |
| 574 | |
| 575 | if (fabsf(value) < 1e-30 && fabsf(value) != 0.f) |
| 576 | { |
| 577 | data[i] = 0.f; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | static float RandomFloat(float a = -1.2f, float b = 1.2f) |
| 583 | { |
| 584 | float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX; |
| 585 | float diff = b - a; |
| 586 | float r = random * diff; |
| 587 | return a + r; |
| 588 | } |
| 589 | |
| 590 | static void Randomize(ncnn::Mat& m, float a = -1.2f, float b = 1.2f) |
| 591 | { |
| 592 | if (m.elemsize == 4) |
| 593 | { |
| 594 | for (size_t i = 0; i < m.total(); i++) |
| 595 | { |
| 596 | m[i] = RandomFloat(a, b); |
| 597 | } |
| 598 | } |
| 599 | else if (m.elemsize == 2) |
| 600 | { |
| 601 | unsigned short* p = m; |
| 602 | for (size_t i = 0; i < m.total(); i++) |
| 603 | { |
| 604 | p[i] = ncnn::float32_to_float16(RandomFloat(a, b)); |
| 605 | } |
| 606 | } |
| 607 | else if (m.elemsize == 1) |
| 608 | { |
| 609 | signed char* p = m; |
| 610 | for (size_t i = 0; i < m.total(); i++) |
| 611 | { |
| 612 | p[i] = (signed char)RandomFloat(-127, 127); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | int ModelWriter::fwrite_weight_tag_data(const ncnn::Mat& data, FILE* bp, float a, float b) |
| 618 | { |
| 619 | int p0 = ftell(bp); |
| 620 | |
| 621 | ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.d * data.c); |
| 622 | if (gen_random_weight) |
| 623 | Randomize(data_flattened, a, b); |
| 624 | |
| 625 | if (data_flattened.elemsize == 4) |
| 626 | { |
| 627 | if (storage_type == 1) |
| 628 | { |
| 629 | const int tag = 0x01306B47; // fp16 magic |
| 630 | fwrite(&tag, sizeof(int), 1, bp); |
| 631 | ncnn::Mat data_flattened_fp16; |
| 632 | ncnn::cast_float32_to_float16(data_flattened, data_flattened_fp16); |
| 633 | fwrite(data_flattened_fp16.data, data_flattened_fp16.elemsize, data_flattened_fp16.w, bp); |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | const int tag = 0; // fp32 magic |
| 638 | fwrite(&tag, sizeof(int), 1, bp); |
| 639 | replace_denormals_with_zero(data_flattened, data_flattened.w); |
| 640 | fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp); |
| 641 | } |
| 642 | } |
| 643 | else if (data_flattened.elemsize == 2) |
| 644 | { |
| 645 | const int tag = 0x01306B47; // fp16 magic |
| 646 | fwrite(&tag, sizeof(int), 1, bp); |
| 647 | fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp); |
| 648 | } |
| 649 | else if (data_flattened.elemsize == 1) |
| 650 | { |
| 651 | const int tag = 0x000D4B38; // int8 magic |
| 652 | fwrite(&tag, sizeof(int), 1, bp); |
| 653 | fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp); |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | fprintf(stderr, "unknown weight data type %d\n", (int)data_flattened.elemsize); |
| 658 | } |
| 659 | |
| 660 | // padding to 32bit align |
| 661 | int nwrite = ftell(bp) - p0; |
| 662 | size_t nalign = alignSize(nwrite, 4); |
| 663 | unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00}; |
| 664 | fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | int ModelWriter::fwrite_weight_data(const ncnn::Mat& data, FILE* bp, float a, float b) |
| 670 | { |
| 671 | int p0 = ftell(bp); |
| 672 | |
| 673 | ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.d * data.c); |
| 674 | if (gen_random_weight) |
| 675 | Randomize(data_flattened, a, b); |
| 676 | |
| 677 | if (data_flattened.elemsize == 4) // fp32 |
| 678 | { |
| 679 | replace_denormals_with_zero(data_flattened, data_flattened.w); |
| 680 | } |
| 681 | |
| 682 | fwrite(data_flattened.data, data_flattened.elemsize, data_flattened.w, bp); |
| 683 | |
| 684 | // padding to 32bit align |
| 685 | int nwrite = ftell(bp) - p0; |
| 686 | size_t nalign = alignSize(nwrite, 4); |
| 687 | unsigned char padding[4] = {0x00, 0x00, 0x00, 0x00}; |
| 688 | fwrite(padding, sizeof(unsigned char), nalign - nwrite, bp); |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | int ModelWriter::save(const char* parampath, const char* binpath) |
| 694 | { |
| 695 | uint64_t mac = 0; |
| 696 | |
| 697 | FILE* pp = fopen(parampath, "wb"); |
| 698 | FILE* bp = fopen(binpath, "wb"); |
| 699 | |
| 700 | fprintf(pp, "7767517\n"); |
| 701 | |
| 702 | const size_t layer_count = layers.size(); |
| 703 | |
| 704 | int layer_count_fused = 0; |
| 705 | std::set<std::string> blob_names; |
| 706 | for (size_t i = 0; i < layer_count; i++) |
| 707 | { |
| 708 | const ncnn::Layer* layer = layers[i]; |
| 709 | if (layer->type == "ncnnfused") |
| 710 | continue; |
| 711 | |
| 712 | layer_count_fused++; |
| 713 | |
| 714 | size_t bottom_count = layer->bottoms.size(); |
| 715 | for (size_t j = 0; j < bottom_count; j++) |
| 716 | { |
| 717 | int bottom_blob_index = layer->bottoms[j]; |
| 718 | blob_names.insert(blobs[bottom_blob_index].name); |
| 719 | } |
| 720 | |
| 721 | size_t top_count = layer->tops.size(); |
| 722 | for (size_t j = 0; j < top_count; j++) |
| 723 | { |
| 724 | int top_blob_index = layer->tops[j]; |
| 725 | blob_names.insert(blobs[top_blob_index].name); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | size_t blob_count_fused = blob_names.size(); |
| 730 | |
| 731 | fprintf(pp, "%d %zd\n", layer_count_fused, blob_count_fused); |
| 732 | |
| 733 | for (size_t i = 0; i < layer_count; i++) |
| 734 | { |
| 735 | const ncnn::Layer* layer = layers[i]; |
| 736 | if (layer->type == "ncnnfused") |
| 737 | continue; |
| 738 | |
| 739 | if (cutstart > 0 && i < cutstart) |
| 740 | continue; |
| 741 | |
| 742 | if (cutend > 0 && i > cutend) |
| 743 | continue; |
| 744 | |
| 745 | size_t bottom_count = layer->bottoms.size(); |
| 746 | size_t top_count = layer->tops.size(); |
| 747 | |
| 748 | fprintf(pp, "%-24s %-24s %zd %zd", layer->type.c_str(), layer->name.c_str(), bottom_count, top_count); |
| 749 | |
| 750 | for (size_t j = 0; j < bottom_count; j++) |
| 751 | { |
| 752 | int bottom_blob_index = layer->bottoms[j]; |
| 753 | fprintf(pp, " %s", blobs[bottom_blob_index].name.c_str()); |
| 754 | } |
| 755 | for (size_t j = 0; j < top_count; j++) |
| 756 | { |
| 757 | int top_blob_index = layer->tops[j]; |
| 758 | fprintf(pp, " %s", blobs[top_blob_index].name.c_str()); |
| 759 | } |
| 760 | |
| 761 | // write shape hints |
| 762 | bool shape_ready = true; |
| 763 | for (size_t j = 0; j < top_count; j++) |
| 764 | { |
| 765 | int top_blob_index = layer->tops[j]; |
| 766 | |
| 767 | int dims = blobs[top_blob_index].shape.dims; |
| 768 | if (dims == 0) |
| 769 | { |
| 770 | shape_ready = false; |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | if (shape_ready) |
| 775 | { |
| 776 | fprintf(pp, " -23330=%zd", top_count * 4); |
| 777 | for (size_t j = 0; j < top_count; j++) |
| 778 | { |
| 779 | int top_blob_index = layer->tops[j]; |
| 780 | |
| 781 | int dims = blobs[top_blob_index].shape.dims; |
| 782 | int w = blobs[top_blob_index].shape.w; |
| 783 | int h = blobs[top_blob_index].shape.h; |
| 784 | int c = blobs[top_blob_index].shape.c; |
| 785 | |
| 786 | fprintf(pp, ",%d,%d,%d,%d", dims, w, h, c); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // custom op |
| 791 | if (layer->typeindex & ncnn::LayerType::CustomBit) |
| 792 | { |
| 793 | ((CustomLayer*)layer)->write_param(pp); |
| 794 | |
| 795 | fprintf(pp, "\n"); |
| 796 | |
| 797 | continue; |
| 798 | } |
| 799 | |
| 800 | ncnn::Layer* layer_default = ncnn::create_layer_cpu(layer->typeindex); |
| 801 | |
| 802 | ncnn::ParamDict pd; |
| 803 | layer_default->load_param(pd); |
| 804 | |
| 805 | #define fprintf_param_value(format, phase) \ |
| 806 | { \ |
| 807 | if (op->phase != op_default->phase) fprintf(pp, format, op->phase); \ |
| 808 | } |
| 809 | |
| 810 | if (layer->type == "BatchNorm") |
| 811 | { |
| 812 | ncnn::BatchNorm* op = (ncnn::BatchNorm*)layer; |
| 813 | ncnn::BatchNorm* op_default = (ncnn::BatchNorm*)layer_default; |
| 814 | |
| 815 | fprintf_param_value(" 0=%d", channels) |
| 816 | fprintf_param_value(" 1=%e", eps) |
| 817 | |
| 818 | fwrite_weight_data(op->slope_data, bp); |
| 819 | fwrite_weight_data(op->mean_data, bp); |
| 820 | fwrite_weight_data(op->var_data, bp); |
| 821 | fwrite_weight_data(op->bias_data, bp); |
| 822 | } |
| 823 | else if (layer->type == "Bias") |
| 824 | { |
| 825 | ncnn::Bias* op = (ncnn::Bias*)layer; |
| 826 | ncnn::Bias* op_default = (ncnn::Bias*)layer_default; |
| 827 | |
| 828 | fprintf_param_value(" 0=%d", bias_data_size) |
| 829 | |
| 830 | fwrite_weight_data(op->bias_data, bp); |
| 831 | } |
| 832 | else if (layer->type == "BinaryOp") |
| 833 | { |
| 834 | ncnn::BinaryOp* op = (ncnn::BinaryOp*)layer; |
| 835 | ncnn::BinaryOp* op_default = (ncnn::BinaryOp*)layer_default; |
| 836 | |
| 837 | fprintf_param_value(" 0=%d", op_type) |
| 838 | fprintf_param_value(" 1=%d", with_scalar) |
| 839 | fprintf_param_value(" 2=%e", b) |
| 840 | } |
| 841 | else if (layer->type == "CELU") |
| 842 | { |
| 843 | ncnn::CELU* op = (ncnn::CELU*)layer; |
| 844 | ncnn::CELU* op_default = (ncnn::CELU*)layer_default; |
| 845 | |
| 846 | fprintf_param_value(" 0=%e", alpha) |
| 847 | } |
| 848 | else if (layer->type == "Clip") |
| 849 | { |
| 850 | ncnn::Clip* op = (ncnn::Clip*)layer; |
| 851 | ncnn::Clip* op_default = (ncnn::Clip*)layer_default; |
| 852 | |
| 853 | fprintf_param_value(" 0=%e", min) |
| 854 | fprintf_param_value(" 1=%e", max) |
| 855 | } |
| 856 | else if (layer->type == "Concat") |
| 857 | { |
| 858 | ncnn::Concat* op = (ncnn::Concat*)layer; |
| 859 | ncnn::Concat* op_default = (ncnn::Concat*)layer_default; |
| 860 | |
| 861 | fprintf_param_value(" 0=%d", axis) |
| 862 | } |
| 863 | else if (layer->type == "Convolution") |
| 864 | { |
| 865 | ncnn::Convolution* op = (ncnn::Convolution*)layer; |
| 866 | ncnn::Convolution* op_default = (ncnn::Convolution*)layer_default; |
| 867 | |
| 868 | fprintf_param_value(" 0=%d", num_output) |
| 869 | fprintf_param_value(" 1=%d", kernel_w) |
| 870 | { |
| 871 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 872 | } |
| 873 | fprintf_param_value(" 2=%d", dilation_w) |
| 874 | { |
| 875 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 876 | } |
| 877 | fprintf_param_value(" 3=%d", stride_w) |
| 878 | { |
| 879 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 880 | } |
| 881 | fprintf_param_value(" 4=%d", pad_left) |
| 882 | { |
| 883 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 884 | } |
| 885 | { |
| 886 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 887 | } |
| 888 | { |
| 889 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 890 | } |
| 891 | fprintf_param_value(" 18=%e", pad_value) |
| 892 | fprintf_param_value(" 5=%d", bias_term) |
| 893 | fprintf_param_value(" 6=%d", weight_data_size) |
| 894 | fprintf_param_value(" 8=%d", int8_scale_term) |
| 895 | fprintf_param_value(" 9=%d", activation_type) |
| 896 | { |
| 897 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 898 | } |
| 899 | fprintf_param_value(" 19=%d", dynamic_weight) |
| 900 | |
| 901 | if (op->dynamic_weight == 0) |
| 902 | { |
| 903 | fwrite_weight_tag_data(op->weight_data, bp); |
| 904 | fwrite_weight_data(op->bias_data, bp); |
| 905 | |
| 906 | #if NCNN_INT8 |
| 907 | // write int8_scale data |
| 908 | if (op->int8_scale_term) |
| 909 | { |
| 910 | fwrite_weight_data(op->weight_data_int8_scales, bp, 90, 100); |
| 911 | fwrite_weight_data(op->bottom_blob_int8_scales, bp, 0.001, 1); |
| 912 | fwrite_weight_data(op->top_blob_int8_scales, bp, 0.001, 1); |
| 913 | } |
| 914 | #endif // NCNN_INT8 |
| 915 | } |
| 916 | |
| 917 | if (shape_ready) |
| 918 | { |
| 919 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 920 | int outw = blobs[layer->tops[0]].shape.w; |
| 921 | int outh = blobs[layer->tops[0]].shape.h; |
| 922 | int outc = blobs[layer->tops[0]].shape.c; |
| 923 | |
| 924 | mac += (uint64_t)op->kernel_h * op->kernel_w * outw * outh * outc * inc; |
| 925 | } |
| 926 | } |
| 927 | else if (layer->type == "Convolution1D") |
| 928 | { |
| 929 | ncnn::Convolution1D* op = (ncnn::Convolution1D*)layer; |
| 930 | ncnn::Convolution1D* op_default = (ncnn::Convolution1D*)layer_default; |
| 931 | |
| 932 | fprintf_param_value(" 0=%d", num_output) |
| 933 | fprintf_param_value(" 1=%d", kernel_w) |
| 934 | fprintf_param_value(" 2=%d", dilation_w) |
| 935 | fprintf_param_value(" 3=%d", stride_w) |
| 936 | fprintf_param_value(" 4=%d", pad_left) |
| 937 | { |
| 938 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 939 | } |
| 940 | fprintf_param_value(" 18=%e", pad_value) |
| 941 | fprintf_param_value(" 5=%d", bias_term) |
| 942 | fprintf_param_value(" 6=%d", weight_data_size) |
| 943 | fprintf_param_value(" 9=%d", activation_type) |
| 944 | { |
| 945 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 946 | } |
| 947 | fprintf_param_value(" 19=%d", dynamic_weight) |
| 948 | |
| 949 | if (op->dynamic_weight == 0) |
| 950 | { |
| 951 | fwrite_weight_tag_data(op->weight_data, bp); |
| 952 | fwrite_weight_data(op->bias_data, bp); |
| 953 | } |
| 954 | |
| 955 | if (shape_ready) |
| 956 | { |
| 957 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 958 | int outw = blobs[layer->tops[0]].shape.w; |
| 959 | int outh = blobs[layer->tops[0]].shape.h; |
| 960 | |
| 961 | mac += (uint64_t)op->kernel_w * outw * outh * inh; |
| 962 | } |
| 963 | } |
| 964 | else if (layer->type == "Convolution3D") |
| 965 | { |
| 966 | ncnn::Convolution3D* op = (ncnn::Convolution3D*)layer; |
| 967 | ncnn::Convolution3D* op_default = (ncnn::Convolution3D*)layer_default; |
| 968 | |
| 969 | fprintf_param_value(" 0=%d", num_output) |
| 970 | fprintf_param_value(" 1=%d", kernel_w) |
| 971 | { |
| 972 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 973 | if (op->kernel_d != op->kernel_w) fprintf(pp, " 21=%d", op->kernel_d); |
| 974 | } |
| 975 | fprintf_param_value(" 2=%d", dilation_w) |
| 976 | { |
| 977 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 978 | if (op->dilation_d != op->dilation_w) fprintf(pp, " 22=%d", op->dilation_d); |
| 979 | } |
| 980 | fprintf_param_value(" 3=%d", stride_w) |
| 981 | { |
| 982 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 983 | if (op->stride_d != op->stride_w) fprintf(pp, " 23=%d", op->stride_d); |
| 984 | } |
| 985 | fprintf_param_value(" 4=%d", pad_left) |
| 986 | { |
| 987 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 988 | if (op->pad_front != op->pad_left) fprintf(pp, " 24=%d", op->pad_front); |
| 989 | } |
| 990 | { |
| 991 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 992 | } |
| 993 | { |
| 994 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 995 | } |
| 996 | { |
| 997 | if (op->pad_behind != op->pad_front) fprintf(pp, " 17=%d", op->pad_behind); |
| 998 | } |
| 999 | fprintf_param_value(" 18=%e", pad_value) |
| 1000 | fprintf_param_value(" 5=%d", bias_term) |
| 1001 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1002 | fprintf_param_value(" 9=%d", activation_type) |
| 1003 | { |
| 1004 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1005 | } |
| 1006 | |
| 1007 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1008 | fwrite_weight_data(op->bias_data, bp); |
| 1009 | |
| 1010 | if (shape_ready) |
| 1011 | { |
| 1012 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1013 | int outw = blobs[layer->tops[0]].shape.w; |
| 1014 | int outh = blobs[layer->tops[0]].shape.h; |
| 1015 | int outd = blobs[layer->tops[0]].shape.d; |
| 1016 | int outc = blobs[layer->tops[0]].shape.c; |
| 1017 | |
| 1018 | mac += (uint64_t)op->kernel_d * op->kernel_h * op->kernel_w * outw * outh * outd * outc * inc; |
| 1019 | } |
| 1020 | } |
| 1021 | else if (layer->type == "ConvolutionDepthWise") |
| 1022 | { |
| 1023 | ncnn::ConvolutionDepthWise* op = (ncnn::ConvolutionDepthWise*)layer; |
| 1024 | ncnn::ConvolutionDepthWise* op_default = (ncnn::ConvolutionDepthWise*)layer_default; |
| 1025 | |
| 1026 | fprintf_param_value(" 0=%d", num_output) |
| 1027 | fprintf_param_value(" 1=%d", kernel_w) |
| 1028 | { |
| 1029 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1030 | } |
| 1031 | fprintf_param_value(" 2=%d", dilation_w) |
| 1032 | { |
| 1033 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1034 | } |
| 1035 | fprintf_param_value(" 3=%d", stride_w) |
| 1036 | { |
| 1037 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1038 | } |
| 1039 | fprintf_param_value(" 4=%d", pad_left) |
| 1040 | { |
| 1041 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1042 | } |
| 1043 | { |
| 1044 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1045 | } |
| 1046 | { |
| 1047 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1048 | } |
| 1049 | fprintf_param_value(" 18=%e", pad_value) |
| 1050 | fprintf_param_value(" 5=%d", bias_term) |
| 1051 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1052 | fprintf_param_value(" 7=%d", group) |
| 1053 | fprintf_param_value(" 8=%d", int8_scale_term) |
| 1054 | fprintf_param_value(" 9=%d", activation_type) |
| 1055 | { |
| 1056 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1057 | } |
| 1058 | fprintf_param_value(" 19=%d", dynamic_weight) |
| 1059 | |
| 1060 | if (op->dynamic_weight == 0) |
| 1061 | { |
| 1062 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1063 | fwrite_weight_data(op->bias_data, bp); |
| 1064 | |
| 1065 | #if NCNN_INT8 |
| 1066 | // write int8_scale data |
| 1067 | if (op->int8_scale_term == 1 || op->int8_scale_term == 101) |
| 1068 | { |
| 1069 | op->bottom_blob_int8_scales.w = 1; |
| 1070 | } |
| 1071 | if (op->int8_scale_term == 2 || op->int8_scale_term == 102) |
| 1072 | { |
| 1073 | op->weight_data_int8_scales.w = 1; |
| 1074 | op->bottom_blob_int8_scales.w = 1; |
| 1075 | } |
| 1076 | if (op->int8_scale_term > 100) |
| 1077 | { |
| 1078 | op->top_blob_int8_scales.w = 1; |
| 1079 | } |
| 1080 | |
| 1081 | if (op->int8_scale_term) |
| 1082 | { |
| 1083 | fwrite_weight_data(op->weight_data_int8_scales, bp, 90, 100); |
| 1084 | fwrite_weight_data(op->bottom_blob_int8_scales, bp, 0.001, 1); |
| 1085 | fwrite_weight_data(op->top_blob_int8_scales, bp, 0.001, 1); |
| 1086 | } |
| 1087 | #endif // NCNN_INT8 |
| 1088 | } |
| 1089 | |
| 1090 | if (shape_ready) |
| 1091 | { |
| 1092 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1093 | int outw = blobs[layer->tops[0]].shape.w; |
| 1094 | int outh = blobs[layer->tops[0]].shape.h; |
| 1095 | int outc = blobs[layer->tops[0]].shape.c; |
| 1096 | |
| 1097 | mac += (uint64_t)op->kernel_h * op->kernel_w * outw * outh * (outc / op->group) * (inc / op->group) * op->group; |
| 1098 | } |
| 1099 | } |
| 1100 | else if (layer->type == "ConvolutionDepthWise1D") |
| 1101 | { |
| 1102 | ncnn::ConvolutionDepthWise1D* op = (ncnn::ConvolutionDepthWise1D*)layer; |
| 1103 | ncnn::ConvolutionDepthWise1D* op_default = (ncnn::ConvolutionDepthWise1D*)layer_default; |
| 1104 | |
| 1105 | fprintf_param_value(" 0=%d", num_output) |
| 1106 | fprintf_param_value(" 1=%d", kernel_w) |
| 1107 | fprintf_param_value(" 2=%d", dilation_w) |
| 1108 | fprintf_param_value(" 3=%d", stride_w) |
| 1109 | fprintf_param_value(" 4=%d", pad_left) |
| 1110 | { |
| 1111 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1112 | } |
| 1113 | fprintf_param_value(" 18=%e", pad_value) |
| 1114 | fprintf_param_value(" 5=%d", bias_term) |
| 1115 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1116 | fprintf_param_value(" 7=%d", group) |
| 1117 | fprintf_param_value(" 9=%d", activation_type) |
| 1118 | { |
| 1119 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1120 | } |
| 1121 | fprintf_param_value(" 19=%d", dynamic_weight) |
| 1122 | |
| 1123 | if (op->dynamic_weight == 0) |
| 1124 | { |
| 1125 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1126 | fwrite_weight_data(op->bias_data, bp); |
| 1127 | } |
| 1128 | |
| 1129 | if (shape_ready) |
| 1130 | { |
| 1131 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1132 | int outw = blobs[layer->tops[0]].shape.w; |
| 1133 | int outh = blobs[layer->tops[0]].shape.h; |
| 1134 | |
| 1135 | mac += (uint64_t)op->kernel_w * outw * (outh / op->group) * (inh / op->group) * op->group; |
| 1136 | } |
| 1137 | } |
| 1138 | else if (layer->type == "ConvolutionDepthWise3D") |
| 1139 | { |
| 1140 | ncnn::ConvolutionDepthWise3D* op = (ncnn::ConvolutionDepthWise3D*)layer; |
| 1141 | ncnn::ConvolutionDepthWise3D* op_default = (ncnn::ConvolutionDepthWise3D*)layer_default; |
| 1142 | |
| 1143 | fprintf_param_value(" 0=%d", num_output) |
| 1144 | fprintf_param_value(" 1=%d", kernel_w) |
| 1145 | { |
| 1146 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1147 | if (op->kernel_d != op->kernel_w) fprintf(pp, " 21=%d", op->kernel_d); |
| 1148 | } |
| 1149 | fprintf_param_value(" 2=%d", dilation_w) |
| 1150 | { |
| 1151 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1152 | if (op->dilation_d != op->dilation_w) fprintf(pp, " 22=%d", op->dilation_d); |
| 1153 | } |
| 1154 | fprintf_param_value(" 3=%d", stride_w) |
| 1155 | { |
| 1156 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1157 | if (op->stride_d != op->stride_w) fprintf(pp, " 23=%d", op->stride_d); |
| 1158 | } |
| 1159 | fprintf_param_value(" 4=%d", pad_left) |
| 1160 | { |
| 1161 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1162 | if (op->pad_front != op->pad_left) fprintf(pp, " 24=%d", op->pad_front); |
| 1163 | } |
| 1164 | { |
| 1165 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1166 | } |
| 1167 | { |
| 1168 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1169 | } |
| 1170 | { |
| 1171 | if (op->pad_behind != op->pad_front) fprintf(pp, " 17=%d", op->pad_behind); |
| 1172 | } |
| 1173 | fprintf_param_value(" 18=%e", pad_value) |
| 1174 | fprintf_param_value(" 5=%d", bias_term) |
| 1175 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1176 | fprintf_param_value(" 7=%d", group) |
| 1177 | fprintf_param_value(" 9=%d", activation_type) |
| 1178 | { |
| 1179 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1180 | } |
| 1181 | |
| 1182 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1183 | fwrite_weight_data(op->bias_data, bp); |
| 1184 | |
| 1185 | if (shape_ready) |
| 1186 | { |
| 1187 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1188 | int outw = blobs[layer->tops[0]].shape.w; |
| 1189 | int outh = blobs[layer->tops[0]].shape.h; |
| 1190 | int outd = blobs[layer->tops[0]].shape.d; |
| 1191 | int outc = blobs[layer->tops[0]].shape.c; |
| 1192 | |
| 1193 | mac += (uint64_t)op->kernel_d * op->kernel_h * op->kernel_w * outw * outh * outd * (outc / op->group) * (inc / op->group) * op->group; |
| 1194 | } |
| 1195 | } |
| 1196 | else if (layer->type == "CopyTo") |
| 1197 | { |
| 1198 | ncnn::CopyTo* op = (ncnn::CopyTo*)layer; |
| 1199 | ncnn::CopyTo* op_default = (ncnn::CopyTo*)layer_default; |
| 1200 | |
| 1201 | fprintf_param_value(" 0=%d", woffset) |
| 1202 | fprintf_param_value(" 1=%d", hoffset) |
| 1203 | fprintf_param_value(" 13=%d", doffset) |
| 1204 | fprintf_param_value(" 2=%d", coffset) |
| 1205 | { |
| 1206 | if (!op->starts.empty()) fprintf_param_int_array(9, op->starts, pp); |
| 1207 | } |
| 1208 | { |
| 1209 | if (!op->axes.empty()) fprintf_param_int_array(11, op->axes, pp); |
| 1210 | } |
| 1211 | } |
| 1212 | else if (layer->type == "Crop") |
| 1213 | { |
| 1214 | ncnn::Crop* op = (ncnn::Crop*)layer; |
| 1215 | ncnn::Crop* op_default = (ncnn::Crop*)layer_default; |
| 1216 | |
| 1217 | fprintf_param_value(" 0=%d", woffset) |
| 1218 | fprintf_param_value(" 1=%d", hoffset) |
| 1219 | fprintf_param_value(" 13=%d", doffset) |
| 1220 | fprintf_param_value(" 2=%d", coffset) |
| 1221 | fprintf_param_value(" 3=%d", outw) |
| 1222 | fprintf_param_value(" 4=%d", outh) |
| 1223 | fprintf_param_value(" 14=%d", outd) |
| 1224 | fprintf_param_value(" 5=%d", outc) |
| 1225 | fprintf_param_value(" 6=%d", woffset2) |
| 1226 | fprintf_param_value(" 7=%d", hoffset2) |
| 1227 | fprintf_param_value(" 15=%d", doffset2) |
| 1228 | fprintf_param_value(" 8=%d", coffset2) |
| 1229 | { |
| 1230 | if (!op->starts.empty()) fprintf_param_int_array(9, op->starts, pp); |
| 1231 | } |
| 1232 | { |
| 1233 | if (!op->ends.empty()) fprintf_param_int_array(10, op->ends, pp); |
| 1234 | } |
| 1235 | { |
| 1236 | if (!op->axes.empty()) fprintf_param_int_array(11, op->axes, pp); |
| 1237 | } |
| 1238 | } |
| 1239 | else if (layer->type == "CumulativeSum") |
| 1240 | { |
| 1241 | ncnn::CumulativeSum* op = (ncnn::CumulativeSum*)layer; |
| 1242 | ncnn::CumulativeSum* op_default = (ncnn::CumulativeSum*)layer_default; |
| 1243 | |
| 1244 | fprintf_param_value(" 0=%d", axis) |
| 1245 | } |
| 1246 | else if (layer->type == "Deconvolution") |
| 1247 | { |
| 1248 | ncnn::Deconvolution* op = (ncnn::Deconvolution*)layer; |
| 1249 | ncnn::Deconvolution* op_default = (ncnn::Deconvolution*)layer_default; |
| 1250 | |
| 1251 | fprintf_param_value(" 0=%d", num_output) |
| 1252 | fprintf_param_value(" 1=%d", kernel_w) |
| 1253 | { |
| 1254 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1255 | } |
| 1256 | fprintf_param_value(" 2=%d", dilation_w) |
| 1257 | { |
| 1258 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1259 | } |
| 1260 | fprintf_param_value(" 3=%d", stride_w) |
| 1261 | { |
| 1262 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1263 | } |
| 1264 | fprintf_param_value(" 4=%d", pad_left) |
| 1265 | { |
| 1266 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1267 | } |
| 1268 | { |
| 1269 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1270 | } |
| 1271 | { |
| 1272 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1273 | } |
| 1274 | fprintf_param_value(" 18=%d", output_pad_right) |
| 1275 | { |
| 1276 | if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom); |
| 1277 | } |
| 1278 | fprintf_param_value(" 20=%d", output_w) |
| 1279 | { |
| 1280 | if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h); |
| 1281 | } |
| 1282 | fprintf_param_value(" 5=%d", bias_term) |
| 1283 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1284 | fprintf_param_value(" 9=%d", activation_type) |
| 1285 | { |
| 1286 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1287 | } |
| 1288 | fprintf_param_value(" 28=%d", dynamic_weight) |
| 1289 | |
| 1290 | if (op->dynamic_weight == 0) |
| 1291 | { |
| 1292 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1293 | fwrite_weight_data(op->bias_data, bp); |
| 1294 | } |
| 1295 | |
| 1296 | if (shape_ready) |
| 1297 | { |
| 1298 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1299 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1300 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1301 | int outc = blobs[layer->tops[0]].shape.c; |
| 1302 | |
| 1303 | mac += (uint64_t)op->kernel_h * op->kernel_w * inw * inh * outc * inc; |
| 1304 | } |
| 1305 | } |
| 1306 | else if (layer->type == "Deconvolution1D") |
| 1307 | { |
| 1308 | ncnn::Deconvolution1D* op = (ncnn::Deconvolution1D*)layer; |
| 1309 | ncnn::Deconvolution1D* op_default = (ncnn::Deconvolution1D*)layer_default; |
| 1310 | |
| 1311 | fprintf_param_value(" 0=%d", num_output) |
| 1312 | fprintf_param_value(" 1=%d", kernel_w) |
| 1313 | fprintf_param_value(" 2=%d", dilation_w) |
| 1314 | fprintf_param_value(" 3=%d", stride_w) |
| 1315 | fprintf_param_value(" 4=%d", pad_left) |
| 1316 | { |
| 1317 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1318 | } |
| 1319 | fprintf_param_value(" 18=%d", output_pad_right) |
| 1320 | fprintf_param_value(" 20=%d", output_w) |
| 1321 | fprintf_param_value(" 5=%d", bias_term) |
| 1322 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1323 | fprintf_param_value(" 9=%d", activation_type) |
| 1324 | { |
| 1325 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1326 | } |
| 1327 | fprintf_param_value(" 28=%d", dynamic_weight) |
| 1328 | |
| 1329 | if (op->dynamic_weight == 0) |
| 1330 | { |
| 1331 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1332 | fwrite_weight_data(op->bias_data, bp); |
| 1333 | } |
| 1334 | |
| 1335 | if (shape_ready) |
| 1336 | { |
| 1337 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1338 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1339 | int outh = blobs[layer->tops[0]].shape.h; |
| 1340 | |
| 1341 | mac += (uint64_t)op->kernel_w * inw * outh * inh; |
| 1342 | } |
| 1343 | } |
| 1344 | else if (layer->type == "Deconvolution3D") |
| 1345 | { |
| 1346 | ncnn::Deconvolution3D* op = (ncnn::Deconvolution3D*)layer; |
| 1347 | ncnn::Deconvolution3D* op_default = (ncnn::Deconvolution3D*)layer_default; |
| 1348 | |
| 1349 | fprintf_param_value(" 0=%d", num_output) |
| 1350 | fprintf_param_value(" 1=%d", kernel_w) |
| 1351 | { |
| 1352 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1353 | if (op->kernel_d != op->kernel_w) fprintf(pp, " 21=%d", op->kernel_d); |
| 1354 | } |
| 1355 | fprintf_param_value(" 2=%d", dilation_w) |
| 1356 | { |
| 1357 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1358 | if (op->dilation_d != op->dilation_w) fprintf(pp, " 22=%d", op->dilation_d); |
| 1359 | } |
| 1360 | fprintf_param_value(" 3=%d", stride_w) |
| 1361 | { |
| 1362 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1363 | if (op->stride_d != op->stride_w) fprintf(pp, " 23=%d", op->stride_d); |
| 1364 | } |
| 1365 | fprintf_param_value(" 4=%d", pad_left) |
| 1366 | { |
| 1367 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1368 | if (op->pad_front != op->pad_left) fprintf(pp, " 24=%d", op->pad_front); |
| 1369 | } |
| 1370 | { |
| 1371 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1372 | } |
| 1373 | { |
| 1374 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1375 | } |
| 1376 | { |
| 1377 | if (op->pad_behind != op->pad_front) fprintf(pp, " 17=%d", op->pad_behind); |
| 1378 | } |
| 1379 | fprintf_param_value(" 18=%d", output_pad_right) |
| 1380 | { |
| 1381 | if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom); |
| 1382 | if (op->output_pad_behind != op->output_pad_right) fprintf(pp, " 20=%d", op->output_pad_behind); |
| 1383 | } |
| 1384 | fprintf_param_value(" 25=%d", output_w) |
| 1385 | { |
| 1386 | if (op->output_h != op->output_w) fprintf(pp, " 26=%d", op->output_h); |
| 1387 | if (op->output_d != op->output_w) fprintf(pp, " 27=%d", op->output_d); |
| 1388 | } |
| 1389 | fprintf_param_value(" 5=%d", bias_term) |
| 1390 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1391 | fprintf_param_value(" 9=%d", activation_type) |
| 1392 | { |
| 1393 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1394 | } |
| 1395 | |
| 1396 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1397 | fwrite_weight_data(op->bias_data, bp); |
| 1398 | |
| 1399 | if (shape_ready) |
| 1400 | { |
| 1401 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1402 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1403 | int ind = blobs[layer->bottoms[0]].shape.d; |
| 1404 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1405 | int outc = blobs[layer->tops[0]].shape.c; |
| 1406 | |
| 1407 | mac += (uint64_t)op->kernel_d * op->kernel_h * op->kernel_w * inw * inh * ind * outc * inc; |
| 1408 | } |
| 1409 | } |
| 1410 | else if (layer->type == "DeconvolutionDepthWise") |
| 1411 | { |
| 1412 | ncnn::DeconvolutionDepthWise* op = (ncnn::DeconvolutionDepthWise*)layer; |
| 1413 | ncnn::DeconvolutionDepthWise* op_default = (ncnn::DeconvolutionDepthWise*)layer_default; |
| 1414 | |
| 1415 | fprintf_param_value(" 0=%d", num_output) |
| 1416 | fprintf_param_value(" 1=%d", kernel_w) |
| 1417 | { |
| 1418 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1419 | } |
| 1420 | fprintf_param_value(" 2=%d", dilation_w) |
| 1421 | { |
| 1422 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1423 | } |
| 1424 | fprintf_param_value(" 3=%d", stride_w) |
| 1425 | { |
| 1426 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1427 | } |
| 1428 | fprintf_param_value(" 4=%d", pad_left) |
| 1429 | { |
| 1430 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1431 | } |
| 1432 | { |
| 1433 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1434 | } |
| 1435 | { |
| 1436 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1437 | } |
| 1438 | fprintf_param_value(" 18=%d", output_pad_right) |
| 1439 | { |
| 1440 | if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom); |
| 1441 | } |
| 1442 | fprintf_param_value(" 20=%d", output_w) |
| 1443 | { |
| 1444 | if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h); |
| 1445 | } |
| 1446 | fprintf_param_value(" 5=%d", bias_term) |
| 1447 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1448 | fprintf_param_value(" 7=%d", group) |
| 1449 | fprintf_param_value(" 9=%d", activation_type) |
| 1450 | { |
| 1451 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1452 | } |
| 1453 | fprintf_param_value(" 28=%d", dynamic_weight) |
| 1454 | |
| 1455 | if (op->dynamic_weight == 0) |
| 1456 | { |
| 1457 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1458 | fwrite_weight_data(op->bias_data, bp); |
| 1459 | } |
| 1460 | |
| 1461 | if (shape_ready) |
| 1462 | { |
| 1463 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1464 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1465 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1466 | int outc = blobs[layer->tops[0]].shape.c; |
| 1467 | |
| 1468 | mac += (uint64_t)op->kernel_h * op->kernel_w * inw * inh * (outc / op->group) * (inc / op->group) * op->group; |
| 1469 | } |
| 1470 | } |
| 1471 | else if (layer->type == "DeconvolutionDepthWise1D") |
| 1472 | { |
| 1473 | ncnn::DeconvolutionDepthWise1D* op = (ncnn::DeconvolutionDepthWise1D*)layer; |
| 1474 | ncnn::DeconvolutionDepthWise1D* op_default = (ncnn::DeconvolutionDepthWise1D*)layer_default; |
| 1475 | |
| 1476 | fprintf_param_value(" 0=%d", num_output) |
| 1477 | fprintf_param_value(" 1=%d", kernel_w) |
| 1478 | fprintf_param_value(" 2=%d", dilation_w) |
| 1479 | fprintf_param_value(" 3=%d", stride_w) |
| 1480 | fprintf_param_value(" 4=%d", pad_left) |
| 1481 | { |
| 1482 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1483 | } |
| 1484 | fprintf_param_value(" 18=%d", output_pad_right) |
| 1485 | fprintf_param_value(" 20=%d", output_w) |
| 1486 | fprintf_param_value(" 5=%d", bias_term) |
| 1487 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1488 | fprintf_param_value(" 7=%d", group) |
| 1489 | fprintf_param_value(" 9=%d", activation_type) |
| 1490 | { |
| 1491 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1492 | } |
| 1493 | fprintf_param_value(" 28=%d", dynamic_weight) |
| 1494 | |
| 1495 | if (op->dynamic_weight == 0) |
| 1496 | { |
| 1497 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1498 | fwrite_weight_data(op->bias_data, bp); |
| 1499 | } |
| 1500 | |
| 1501 | if (shape_ready) |
| 1502 | { |
| 1503 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1504 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1505 | int outh = blobs[layer->tops[0]].shape.h; |
| 1506 | |
| 1507 | mac += (uint64_t)op->kernel_w * inw * (outh / op->group) * (inh / op->group) * op->group; |
| 1508 | } |
| 1509 | } |
| 1510 | else if (layer->type == "DeconvolutionDepthWise3D") |
| 1511 | { |
| 1512 | ncnn::DeconvolutionDepthWise3D* op = (ncnn::DeconvolutionDepthWise3D*)layer; |
| 1513 | ncnn::DeconvolutionDepthWise3D* op_default = (ncnn::DeconvolutionDepthWise3D*)layer_default; |
| 1514 | |
| 1515 | fprintf_param_value(" 0=%d", num_output) |
| 1516 | fprintf_param_value(" 1=%d", kernel_w) |
| 1517 | { |
| 1518 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1519 | if (op->kernel_d != op->kernel_w) fprintf(pp, " 21=%d", op->kernel_d); |
| 1520 | } |
| 1521 | fprintf_param_value(" 2=%d", dilation_w) |
| 1522 | { |
| 1523 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1524 | if (op->dilation_d != op->dilation_w) fprintf(pp, " 22=%d", op->dilation_d); |
| 1525 | } |
| 1526 | fprintf_param_value(" 3=%d", stride_w) |
| 1527 | { |
| 1528 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1529 | if (op->stride_d != op->stride_w) fprintf(pp, " 23=%d", op->stride_d); |
| 1530 | } |
| 1531 | fprintf_param_value(" 4=%d", pad_left) |
| 1532 | { |
| 1533 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1534 | if (op->pad_front != op->pad_left) fprintf(pp, " 24=%d", op->pad_front); |
| 1535 | } |
| 1536 | { |
| 1537 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1538 | } |
| 1539 | { |
| 1540 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1541 | } |
| 1542 | { |
| 1543 | if (op->pad_behind != op->pad_front) fprintf(pp, " 17=%d", op->pad_behind); |
| 1544 | } |
| 1545 | fprintf_param_value(" 18=%d", output_pad_right) |
| 1546 | { |
| 1547 | if (op->output_pad_bottom != op->output_pad_right) fprintf(pp, " 19=%d", op->output_pad_bottom); |
| 1548 | if (op->output_pad_behind != op->output_pad_right) fprintf(pp, " 20=%d", op->output_pad_behind); |
| 1549 | } |
| 1550 | fprintf_param_value(" 25=%d", output_w) |
| 1551 | { |
| 1552 | if (op->output_h != op->output_w) fprintf(pp, " 26=%d", op->output_h); |
| 1553 | if (op->output_d != op->output_w) fprintf(pp, " 27=%d", op->output_d); |
| 1554 | } |
| 1555 | fprintf_param_value(" 5=%d", bias_term) |
| 1556 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1557 | fprintf_param_value(" 7=%d", group) |
| 1558 | fprintf_param_value(" 9=%d", activation_type) |
| 1559 | { |
| 1560 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1561 | } |
| 1562 | |
| 1563 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1564 | fwrite_weight_data(op->bias_data, bp); |
| 1565 | |
| 1566 | if (shape_ready) |
| 1567 | { |
| 1568 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1569 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1570 | int ind = blobs[layer->bottoms[0]].shape.d; |
| 1571 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1572 | int outc = blobs[layer->tops[0]].shape.c; |
| 1573 | |
| 1574 | mac += (uint64_t)op->kernel_d * op->kernel_h * op->kernel_w * inw * inh * ind * (outc / op->group) * (inc / op->group) * op->group; |
| 1575 | } |
| 1576 | } |
| 1577 | else if (layer->type == "DeformableConv2D") |
| 1578 | { |
| 1579 | ncnn::DeformableConv2D* op = (ncnn::DeformableConv2D*)layer; |
| 1580 | ncnn::DeformableConv2D* op_default = (ncnn::DeformableConv2D*)layer_default; |
| 1581 | |
| 1582 | fprintf_param_value(" 0=%d", num_output) |
| 1583 | fprintf_param_value(" 1=%d", kernel_w) |
| 1584 | { |
| 1585 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1586 | } |
| 1587 | fprintf_param_value(" 2=%d", dilation_w) |
| 1588 | { |
| 1589 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1590 | } |
| 1591 | fprintf_param_value(" 3=%d", stride_w) |
| 1592 | { |
| 1593 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1594 | } |
| 1595 | fprintf_param_value(" 4=%d", pad_left) |
| 1596 | { |
| 1597 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1598 | } |
| 1599 | { |
| 1600 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1601 | } |
| 1602 | { |
| 1603 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1604 | } |
| 1605 | fprintf_param_value(" 5=%d", bias_term) |
| 1606 | fprintf_param_value(" 6=%d", weight_data_size) |
| 1607 | fprintf_param_value(" 9=%d", activation_type) |
| 1608 | { |
| 1609 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1610 | } |
| 1611 | |
| 1612 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1613 | fwrite_weight_data(op->bias_data, bp); |
| 1614 | |
| 1615 | if (shape_ready) |
| 1616 | { |
| 1617 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1618 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1619 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1620 | int outc = blobs[layer->tops[0]].shape.c; |
| 1621 | |
| 1622 | mac += (uint64_t)op->kernel_h * op->kernel_w * inw * inh * outc * inc; |
| 1623 | } |
| 1624 | } |
| 1625 | else if (layer->type == "DetectionOutput") |
| 1626 | { |
| 1627 | ncnn::DetectionOutput* op = (ncnn::DetectionOutput*)layer; |
| 1628 | ncnn::DetectionOutput* op_default = (ncnn::DetectionOutput*)layer_default; |
| 1629 | |
| 1630 | fprintf_param_value(" 0=%d", num_class) |
| 1631 | fprintf_param_value(" 1=%e", nms_threshold) |
| 1632 | fprintf_param_value(" 2=%d", nms_top_k) |
| 1633 | fprintf_param_value(" 3=%d", keep_top_k) |
| 1634 | fprintf_param_value(" 4=%e", confidence_threshold) |
| 1635 | fprintf_param_value(" 5=%e", variances[0]) |
| 1636 | fprintf_param_value(" 6=%e", variances[1]) |
| 1637 | fprintf_param_value(" 7=%e", variances[2]) |
| 1638 | fprintf_param_value(" 8=%e", variances[3]) |
| 1639 | } |
| 1640 | else if (layer->type == "Diag") |
| 1641 | { |
| 1642 | ncnn::Diag* op = (ncnn::Diag*)layer; |
| 1643 | ncnn::Diag* op_default = (ncnn::Diag*)layer_default; |
| 1644 | |
| 1645 | fprintf_param_value(" 0=%d", diagonal) |
| 1646 | } |
| 1647 | else if (layer->type == "Dropout") |
| 1648 | { |
| 1649 | ncnn::Dropout* op = (ncnn::Dropout*)layer; |
| 1650 | ncnn::Dropout* op_default = (ncnn::Dropout*)layer_default; |
| 1651 | |
| 1652 | fprintf_param_value(" 0=%e", scale) |
| 1653 | } |
| 1654 | else if (layer->type == "Eltwise") |
| 1655 | { |
| 1656 | ncnn::Eltwise* op = (ncnn::Eltwise*)layer; |
| 1657 | ncnn::Eltwise* op_default = (ncnn::Eltwise*)layer_default; |
| 1658 | |
| 1659 | fprintf_param_value(" 0=%d", op_type) |
| 1660 | { |
| 1661 | if (!op->coeffs.empty()) fprintf_param_float_array(1, op->coeffs, pp); |
| 1662 | } |
| 1663 | } |
| 1664 | else if (layer->type == "ELU") |
| 1665 | { |
| 1666 | ncnn::ELU* op = (ncnn::ELU*)layer; |
| 1667 | ncnn::ELU* op_default = (ncnn::ELU*)layer_default; |
| 1668 | |
| 1669 | fprintf_param_value(" 0=%e", alpha) |
| 1670 | } |
| 1671 | else if (layer->type == "Embed") |
| 1672 | { |
| 1673 | ncnn::Embed* op = (ncnn::Embed*)layer; |
| 1674 | ncnn::Embed* op_default = (ncnn::Embed*)layer_default; |
| 1675 | |
| 1676 | fprintf_param_value(" 0=%d", num_output) |
| 1677 | fprintf_param_value(" 1=%d", input_dim) |
| 1678 | fprintf_param_value(" 2=%d", bias_term) |
| 1679 | fprintf_param_value(" 3=%d", weight_data_size) |
| 1680 | fprintf_param_value(" 18=%d", int8_scale_term) |
| 1681 | |
| 1682 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1683 | fwrite_weight_data(op->bias_data, bp); |
| 1684 | |
| 1685 | #if NCNN_INT8 |
| 1686 | // write int8_scale data |
| 1687 | if (op->int8_scale_term) |
| 1688 | { |
| 1689 | ncnn::Mat weight_data_int8_scales(1); |
| 1690 | weight_data_int8_scales[0] = op->weight_data_int8_scale; |
| 1691 | fwrite_weight_data(weight_data_int8_scales, bp, 90, 100); |
| 1692 | } |
| 1693 | #endif // NCNN_INT8 |
| 1694 | } |
| 1695 | else if (layer->type == "Exp") |
| 1696 | { |
| 1697 | ncnn::Exp* op = (ncnn::Exp*)layer; |
| 1698 | ncnn::Exp* op_default = (ncnn::Exp*)layer_default; |
| 1699 | |
| 1700 | fprintf_param_value(" 0=%e", base) |
| 1701 | fprintf_param_value(" 1=%e", scale) |
| 1702 | fprintf_param_value(" 2=%e", shift) |
| 1703 | } |
| 1704 | else if (layer->type == "ExpandDims") |
| 1705 | { |
| 1706 | ncnn::ExpandDims* op = (ncnn::ExpandDims*)layer; |
| 1707 | ncnn::ExpandDims* op_default = (ncnn::ExpandDims*)layer_default; |
| 1708 | |
| 1709 | fprintf_param_value(" 0=%d", expand_w) |
| 1710 | fprintf_param_value(" 1=%d", expand_h) |
| 1711 | fprintf_param_value(" 11=%d", expand_d) |
| 1712 | fprintf_param_value(" 2=%d", expand_c) |
| 1713 | { |
| 1714 | if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp); |
| 1715 | } |
| 1716 | } |
| 1717 | else if (layer->type == "Fold") |
| 1718 | { |
| 1719 | ncnn::Fold* op = (ncnn::Fold*)layer; |
| 1720 | ncnn::Fold* op_default = (ncnn::Fold*)layer_default; |
| 1721 | |
| 1722 | fprintf_param_value(" 1=%d", kernel_w) |
| 1723 | { |
| 1724 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 1725 | } |
| 1726 | fprintf_param_value(" 2=%d", dilation_w) |
| 1727 | { |
| 1728 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 1729 | } |
| 1730 | fprintf_param_value(" 3=%d", stride_w) |
| 1731 | { |
| 1732 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 1733 | } |
| 1734 | fprintf_param_value(" 4=%d", pad_left) |
| 1735 | { |
| 1736 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 1737 | } |
| 1738 | { |
| 1739 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 1740 | } |
| 1741 | { |
| 1742 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 1743 | } |
| 1744 | fprintf_param_value(" 20=%d", output_w) |
| 1745 | { |
| 1746 | if (op->output_h != op->output_w) fprintf(pp, " 21=%d", op->output_h); |
| 1747 | } |
| 1748 | } |
| 1749 | else if (layer->type == "GELU") |
| 1750 | { |
| 1751 | ncnn::GELU* op = (ncnn::GELU*)layer; |
| 1752 | ncnn::GELU* op_default = (ncnn::GELU*)layer_default; |
| 1753 | |
| 1754 | fprintf_param_value(" 0=%d", fast_gelu) |
| 1755 | } |
| 1756 | else if (layer->type == "Gemm") |
| 1757 | { |
| 1758 | ncnn::Gemm* op = (ncnn::Gemm*)layer; |
| 1759 | ncnn::Gemm* op_default = (ncnn::Gemm*)layer_default; |
| 1760 | |
| 1761 | fprintf_param_value(" 0=%e", alpha) |
| 1762 | fprintf_param_value(" 1=%e", beta) |
| 1763 | fprintf_param_value(" 2=%d", transA) |
| 1764 | fprintf_param_value(" 3=%d", transB) |
| 1765 | fprintf_param_value(" 4=%d", constantA) |
| 1766 | fprintf_param_value(" 5=%d", constantB) |
| 1767 | fprintf_param_value(" 6=%d", constantC) |
| 1768 | fprintf_param_value(" 7=%d", constantM) |
| 1769 | fprintf_param_value(" 8=%d", constantN) |
| 1770 | fprintf_param_value(" 9=%d", constantK) |
| 1771 | fprintf_param_value(" 10=%d", constant_broadcast_type_C) |
| 1772 | fprintf_param_value(" 11=%d", output_N1M) |
| 1773 | fprintf_param_value(" 12=%d", output_elempack) |
| 1774 | fprintf_param_value(" 13=%d", output_elemtype) |
| 1775 | fprintf_param_value(" 14=%d", output_transpose) |
| 1776 | fprintf_param_value(" 18=%d", int8_scale_term) |
| 1777 | fprintf_param_value(" 20=%d", constant_TILE_M) |
| 1778 | fprintf_param_value(" 21=%d", constant_TILE_N) |
| 1779 | fprintf_param_value(" 22=%d", constant_TILE_K) |
| 1780 | |
| 1781 | if (op->constantA == 1) |
| 1782 | { |
| 1783 | fwrite_weight_tag_data(op->A_data, bp); |
| 1784 | } |
| 1785 | if (op->constantB == 1) |
| 1786 | { |
| 1787 | fwrite_weight_tag_data(op->B_data, bp); |
| 1788 | } |
| 1789 | if (op->constantC == 1 && op->constant_broadcast_type_C != -1) |
| 1790 | { |
| 1791 | fwrite_weight_tag_data(op->C_data, bp); |
| 1792 | } |
| 1793 | |
| 1794 | #if NCNN_INT8 |
| 1795 | // write int8_scale data |
| 1796 | if (op->int8_scale_term) |
| 1797 | { |
| 1798 | if (op->constantA == 1) |
| 1799 | { |
| 1800 | fwrite_weight_data(op->A_data_int8_scales, bp, 90, 100); |
| 1801 | } |
| 1802 | if (op->constantB == 1) |
| 1803 | { |
| 1804 | ncnn::Mat B_data_int8_scales(1); |
| 1805 | B_data_int8_scales[0] = op->B_data_int8_scale; |
| 1806 | fwrite_weight_data(B_data_int8_scales, bp, 90, 100); |
| 1807 | } |
| 1808 | } |
| 1809 | #endif // NCNN_INT8 |
| 1810 | } |
| 1811 | else if (layer->type == "GLU") |
| 1812 | { |
| 1813 | ncnn::GLU* op = (ncnn::GLU*)layer; |
| 1814 | ncnn::GLU* op_default = (ncnn::GLU*)layer_default; |
| 1815 | |
| 1816 | fprintf_param_value(" 0=%d", axis) |
| 1817 | } |
| 1818 | else if (layer->type == "GridSample") |
| 1819 | { |
| 1820 | ncnn::GridSample* op = (ncnn::GridSample*)layer; |
| 1821 | ncnn::GridSample* op_default = (ncnn::GridSample*)layer_default; |
| 1822 | |
| 1823 | fprintf_param_value(" 0=%d", sample_type) |
| 1824 | fprintf_param_value(" 1=%d", padding_mode) |
| 1825 | fprintf_param_value(" 2=%d", align_corner) |
| 1826 | fprintf_param_value(" 3=%d", permute_fusion) |
| 1827 | } |
| 1828 | else if (layer->type == "GroupNorm") |
| 1829 | { |
| 1830 | ncnn::GroupNorm* op = (ncnn::GroupNorm*)layer; |
| 1831 | ncnn::GroupNorm* op_default = (ncnn::GroupNorm*)layer_default; |
| 1832 | |
| 1833 | fprintf_param_value(" 0=%d", group) |
| 1834 | fprintf_param_value(" 1=%d", channels) |
| 1835 | fprintf_param_value(" 2=%e", eps) |
| 1836 | fprintf_param_value(" 3=%d", affine) |
| 1837 | |
| 1838 | fwrite_weight_data(op->gamma_data, bp); |
| 1839 | fwrite_weight_data(op->beta_data, bp); |
| 1840 | } |
| 1841 | else if (layer->type == "GRU") |
| 1842 | { |
| 1843 | ncnn::GRU* op = (ncnn::GRU*)layer; |
| 1844 | ncnn::GRU* op_default = (ncnn::GRU*)layer_default; |
| 1845 | |
| 1846 | fprintf_param_value(" 0=%d", num_output) |
| 1847 | fprintf_param_value(" 1=%d", weight_data_size) |
| 1848 | fprintf_param_value(" 2=%d", direction) |
| 1849 | fprintf_param_value(" 8=%d", int8_scale_term) |
| 1850 | |
| 1851 | fwrite_weight_tag_data(op->weight_xc_data, bp); |
| 1852 | fwrite_weight_tag_data(op->bias_c_data, bp); |
| 1853 | fwrite_weight_tag_data(op->weight_hc_data, bp); |
| 1854 | |
| 1855 | #if NCNN_INT8 |
| 1856 | // write int8_scale data |
| 1857 | if (op->int8_scale_term) |
| 1858 | { |
| 1859 | fwrite_weight_data(op->weight_xc_data_int8_scales, bp, 90, 100); |
| 1860 | fwrite_weight_data(op->weight_hc_data_int8_scales, bp, 90, 100); |
| 1861 | } |
| 1862 | #endif // NCNN_INT8 |
| 1863 | } |
| 1864 | else if (layer->type == "HardSigmoid") |
| 1865 | { |
| 1866 | ncnn::HardSigmoid* op = (ncnn::HardSigmoid*)layer; |
| 1867 | ncnn::HardSigmoid* op_default = (ncnn::HardSigmoid*)layer_default; |
| 1868 | |
| 1869 | fprintf_param_value(" 0=%e", alpha) |
| 1870 | fprintf_param_value(" 1=%e", beta) |
| 1871 | } |
| 1872 | else if (layer->type == "HardSwish") |
| 1873 | { |
| 1874 | ncnn::HardSwish* op = (ncnn::HardSwish*)layer; |
| 1875 | ncnn::HardSwish* op_default = (ncnn::HardSwish*)layer_default; |
| 1876 | |
| 1877 | fprintf_param_value(" 0=%e", alpha) |
| 1878 | fprintf_param_value(" 1=%e", beta) |
| 1879 | } |
| 1880 | else if (layer->type == "InnerProduct") |
| 1881 | { |
| 1882 | ncnn::InnerProduct* op = (ncnn::InnerProduct*)layer; |
| 1883 | ncnn::InnerProduct* op_default = (ncnn::InnerProduct*)layer_default; |
| 1884 | |
| 1885 | fprintf_param_value(" 0=%d", num_output) |
| 1886 | fprintf_param_value(" 1=%d", bias_term) |
| 1887 | fprintf_param_value(" 2=%d", weight_data_size) |
| 1888 | fprintf_param_value(" 8=%d", int8_scale_term) |
| 1889 | fprintf_param_value(" 9=%d", activation_type) |
| 1890 | { |
| 1891 | if (!op->activation_params.empty()) fprintf_param_float_array(10, op->activation_params, pp); |
| 1892 | } |
| 1893 | |
| 1894 | fwrite_weight_tag_data(op->weight_data, bp); |
| 1895 | fwrite_weight_data(op->bias_data, bp); |
| 1896 | |
| 1897 | #if NCNN_INT8 |
| 1898 | // write int8_scale data |
| 1899 | if (op->int8_scale_term) |
| 1900 | { |
| 1901 | fwrite_weight_data(op->weight_data_int8_scales, bp, 90, 100); |
| 1902 | fwrite_weight_data(op->bottom_blob_int8_scales, bp, 0.001, 1); |
| 1903 | } |
| 1904 | #endif // NCNN_INT8 |
| 1905 | |
| 1906 | if (shape_ready) |
| 1907 | { |
| 1908 | int inw = blobs[layer->bottoms[0]].shape.w; |
| 1909 | int inh = blobs[layer->bottoms[0]].shape.h; |
| 1910 | int inc = blobs[layer->bottoms[0]].shape.c; |
| 1911 | int outw = blobs[layer->tops[0]].shape.w; |
| 1912 | |
| 1913 | mac += (uint64_t)inw * inh * inc * outw; |
| 1914 | } |
| 1915 | } |
| 1916 | else if (layer->type == "Input") |
| 1917 | { |
| 1918 | ncnn::Input* op = (ncnn::Input*)layer; |
| 1919 | ncnn::Input* op_default = (ncnn::Input*)layer_default; |
| 1920 | |
| 1921 | fprintf_param_value(" 0=%d", w) |
| 1922 | fprintf_param_value(" 1=%d", h) |
| 1923 | fprintf_param_value(" 2=%d", c) |
| 1924 | } |
| 1925 | else if (layer->type == "InstanceNorm") |
| 1926 | { |
| 1927 | ncnn::InstanceNorm* op = (ncnn::InstanceNorm*)layer; |
| 1928 | ncnn::InstanceNorm* op_default = (ncnn::InstanceNorm*)layer_default; |
| 1929 | |
| 1930 | fprintf_param_value(" 0=%d", channels) |
| 1931 | fprintf_param_value(" 1=%e", eps) |
| 1932 | fprintf_param_value(" 2=%d", affine) |
| 1933 | |
| 1934 | fwrite_weight_data(op->gamma_data, bp); |
| 1935 | fwrite_weight_data(op->beta_data, bp); |
| 1936 | } |
| 1937 | else if (layer->type == "Interp") |
| 1938 | { |
| 1939 | ncnn::Interp* op = (ncnn::Interp*)layer; |
| 1940 | ncnn::Interp* op_default = (ncnn::Interp*)layer_default; |
| 1941 | |
| 1942 | fprintf_param_value(" 0=%d", resize_type) |
| 1943 | fprintf_param_value(" 1=%e", height_scale) |
| 1944 | fprintf_param_value(" 2=%e", width_scale) |
| 1945 | fprintf_param_value(" 3=%d", output_height) |
| 1946 | fprintf_param_value(" 4=%d", output_width) |
| 1947 | fprintf_param_value(" 5=%d", dynamic_target_size) |
| 1948 | fprintf_param_value(" 6=%d", align_corner) |
| 1949 | } |
| 1950 | else if (layer->type == "LayerNorm") |
| 1951 | { |
| 1952 | ncnn::LayerNorm* op = (ncnn::LayerNorm*)layer; |
| 1953 | ncnn::LayerNorm* op_default = (ncnn::LayerNorm*)layer_default; |
| 1954 | |
| 1955 | fprintf_param_value(" 0=%d", affine_size) |
| 1956 | fprintf_param_value(" 1=%e", eps) |
| 1957 | fprintf_param_value(" 2=%d", affine) |
| 1958 | |
| 1959 | fwrite_weight_data(op->gamma_data, bp); |
| 1960 | fwrite_weight_data(op->beta_data, bp); |
| 1961 | } |
| 1962 | else if (layer->type == "Log") |
| 1963 | { |
| 1964 | ncnn::Log* op = (ncnn::Log*)layer; |
| 1965 | ncnn::Log* op_default = (ncnn::Log*)layer_default; |
| 1966 | |
| 1967 | fprintf_param_value(" 0=%e", base) |
| 1968 | fprintf_param_value(" 1=%e", scale) |
| 1969 | fprintf_param_value(" 2=%e", shift) |
| 1970 | } |
| 1971 | else if (layer->type == "LRN") |
| 1972 | { |
| 1973 | ncnn::LRN* op = (ncnn::LRN*)layer; |
| 1974 | ncnn::LRN* op_default = (ncnn::LRN*)layer_default; |
| 1975 | |
| 1976 | fprintf_param_value(" 0=%d", region_type) |
| 1977 | fprintf_param_value(" 1=%d", local_size) |
| 1978 | fprintf_param_value(" 2=%e", alpha) |
| 1979 | fprintf_param_value(" 3=%e", beta) |
| 1980 | fprintf_param_value(" 4=%e", bias) |
| 1981 | } |
| 1982 | else if (layer->type == "LSTM") |
| 1983 | { |
| 1984 | ncnn::LSTM* op = (ncnn::LSTM*)layer; |
| 1985 | ncnn::LSTM* op_default = (ncnn::LSTM*)layer_default; |
| 1986 | |
| 1987 | fprintf_param_value(" 0=%d", num_output) |
| 1988 | fprintf_param_value(" 1=%d", weight_data_size) |
| 1989 | fprintf_param_value(" 2=%d", direction) |
| 1990 | fprintf_param_value(" 3=%d", hidden_size) |
| 1991 | fprintf_param_value(" 8=%d", int8_scale_term) |
| 1992 | |
| 1993 | fwrite_weight_tag_data(op->weight_xc_data, bp); |
| 1994 | fwrite_weight_tag_data(op->bias_c_data, bp); |
| 1995 | fwrite_weight_tag_data(op->weight_hc_data, bp); |
| 1996 | |
| 1997 | if (op->num_output != op->hidden_size) |
| 1998 | { |
| 1999 | fwrite_weight_tag_data(op->weight_hr_data, bp); |
| 2000 | } |
| 2001 | |
| 2002 | #if NCNN_INT8 |
| 2003 | // write int8_scale data |
| 2004 | if (op->int8_scale_term) |
| 2005 | { |
| 2006 | fwrite_weight_data(op->weight_xc_data_int8_scales, bp, 90, 100); |
| 2007 | fwrite_weight_data(op->weight_hc_data_int8_scales, bp, 90, 100); |
| 2008 | } |
| 2009 | #endif // NCNN_INT8 |
| 2010 | } |
| 2011 | else if (layer->type == "MatMul") |
| 2012 | { |
| 2013 | ncnn::MatMul* op = (ncnn::MatMul*)layer; |
| 2014 | ncnn::MatMul* op_default = (ncnn::MatMul*)layer_default; |
| 2015 | |
| 2016 | fprintf_param_value(" 0=%d", transB) |
| 2017 | } |
| 2018 | else if (layer->type == "MemoryData") |
| 2019 | { |
| 2020 | ncnn::MemoryData* op = (ncnn::MemoryData*)layer; |
| 2021 | ncnn::MemoryData* op_default = (ncnn::MemoryData*)layer_default; |
| 2022 | |
| 2023 | fprintf_param_value(" 0=%d", w) |
| 2024 | fprintf_param_value(" 1=%d", h) |
| 2025 | fprintf_param_value(" 2=%d", c) |
| 2026 | fprintf_param_value(" 11=%d", d) |
| 2027 | fwrite_weight_data(op->data, bp); |
| 2028 | } |
| 2029 | else if (layer->type == "MultiHeadAttention") |
| 2030 | { |
| 2031 | ncnn::MultiHeadAttention* op = (ncnn::MultiHeadAttention*)layer; |
| 2032 | ncnn::MultiHeadAttention* op_default = (ncnn::MultiHeadAttention*)layer_default; |
| 2033 | |
| 2034 | fprintf_param_value(" 0=%d", embed_dim) |
| 2035 | fprintf_param_value(" 1=%d", num_heads) |
| 2036 | fprintf_param_value(" 2=%d", weight_data_size) |
| 2037 | fprintf_param_value(" 3=%d", kdim) |
| 2038 | fprintf_param_value(" 4=%d", vdim) |
| 2039 | fprintf_param_value(" 5=%d", attn_mask) |
| 2040 | fprintf_param_value(" 6=%e", scale) |
| 2041 | |
| 2042 | fwrite_weight_tag_data(op->q_weight_data, bp); |
| 2043 | fwrite_weight_data(op->q_bias_data, bp); |
| 2044 | fwrite_weight_tag_data(op->k_weight_data, bp); |
| 2045 | fwrite_weight_data(op->k_bias_data, bp); |
| 2046 | fwrite_weight_tag_data(op->v_weight_data, bp); |
| 2047 | fwrite_weight_data(op->v_bias_data, bp); |
| 2048 | fwrite_weight_tag_data(op->out_weight_data, bp); |
| 2049 | fwrite_weight_data(op->out_bias_data, bp); |
| 2050 | } |
| 2051 | else if (layer->type == "MVN") |
| 2052 | { |
| 2053 | ncnn::MVN* op = (ncnn::MVN*)layer; |
| 2054 | ncnn::MVN* op_default = (ncnn::MVN*)layer_default; |
| 2055 | |
| 2056 | fprintf_param_value(" 0=%d", normalize_variance) |
| 2057 | fprintf_param_value(" 1=%d", across_channels) |
| 2058 | fprintf_param_value(" 2=%e", eps) |
| 2059 | } |
| 2060 | else if (layer->type == "Normalize") |
| 2061 | { |
| 2062 | ncnn::Normalize* op = (ncnn::Normalize*)layer; |
| 2063 | ncnn::Normalize* op_default = (ncnn::Normalize*)layer_default; |
| 2064 | |
| 2065 | fprintf_param_value(" 0=%d", across_spatial) |
| 2066 | fprintf_param_value(" 1=%d", channel_shared) |
| 2067 | fprintf_param_value(" 2=%e", eps) |
| 2068 | fprintf_param_value(" 3=%d", scale_data_size) |
| 2069 | fprintf_param_value(" 4=%d", across_channel) |
| 2070 | fprintf_param_value(" 9=%d", eps_mode) |
| 2071 | |
| 2072 | fwrite_weight_data(op->scale_data, bp); |
| 2073 | } |
| 2074 | else if (layer->type == "Padding") |
| 2075 | { |
| 2076 | ncnn::Padding* op = (ncnn::Padding*)layer; |
| 2077 | ncnn::Padding* op_default = (ncnn::Padding*)layer_default; |
| 2078 | |
| 2079 | fprintf_param_value(" 0=%d", top) |
| 2080 | fprintf_param_value(" 1=%d", bottom) |
| 2081 | fprintf_param_value(" 2=%d", left) |
| 2082 | fprintf_param_value(" 3=%d", right) |
| 2083 | fprintf_param_value(" 4=%d", type) |
| 2084 | fprintf_param_value(" 5=%e", value) |
| 2085 | fprintf_param_value(" 6=%d", per_channel_pad_data_size) |
| 2086 | fprintf_param_value(" 7=%d", front) |
| 2087 | fprintf_param_value(" 8=%d", behind) |
| 2088 | |
| 2089 | fwrite_weight_data(op->per_channel_pad_data, bp); |
| 2090 | } |
| 2091 | else if (layer->type == "Permute") |
| 2092 | { |
| 2093 | ncnn::Permute* op = (ncnn::Permute*)layer; |
| 2094 | ncnn::Permute* op_default = (ncnn::Permute*)layer_default; |
| 2095 | |
| 2096 | fprintf_param_value(" 0=%d", order_type) |
| 2097 | } |
| 2098 | else if (layer->type == "PixelShuffle") |
| 2099 | { |
| 2100 | ncnn::PixelShuffle* op = (ncnn::PixelShuffle*)layer; |
| 2101 | ncnn::PixelShuffle* op_default = (ncnn::PixelShuffle*)layer_default; |
| 2102 | |
| 2103 | fprintf_param_value(" 0=%d", upscale_factor) |
| 2104 | fprintf_param_value(" 1=%d", mode) |
| 2105 | } |
| 2106 | else if (layer->type == "Pooling") |
| 2107 | { |
| 2108 | ncnn::Pooling* op = (ncnn::Pooling*)layer; |
| 2109 | ncnn::Pooling* op_default = (ncnn::Pooling*)layer_default; |
| 2110 | |
| 2111 | fprintf_param_value(" 0=%d", pooling_type) |
| 2112 | fprintf_param_value(" 1=%d", kernel_w) |
| 2113 | { |
| 2114 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 2115 | } |
| 2116 | fprintf_param_value(" 2=%d", stride_w) |
| 2117 | { |
| 2118 | if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h); |
| 2119 | } |
| 2120 | fprintf_param_value(" 3=%d", pad_left) |
| 2121 | { |
| 2122 | if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top); |
| 2123 | } |
| 2124 | { |
| 2125 | if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right); |
| 2126 | } |
| 2127 | { |
| 2128 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom); |
| 2129 | } |
| 2130 | fprintf_param_value(" 4=%d", global_pooling) |
| 2131 | fprintf_param_value(" 5=%d", pad_mode) |
| 2132 | fprintf_param_value(" 6=%d", avgpool_count_include_pad) |
| 2133 | fprintf_param_value(" 7=%d", adaptive_pooling) |
| 2134 | fprintf_param_value(" 8=%d", out_w) |
| 2135 | { |
| 2136 | if (op->out_h != op->out_w) fprintf(pp, " 18=%d", op->out_h); |
| 2137 | } |
| 2138 | } |
| 2139 | else if (layer->type == "Pooling1D") |
| 2140 | { |
| 2141 | ncnn::Pooling1D* op = (ncnn::Pooling1D*)layer; |
| 2142 | ncnn::Pooling1D* op_default = (ncnn::Pooling1D*)layer_default; |
| 2143 | |
| 2144 | fprintf_param_value(" 0=%d", pooling_type) |
| 2145 | fprintf_param_value(" 1=%d", kernel_w) |
| 2146 | fprintf_param_value(" 2=%d", stride_w) |
| 2147 | fprintf_param_value(" 3=%d", pad_left) |
| 2148 | { |
| 2149 | if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right); |
| 2150 | } |
| 2151 | fprintf_param_value(" 4=%d", global_pooling) |
| 2152 | fprintf_param_value(" 5=%d", pad_mode) |
| 2153 | fprintf_param_value(" 6=%d", avgpool_count_include_pad) |
| 2154 | fprintf_param_value(" 7=%d", adaptive_pooling) |
| 2155 | fprintf_param_value(" 8=%d", out_w) |
| 2156 | } |
| 2157 | else if (layer->type == "Pooling3D") |
| 2158 | { |
| 2159 | ncnn::Pooling3D* op = (ncnn::Pooling3D*)layer; |
| 2160 | ncnn::Pooling3D* op_default = (ncnn::Pooling3D*)layer_default; |
| 2161 | |
| 2162 | fprintf_param_value(" 0=%d", pooling_type) |
| 2163 | fprintf_param_value(" 1=%d", kernel_w) |
| 2164 | { |
| 2165 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 2166 | if (op->kernel_d != op->kernel_w) fprintf(pp, " 21=%d", op->kernel_d); |
| 2167 | } |
| 2168 | fprintf_param_value(" 2=%d", stride_w) |
| 2169 | { |
| 2170 | if (op->stride_h != op->stride_w) fprintf(pp, " 12=%d", op->stride_h); |
| 2171 | if (op->stride_d != op->stride_w) fprintf(pp, " 22=%d", op->stride_d); |
| 2172 | } |
| 2173 | fprintf_param_value(" 3=%d", pad_left) |
| 2174 | { |
| 2175 | if (op->pad_top != op->pad_left) fprintf(pp, " 13=%d", op->pad_top); |
| 2176 | if (op->pad_front != op->pad_left) fprintf(pp, " 23=%d", op->pad_front); |
| 2177 | } |
| 2178 | { |
| 2179 | if (op->pad_right != op->pad_left) fprintf(pp, " 14=%d", op->pad_right); |
| 2180 | } |
| 2181 | { |
| 2182 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 15=%d", op->pad_bottom); |
| 2183 | } |
| 2184 | { |
| 2185 | if (op->pad_behind != op->pad_front) fprintf(pp, " 16=%d", op->pad_behind); |
| 2186 | } |
| 2187 | fprintf_param_value(" 4=%d", global_pooling) |
| 2188 | fprintf_param_value(" 5=%d", pad_mode) |
| 2189 | fprintf_param_value(" 6=%d", avgpool_count_include_pad) |
| 2190 | fprintf_param_value(" 7=%d", adaptive_pooling) |
| 2191 | fprintf_param_value(" 8=%d", out_w) |
| 2192 | { |
| 2193 | if (op->out_h != op->out_w) fprintf(pp, " 18=%d", op->out_h); |
| 2194 | if (op->out_d != op->out_w) fprintf(pp, " 28=%d", op->out_d); |
| 2195 | } |
| 2196 | } |
| 2197 | else if (layer->type == "Power") |
| 2198 | { |
| 2199 | ncnn::Power* op = (ncnn::Power*)layer; |
| 2200 | ncnn::Power* op_default = (ncnn::Power*)layer_default; |
| 2201 | |
| 2202 | fprintf_param_value(" 0=%e", power) |
| 2203 | fprintf_param_value(" 1=%e", scale) |
| 2204 | fprintf_param_value(" 2=%e", shift) |
| 2205 | } |
| 2206 | else if (layer->type == "PReLU") |
| 2207 | { |
| 2208 | ncnn::PReLU* op = (ncnn::PReLU*)layer; |
| 2209 | ncnn::PReLU* op_default = (ncnn::PReLU*)layer_default; |
| 2210 | |
| 2211 | fprintf_param_value(" 0=%d", num_slope) |
| 2212 | |
| 2213 | fwrite_weight_data(op->slope_data, bp); |
| 2214 | } |
| 2215 | else if (layer->type == "PriorBox") |
| 2216 | { |
| 2217 | ncnn::PriorBox* op = (ncnn::PriorBox*)layer; |
| 2218 | ncnn::PriorBox* op_default = (ncnn::PriorBox*)layer_default; |
| 2219 | |
| 2220 | { |
| 2221 | if (!op->min_sizes.empty()) fprintf_param_float_array(0, op->min_sizes, pp); |
| 2222 | } |
| 2223 | { |
| 2224 | if (!op->max_sizes.empty()) fprintf_param_float_array(1, op->max_sizes, pp); |
| 2225 | } |
| 2226 | { |
| 2227 | if (!op->aspect_ratios.empty()) fprintf_param_float_array(2, op->aspect_ratios, pp); |
| 2228 | } |
| 2229 | fprintf_param_value(" 3=%e", variances[0]) |
| 2230 | fprintf_param_value(" 4=%e", variances[1]) |
| 2231 | fprintf_param_value(" 5=%e", variances[2]) |
| 2232 | fprintf_param_value(" 6=%e", variances[3]) |
| 2233 | fprintf_param_value(" 7=%d", flip) |
| 2234 | fprintf_param_value(" 8=%d", clip) |
| 2235 | fprintf_param_value(" 9=%d", image_width) |
| 2236 | fprintf_param_value(" 10=%d", image_height) |
| 2237 | fprintf_param_value(" 11=%e", step_width) |
| 2238 | fprintf_param_value(" 12=%e", step_height) |
| 2239 | fprintf_param_value(" 13=%e", offset) |
| 2240 | } |
| 2241 | else if (layer->type == "Proposal") |
| 2242 | { |
| 2243 | ncnn::Proposal* op = (ncnn::Proposal*)layer; |
| 2244 | ncnn::Proposal* op_default = (ncnn::Proposal*)layer_default; |
| 2245 | |
| 2246 | fprintf_param_value(" 0=%d", feat_stride) |
| 2247 | fprintf_param_value(" 1=%d", base_size) |
| 2248 | fprintf_param_value(" 2=%d", pre_nms_topN) |
| 2249 | fprintf_param_value(" 3=%d", after_nms_topN) |
| 2250 | fprintf_param_value(" 4=%e", nms_thresh) |
| 2251 | fprintf_param_value(" 5=%d", min_size) |
| 2252 | } |
| 2253 | else if (layer->type == "PSROIPooling") |
| 2254 | { |
| 2255 | ncnn::PSROIPooling* op = (ncnn::PSROIPooling*)layer; |
| 2256 | ncnn::PSROIPooling* op_default = (ncnn::PSROIPooling*)layer_default; |
| 2257 | |
| 2258 | fprintf_param_value(" 0=%d", pooled_width) |
| 2259 | fprintf_param_value(" 1=%d", pooled_height) |
| 2260 | fprintf_param_value(" 2=%e", spatial_scale) |
| 2261 | fprintf_param_value(" 3=%d", output_dim) |
| 2262 | } |
| 2263 | else if (layer->type == "Quantize") |
| 2264 | { |
| 2265 | ncnn::Quantize* op = (ncnn::Quantize*)layer; |
| 2266 | ncnn::Quantize* op_default = (ncnn::Quantize*)layer_default; |
| 2267 | |
| 2268 | fprintf_param_value(" 0=%d", scale_data_size) |
| 2269 | |
| 2270 | fwrite_weight_data(op->scale_data, bp); |
| 2271 | } |
| 2272 | else if (layer->type == "Reduction") |
| 2273 | { |
| 2274 | ncnn::Reduction* op = (ncnn::Reduction*)layer; |
| 2275 | ncnn::Reduction* op_default = (ncnn::Reduction*)layer_default; |
| 2276 | |
| 2277 | fprintf_param_value(" 0=%d", operation) |
| 2278 | fprintf_param_value(" 1=%d", reduce_all) |
| 2279 | fprintf_param_value(" 2=%e", coeff) |
| 2280 | { |
| 2281 | if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp); |
| 2282 | } |
| 2283 | fprintf_param_value(" 4=%d", keepdims) |
| 2284 | |
| 2285 | // HACK |
| 2286 | if (!op->axes.empty()) |
| 2287 | { |
| 2288 | int fixbug0 = 1; |
| 2289 | fprintf(pp, " 5=%d", fixbug0); |
| 2290 | } |
| 2291 | } |
| 2292 | else if (layer->type == "ReLU") |
| 2293 | { |
| 2294 | ncnn::ReLU* op = (ncnn::ReLU*)layer; |
| 2295 | ncnn::ReLU* op_default = (ncnn::ReLU*)layer_default; |
| 2296 | |
| 2297 | fprintf_param_value(" 0=%e", slope) |
| 2298 | } |
| 2299 | else if (layer->type == "Reorg") |
| 2300 | { |
| 2301 | ncnn::Reorg* op = (ncnn::Reorg*)layer; |
| 2302 | ncnn::Reorg* op_default = (ncnn::Reorg*)layer_default; |
| 2303 | |
| 2304 | fprintf_param_value(" 0=%d", stride) |
| 2305 | fprintf_param_value(" 1=%d", mode) |
| 2306 | } |
| 2307 | else if (layer->type == "Requantize") |
| 2308 | { |
| 2309 | ncnn::Requantize* op = (ncnn::Requantize*)layer; |
| 2310 | ncnn::Requantize* op_default = (ncnn::Requantize*)layer_default; |
| 2311 | |
| 2312 | fprintf_param_value(" 0=%d", scale_in_data_size) |
| 2313 | fprintf_param_value(" 1=%d", scale_out_data_size) |
| 2314 | fprintf_param_value(" 2=%d", bias_data_size) |
| 2315 | fprintf_param_value(" 3=%d", activation_type) |
| 2316 | { |
| 2317 | if (!op->activation_params.empty()) fprintf_param_float_array(4, op->activation_params, pp); |
| 2318 | } |
| 2319 | |
| 2320 | fwrite_weight_data(op->scale_in_data, bp); |
| 2321 | fwrite_weight_data(op->scale_out_data, bp); |
| 2322 | fwrite_weight_data(op->bias_data, bp); |
| 2323 | } |
| 2324 | else if (layer->type == "Reshape") |
| 2325 | { |
| 2326 | ncnn::Reshape* op = (ncnn::Reshape*)layer; |
| 2327 | ncnn::Reshape* op_default = (ncnn::Reshape*)layer_default; |
| 2328 | |
| 2329 | fprintf_param_value(" 0=%d", w) |
| 2330 | fprintf_param_value(" 1=%d", h) |
| 2331 | fprintf_param_value(" 11=%d", d) |
| 2332 | fprintf_param_value(" 2=%d", c) |
| 2333 | fprintf_param_value(" 3=%d", permute) |
| 2334 | } |
| 2335 | else if (layer->type == "RMSNorm") |
| 2336 | { |
| 2337 | ncnn::RMSNorm* op = (ncnn::RMSNorm*)layer; |
| 2338 | ncnn::RMSNorm* op_default = (ncnn::RMSNorm*)layer_default; |
| 2339 | |
| 2340 | fprintf_param_value(" 0=%d", affine_size) |
| 2341 | fprintf_param_value(" 1=%e", eps) |
| 2342 | fprintf_param_value(" 2=%d", affine) |
| 2343 | |
| 2344 | fwrite_weight_data(op->gamma_data, bp); |
| 2345 | } |
| 2346 | else if (layer->type == "RNN") |
| 2347 | { |
| 2348 | ncnn::RNN* op = (ncnn::RNN*)layer; |
| 2349 | ncnn::RNN* op_default = (ncnn::RNN*)layer_default; |
| 2350 | |
| 2351 | fprintf_param_value(" 0=%d", num_output) |
| 2352 | fprintf_param_value(" 1=%d", weight_data_size) |
| 2353 | fprintf_param_value(" 2=%d", direction) |
| 2354 | fprintf_param_value(" 8=%d", int8_scale_term) |
| 2355 | |
| 2356 | fwrite_weight_tag_data(op->weight_xc_data, bp); |
| 2357 | fwrite_weight_tag_data(op->bias_c_data, bp); |
| 2358 | fwrite_weight_tag_data(op->weight_hc_data, bp); |
| 2359 | |
| 2360 | #if NCNN_INT8 |
| 2361 | // write int8_scale data |
| 2362 | if (op->int8_scale_term) |
| 2363 | { |
| 2364 | fwrite_weight_data(op->weight_xc_data_int8_scales, bp, 90, 100); |
| 2365 | fwrite_weight_data(op->weight_hc_data_int8_scales, bp, 90, 100); |
| 2366 | } |
| 2367 | #endif // NCNN_INT8 |
| 2368 | } |
| 2369 | else if (layer->type == "ROIAlign") |
| 2370 | { |
| 2371 | ncnn::ROIAlign* op = (ncnn::ROIAlign*)layer; |
| 2372 | ncnn::ROIAlign* op_default = (ncnn::ROIAlign*)layer_default; |
| 2373 | |
| 2374 | fprintf_param_value(" 0=%d", pooled_width) |
| 2375 | fprintf_param_value(" 1=%d", pooled_height) |
| 2376 | fprintf_param_value(" 2=%e", spatial_scale) |
| 2377 | fprintf_param_value(" 3=%d", sampling_ratio) |
| 2378 | fprintf_param_value(" 4=%d", aligned) |
| 2379 | fprintf_param_value(" 5=%d", version) |
| 2380 | } |
| 2381 | else if (layer->type == "ROIPooling") |
| 2382 | { |
| 2383 | ncnn::ROIPooling* op = (ncnn::ROIPooling*)layer; |
| 2384 | ncnn::ROIPooling* op_default = (ncnn::ROIPooling*)layer_default; |
| 2385 | |
| 2386 | fprintf_param_value(" 0=%d", pooled_width) |
| 2387 | fprintf_param_value(" 1=%d", pooled_height) |
| 2388 | fprintf_param_value(" 2=%e", spatial_scale) |
| 2389 | } |
| 2390 | else if (layer->type == "Scale") |
| 2391 | { |
| 2392 | ncnn::Scale* op = (ncnn::Scale*)layer; |
| 2393 | ncnn::Scale* op_default = (ncnn::Scale*)layer_default; |
| 2394 | |
| 2395 | fprintf_param_value(" 0=%d", scale_data_size) |
| 2396 | fprintf_param_value(" 1=%d", bias_term) |
| 2397 | |
| 2398 | fwrite_weight_data(op->scale_data, bp); |
| 2399 | fwrite_weight_data(op->bias_data, bp); |
| 2400 | } |
| 2401 | else if (layer->type == "ShuffleChannel") |
| 2402 | { |
| 2403 | ncnn::ShuffleChannel* op = (ncnn::ShuffleChannel*)layer; |
| 2404 | ncnn::ShuffleChannel* op_default = (ncnn::ShuffleChannel*)layer_default; |
| 2405 | |
| 2406 | fprintf_param_value(" 0=%d", group) |
| 2407 | fprintf_param_value(" 1=%d", reverse) |
| 2408 | } |
| 2409 | else if (layer->type == "Slice") |
| 2410 | { |
| 2411 | ncnn::Slice* op = (ncnn::Slice*)layer; |
| 2412 | ncnn::Slice* op_default = (ncnn::Slice*)layer_default; |
| 2413 | |
| 2414 | { |
| 2415 | if (!op->slices.empty()) fprintf_param_int_array(0, op->slices, pp); |
| 2416 | } |
| 2417 | fprintf_param_value(" 1=%d", axis) |
| 2418 | } |
| 2419 | else if (layer->type == "Softmax") |
| 2420 | { |
| 2421 | ncnn::Softmax* op = (ncnn::Softmax*)layer; |
| 2422 | ncnn::Softmax* op_default = (ncnn::Softmax*)layer_default; |
| 2423 | |
| 2424 | fprintf_param_value(" 0=%d", axis) |
| 2425 | |
| 2426 | // HACK |
| 2427 | if (op->axis != 0) |
| 2428 | { |
| 2429 | int fixbug0 = 1; |
| 2430 | fprintf(pp, " 1=%d", fixbug0); |
| 2431 | } |
| 2432 | } |
| 2433 | else if (layer->type == "Squeeze") |
| 2434 | { |
| 2435 | ncnn::Squeeze* op = (ncnn::Squeeze*)layer; |
| 2436 | ncnn::Squeeze* op_default = (ncnn::Squeeze*)layer_default; |
| 2437 | |
| 2438 | fprintf_param_value(" 0=%d", squeeze_w) |
| 2439 | fprintf_param_value(" 1=%d", squeeze_h) |
| 2440 | fprintf_param_value(" 11=%d", squeeze_d) |
| 2441 | fprintf_param_value(" 2=%d", squeeze_c) |
| 2442 | { |
| 2443 | if (!op->axes.empty()) fprintf_param_int_array(3, op->axes, pp); |
| 2444 | } |
| 2445 | } |
| 2446 | else if (layer->type == "Threshold") |
| 2447 | { |
| 2448 | ncnn::Threshold* op = (ncnn::Threshold*)layer; |
| 2449 | ncnn::Threshold* op_default = (ncnn::Threshold*)layer_default; |
| 2450 | |
| 2451 | fprintf_param_value(" 0=%e", threshold) |
| 2452 | } |
| 2453 | else if (layer->type == "UnaryOp") |
| 2454 | { |
| 2455 | ncnn::UnaryOp* op = (ncnn::UnaryOp*)layer; |
| 2456 | ncnn::UnaryOp* op_default = (ncnn::UnaryOp*)layer_default; |
| 2457 | |
| 2458 | fprintf_param_value(" 0=%d", op_type) |
| 2459 | } |
| 2460 | else if (layer->type == "Unfold") |
| 2461 | { |
| 2462 | ncnn::Unfold* op = (ncnn::Unfold*)layer; |
| 2463 | ncnn::Unfold* op_default = (ncnn::Unfold*)layer_default; |
| 2464 | |
| 2465 | fprintf_param_value(" 1=%d", kernel_w) |
| 2466 | { |
| 2467 | if (op->kernel_h != op->kernel_w) fprintf(pp, " 11=%d", op->kernel_h); |
| 2468 | } |
| 2469 | fprintf_param_value(" 2=%d", dilation_w) |
| 2470 | { |
| 2471 | if (op->dilation_h != op->dilation_w) fprintf(pp, " 12=%d", op->dilation_h); |
| 2472 | } |
| 2473 | fprintf_param_value(" 3=%d", stride_w) |
| 2474 | { |
| 2475 | if (op->stride_h != op->stride_w) fprintf(pp, " 13=%d", op->stride_h); |
| 2476 | } |
| 2477 | fprintf_param_value(" 4=%d", pad_left) |
| 2478 | { |
| 2479 | if (op->pad_top != op->pad_left) fprintf(pp, " 14=%d", op->pad_top); |
| 2480 | } |
| 2481 | { |
| 2482 | if (op->pad_right != op->pad_left) fprintf(pp, " 15=%d", op->pad_right); |
| 2483 | } |
| 2484 | { |
| 2485 | if (op->pad_bottom != op->pad_top) fprintf(pp, " 16=%d", op->pad_bottom); |
| 2486 | } |
| 2487 | fprintf_param_value(" 18=%e", pad_value) |
| 2488 | } |
| 2489 | else if (layer->type == "YoloDetectionOutput") |
| 2490 | { |
| 2491 | ncnn::YoloDetectionOutput* op = (ncnn::YoloDetectionOutput*)layer; |
| 2492 | ncnn::YoloDetectionOutput* op_default = (ncnn::YoloDetectionOutput*)layer_default; |
| 2493 | |
| 2494 | fprintf_param_value(" 0=%d", num_class) |
| 2495 | fprintf_param_value(" 1=%d", num_box) |
| 2496 | fprintf_param_value(" 2=%e", confidence_threshold) |
| 2497 | fprintf_param_value(" 3=%e", nms_threshold) |
| 2498 | { |
| 2499 | if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); |
| 2500 | } |
| 2501 | } |
| 2502 | else if (layer->type == "Yolov3DetectionOutput") |
| 2503 | { |
| 2504 | ncnn::Yolov3DetectionOutput* op = (ncnn::Yolov3DetectionOutput*)layer; |
| 2505 | ncnn::Yolov3DetectionOutput* op_default = (ncnn::Yolov3DetectionOutput*)layer_default; |
| 2506 | |
| 2507 | fprintf_param_value(" 0=%d", num_class) |
| 2508 | fprintf_param_value(" 1=%d", num_box) |
| 2509 | fprintf_param_value(" 2=%e", confidence_threshold) |
| 2510 | fprintf_param_value(" 3=%e", nms_threshold) |
| 2511 | { |
| 2512 | if (!op->biases.empty()) fprintf_param_float_array(4, op->biases, pp); |
| 2513 | } |
| 2514 | { |
| 2515 | if (!op->mask.empty()) fprintf_param_int_array(5, op->mask, pp); |
| 2516 | } |
| 2517 | { |
| 2518 | if (!op->anchors_scale.empty()) fprintf_param_float_array(6, op->anchors_scale, pp); |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | #undef fprintf_param_value |
| 2523 | |
| 2524 | fprintf(pp, "\n"); |
| 2525 | |
| 2526 | delete layer_default; |
| 2527 | } |
| 2528 | |
| 2529 | fclose(pp); |
| 2530 | fclose(bp); |
| 2531 | |
| 2532 | if (mac) |
| 2533 | { |
| 2534 | fprintf(stderr, "mac = %llu = %.2f M\n", static_cast<long long unsigned>(mac), mac / 1000000.0); |
| 2535 | } |
| 2536 | |
| 2537 | return 0; |
| 2538 | } |