Skip to content

Commit 304ac56

Browse files
authored
Vulkan iGPU device selection overhaul and PCI ID API support (#15947)
* vulkan: implement ggml igpu device type, implement pci id support * fix compiler warning * prevent printf overflow warning
1 parent 6c88ad8 commit 304ac56

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4497,7 +4497,7 @@ static void ggml_vk_instance_init() {
44974497
new_driver.pNext = &new_id;
44984498
devices[i].getProperties2(&new_props);
44994499

4500-
if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
4500+
if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu || new_props.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu) {
45014501
// Check if there are two physical devices corresponding to the same GPU
45024502
auto old_device = std::find_if(
45034503
vk_instance.device_indices.begin(),
@@ -4567,7 +4567,7 @@ static void ggml_vk_instance_init() {
45674567
}
45684568
}
45694569

4570-
// If no dedicated GPUs found, fall back to the first non-CPU device.
4570+
// If no GPUs found, fall back to the first non-CPU device.
45714571
// If only CPU devices are available, return without devices.
45724572
if (vk_instance.device_indices.empty()) {
45734573
for (size_t i = 0; i < devices.size(); i++) {
@@ -12078,12 +12078,63 @@ void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total
1207812078
}
1207912079
}
1208012080

12081+
static vk::PhysicalDeviceType ggml_backend_vk_get_device_type(int device_idx) {
12082+
GGML_ASSERT(device_idx >= 0 && device_idx < (int) vk_instance.device_indices.size());
12083+
12084+
vk::PhysicalDevice device = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device_idx]];
12085+
12086+
vk::PhysicalDeviceProperties2 props = {};
12087+
device.getProperties2(&props);
12088+
12089+
return props.properties.deviceType;
12090+
}
12091+
12092+
static std::string ggml_backend_vk_get_device_pci_id(int device_idx) {
12093+
GGML_ASSERT(device_idx >= 0 && device_idx < (int) vk_instance.device_indices.size());
12094+
12095+
vk::PhysicalDevice device = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device_idx]];
12096+
12097+
const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
12098+
12099+
bool ext_support = false;
12100+
12101+
for (const auto& properties : ext_props) {
12102+
if (strcmp("VK_EXT_pci_bus_info", properties.extensionName) == 0) {
12103+
ext_support = true;
12104+
break;
12105+
}
12106+
}
12107+
12108+
if (!ext_support) {
12109+
return "";
12110+
}
12111+
12112+
vk::PhysicalDeviceProperties2 props = {};
12113+
vk::PhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info = {};
12114+
12115+
props.pNext = &pci_bus_info;
12116+
12117+
device.getProperties2(&props);
12118+
12119+
const uint32_t pci_domain = pci_bus_info.pciDomain;
12120+
const uint32_t pci_bus = pci_bus_info.pciBus;
12121+
const uint32_t pci_device = pci_bus_info.pciDevice;
12122+
const uint8_t pci_function = (uint8_t) pci_bus_info.pciFunction; // pci function is between 0 and 7, prevent printf overflow warning
12123+
12124+
char pci_bus_id[16] = {};
12125+
snprintf(pci_bus_id, sizeof(pci_bus_id), "%04x:%02x:%02x.%x", pci_domain, pci_bus, pci_device, pci_function);
12126+
12127+
return std::string(pci_bus_id);
12128+
}
12129+
1208112130
//////////////////////////
1208212131

1208312132
struct ggml_backend_vk_device_context {
1208412133
size_t device;
1208512134
std::string name;
1208612135
std::string description;
12136+
bool is_integrated_gpu;
12137+
std::string pci_bus_id;
1208712138
};
1208812139

1208912140
static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
@@ -12112,16 +12163,18 @@ static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(gg
1211212163
}
1211312164

1211412165
static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
12115-
UNUSED(dev);
12116-
// TODO: return GGML_BACKEND_DEVICE_TYPE_IGPU for integrated GPUs
12117-
return GGML_BACKEND_DEVICE_TYPE_GPU;
12166+
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
12167+
12168+
return ctx->is_integrated_gpu ? GGML_BACKEND_DEVICE_TYPE_IGPU : GGML_BACKEND_DEVICE_TYPE_GPU;
1211812169
}
1211912170

1212012171
static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
12172+
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
12173+
1212112174
props->name = ggml_backend_vk_device_get_name(dev);
1212212175
props->description = ggml_backend_vk_device_get_description(dev);
1212312176
props->type = ggml_backend_vk_device_get_type(dev);
12124-
// TODO: set props->device_id to PCI bus id
12177+
props->device_id = ctx->pci_bus_id.empty() ? nullptr : ctx->pci_bus_id.c_str();
1212512178
ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
1212612179
props->caps = {
1212712180
/* .async = */ false,
@@ -12388,8 +12441,8 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
1238812441
}
1238912442

1239012443
if (
12391-
src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_I32 ||
12392-
src0_type == GGML_TYPE_I32 && src1_type == GGML_TYPE_F32
12444+
(src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_I32) ||
12445+
(src0_type == GGML_TYPE_I32 && src1_type == GGML_TYPE_F32)
1239312446
) {
1239412447
return true;
1239512448
}
@@ -12554,6 +12607,8 @@ static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg,
1255412607
ctx->device = i;
1255512608
ctx->name = GGML_VK_NAME + std::to_string(i);
1255612609
ctx->description = desc;
12610+
ctx->is_integrated_gpu = ggml_backend_vk_get_device_type(i) == vk::PhysicalDeviceType::eIntegratedGpu;
12611+
ctx->pci_bus_id = ggml_backend_vk_get_device_pci_id(i);
1255712612
devices.push_back(new ggml_backend_device {
1255812613
/* .iface = */ ggml_backend_vk_device_i,
1255912614
/* .reg = */ reg,

0 commit comments

Comments
 (0)