| 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 | #include "testutil.h" |
| 16 | |
| 17 | #include "cpu.h" |
| 18 | #include "layer.h" |
| 19 | #include "mat.h" |
| 20 | #include "prng.h" |
| 21 | |
| 22 | #include <limits.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #if NCNN_VULKAN |
| 27 | #include "command.h" |
| 28 | #include "gpu.h" |
| 29 | #endif // NCNN_VULKAN |
| 30 | |
| 31 | static struct prng_rand_t g_prng_rand_state; |
| 32 | |
| 33 | void SRAND(int seed) |
| 34 | { |
| 35 | prng_srand(seed, &g_prng_rand_state); |
| 36 | } |
| 37 | |
| 38 | uint64_t RAND() |
| 39 | { |
| 40 | return prng_rand(&g_prng_rand_state); |
| 41 | } |
| 42 | |
| 43 | float RandomFloat(float a, float b) |
| 44 | { |
| 45 | float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX; |
| 46 | float diff = b - a; |
| 47 | float r = random * diff; |
| 48 | float v = a + r; |
| 49 | // generate denormal as zero |
| 50 | if (v < 0.0001 && v > -0.0001) |
| 51 | v = 0.f; |
| 52 | return v; |
| 53 | } |
| 54 | |
| 55 | int RandomInt(int a, int b) |
| 56 | { |
| 57 | float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX; |
| 58 | int diff = b - a; |
| 59 | float r = random * diff; |
| 60 | return a + (int)r; |
| 61 | } |
| 62 | |
| 63 | signed char RandomS8() |
| 64 | { |
| 65 | return (signed char)RandomInt(-127, 127); |
| 66 | } |
| 67 | |
| 68 | void Randomize(ncnn::Mat& m, float a, float b) |
| 69 | { |
| 70 | for (size_t i = 0; i < m.total(); i++) |
| 71 | { |
| 72 | m[i] = RandomFloat(a, b); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void RandomizeInt(ncnn::Mat& m, int a, int b) |
| 77 | { |
| 78 | for (size_t i = 0; i < m.total(); i++) |
| 79 | { |
| 80 | ((int*)m)[i] = RandomInt(a, b); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void RandomizeS8(ncnn::Mat& m) |
| 85 | { |
| 86 | for (size_t i = 0; i < m.total(); i++) |
| 87 | { |
| 88 | ((signed char*)m)[i] = RandomS8(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | ncnn::Mat RandomMat(int w, float a, float b) |
| 93 | { |
| 94 | ncnn::Mat m(w); |
| 95 | Randomize(m, a, b); |
| 96 | return m; |
| 97 | } |
| 98 | |
| 99 | ncnn::Mat RandomMat(int w, int h, float a, float b) |
| 100 | { |
| 101 | ncnn::Mat m(w, h); |
| 102 | Randomize(m, a, b); |
| 103 | return m; |
| 104 | } |
| 105 | |
| 106 | ncnn::Mat RandomMat(int w, int h, int c, float a, float b) |
| 107 | { |
| 108 | ncnn::Mat m(w, h, c); |
| 109 | Randomize(m, a, b); |
| 110 | return m; |
| 111 | } |
| 112 | |
| 113 | ncnn::Mat RandomMat(int w, int h, int d, int c, float a, float b) |
| 114 | { |
| 115 | ncnn::Mat m(w, h, d, c); |
| 116 | Randomize(m, a, b); |
| 117 | return m; |
| 118 | } |
| 119 | |
| 120 | ncnn::Mat RandomIntMat(int w) |
| 121 | { |
| 122 | ncnn::Mat m(w); |
| 123 | RandomizeInt(m); |
| 124 | return m; |
| 125 | } |
| 126 | |
| 127 | ncnn::Mat RandomIntMat(int w, int h) |
| 128 | { |
| 129 | ncnn::Mat m(w, h); |
| 130 | RandomizeInt(m); |
| 131 | return m; |
| 132 | } |
| 133 | |
| 134 | ncnn::Mat RandomIntMat(int w, int h, int c) |
| 135 | { |
| 136 | ncnn::Mat m(w, h, c); |
| 137 | RandomizeInt(m); |
| 138 | return m; |
| 139 | } |
| 140 | |
| 141 | ncnn::Mat RandomIntMat(int w, int h, int d, int c) |
| 142 | { |
| 143 | ncnn::Mat m(w, h, d, c); |
| 144 | RandomizeInt(m); |
| 145 | return m; |
| 146 | } |
| 147 | |
| 148 | ncnn::Mat RandomS8Mat(int w) |
| 149 | { |
| 150 | ncnn::Mat m(w, (size_t)1u); |
| 151 | RandomizeS8(m); |
| 152 | return m; |
| 153 | } |
| 154 | |
| 155 | ncnn::Mat RandomS8Mat(int w, int h) |
| 156 | { |
| 157 | ncnn::Mat m(w, h, (size_t)1u); |
| 158 | RandomizeS8(m); |
| 159 | return m; |
| 160 | } |
| 161 | |
| 162 | ncnn::Mat RandomS8Mat(int w, int h, int c) |
| 163 | { |
| 164 | ncnn::Mat m(w, h, c, (size_t)1u); |
| 165 | RandomizeS8(m); |
| 166 | return m; |
| 167 | } |
| 168 | |
| 169 | ncnn::Mat RandomS8Mat(int w, int h, int d, int c) |
| 170 | { |
| 171 | ncnn::Mat m(w, h, d, c, (size_t)1u); |
| 172 | RandomizeS8(m); |
| 173 | return m; |
| 174 | } |
| 175 | |
| 176 | ncnn::Mat scales_mat(const ncnn::Mat& mat, int m, int k, int ldx) |
| 177 | { |
| 178 | ncnn::Mat weight_scales(m); |
| 179 | for (int i = 0; i < m; ++i) |
| 180 | { |
| 181 | float min = mat[0], _max = mat[0]; |
| 182 | const float* ptr = (const float*)(mat.data) + i * ldx; |
| 183 | for (int j = 0; j < k; ++j) |
| 184 | { |
| 185 | if (min > ptr[j]) |
| 186 | { |
| 187 | min = ptr[j]; |
| 188 | } |
| 189 | if (_max < ptr[j]) |
| 190 | { |
| 191 | _max = ptr[j]; |
| 192 | } |
| 193 | } |
| 194 | const float abs_min = abs(min), abs_max = abs(_max); |
| 195 | weight_scales[i] = 127.f / (abs_min > abs_max ? abs_min : abs_max); |
| 196 | } |
| 197 | return weight_scales; |
| 198 | } |
| 199 | |
| 200 | bool NearlyEqual(float a, float b, float epsilon) |
| 201 | { |
| 202 | if (a == b) |
| 203 | return true; |
| 204 | |
| 205 | float diff = (float)fabs(a - b); |
| 206 | if (diff <= epsilon) |
| 207 | return true; |
| 208 | |
| 209 | // relative error |
| 210 | return diff < epsilon * std::max(fabs(a), fabs(b)); |
| 211 | } |
| 212 | |
| 213 | int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon) |
| 214 | { |
| 215 | #define CHECK_MEMBER(m) \ |
| 216 | if (a.m != b.m) \ |
| 217 | { \ |
| 218 | fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \ |
| 219 | return -1; \ |
| 220 | } |
| 221 | |
| 222 | CHECK_MEMBER(dims) |
| 223 | CHECK_MEMBER(w) |
| 224 | CHECK_MEMBER(h) |
| 225 | CHECK_MEMBER(d) |
| 226 | CHECK_MEMBER(c) |
| 227 | CHECK_MEMBER(elemsize) |
| 228 | CHECK_MEMBER(elempack) |
| 229 | |
| 230 | #undef CHECK_MEMBER |
| 231 | |
| 232 | for (int q = 0; q < a.c; q++) |
| 233 | { |
| 234 | const ncnn::Mat ma = a.channel(q); |
| 235 | const ncnn::Mat mb = b.channel(q); |
| 236 | for (int z = 0; z < a.d; z++) |
| 237 | { |
| 238 | const ncnn::Mat da = ma.depth(z); |
| 239 | const ncnn::Mat db = mb.depth(z); |
| 240 | for (int i = 0; i < a.h; i++) |
| 241 | { |
| 242 | const float* pa = da.row(i); |
| 243 | const float* pb = db.row(i); |
| 244 | for (int j = 0; j < a.w; j++) |
| 245 | { |
| 246 | if (!NearlyEqual(pa[j], pb[j], epsilon)) |
| 247 | { |
| 248 | fprintf(stderr, "value not match at c:%d d:%d h:%d w:%d expect %f but got %f\n", q, z, i, j, pa[j], pb[j]); |
| 249 | return -1; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | int CompareMat(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon) |
| 260 | { |
| 261 | ncnn::Option opt; |
| 262 | opt.num_threads = 1; |
| 263 | |
| 264 | if (a.elempack != 1) |
| 265 | { |
| 266 | ncnn::Mat a1; |
| 267 | ncnn::convert_packing(a, a1, 1, opt); |
| 268 | return CompareMat(a1, b, epsilon); |
| 269 | } |
| 270 | |
| 271 | if (b.elempack != 1) |
| 272 | { |
| 273 | ncnn::Mat b1; |
| 274 | ncnn::convert_packing(b, b1, 1, opt); |
| 275 | return CompareMat(a, b1, epsilon); |
| 276 | } |
| 277 | |
| 278 | if (a.elemsize == 2u) |
| 279 | { |
| 280 | ncnn::Mat a32; |
| 281 | cast_float16_to_float32(a, a32, opt); |
| 282 | return CompareMat(a32, b, epsilon); |
| 283 | } |
| 284 | if (a.elemsize == 1u) |
| 285 | { |
| 286 | ncnn::Mat a32; |
| 287 | cast_int8_to_float32(a, a32, opt); |
| 288 | return CompareMat(a32, b, epsilon); |
| 289 | } |
| 290 | |
| 291 | if (b.elemsize == 2u) |
| 292 | { |
| 293 | ncnn::Mat b32; |
| 294 | cast_float16_to_float32(b, b32, opt); |
| 295 | return CompareMat(a, b32, epsilon); |
| 296 | } |
| 297 | if (b.elemsize == 1u) |
| 298 | { |
| 299 | ncnn::Mat b32; |
| 300 | cast_int8_to_float32(b, b32, opt); |
| 301 | return CompareMat(a, b32, epsilon); |
| 302 | } |
| 303 | |
| 304 | return Compare(a, b, epsilon); |
| 305 | } |
| 306 | |
| 307 | int CompareMat(const std::vector<ncnn::Mat>& a, const std::vector<ncnn::Mat>& b, float epsilon) |
| 308 | { |
| 309 | if (a.size() != b.size()) |
| 310 | { |
| 311 | fprintf(stderr, "output blob count not match %zu %zu\n", a.size(), b.size()); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | for (size_t i = 0; i < a.size(); i++) |
| 316 | { |
| 317 | if (CompareMat(a[i], b[i], epsilon)) |
| 318 | { |
| 319 | fprintf(stderr, "output blob %zu not match\n", i); |
| 320 | return -1; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int convert_to_optimal_layout(const ncnn::Mat& a, ncnn::Mat& a4, const ncnn::Option& opt, const ncnn::Layer* op, int flag) |
| 328 | { |
| 329 | // clang-format off |
| 330 | // *INDENT-OFF* |
| 331 | #if NCNN_VFPV4 |
| 332 | if (opt.use_fp16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 333 | { |
| 334 | ncnn::cast_float32_to_float16(a, a4, opt); |
| 335 | } |
| 336 | else |
| 337 | #endif // NCNN_VFPV4 |
| 338 | #if NCNN_RVV |
| 339 | if (opt.use_fp16_storage && ncnn::cpu_support_riscv_v() && ncnn::cpu_support_riscv_zfh() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 340 | { |
| 341 | ncnn::cast_float32_to_float16(a, a4, opt); |
| 342 | } |
| 343 | else |
| 344 | #endif // NCNN_RVV |
| 345 | #if NCNN_BF16 |
| 346 | if (opt.use_bf16_storage && op->support_bf16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 347 | { |
| 348 | ncnn::cast_float32_to_bfloat16(a, a4, opt); |
| 349 | } |
| 350 | else |
| 351 | #endif // NCNN_BF16 |
| 352 | if (opt.use_fp16_storage && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 353 | { |
| 354 | ncnn::cast_float32_to_float16(a, a4, opt); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | a4 = a; |
| 359 | } |
| 360 | // *INDENT-ON* |
| 361 | // clang-format on |
| 362 | |
| 363 | if (opt.use_packing_layout && op->support_packing && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_PACKING)) |
| 364 | { |
| 365 | // resolve dst_elempack |
| 366 | int dims = a4.dims; |
| 367 | int elemcount = 0; |
| 368 | if (dims == 1) elemcount = a4.elempack * a4.w; |
| 369 | if (dims == 2) elemcount = a4.elempack * a4.h; |
| 370 | if (dims == 3 || dims == 4) elemcount = a4.elempack * a4.c; |
| 371 | |
| 372 | int elembits = a4.elembits(); |
| 373 | |
| 374 | int dst_elempack = 1; |
| 375 | |
| 376 | if (elembits == 32) |
| 377 | { |
| 378 | #if NCNN_AVX512 |
| 379 | if (elemcount % 16 == 0 && ncnn::cpu_support_x86_avx512()) |
| 380 | dst_elempack = 16; |
| 381 | else if (elemcount % 8 == 0 && ncnn::cpu_support_x86_avx()) |
| 382 | dst_elempack = 8; |
| 383 | else if (elemcount % 4 == 0) |
| 384 | dst_elempack = 4; |
| 385 | #elif NCNN_AVX |
| 386 | if (elemcount % 8 == 0 && ncnn::cpu_support_x86_avx()) |
| 387 | dst_elempack = 8; |
| 388 | else if (elemcount % 4 == 0) |
| 389 | dst_elempack = 4; |
| 390 | #elif NCNN_RVV |
| 391 | const int packn = ncnn::cpu_riscv_vlenb() / (elembits / 8); |
| 392 | if (elemcount % packn == 0) |
| 393 | dst_elempack = packn; |
| 394 | #else |
| 395 | if (elemcount % 4 == 0) |
| 396 | dst_elempack = 4; |
| 397 | #endif |
| 398 | } |
| 399 | if (elembits == 16) |
| 400 | { |
| 401 | #if NCNN_ARM82 |
| 402 | if (elemcount % 8 == 0 && ncnn::cpu_support_arm_asimdhp() && opt.use_fp16_arithmetic) |
| 403 | dst_elempack = 8; |
| 404 | else if (elemcount % 4 == 0) |
| 405 | dst_elempack = 4; |
| 406 | #elif NCNN_RVV |
| 407 | const int packn = ncnn::cpu_riscv_vlenb() / 2; |
| 408 | if (elemcount % packn == 0) |
| 409 | dst_elempack = packn; |
| 410 | #else |
| 411 | if (elemcount % 4 == 0) |
| 412 | dst_elempack = 4; |
| 413 | #endif |
| 414 | } |
| 415 | if (elembits == 8) |
| 416 | { |
| 417 | #if NCNN_RVV |
| 418 | const int packn = ncnn::cpu_riscv_vlenb() / 1; |
| 419 | if (elemcount % packn == 0) |
| 420 | dst_elempack = packn; |
| 421 | #else |
| 422 | if (elemcount % 8 == 0) |
| 423 | dst_elempack = 8; |
| 424 | #endif |
| 425 | } |
| 426 | |
| 427 | if (flag & TEST_LAYER_ENABLE_FORCE_INPUT_PACK8) |
| 428 | dst_elempack = 8; |
| 429 | |
| 430 | ncnn::Mat a4_packed; |
| 431 | ncnn::convert_packing(a4, a4_packed, dst_elempack, opt); |
| 432 | a4 = a4_packed; |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int convert_to_vanilla_layout(const ncnn::Mat& c4, ncnn::Mat& c, const ncnn::Option& opt, const ncnn::Layer* op, int flag) |
| 439 | { |
| 440 | ncnn::Mat c4_unpacked; |
| 441 | if (c4.elempack != 1) |
| 442 | { |
| 443 | ncnn::convert_packing(c4, c4_unpacked, 1, opt); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | c4_unpacked = c4; |
| 448 | } |
| 449 | |
| 450 | // clang-format off |
| 451 | // *INDENT-OFF* |
| 452 | #if NCNN_VFPV4 |
| 453 | if (opt.use_fp16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && c4_unpacked.elembits() == 16) |
| 454 | { |
| 455 | ncnn::cast_float16_to_float32(c4_unpacked, c, opt); |
| 456 | } |
| 457 | else |
| 458 | #endif // NCNN_VFPV4 |
| 459 | #if NCNN_RVV |
| 460 | if (opt.use_fp16_storage && ncnn::cpu_support_riscv_v() && ncnn::cpu_support_riscv_zfh() && op->support_fp16_storage && c4_unpacked.elembits() == 16) |
| 461 | { |
| 462 | ncnn::cast_float16_to_float32(c4_unpacked, c, opt); |
| 463 | } |
| 464 | else |
| 465 | #endif // NCNN_RVV |
| 466 | #if NCNN_BF16 |
| 467 | if (opt.use_bf16_storage && op->support_bf16_storage && c4_unpacked.elembits() == 16) |
| 468 | { |
| 469 | ncnn::cast_bfloat16_to_float32(c4_unpacked, c, opt); |
| 470 | } |
| 471 | else |
| 472 | #endif // NCNN_BF16 |
| 473 | if (opt.use_fp16_storage && op->support_fp16_storage && c4_unpacked.elembits() == 16) |
| 474 | { |
| 475 | ncnn::cast_float16_to_float32(c4_unpacked, c, opt); |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | c = c4_unpacked; |
| 480 | } |
| 481 | // *INDENT-ON* |
| 482 | // clang-format on |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int test_layer_naive(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const std::vector<ncnn::Mat>& a, int top_blob_count, std::vector<ncnn::Mat>& b, void (*func)(ncnn::Layer*), int flag) |
| 488 | { |
| 489 | ncnn::Layer* op = ncnn::create_layer_naive(typeindex); |
| 490 | |
| 491 | if (func) |
| 492 | { |
| 493 | (*func)((ncnn::Layer*)op); |
| 494 | } |
| 495 | |
| 496 | op->load_param(pd); |
| 497 | |
| 498 | if (op->one_blob_only && a.size() != 1) |
| 499 | { |
| 500 | fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n"); |
| 501 | delete op; |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 506 | |
| 507 | op->load_model(mb); |
| 508 | |
| 509 | ncnn::Option opt; |
| 510 | opt.num_threads = 1; |
| 511 | opt.lightmode = false; |
| 512 | opt.use_packing_layout = false; |
| 513 | opt.use_fp16_packed = false; |
| 514 | opt.use_fp16_storage = false; |
| 515 | opt.use_fp16_arithmetic = false; |
| 516 | opt.use_shader_pack8 = false; |
| 517 | opt.use_image_storage = false; |
| 518 | opt.use_bf16_storage = false; |
| 519 | opt.use_vulkan_compute = false; |
| 520 | |
| 521 | op->create_pipeline(opt); |
| 522 | |
| 523 | b.resize(top_blob_count); |
| 524 | |
| 525 | if (op->support_inplace) |
| 526 | { |
| 527 | for (size_t i = 0; i < a.size(); i++) |
| 528 | { |
| 529 | b[i] = a[i].clone(); |
| 530 | } |
| 531 | |
| 532 | op->forward_inplace(b, opt); |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | op->forward(a, b, opt); |
| 537 | } |
| 538 | |
| 539 | op->destroy_pipeline(opt); |
| 540 | |
| 541 | delete op; |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | int test_layer_cpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, std::vector<ncnn::Mat>& c, const std::vector<ncnn::Mat>& top_shapes, void (*func)(ncnn::Layer*), int flag) |
| 547 | { |
| 548 | ncnn::Layer* op = ncnn::create_layer_cpu(typeindex); |
| 549 | |
| 550 | if (!op->support_packing && _opt.use_packing_layout) |
| 551 | { |
| 552 | delete op; |
| 553 | return 233; |
| 554 | } |
| 555 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 556 | { |
| 557 | delete op; |
| 558 | return 233; |
| 559 | } |
| 560 | |
| 561 | if (func) |
| 562 | { |
| 563 | (*func)((ncnn::Layer*)op); |
| 564 | } |
| 565 | |
| 566 | if (!top_shapes.empty()) |
| 567 | { |
| 568 | op->bottom_shapes = a; |
| 569 | op->top_shapes = top_shapes; |
| 570 | } |
| 571 | |
| 572 | op->load_param(pd); |
| 573 | |
| 574 | if (op->one_blob_only && a.size() != 1) |
| 575 | { |
| 576 | fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n"); |
| 577 | delete op; |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 582 | |
| 583 | op->load_model(mb); |
| 584 | |
| 585 | ncnn::Option opt = _opt; |
| 586 | opt.num_threads = 1; |
| 587 | opt.use_vulkan_compute = false; |
| 588 | |
| 589 | op->create_pipeline(opt); |
| 590 | |
| 591 | if (!op->support_packing && _opt.use_packing_layout) |
| 592 | { |
| 593 | op->destroy_pipeline(opt); |
| 594 | delete op; |
| 595 | return 233; |
| 596 | } |
| 597 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 598 | { |
| 599 | op->destroy_pipeline(opt); |
| 600 | delete op; |
| 601 | return 233; |
| 602 | } |
| 603 | |
| 604 | std::vector<ncnn::Mat> a4(a.size()); |
| 605 | |
| 606 | for (size_t i = 0; i < a4.size(); i++) |
| 607 | { |
| 608 | convert_to_optimal_layout(a[i], a4[i], opt, op, flag); |
| 609 | } |
| 610 | |
| 611 | c.resize(top_blob_count); |
| 612 | |
| 613 | if (op->support_inplace) |
| 614 | { |
| 615 | for (size_t i = 0; i < a4.size(); i++) |
| 616 | { |
| 617 | c[i] = a4[i].clone(); |
| 618 | } |
| 619 | |
| 620 | op->forward_inplace(c, opt); |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | op->forward(a4, c, opt); |
| 625 | } |
| 626 | |
| 627 | for (size_t i = 0; i < c.size(); i++) |
| 628 | { |
| 629 | convert_to_vanilla_layout(c[i], c[i], opt, op, flag); |
| 630 | } |
| 631 | |
| 632 | op->destroy_pipeline(opt); |
| 633 | |
| 634 | delete op; |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | #if NCNN_VULKAN |
| 640 | int test_layer_gpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, std::vector<ncnn::Mat>& d, const std::vector<ncnn::Mat>& top_shapes, void (*func)(ncnn::Layer*), int flag) |
| 641 | { |
| 642 | if (!_opt.use_packing_layout) |
| 643 | { |
| 644 | // pack1 test is useless for gpu |
| 645 | return 233; |
| 646 | } |
| 647 | |
| 648 | ncnn::Layer* op = ncnn::create_layer_vulkan(typeindex); |
| 649 | if (!op) |
| 650 | { |
| 651 | return 233; |
| 652 | } |
| 653 | |
| 654 | op->load_param(pd); |
| 655 | |
| 656 | if (!op->support_vulkan) |
| 657 | { |
| 658 | delete op; |
| 659 | return 233; |
| 660 | } |
| 661 | |
| 662 | ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device(); |
| 663 | |
| 664 | op->vkdev = vkdev; |
| 665 | |
| 666 | if (func) |
| 667 | { |
| 668 | (*func)((ncnn::Layer*)op); |
| 669 | } |
| 670 | |
| 671 | if (!top_shapes.empty()) |
| 672 | { |
| 673 | op->bottom_shapes = a; |
| 674 | op->top_shapes = top_shapes; |
| 675 | } |
| 676 | |
| 677 | if (op->one_blob_only && a.size() != 1) |
| 678 | { |
| 679 | fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n"); |
| 680 | delete op; |
| 681 | return -1; |
| 682 | } |
| 683 | |
| 684 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 685 | |
| 686 | op->load_model(mb); |
| 687 | |
| 688 | ncnn::VkWeightAllocator g_weight_vkallocator(vkdev); |
| 689 | ncnn::VkWeightStagingAllocator g_weight_staging_vkallocator(vkdev); |
| 690 | |
| 691 | ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator(); |
| 692 | ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator(); |
| 693 | |
| 694 | ncnn::Option opt = _opt; |
| 695 | opt.num_threads = 1; |
| 696 | opt.use_vulkan_compute = true; |
| 697 | |
| 698 | #if __APPLE__ |
| 699 | opt.use_image_storage = false; |
| 700 | #endif |
| 701 | |
| 702 | opt.blob_vkallocator = blob_vkallocator; |
| 703 | opt.workspace_vkallocator = blob_vkallocator; |
| 704 | opt.staging_vkallocator = staging_vkallocator; |
| 705 | |
| 706 | if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false; |
| 707 | if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false; |
| 708 | if (!vkdev->info.support_fp16_uniform()) opt.use_fp16_uniform = false; |
| 709 | if (!vkdev->info.support_fp16_arithmetic()) opt.use_fp16_arithmetic = false; |
| 710 | if (!vkdev->info.support_int8_packed()) opt.use_int8_packed = false; |
| 711 | if (!vkdev->info.support_int8_storage()) opt.use_int8_storage = false; |
| 712 | if (!vkdev->info.support_int8_uniform()) opt.use_int8_uniform = false; |
| 713 | if (!vkdev->info.support_int8_arithmetic()) opt.use_int8_arithmetic = false; |
| 714 | if (!vkdev->info.support_cooperative_matrix()) opt.use_cooperative_matrix = false; |
| 715 | |
| 716 | // FIXME fp16a may produce large error |
| 717 | opt.use_fp16_arithmetic = false; |
| 718 | |
| 719 | op->create_pipeline(opt); |
| 720 | |
| 721 | if (!op->support_vulkan) |
| 722 | { |
| 723 | op->destroy_pipeline(opt); |
| 724 | delete op; |
| 725 | return 233; |
| 726 | } |
| 727 | |
| 728 | { |
| 729 | ncnn::VkTransfer cmd(vkdev); |
| 730 | |
| 731 | ncnn::Option opt_upload = opt; |
| 732 | opt_upload.blob_vkallocator = &g_weight_vkallocator; |
| 733 | opt_upload.workspace_vkallocator = &g_weight_vkallocator; |
| 734 | opt_upload.staging_vkallocator = &g_weight_staging_vkallocator; |
| 735 | |
| 736 | op->upload_model(cmd, opt_upload); |
| 737 | |
| 738 | cmd.submit_and_wait(); |
| 739 | } |
| 740 | |
| 741 | d.resize(top_blob_count); |
| 742 | |
| 743 | { |
| 744 | // forward |
| 745 | ncnn::VkCompute cmd(vkdev); |
| 746 | |
| 747 | if (op->support_image_storage && opt.use_image_storage) |
| 748 | { |
| 749 | // upload |
| 750 | std::vector<ncnn::VkImageMat> a_gpu(a.size()); |
| 751 | for (size_t i = 0; i < a_gpu.size(); i++) |
| 752 | { |
| 753 | cmd.record_upload(a[i], a_gpu[i], opt); |
| 754 | } |
| 755 | |
| 756 | std::vector<ncnn::VkImageMat> d_gpu(top_blob_count); |
| 757 | if (op->support_inplace) |
| 758 | { |
| 759 | op->forward_inplace(a_gpu, cmd, opt); |
| 760 | |
| 761 | d_gpu = a_gpu; |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | op->forward(a_gpu, d_gpu, cmd, opt); |
| 766 | } |
| 767 | |
| 768 | // download |
| 769 | for (size_t i = 0; i < d_gpu.size(); i++) |
| 770 | { |
| 771 | cmd.record_download(d_gpu[i], d[i], opt); |
| 772 | } |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | // upload |
| 777 | std::vector<ncnn::VkMat> a_gpu(a.size()); |
| 778 | for (size_t i = 0; i < a_gpu.size(); i++) |
| 779 | { |
| 780 | cmd.record_upload(a[i], a_gpu[i], opt); |
| 781 | } |
| 782 | |
| 783 | std::vector<ncnn::VkMat> d_gpu(top_blob_count); |
| 784 | if (op->support_inplace) |
| 785 | { |
| 786 | op->forward_inplace(a_gpu, cmd, opt); |
| 787 | |
| 788 | d_gpu = a_gpu; |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | op->forward(a_gpu, d_gpu, cmd, opt); |
| 793 | } |
| 794 | |
| 795 | // download |
| 796 | for (size_t i = 0; i < d_gpu.size(); i++) |
| 797 | { |
| 798 | cmd.record_download(d_gpu[i], d[i], opt); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | cmd.submit_and_wait(); |
| 803 | } |
| 804 | |
| 805 | op->destroy_pipeline(opt); |
| 806 | |
| 807 | delete op; |
| 808 | |
| 809 | vkdev->reclaim_blob_allocator(blob_vkallocator); |
| 810 | vkdev->reclaim_staging_allocator(staging_vkallocator); |
| 811 | g_weight_vkallocator.clear(); |
| 812 | g_weight_staging_vkallocator.clear(); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | #endif // NCNN_VULKAN |
| 817 | |
| 818 | int test_layer(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, const std::vector<ncnn::Mat>& top_shapes, float epsilon, void (*func)(ncnn::Layer*), int flag) |
| 819 | { |
| 820 | // naive |
| 821 | std::vector<ncnn::Mat> b; |
| 822 | { |
| 823 | int ret = test_layer_naive(typeindex, pd, weights, a, top_blob_count, b, func, flag); |
| 824 | if (ret != 233 && ret != 0) |
| 825 | { |
| 826 | fprintf(stderr, "test_layer_naive failed\n"); |
| 827 | return -1; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | // cpu |
| 832 | { |
| 833 | std::vector<ncnn::Mat> c; |
| 834 | int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, top_blob_count, c, std::vector<ncnn::Mat>(), func, flag); |
| 835 | if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0)) |
| 836 | { |
| 837 | fprintf(stderr, "test_layer_cpu failed\n"); |
| 838 | return -1; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | // cpu shape hint |
| 843 | { |
| 844 | std::vector<ncnn::Mat> c; |
| 845 | int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, top_blob_count, c, b, func, flag); |
| 846 | if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0)) |
| 847 | { |
| 848 | fprintf(stderr, "test_layer_cpu failed with shape hint\n"); |
| 849 | return -1; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | #if NCNN_VULKAN |
| 854 | // gpu |
| 855 | if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING)) |
| 856 | { |
| 857 | std::vector<ncnn::Mat> d; |
| 858 | int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, top_blob_count, d, std::vector<ncnn::Mat>(), func, flag); |
| 859 | if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0)) |
| 860 | { |
| 861 | fprintf(stderr, "test_layer_gpu failed\n"); |
| 862 | return -1; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | // gpu shape hint |
| 867 | if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING)) |
| 868 | { |
| 869 | std::vector<ncnn::Mat> d; |
| 870 | int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, top_blob_count, d, b, func, flag); |
| 871 | if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0)) |
| 872 | { |
| 873 | fprintf(stderr, "test_layer_gpu failed with shape hint\n"); |
| 874 | return -1; |
| 875 | } |
| 876 | } |
| 877 | #endif // NCNN_VULKAN |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | int test_layer_naive(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Mat& a, ncnn::Mat& b, void (*func)(ncnn::Layer*), int flag) |
| 883 | { |
| 884 | ncnn::Layer* op = ncnn::create_layer_naive(typeindex); |
| 885 | |
| 886 | if (func) |
| 887 | { |
| 888 | (*func)((ncnn::Layer*)op); |
| 889 | } |
| 890 | |
| 891 | op->load_param(pd); |
| 892 | |
| 893 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 894 | |
| 895 | op->load_model(mb); |
| 896 | |
| 897 | ncnn::Option opt; |
| 898 | opt.num_threads = 1; |
| 899 | opt.lightmode = false; |
| 900 | opt.use_packing_layout = false; |
| 901 | opt.use_fp16_packed = false; |
| 902 | opt.use_fp16_storage = false; |
| 903 | opt.use_fp16_arithmetic = false; |
| 904 | opt.use_shader_pack8 = false; |
| 905 | opt.use_image_storage = false; |
| 906 | opt.use_bf16_storage = false; |
| 907 | opt.use_vulkan_compute = false; |
| 908 | |
| 909 | op->create_pipeline(opt); |
| 910 | |
| 911 | if (op->support_inplace) |
| 912 | { |
| 913 | b = a.clone(); |
| 914 | op->forward_inplace(b, opt); |
| 915 | } |
| 916 | else |
| 917 | { |
| 918 | op->forward(a, b, opt); |
| 919 | } |
| 920 | |
| 921 | op->destroy_pipeline(opt); |
| 922 | |
| 923 | delete op; |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | int test_layer_cpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, ncnn::Mat& c, const ncnn::Mat& top_shape, void (*func)(ncnn::Layer*), int flag) |
| 929 | { |
| 930 | ncnn::Layer* op = ncnn::create_layer_cpu(typeindex); |
| 931 | |
| 932 | if (!op->support_packing && _opt.use_packing_layout) |
| 933 | { |
| 934 | delete op; |
| 935 | return 233; |
| 936 | } |
| 937 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 938 | { |
| 939 | delete op; |
| 940 | return 233; |
| 941 | } |
| 942 | |
| 943 | if (func) |
| 944 | { |
| 945 | (*func)((ncnn::Layer*)op); |
| 946 | } |
| 947 | |
| 948 | if (top_shape.dims) |
| 949 | { |
| 950 | op->bottom_shapes.resize(1); |
| 951 | op->top_shapes.resize(1); |
| 952 | op->bottom_shapes[0] = a; |
| 953 | op->top_shapes[0] = top_shape; |
| 954 | } |
| 955 | |
| 956 | op->load_param(pd); |
| 957 | |
| 958 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 959 | |
| 960 | op->load_model(mb); |
| 961 | |
| 962 | ncnn::Option opt = _opt; |
| 963 | opt.num_threads = 1; |
| 964 | opt.use_vulkan_compute = false; |
| 965 | |
| 966 | op->create_pipeline(opt); |
| 967 | |
| 968 | if (!op->support_packing && _opt.use_packing_layout) |
| 969 | { |
| 970 | op->destroy_pipeline(opt); |
| 971 | delete op; |
| 972 | return 233; |
| 973 | } |
| 974 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 975 | { |
| 976 | op->destroy_pipeline(opt); |
| 977 | delete op; |
| 978 | return 233; |
| 979 | } |
| 980 | |
| 981 | ncnn::Mat a4; |
| 982 | convert_to_optimal_layout(a, a4, opt, op, flag); |
| 983 | |
| 984 | if (op->support_inplace) |
| 985 | { |
| 986 | c = a4.clone(); |
| 987 | op->forward_inplace(c, opt); |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | op->forward(a4, c, opt); |
| 992 | } |
| 993 | |
| 994 | convert_to_vanilla_layout(c, c, opt, op, flag); |
| 995 | |
| 996 | op->destroy_pipeline(opt); |
| 997 | |
| 998 | delete op; |
| 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | #if NCNN_VULKAN |
| 1004 | int test_layer_gpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, ncnn::Mat& d, const ncnn::Mat& top_shape, void (*func)(ncnn::Layer*), int flag) |
| 1005 | { |
| 1006 | if (!_opt.use_packing_layout) |
| 1007 | { |
| 1008 | // pack1 test is useless for gpu |
| 1009 | return 233; |
| 1010 | } |
| 1011 | |
| 1012 | ncnn::Layer* op = ncnn::create_layer_vulkan(typeindex); |
| 1013 | if (!op) |
| 1014 | { |
| 1015 | return 233; |
| 1016 | } |
| 1017 | |
| 1018 | op->load_param(pd); |
| 1019 | |
| 1020 | if (!op->support_vulkan) |
| 1021 | { |
| 1022 | delete op; |
| 1023 | return 233; |
| 1024 | } |
| 1025 | |
| 1026 | ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device(); |
| 1027 | |
| 1028 | op->vkdev = vkdev; |
| 1029 | |
| 1030 | if (func) |
| 1031 | { |
| 1032 | (*func)((ncnn::Layer*)op); |
| 1033 | } |
| 1034 | |
| 1035 | if (top_shape.dims) |
| 1036 | { |
| 1037 | op->bottom_shapes.resize(1); |
| 1038 | op->top_shapes.resize(1); |
| 1039 | op->bottom_shapes[0] = a; |
| 1040 | op->top_shapes[0] = top_shape; |
| 1041 | } |
| 1042 | |
| 1043 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 1044 | |
| 1045 | op->load_model(mb); |
| 1046 | |
| 1047 | ncnn::VkWeightAllocator g_weight_vkallocator(vkdev); |
| 1048 | ncnn::VkWeightStagingAllocator g_weight_staging_vkallocator(vkdev); |
| 1049 | |
| 1050 | ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator(); |
| 1051 | ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator(); |
| 1052 | |
| 1053 | ncnn::Option opt = _opt; |
| 1054 | opt.num_threads = 1; |
| 1055 | opt.use_vulkan_compute = true; |
| 1056 | |
| 1057 | #if __APPLE__ |
| 1058 | opt.use_image_storage = false; |
| 1059 | #endif |
| 1060 | |
| 1061 | opt.blob_vkallocator = blob_vkallocator; |
| 1062 | opt.workspace_vkallocator = blob_vkallocator; |
| 1063 | opt.staging_vkallocator = staging_vkallocator; |
| 1064 | |
| 1065 | if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false; |
| 1066 | if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false; |
| 1067 | if (!vkdev->info.support_fp16_uniform()) opt.use_fp16_uniform = false; |
| 1068 | if (!vkdev->info.support_fp16_arithmetic()) opt.use_fp16_arithmetic = false; |
| 1069 | if (!vkdev->info.support_int8_packed()) opt.use_int8_packed = false; |
| 1070 | if (!vkdev->info.support_int8_storage()) opt.use_int8_storage = false; |
| 1071 | if (!vkdev->info.support_int8_uniform()) opt.use_int8_uniform = false; |
| 1072 | if (!vkdev->info.support_int8_arithmetic()) opt.use_int8_arithmetic = false; |
| 1073 | if (!vkdev->info.support_cooperative_matrix()) opt.use_cooperative_matrix = false; |
| 1074 | |
| 1075 | // FIXME fp16a may produce large error |
| 1076 | opt.use_fp16_arithmetic = false; |
| 1077 | |
| 1078 | op->create_pipeline(opt); |
| 1079 | |
| 1080 | if (!op->support_vulkan) |
| 1081 | { |
| 1082 | op->destroy_pipeline(opt); |
| 1083 | delete op; |
| 1084 | return 233; |
| 1085 | } |
| 1086 | |
| 1087 | { |
| 1088 | ncnn::VkTransfer cmd(vkdev); |
| 1089 | |
| 1090 | ncnn::Option opt_upload = opt; |
| 1091 | opt_upload.blob_vkallocator = &g_weight_vkallocator; |
| 1092 | opt_upload.workspace_vkallocator = &g_weight_vkallocator; |
| 1093 | opt_upload.staging_vkallocator = &g_weight_staging_vkallocator; |
| 1094 | |
| 1095 | op->upload_model(cmd, opt_upload); |
| 1096 | |
| 1097 | cmd.submit_and_wait(); |
| 1098 | } |
| 1099 | |
| 1100 | { |
| 1101 | // forward |
| 1102 | ncnn::VkCompute cmd(vkdev); |
| 1103 | |
| 1104 | if (op->support_image_storage && opt.use_image_storage) |
| 1105 | { |
| 1106 | // upload |
| 1107 | ncnn::VkImageMat a_gpu; |
| 1108 | cmd.record_upload(a, a_gpu, opt); |
| 1109 | |
| 1110 | ncnn::VkImageMat d_gpu; |
| 1111 | if (op->support_inplace) |
| 1112 | { |
| 1113 | op->forward_inplace(a_gpu, cmd, opt); |
| 1114 | |
| 1115 | d_gpu = a_gpu; |
| 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | op->forward(a_gpu, d_gpu, cmd, opt); |
| 1120 | } |
| 1121 | |
| 1122 | // download |
| 1123 | cmd.record_download(d_gpu, d, opt); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
| 1127 | // upload |
| 1128 | ncnn::VkMat a_gpu; |
| 1129 | cmd.record_upload(a, a_gpu, opt); |
| 1130 | |
| 1131 | ncnn::VkMat d_gpu; |
| 1132 | if (op->support_inplace) |
| 1133 | { |
| 1134 | op->forward_inplace(a_gpu, cmd, opt); |
| 1135 | |
| 1136 | d_gpu = a_gpu; |
| 1137 | } |
| 1138 | else |
| 1139 | { |
| 1140 | op->forward(a_gpu, d_gpu, cmd, opt); |
| 1141 | } |
| 1142 | |
| 1143 | // download |
| 1144 | cmd.record_download(d_gpu, d, opt); |
| 1145 | } |
| 1146 | |
| 1147 | cmd.submit_and_wait(); |
| 1148 | } |
| 1149 | |
| 1150 | op->destroy_pipeline(opt); |
| 1151 | |
| 1152 | delete op; |
| 1153 | |
| 1154 | vkdev->reclaim_blob_allocator(blob_vkallocator); |
| 1155 | vkdev->reclaim_staging_allocator(staging_vkallocator); |
| 1156 | g_weight_vkallocator.clear(); |
| 1157 | g_weight_staging_vkallocator.clear(); |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | #endif // NCNN_VULKAN |
| 1162 | |
| 1163 | int test_layer(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, const ncnn::Mat& top_shape, float epsilon, void (*func)(ncnn::Layer*), int flag) |
| 1164 | { |
| 1165 | // naive |
| 1166 | ncnn::Mat b; |
| 1167 | { |
| 1168 | int ret = test_layer_naive(typeindex, pd, weights, a, b, func, flag); |
| 1169 | if (ret != 233 && ret != 0) |
| 1170 | { |
| 1171 | fprintf(stderr, "test_layer_naive failed\n"); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | // cpu |
| 1177 | { |
| 1178 | ncnn::Mat c; |
| 1179 | int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, c, ncnn::Mat(), func, flag); |
| 1180 | if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0)) |
| 1181 | { |
| 1182 | fprintf(stderr, "test_layer_cpu failed\n"); |
| 1183 | return -1; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | // cpu shape hint |
| 1188 | { |
| 1189 | ncnn::Mat c; |
| 1190 | int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, c, b, func, flag); |
| 1191 | if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0)) |
| 1192 | { |
| 1193 | fprintf(stderr, "test_layer_cpu failed with shape hint\n"); |
| 1194 | return -1; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | #if NCNN_VULKAN |
| 1199 | // gpu |
| 1200 | if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING)) |
| 1201 | { |
| 1202 | ncnn::Mat d; |
| 1203 | int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, d, ncnn::Mat(), func, flag); |
| 1204 | if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0)) |
| 1205 | { |
| 1206 | fprintf(stderr, "test_layer_gpu failed\n"); |
| 1207 | return -1; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | // gpu shape hint |
| 1212 | if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING)) |
| 1213 | { |
| 1214 | ncnn::Mat d; |
| 1215 | int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, d, b, func, flag); |
| 1216 | if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0)) |
| 1217 | { |
| 1218 | fprintf(stderr, "test_layer_gpu failed with shape hint\n"); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | } |
| 1222 | #endif // NCNN_VULKAN |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | int test_layer_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& opt, const std::vector<ncnn::Mat>& a, int top_blob_count, float epsilon, void (*func)(ncnn::Layer*), int flag) |
| 1228 | { |
| 1229 | // fp16 representation |
| 1230 | std::vector<ncnn::Mat> a_fp16; |
| 1231 | if (opt.use_bf16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 1232 | { |
| 1233 | a_fp16.resize(a.size()); |
| 1234 | for (size_t j = 0; j < a.size(); j++) |
| 1235 | { |
| 1236 | ncnn::Mat tmp; |
| 1237 | ncnn::cast_float32_to_bfloat16(a[j], tmp, opt); |
| 1238 | ncnn::cast_bfloat16_to_float32(tmp, a_fp16[j], opt); |
| 1239 | } |
| 1240 | } |
| 1241 | else if ((opt.use_fp16_packed || opt.use_fp16_storage) && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 1242 | { |
| 1243 | a_fp16.resize(a.size()); |
| 1244 | for (size_t j = 0; j < a.size(); j++) |
| 1245 | { |
| 1246 | ncnn::Mat tmp; |
| 1247 | ncnn::cast_float32_to_float16(a[j], tmp, opt); |
| 1248 | ncnn::cast_float16_to_float32(tmp, a_fp16[j], opt); |
| 1249 | } |
| 1250 | } |
| 1251 | else |
| 1252 | { |
| 1253 | a_fp16 = a; |
| 1254 | } |
| 1255 | |
| 1256 | std::vector<ncnn::Mat> weights_fp16; |
| 1257 | float epsilon_fp16; |
| 1258 | if (opt.use_bf16_storage) |
| 1259 | { |
| 1260 | weights_fp16.resize(weights.size()); |
| 1261 | for (size_t j = 0; j < weights.size(); j++) |
| 1262 | { |
| 1263 | if (weights[j].elembits() != 32) |
| 1264 | { |
| 1265 | weights_fp16[j] = weights[j]; |
| 1266 | continue; |
| 1267 | } |
| 1268 | |
| 1269 | ncnn::Mat tmp; |
| 1270 | ncnn::cast_float32_to_bfloat16(weights[j], tmp, opt); |
| 1271 | ncnn::cast_bfloat16_to_float32(tmp, weights_fp16[j], opt); |
| 1272 | } |
| 1273 | epsilon_fp16 = epsilon * 100; // 0.1 |
| 1274 | } |
| 1275 | else if (opt.use_fp16_packed || opt.use_fp16_storage) |
| 1276 | { |
| 1277 | weights_fp16.resize(weights.size()); |
| 1278 | for (size_t j = 0; j < weights.size(); j++) |
| 1279 | { |
| 1280 | if (weights[j].elembits() != 32) |
| 1281 | { |
| 1282 | weights_fp16[j] = weights[j]; |
| 1283 | continue; |
| 1284 | } |
| 1285 | |
| 1286 | ncnn::Mat tmp; |
| 1287 | ncnn::cast_float32_to_float16(weights[j], tmp, opt); |
| 1288 | ncnn::cast_float16_to_float32(tmp, weights_fp16[j], opt); |
| 1289 | } |
| 1290 | epsilon_fp16 = epsilon * 100; // 0.1 |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | weights_fp16 = weights; |
| 1295 | epsilon_fp16 = epsilon; |
| 1296 | } |
| 1297 | |
| 1298 | if (opt.use_fp16_arithmetic) |
| 1299 | { |
| 1300 | epsilon_fp16 = epsilon * 1000; // 1.0 |
| 1301 | } |
| 1302 | |
| 1303 | std::vector<ncnn::Mat> top_shapes; |
| 1304 | int ret = test_layer(ncnn::layer_to_index(layer_type), pd, weights_fp16, opt, a_fp16, top_blob_count, top_shapes, epsilon_fp16, func, flag); |
| 1305 | if (ret != 0) |
| 1306 | { |
| 1307 | fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_fp16_arithmetic=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d use_sgemm_convolution=%d use_winograd_convolution=%d\n", layer_type, opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_fp16_arithmetic, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage, opt.use_sgemm_convolution, opt.use_winograd_convolution); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | int test_layer_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& opt, const ncnn::Mat& a, float epsilon, void (*func)(ncnn::Layer*), int flag) |
| 1315 | { |
| 1316 | // fp16 representation |
| 1317 | ncnn::Mat a_fp16; |
| 1318 | if (opt.use_bf16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 1319 | { |
| 1320 | ncnn::Mat tmp; |
| 1321 | ncnn::cast_float32_to_bfloat16(a, tmp, opt); |
| 1322 | ncnn::cast_bfloat16_to_float32(tmp, a_fp16, opt); |
| 1323 | } |
| 1324 | else if ((opt.use_fp16_packed || opt.use_fp16_storage) && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING)) |
| 1325 | { |
| 1326 | ncnn::Mat tmp; |
| 1327 | ncnn::cast_float32_to_float16(a, tmp, opt); |
| 1328 | ncnn::cast_float16_to_float32(tmp, a_fp16, opt); |
| 1329 | } |
| 1330 | else |
| 1331 | { |
| 1332 | a_fp16 = a; |
| 1333 | } |
| 1334 | |
| 1335 | std::vector<ncnn::Mat> weights_fp16; |
| 1336 | float epsilon_fp16; |
| 1337 | if (opt.use_bf16_storage) |
| 1338 | { |
| 1339 | weights_fp16.resize(weights.size()); |
| 1340 | for (size_t j = 0; j < weights.size(); j++) |
| 1341 | { |
| 1342 | if (weights[j].elembits() != 32) |
| 1343 | { |
| 1344 | weights_fp16[j] = weights[j]; |
| 1345 | continue; |
| 1346 | } |
| 1347 | |
| 1348 | ncnn::Mat tmp; |
| 1349 | ncnn::cast_float32_to_bfloat16(weights[j], tmp, opt); |
| 1350 | ncnn::cast_bfloat16_to_float32(tmp, weights_fp16[j], opt); |
| 1351 | } |
| 1352 | epsilon_fp16 = epsilon * 100; // 0.1 |
| 1353 | } |
| 1354 | else if (opt.use_fp16_packed || opt.use_fp16_storage) |
| 1355 | { |
| 1356 | weights_fp16.resize(weights.size()); |
| 1357 | for (size_t j = 0; j < weights.size(); j++) |
| 1358 | { |
| 1359 | if (weights[j].elembits() != 32) |
| 1360 | { |
| 1361 | weights_fp16[j] = weights[j]; |
| 1362 | continue; |
| 1363 | } |
| 1364 | |
| 1365 | ncnn::Mat tmp; |
| 1366 | ncnn::cast_float32_to_float16(weights[j], tmp, opt); |
| 1367 | ncnn::cast_float16_to_float32(tmp, weights_fp16[j], opt); |
| 1368 | } |
| 1369 | epsilon_fp16 = epsilon * 100; // 0.1 |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | weights_fp16 = weights; |
| 1374 | epsilon_fp16 = epsilon; |
| 1375 | } |
| 1376 | |
| 1377 | if (opt.use_fp16_arithmetic) |
| 1378 | { |
| 1379 | epsilon_fp16 = epsilon * 1000; // 1.0 |
| 1380 | } |
| 1381 | |
| 1382 | ncnn::Mat top_shape; |
| 1383 | int ret = test_layer(ncnn::layer_to_index(layer_type), pd, weights_fp16, opt, a_fp16, top_shape, epsilon_fp16, func, flag); |
| 1384 | if (ret != 0) |
| 1385 | { |
| 1386 | fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_fp16_arithmetic=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d use_sgemm_convolution=%d use_winograd_convolution=%d\n", layer_type, opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_fp16_arithmetic, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage, opt.use_sgemm_convolution, opt.use_winograd_convolution); |
| 1387 | return ret; |
| 1388 | } |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | int test_layer(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const std::vector<ncnn::Mat>& a, int top_blob_count, float epsilon, void (*func)(ncnn::Layer*), int flag) |
| 1394 | { |
| 1395 | // pack fp16p fp16s fp16a bf16s shader8 image |
| 1396 | const int options[][7] = { |
| 1397 | {0, 0, 0, 0, 0, 0, 0}, |
| 1398 | {0, 0, 1, 0, 0, 0, 0}, |
| 1399 | {0, 0, 1, 1, 1, 0, 0}, |
| 1400 | {1, 0, 0, 0, 0, 0, 0}, |
| 1401 | {1, 1, 0, 0, 1, 0, 0}, |
| 1402 | {1, 0, 1, 0, 0, 1, 0}, |
| 1403 | {1, 1, 1, 1, 0, 0, 0}, |
| 1404 | {1, 1, 1, 1, 1, 1, 1}, |
| 1405 | }; |
| 1406 | |
| 1407 | const int opt_count = sizeof(options) / sizeof(options[0]); |
| 1408 | |
| 1409 | for (int i = 0; i < opt_count; i++) |
| 1410 | { |
| 1411 | ncnn::Option opt; |
| 1412 | opt.num_threads = 1; |
| 1413 | opt.use_packing_layout = options[i][0]; |
| 1414 | opt.use_fp16_packed = options[i][1]; |
| 1415 | opt.use_fp16_storage = options[i][2]; |
| 1416 | opt.use_fp16_arithmetic = options[i][3]; |
| 1417 | opt.use_bf16_storage = options[i][4]; |
| 1418 | opt.use_shader_pack8 = options[i][5]; |
| 1419 | opt.use_image_storage = options[i][6]; |
| 1420 | |
| 1421 | int ret = test_layer_opt(layer_type, pd, weights, opt, a, top_blob_count, epsilon, func, flag); |
| 1422 | if (ret != 0) |
| 1423 | return ret; |
| 1424 | } |
| 1425 | |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | int test_layer(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Mat& a, float epsilon, void (*func)(ncnn::Layer*), int flag) |
| 1430 | { |
| 1431 | // pack fp16p fp16s fp16a bf16s shader8 image |
| 1432 | const int options[][7] = { |
| 1433 | {0, 0, 0, 0, 0, 0, 0}, |
| 1434 | {0, 0, 1, 0, 0, 0, 0}, |
| 1435 | {0, 0, 1, 1, 1, 0, 0}, |
| 1436 | {1, 0, 0, 0, 0, 0, 0}, |
| 1437 | {1, 1, 0, 0, 1, 0, 0}, |
| 1438 | {1, 0, 1, 0, 0, 1, 0}, |
| 1439 | {1, 1, 1, 1, 0, 0, 0}, |
| 1440 | {1, 1, 1, 1, 1, 1, 1}, |
| 1441 | }; |
| 1442 | |
| 1443 | const int opt_count = sizeof(options) / sizeof(options[0]); |
| 1444 | |
| 1445 | for (int i = 0; i < opt_count; i++) |
| 1446 | { |
| 1447 | ncnn::Option opt; |
| 1448 | opt.num_threads = 1; |
| 1449 | opt.use_packing_layout = options[i][0]; |
| 1450 | opt.use_fp16_packed = options[i][1]; |
| 1451 | opt.use_fp16_storage = options[i][2]; |
| 1452 | opt.use_fp16_arithmetic = options[i][3]; |
| 1453 | opt.use_bf16_storage = options[i][4]; |
| 1454 | opt.use_shader_pack8 = options[i][5]; |
| 1455 | opt.use_image_storage = options[i][6]; |
| 1456 | |
| 1457 | int ret = test_layer_opt(layer_type, pd, weights, opt, a, epsilon, func, flag); |
| 1458 | if (ret != 0) |
| 1459 | return ret; |
| 1460 | } |
| 1461 | |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | class TestOOMAllocator : public ncnn::UnlockedPoolAllocator |
| 1466 | { |
| 1467 | public: |
| 1468 | TestOOMAllocator(); |
| 1469 | virtual void* fastMalloc(size_t size); |
| 1470 | virtual void fastFree(void* ptr); |
| 1471 | |
| 1472 | ncnn::Mutex lock; |
| 1473 | int counter; |
| 1474 | int failid; |
| 1475 | }; |
| 1476 | |
| 1477 | TestOOMAllocator::TestOOMAllocator() |
| 1478 | { |
| 1479 | counter = 0; |
| 1480 | failid = INT_MAX; |
| 1481 | } |
| 1482 | |
| 1483 | void* TestOOMAllocator::fastMalloc(size_t size) |
| 1484 | { |
| 1485 | lock.lock(); |
| 1486 | |
| 1487 | void* ptr; |
| 1488 | if (counter == failid) |
| 1489 | { |
| 1490 | ptr = 0; |
| 1491 | } |
| 1492 | else |
| 1493 | { |
| 1494 | ptr = ncnn::UnlockedPoolAllocator::fastMalloc(size); |
| 1495 | } |
| 1496 | counter++; |
| 1497 | |
| 1498 | lock.unlock(); |
| 1499 | |
| 1500 | return ptr; |
| 1501 | } |
| 1502 | |
| 1503 | void TestOOMAllocator::fastFree(void* ptr) |
| 1504 | { |
| 1505 | lock.lock(); |
| 1506 | |
| 1507 | ncnn::UnlockedPoolAllocator::fastFree(ptr); |
| 1508 | |
| 1509 | lock.unlock(); |
| 1510 | } |
| 1511 | |
| 1512 | int test_layer_oom_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, int flag) |
| 1513 | { |
| 1514 | int typeindex = ncnn::layer_to_index(layer_type); |
| 1515 | if (typeindex == -1) |
| 1516 | return -1; |
| 1517 | |
| 1518 | ncnn::Layer* op = ncnn::create_layer_cpu(typeindex); |
| 1519 | |
| 1520 | if (!op->support_packing && _opt.use_packing_layout) |
| 1521 | { |
| 1522 | delete op; |
| 1523 | return 233; |
| 1524 | } |
| 1525 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 1526 | { |
| 1527 | delete op; |
| 1528 | return 233; |
| 1529 | } |
| 1530 | |
| 1531 | op->load_param(pd); |
| 1532 | |
| 1533 | if (op->one_blob_only && a.size() != 1) |
| 1534 | { |
| 1535 | fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n"); |
| 1536 | delete op; |
| 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 1541 | |
| 1542 | op->load_model(mb); |
| 1543 | |
| 1544 | ncnn::Option opt = _opt; |
| 1545 | opt.num_threads = 1; |
| 1546 | opt.use_vulkan_compute = false; |
| 1547 | |
| 1548 | op->create_pipeline(opt); |
| 1549 | |
| 1550 | if (!op->support_packing && _opt.use_packing_layout) |
| 1551 | { |
| 1552 | op->destroy_pipeline(opt); |
| 1553 | delete op; |
| 1554 | return 233; |
| 1555 | } |
| 1556 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 1557 | { |
| 1558 | op->destroy_pipeline(opt); |
| 1559 | delete op; |
| 1560 | return 233; |
| 1561 | } |
| 1562 | |
| 1563 | std::vector<ncnn::Mat> a4(a.size()); |
| 1564 | |
| 1565 | for (size_t i = 0; i < a4.size(); i++) |
| 1566 | { |
| 1567 | convert_to_optimal_layout(a[i], a4[i], opt, op, flag); |
| 1568 | } |
| 1569 | |
| 1570 | TestOOMAllocator test_oom_allocator; |
| 1571 | opt.blob_allocator = &test_oom_allocator; |
| 1572 | opt.workspace_allocator = &test_oom_allocator; |
| 1573 | |
| 1574 | std::vector<ncnn::Mat> c; |
| 1575 | c.resize(top_blob_count); |
| 1576 | |
| 1577 | if (op->support_inplace) |
| 1578 | { |
| 1579 | for (size_t i = 0; i < a4.size(); i++) |
| 1580 | { |
| 1581 | c[i] = a4[i].clone(); |
| 1582 | } |
| 1583 | |
| 1584 | op->forward_inplace(c, opt); |
| 1585 | } |
| 1586 | else |
| 1587 | { |
| 1588 | op->forward(a4, c, opt); |
| 1589 | } |
| 1590 | |
| 1591 | for (int i = 0; i < top_blob_count; i++) |
| 1592 | { |
| 1593 | c[i].release(); |
| 1594 | } |
| 1595 | |
| 1596 | const int alloc_count = test_oom_allocator.counter; |
| 1597 | for (int i = 0; i < alloc_count; i++) |
| 1598 | { |
| 1599 | test_oom_allocator.counter = 0; |
| 1600 | test_oom_allocator.failid = i; |
| 1601 | |
| 1602 | int ret = 0; |
| 1603 | if (op->support_inplace) |
| 1604 | { |
| 1605 | for (size_t i = 0; i < a4.size(); i++) |
| 1606 | { |
| 1607 | c[i] = a4[i].clone(); |
| 1608 | } |
| 1609 | |
| 1610 | ret = op->forward_inplace(c, opt); |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | ret = op->forward(a4, c, opt); |
| 1615 | } |
| 1616 | |
| 1617 | for (int i = 0; i < top_blob_count; i++) |
| 1618 | { |
| 1619 | c[i].release(); |
| 1620 | } |
| 1621 | |
| 1622 | if (ret != -100) |
| 1623 | { |
| 1624 | fprintf(stderr, "oom not catched %d/%d\n", i, alloc_count); |
| 1625 | |
| 1626 | op->destroy_pipeline(opt); |
| 1627 | |
| 1628 | delete op; |
| 1629 | |
| 1630 | return -1; |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | op->destroy_pipeline(opt); |
| 1635 | |
| 1636 | delete op; |
| 1637 | |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
| 1641 | int test_layer_oom_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, int flag) |
| 1642 | { |
| 1643 | int typeindex = ncnn::layer_to_index(layer_type); |
| 1644 | if (typeindex == -1) |
| 1645 | return -1; |
| 1646 | |
| 1647 | ncnn::Layer* op = ncnn::create_layer_cpu(typeindex); |
| 1648 | |
| 1649 | if (!op->support_packing && _opt.use_packing_layout) |
| 1650 | { |
| 1651 | delete op; |
| 1652 | return 233; |
| 1653 | } |
| 1654 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 1655 | { |
| 1656 | delete op; |
| 1657 | return 233; |
| 1658 | } |
| 1659 | |
| 1660 | op->load_param(pd); |
| 1661 | |
| 1662 | ncnn::ModelBinFromMatArray mb(weights.data()); |
| 1663 | |
| 1664 | op->load_model(mb); |
| 1665 | |
| 1666 | ncnn::Option opt = _opt; |
| 1667 | opt.num_threads = 1; |
| 1668 | opt.use_vulkan_compute = false; |
| 1669 | |
| 1670 | op->create_pipeline(opt); |
| 1671 | |
| 1672 | if (!op->support_packing && _opt.use_packing_layout) |
| 1673 | { |
| 1674 | op->destroy_pipeline(opt); |
| 1675 | delete op; |
| 1676 | return 233; |
| 1677 | } |
| 1678 | if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic)) |
| 1679 | { |
| 1680 | op->destroy_pipeline(opt); |
| 1681 | delete op; |
| 1682 | return 233; |
| 1683 | } |
| 1684 | |
| 1685 | ncnn::Mat a4; |
| 1686 | convert_to_optimal_layout(a, a4, opt, op, flag); |
| 1687 | |
| 1688 | TestOOMAllocator test_oom_allocator; |
| 1689 | opt.blob_allocator = &test_oom_allocator; |
| 1690 | opt.workspace_allocator = &test_oom_allocator; |
| 1691 | |
| 1692 | ncnn::Mat c; |
| 1693 | |
| 1694 | if (op->support_inplace) |
| 1695 | { |
| 1696 | c = a4.clone(); |
| 1697 | op->forward_inplace(c, opt); |
| 1698 | } |
| 1699 | else |
| 1700 | { |
| 1701 | op->forward(a4, c, opt); |
| 1702 | } |
| 1703 | |
| 1704 | c.release(); |
| 1705 | |
| 1706 | const int alloc_count = test_oom_allocator.counter; |
| 1707 | for (int i = 0; i < alloc_count; i++) |
| 1708 | { |
| 1709 | test_oom_allocator.counter = 0; |
| 1710 | test_oom_allocator.failid = i; |
| 1711 | |
| 1712 | int ret = 0; |
| 1713 | if (op->support_inplace) |
| 1714 | { |
| 1715 | c = a4.clone(); |
| 1716 | ret = op->forward_inplace(c, opt); |
| 1717 | } |
| 1718 | else |
| 1719 | { |
| 1720 | ret = op->forward(a4, c, opt); |
| 1721 | } |
| 1722 | |
| 1723 | c.release(); |
| 1724 | |
| 1725 | if (ret != -100) |
| 1726 | { |
| 1727 | fprintf(stderr, "oom not catched %d/%d\n", i, alloc_count); |
| 1728 | |
| 1729 | op->destroy_pipeline(opt); |
| 1730 | |
| 1731 | delete op; |
| 1732 | |
| 1733 | return -1; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | op->destroy_pipeline(opt); |
| 1738 | |
| 1739 | delete op; |
| 1740 | |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | int test_layer_oom(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const std::vector<ncnn::Mat>& a, int top_blob_count, int flag) |
| 1745 | { |
| 1746 | // pack fp16p fp16s fp16a bf16s shader8 image |
| 1747 | const int options[][7] = { |
| 1748 | {0, 0, 0, 0, 0, 0, 0}, |
| 1749 | {0, 0, 1, 0, 0, 0, 0}, |
| 1750 | {0, 0, 1, 1, 1, 0, 0}, |
| 1751 | {1, 0, 0, 0, 0, 0, 0}, |
| 1752 | {1, 1, 0, 0, 1, 0, 0}, |
| 1753 | {1, 0, 1, 0, 0, 1, 0}, |
| 1754 | {1, 1, 1, 1, 0, 0, 0}, |
| 1755 | {1, 1, 1, 1, 1, 1, 1}, |
| 1756 | }; |
| 1757 | |
| 1758 | const int opt_count = sizeof(options) / sizeof(options[0]); |
| 1759 | |
| 1760 | for (int i = 0; i < opt_count; i++) |
| 1761 | { |
| 1762 | ncnn::Option opt; |
| 1763 | opt.num_threads = 1; |
| 1764 | opt.use_packing_layout = options[i][0]; |
| 1765 | opt.use_fp16_packed = options[i][1]; |
| 1766 | opt.use_fp16_storage = options[i][2]; |
| 1767 | opt.use_fp16_arithmetic = options[i][3]; |
| 1768 | opt.use_bf16_storage = options[i][4]; |
| 1769 | opt.use_shader_pack8 = options[i][5]; |
| 1770 | opt.use_image_storage = options[i][6]; |
| 1771 | |
| 1772 | int ret = test_layer_oom_opt(layer_type, pd, weights, opt, a, top_blob_count, flag); |
| 1773 | if (ret != 233 && ret != 0) |
| 1774 | return ret; |
| 1775 | } |
| 1776 | |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | int test_layer_oom(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Mat& a, int flag) |
| 1781 | { |
| 1782 | // pack fp16p fp16s fp16a bf16s shader8 image |
| 1783 | const int options[][7] = { |
| 1784 | {0, 0, 0, 0, 0, 0, 0}, |
| 1785 | {0, 0, 1, 0, 0, 0, 0}, |
| 1786 | {0, 0, 1, 1, 1, 0, 0}, |
| 1787 | {1, 0, 0, 0, 0, 0, 0}, |
| 1788 | {1, 1, 0, 0, 1, 0, 0}, |
| 1789 | {1, 0, 1, 0, 0, 1, 0}, |
| 1790 | {1, 1, 1, 1, 0, 0, 0}, |
| 1791 | {1, 1, 1, 1, 1, 1, 1}, |
| 1792 | }; |
| 1793 | |
| 1794 | const int opt_count = sizeof(options) / sizeof(options[0]); |
| 1795 | |
| 1796 | for (int i = 0; i < opt_count; i++) |
| 1797 | { |
| 1798 | ncnn::Option opt; |
| 1799 | opt.num_threads = 1; |
| 1800 | opt.use_packing_layout = options[i][0]; |
| 1801 | opt.use_fp16_packed = options[i][1]; |
| 1802 | opt.use_fp16_storage = options[i][2]; |
| 1803 | opt.use_fp16_arithmetic = options[i][3]; |
| 1804 | opt.use_bf16_storage = options[i][4]; |
| 1805 | opt.use_shader_pack8 = options[i][5]; |
| 1806 | opt.use_image_storage = options[i][6]; |
| 1807 | |
| 1808 | int ret = test_layer_oom_opt(layer_type, pd, weights, opt, a, flag); |
| 1809 | if (ret != 233 && ret != 0) |
| 1810 | return ret; |
| 1811 | } |
| 1812 | |
| 1813 | return 0; |
| 1814 | } |