--- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,10 @@ #include #include "whisper.h" +#include "ggml-backend.h" + +#include +#include #define STRINGIFY(x) #x @@ -850,7 +854,25 @@ //////////// +// With GGML_BACKEND_DL the CPU and GPU backends are libraries next to this +// extension, and whisper.cpp cannot create a context until a CPU device is +// registered. ggml searches the executable's directory (the interpreter's, for +// Python) and the working directory, so load them from this module's directory. +static void load_ggml_backends_beside_module() { + Dl_info info; + if (dladdr(reinterpret_cast(&load_ggml_backends_beside_module), &info) == 0 || + info.dli_fname == nullptr) { + ggml_backend_load_all(); + return; + } + const std::string path(info.dli_fname); + const std::size_t slash = path.find_last_of('/'); + const std::string dir = slash == std::string::npos ? std::string(".") : path.substr(0, slash); + ggml_backend_load_all_from_path(dir.c_str()); +} + PYBIND11_MODULE(_pywhispercpp, m) { + load_ggml_backends_beside_module(); m.doc() = R"pbdoc( Pywhispercpp: Python binding to whisper.cpp -----------------------