Skip to content

Commit 8899a09

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 30c5194 commit 8899a09

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
@@ -2007,6 +2007,7 @@ struct vk_instance_t {
20072007
vk_device devices[GGML_VK_MAX_DEVICES];
20082008
};
20092009

2010+
static std::recursive_mutex vk_instance_init_mutex;
20102011
static bool vk_instance_initialized = false;
20112012
static vk_instance_t vk_instance;
20122013

@@ -4674,6 +4675,7 @@ static uint32_t ggml_vk_intel_shader_core_count(const vk::PhysicalDevice& vkdev)
46744675
static vk_device ggml_vk_get_device(size_t idx) {
46754676
VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
46764677

4678+
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
46774679
if (vk_instance.devices[idx] == nullptr) {
46784680
VK_LOG_DEBUG("Initializing new vk_device");
46794681
vk_device device = std::make_shared<vk_device_struct>();
@@ -5600,6 +5602,7 @@ DispatchLoaderDynamic & ggml_vk_default_dispatcher() {
56005602
}
56015603

56025604
static void ggml_vk_instance_init() {
5605+
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
56035606
if (vk_instance_initialized) {
56045607
return;
56055608
}

0 commit comments

Comments
 (0)