From 8dbcfee5ecf632cbd151c0c79b4b73a267952db6 Mon Sep 17 00:00:00 2001 From: nihui Date: Thu, 3 Apr 2025 16:34:00 +0800 Subject: [PATCH] option owns vulkan device index (#5973) --- src/gpu.cpp | 41 +++++++++++++++++++++++++++-------------- src/gpu.h | 4 ++++ src/net.cpp | 20 ++++++++++++++++++-- src/option.cpp | 19 +++++++++++-------- src/option.h | 20 ++++++++++++-------- 5 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/gpu.cpp b/src/gpu.cpp index 9ae04525e..79a1ca752 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -266,6 +266,8 @@ public: void query_extension_properties(); public: + int device_index; + // physical device VkPhysicalDevice physicalDevice; @@ -1141,11 +1143,21 @@ GpuInfo& GpuInfo::operator=(const GpuInfo&) return *this; } +int GpuInfo::device_index() const +{ + return d->device_index; +} + VkPhysicalDevice GpuInfo::physicalDevice() const { return d->physicalDevice; } +VkPhysicalDevice GpuInfo::physical_device() const +{ + return d->physicalDevice; +} + const VkPhysicalDeviceFeatures& GpuInfo::physicalDevicefeatures() const { return d->physicalDevicefeatures; @@ -1161,6 +1173,11 @@ const VkPhysicalDeviceMemoryProperties& GpuInfo::physicalDeviceMemoryProperties( return d->physicalDeviceMemoryProperties; } +const VkPhysicalDeviceMemoryProperties& GpuInfo::physical_device_memory_properties() const +{ + return d->physicalDeviceMemoryProperties; +} + const std::vector& GpuInfo::deviceExtensionProperties() const { return d->deviceExtensionProperties; @@ -2209,10 +2226,11 @@ int create_gpu_instance(const char* driver_path) const VkPhysicalDevice& physicalDevice = physicalDevices[i]; delete g_gpu_infos[gpu_info_index]; g_gpu_infos[gpu_info_index] = new GpuInfo; - // GpuInfoPrivate& gpu_info = *(g_gpu_infos[gpu_info_index]->d); GpuInfo& gpu_info = *g_gpu_infos[gpu_info_index]; + gpu_info.d->device_index = gpu_info_index; + gpu_info.d->physicalDevice = physicalDevice; gpu_info.d->query_features(); @@ -2591,12 +2609,11 @@ const ncnn::Packing_vulkan* VulkanDevicePrivate::get_utility_operator(int storag opt.use_vulkan_compute = true; - opt.blob_vkallocator = opt.workspace_vkallocator = vkdev->acquire_blob_allocator(); - opt.staging_vkallocator = vkdev->acquire_staging_allocator(); - // cache uop pipeline as device member explicitly opt.pipeline_cache = 0; + opt.vulkan_device_index = vkdev->info.device_index(); + ncnn::Packing_vulkan* uop = new ncnn::Packing_vulkan; uop->vkdev = vkdev; @@ -2613,9 +2630,6 @@ const ncnn::Packing_vulkan* VulkanDevicePrivate::get_utility_operator(int storag uop_packing[storage_type_from][storage_type_to][cast_type_from_index][cast_type_to_index][packing_type_to_index] = uop; - vkdev->reclaim_blob_allocator(opt.blob_vkallocator); - vkdev->reclaim_staging_allocator(opt.staging_vkallocator); - return uop; } @@ -2627,6 +2641,7 @@ void VulkanDevicePrivate::destroy_utility_operator() opt.use_int8_arithmetic = false; opt.use_cooperative_matrix = false; opt.pipeline_cache = 0; + opt.vulkan_device_index = vkdev->info.device_index(); // from buffer | image // to buffer | image @@ -4576,11 +4591,13 @@ int compile_spirv_module(const char* comp_data, int comp_data_size, const Option bool support_shader_int64 = false; - if (opt.blob_vkallocator) + // fill device macros { - const VulkanDevice* vkdev = opt.blob_vkallocator->vkdev; + int device_index = opt.vulkan_device_index; + if (device_index < 0 || device_index >= get_gpu_count()) + device_index = get_default_gpu_index(); - const GpuInfo& info = vkdev->info; + const GpuInfo& info = get_gpu_info(device_index); support_shader_int64 = info.physicalDevicefeatures().shaderInt64; @@ -4935,10 +4952,6 @@ int compile_spirv_module(const char* comp_data, int comp_data_size, const Option #undef DD_APPEND_PROPERTY } - else - { - NCNN_LOGE("opt.blob_vkallocator is null"); - } std::string define_macro_data; diff --git a/src/gpu.h b/src/gpu.h index 24e696645..b9541c01f 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -193,8 +193,11 @@ public: explicit GpuInfo(); virtual ~GpuInfo(); + int device_index() const; + // vulkan physical device VkPhysicalDevice physicalDevice() const; + VkPhysicalDevice physical_device() const; // api compatibility // features const VkPhysicalDeviceFeatures& physicalDevicefeatures() const; @@ -204,6 +207,7 @@ public: // memory properties const VkPhysicalDeviceMemoryProperties& physicalDeviceMemoryProperties() const; + const VkPhysicalDeviceMemoryProperties& physical_device_memory_properties() const; // api compatibility // extension properties const std::vector& deviceExtensionProperties() const; diff --git a/src/net.cpp b/src/net.cpp index 793fdc825..1a130c56c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1380,7 +1380,14 @@ int Net::load_param(const DataReader& dr) if (opt.use_vulkan_compute) { - if (!d->vkdev) d->vkdev = get_gpu_device(); + if (!d->vkdev) + { + int device_index = opt.vulkan_device_index; + if (device_index < 0 || device_index >= get_gpu_count()) + device_index = get_default_gpu_index(); + + d->vkdev = get_gpu_device(device_index); + } if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu } if (opt.use_vulkan_compute) @@ -1683,7 +1690,14 @@ int Net::load_param_bin(const DataReader& dr) if (opt.use_vulkan_compute) { - if (!d->vkdev) d->vkdev = get_gpu_device(); + if (!d->vkdev) + { + int device_index = opt.vulkan_device_index; + if (device_index < 0 || device_index >= get_gpu_count()) + device_index = get_default_gpu_index(); + + d->vkdev = get_gpu_device(device_index); + } if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu } if (opt.use_vulkan_compute) @@ -2305,11 +2319,13 @@ std::vector& Net::mutable_layers() #if NCNN_VULKAN void Net::set_vulkan_device(int device_index) { + opt.vulkan_device_index = device_index; d->vkdev = get_gpu_device(device_index); } void Net::set_vulkan_device(const VulkanDevice* _vkdev) { + opt.vulkan_device_index = _vkdev->info.device_index(); d->vkdev = _vkdev; } diff --git a/src/option.cpp b/src/option.cpp index d807c1aa9..645f195cb 100644 --- a/src/option.cpp +++ b/src/option.cpp @@ -21,6 +21,10 @@ namespace ncnn { Option::Option() { lightmode = true; + use_shader_pack8 = false; + use_subgroup_ops = false; + use_reserved_0 = false; + num_threads = get_physical_big_cpu_count(); blob_allocator = 0; workspace_allocator = 0; @@ -50,18 +54,13 @@ Option::Option() use_packing_layout = true; - use_shader_pack8 = false; - - use_subgroup_ops = false; - - use_subgroup_reserved_1 = false; - use_subgroup_reserved_2 = false; - use_subgroup_reserved_3 = false; + vulkan_device_index = -1; + use_reserved_1 = false; use_image_storage = false; use_tensor_storage = false; - use_reserved_0 = false; + use_reserved_2 = false; flush_denormals = 3; @@ -78,6 +77,10 @@ Option::Option() use_fp16_uniform = true; use_int8_uniform = true; + + use_reserved_9 = false; + use_reserved_10 = false; + use_reserved_11 = false; } } // namespace ncnn diff --git a/src/option.h b/src/option.h index cd4d4416c..50bb944ad 100644 --- a/src/option.h +++ b/src/option.h @@ -37,6 +37,14 @@ public: // enabled by default bool lightmode; + // use pack8 shader + bool use_shader_pack8; + + // enable subgroup in shader + bool use_subgroup_ops; + + bool use_reserved_0; + // thread count // default value is the one returned by get_cpu_count() int num_threads; @@ -105,20 +113,16 @@ public: // enabled by default bool use_packing_layout; - bool use_shader_pack8; + // the vulkan device + int vulkan_device_index; - // subgroup option - bool use_subgroup_ops; - - bool use_subgroup_reserved_1; - bool use_subgroup_reserved_2; - bool use_subgroup_reserved_3; + bool use_reserved_1; // turn on for adreno bool use_image_storage; bool use_tensor_storage; - bool use_reserved_0; + bool use_reserved_2; // enable DAZ(Denormals-Are-Zero) and FTZ(Flush-To-Zero) // default value is 3