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()
This commit is contained in:
parent
95ea8f9bfb
commit
478f30b46d
|
|
@ -2060,6 +2060,7 @@ struct vk_instance_t {
|
|||
vk_device devices[GGML_VK_MAX_DEVICES];
|
||||
};
|
||||
|
||||
static std::recursive_mutex vk_instance_init_mutex;
|
||||
static bool vk_instance_initialized = false;
|
||||
static vk_instance_t vk_instance;
|
||||
|
||||
|
|
@ -4779,6 +4780,7 @@ static uint32_t ggml_vk_intel_shader_core_count(const vk::PhysicalDevice& vkdev)
|
|||
static vk_device ggml_vk_get_device(size_t idx) {
|
||||
VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
|
||||
if (vk_instance.devices[idx] == nullptr) {
|
||||
VK_LOG_DEBUG("Initializing new vk_device");
|
||||
vk_device device = std::make_shared<vk_device_struct>();
|
||||
|
|
@ -5713,6 +5715,7 @@ DispatchLoaderDynamic & ggml_vk_default_dispatcher() {
|
|||
}
|
||||
|
||||
static void ggml_vk_instance_init() {
|
||||
std::lock_guard<std::recursive_mutex> lock(vk_instance_init_mutex);
|
||||
if (vk_instance_initialized) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue