Elffuss BraidHub

kikocis/ncnn public

3a0df369be07 · 84.8 KiBRawPermalink
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 "net.h"
16
17#include "cpu.h"
18#include "datareader.h"
19#include "layer_type.h"
20#include "modelbin.h"
21#include "paramdict.h"
22
23#include <stdarg.h>
24#include <stdint.h>
25#include <string.h>
26
27#if NCNN_BENCHMARK
28#include "benchmark.h"
29#endif // NCNN_BENCHMARK
30
31#if NCNN_VULKAN
32#include "command.h"
33#include "pipelinecache.h"
34#endif // NCNN_VULKAN
35
36namespace ncnn {
37
38class NetPrivate
39{
40public:
41 NetPrivate(Option& _opt);
42
43 Option& opt;
44
45#if NCNN_VULKAN
46
47 int upload_model();
48
49#endif // NCNN_VULKAN
50
51 friend class Extractor;
52 int forward_layer(int layer_index, std::vector<Mat>& blob_mats, const Option& opt) const;
53
54#if NCNN_VULKAN
55 int forward_layer(int layer_index, std::vector<Mat>& blob_mats, std::vector<VkMat>& blob_mats_gpu, VkCompute& cmd, const Option& opt) const;
56 int forward_layer(int layer_index, std::vector<Mat>& blob_mats, std::vector<VkMat>& blob_mats_gpu, std::vector<VkImageMat>& blob_mats_gpu_image, VkCompute& cmd, const Option& opt) const;
57#endif // NCNN_VULKAN
58
59 int convert_layout(Mat& bottom_blob, const Layer* layer, const Option& opt) const;
60
61 int do_forward_layer(const Layer* layer, std::vector<Mat>& blob_mats, const Option& opt) const;
62#if NCNN_VULKAN
63 int do_forward_layer(const Layer* layer, std::vector<VkMat>& blob_mats_gpu, VkCompute& cmd, const Option& opt) const;
64 int do_forward_layer(const Layer* layer, std::vector<VkImageMat>& blob_mats_gpu_image, VkCompute& cmd, const Option& opt) const;
65#endif // NCNN_VULKAN
66
67 void update_input_output_indexes();
68#if NCNN_STRING
69 void update_input_output_names();
70#endif // NCNN_STRING
71
72 std::vector<Blob> blobs;
73 std::vector<Layer*> layers;
74
75 std::vector<int> input_blob_indexes;
76 std::vector<int> output_blob_indexes;
77#if NCNN_STRING
78 std::vector<const char*> input_blob_names;
79 std::vector<const char*> output_blob_names;
80#endif // NCNN_STRING
81
82 std::vector<custom_layer_registry_entry> custom_layer_registry;
83 std::vector<overwrite_builtin_layer_registry_entry> overwrite_builtin_layer_registry;
84
85 PoolAllocator* local_blob_allocator;
86 PoolAllocator* local_workspace_allocator;
87
88#if NCNN_VULKAN
89 const VulkanDevice* vkdev;
90
91 VkAllocator* weight_vkallocator;
92 VkAllocator* weight_staging_vkallocator;
93
94 PipelineCache* pipeline_cache;
95#endif // NCNN_VULKAN
96};
97
98NetPrivate::NetPrivate(Option& _opt)
99 : opt(_opt)
100{
101 local_blob_allocator = 0;
102 local_workspace_allocator = 0;
103
104#if NCNN_VULKAN
105 vkdev = 0;
106 weight_vkallocator = 0;
107 weight_staging_vkallocator = 0;
108 pipeline_cache = 0;
109#endif // NCNN_VULKAN
110}
111
112static Option get_masked_option(const Option& opt, int featmask)
113{
114 // mask option usage as layer specific featmask
115 Option opt1 = opt;
116 opt1.use_fp16_arithmetic = opt1.use_fp16_arithmetic && !(featmask & (1 << 0));
117 opt1.use_fp16_storage = opt1.use_fp16_storage && !(featmask & (1 << 1));
118 opt1.use_fp16_packed = opt1.use_fp16_packed && !(featmask & (1 << 1));
119 opt1.use_bf16_storage = opt1.use_bf16_storage && !(featmask & (1 << 2));
120 opt1.use_int8_packed = opt1.use_int8_packed && !(featmask & (1 << 3));
121 opt1.use_int8_storage = opt1.use_int8_storage && !(featmask & (1 << 3));
122 opt1.use_int8_arithmetic = opt1.use_int8_arithmetic && !(featmask & (1 << 3));
123 opt1.use_vulkan_compute = opt1.use_vulkan_compute && !(featmask & (1 << 4));
124 opt1.use_image_storage = opt1.use_image_storage && !(featmask & (1 << 4));
125 opt1.use_tensor_storage = opt1.use_tensor_storage && !(featmask & (1 << 4));
126 opt1.use_sgemm_convolution = opt1.use_sgemm_convolution && !(featmask & (1 << 5));
127 opt1.use_winograd_convolution = opt1.use_winograd_convolution && !(featmask & (1 << 6));
128
129 if (featmask & (1 << 7))
130 opt1.num_threads = 1;
131
132 return opt1;
133}
134
135#if NCNN_VULKAN
136int NetPrivate::upload_model()
137{
138 ncnn::VkTransfer cmd(vkdev);
139
140 // create gpu device allocator if null
141 if (!weight_vkallocator)
142 {
143 weight_vkallocator = new VkWeightAllocator(vkdev);
144 }
145 if (!weight_staging_vkallocator)
146 {
147 weight_staging_vkallocator = new VkWeightStagingAllocator(vkdev);
148 }
149
150 Option opt_upload = opt;
151 opt_upload.blob_allocator = 0;
152 opt_upload.workspace_allocator = 0;
153 opt_upload.blob_vkallocator = weight_vkallocator;
154 opt_upload.workspace_vkallocator = weight_vkallocator;
155 opt_upload.staging_vkallocator = weight_staging_vkallocator;
156
157 for (size_t i = 0; i < layers.size(); i++)
158 {
159 if (layers[i]->support_vulkan)
160 {
161 int uret = layers[i]->upload_model(cmd, get_masked_option(opt_upload, layers[i]->featmask));
162 if (uret != 0)
163 {
164 NCNN_LOGE("layer upload_model %d failed", (int)i);
165 return -1;
166 }
167 }
168 }
169
170 return cmd.submit_and_wait();
171}
172#endif // NCNN_VULKAN
173
174int NetPrivate::forward_layer(int layer_index, std::vector<Mat>& blob_mats, const Option& opt) const
175{
176 const Layer* layer = layers[layer_index];
177
178 // NCNN_LOGE("forward_layer %d %s", layer_index, layer->name.c_str());
179
180 // load bottom blobs
181 for (size_t i = 0; i < layer->bottoms.size(); i++)
182 {
183 int bottom_blob_index = layer->bottoms[i];
184
185 if (blob_mats[bottom_blob_index].dims == 0)
186 {
187 int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, opt);
188 if (ret != 0)
189 return ret;
190 }
191 }
192
193#if NCNN_BENCHMARK
194 double start = get_current_time();
195 Mat bottom_blob;
196 if (layer->one_blob_only)
197 {
198 int bottom_blob_index = layer->bottoms[0];
199 bottom_blob.dims = blob_mats[bottom_blob_index].dims;
200 bottom_blob.w = blob_mats[bottom_blob_index].w;
201 bottom_blob.h = blob_mats[bottom_blob_index].h;
202 bottom_blob.d = blob_mats[bottom_blob_index].d;
203 bottom_blob.c = blob_mats[bottom_blob_index].c;
204 bottom_blob.elempack = blob_mats[bottom_blob_index].elempack;
205 bottom_blob.elemsize = blob_mats[bottom_blob_index].elemsize;
206 }
207#endif
208 int ret = 0;
209 if (layer->featmask)
210 {
211 ret = do_forward_layer(layer, blob_mats, get_masked_option(opt, layer->featmask));
212 }
213 else
214 {
215 ret = do_forward_layer(layer, blob_mats, opt);
216 }
217#if NCNN_BENCHMARK
218 double end = get_current_time();
219 if (layer->one_blob_only)
220 {
221 int top_blob_index = layer->tops[0];
222 benchmark(layer, bottom_blob, blob_mats[top_blob_index], start, end);
223 }
224 else
225 {
226 benchmark(layer, start, end);
227 }
228#endif
229 if (ret != 0)
230 return ret;
231
232 // NCNN_LOGE("forward_layer %d %s done", layer_index, layer->name.c_str());
233 // const Mat& blob = blob_mats[layer->tops[0]];
234 // NCNN_LOGE("[%-2d %-16s %-16s] %d blobs count = %-3d size = %-3d x %-3d", layer_index, layer->type.c_str(), layer->name.c_str(), layer->tops[0], blob.c, blob.h, blob.w);
235
236 return 0;
237}
238
239#if NCNN_VULKAN
240int NetPrivate::forward_layer(int layer_index, std::vector<Mat>& blob_mats, std::vector<VkMat>& blob_mats_gpu, VkCompute& cmd, const Option& opt) const
241{
242 const Layer* layer = layers[layer_index];
243
244 // NCNN_LOGE("forward_layer %d %d %s", layer->support_vulkan, layer_index, layer->name.c_str());
245
246 bool cmd_submit_and_wait = false;
247
248 // load bottom blobs
249 for (size_t i = 0; i < layer->bottoms.size(); i++)
250 {
251 int bottom_blob_index = layer->bottoms[i];
252
253 if (blob_mats_gpu[bottom_blob_index].dims == 0 && blob_mats[bottom_blob_index].dims == 0)
254 {
255 int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, blob_mats_gpu, cmd, opt);
256 if (ret != 0)
257 return ret;
258 }
259
260 if (layer->support_vulkan)
261 {
262 if (blob_mats_gpu[bottom_blob_index].dims == 0)
263 {
264 // host to buffer
265 cmd.record_upload(blob_mats[bottom_blob_index], blob_mats_gpu[bottom_blob_index], opt);
266
267 if (opt.lightmode)
268 {
269 // delete after taken in light mode
270 blob_mats[bottom_blob_index].release();
271 }
272 }
273 }
274 else
275 {
276 if (blob_mats[bottom_blob_index].dims == 0)
277 {
278 Option opt_download = opt;
279 opt_download.use_packing_layout = layer->support_packing;
280
281 // buffer to host
282 cmd.record_download(blob_mats_gpu[bottom_blob_index], blob_mats[bottom_blob_index], opt_download);
283
284 if (opt.lightmode)
285 {
286 // delete after taken in light mode
287 blob_mats_gpu[bottom_blob_index].release();
288 }
289
290 cmd_submit_and_wait = true;
291 }
292 }
293 }
294
295 int ret;
296 if (cmd_submit_and_wait)
297 {
298 ret = cmd.submit_and_wait();
299
300#if NCNN_BENCHMARK
301 std::vector<uint64_t> results(layer_index * 2);
302 cmd.get_query_pool_results(0, layer_index * 2, results);
303 for (int i = 0; i < layer_index; i++)
304 {
305 uint64_t start = results[i * 2];
306 uint64_t end = results[i * 2 + 1];
307 if (start == 0 || end == 0)
308 continue;
309
310 double duration_us = (end - start) * vkdev->info.timestamp_period() / 1000;
311 NCNN_LOGE("%-24s %-30s %8.2lfus |", layers[i]->type.c_str(), layers[i]->name.c_str(), duration_us);
312 }
313#endif // NCNN_BENCHMARK
314
315 cmd.reset();
316 if (ret != 0)
317 return ret;
318 }
319
320 if (layer->support_vulkan)
321 {
322#if NCNN_BENCHMARK
323 cmd.record_write_timestamp(layer_index * 2);
324#endif
325 if (layer->featmask)
326 {
327 ret = do_forward_layer(layer, blob_mats_gpu, cmd, get_masked_option(opt, layer->featmask));
328 }
329 else
330 {
331 ret = do_forward_layer(layer, blob_mats_gpu, cmd, opt);
332 }
333#if NCNN_BENCHMARK
334 cmd.record_write_timestamp(layer_index * 2 + 1);
335#endif
336 }
337 else
338 {
339#if NCNN_BENCHMARK
340 double start = get_current_time();
341 Mat bottom_blob;
342 if (layer->one_blob_only)
343 {
344 int bottom_blob_index = layer->bottoms[0];
345 bottom_blob = blob_mats[bottom_blob_index].shape();
346 }
347#endif
348 if (layer->featmask)
349 {
350 ret = do_forward_layer(layer, blob_mats, get_masked_option(opt, layer->featmask));
351 }
352 else
353 {
354 ret = do_forward_layer(layer, blob_mats, opt);
355 }
356#if NCNN_BENCHMARK
357 double end = get_current_time();
358 if (layer->one_blob_only)
359 {
360 int top_blob_index = layer->tops[0];
361 benchmark(layer, bottom_blob, blob_mats[top_blob_index], start, end);
362 }
363 else
364 {
365 benchmark(layer, start, end);
366 }
367#endif
368 }
369 if (ret != 0)
370 return ret;
371
372 // NCNN_LOGE("forward_layer %d %d %s done", layer->support_vulkan, layer_index, layer->name.c_str());
373
374 return 0;
375}
376
377int NetPrivate::forward_layer(int layer_index, std::vector<Mat>& blob_mats, std::vector<VkMat>& blob_mats_gpu, std::vector<VkImageMat>& blob_mats_gpu_image, VkCompute& cmd, const Option& opt) const
378{
379 const Layer* layer = layers[layer_index];
380
381 // NCNN_LOGE("forward_layer %d %d %s", layer->support_vulkan, layer_index, layer->name.c_str());
382
383 bool cmd_submit_and_wait = false;
384 bool image_allocation_failed = false;
385
386IMAGE_ALLOCATION_FAILED:
387
388 if (image_allocation_failed)
389 {
390#if NCNN_STRING
391 NCNN_LOGE("forward_layer %d %s image allocation failed, fallback to cpu", layer_index, layer->name.c_str());
392#else
393 NCNN_LOGE("forward_layer %d image allocation failed, fallback to cpu", layer_index);
394#endif
395 }
396
397 // load bottom blobs
398 for (size_t i = 0; i < layer->bottoms.size(); i++)
399 {
400 int bottom_blob_index = layer->bottoms[i];
401
402 if (blob_mats_gpu_image[bottom_blob_index].dims == 0 && blob_mats_gpu[bottom_blob_index].dims == 0 && blob_mats[bottom_blob_index].dims == 0)
403 {
404 int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, blob_mats_gpu, blob_mats_gpu_image, cmd, opt);
405 if (ret != 0)
406 return ret;
407 }
408
409 if (layer->support_vulkan && !image_allocation_failed)
410 {
411 if (layer->support_image_storage)
412 {
413 if (blob_mats_gpu_image[bottom_blob_index].dims == 0)
414 {
415 if (blob_mats_gpu[bottom_blob_index].dims == 0)
416 {
417 // host to image
418 cmd.record_upload(blob_mats[bottom_blob_index], blob_mats_gpu_image[bottom_blob_index], opt);
419
420 if (blob_mats_gpu_image[bottom_blob_index].empty())
421 {
422 image_allocation_failed = true;
423 goto IMAGE_ALLOCATION_FAILED;
424 }
425
426 if (opt.lightmode)
427 {
428 // delete after taken in light mode
429 blob_mats[bottom_blob_index].release();
430 }
431 }
432 else
433 {
434 // buffer to image
435 cmd.record_buffer_to_image(blob_mats_gpu[bottom_blob_index], blob_mats_gpu_image[bottom_blob_index], opt);
436
437 if (blob_mats_gpu_image[bottom_blob_index].empty())
438 {
439 image_allocation_failed = true;
440 goto IMAGE_ALLOCATION_FAILED;
441 }
442
443 if (opt.lightmode)
444 {
445 // delete after taken in light mode
446 blob_mats_gpu[bottom_blob_index].release();
447 }
448 }
449 }
450 }
451 else
452 {
453 if (blob_mats_gpu[bottom_blob_index].dims == 0)
454 {
455 if (blob_mats_gpu_image[bottom_blob_index].dims == 0)
456 {
457 // host to buffer
458 cmd.record_upload(blob_mats[bottom_blob_index], blob_mats_gpu[bottom_blob_index], opt);
459
460 if (opt.lightmode)
461 {
462 // delete after taken in light mode
463 blob_mats[bottom_blob_index].release();
464 }
465 }
466 else
467 {
468 // image to buffer
469 cmd.record_image_to_buffer(blob_mats_gpu_image[bottom_blob_index], blob_mats_gpu[bottom_blob_index], opt);
470
471 if (opt.lightmode)
472 {
473 // delete after taken in light mode
474 blob_mats_gpu_image[bottom_blob_index].release();
475 }
476 }
477 }
478 }
479 }
480 else
481 {
482 if (blob_mats[bottom_blob_index].dims == 0)
483 {
484 if (blob_mats_gpu_image[bottom_blob_index].dims == 0)
485 {
486 // buffer to host
487 cmd.record_download(blob_mats_gpu[bottom_blob_index], blob_mats[bottom_blob_index], opt);
488
489 if (opt.lightmode)
490 {
491 // delete after taken in light mode
492 blob_mats_gpu[bottom_blob_index].release();
493 }
494
495 cmd_submit_and_wait = true;
496 }
497 else
498 {
499 // image to host
500 cmd.record_download(blob_mats_gpu_image[bottom_blob_index], blob_mats[bottom_blob_index], opt);
501
502 if (opt.lightmode)
503 {
504 // delete after taken in light mode
505 blob_mats_gpu_image[bottom_blob_index].release();
506 }
507
508 cmd_submit_and_wait = true;
509 }
510 }
511 }
512 }
513
514 int ret;
515 if (cmd_submit_and_wait)
516 {
517 ret = cmd.submit_and_wait();
518
519#if NCNN_BENCHMARK
520 std::vector<uint64_t> results(layer_index * 2);
521 cmd.get_query_pool_results(0, layer_index * 2, results);
522 for (int i = 0; i < layer_index; i++)
523 {
524 uint64_t start = results[i * 2];
525 uint64_t end = results[i * 2 + 1];
526 if (start == 0 || end == 0)
527 continue;
528
529 double duration_us = (end - start) * vkdev->info.timestamp_period() / 1000;
530 NCNN_LOGE("%-24s %-30s %8.2lfus |", layers[i]->type.c_str(), layers[i]->name.c_str(), duration_us);
531 }
532#endif // NCNN_BENCHMARK
533
534 cmd.reset();
535
536 if (ret != 0)
537 return ret;
538 }
539
540 if (layer->support_vulkan && !image_allocation_failed)
541 {
542#if NCNN_BENCHMARK
543 cmd.record_write_timestamp(layer_index * 2);
544#endif
545 if (layer->support_image_storage)
546 {
547 if (layer->featmask)
548 {
549 ret = do_forward_layer(layer, blob_mats_gpu_image, cmd, get_masked_option(opt, layer->featmask));
550 }
551 else
552 {
553 ret = do_forward_layer(layer, blob_mats_gpu_image, cmd, opt);
554 }
555 if (ret == -100)
556 {
557 image_allocation_failed = true;
558 goto IMAGE_ALLOCATION_FAILED;
559 }
560 }
561 else
562 {
563 if (layer->featmask)
564 {
565 ret = do_forward_layer(layer, blob_mats_gpu, cmd, get_masked_option(opt, layer->featmask));
566 }
567 else
568 {
569 ret = do_forward_layer(layer, blob_mats_gpu, cmd, opt);
570 }
571 }
572#if NCNN_BENCHMARK
573 cmd.record_write_timestamp(layer_index * 2 + 1);
574#endif
575 }
576 else
577 {
578#if NCNN_BENCHMARK
579 double start = get_current_time();
580 Mat bottom_blob;
581 if (layer->one_blob_only)
582 {
583 int bottom_blob_index = layer->bottoms[0];
584 bottom_blob = blob_mats[bottom_blob_index].shape();
585 }
586#endif
587 if (layer->featmask)
588 {
589 ret = do_forward_layer(layer, blob_mats, get_masked_option(opt, layer->featmask));
590 }
591 else
592 {
593 ret = do_forward_layer(layer, blob_mats, opt);
594 }
595#if NCNN_BENCHMARK
596 double end = get_current_time();
597 if (layer->one_blob_only)
598 {
599 int top_blob_index = layer->tops[0];
600 benchmark(layer, bottom_blob, blob_mats[top_blob_index], start, end);
601 }
602 else
603 {
604 benchmark(layer, start, end);
605 }
606#endif
607 }
608 if (ret != 0)
609 return ret;
610
611 // NCNN_LOGE("forward_layer %d %d %s done", layer->support_vulkan, layer_index, layer->name.c_str());
612
613 return 0;
614}
615#endif // NCNN_VULKAN
616
617int NetPrivate::convert_layout(Mat& bottom_blob, const Layer* layer, const Option& opt) const
618{
619 if (bottom_blob.elembits() == 32)
620 {
621 // clang-format off
622 // *INDENT-OFF*
623
624#if NCNN_VFPV4
625 if (opt.use_fp16_storage && cpu_support_arm_vfpv4() && layer->support_fp16_storage)
626 {
627 Mat bottom_blob_fp16;
628 cast_float32_to_float16(bottom_blob, bottom_blob_fp16, opt);
629 bottom_blob = bottom_blob_fp16;
630 }
631 else
632#endif // NCNN_VFPV4
633#if NCNN_RVV
634 if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && layer->support_fp16_storage)
635 {
636 Mat bottom_blob_fp16;
637 cast_float32_to_float16(bottom_blob, bottom_blob_fp16, opt);
638 bottom_blob = bottom_blob_fp16;
639 }
640 else
641#endif // NCNN_RVV
642#if NCNN_BF16
643 if (opt.use_bf16_storage && layer->support_bf16_storage)
644 {
645 Mat bottom_blob_bf16;
646 cast_float32_to_bfloat16(bottom_blob, bottom_blob_bf16, opt);
647 bottom_blob = bottom_blob_bf16;
648 }
649 else
650#endif // NCNN_BF16
651 {
652 }
653
654 // *INDENT-ON*
655 // clang-format on
656
657 if (bottom_blob.empty())
658 return -100;
659 }
660
661 int dst_elempack = 1;
662 if (opt.use_packing_layout)
663 {
664 // resolve dst_elempack
665 int dims = bottom_blob.dims;
666 int elemcount = 0;
667 if (dims == 1) elemcount = bottom_blob.elempack * bottom_blob.w;
668 if (dims == 2) elemcount = bottom_blob.elempack * bottom_blob.h;
669 if (dims == 3 || dims == 4) elemcount = bottom_blob.elempack * bottom_blob.c;
670
671 int elembits = bottom_blob.elembits();
672
673 if (layer->support_packing)
674 {
675 if (elembits == 32)
676 {
677#if NCNN_AVX512
678 if (elemcount % 16 == 0 && ncnn::cpu_support_x86_avx512())
679 dst_elempack = 16;
680 else if (elemcount % 8 == 0 && ncnn::cpu_support_x86_avx())
681 dst_elempack = 8;
682 else if (elemcount % 4 == 0)
683 dst_elempack = 4;
684#elif NCNN_AVX
685 if (elemcount % 8 == 0 && ncnn::cpu_support_x86_avx())
686 dst_elempack = 8;
687 else if (elemcount % 4 == 0)
688 dst_elempack = 4;
689#elif NCNN_RVV
690 const int packn = ncnn::cpu_riscv_vlenb() / 4;
691 if (elemcount % packn == 0)
692 dst_elempack = packn;
693#else
694 if (elemcount % 4 == 0)
695 dst_elempack = 4;
696#endif
697 }
698 if (elembits == 16)
699 {
700#if NCNN_ARM82
701 if (elemcount % 8 == 0 && ncnn::cpu_support_arm_asimdhp() && opt.use_fp16_arithmetic)
702 dst_elempack = 8;
703 else if (elemcount % 4 == 0)
704 dst_elempack = 4;
705#elif NCNN_RVV
706 const int packn = ncnn::cpu_riscv_vlenb() / 2;
707 if (elemcount % packn == 0)
708 dst_elempack = packn;
709#else
710 if (elemcount % 4 == 0)
711 dst_elempack = 4;
712#endif
713 }
714 if (elembits == 8)
715 {
716#if NCNN_RVV
717 const int packn = ncnn::cpu_riscv_vlenb() / 1;
718 if (elemcount % packn == 0)
719 dst_elempack = packn;
720#else
721 if (elemcount % 8 == 0)
722 dst_elempack = 8;
723#endif
724 }
725 }
726 }
727
728 if (bottom_blob.elempack != dst_elempack)
729 {
730 Mat bottom_blob_packed;
731 convert_packing(bottom_blob, bottom_blob_packed, dst_elempack, opt);
732 bottom_blob = bottom_blob_packed;
733
734 if (bottom_blob.empty())
735 return -100;
736 }
737
738 if (bottom_blob.elembits() == 16)
739 {
740 // clang-format off
741 // *INDENT-OFF*
742
743#if NCNN_VFPV4
744 if (opt.use_fp16_storage && cpu_support_arm_vfpv4() && !layer->support_fp16_storage)
745 {
746 Mat bottom_blob_fp32;
747 cast_float16_to_float32(bottom_blob, bottom_blob_fp32, opt);
748 bottom_blob = bottom_blob_fp32;
749 }
750 else
751#endif // NCNN_VFPV4
752#if NCNN_RVV
753 if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && !layer->support_fp16_storage)
754 {
755 Mat bottom_blob_fp32;
756 cast_float16_to_float32(bottom_blob, bottom_blob_fp32, opt);
757 bottom_blob = bottom_blob_fp32;
758 }
759 else
760#endif // NCNN_RVV
761#if NCNN_BF16
762 if (opt.use_bf16_storage && !layer->support_bf16_storage)
763 {
764 Mat bottom_blob_fp32;
765 cast_bfloat16_to_float32(bottom_blob, bottom_blob_fp32, opt);
766 bottom_blob = bottom_blob_fp32;
767 }
768 else
769#endif // NCNN_BF16
770 {
771 }
772
773 // *INDENT-ON*
774 // clang-format on
775
776 if (bottom_blob.empty())
777 return -100;
778 }
779
780 return 0;
781}
782
783int NetPrivate::do_forward_layer(const Layer* layer, std::vector<Mat>& blob_mats, const Option& opt) const
784{
785 if (layer->one_blob_only)
786 {
787 int bottom_blob_index = layer->bottoms[0];
788 int top_blob_index = layer->tops[0];
789
790 Mat& bottom_blob_ref = blob_mats[bottom_blob_index];
791 Mat bottom_blob;
792
793 if (opt.lightmode)
794 {
795 // deep copy for inplace forward if data is shared
796 if (layer->support_inplace && *bottom_blob_ref.refcount != 1)
797 {
798 bottom_blob = bottom_blob_ref.clone(opt.blob_allocator);
799 if (bottom_blob.empty())
800 return -100;
801 }
802 }
803 if (bottom_blob.dims == 0)
804 {
805 bottom_blob = bottom_blob_ref;
806 }
807
808 int ret = convert_layout(bottom_blob, layer, opt);
809 if (ret != 0)
810 return ret;
811
812 // forward
813 if (opt.lightmode && layer->support_inplace)
814 {
815 Mat& bottom_top_blob = bottom_blob;
816 int ret = layer->forward_inplace(bottom_top_blob, opt);
817 if (ret != 0)
818 return ret;
819
820 // store top blob
821 blob_mats[top_blob_index] = bottom_top_blob;
822 }
823 else
824 {
825 Mat top_blob;
826 int ret = layer->forward(bottom_blob, top_blob, opt);
827 if (ret != 0)
828 return ret;
829
830 // store top blob
831 blob_mats[top_blob_index] = top_blob;
832 }
833
834 if (opt.lightmode)
835 {
836 // delete after taken in light mode
837 blob_mats[bottom_blob_index].release();
838 }
839 }
840 else
841 {
842 std::vector<Mat> bottom_blobs(layer->bottoms.size());
843 for (size_t i = 0; i < layer->bottoms.size(); i++)
844 {
845 int bottom_blob_index = layer->bottoms[i];
846
847 Mat& bottom_blob_ref = blob_mats[bottom_blob_index];
848 bottom_blobs[i].release();
849
850 if (opt.lightmode)
851 {
852 // deep copy for inplace forward if data is shared
853 if (layer->support_inplace && *bottom_blob_ref.refcount != 1)
854 {
855 bottom_blobs[i] = bottom_blob_ref.clone(opt.blob_allocator);
856 if (bottom_blobs[i].empty())
857 return -100;
858 }
859 }
860 if (bottom_blobs[i].dims == 0)
861 {
862 bottom_blobs[i] = bottom_blob_ref;
863 }
864
865 int ret = convert_layout(bottom_blobs[i], layer, opt);
866 if (ret != 0)
867 return ret;
868 }
869
870 // forward
871 if (opt.lightmode && layer->support_inplace)
872 {
873 std::vector<Mat>& bottom_top_blobs = bottom_blobs;
874 int ret = layer->forward_inplace(bottom_top_blobs, opt);
875 if (ret != 0)
876 return ret;
877
878 // store top blobs
879 for (size_t i = 0; i < layer->tops.size(); i++)
880 {
881 int top_blob_index = layer->tops[i];
882
883 blob_mats[top_blob_index] = bottom_top_blobs[i];
884 }
885 }
886 else
887 {
888 std::vector<Mat> top_blobs(layer->tops.size());
889 int ret = layer->forward(bottom_blobs, top_blobs, opt);
890 if (ret != 0)
891 return ret;
892
893 // store top blobs
894 for (size_t i = 0; i < layer->tops.size(); i++)
895 {
896 int top_blob_index = layer->tops[i];
897
898 blob_mats[top_blob_index] = top_blobs[i];
899 }
900 }
901
902 if (opt.lightmode)
903 {
904 for (size_t i = 0; i < layer->bottoms.size(); i++)
905 {
906 int bottom_blob_index = layer->bottoms[i];
907
908 // delete after taken in light mode
909 blob_mats[bottom_blob_index].release();
910 }
911 }
912 }
913
914 return 0;
915}
916
917#if NCNN_VULKAN
918int NetPrivate::do_forward_layer(const Layer* layer, std::vector<VkMat>& blob_mats_gpu, VkCompute& cmd, const Option& opt) const
919{
920 if (layer->one_blob_only)
921 {
922 // load bottom blob
923 int bottom_blob_index = layer->bottoms[0];
924 int top_blob_index = layer->tops[0];
925
926 VkMat& bottom_blob_ref = blob_mats_gpu[bottom_blob_index];
927 VkMat bottom_blob;
928
929 if (opt.lightmode)
930 {
931 // deep copy for inplace forward if data is shared
932 if (layer->support_inplace && *bottom_blob_ref.refcount != 1)
933 {
934 cmd.record_clone(bottom_blob_ref, bottom_blob, opt);
935 // NCNN_LOGE("clone %p[+%lu] %p[+%lu]", bottom_blob_ref.buffer(), bottom_blob_ref.buffer_offset(), bottom_blob.buffer(), bottom_blob.buffer_offset());
936 }
937 }
938 if (bottom_blob.dims == 0)
939 {
940 bottom_blob = bottom_blob_ref;
941 }
942
943 // forward
944 if (opt.lightmode && layer->support_inplace)
945 {
946 VkMat& bottom_top_blob = bottom_blob;
947 int ret = layer->forward_inplace(bottom_top_blob, cmd, opt);
948 if (ret != 0)
949 return ret;
950
951 // store top blob
952 blob_mats_gpu[top_blob_index] = bottom_top_blob;
953 }
954 else
955 {
956 VkMat top_blob;
957 int ret = layer->forward(bottom_blob, top_blob, cmd, opt);
958 if (ret != 0)
959 return ret;
960
961 // store top blob
962 blob_mats_gpu[top_blob_index] = top_blob;
963 }
964
965 if (opt.lightmode)
966 {
967 // delete after taken in light mode
968 blob_mats_gpu[bottom_blob_index].release();
969 }
970 }
971 else
972 {
973 // load bottom blobs
974 std::vector<VkMat> bottom_blobs(layer->bottoms.size());
975 for (size_t i = 0; i < layer->bottoms.size(); i++)
976 {
977 int bottom_blob_index = layer->bottoms[i];
978
979 VkMat& bottom_blob_ref = blob_mats_gpu[bottom_blob_index];
980 bottom_blobs[i].release();
981
982 if (opt.lightmode)
983 {
984 // deep copy for inplace forward if data is shared
985 if (layer->support_inplace && *bottom_blob_ref.refcount != 1)
986 {
987 cmd.record_clone(bottom_blob_ref, bottom_blobs[i], opt);
988 // NCNN_LOGE("clone %p[+%lu] %p[+%lu]", bottom_blob_ref.buffer(), bottom_blob_ref.buffer_offset(), bottom_blobs[i].buffer(), bottom_blobs[i].buffer_offset());
989 }
990 }
991 if (bottom_blobs[i].dims == 0)
992 {
993 bottom_blobs[i] = bottom_blob_ref;
994 }
995 }
996
997 // forward
998 if (opt.lightmode && layer->support_inplace)
999 {
1000 std::vector<VkMat>& bottom_top_blobs = bottom_blobs;
1001 int ret = layer->forward_inplace(bottom_top_blobs, cmd, opt);
1002 if (ret != 0)
1003 return ret;
1004
1005 // store top blobs
1006 for (size_t i = 0; i < layer->tops.size(); i++)
1007 {
1008 int top_blob_index = layer->tops[i];
1009
1010 blob_mats_gpu[top_blob_index] = bottom_top_blobs[i];
1011 }
1012 }
1013 else
1014 {
1015 std::vector<VkMat> top_blobs(layer->tops.size());
1016 int ret = layer->forward(bottom_blobs, top_blobs, cmd, opt);
1017 if (ret != 0)
1018 return ret;
1019
1020 // store top blobs
1021 for (size_t i = 0; i < layer->tops.size(); i++)
1022 {
1023 int top_blob_index = layer->tops[i];
1024
1025 blob_mats_gpu[top_blob_index] = top_blobs[i];
1026 }
1027 }
1028
1029 if (opt.lightmode)
1030 {
1031 for (size_t i = 0; i < layer->bottoms.size(); i++)
1032 {
1033 int bottom_blob_index = layer->bottoms[i];
1034
1035 // delete after taken in light mode
1036 blob_mats_gpu[bottom_blob_index].release();
1037 }
1038 }
1039 }
1040
1041 return 0;
1042}
1043
1044int NetPrivate::do_forward_layer(const Layer* layer, std::vector<VkImageMat>& blob_mats_gpu_image, VkCompute& cmd, const Option& opt) const
1045{
1046 if (layer->one_blob_only)
1047 {
1048 // load bottom blob
1049 int bottom_blob_index = layer->bottoms[0];
1050 int top_blob_index = layer->tops[0];
1051
1052 VkImageMat& bottom_blob_ref = blob_mats_gpu_image[bottom_blob_index];
1053 VkImageMat bottom_blob;
1054
1055 if (opt.lightmode)
1056 {
1057 // deep copy for inplace forward if data is shared
1058 if (layer->support_inplace && *bottom_blob_ref.refcount != 1)
1059 {
1060 cmd.record_clone(bottom_blob_ref, bottom_blob, opt);
1061 // NCNN_LOGE("clone %p[+%lu] %p[+%lu]", bottom_blob_ref.buffer(), bottom_blob_ref.buffer_offset(), bottom_blob.buffer(), bottom_blob.buffer_offset());
1062 }
1063 }
1064 if (bottom_blob.dims == 0)
1065 {
1066 bottom_blob = bottom_blob_ref;
1067 }
1068
1069 // forward
1070 if (opt.lightmode && layer->support_inplace)
1071 {
1072 VkImageMat& bottom_top_blob = bottom_blob;
1073 int ret = layer->forward_inplace(bottom_top_blob, cmd, opt);
1074 if (ret != 0)
1075 return ret;
1076
1077 // store top blob
1078 blob_mats_gpu_image[top_blob_index] = bottom_top_blob;
1079 }
1080 else
1081 {
1082 VkImageMat top_blob;
1083 int ret = layer->forward(bottom_blob, top_blob, cmd, opt);
1084 if (ret != 0)
1085 return ret;
1086
1087 // store top blob
1088 blob_mats_gpu_image[top_blob_index] = top_blob;
1089 }
1090
1091 if (opt.lightmode)
1092 {
1093 // delete after taken in light mode
1094 blob_mats_gpu_image[bottom_blob_index].release();
1095 }
1096 }
1097 else
1098 {
1099 // load bottom blobs
1100 std::vector<VkImageMat> bottom_blobs(layer->bottoms.size());
1101 for (size_t i = 0; i < layer->bottoms.size(); i++)
1102 {
1103 int bottom_blob_index = layer->bottoms[i];
1104
1105 VkImageMat& bottom_blob_ref = blob_mats_gpu_image[bottom_blob_index];
1106
1107 if (opt.lightmode)
1108 {
1109 // deep copy for inplace forward if data is shared
1110 if (layer->support_inplace && *bottom_blob_ref.refcount != 1)
1111 {
1112 cmd.record_clone(bottom_blob_ref, bottom_blobs[i], opt);
1113 // NCNN_LOGE("clone %p[+%lu] %p[+%lu]", bottom_blob_ref.buffer(), bottom_blob_ref.buffer_offset(), bottom_blobs[i].buffer(), bottom_blobs[i].buffer_offset());
1114 }
1115 }
1116 if (bottom_blobs[i].dims == 0)
1117 {
1118 bottom_blobs[i] = bottom_blob_ref;
1119 }
1120 }
1121
1122 // forward
1123 if (opt.lightmode && layer->support_inplace)
1124 {
1125 std::vector<VkImageMat>& bottom_top_blobs = bottom_blobs;
1126 int ret = layer->forward_inplace(bottom_top_blobs, cmd, opt);
1127 if (ret != 0)
1128 return ret;
1129
1130 // store top blobs
1131 for (size_t i = 0; i < layer->tops.size(); i++)
1132 {
1133 int top_blob_index = layer->tops[i];
1134
1135 blob_mats_gpu_image[top_blob_index] = bottom_top_blobs[i];
1136 }
1137 }
1138 else
1139 {
1140 std::vector<VkImageMat> top_blobs(layer->tops.size());
1141 int ret = layer->forward(bottom_blobs, top_blobs, cmd, opt);
1142 if (ret != 0)
1143 return ret;
1144
1145 // store top blobs
1146 for (size_t i = 0; i < layer->tops.size(); i++)
1147 {
1148 int top_blob_index = layer->tops[i];
1149
1150 blob_mats_gpu_image[top_blob_index] = top_blobs[i];
1151 }
1152 }
1153
1154 if (opt.lightmode)
1155 {
1156 for (size_t i = 0; i < layer->bottoms.size(); i++)
1157 {
1158 int bottom_blob_index = layer->bottoms[i];
1159
1160 // delete after taken in light mode
1161 blob_mats_gpu_image[bottom_blob_index].release();
1162 }
1163 }
1164 }
1165
1166 return 0;
1167}
1168#endif // NCNN_VULKAN
1169
1170void NetPrivate::update_input_output_indexes()
1171{
1172 input_blob_indexes.clear();
1173 output_blob_indexes.clear();
1174
1175 for (size_t i = 0; i < layers.size(); i++)
1176 {
1177 if (layers[i]->typeindex == LayerType::Input)
1178 {
1179 int blob_index = layers[i]->tops[0];
1180 input_blob_indexes.push_back(blob_index);
1181 }
1182 }
1183
1184 for (size_t i = 0; i < blobs.size(); i++)
1185 {
1186 if (blobs[i].producer != -1 && blobs[i].consumer == -1)
1187 {
1188 output_blob_indexes.push_back(i);
1189 }
1190 }
1191}
1192
1193#if NCNN_STRING
1194void NetPrivate::update_input_output_names()
1195{
1196 input_blob_names.clear();
1197 output_blob_names.clear();
1198
1199 for (size_t i = 0; i < input_blob_indexes.size(); i++)
1200 {
1201 int blob_index = input_blob_indexes[i];
1202 input_blob_names.push_back(blobs[blob_index].name.c_str());
1203 }
1204
1205 for (size_t i = 0; i < output_blob_indexes.size(); i++)
1206 {
1207 int blob_index = output_blob_indexes[i];
1208 output_blob_names.push_back(blobs[blob_index].name.c_str());
1209 }
1210}
1211#endif // NCNN_STRING
1212
1213Net::Net()
1214 : d(new NetPrivate(opt))
1215{
1216}
1217
1218Net::~Net()
1219{
1220 clear();
1221
1222 delete d;
1223}
1224
1225Net::Net(const Net&)
1226 : d(0)
1227{
1228}
1229
1230Net& Net::operator=(const Net&)
1231{
1232 return *this;
1233}
1234
1235#if NCNN_STRING
1236int Net::register_custom_layer(const char* type, layer_creator_func creator, layer_destroyer_func destroyer, void* userdata)
1237{
1238 int typeindex = layer_to_index(type);
1239 if (typeindex != -1)
1240 {
1241 NCNN_LOGE("overwrite built-in layer type %s", type);
1242
1243 for (size_t i = 0; i < d->overwrite_builtin_layer_registry.size(); i++)
1244 {
1245 if (d->overwrite_builtin_layer_registry[i].typeindex == typeindex)
1246 {
1247 NCNN_LOGE("overwrite existing overwritten built-in layer index %d", typeindex);
1248
1249 d->overwrite_builtin_layer_registry[i].creator = creator;
1250 d->overwrite_builtin_layer_registry[i].destroyer = destroyer;
1251 d->overwrite_builtin_layer_registry[i].userdata = userdata;
1252 return 0;
1253 }
1254 }
1255
1256 struct overwrite_builtin_layer_registry_entry entry = {typeindex, creator, destroyer, userdata};
1257 d->overwrite_builtin_layer_registry.push_back(entry);
1258 return 0;
1259 }
1260
1261 int custom_index = custom_layer_to_index(type);
1262 if (custom_index == -1)
1263 {
1264 struct custom_layer_registry_entry entry = {type, creator, destroyer, userdata};
1265 d->custom_layer_registry.push_back(entry);
1266 }
1267 else
1268 {
1269 NCNN_LOGE("overwrite existing custom layer type %s", type);
1270 d->custom_layer_registry[custom_index].name = type;
1271 d->custom_layer_registry[custom_index].creator = creator;
1272 d->custom_layer_registry[custom_index].destroyer = destroyer;
1273 d->custom_layer_registry[custom_index].userdata = userdata;
1274 }
1275
1276 return 0;
1277}
1278#endif // NCNN_STRING
1279
1280int Net::register_custom_layer(int index, layer_creator_func creator, layer_destroyer_func destroyer, void* userdata)
1281{
1282 int custom_index = index & ~LayerType::CustomBit;
1283 if (index == custom_index)
1284 {
1285 NCNN_LOGE("overwrite built-in layer type %d", index);
1286
1287 for (size_t i = 0; i < d->overwrite_builtin_layer_registry.size(); i++)
1288 {
1289 if (d->overwrite_builtin_layer_registry[i].typeindex == index)
1290 {
1291 NCNN_LOGE("overwrite existing overwritten built-in layer index %d", index);
1292
1293 d->overwrite_builtin_layer_registry[i].creator = creator;
1294 d->overwrite_builtin_layer_registry[i].destroyer = destroyer;
1295 d->overwrite_builtin_layer_registry[i].userdata = userdata;
1296 return 0;
1297 }
1298 }
1299
1300 struct overwrite_builtin_layer_registry_entry entry = {index, creator, destroyer, userdata};
1301 d->overwrite_builtin_layer_registry.push_back(entry);
1302 return 0;
1303 }
1304
1305 if ((int)d->custom_layer_registry.size() <= custom_index)
1306 {
1307#if NCNN_STRING
1308 struct custom_layer_registry_entry dummy = {"", 0, 0, 0};
1309#else
1310 struct custom_layer_registry_entry dummy = {0, 0, 0};
1311#endif // NCNN_STRING
1312 d->custom_layer_registry.resize(custom_index + 1, dummy);
1313 }
1314
1315 if (d->custom_layer_registry[custom_index].creator)
1316 {
1317 NCNN_LOGE("overwrite existing custom layer index %d", custom_index);
1318 }
1319
1320 d->custom_layer_registry[custom_index].creator = creator;
1321 d->custom_layer_registry[custom_index].destroyer = destroyer;
1322 d->custom_layer_registry[custom_index].userdata = userdata;
1323 return 0;
1324}
1325
1326#if NCNN_STRING
1327int Net::load_param(const DataReader& dr)
1328{
1329#define SCAN_VALUE(fmt, v) \
1330 if (dr.scan(fmt, &v) != 1) \
1331 { \
1332 NCNN_LOGE("parse " #v " failed"); \
1333 return -1; \
1334 }
1335
1336 int magic = 0;
1337 SCAN_VALUE("%d", magic)
1338 if (magic != 7767517)
1339 {
1340 NCNN_LOGE("param is too old, please regenerate");
1341 return -1;
1342 }
1343
1344 // parse
1345 int layer_count = 0;
1346 int blob_count = 0;
1347 SCAN_VALUE("%d", layer_count)
1348 SCAN_VALUE("%d", blob_count)
1349 if (layer_count <= 0 || blob_count <= 0)
1350 {
1351 NCNN_LOGE("invalid layer_count or blob_count");
1352 return -1;
1353 }
1354
1355 d->layers.resize((size_t)layer_count);
1356 d->blobs.resize((size_t)blob_count);
1357
1358#if NCNN_VULKAN
1359 // TODO enable gpu when bf16 conversion implemented
1360 if (opt.use_bf16_storage)
1361 opt.use_vulkan_compute = false;
1362
1363 if (opt.use_vulkan_compute)
1364 {
1365 if (!d->vkdev) d->vkdev = get_gpu_device();
1366 if (!d->vkdev) opt.use_vulkan_compute = false; // no vulkan device, fallback to cpu
1367 }
1368 if (opt.use_vulkan_compute)
1369 {
1370 // sanitize use options
1371 if (!d->vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
1372 if (!d->vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
1373 if (!d->vkdev->info.support_fp16_uniform()) opt.use_fp16_uniform = false;
1374 if (!d->vkdev->info.support_fp16_arithmetic()) opt.use_fp16_arithmetic = false;
1375 if (!d->vkdev->info.support_int8_packed()) opt.use_int8_packed = false;
1376 if (!d->vkdev->info.support_int8_storage()) opt.use_int8_storage = false;
1377 if (!d->vkdev->info.support_int8_uniform()) opt.use_int8_uniform = false;
1378 if (!d->vkdev->info.support_int8_arithmetic()) opt.use_int8_arithmetic = false;
1379 if (!d->vkdev->info.support_cooperative_matrix()) opt.use_cooperative_matrix = false;
1380
1381 if (d->vkdev->info.bug_buffer_image_load_zero()) opt.use_image_storage = false;
1382
1383 // enable local memory optimization on discrete gpu only
1384 if (d->vkdev->info.type() != 0) opt.use_shader_local_memory = false;
1385
1386 // fp16a makes no sense when fp16 storage disabled
1387 if (!opt.use_fp16_packed && !opt.use_fp16_storage) opt.use_fp16_arithmetic = false;
1388
1389 // fp16 uniform makes no sense when fp16 arithmetic disabled
1390 if (!opt.use_fp16_arithmetic) opt.use_fp16_uniform = false;
1391 }
1392 else
1393 {
1394 // fp16a makes no sense when fp16 storage disabled
1395 if (!opt.use_fp16_storage) opt.use_fp16_arithmetic = false;
1396 }
1397#endif // NCNN_VULKAN
1398
1399 ParamDict pd;
1400
1401 int blob_index = 0;
1402 for (int i = 0; i < layer_count; i++)
1403 {
1404 char layer_type[256];
1405 char layer_name[256];
1406 int bottom_count = 0;
1407 int top_count = 0;
1408 SCAN_VALUE("%255s", layer_type)
1409 SCAN_VALUE("%255s", layer_name)
1410 SCAN_VALUE("%d", bottom_count)
1411 SCAN_VALUE("%d", top_count)
1412
1413 Layer* layer = create_overwrite_builtin_layer(layer_type);
1414#if NCNN_VULKAN
1415 if (!layer && opt.use_vulkan_compute && d->vkdev)
1416 {
1417 layer = create_layer_vulkan(layer_type);
1418 }
1419#endif // NCNN_VULKAN
1420 if (!layer)
1421 {
1422 layer = create_layer_cpu(layer_type);
1423 }
1424 if (!layer)
1425 {
1426 layer = create_custom_layer(layer_type);
1427 }
1428 if (!layer)
1429 {
1430 NCNN_LOGE("layer %s not exists or registered", layer_type);
1431 clear();
1432 return -1;
1433 }
1434
1435#if NCNN_VULKAN
1436 if (opt.use_vulkan_compute)
1437 layer->vkdev = d->vkdev;
1438#endif // NCNN_VULKAN
1439
1440 layer->type = std::string(layer_type);
1441 layer->name = std::string(layer_name);
1442 // NCNN_LOGE("new layer %d %s", i, layer_name);
1443
1444 layer->bottoms.resize(bottom_count);
1445 for (int j = 0; j < bottom_count; j++)
1446 {
1447 char bottom_name[256];
1448 SCAN_VALUE("%255s", bottom_name)
1449
1450 int bottom_blob_index = find_blob_index_by_name(bottom_name);
1451 if (bottom_blob_index == -1)
1452 {
1453 Blob& blob = d->blobs[blob_index];
1454
1455 bottom_blob_index = blob_index;
1456
1457 blob.name = std::string(bottom_name);
1458 // NCNN_LOGE("new blob %s", bottom_name);
1459
1460 blob_index++;
1461 }
1462
1463 Blob& blob = d->blobs[bottom_blob_index];
1464
1465 blob.consumer = i;
1466
1467 layer->bottoms[j] = bottom_blob_index;
1468 }
1469
1470 layer->tops.resize(top_count);
1471 for (int j = 0; j < top_count; j++)
1472 {
1473 Blob& blob = d->blobs[blob_index];
1474
1475 char blob_name[256];
1476 SCAN_VALUE("%255s", blob_name)
1477
1478 blob.name = std::string(blob_name);
1479 // NCNN_LOGE("new blob %s", blob_name);
1480
1481 blob.producer = i;
1482
1483 layer->tops[j] = blob_index;
1484
1485 blob_index++;
1486 }
1487
1488 int layer_support_vulkan = layer->support_vulkan;
1489
1490 // layer specific params
1491 int pdlr = pd.load_param(dr);
1492 if (pdlr != 0)
1493 {
1494 NCNN_LOGE("ParamDict load_param %d %s failed", i, layer_name);
1495 continue;
1496 }
1497
1498 // pull out top shape hints
1499 Mat shape_hints = pd.get(30, Mat());
1500 if (!shape_hints.empty())
1501 {
1502 const int* psh = shape_hints;
1503 for (int j = 0; j < top_count; j++)
1504 {
1505 Blob& blob = d->blobs[layer->tops[j]];
1506
1507 int dims = psh[0];
1508 if (dims == 1)
1509 {
1510 blob.shape = Mat(psh[1], (void*)0, 4u, 1);
1511 }
1512 if (dims == 2)
1513 {
1514 blob.shape = Mat(psh[1], psh[2], (void*)0, 4u, 1);
1515 }
1516 if (dims == 3)
1517 {
1518 blob.shape = Mat(psh[1], psh[2], psh[3], (void*)0, 4u, 1);
1519 }
1520
1521 psh += 4;
1522 }
1523 }
1524
1525 // set bottom and top shape hints
1526 layer->bottom_shapes.resize(bottom_count);
1527 for (int j = 0; j < bottom_count; j++)
1528 {
1529 layer->bottom_shapes[j] = d->blobs[layer->bottoms[j]].shape;
1530 }
1531
1532 layer->top_shapes.resize(top_count);
1533 for (int j = 0; j < top_count; j++)
1534 {
1535 layer->top_shapes[j] = d->blobs[layer->tops[j]].shape;
1536 }
1537
1538 // pull out layer specific feature disabled set
1539 layer->featmask = pd.get(31, 0);
1540
1541 int lr = layer->load_param(pd);
1542 if (lr != 0)
1543 {
1544 NCNN_LOGE("layer load_param %d %s failed", i, layer_name);
1545 continue;
1546 }
1547
1548 if (layer->support_int8_storage)
1549 {
1550 // no int8 gpu support yet
1551 opt.use_vulkan_compute = false;
1552 }
1553
1554 Option opt1 = get_masked_option(opt, layer->featmask);
1555#if NCNN_VULKAN
1556 if (opt1.use_vulkan_compute)
1557 {
1558 if (!layer->support_image_storage) opt1.use_image_storage = false;
1559 }
1560#endif // NCNN_VULKAN
1561
1562 if (layer_support_vulkan && (!layer->support_vulkan || !opt1.use_vulkan_compute))
1563 {
1564 // vulkan layer cannot handle these param, recreate cpu layer
1565 Layer* layer_cpu = create_overwrite_builtin_layer(layer_type);
1566 if (!layer_cpu)
1567 {
1568 layer_cpu = create_layer_cpu(layer_type);
1569 }
1570 if (!layer_cpu)
1571 {
1572 layer_cpu = create_custom_layer(layer_type);
1573 }
1574 if (!layer_cpu)
1575 {
1576 NCNN_LOGE("layer %s not exists or registered", layer_type);
1577 clear();
1578 return -1;
1579 }
1580
1581 layer_cpu->type = layer->type;
1582 layer_cpu->name = layer->name;
1583 layer_cpu->bottoms = layer->bottoms;
1584 layer_cpu->tops = layer->tops;
1585 layer_cpu->bottom_shapes = layer->bottom_shapes;
1586 layer_cpu->top_shapes = layer->top_shapes;
1587 layer_cpu->featmask = layer->featmask;
1588
1589 int lr = layer_cpu->load_param(pd);
1590 if (lr != 0)
1591 {
1592 NCNN_LOGE("layer load_param %d %s failed", i, layer_name);
1593 continue;
1594 }
1595
1596 delete layer;
1597 layer = layer_cpu;
1598 }
1599
1600 d->layers[i] = layer;
1601 }
1602
1603 d->update_input_output_indexes();
1604 d->update_input_output_names();
1605
1606#undef SCAN_VALUE
1607 return 0;
1608}
1609#endif // NCNN_STRING
1610
1611int Net::load_param_bin(const DataReader& dr)
1612{
1613#if __BIG_ENDIAN__
1614#define READ_VALUE(buf) \
1615 if (dr.read(&buf, sizeof(buf)) != sizeof(buf)) \
1616 { \
1617 NCNN_LOGE("read " #buf " failed"); \
1618 return -1; \
1619 } \
1620 if (sizeof(buf) == 2) \
1621 swap_endianness_16(&buf); \
1622 if (sizeof(buf) == 4) \
1623 swap_endianness_32(&buf);
1624#else
1625#define READ_VALUE(buf) \
1626 if (dr.read(&buf, sizeof(buf)) != sizeof(buf)) \
1627 { \
1628 NCNN_LOGE("read " #buf " failed"); \
1629 return -1; \
1630 }
1631#endif
1632
1633 int magic = 0;
1634 READ_VALUE(magic)
1635 if (magic != 7767517)
1636 {
1637 NCNN_LOGE("param is too old, please regenerate");
1638 return -1;
1639 }
1640
1641 int layer_count = 0;
1642 int blob_count = 0;
1643 READ_VALUE(layer_count)
1644 READ_VALUE(blob_count)
1645 if (layer_count <= 0 || blob_count <= 0)
1646 {
1647 NCNN_LOGE("invalid layer_count or blob_count");
1648 return -1;
1649 }
1650
1651 d->layers.resize(layer_count);
1652 d->blobs.resize(blob_count);
1653
1654#if NCNN_VULKAN
1655 // TODO enable gpu when bf16 conversion implemented
1656 if (opt.use_bf16_storage)
1657 opt.use_vulkan_compute = false;
1658
1659 if (opt.use_vulkan_compute)
1660 {
1661 if (!d->vkdev) d->vkdev = get_gpu_device();
1662 if (!d->vkdev) opt.use_vulkan_compute = false; // no vulkan device, fallback to cpu
1663 }
1664 if (opt.use_vulkan_compute)
1665 {
1666 // sanitize use options
1667 if (!d->vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
1668 if (!d->vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
1669 if (!d->vkdev->info.support_fp16_uniform()) opt.use_fp16_uniform = false;
1670 if (!d->vkdev->info.support_fp16_arithmetic()) opt.use_fp16_arithmetic = false;
1671 if (!d->vkdev->info.support_int8_packed()) opt.use_int8_packed = false;
1672 if (!d->vkdev->info.support_int8_storage()) opt.use_int8_storage = false;
1673 if (!d->vkdev->info.support_int8_uniform()) opt.use_int8_uniform = false;
1674 if (!d->vkdev->info.support_int8_arithmetic()) opt.use_int8_arithmetic = false;
1675 if (!d->vkdev->info.support_cooperative_matrix()) opt.use_cooperative_matrix = false;
1676
1677 if (d->vkdev->info.bug_buffer_image_load_zero()) opt.use_image_storage = false;
1678
1679 // enable local memory optimization on discrete gpu only
1680 if (d->vkdev->info.type() != 0) opt.use_shader_local_memory = false;
1681
1682 // fp16a makes no sense when fp16 storage disabled
1683 if (!opt.use_fp16_packed && !opt.use_fp16_storage) opt.use_fp16_arithmetic = false;
1684
1685 // fp16 uniform makes no sense when fp16 arithmetic disabled
1686 if (!opt.use_fp16_arithmetic) opt.use_fp16_uniform = false;
1687 }
1688 else
1689 {
1690 // fp16a makes no sense when fp16 storage disabled
1691 if (!opt.use_fp16_storage) opt.use_fp16_arithmetic = false;
1692 }
1693#endif // NCNN_VULKAN
1694
1695 ParamDict pd;
1696
1697 for (int i = 0; i < layer_count; i++)
1698 {
1699 int typeindex;
1700 int bottom_count;
1701 int top_count;
1702 READ_VALUE(typeindex)
1703 READ_VALUE(bottom_count)
1704 READ_VALUE(top_count)
1705
1706 Layer* layer = create_overwrite_builtin_layer(typeindex);
1707#if NCNN_VULKAN
1708 if (!layer && opt.use_vulkan_compute && d->vkdev)
1709 {
1710 layer = create_layer_vulkan(typeindex);
1711 }
1712#endif // NCNN_VULKAN
1713 if (!layer)
1714 {
1715 layer = create_layer_cpu(typeindex);
1716 }
1717 if (!layer)
1718 {
1719 int custom_index = typeindex & ~LayerType::CustomBit;
1720 layer = create_custom_layer(custom_index);
1721 }
1722 if (!layer)
1723 {
1724 NCNN_LOGE("layer %d not exists or registered", typeindex);
1725 clear();
1726 return -1;
1727 }
1728
1729#if NCNN_VULKAN
1730 if (opt.use_vulkan_compute)
1731 layer->vkdev = d->vkdev;
1732#endif // NCNN_VULKAN
1733
1734 // layer->type = std::string(layer_type);
1735 // layer->name = std::string(layer_name);
1736 // NCNN_LOGE("new layer %d", typeindex);
1737
1738 layer->bottoms.resize(bottom_count);
1739 for (int j = 0; j < bottom_count; j++)
1740 {
1741 int bottom_blob_index;
1742 READ_VALUE(bottom_blob_index)
1743
1744 Blob& blob = d->blobs[bottom_blob_index];
1745
1746 blob.consumer = i;
1747
1748 layer->bottoms[j] = bottom_blob_index;
1749 }
1750
1751 layer->tops.resize(top_count);
1752 for (int j = 0; j < top_count; j++)
1753 {
1754 int top_blob_index;
1755 READ_VALUE(top_blob_index)
1756
1757 Blob& blob = d->blobs[top_blob_index];
1758
1759 // blob.name = std::string(blob_name);
1760 // NCNN_LOGE("new blob %s", blob_name);
1761
1762 blob.producer = i;
1763
1764 layer->tops[j] = top_blob_index;
1765 }
1766
1767 int layer_support_vulkan = layer->support_vulkan;
1768
1769 // layer specific params
1770 int pdlr = pd.load_param_bin(dr);
1771 if (pdlr != 0)
1772 {
1773 NCNN_LOGE("ParamDict load_param_bin %d failed", i);
1774 continue;
1775 }
1776
1777 // pull out top blob shape hints
1778 Mat shape_hints = pd.get(30, Mat());
1779 if (!shape_hints.empty())
1780 {
1781 const int* psh = shape_hints;
1782 for (int j = 0; j < top_count; j++)
1783 {
1784 Blob& blob = d->blobs[layer->tops[j]];
1785
1786 int dims = psh[0];
1787 if (dims == 1)
1788 {
1789 blob.shape = Mat(psh[1], (void*)0, 4u, 1);
1790 }
1791 if (dims == 2)
1792 {
1793 blob.shape = Mat(psh[1], psh[2], (void*)0, 4u, 1);
1794 }
1795 if (dims == 3)
1796 {
1797 blob.shape = Mat(psh[1], psh[2], psh[3], (void*)0, 4u, 1);
1798 }
1799
1800 psh += 4;
1801 }
1802 }
1803
1804 // set bottom and top shape hints
1805 layer->bottom_shapes.resize(bottom_count);
1806 for (int j = 0; j < bottom_count; j++)
1807 {
1808 layer->bottom_shapes[j] = d->blobs[layer->bottoms[j]].shape;
1809 }
1810
1811 layer->top_shapes.resize(top_count);
1812 for (int j = 0; j < top_count; j++)
1813 {
1814 layer->top_shapes[j] = d->blobs[layer->tops[j]].shape;
1815 }
1816
1817 // pull out layer specific feature disabled set
1818 layer->featmask = pd.get(31, 0);
1819
1820 int lr = layer->load_param(pd);
1821 if (lr != 0)
1822 {
1823 NCNN_LOGE("layer load_param %d failed", i);
1824 continue;
1825 }
1826
1827 if (layer->support_int8_storage)
1828 {
1829 // no int8 gpu support yet
1830 opt.use_vulkan_compute = false;
1831 }
1832
1833 Option opt1 = get_masked_option(opt, layer->featmask);
1834#if NCNN_VULKAN
1835 if (opt1.use_vulkan_compute)
1836 {
1837 if (!layer->support_image_storage) opt1.use_image_storage = false;
1838 }
1839#endif // NCNN_VULKAN
1840
1841 if (layer_support_vulkan && (!layer->support_vulkan || !opt1.use_vulkan_compute))
1842 {
1843 // vulkan layer cannot handle these param, recreate cpu layer
1844 Layer* layer_cpu = create_overwrite_builtin_layer(typeindex);
1845 if (!layer_cpu)
1846 {
1847 layer_cpu = create_layer_cpu(typeindex);
1848 }
1849 if (!layer_cpu)
1850 {
1851 int custom_index = typeindex & ~LayerType::CustomBit;
1852 layer_cpu = create_custom_layer(custom_index);
1853 }
1854 if (!layer_cpu)
1855 {
1856 NCNN_LOGE("layer %d not exists or registered", typeindex);
1857 clear();
1858 return -1;
1859 }
1860
1861 layer_cpu->bottoms = layer->bottoms;
1862 layer_cpu->tops = layer->tops;
1863 layer_cpu->bottom_shapes = layer->bottom_shapes;
1864 layer_cpu->top_shapes = layer->top_shapes;
1865 layer_cpu->featmask = layer->featmask;
1866
1867 int lr = layer_cpu->load_param(pd);
1868 if (lr != 0)
1869 {
1870 NCNN_LOGE("layer load_param %d failed", i);
1871 continue;
1872 }
1873
1874 delete layer;
1875 layer = layer_cpu;
1876 }
1877
1878 d->layers[i] = layer;
1879 }
1880
1881 d->update_input_output_indexes();
1882
1883#undef READ_VALUE
1884 return 0;
1885}
1886
1887int Net::load_model(const DataReader& dr)
1888{
1889 if (d->layers.empty())
1890 {
1891 NCNN_LOGE("network graph not ready");
1892 return -1;
1893 }
1894
1895 int layer_count = (int)d->layers.size();
1896
1897 // load file
1898 int ret = 0;
1899
1900#if NCNN_VULKAN
1901 if (opt.use_vulkan_compute)
1902 {
1903 if (!opt.pipeline_cache)
1904 {
1905 if (!d->pipeline_cache)
1906 d->pipeline_cache = new PipelineCache(d->vkdev);
1907 opt.pipeline_cache = d->pipeline_cache;
1908 }
1909 }
1910#endif // NCNN_VULKAN
1911
1912 ModelBinFromDataReader mb(dr);
1913 for (int i = 0; i < layer_count; i++)
1914 {
1915 Layer* layer = d->layers[i];
1916
1917 //Here we found inconsistent content in the parameter file.
1918 if (!layer)
1919 {
1920 NCNN_LOGE("load_model error at layer %d, parameter file has inconsistent content.", i);
1921 ret = -1;
1922 break;
1923 }
1924
1925 int lret = layer->load_model(mb);
1926 if (lret != 0)
1927 {
1928#if NCNN_STRING
1929 NCNN_LOGE("layer load_model %d %s failed", i, layer->name.c_str());
1930#else
1931 NCNN_LOGE("layer load_model %d failed", i);
1932#endif
1933 ret = -1;
1934 break;
1935 }
1936
1937 Option opt1 = get_masked_option(opt, layer->featmask);
1938
1939 int cret = layer->create_pipeline(opt1);
1940 if (cret != 0)
1941 {
1942#if NCNN_STRING
1943 NCNN_LOGE("layer create_pipeline %d %s failed", i, layer->name.c_str());
1944#else
1945 NCNN_LOGE("layer create_pipeline %d failed", i);
1946#endif
1947 ret = -1;
1948 break;
1949 }
1950 }
1951
1952 if (opt.use_local_pool_allocator)
1953 {
1954 if (opt.blob_allocator == 0)
1955 {
1956 if (!d->local_blob_allocator)
1957 {
1958 d->local_blob_allocator = new PoolAllocator;
1959 d->local_blob_allocator->set_size_compare_ratio(0.f);
1960 }
1961 }
1962 if (opt.workspace_allocator == 0)
1963 {
1964 if (!d->local_workspace_allocator)
1965 {
1966 d->local_workspace_allocator = new PoolAllocator;
1967 d->local_workspace_allocator->set_size_compare_ratio(0.f);
1968 }
1969 }
1970 }
1971
1972#if NCNN_VULKAN
1973 if (ret == 0 && opt.use_vulkan_compute)
1974 {
1975 ret = d->upload_model();
1976 }
1977#endif // NCNN_VULKAN
1978
1979 return ret;
1980}
1981
1982#if NCNN_STDIO
1983#if NCNN_STRING
1984int Net::load_param(FILE* fp)
1985{
1986 DataReaderFromStdio dr(fp);
1987 return load_param(dr);
1988}
1989
1990int Net::load_param_mem(const char* _mem)
1991{
1992 const unsigned char* mem = (const unsigned char*)_mem;
1993 DataReaderFromMemory dr(mem);
1994 return load_param(dr);
1995}
1996
1997int Net::load_param(const char* protopath)
1998{
1999 FILE* fp = fopen(protopath, "rb");
2000 if (!fp)
2001 {
2002 NCNN_LOGE("fopen %s failed", protopath);
2003 return -1;
2004 }
2005
2006 int ret = load_param(fp);
2007 fclose(fp);
2008 return ret;
2009}
2010#endif // NCNN_STRING
2011
2012int Net::load_param_bin(FILE* fp)
2013{
2014 DataReaderFromStdio dr(fp);
2015 return load_param_bin(dr);
2016}
2017
2018int Net::load_param_bin(const char* protopath)
2019{
2020 FILE* fp = fopen(protopath, "rb");
2021 if (!fp)
2022 {
2023 NCNN_LOGE("fopen %s failed", protopath);
2024 return -1;
2025 }
2026
2027 int ret = load_param_bin(fp);
2028 fclose(fp);
2029 return ret;
2030}
2031
2032int Net::load_model(FILE* fp)
2033{
2034 DataReaderFromStdio dr(fp);
2035 return load_model(dr);
2036}
2037
2038int Net::load_model(const char* modelpath)
2039{
2040 FILE* fp = fopen(modelpath, "rb");
2041 if (!fp)
2042 {
2043 NCNN_LOGE("fopen %s failed", modelpath);
2044 return -1;
2045 }
2046
2047 int ret = load_model(fp);
2048 fclose(fp);
2049 return ret;
2050}
2051#endif // NCNN_STDIO
2052
2053int Net::load_param(const unsigned char* _mem)
2054{
2055 const unsigned char* mem = _mem;
2056 DataReaderFromMemory dr(mem);
2057 load_param_bin(dr);
2058 return static_cast<int>(mem - _mem);
2059}
2060
2061int Net::load_model(const unsigned char* _mem)
2062{
2063 const unsigned char* mem = _mem;
2064 DataReaderFromMemory dr(mem);
2065 load_model(dr);
2066 return static_cast<int>(mem - _mem);
2067}
2068
2069#if NCNN_PLATFORM_API
2070#if __ANDROID_API__ >= 9
2071#if NCNN_STRING
2072int Net::load_param(AAsset* asset)
2073{
2074 DataReaderFromAndroidAsset dr(asset);
2075 return load_param(dr);
2076}
2077
2078int Net::load_param(AAssetManager* mgr, const char* assetpath)
2079{
2080 AAsset* asset = AAssetManager_open(mgr, assetpath, AASSET_MODE_BUFFER);
2081 if (!asset)
2082 {
2083 NCNN_LOGE("AAssetManager_open %s failed", assetpath);
2084 return -1;
2085 }
2086
2087 int ret = load_param(asset);
2088 AAsset_close(asset);
2089 return ret;
2090}
2091#endif // NCNN_STRING
2092
2093int Net::load_param_bin(AAsset* asset)
2094{
2095 DataReaderFromAndroidAsset dr(asset);
2096 return load_param_bin(dr);
2097}
2098
2099int Net::load_param_bin(AAssetManager* mgr, const char* assetpath)
2100{
2101 AAsset* asset = AAssetManager_open(mgr, assetpath, AASSET_MODE_BUFFER);
2102 if (!asset)
2103 {
2104 NCNN_LOGE("AAssetManager_open %s failed", assetpath);
2105 return -1;
2106 }
2107
2108 int ret = load_param_bin(asset);
2109 AAsset_close(asset);
2110 return ret;
2111}
2112
2113int Net::load_model(AAsset* asset)
2114{
2115 DataReaderFromAndroidAsset dr(asset);
2116 return load_model(dr);
2117}
2118
2119int Net::load_model(AAssetManager* mgr, const char* assetpath)
2120{
2121 AAsset* asset = AAssetManager_open(mgr, assetpath, AASSET_MODE_STREAMING);
2122 if (!asset)
2123 {
2124 NCNN_LOGE("AAssetManager_open %s failed", assetpath);
2125 return -1;
2126 }
2127
2128 int ret = load_model(asset);
2129 AAsset_close(asset);
2130 return ret;
2131}
2132#endif // __ANDROID_API__ >= 9
2133#endif // NCNN_PLATFORM_API
2134
2135void Net::clear()
2136{
2137 d->blobs.clear();
2138 for (size_t i = 0; i < d->layers.size(); i++)
2139 {
2140 Layer* layer = d->layers[i];
2141
2142 Option opt1 = get_masked_option(opt, layer->featmask);
2143#if NCNN_VULKAN
2144 if (!layer->support_image_storage)
2145 {
2146 opt1.use_image_storage = false;
2147 }
2148#endif // NCNN_VULKAN
2149
2150 int dret = layer->destroy_pipeline(opt1);
2151 if (dret != 0)
2152 {
2153 NCNN_LOGE("layer destroy_pipeline failed");
2154 // ignore anyway
2155 }
2156
2157 if (layer->typeindex & ncnn::LayerType::CustomBit)
2158 {
2159 int custom_index = layer->typeindex & ~ncnn::LayerType::CustomBit;
2160 if (d->custom_layer_registry[custom_index].destroyer)
2161 {
2162 d->custom_layer_registry[custom_index].destroyer(layer, d->custom_layer_registry[custom_index].userdata);
2163 }
2164 else
2165 {
2166 delete layer;
2167 }
2168 }
2169 else
2170 {
2171 // check overwrite builtin layer destroyer
2172 int index = -1;
2173 const size_t overwrite_builtin_layer_registry_entry_count = d->overwrite_builtin_layer_registry.size();
2174 for (size_t i = 0; i < overwrite_builtin_layer_registry_entry_count; i++)
2175 {
2176 if (d->overwrite_builtin_layer_registry[i].typeindex == layer->typeindex)
2177 {
2178 index = i;
2179 break;
2180 }
2181 }
2182
2183 if (index != -1 && d->overwrite_builtin_layer_registry[index].destroyer)
2184 {
2185 d->overwrite_builtin_layer_registry[index].destroyer(layer, d->overwrite_builtin_layer_registry[index].userdata);
2186 }
2187 else
2188 {
2189 delete layer;
2190 }
2191 }
2192 }
2193 d->layers.clear();
2194
2195 if (d->local_blob_allocator)
2196 {
2197 delete d->local_blob_allocator;
2198 d->local_blob_allocator = 0;
2199 }
2200 if (d->local_workspace_allocator)
2201 {
2202 delete d->local_workspace_allocator;
2203 d->local_workspace_allocator = 0;
2204 }
2205
2206#if NCNN_VULKAN
2207 if (d->weight_vkallocator)
2208 {
2209 delete d->weight_vkallocator;
2210 d->weight_vkallocator = 0;
2211 }
2212 if (d->weight_staging_vkallocator)
2213 {
2214 delete d->weight_staging_vkallocator;
2215 d->weight_staging_vkallocator = 0;
2216 }
2217 if (d->pipeline_cache)
2218 {
2219 delete d->pipeline_cache;
2220 d->pipeline_cache = 0;
2221 opt.pipeline_cache = 0;
2222 }
2223#endif // NCNN_VULKAN
2224}
2225
2226Extractor Net::create_extractor() const
2227{
2228 return Extractor(this, d->blobs.size());
2229}
2230
2231const std::vector<int>& Net::input_indexes() const
2232{
2233 return d->input_blob_indexes;
2234}
2235
2236const std::vector<int>& Net::output_indexes() const
2237{
2238 return d->output_blob_indexes;
2239}
2240
2241#if NCNN_STRING
2242const std::vector<const char*>& Net::input_names() const
2243{
2244 return d->input_blob_names;
2245}
2246
2247const std::vector<const char*>& Net::output_names() const
2248{
2249 return d->output_blob_names;
2250}
2251#endif
2252
2253const std::vector<Blob>& Net::blobs() const
2254{
2255 return d->blobs;
2256}
2257
2258const std::vector<Layer*>& Net::layers() const
2259{
2260 return d->layers;
2261}
2262
2263std::vector<Blob>& Net::mutable_blobs()
2264{
2265 return d->blobs;
2266}
2267
2268std::vector<Layer*>& Net::mutable_layers()
2269{
2270 return d->layers;
2271}
2272
2273#if NCNN_VULKAN
2274void Net::set_vulkan_device(int device_index)
2275{
2276 d->vkdev = get_gpu_device(device_index);
2277}
2278
2279void Net::set_vulkan_device(const VulkanDevice* _vkdev)
2280{
2281 d->vkdev = _vkdev;
2282}
2283
2284const VulkanDevice* Net::vulkan_device() const
2285{
2286 return d->vkdev;
2287}
2288#endif // NCNN_VULKAN
2289
2290#if NCNN_STRING
2291int Net::find_blob_index_by_name(const char* name) const
2292{
2293 for (size_t i = 0; i < d->blobs.size(); i++)
2294 {
2295 const Blob& blob = d->blobs[i];
2296 if (blob.name == name)
2297 {
2298 return static_cast<int>(i);
2299 }
2300 }
2301
2302 NCNN_LOGE("find_blob_index_by_name %s failed", name);
2303 return -1;
2304}
2305
2306int Net::find_layer_index_by_name(const char* name) const
2307{
2308 for (size_t i = 0; i < d->layers.size(); i++)
2309 {
2310 const Layer* layer = d->layers[i];
2311 if (layer->name == name)
2312 {
2313 return static_cast<int>(i);
2314 }
2315 }
2316
2317 NCNN_LOGE("find_layer_index_by_name %s failed", name);
2318 return -1;
2319}
2320
2321int Net::custom_layer_to_index(const char* type)
2322{
2323 const size_t custom_layer_registry_entry_count = d->custom_layer_registry.size();
2324 for (size_t i = 0; i < custom_layer_registry_entry_count; i++)
2325 {
2326 if (strcmp(type, d->custom_layer_registry[i].name) == 0)
2327 return static_cast<int>(i);
2328 }
2329
2330 return -1;
2331}
2332
2333Layer* Net::create_custom_layer(const char* type)
2334{
2335 int index = custom_layer_to_index(type);
2336 if (index == -1)
2337 return 0;
2338
2339 return create_custom_layer(index);
2340}
2341
2342Layer* Net::create_overwrite_builtin_layer(const char* type)
2343{
2344 int typeindex = layer_to_index(type);
2345 if (typeindex == -1)
2346 return 0;
2347
2348 return create_overwrite_builtin_layer(typeindex);
2349}
2350#endif // NCNN_STRING
2351
2352Layer* Net::create_custom_layer(int index)
2353{
2354 const size_t custom_layer_registry_entry_count = d->custom_layer_registry.size();
2355 if (index < 0 || static_cast<unsigned int>(index) >= custom_layer_registry_entry_count)
2356 return 0;
2357
2358 layer_creator_func layer_creator = d->custom_layer_registry[index].creator;
2359 if (!layer_creator)
2360 return 0;
2361
2362 Layer* layer = layer_creator(d->custom_layer_registry[index].userdata);
2363 layer->typeindex = ncnn::LayerType::CustomBit | index;
2364 return layer;
2365}
2366
2367Layer* Net::create_overwrite_builtin_layer(int typeindex)
2368{
2369 int index = -1;
2370 const size_t overwrite_builtin_layer_registry_entry_count = d->overwrite_builtin_layer_registry.size();
2371 for (size_t i = 0; i < overwrite_builtin_layer_registry_entry_count; i++)
2372 {
2373 if (d->overwrite_builtin_layer_registry[i].typeindex == typeindex)
2374 {
2375 index = i;
2376 break;
2377 }
2378 }
2379
2380 if (index == -1)
2381 return 0;
2382
2383 layer_creator_func layer_creator = d->overwrite_builtin_layer_registry[index].creator;
2384 if (!layer_creator)
2385 return 0;
2386
2387 Layer* layer = layer_creator(d->overwrite_builtin_layer_registry[index].userdata);
2388 layer->typeindex = typeindex;
2389 return layer;
2390}
2391
2392class ExtractorPrivate
2393{
2394public:
2395 ExtractorPrivate(const Net* _net)
2396 : net(_net)
2397 {
2398 }
2399 const Net* net;
2400 std::vector<Mat> blob_mats;
2401 Option opt;
2402
2403#if NCNN_VULKAN
2404 VkAllocator* local_blob_vkallocator;
2405 VkAllocator* local_staging_vkallocator;
2406
2407 std::vector<VkMat> blob_mats_gpu;
2408 std::vector<VkImageMat> blob_mats_gpu_image;
2409#endif // NCNN_VULKAN
2410};
2411
2412Extractor::Extractor(const Net* _net, size_t blob_count)
2413 : d(new ExtractorPrivate(_net))
2414{
2415 d->blob_mats.resize(blob_count);
2416 d->opt = d->net->opt;
2417
2418#if NCNN_VULKAN
2419 if (d->net->opt.use_vulkan_compute)
2420 {
2421 d->local_blob_vkallocator = 0;
2422 d->local_staging_vkallocator = 0;
2423
2424 d->blob_mats_gpu.resize(blob_count);
2425 d->blob_mats_gpu_image.resize(blob_count);
2426 }
2427#endif // NCNN_VULKAN
2428}
2429
2430Extractor::~Extractor()
2431{
2432 clear();
2433
2434 delete d;
2435}
2436
2437Extractor::Extractor(const Extractor& rhs)
2438 : d(new ExtractorPrivate(0))
2439{
2440 d->net = rhs.d->net;
2441 d->blob_mats = rhs.d->blob_mats;
2442 d->opt = rhs.d->opt;
2443
2444#if NCNN_VULKAN
2445 d->local_blob_vkallocator = 0;
2446 d->local_staging_vkallocator = 0;
2447
2448 d->blob_mats_gpu = rhs.d->blob_mats_gpu;
2449 d->blob_mats_gpu_image = rhs.d->blob_mats_gpu_image;
2450#endif // NCNN_VULKAN
2451}
2452
2453Extractor& Extractor::operator=(const Extractor& rhs)
2454{
2455 if (this == &rhs)
2456 return *this;
2457
2458 d->net = rhs.d->net;
2459 d->blob_mats = rhs.d->blob_mats;
2460 d->opt = rhs.d->opt;
2461
2462#if NCNN_VULKAN
2463 d->local_blob_vkallocator = 0;
2464 d->local_staging_vkallocator = 0;
2465
2466 d->blob_mats_gpu = rhs.d->blob_mats_gpu;
2467 d->blob_mats_gpu_image = rhs.d->blob_mats_gpu_image;
2468#endif // NCNN_VULKAN
2469
2470 return *this;
2471}
2472
2473void Extractor::clear()
2474{
2475 d->blob_mats.clear();
2476
2477#if NCNN_VULKAN
2478 if (d->opt.use_vulkan_compute)
2479 {
2480 d->blob_mats_gpu.clear();
2481 d->blob_mats_gpu_image.clear();
2482
2483 if (d->local_blob_vkallocator)
2484 {
2485 d->net->vulkan_device()->reclaim_blob_allocator(d->local_blob_vkallocator);
2486 }
2487 if (d->local_staging_vkallocator)
2488 {
2489 d->net->vulkan_device()->reclaim_staging_allocator(d->local_staging_vkallocator);
2490 }
2491 }
2492#endif // NCNN_VULKAN
2493}
2494
2495void Extractor::set_light_mode(bool enable)
2496{
2497 d->opt.lightmode = enable;
2498}
2499
2500void Extractor::set_num_threads(int num_threads)
2501{
2502 NCNN_LOGE("ex.set_num_threads() is no-op, please set net.opt.num_threads=N before net.load_param()");
2503 NCNN_LOGE("If you want to use single thread for only some layer, see https://github.com/Tencent/ncnn/wiki/layer-feat-mask");
2504}
2505
2506void Extractor::set_blob_allocator(Allocator* allocator)
2507{
2508 d->opt.blob_allocator = allocator;
2509}
2510
2511void Extractor::set_workspace_allocator(Allocator* allocator)
2512{
2513 d->opt.workspace_allocator = allocator;
2514}
2515
2516#if NCNN_VULKAN
2517void Extractor::set_vulkan_compute(bool enable)
2518{
2519 NCNN_LOGE("ex.set_vulkan_compute() is no-op, please set net.opt.use_vulkan_compute=true/false before net.load_param()");
2520 NCNN_LOGE("If you want to disable vulkan for only some layer, see https://github.com/Tencent/ncnn/wiki/layer-feat-mask");
2521}
2522
2523void Extractor::set_blob_vkallocator(VkAllocator* allocator)
2524{
2525 d->opt.blob_vkallocator = allocator;
2526}
2527
2528void Extractor::set_workspace_vkallocator(VkAllocator* allocator)
2529{
2530 d->opt.workspace_vkallocator = allocator;
2531}
2532
2533void Extractor::set_staging_vkallocator(VkAllocator* allocator)
2534{
2535 d->opt.staging_vkallocator = allocator;
2536}
2537#endif // NCNN_VULKAN
2538
2539#if NCNN_STRING
2540int Extractor::input(const char* blob_name, const Mat& in)
2541{
2542 int blob_index = d->net->find_blob_index_by_name(blob_name);
2543 if (blob_index == -1)
2544 {
2545 NCNN_LOGE("Try");
2546 const std::vector<const char*>& input_names = d->net->input_names();
2547 for (size_t i = 0; i < input_names.size(); i++)
2548 {
2549 NCNN_LOGE(" ex.input(\"%s\", in%d);", input_names[i], (int)i);
2550 }
2551
2552 return -1;
2553 }
2554
2555 return input(blob_index, in);
2556}
2557
2558int Extractor::extract(const char* blob_name, Mat& feat, int type)
2559{
2560 int blob_index = d->net->find_blob_index_by_name(blob_name);
2561 if (blob_index == -1)
2562 {
2563 NCNN_LOGE("Try");
2564 const std::vector<const char*>& output_names = d->net->output_names();
2565 for (size_t i = 0; i < output_names.size(); i++)
2566 {
2567 NCNN_LOGE(" ex.extract(\"%s\", out%d);", output_names[i], (int)i);
2568 }
2569
2570 return -1;
2571 }
2572
2573 return extract(blob_index, feat, type);
2574}
2575#endif // NCNN_STRING
2576
2577int Extractor::input(int blob_index, const Mat& in)
2578{
2579 if (blob_index < 0 || blob_index >= (int)d->blob_mats.size())
2580 return -1;
2581
2582 d->blob_mats[blob_index] = in;
2583
2584 return 0;
2585}
2586
2587int Extractor::extract(int blob_index, Mat& feat, int type)
2588{
2589 if (blob_index < 0 || blob_index >= (int)d->blob_mats.size())
2590 return -1;
2591
2592 int old_blocktime = get_kmp_blocktime();
2593 set_kmp_blocktime(d->opt.openmp_blocktime);
2594
2595 int old_flush_denormals = get_flush_denormals();
2596 set_flush_denormals(d->opt.flush_denormals);
2597
2598 int ret = 0;
2599
2600 if (d->blob_mats[blob_index].dims == 0)
2601 {
2602 int layer_index = d->net->blobs()[blob_index].producer;
2603
2604 // use local allocator
2605 if (d->opt.use_local_pool_allocator)
2606 {
2607 if (!d->opt.blob_allocator)
2608 {
2609 d->opt.blob_allocator = d->net->d->local_blob_allocator;
2610 }
2611 if (!d->opt.workspace_allocator)
2612 {
2613 d->opt.workspace_allocator = d->net->d->local_workspace_allocator;
2614 }
2615 }
2616
2617#if NCNN_VULKAN
2618 if (d->opt.use_vulkan_compute)
2619 {
2620 // use local allocator
2621 if (!d->opt.blob_vkallocator)
2622 {
2623 d->local_blob_vkallocator = d->net->vulkan_device()->acquire_blob_allocator();
2624 d->opt.blob_vkallocator = d->local_blob_vkallocator;
2625 }
2626 if (!d->opt.workspace_vkallocator)
2627 {
2628 d->opt.workspace_vkallocator = d->opt.blob_vkallocator;
2629 }
2630 if (!d->opt.staging_vkallocator)
2631 {
2632 d->local_staging_vkallocator = d->net->vulkan_device()->acquire_staging_allocator();
2633 d->opt.staging_vkallocator = d->local_staging_vkallocator;
2634 }
2635
2636 ncnn::VkCompute cmd(d->net->vulkan_device());
2637#if NCNN_BENCHMARK
2638 cmd.create_query_pool(d->net->layers().size() * 2);
2639#endif // NCNN_BENCHMARK
2640
2641 // TODO vkimagemat for adreno
2642 if (d->opt.use_image_storage)
2643 {
2644 VkImageMat feat_gpu;
2645 ret = extract(blob_index, feat_gpu, cmd);
2646
2647 if (ret == 0 && d->blob_mats[blob_index].dims == 0 && feat_gpu.dims != 0)
2648 {
2649 cmd.record_download(feat_gpu, d->blob_mats[blob_index], d->opt);
2650
2651 ret = cmd.submit_and_wait();
2652
2653#if NCNN_BENCHMARK
2654 std::vector<uint64_t> results(d->net->layers().size() * 2);
2655 cmd.get_query_pool_results(0, d->net->layers().size() * 2, results);
2656 for (size_t i = 0; i < d->net->layers().size(); i++)
2657 {
2658 uint64_t start = results[i * 2];
2659 uint64_t end = results[i * 2 + 1];
2660 if (start == 0 || end == 0)
2661 continue;
2662
2663 double duration_us = (end - start) * d->net->vulkan_device()->info.timestamp_period() / 1000;
2664 NCNN_LOGE("%-24s %-30s %8.2lfus |", d->net->layers()[i]->type.c_str(), d->net->layers()[i]->name.c_str(), duration_us);
2665 }
2666#endif // NCNN_BENCHMARK
2667 }
2668 }
2669 else
2670 {
2671 VkMat feat_gpu;
2672 ret = extract(blob_index, feat_gpu, cmd);
2673
2674 if (ret == 0 && d->blob_mats[blob_index].dims == 0 && feat_gpu.dims != 0)
2675 {
2676 cmd.record_download(feat_gpu, d->blob_mats[blob_index], d->opt);
2677
2678 ret = cmd.submit_and_wait();
2679
2680#if NCNN_BENCHMARK
2681 std::vector<uint64_t> results(d->net->layers().size() * 2);
2682 cmd.get_query_pool_results(0, d->net->layers().size() * 2, results);
2683 for (size_t i = 0; i < d->net->layers().size(); i++)
2684 {
2685 uint64_t start = results[i * 2];
2686 uint64_t end = results[i * 2 + 1];
2687 if (start == 0 || end == 0)
2688 continue;
2689
2690 double duration_us = (end - start) * d->net->vulkan_device()->info.timestamp_period() / 1000;
2691 NCNN_LOGE("%-24s %-30s %8.2lfus |", d->net->layers()[i]->type.c_str(), d->net->layers()[i]->name.c_str(), duration_us);
2692 }
2693#endif // NCNN_BENCHMARK
2694 }
2695 }
2696 }
2697 else
2698 {
2699 ret = d->net->d->forward_layer(layer_index, d->blob_mats, d->opt);
2700 }
2701#else
2702 ret = d->net->d->forward_layer(layer_index, d->blob_mats, d->opt);
2703#endif // NCNN_VULKAN
2704 }
2705
2706 feat = d->blob_mats[blob_index];
2707
2708 // empty is valid for outputs
2709 if (!feat.empty())
2710 {
2711 if (d->opt.use_packing_layout && (type == 0) && feat.elempack != 1)
2712 {
2713 Mat bottom_blob_unpacked;
2714 convert_packing(feat, bottom_blob_unpacked, 1, d->opt);
2715 feat = bottom_blob_unpacked;
2716 if (feat.empty())
2717 return -100;
2718 }
2719
2720 // clang-format off
2721 // *INDENT-OFF*
2722#if NCNN_VFPV4
2723 if (d->opt.use_fp16_storage && cpu_support_arm_vfpv4() && (type == 0))
2724 {
2725 if (feat.elembits() == 16)
2726 {
2727 Mat feat_fp32;
2728 cast_float16_to_float32(feat, feat_fp32, d->opt);
2729 feat = feat_fp32;
2730 }
2731 }
2732 else
2733#endif // NCNN_VFPV4
2734#if NCNN_RVV
2735 if (d->opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && (type == 0))
2736 {
2737 if (feat.elembits() == 16)
2738 {
2739 Mat feat_fp32;
2740 cast_float16_to_float32(feat, feat_fp32, d->opt);
2741 feat = feat_fp32;
2742 }
2743 }
2744 else
2745#endif // NCNN_RVV
2746#if NCNN_BF16
2747 if (d->opt.use_bf16_storage && (type == 0))
2748 {
2749 if (feat.elembits() == 16)
2750 {
2751 Mat feat_fp32;
2752 cast_bfloat16_to_float32(feat, feat_fp32, d->opt);
2753 feat = feat_fp32;
2754 }
2755 }
2756 else
2757#endif // NCNN_BF16
2758 if (feat.elembits() == 8 && (type == 0))
2759 {
2760 Mat feat_fp32;
2761 cast_int8_to_float32(feat, feat_fp32, d->opt);
2762 feat = feat_fp32;
2763 }
2764 // *INDENT-ON*
2765 // clang-format on
2766 if (feat.empty())
2767 return -100;
2768
2769 if (d->opt.use_local_pool_allocator && feat.allocator == d->net->d->local_blob_allocator)
2770 {
2771 // detach the returned mat from local pool allocator
2772 // so we could destroy net instance much earlier
2773 feat = feat.clone();
2774 if (feat.empty())
2775 return -100;
2776 }
2777 }
2778
2779 set_kmp_blocktime(old_blocktime);
2780 set_flush_denormals(old_flush_denormals);
2781
2782 return ret;
2783}
2784
2785#if NCNN_VULKAN
2786#if NCNN_STRING
2787int Extractor::input(const char* blob_name, const VkMat& in)
2788{
2789 int blob_index = d->net->find_blob_index_by_name(blob_name);
2790 if (blob_index == -1)
2791 {
2792 NCNN_LOGE("Try");
2793 const std::vector<const char*>& input_names = d->net->input_names();
2794 for (size_t i = 0; i < input_names.size(); i++)
2795 {
2796 NCNN_LOGE(" ex.input(\"%s\", in%d);", input_names[i], (int)i);
2797 }
2798
2799 return -1;
2800 }
2801
2802 return input(blob_index, in);
2803}
2804
2805int Extractor::extract(const char* blob_name, VkMat& feat, VkCompute& cmd)
2806{
2807 int blob_index = d->net->find_blob_index_by_name(blob_name);
2808 if (blob_index == -1)
2809 {
2810 NCNN_LOGE("Try");
2811 const std::vector<const char*>& output_names = d->net->output_names();
2812 for (size_t i = 0; i < output_names.size(); i++)
2813 {
2814 NCNN_LOGE(" ex.extract(\"%s\", out%d);", output_names[i], (int)i);
2815 }
2816
2817 return -1;
2818 }
2819
2820 return extract(blob_index, feat, cmd);
2821}
2822
2823int Extractor::input(const char* blob_name, const VkImageMat& in)
2824{
2825 int blob_index = d->net->find_blob_index_by_name(blob_name);
2826 if (blob_index == -1)
2827 {
2828 NCNN_LOGE("Try");
2829 const std::vector<const char*>& input_names = d->net->input_names();
2830 for (size_t i = 0; i < input_names.size(); i++)
2831 {
2832 NCNN_LOGE(" ex.input(\"%s\", in%d);", input_names[i], (int)i);
2833 }
2834
2835 return -1;
2836 }
2837
2838 return input(blob_index, in);
2839}
2840
2841int Extractor::extract(const char* blob_name, VkImageMat& feat, VkCompute& cmd)
2842{
2843 int blob_index = d->net->find_blob_index_by_name(blob_name);
2844 if (blob_index == -1)
2845 {
2846 NCNN_LOGE("Try");
2847 const std::vector<const char*>& output_names = d->net->output_names();
2848 for (size_t i = 0; i < output_names.size(); i++)
2849 {
2850 NCNN_LOGE(" ex.extract(\"%s\", out%d);", output_names[i], (int)i);
2851 }
2852
2853 return -1;
2854 }
2855
2856 return extract(blob_index, feat, cmd);
2857}
2858#endif // NCNN_STRING
2859
2860int Extractor::input(int blob_index, const VkMat& in)
2861{
2862 if (blob_index < 0 || blob_index >= (int)d->blob_mats.size())
2863 return -1;
2864
2865 d->blob_mats_gpu[blob_index] = in;
2866
2867 return 0;
2868}
2869
2870int Extractor::extract(int blob_index, VkMat& feat, VkCompute& cmd)
2871{
2872 if (blob_index < 0 || blob_index >= (int)d->blob_mats.size())
2873 return -1;
2874
2875 int old_blocktime = get_kmp_blocktime();
2876 set_kmp_blocktime(d->opt.openmp_blocktime);
2877
2878 int old_flush_denormals = get_flush_denormals();
2879 set_flush_denormals(d->opt.flush_denormals);
2880
2881 int ret = 0;
2882
2883 if (d->blob_mats_gpu[blob_index].dims == 0)
2884 {
2885 if (d->blob_mats_gpu_image[blob_index].dims != 0)
2886 {
2887 // image to buffer
2888 cmd.record_image_to_buffer(d->blob_mats_gpu_image[blob_index], d->blob_mats_gpu[blob_index], d->opt);
2889 }
2890 else if (d->blob_mats[blob_index].dims != 0)
2891 {
2892 // host to buffer
2893 cmd.record_upload(d->blob_mats[blob_index], d->blob_mats_gpu[blob_index], d->opt);
2894 }
2895 else
2896 {
2897 int layer_index = d->net->blobs()[blob_index].producer;
2898 ret = d->net->d->forward_layer(layer_index, d->blob_mats, d->blob_mats_gpu, cmd, d->opt);
2899 }
2900 }
2901
2902 feat = d->blob_mats_gpu[blob_index];
2903
2904 set_kmp_blocktime(old_blocktime);
2905 set_flush_denormals(old_flush_denormals);
2906
2907 return ret;
2908}
2909
2910int Extractor::input(int blob_index, const VkImageMat& in)
2911{
2912 if (blob_index < 0 || blob_index >= (int)d->blob_mats.size())
2913 return -1;
2914
2915 d->blob_mats_gpu_image[blob_index] = in;
2916
2917 return 0;
2918}
2919
2920int Extractor::extract(int blob_index, VkImageMat& feat, VkCompute& cmd)
2921{
2922 if (blob_index < 0 || blob_index >= (int)d->blob_mats.size())
2923 return -1;
2924
2925 int old_blocktime = get_kmp_blocktime();
2926 set_kmp_blocktime(d->opt.openmp_blocktime);
2927
2928 int old_flush_denormals = get_flush_denormals();
2929 set_flush_denormals(d->opt.flush_denormals);
2930
2931 int ret = 0;
2932
2933 if (d->blob_mats_gpu_image[blob_index].dims == 0)
2934 {
2935 if (d->blob_mats_gpu[blob_index].dims != 0)
2936 {
2937 // buffer to image
2938 cmd.record_buffer_to_image(d->blob_mats_gpu[blob_index], d->blob_mats_gpu_image[blob_index], d->opt);
2939 }
2940 else if (d->blob_mats[blob_index].dims != 0)
2941 {
2942 // host to image
2943 cmd.record_upload(d->blob_mats[blob_index], d->blob_mats_gpu_image[blob_index], d->opt);
2944 }
2945 else
2946 {
2947 int layer_index = d->net->blobs()[blob_index].producer;
2948 ret = d->net->d->forward_layer(layer_index, d->blob_mats, d->blob_mats_gpu, d->blob_mats_gpu_image, cmd, d->opt);
2949 }
2950 }
2951
2952 feat = d->blob_mats_gpu_image[blob_index];
2953
2954 if (feat.empty())
2955 {
2956 NCNN_LOGE("extract %d image allocation failed", blob_index);
2957 ret = -100;
2958 }
2959
2960 set_kmp_blocktime(old_blocktime);
2961 set_flush_denormals(old_flush_denormals);
2962
2963 return ret;
2964}
2965#endif // NCNN_VULKAN
2966
2967} // namespace ncnn