| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "layer.h" |
| 16 | #include "layer_type.h" |
| 17 | |
| 18 | #include <cstddef> |
| 19 | #include <ctype.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | static std::vector<std::string> layer_names; |
| 26 | static std::vector<std::string> blob_names; |
| 27 | |
| 28 | static int find_blob_index_by_name(const char* name) |
| 29 | { |
| 30 | for (std::size_t i = 0; i < blob_names.size(); i++) |
| 31 | { |
| 32 | if (blob_names[i] == name) |
| 33 | { |
| 34 | return static_cast<int>(i); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fprintf(stderr, "find_blob_index_by_name %s failed\n", name); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | static void sanitize_name(char* name) |
| 43 | { |
| 44 | for (std::size_t i = 0; i < strlen(name); i++) |
| 45 | { |
| 46 | if (!isalnum(name[i])) |
| 47 | { |
| 48 | name[i] = '_'; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static std::string path_to_varname(const char* path) |
| 54 | { |
| 55 | const char* lastslash = strrchr(path, '/'); |
| 56 | const char* name = lastslash == NULL ? path : lastslash + 1; |
| 57 | |
| 58 | std::string varname = name; |
| 59 | sanitize_name((char*)varname.c_str()); |
| 60 | |
| 61 | return varname; |
| 62 | } |
| 63 | |
| 64 | static bool vstr_is_float(const char vstr[16]) |
| 65 | { |
| 66 | // look ahead for determine isfloat |
| 67 | for (int j = 0; j < 16; j++) |
| 68 | { |
| 69 | if (vstr[j] == '\0') |
| 70 | break; |
| 71 | |
| 72 | if (vstr[j] == '.' || tolower(vstr[j]) == 'e') |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | static float vstr_to_float(const char vstr[16]) |
| 80 | { |
| 81 | double v = 0.0; |
| 82 | |
| 83 | const char* p = vstr; |
| 84 | |
| 85 | // sign |
| 86 | bool sign = *p != '-'; |
| 87 | if (*p == '+' || *p == '-') |
| 88 | { |
| 89 | p++; |
| 90 | } |
| 91 | |
| 92 | // digits before decimal point or exponent |
| 93 | unsigned int v1 = 0; |
| 94 | while (isdigit(*p)) |
| 95 | { |
| 96 | v1 = v1 * 10 + (*p - '0'); |
| 97 | p++; |
| 98 | } |
| 99 | |
| 100 | v = (double)v1; |
| 101 | |
| 102 | // digits after decimal point |
| 103 | if (*p == '.') |
| 104 | { |
| 105 | p++; |
| 106 | |
| 107 | unsigned int pow10 = 1; |
| 108 | unsigned int v2 = 0; |
| 109 | |
| 110 | while (isdigit(*p)) |
| 111 | { |
| 112 | v2 = v2 * 10 + (*p - '0'); |
| 113 | pow10 *= 10; |
| 114 | p++; |
| 115 | } |
| 116 | |
| 117 | v += v2 / (double)pow10; |
| 118 | } |
| 119 | |
| 120 | // exponent |
| 121 | if (*p == 'e' || *p == 'E') |
| 122 | { |
| 123 | p++; |
| 124 | |
| 125 | // sign of exponent |
| 126 | bool fact = *p != '-'; |
| 127 | if (*p == '+' || *p == '-') |
| 128 | { |
| 129 | p++; |
| 130 | } |
| 131 | |
| 132 | // digits of exponent |
| 133 | unsigned int expon = 0; |
| 134 | while (isdigit(*p)) |
| 135 | { |
| 136 | expon = expon * 10 + (*p - '0'); |
| 137 | p++; |
| 138 | } |
| 139 | |
| 140 | double scale = 1.0; |
| 141 | while (expon >= 8) |
| 142 | { |
| 143 | scale *= 1e8; |
| 144 | expon -= 8; |
| 145 | } |
| 146 | while (expon > 0) |
| 147 | { |
| 148 | scale *= 10.0; |
| 149 | expon -= 1; |
| 150 | } |
| 151 | |
| 152 | v = fact ? v * scale : v / scale; |
| 153 | } |
| 154 | |
| 155 | // fprintf(stderr, "v = %f\n", v); |
| 156 | return sign ? (float)v : (float)-v; |
| 157 | } |
| 158 | |
| 159 | static int dump_param(const char* parampath, const char* parambinpath, const char* idcpppath) |
| 160 | { |
| 161 | FILE* fp = fopen(parampath, "rb"); |
| 162 | |
| 163 | if (!fp) |
| 164 | { |
| 165 | fprintf(stderr, "fopen %s failed\n", parampath); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | FILE* mp = fopen(parambinpath, "wb"); |
| 170 | FILE* ip = fopen(idcpppath, "wb"); |
| 171 | |
| 172 | std::string param_var = path_to_varname(parampath); |
| 173 | |
| 174 | std::string include_guard_var = path_to_varname(idcpppath); |
| 175 | |
| 176 | fprintf(ip, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str()); |
| 177 | fprintf(ip, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str()); |
| 178 | fprintf(ip, "namespace %s_id {\n", param_var.c_str()); |
| 179 | |
| 180 | int nscan = 0; |
| 181 | int magic = 0; |
| 182 | nscan = fscanf(fp, "%d", &magic); |
| 183 | if (nscan != 1) |
| 184 | { |
| 185 | fprintf(stderr, "read magic failed %d\n", nscan); |
| 186 | return -1; |
| 187 | } |
| 188 | fwrite(&magic, sizeof(int), 1, mp); |
| 189 | |
| 190 | int layer_count = 0; |
| 191 | int blob_count = 0; |
| 192 | nscan = fscanf(fp, "%d %d", &layer_count, &blob_count); |
| 193 | if (nscan != 2) |
| 194 | { |
| 195 | fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan); |
| 196 | return -1; |
| 197 | } |
| 198 | fwrite(&layer_count, sizeof(int), 1, mp); |
| 199 | fwrite(&blob_count, sizeof(int), 1, mp); |
| 200 | |
| 201 | layer_names.resize(layer_count); |
| 202 | blob_names.resize(blob_count); |
| 203 | |
| 204 | std::vector<std::string> custom_layer_index; |
| 205 | |
| 206 | int blob_index = 0; |
| 207 | for (int i = 0; i < layer_count; i++) |
| 208 | { |
| 209 | char layer_type[33]; |
| 210 | char layer_name[257]; |
| 211 | int bottom_count = 0; |
| 212 | int top_count = 0; |
| 213 | nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count); |
| 214 | if (nscan != 4) |
| 215 | { |
| 216 | fprintf(stderr, "read layer params failed %d\n", nscan); |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | sanitize_name(layer_name); |
| 221 | |
| 222 | int typeindex = ncnn::layer_to_index(layer_type); |
| 223 | if (typeindex == -1) |
| 224 | { |
| 225 | // lookup custom_layer_index |
| 226 | for (size_t j = 0; j < custom_layer_index.size(); j++) |
| 227 | { |
| 228 | if (custom_layer_index[j] == layer_type) |
| 229 | { |
| 230 | typeindex = ncnn::LayerType::CustomBit | j; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (typeindex == -1) |
| 236 | { |
| 237 | // new custom layer type |
| 238 | size_t j = custom_layer_index.size(); |
| 239 | custom_layer_index.push_back(layer_type); |
| 240 | typeindex = ncnn::LayerType::CustomBit | j; |
| 241 | } |
| 242 | } |
| 243 | fwrite(&typeindex, sizeof(int), 1, mp); |
| 244 | |
| 245 | fwrite(&bottom_count, sizeof(int), 1, mp); |
| 246 | fwrite(&top_count, sizeof(int), 1, mp); |
| 247 | |
| 248 | fprintf(ip, "const int LAYER_%s = %d;\n", layer_name, i); |
| 249 | |
| 250 | // layer->bottoms.resize(bottom_count); |
| 251 | for (int j = 0; j < bottom_count; j++) |
| 252 | { |
| 253 | char bottom_name[257]; |
| 254 | nscan = fscanf(fp, "%256s", bottom_name); |
| 255 | if (nscan != 1) |
| 256 | { |
| 257 | fprintf(stderr, "read bottom_name failed %d\n", nscan); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | sanitize_name(bottom_name); |
| 262 | |
| 263 | int bottom_blob_index = find_blob_index_by_name(bottom_name); |
| 264 | |
| 265 | fwrite(&bottom_blob_index, sizeof(int), 1, mp); |
| 266 | } |
| 267 | |
| 268 | // layer->tops.resize(top_count); |
| 269 | for (int j = 0; j < top_count; j++) |
| 270 | { |
| 271 | char blob_name[257]; |
| 272 | nscan = fscanf(fp, "%256s", blob_name); |
| 273 | if (nscan != 1) |
| 274 | { |
| 275 | fprintf(stderr, "read blob_name failed %d\n", nscan); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | sanitize_name(blob_name); |
| 280 | |
| 281 | blob_names[blob_index] = std::string(blob_name); |
| 282 | |
| 283 | fprintf(ip, "const int BLOB_%s = %d;\n", blob_name, blob_index); |
| 284 | |
| 285 | fwrite(&blob_index, sizeof(int), 1, mp); |
| 286 | |
| 287 | blob_index++; |
| 288 | } |
| 289 | |
| 290 | // dump layer specific params |
| 291 | // parse each key=value pair |
| 292 | int id = 0; |
| 293 | while (fscanf(fp, "%d=", &id) == 1) |
| 294 | { |
| 295 | fwrite(&id, sizeof(int), 1, mp); |
| 296 | |
| 297 | bool is_array = id <= -23300; |
| 298 | |
| 299 | if (is_array) |
| 300 | { |
| 301 | int len = 0; |
| 302 | nscan = fscanf(fp, "%d", &len); |
| 303 | if (nscan != 1) |
| 304 | { |
| 305 | fprintf(stderr, "read array length failed %d\n", nscan); |
| 306 | return -1; |
| 307 | } |
| 308 | fwrite(&len, sizeof(int), 1, mp); |
| 309 | |
| 310 | for (int j = 0; j < len; j++) |
| 311 | { |
| 312 | char vstr[16]; |
| 313 | nscan = fscanf(fp, ",%15[^,\n ]", vstr); |
| 314 | if (nscan != 1) |
| 315 | { |
| 316 | fprintf(stderr, "read array element failed %d\n", nscan); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | bool is_float = vstr_is_float(vstr); |
| 321 | |
| 322 | if (is_float) |
| 323 | { |
| 324 | float vf = vstr_to_float(vstr); |
| 325 | fwrite(&vf, sizeof(float), 1, mp); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | int v; |
| 330 | sscanf(vstr, "%d", &v); |
| 331 | fwrite(&v, sizeof(int), 1, mp); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | char vstr[16]; |
| 338 | nscan = fscanf(fp, "%15s", vstr); |
| 339 | if (nscan != 1) |
| 340 | { |
| 341 | fprintf(stderr, "read value failed %d\n", nscan); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | bool is_float = vstr_is_float(vstr); |
| 346 | |
| 347 | if (is_float) |
| 348 | { |
| 349 | float vf = vstr_to_float(vstr); |
| 350 | fwrite(&vf, sizeof(float), 1, mp); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | int v; |
| 355 | sscanf(vstr, "%d", &v); |
| 356 | fwrite(&v, sizeof(int), 1, mp); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | int EOP = -233; |
| 362 | fwrite(&EOP, sizeof(int), 1, mp); |
| 363 | |
| 364 | layer_names[i] = std::string(layer_name); |
| 365 | } |
| 366 | |
| 367 | // dump custom layer index |
| 368 | for (size_t j = 0; j < custom_layer_index.size(); j++) |
| 369 | { |
| 370 | const std::string& layer_type = custom_layer_index[j]; |
| 371 | int typeindex = ncnn::LayerType::CustomBit | j; |
| 372 | |
| 373 | fprintf(ip, "const int TYPEINDEX_%s = %d;\n", layer_type.c_str(), typeindex); |
| 374 | |
| 375 | fprintf(stderr, "net.register_custom_layer(%s_id::TYPEINDEX_%s, %s_layer_creator);\n", param_var.c_str(), layer_type.c_str(), layer_type.c_str()); |
| 376 | } |
| 377 | |
| 378 | fprintf(ip, "} // namespace %s_id\n", param_var.c_str()); |
| 379 | fprintf(ip, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str()); |
| 380 | |
| 381 | fclose(fp); |
| 382 | |
| 383 | fclose(mp); |
| 384 | fclose(ip); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int write_memcpp(const char* parambinpath, const char* modelpath, const char* memcpppath) |
| 390 | { |
| 391 | FILE* cppfp = fopen(memcpppath, "wb"); |
| 392 | |
| 393 | // dump param |
| 394 | std::string param_var = path_to_varname(parambinpath); |
| 395 | |
| 396 | std::string include_guard_var = path_to_varname(memcpppath); |
| 397 | |
| 398 | FILE* mp = fopen(parambinpath, "rb"); |
| 399 | |
| 400 | if (!mp) |
| 401 | { |
| 402 | fprintf(stderr, "fopen %s failed\n", parambinpath); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | fprintf(cppfp, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str()); |
| 407 | fprintf(cppfp, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str()); |
| 408 | |
| 409 | fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n"); |
| 410 | fprintf(cppfp, "static const unsigned char %s[] = {\n", param_var.c_str()); |
| 411 | |
| 412 | int i = 0; |
| 413 | while (!feof(mp)) |
| 414 | { |
| 415 | int c = fgetc(mp); |
| 416 | if (c == EOF) |
| 417 | break; |
| 418 | fprintf(cppfp, "0x%02x,", c); |
| 419 | |
| 420 | i++; |
| 421 | if (i % 16 == 0) |
| 422 | { |
| 423 | fprintf(cppfp, "\n"); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | fprintf(cppfp, "};\n"); |
| 428 | |
| 429 | fclose(mp); |
| 430 | |
| 431 | // dump model |
| 432 | std::string model_var = path_to_varname(modelpath); |
| 433 | |
| 434 | FILE* bp = fopen(modelpath, "rb"); |
| 435 | |
| 436 | if (!bp) |
| 437 | { |
| 438 | fprintf(stderr, "fopen %s failed\n", modelpath); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n"); |
| 443 | fprintf(cppfp, "static const unsigned char %s[] = {\n", model_var.c_str()); |
| 444 | |
| 445 | i = 0; |
| 446 | while (!feof(bp)) |
| 447 | { |
| 448 | int c = fgetc(bp); |
| 449 | if (c == EOF) |
| 450 | break; |
| 451 | fprintf(cppfp, "0x%02x,", c); |
| 452 | |
| 453 | i++; |
| 454 | if (i % 16 == 0) |
| 455 | { |
| 456 | fprintf(cppfp, "\n"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | fprintf(cppfp, "};\n"); |
| 461 | |
| 462 | fprintf(cppfp, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str()); |
| 463 | |
| 464 | fclose(bp); |
| 465 | |
| 466 | fclose(cppfp); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int main(int argc, char** argv) |
| 472 | { |
| 473 | if (argc != 5) |
| 474 | { |
| 475 | fprintf(stderr, "Usage: %s [ncnnproto] [ncnnbin] [idcpppath] [memcpppath]\n", argv[0]); |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | const char* parampath = argv[1]; |
| 480 | const char* modelpath = argv[2]; |
| 481 | const char* idcpppath = argv[3]; |
| 482 | const char* memcpppath = argv[4]; |
| 483 | |
| 484 | std::string parambinpath = std::string(parampath) + ".bin"; |
| 485 | |
| 486 | dump_param(parampath, parambinpath.c_str(), idcpppath); |
| 487 | |
| 488 | write_memcpp(parambinpath.c_str(), modelpath, memcpppath); |
| 489 | |
| 490 | return 0; |
| 491 | } |