From a9e8dc4073c609f93fe2bc37f570b2baf27c30a6 Mon Sep 17 00:00:00 2001 From: Adriankhl Date: Thu, 20 Jun 2024 08:43:33 +0800 Subject: [PATCH 1/3] vulkan: detect multiple devices by deviceUUID instead of deviceID --- ggml-vulkan.cpp | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ggml-vulkan.cpp b/ggml-vulkan.cpp index f389934ead3ed..dbf173a27d34a 100644 --- a/ggml-vulkan.cpp +++ b/ggml-vulkan.cpp @@ -1745,31 +1745,37 @@ void ggml_vk_instance_init() { // Default to using all dedicated GPUs for (size_t i = 0; i < devices.size(); i++) { - vk::PhysicalDeviceProperties props = devices[i].getProperties(); - - if (props.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) { + vk::PhysicalDeviceProperties new_props_1 = devices[i].getProperties(); + vk::PhysicalDeviceProperties2 new_props_2; + vk::PhysicalDeviceDriverProperties new_driver; + vk::PhysicalDeviceIDProperties new_id; + new_props_2.pNext = &new_driver; + new_driver.pNext = &new_id; + devices[i].getProperties2(&new_props_2); + + if (new_props_1.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) { // Check if there are two physical devices corresponding to the same GPU auto old_device = std::find_if( vk_instance.device_indices.begin(), vk_instance.device_indices.end(), - [&devices, &props](const size_t k){ return devices[k].getProperties().deviceID == props.deviceID; } + [&devices, &new_id](const size_t k){ + vk::PhysicalDeviceProperties2 old_props_2; + vk::PhysicalDeviceIDProperties old_id; + devices[k].getProperties2(&old_props_2); + return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID)); + } ); if (old_device == vk_instance.device_indices.end()) { vk_instance.device_indices.push_back(i); } else { // There can be two physical devices corresponding to the same GPU if there are 2 different drivers // This can cause error when splitting layers aross the devices, need to keep only 1 - VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same device id"); + VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID"); - vk::PhysicalDeviceProperties2 old_prop; + vk::PhysicalDeviceProperties2 old_props_2; vk::PhysicalDeviceDriverProperties old_driver; - old_prop.pNext = &old_driver; - devices[*old_device].getProperties2(&old_prop); - - vk::PhysicalDeviceProperties2 new_prop; - vk::PhysicalDeviceDriverProperties new_driver; - new_prop.pNext = &new_driver; - devices[i].getProperties2(&new_prop); + old_props_2.pNext = &old_driver; + devices[*old_device].getProperties2(&old_props_2); std::map driver_priorities {}; int old_priority = std::numeric_limits::max(); @@ -1777,7 +1783,7 @@ void ggml_vk_instance_init() { // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id // Smaller number -> higher priority - switch (old_prop.properties.vendorID) { + switch (old_props_2.properties.vendorID) { case VK_VENDOR_ID_AMD: driver_priorities[vk::DriverId::eMesaRadv] = 1; driver_priorities[vk::DriverId::eAmdOpenSource] = 2; From 2f290b5cea500ae4ca7d2c69bf88c1fbf4d3d6fe Mon Sep 17 00:00:00 2001 From: Adriankhl Date: Thu, 20 Jun 2024 21:44:23 +0800 Subject: [PATCH 2/3] vulkan: remove unneeded variables --- ggml-vulkan.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/ggml-vulkan.cpp b/ggml-vulkan.cpp index dbf173a27d34a..57e88017d6181 100644 --- a/ggml-vulkan.cpp +++ b/ggml-vulkan.cpp @@ -1745,23 +1745,22 @@ void ggml_vk_instance_init() { // Default to using all dedicated GPUs for (size_t i = 0; i < devices.size(); i++) { - vk::PhysicalDeviceProperties new_props_1 = devices[i].getProperties(); - vk::PhysicalDeviceProperties2 new_props_2; + vk::PhysicalDeviceProperties2 new_props; vk::PhysicalDeviceDriverProperties new_driver; vk::PhysicalDeviceIDProperties new_id; - new_props_2.pNext = &new_driver; + new_props.pNext = &new_driver; new_driver.pNext = &new_id; - devices[i].getProperties2(&new_props_2); + devices[i].getProperties2(&new_props); - if (new_props_1.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) { + if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) { // Check if there are two physical devices corresponding to the same GPU auto old_device = std::find_if( vk_instance.device_indices.begin(), vk_instance.device_indices.end(), - [&devices, &new_id](const size_t k){ - vk::PhysicalDeviceProperties2 old_props_2; + [&devices, &new_id](const size_t k){ + vk::PhysicalDeviceProperties2 old_props; vk::PhysicalDeviceIDProperties old_id; - devices[k].getProperties2(&old_props_2); + devices[k].getProperties2(&old_props); return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID)); } ); @@ -1772,10 +1771,10 @@ void ggml_vk_instance_init() { // This can cause error when splitting layers aross the devices, need to keep only 1 VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID"); - vk::PhysicalDeviceProperties2 old_props_2; + vk::PhysicalDeviceProperties2 old_props; vk::PhysicalDeviceDriverProperties old_driver; - old_props_2.pNext = &old_driver; - devices[*old_device].getProperties2(&old_props_2); + old_props.pNext = &old_driver; + devices[*old_device].getProperties2(&old_props); std::map driver_priorities {}; int old_priority = std::numeric_limits::max(); @@ -1783,7 +1782,7 @@ void ggml_vk_instance_init() { // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id // Smaller number -> higher priority - switch (old_props_2.properties.vendorID) { + switch (old_props.properties.vendorID) { case VK_VENDOR_ID_AMD: driver_priorities[vk::DriverId::eMesaRadv] = 1; driver_priorities[vk::DriverId::eAmdOpenSource] = 2; From 733cb122a6d56d1c98f2b499b6edef1f831fa3ef Mon Sep 17 00:00:00 2001 From: Adriankhl Date: Fri, 21 Jun 2024 15:43:59 +0800 Subject: [PATCH 3/3] vulkan: fix id query --- ggml-vulkan.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ggml-vulkan.cpp b/ggml-vulkan.cpp index 57e88017d6181..87af33b563271 100644 --- a/ggml-vulkan.cpp +++ b/ggml-vulkan.cpp @@ -1760,6 +1760,7 @@ void ggml_vk_instance_init() { [&devices, &new_id](const size_t k){ vk::PhysicalDeviceProperties2 old_props; vk::PhysicalDeviceIDProperties old_id; + old_props.pNext = &old_id; devices[k].getProperties2(&old_props); return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID)); }