diff --git a/kcpp_backend.cpp b/kcpp_backend.cpp index 30564cfab..ad89f80d0 100644 --- a/kcpp_backend.cpp +++ b/kcpp_backend.cpp @@ -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 diff --git a/kcpp_backend.h b/kcpp_backend.h index ee9932033..4228409b2 100644 --- a/kcpp_backend.h +++ b/kcpp_backend.h @@ -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 diff --git a/otherarch/whispercpp/whisper.cpp b/otherarch/whispercpp/whisper.cpp index fb335542d..88a036a9e 100644 --- a/otherarch/whispercpp/whisper.cpp +++ b/otherarch/whispercpp/whisper.cpp @@ -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);