From 056f59244347140822e2aa5b7d567425ed7464b8 Mon Sep 17 00:00:00 2001 From: nihuini Date: Sun, 7 Apr 2024 17:35:44 +0800 Subject: [PATCH 1/4] fix create_pipeline crash in vulkan-enabled layer without calling load_param/load_model first --- src/layer.cpp | 54 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/src/layer.cpp b/src/layer.cpp index cca3e77bf1fe..3dc0ac4d452d 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -320,18 +320,22 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { + int ret = 0; if (vkdev) { - int ret = layer_vulkan->load_param(pd); + ret = layer_vulkan->load_param(pd); get_layer_properties(); + } - if (layer_vulkan->support_vulkan) - return ret; + if (!support_vulkan || !vkdev) + { + // fallback to cpu layer + delete layer_vulkan; + layer_vulkan = 0; } - // fallback to cpu layer - delete layer_vulkan; - layer_vulkan = 0; + if (ret) + return ret; } #endif // NCNN_VULKAN @@ -345,9 +349,22 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - int ret = layer_vulkan->load_model(mb); - get_layer_properties(); - return ret; + int ret = 0; + if (vkdev) + { + ret = layer_vulkan->load_model(mb); + get_layer_properties(); + } + + if (!support_vulkan || !vkdev) + { + // fallback to cpu layer + delete layer_vulkan; + layer_vulkan = 0; + } + + if (ret) + return ret; } #endif // NCNN_VULKAN @@ -362,9 +379,22 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - int ret = layer_vulkan->create_pipeline(opt); - get_layer_properties(); - return ret; + int ret = 0; + if (vkdev) + { + ret = layer_vulkan->create_pipeline(opt); + get_layer_properties(); + } + + if (!support_vulkan || !vkdev) + { + // fallback to cpu layer + delete layer_vulkan; + layer_vulkan = 0; + } + + if (ret) + return ret; } #endif // NCNN_VULKAN From e5e7f10588093b421b70b3601e7962e740ba3907 Mon Sep 17 00:00:00 2001 From: nihuini Date: Sun, 7 Apr 2024 19:39:15 +0800 Subject: [PATCH 2/4] guard destroy_pipeline --- src/layer.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/layer.cpp b/src/layer.cpp index 3dc0ac4d452d..e98aeaea07c0 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -408,7 +408,21 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - return layer_vulkan->destroy_pipeline(opt); + int ret = 0; + if (vkdev) + { + ret = layer_vulkan->destroy_pipeline(opt); + } + + if (!support_vulkan || !vkdev) + { + // fallback to cpu layer + delete layer_vulkan; + layer_vulkan = 0; + } + + if (ret) + return ret; } #endif // NCNN_VULKAN From d61ea939e9a9c3f1a4e1e0b4ca0ccef7dc57a475 Mon Sep 17 00:00:00 2001 From: nihuini Date: Mon, 8 Apr 2024 15:37:58 +0800 Subject: [PATCH 3/4] wip --- src/layer.cpp | 74 +++++++++++++++------------------------------------ 1 file changed, 22 insertions(+), 52 deletions(-) diff --git a/src/layer.cpp b/src/layer.cpp index e98aeaea07c0..42e36f54b328 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -290,6 +290,13 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { + one_blob_only = layer_vulkan->one_blob_only; + support_inplace = layer_vulkan->support_inplace; + support_packing = layer_vulkan->support_packing; + support_bf16_storage = layer_vulkan->support_bf16_storage; + support_fp16_storage = layer_vulkan->support_fp16_storage; + support_int8_storage = layer_vulkan->support_int8_storage; + support_vulkan = layer_vulkan->support_vulkan; support_image_storage = layer_vulkan->support_image_storage; support_tensor_storage = layer_vulkan->support_tensor_storage; @@ -320,22 +327,18 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - int ret = 0; if (vkdev) { - ret = layer_vulkan->load_param(pd); + int ret = layer_vulkan->load_param(pd); get_layer_properties(); - } - if (!support_vulkan || !vkdev) - { - // fallback to cpu layer - delete layer_vulkan; - layer_vulkan = 0; + if (layer_vulkan->support_vulkan) + return ret; } - if (ret) - return ret; + // fallback to cpu layer + delete layer_vulkan; + layer_vulkan = 0; } #endif // NCNN_VULKAN @@ -349,22 +352,9 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - int ret = 0; - if (vkdev) - { - ret = layer_vulkan->load_model(mb); - get_layer_properties(); - } - - if (!support_vulkan || !vkdev) - { - // fallback to cpu layer - delete layer_vulkan; - layer_vulkan = 0; - } - - if (ret) - return ret; + int ret = layer_vulkan->load_model(mb); + get_layer_properties(); + return ret; } #endif // NCNN_VULKAN @@ -379,22 +369,16 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - int ret = 0; if (vkdev) { - ret = layer_vulkan->create_pipeline(opt); + int ret = layer_vulkan->create_pipeline(opt); get_layer_properties(); + return ret; } - if (!support_vulkan || !vkdev) - { - // fallback to cpu layer - delete layer_vulkan; - layer_vulkan = 0; - } - - if (ret) - return ret; + // fallback to cpu layer + delete layer_vulkan; + layer_vulkan = 0; } #endif // NCNN_VULKAN @@ -408,21 +392,7 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - int ret = 0; - if (vkdev) - { - ret = layer_vulkan->destroy_pipeline(opt); - } - - if (!support_vulkan || !vkdev) - { - // fallback to cpu layer - delete layer_vulkan; - layer_vulkan = 0; - } - - if (ret) - return ret; + return layer_vulkan->destroy_pipeline(opt); } #endif // NCNN_VULKAN From 7c09a1b1d35c65bc3b53974393be96bc169cc149 Mon Sep 17 00:00:00 2001 From: nihuini Date: Mon, 8 Apr 2024 15:39:58 +0800 Subject: [PATCH 4/4] wip --- src/layer.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/layer.cpp b/src/layer.cpp index 42e36f54b328..3c40cbd67ca2 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -290,13 +290,6 @@ class Layer_final : public Layer #if NCNN_VULKAN if (layer_vulkan) { - one_blob_only = layer_vulkan->one_blob_only; - support_inplace = layer_vulkan->support_inplace; - support_packing = layer_vulkan->support_packing; - support_bf16_storage = layer_vulkan->support_bf16_storage; - support_fp16_storage = layer_vulkan->support_fp16_storage; - support_int8_storage = layer_vulkan->support_int8_storage; - support_vulkan = layer_vulkan->support_vulkan; support_image_storage = layer_vulkan->support_image_storage; support_tensor_storage = layer_vulkan->support_tensor_storage;