fix whisper device selection

This commit is contained in:
Concedo
2026-08-22 18:16:59 +08:00
parent 2b78904b4a
commit b6a253517e
3 changed files with 24 additions and 2 deletions
+21
View File
@@ -70,6 +70,27 @@ ggml_backend_dev_t kcpp_backend_get_device(int index)
}
}
ggml_backend_dev_t kcpp_backend_get_gpu_device(int index)
{
if (index < 0) {
return get_ggml_main_device();
}
size_t gpu_index = 0;
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
const enum ggml_backend_dev_type type = ggml_backend_dev_type(dev);
if (type == GGML_BACKEND_DEVICE_TYPE_GPU || type == GGML_BACKEND_DEVICE_TYPE_IGPU) {
if (gpu_index == (size_t) index) {
return dev;
}
++gpu_index;
}
}
return nullptr;
}
// this is similar to sd_backend_is, except:
// - if no backend is provided, checks the first ggml device (should be equivalent to a compile-time check)
// - tests a |-separated list of backend/device name prefixes
+1
View File
@@ -20,6 +20,7 @@
int kcpp_backend_check(const char* name_list, ggml_backend_t backend = nullptr);
ggml_backend_dev_t kcpp_backend_get_device(int index);
ggml_backend_dev_t kcpp_backend_get_gpu_device(int index);
// per-backend aux functions
+2 -2
View File
@@ -1216,9 +1216,9 @@ static ggml_backend_t whisper_backend_init(const whisper_context_params & params
ggml_backend_t backend_gpu = NULL;
if (params.use_gpu) {
auto device = kcpp_backend_get_device(params.gpu_device);
auto device = kcpp_backend_get_gpu_device(params.gpu_device);
if (!device) {
WHISPER_LOG_ERROR("%s: couldn't get device %d\n", __func__, params.gpu_device);
WHISPER_LOG_ERROR("%s: couldn't get GPU device %d\n", __func__, params.gpu_device);
} else {
WHISPER_LOG_INFO("%s: using backend %s\n", __func__, ggml_backend_dev_name(device));
backend_gpu = ggml_backend_dev_init(device, nullptr);