Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vulkan: Make Vulkan optional at runtime (#11493). #11494

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions ggml/src/ggml-vulkan/ggml-vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2765,8 +2765,6 @@ void ggml_vk_instance_init() {
}
VK_LOG_DEBUG("ggml_vk_instance_init()");

vk_instance_initialized = true;

uint32_t api_version = vk::enumerateInstanceVersion();

if (api_version < VK_API_VERSION_1_2) {
Expand Down Expand Up @@ -2817,6 +2815,7 @@ void ggml_vk_instance_init() {
GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
}
vk_instance.instance = vk::createInstance(instance_create_info);
vk_instance_initialized = true;

size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();

Expand All @@ -2841,7 +2840,7 @@ void ggml_vk_instance_init() {
// Make sure at least one device exists
if (devices.empty()) {
std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
GGML_ABORT("fatal error");
return;
}

// Default to using all dedicated GPUs
Expand Down Expand Up @@ -8305,8 +8304,13 @@ ggml_backend_reg_t ggml_backend_vk_reg() {
/* .iface = */ ggml_backend_vk_reg_i,
/* .context = */ nullptr,
};

return &reg;
try {
ggml_vk_instance_init();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This took me way too long to figure out)

I tested this by deleting the Vulkan driver on my system. I found that this change was working fine on windows in debug builds, but crashing in release builds. Turns out the problem is that the default exception settings are /EHsc (see https://learn.microsoft.com/en-us/cpp/build/reference/eh-exception-handling-model?view=msvc-170) and the c means "the compiler assumes that functions declared as extern "C" never throw a C++ exception." ggml_vk_instance_init is extern "C", so this whole try/catch is optimized away. I don't think ggml_vk_instance_init is actually used outside of ggml-vulkan anymore, so the easiest fix may just be to remove the forward declaration with GGML_BACKEND_API.

return &reg;
} catch (const vk::SystemError& e) {
VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
return nullptr;
}
}

// Extension availability
Expand Down
Loading