Skip to content

Commit 701d2e6

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 aa1bc0d commit 701d2e6

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,7 @@ struct vk_instance_t {
19631963
vk_device devices[GGML_VK_MAX_DEVICES];
19641964
};
19651965

1966+
static std::recursive_mutex vk_instance_init_mutex;
19661967
static bool vk_instance_initialized = false;
19671968
static vk_instance_t vk_instance;
19681969

@@ -4487,6 +4488,7 @@ static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDevicePrope
44874488
static vk_device ggml_vk_get_device(size_t idx) {
44884489
VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
44894490

4491+
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
44904492
if (vk_instance.devices[idx] == nullptr) {
44914493
VK_LOG_DEBUG("Initializing new vk_device");
44924494
vk_device device = std::make_shared<vk_device_struct>();
@@ -5415,6 +5417,7 @@ DispatchLoaderDynamic & ggml_vk_default_dispatcher() {
54155417
}
54165418

54175419
static void ggml_vk_instance_init() {
5420+
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
54185421
if (vk_instance_initialized) {
54195422
return;
54205423
}

0 commit comments

Comments
 (0)