Skip to content

Commit 478f30b

Browse files
committed
ggml-vulkan: serialize racy vk_instance initialization
When calling whisper_init_from_file_with_params_no_state() from multiple threads with the Vulkan backend enabled, segfaults abound. The problem seems to be that vk_instance initialization is not serialized properly. Introduce a mutex to fix the problem. It needs to be recursive because of the following call chain: ggml_vk_get_device() -> ggml_backend_vk_reg() -> ggml_vk_instance_init()
1 parent 95ea8f9 commit 478f30b

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,7 @@ struct vk_instance_t {
20602060
vk_device devices[GGML_VK_MAX_DEVICES];
20612061
};
20622062

2063+
static std::recursive_mutex vk_instance_init_mutex;
20632064
static bool vk_instance_initialized = false;
20642065
static vk_instance_t vk_instance;
20652066

@@ -4779,6 +4780,7 @@ static uint32_t ggml_vk_intel_shader_core_count(const vk::PhysicalDevice& vkdev)
47794780
static vk_device ggml_vk_get_device(size_t idx) {
47804781
VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
47814782

4783+
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
47824784
if (vk_instance.devices[idx] == nullptr) {
47834785
VK_LOG_DEBUG("Initializing new vk_device");
47844786
vk_device device = std::make_shared<vk_device_struct>();
@@ -5713,6 +5715,7 @@ DispatchLoaderDynamic & ggml_vk_default_dispatcher() {
57135715
}
57145716

57155717
static void ggml_vk_instance_init() {
5718+
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
57165719
if (vk_instance_initialized) {
57175720
return;
57185721
}

0 commit comments

Comments
 (0)