| 1 | #cxx . . $ `<file>/external/whispercpp/ggml-backend-reg.cpp`/include "ggml-backend-impl.h"cxx . . $ `<file>/external/whispercpp/ggml-backend-impl.h`/ |
| 2 | #include "ggml-backend.h"cxx . . $ `<file>/external/whispercpp/ggml-backend.h`/ |
| 3 | #include "ggml-backend-dl.h"cxx . . $ `<file>/external/whispercpp/ggml-backend-dl.h`/ |
| 4 | #include "ggml-impl.h"cxx . . $ `<file>/external/whispercpp/ggml-impl.h`/ |
| 5 | #include <algorithm> |
| 6 | #include <cstring> |
| 7 | #include <filesystem> |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <type_traits> |
| 11 | #include <vector> |
| 12 | #include <cctype> |
| 13 | |
| 14 | #ifdef _WIN32 |
| 15 | # define WIN32_LEAN_AND_MEAN |
| 16 | # ifndef NOMINMAX |
| 17 | # define NOMINMAX |
| 18 | # endif |
| 19 | # include <windows.h> |
| 20 | #elif defined(__APPLE__) |
| 21 | # include <mach-o/dyld.h> |
| 22 | # include <dlfcn.h> |
| 23 | #else |
| 24 | # include <dlfcn.h> |
| 25 | # include <unistd.h> |
| 26 | #endif |
| 27 | |
| 28 | // Backend registry |
| 29 | #ifdef GGML_USE_CPUcxx . . $ `external/whispercpp/ggml.h:9:9`! |
| 30 | #include "ggml-cpu.h"cxx . . $ `<file>/external/whispercpp/ggml-cpu.h`/ |
| 31 | #endif |
| 32 | |
| 33 | #ifdef GGML_USE_CUDA |
| 34 | #include "ggml-cuda.h" |
| 35 | #endif |
| 36 | |
| 37 | #ifdef GGML_USE_METAL |
| 38 | #include "ggml-metal.h" |
| 39 | #endif |
| 40 | |
| 41 | #ifdef GGML_USE_SYCL |
| 42 | #include "ggml-sycl.h" |
| 43 | #endif |
| 44 | |
| 45 | #ifdef GGML_USE_VULKAN |
| 46 | #include "ggml-vulkan.h" |
| 47 | #endif |
| 48 | |
| 49 | #ifdef GGML_USE_WEBGPU |
| 50 | #include "ggml-webgpu.h" |
| 51 | #endif |
| 52 | |
| 53 | #ifdef GGML_USE_ZDNN |
| 54 | #include "ggml-zdnn.h" |
| 55 | #endif |
| 56 | |
| 57 | #ifdef GGML_USE_OPENCL |
| 58 | #include "ggml-opencl.h" |
| 59 | #endif |
| 60 | |
| 61 | #ifdef GGML_USE_HEXAGON |
| 62 | #include "ggml-hexagon.h" |
| 63 | #endif |
| 64 | |
| 65 | #ifdef GGML_USE_BLAS |
| 66 | #include "ggml-blas.h" |
| 67 | #endif |
| 68 | |
| 69 | #ifdef GGML_USE_RPC |
| 70 | #include "ggml-rpc.h" |
| 71 | #endif |
| 72 | |
| 73 | #ifdef GGML_USE_VIRTGPU_FRONTEND |
| 74 | #include "ggml-virtgpu.h" |
| 75 | #endif |
| 76 | |
| 77 | #ifdef GGML_USE_CANN |
| 78 | #include "ggml-cann.h" |
| 79 | #endif |
| 80 | |
| 81 | #ifdef GGML_USE_ZENDNN |
| 82 | #include "ggml-zendnn.h" |
| 83 | #endif |
| 84 | |
| 85 | namespace fs = stdcxx . . $ std/::filesystem; |
| 86 | |
| 87 | static stdcxx . . $ std/::stringcxx . . $ std/string# path_strcxx . . $ path_str(dd83f402b0f84a75).(const fs::pathcxx . . $ std/filesystem/__cxx11/path# & path) { |
| 88 | try { |
| 89 | #if defined(__cpp_lib_char8_t) |
| 90 | // C++20 and later: u8string() returns std::u8string |
| 91 | const std::u8string u8str = path.u8string(); |
| 92 | return std::string(reinterpret_cast<const char *>(u8str.data()), u8str.size()); |
| 93 | #else |
| 94 | // C++17: u8string() returns std::string |
| 95 | return path.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).(); |
| 96 | #endif |
| 97 | } catch (...) { |
| 98 | return stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | struct ggml_backend_reg_entrycxx . . $ ggml_backend_reg_entry# { |
| 103 | ggml_backend_reg_t regcxx . . $ ggml_backend_reg_entry#reg.; |
| 104 | dl_handle_ptrcxx . . $ dl_handle_ptr# handlecxx . . $ ggml_backend_reg_entry#handle.; |
| 105 | }; |
| 106 | |
| 107 | struct ggml_backend_registrycxx . . $ ggml_backend_registry# { |
| 108 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<ggml_backend_reg_entrycxx . . $ ggml_backend_reg_entry#> backendscxx . . $ ggml_backend_registry#backends.; |
| 109 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<ggml_backend_dev_t> devicescxx . . $ ggml_backend_registry#devices.; |
| 110 | |
| 111 | ggml_backend_registrycxx . . $ ggml_backend_registry#ggml_backend_registry(49f6e7a06ebc5aa8).() { |
| 112 | #ifdef GGML_USE_CUDA |
| 113 | register_backend(ggml_backend_cuda_reg()); |
| 114 | #endif |
| 115 | #ifdef GGML_USE_METAL |
| 116 | register_backend(ggml_backend_metal_reg()); |
| 117 | #endif |
| 118 | #ifdef GGML_USE_SYCL |
| 119 | register_backend(ggml_backend_sycl_reg()); |
| 120 | #endif |
| 121 | #ifdef GGML_USE_VULKAN |
| 122 | // Add runtime disable check |
| 123 | if (getenv("GGML_DISABLE_VULKAN") == nullptr) { |
| 124 | register_backend(ggml_backend_vk_reg()); |
| 125 | } else { |
| 126 | GGML_LOG_DEBUG("Vulkan backend disabled by GGML_DISABLE_VULKAN environment variable\n"); |
| 127 | } |
| 128 | #endif |
| 129 | #ifdef GGML_USE_WEBGPU |
| 130 | register_backend(ggml_backend_webgpu_reg()); |
| 131 | #endif |
| 132 | #ifdef GGML_USE_ZDNN |
| 133 | register_backend(ggml_backend_zdnn_reg()); |
| 134 | #endif |
| 135 | #ifdef GGML_USE_VIRTGPU_FRONTEND |
| 136 | register_backend(ggml_backend_virtgpu_reg()); |
| 137 | #endif |
| 138 | |
| 139 | #ifdef GGML_USE_OPENCL |
| 140 | register_backend(ggml_backend_opencl_reg()); |
| 141 | #endif |
| 142 | #ifdef GGML_USE_ZENDNN |
| 143 | register_backend(ggml_backend_zendnn_reg()); |
| 144 | #endif |
| 145 | #ifdef GGML_USE_HEXAGON |
| 146 | register_backend(ggml_backend_hexagon_reg()); |
| 147 | #endif |
| 148 | #ifdef GGML_USE_CANN |
| 149 | register_backend(ggml_backend_cann_reg()); |
| 150 | #endif |
| 151 | #ifdef GGML_USE_BLAS |
| 152 | register_backend(ggml_backend_blas_reg()); |
| 153 | #endif |
| 154 | #ifdef GGML_USE_RPC |
| 155 | register_backend(ggml_backend_rpc_reg()); |
| 156 | #endif |
| 157 | #ifdef GGML_USE_CPUcxx . . $ `external/whispercpp/ggml.h:9:9`! |
| 158 | register_backendcxx . . $ ggml_backend_registry#register_backend(163a2ace36cf467c).(ggml_backend_cpu_reg()); |
| 159 | #endif |
| 160 | } |
| 161 | |
| 162 | ~cxx . . $ ggml_backend_registry#`~ggml_backend_registry`(ced63f7c635d850d).ggml_backend_registrycxx . . $ ggml_backend_registry#() { |
| 163 | // FIXME: backends cannot be safely unloaded without a function to destroy all the backend resources, |
| 164 | // since backend threads may still be running and accessing resources from the dynamic library |
| 165 | for (auto & entry : backendscxx . . $ ggml_backend_registry#backends.) { |
| 166 | if (entry.handlecxx . . $ ggml_backend_reg_entry#handle.) { |
| 167 | entry.handlecxx . . $ ggml_backend_reg_entry#handle..releasecxx . . $ std/unique_ptr#release(aad4c3669c5e940c).(); // NOLINT |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void register_backendcxx . . $ ggml_backend_registry#register_backend(163a2ace36cf467c).(ggml_backend_reg_t reg, dl_handle_ptrcxx . . $ dl_handle_ptr# handle = nullptrcxx . . $ std/unique_ptr#unique_ptr(6d988fd07f070dfc).) { |
| 173 | if (!reg) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | #ifndef NDEBUG |
| 178 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: registered backend %s (%zu devices)\n", |
| 179 | __func__, ggml_backend_reg_name(reg), ggml_backend_reg_dev_count(reg)); |
| 180 | #endif |
| 181 | backendscxx . . $ ggml_backend_registry#backends..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).({ reg, stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(handle) }); |
| 182 | for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); i++) { |
| 183 | register_devicecxx . . $ ggml_backend_registry#register_device(6bc744823ae23507).(ggml_backend_reg_dev_get(reg, i)); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void register_devicecxx . . $ ggml_backend_registry#register_device(6bc744823ae23507).(ggml_backend_dev_t device) { |
| 188 | #ifndef NDEBUG |
| 189 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: registered device %s (%s)\n", __func__, ggml_backend_dev_name(device), ggml_backend_dev_description(device)); |
| 190 | #endif |
| 191 | devicescxx . . $ ggml_backend_registry#devices..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(device); |
| 192 | } |
| 193 | |
| 194 | ggml_backend_reg_t load_backendcxx . . $ ggml_backend_registry#load_backend(f541f65cbf941927).(const fs::pathcxx . . $ std/filesystem/__cxx11/path# & path, bool silent) { |
| 195 | dl_handle_ptrcxx . . $ dl_handle_ptr# handlecxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d). { dl_load_librarycxx . . $ dl_load_library(c59277d6bedb3fff).(path) }; |
| 196 | if (!handle) { |
| 197 | if (!silent) { |
| 198 | GGML_LOG_ERRORcxx . . $ `external/whispercpp/ggml-impl.h:119:9`!("%s: failed to load %s: %s\n", __func__, path_str(path).c_str(), dl_error()); |
| 199 | } |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | auto score_fn = (ggml_backend_score_t) dl_get_symcxx . . $ dl_get_sym(5c3a2d84d269ab4b).(handle.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), "ggml_backend_score"); |
| 204 | if (score_fn && score_fn() == 0) { |
| 205 | if (!silent) { |
| 206 | GGML_LOG_INFOcxx . . $ `external/whispercpp/ggml-impl.h:117:9`!("%s: backend %s is not supported on this system\n", __func__, path_str(path).c_str()); |
| 207 | } |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | auto backend_init_fn = (ggml_backend_init_t) dl_get_symcxx . . $ dl_get_sym(5c3a2d84d269ab4b).(handle.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), "ggml_backend_init"); |
| 212 | if (!backend_init_fn) { |
| 213 | if (!silent) { |
| 214 | GGML_LOG_ERRORcxx . . $ `external/whispercpp/ggml-impl.h:119:9`!("%s: failed to find ggml_backend_init in %s\n", __func__, path_str(path).c_str()); |
| 215 | } |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | ggml_backend_reg_t reg = backend_init_fn(); |
| 220 | if (!reg || reg->api_versioncxx . . $ ggml_backend_reg#api_version. != GGML_BACKEND_API_VERSIONcxx . . $ `external/whispercpp/ggml-backend-impl.h:11:13`!) { |
| 221 | if (!silent) { |
| 222 | if (!reg) { |
| 223 | GGML_LOG_ERRORcxx . . $ `external/whispercpp/ggml-impl.h:119:9`!("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", |
| 224 | __func__, path_str(path).c_str()); |
| 225 | } else { |
| 226 | GGML_LOG_ERRORcxx . . $ `external/whispercpp/ggml-impl.h:119:9`!("%s: failed to initialize backend from %s: incompatible API version (backend: %d, current: %d)\n", |
| 227 | __func__, path_str(path).c_str(), reg->api_version, GGML_BACKEND_API_VERSIONcxx . . $ `external/whispercpp/ggml-backend-impl.h:11:13`!); |
| 228 | } |
| 229 | } |
| 230 | return nullptr; |
| 231 | } |
| 232 | |
| 233 | GGML_LOG_INFOcxx . . $ `external/whispercpp/ggml-impl.h:117:9`!("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), path_str(path).c_str()); |
| 234 | |
| 235 | register_backendcxx . . $ ggml_backend_registry#register_backend(163a2ace36cf467c).(reg, stdcxx . . $ std/unique_ptr#unique_ptr(d6762fce684538e4).::movecxx . . $ std/move(e35c19a1ba7baa26).(handle)); |
| 236 | |
| 237 | return reg; |
| 238 | } |
| 239 | |
| 240 | void unload_backendcxx . . $ ggml_backend_registry#unload_backend(1617dd40fa43cd7f).(ggml_backend_reg_t reg, bool silent) { |
| 241 | auto it = stdcxx . . $ std/::find_ifcxx . . $ std/find_if(f0a0a4f94ee5cb00).(backendscxx . . $ ggml_backend_registry#backends..begincxx . . $ std/vector#begin(55a6de06420d4903).(), backendscxx . . $ ggml_backend_registry#backends..endcxx . . $ std/vector#end(55a6de06420d4903).(), |
| 242 | [reg](const ggml_backend_reg_entrycxx . . $ ggml_backend_reg_entry# & entry) { return entry.regcxx . . $ ggml_backend_reg_entry#reg. == reg; }); |
| 243 | |
| 244 | if (it ==cxx . . $ __gnu_cxx/`operator==`(30ee64f2df4eef8b). backendscxx . . $ ggml_backend_registry#backends..endcxx . . $ std/vector#end(55a6de06420d4903).()) { |
| 245 | if (!silent) { |
| 246 | GGML_LOG_ERRORcxx . . $ `external/whispercpp/ggml-impl.h:119:9`!("%s: backend not found\n", __func__); |
| 247 | } |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | if (!silent) { |
| 252 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: unloading %s backend\n", __func__, ggml_backend_reg_name(reg)); |
| 253 | } |
| 254 | |
| 255 | // remove devices |
| 256 | devicescxx . . $ ggml_backend_registry#devices..erasecxx . . $ std/vector#erase(cc2a997b00bceed8).( |
| 257 | stdcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640).::remove_ifcxx . . $ std/remove_if(f0a0a4f94ee5cb00).(devicescxx . . $ ggml_backend_registry#devices..begincxx . . $ std/vector#begin(55a6de06420d4903).(), devicescxx . . $ ggml_backend_registry#devices..endcxx . . $ std/vector#end(55a6de06420d4903).(), |
| 258 | [reg](ggml_backend_dev_t dev) { return ggml_backend_dev_backend_reg(dev) == reg; }), |
| 259 | devicescxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 260 | |
| 261 | // remove backend |
| 262 | backendscxx . . $ ggml_backend_registry#backends..erasecxx . . $ std/vector#erase(351407f384f345bc).(itcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640).); |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | static ggml_backend_registrycxx . . $ ggml_backend_registry# & get_regcxx . . $ get_reg(85f26f864ed68802).() { |
| 267 | static ggml_backend_registrycxx . . $ ggml_backend_registry# regcxx . . $ ggml_backend_registry#ggml_backend_registry(49f6e7a06ebc5aa8).; |
| 268 | return reg; |
| 269 | } |
| 270 | |
| 271 | // Internal API |
| 272 | void ggml_backend_registercxx . . $ ggml_backend_register(c54a7ad4b653e970).(ggml_backend_reg_t reg) { |
| 273 | get_regcxx . . $ get_reg(85f26f864ed68802).().register_backendcxx . . $ ggml_backend_registry#register_backend(163a2ace36cf467c).(reg); |
| 274 | } |
| 275 | |
| 276 | void ggml_backend_device_registercxx . . $ ggml_backend_device_register(6bc744823ae23507).(ggml_backend_dev_t device) { |
| 277 | get_regcxx . . $ get_reg(85f26f864ed68802).().register_devicecxx . . $ ggml_backend_registry#register_device(6bc744823ae23507).(device); |
| 278 | } |
| 279 | |
| 280 | // Backend (reg) enumeration |
| 281 | static bool striequalscxx . . $ striequals(b7a7aa4a824b7595).(const char * a, const char * b) { |
| 282 | for (; *a && *b; a++, b++) { |
| 283 | if (stdcxx . . $ std/::tolowercxx . . $ std/tolower(a1851578aea3f541).(*a) != stdcxx . . $ std/::tolowercxx . . $ std/tolower(a1851578aea3f541).(*b)) { |
| 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | return *a == *b; |
| 288 | } |
| 289 | |
| 290 | size_t ggml_backend_reg_countcxx . . $ ggml_backend_reg_count(f708dce9a8856c70).() { |
| 291 | return get_regcxx . . $ get_reg(85f26f864ed68802).().backendscxx . . $ ggml_backend_registry#backends..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 292 | } |
| 293 | |
| 294 | ggml_backend_reg_t ggml_backend_reg_getcxx . . $ ggml_backend_reg_get(111997b9a35d8569).(size_t index) { |
| 295 | GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!(index < ggml_backend_reg_count()); |
| 296 | return get_regcxx . . $ get_reg(85f26f864ed68802).().backendscxx . . $ ggml_backend_registry#backends.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).index].regcxx . . $ ggml_backend_reg_entry#reg.; |
| 297 | } |
| 298 | |
| 299 | ggml_backend_reg_t ggml_backend_reg_by_namecxx . . $ ggml_backend_reg_by_name(540634fdca863a2d).(const char * name) { |
| 300 | for (size_t i = 0; i < ggml_backend_reg_countcxx . . $ ggml_backend_reg_count(f708dce9a8856c70).(); i++) { |
| 301 | ggml_backend_reg_t reg = ggml_backend_reg_getcxx . . $ ggml_backend_reg_get(111997b9a35d8569).(i); |
| 302 | if (striequalscxx . . $ striequals(b7a7aa4a824b7595).(ggml_backend_reg_name(reg), name)) { |
| 303 | return reg; |
| 304 | } |
| 305 | } |
| 306 | return nullptr; |
| 307 | } |
| 308 | |
| 309 | // Device enumeration |
| 310 | size_t ggml_backend_dev_countcxx . . $ ggml_backend_dev_count(f708dce9a8856c70).() { |
| 311 | return get_regcxx . . $ get_reg(85f26f864ed68802).().devicescxx . . $ ggml_backend_registry#devices..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 312 | } |
| 313 | |
| 314 | ggml_backend_dev_t ggml_backend_dev_getcxx . . $ ggml_backend_dev_get(98bcaeedb66f1310).(size_t index) { |
| 315 | GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!(index < ggml_backend_dev_count()); |
| 316 | return get_regcxx . . $ get_reg(85f26f864ed68802).().devicescxx . . $ ggml_backend_registry#devices.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).index]; |
| 317 | } |
| 318 | |
| 319 | ggml_backend_dev_t ggml_backend_dev_by_namecxx . . $ ggml_backend_dev_by_name(59c905ce115cae7b).(const char * name) { |
| 320 | for (size_t i = 0; i < ggml_backend_dev_countcxx . . $ ggml_backend_dev_count(f708dce9a8856c70).(); i++) { |
| 321 | ggml_backend_dev_t dev = ggml_backend_dev_getcxx . . $ ggml_backend_dev_get(98bcaeedb66f1310).(i); |
| 322 | if (striequalscxx . . $ striequals(b7a7aa4a824b7595).(ggml_backend_dev_name(dev), name)) { |
| 323 | return dev; |
| 324 | } |
| 325 | } |
| 326 | return nullptr; |
| 327 | } |
| 328 | |
| 329 | ggml_backend_dev_t ggml_backend_dev_by_typecxx . . $ ggml_backend_dev_by_type(6536c115dd2b7fa).(enum ggml_backend_dev_type type) { |
| 330 | for (size_t i = 0; i < ggml_backend_dev_countcxx . . $ ggml_backend_dev_count(f708dce9a8856c70).(); i++) { |
| 331 | ggml_backend_dev_t dev = ggml_backend_dev_getcxx . . $ ggml_backend_dev_get(98bcaeedb66f1310).(i); |
| 332 | if (ggml_backend_dev_type(dev) == type) { |
| 333 | return dev; |
| 334 | } |
| 335 | } |
| 336 | return nullptr; |
| 337 | } |
| 338 | |
| 339 | // Convenience functions |
| 340 | ggml_backend_t ggml_backend_init_by_namecxx . . $ ggml_backend_init_by_name(63901cffb4c7d2b7).(const char * name, const char * params) { |
| 341 | ggml_backend_dev_t dev = ggml_backend_dev_by_namecxx . . $ ggml_backend_dev_by_name(59c905ce115cae7b).(name); |
| 342 | if (!dev) { |
| 343 | return nullptr; |
| 344 | } |
| 345 | return ggml_backend_dev_init(dev, params); |
| 346 | } |
| 347 | |
| 348 | ggml_backend_t ggml_backend_init_by_typecxx . . $ ggml_backend_init_by_type(e42bab738091d2e2).(enum ggml_backend_dev_type type, const char * params) { |
| 349 | ggml_backend_dev_t dev = ggml_backend_dev_by_typecxx . . $ ggml_backend_dev_by_type(6536c115dd2b7fa).(type); |
| 350 | if (!dev) { |
| 351 | return nullptr; |
| 352 | } |
| 353 | return ggml_backend_dev_init(dev, params); |
| 354 | } |
| 355 | |
| 356 | ggml_backend_t ggml_backend_init_bestcxx . . $ ggml_backend_init_best(f2183ca73baf5b57).(void) { |
| 357 | ggml_backend_dev_t dev = ggml_backend_dev_by_typecxx . . $ ggml_backend_dev_by_type(6536c115dd2b7fa).(GGML_BACKEND_DEVICE_TYPE_GPU); |
| 358 | dev = dev ? dev : ggml_backend_dev_by_typecxx . . $ ggml_backend_dev_by_type(6536c115dd2b7fa).(GGML_BACKEND_DEVICE_TYPE_IGPU); |
| 359 | dev = dev ? dev : ggml_backend_dev_by_typecxx . . $ ggml_backend_dev_by_type(6536c115dd2b7fa).(GGML_BACKEND_DEVICE_TYPE_CPU); |
| 360 | if (!dev) { |
| 361 | return nullptr; |
| 362 | } |
| 363 | return ggml_backend_dev_init(dev, nullptr); |
| 364 | } |
| 365 | |
| 366 | // Dynamic loading |
| 367 | ggml_backend_reg_t ggml_backend_loadcxx . . $ ggml_backend_load(540634fdca863a2d).(const char * path) { |
| 368 | return get_regcxx . . $ get_reg(85f26f864ed68802).().load_backendcxx . . $ ggml_backend_registry#load_backend(f541f65cbf941927).(pathcxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d)., false); |
| 369 | } |
| 370 | |
| 371 | void ggml_backend_unloadcxx . . $ ggml_backend_unload(c54a7ad4b653e970).(ggml_backend_reg_t reg) { |
| 372 | get_regcxx . . $ get_reg(85f26f864ed68802).().unload_backendcxx . . $ ggml_backend_registry#unload_backend(1617dd40fa43cd7f).(reg, true); |
| 373 | } |
| 374 | |
| 375 | static fs::pathcxx . . $ std/filesystem/__cxx11/path# get_executable_pathcxx . . $ get_executable_path(4c12bfa3c403d782).() { |
| 376 | #if defined(__APPLE__) |
| 377 | // get executable path |
| 378 | std::vector<char> path; |
| 379 | uint32_t size; |
| 380 | while (true) { |
| 381 | size = path.size(); |
| 382 | if (_NSGetExecutablePath(path.data(), &size) == 0) { |
| 383 | break; |
| 384 | } |
| 385 | path.resize(size); |
| 386 | } |
| 387 | std::string base_path(path.data(), size); |
| 388 | // remove executable name |
| 389 | auto last_slash = base_path.find_last_of('/'); |
| 390 | if (last_slash != std::string::npos) { |
| 391 | base_path = base_path.substr(0, last_slash); |
| 392 | } |
| 393 | return base_path + "/"; |
| 394 | #elif defined(__linux__cxx . . $ `<built-in>:463:9`!) || defined(__FreeBSD__) |
| 395 | stdcxx . . $ std/::stringcxx . . $ std/string# base_path = "."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 396 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> pathcxx . . $ std/vector#vector(83fdeebbe0501a83).(1024); |
| 397 | while (true) { |
| 398 | // get executable path |
| 399 | # if defined(__linux__cxx . . $ `<built-in>:463:9`!) |
| 400 | ssize_t len = readlink("/proc/self/exe", path.datacxx . . $ std/vector#data(258ee1c9e0a18a5c).(), path.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 401 | # elif defined(__FreeBSD__) |
| 402 | ssize_t len = readlink("/proc/curproc/file", path.data(), path.size()); |
| 403 | # endif |
| 404 | if (len == -1) { |
| 405 | break; |
| 406 | } |
| 407 | if (len < (ssize_t) path.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) { |
| 408 | base_path =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). stdcxx . . $ std/__cxx11/basic_string#basic_string(f26a9578972d3fae).::stringcxx . . $ std/string#(path.datacxx . . $ std/vector#data(258ee1c9e0a18a5c).(), len); |
| 409 | // remove executable name |
| 410 | auto last_slash = base_path.find_last_ofcxx . . $ std/__cxx11/basic_string#find_last_of(2dda2fc6b78abe68).('/'); |
| 411 | if (last_slash != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 412 | base_path =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). base_path.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, last_slash); |
| 413 | } |
| 414 | break; |
| 415 | } |
| 416 | path.resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(path.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() * 2); |
| 417 | } |
| 418 | |
| 419 | return base_pathcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333). +cxx . . $ std/operator+(40fc1db9226233c6). "/"; |
| 420 | #elif defined(_WIN32) |
| 421 | std::vector<wchar_t> path(MAX_PATH); |
| 422 | DWORD len = GetModuleFileNameW(NULL, path.data(), path.size()); |
| 423 | if (len == 0) { |
| 424 | return {}; |
| 425 | } |
| 426 | std::wstring base_path(path.data(), len); |
| 427 | // remove executable name |
| 428 | auto last_slash = base_path.find_last_of('\\'); |
| 429 | if (last_slash != std::string::npos) { |
| 430 | base_path = base_path.substr(0, last_slash); |
| 431 | } |
| 432 | return base_path + L"\\"; |
| 433 | #else |
| 434 | return {}; |
| 435 | #endif |
| 436 | } |
| 437 | |
| 438 | static fs::pathcxx . . $ std/filesystem/__cxx11/path# backend_filename_prefixcxx . . $ backend_filename_prefix(4c12bfa3c403d782).() { |
| 439 | #ifdef _WIN32 |
| 440 | return fs::u8path("ggml-"); |
| 441 | #else |
| 442 | return fs::u8pathcxx . . $ std/filesystem/__cxx11/u8path(a356d1ed1322bb3).("libggml-"); |
| 443 | #endif |
| 444 | } |
| 445 | |
| 446 | static fs::pathcxx . . $ std/filesystem/__cxx11/path# backend_filename_extensioncxx . . $ backend_filename_extension(4c12bfa3c403d782).() { |
| 447 | #ifdef _WIN32 |
| 448 | return fs::u8path(".dll"); |
| 449 | #else |
| 450 | return fs::u8pathcxx . . $ std/filesystem/__cxx11/u8path(a356d1ed1322bb3).(".so"); |
| 451 | #endif |
| 452 | } |
| 453 | |
| 454 | static ggml_backend_reg_t ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).(const char * name, bool silent, const char * user_search_path) { |
| 455 | // enumerate all the files that match [lib]ggml-name-*.[so|dll] in the search paths |
| 456 | const fs::pathcxx . . $ std/filesystem/__cxx11/path# name_path = fs::u8pathcxx . . $ std/filesystem/__cxx11/u8path(a356d1ed1322bb3).(name); |
| 457 | const fs::pathcxx . . $ std/filesystem/__cxx11/path# file_prefix = backend_filename_prefixcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).().nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).() +cxx . . $ std/operator+(2764d1739bf7913d). name_path.nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).() +cxx . . $ std/operator+(db5dc2ea912c6495). fs::u8pathcxx . . $ std/filesystem/__cxx11/u8path(a356d1ed1322bb3).("-").nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).(); |
| 458 | const fs::pathcxx . . $ std/filesystem/__cxx11/path# file_extension = backend_filename_extensioncxx . . $ backend_filename_extension(4c12bfa3c403d782).(); |
| 459 | |
| 460 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<fs::pathcxx . . $ std/filesystem/__cxx11/path#> search_pathscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 461 | if (user_search_path == nullptr) { |
| 462 | #ifdef GGML_BACKEND_DIR |
| 463 | search_paths.push_back(fs::u8path(GGML_BACKEND_DIR)); |
| 464 | #endif |
| 465 | // default search paths: executable directory, current directory |
| 466 | search_paths.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(get_executable_pathcxx . . $ get_executable_path(4c12bfa3c403d782).()); |
| 467 | search_paths.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(fs::current_pathcxx . . $ std/filesystem/current_path(4c12bfa3c403d782).()); |
| 468 | } else { |
| 469 | search_paths.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(fs::u8pathcxx . . $ std/filesystem/__cxx11/u8path(a356d1ed1322bb3).(user_search_path)); |
| 470 | } |
| 471 | |
| 472 | int best_score = 0; |
| 473 | fs::pathcxx . . $ std/filesystem/__cxx11/path# best_pathcxx . . $ std/filesystem/__cxx11/path#path(ced63f7c635d850d).; |
| 474 | stdcxx . . $ std/::error_codecxx . . $ std/error_code# eccxx . . $ std/error_code#error_code(ced63f7c635d850d).; |
| 475 | |
| 476 | for (const auto & search_path : search_paths) { |
| 477 | if (!fs::existscxx . . $ std/filesystem/exists(c59b83c1f15b4fe7).(search_path, ec)) { |
| 478 | if (ec) { |
| 479 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: posix_stat(%s) failure, error-message: %s\n", __func__, path_str(search_path).c_str(), ec.message().c_str()); |
| 480 | } else { |
| 481 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: search path %s does not exist\n", __func__, path_str(search_path).c_str()); |
| 482 | } |
| 483 | continue; |
| 484 | } |
| 485 | fs::directory_iteratorcxx . . $ std/filesystem/__cxx11/directory_iterator# dir_itcxx . . $ std/filesystem/__cxx11/directory_iterator#directory_iterator(dd495d8940487fb3).(search_path, fs::directory_optionscxx . . $ std/filesystem/directory_options#::skip_permission_deniedcxx . . $ std/filesystem/directory_options#skip_permission_denied.); |
| 486 | for (const auto & entry : dir_it) { |
| 487 | if (entry.is_regular_filecxx . . $ std/filesystem/__cxx11/directory_entry#is_regular_file(2da30f7f83083074).(ec)) { |
| 488 | auto filename = entry.pathcxx . . $ std/filesystem/__cxx11/directory_entry#path(3df3a2cdf6f4d926).().filenamecxx . . $ std/filesystem/__cxx11/path#filename(159787a050b29eb0).(); |
| 489 | auto ext = entry.pathcxx . . $ std/filesystem/__cxx11/directory_entry#path(3df3a2cdf6f4d926).().extensioncxx . . $ std/filesystem/__cxx11/path#extension(159787a050b29eb0).(); |
| 490 | if (filename.nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).().findcxx . . $ std/__cxx11/basic_string#find(cabbe8683ddb9d2a).(file_prefix) == 0 && ext ==cxx . . $ std/filesystem/__cxx11/`operator==`(33cf7b584ac59a9b). file_extension) { |
| 491 | dl_handle_ptrcxx . . $ dl_handle_ptr# handlecxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d). { dl_load_librarycxx . . $ dl_load_library(c59277d6bedb3fff).(entry) }; |
| 492 | if (!handle && !silent) { |
| 493 | GGML_LOG_ERRORcxx . . $ `external/whispercpp/ggml-impl.h:119:9`!("%s: failed to load %s: %s\n", __func__, path_str(entry.path()).c_str(), dl_error()); |
| 494 | } |
| 495 | if (handle) { |
| 496 | auto score_fn = (ggml_backend_score_t) dl_get_symcxx . . $ dl_get_sym(5c3a2d84d269ab4b).(handle.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), "ggml_backend_score"); |
| 497 | if (score_fn) { |
| 498 | int s = score_fn(); |
| 499 | #ifndef NDEBUG |
| 500 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: %s score: %d\n", __func__, path_str(entry.path()).c_str(), s); |
| 501 | #endif |
| 502 | if (s > best_score) { |
| 503 | best_score = s; |
| 504 | best_path =cxx . . $ std/filesystem/__cxx11/path#`operator=`(554473e25f389c7d). entry.pathcxx . . $ std/filesystem/__cxx11/directory_entry#path(3df3a2cdf6f4d926).(); |
| 505 | } |
| 506 | } else { |
| 507 | if (!silent) { |
| 508 | GGML_LOG_INFOcxx . . $ `external/whispercpp/ggml-impl.h:117:9`!("%s: failed to find ggml_backend_score in %s\n", __func__, path_str(entry.path()).c_str()); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (best_score == 0) { |
| 518 | // try to load the base backend |
| 519 | for (const auto & search_path : search_paths) { |
| 520 | fs::pathcxx . . $ std/filesystem/__cxx11/path# filename = backend_filename_prefixcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).().nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).() +cxx . . $ std/operator+(2764d1739bf7913d). name_path.nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).() +cxx . . $ std/operator+(db5dc2ea912c6495). backend_filename_extensioncxx . . $ backend_filename_extension(4c12bfa3c403d782).().nativecxx . . $ std/filesystem/__cxx11/path#native(7ef77752ffaba5f1).(); |
| 521 | fs::pathcxx . . $ std/filesystem/__cxx11/path# path = search_path /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). filename; |
| 522 | if (stdcxx . . $ std/::error_codecxx . . $ std/error_code# eccxx . . $ std/error_code#error_code(ced63f7c635d850d).; fs::existscxx . . $ std/filesystem/exists(c59b83c1f15b4fe7).(path, ec)) { |
| 523 | return get_regcxx . . $ get_reg(85f26f864ed68802).().load_backendcxx . . $ ggml_backend_registry#load_backend(f541f65cbf941927).(path, silent); |
| 524 | } else { |
| 525 | if (ec) { |
| 526 | GGML_LOG_DEBUGcxx . . $ `external/whispercpp/ggml-impl.h:120:9`!("%s: posix_stat(%s) failure, error-message: %s\n", __func__, path_str(path).c_str(), ec.message().c_str()); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | return nullptr; |
| 531 | } |
| 532 | |
| 533 | return get_regcxx . . $ get_reg(85f26f864ed68802).().load_backendcxx . . $ ggml_backend_registry#load_backend(f541f65cbf941927).(best_path, silent); |
| 534 | } |
| 535 | |
| 536 | void ggml_backend_load_allcxx . . $ ggml_backend_load_all(49f6e7a06ebc5aa8).() { |
| 537 | ggml_backend_load_all_from_path(nullptr); |
| 538 | } |
| 539 | |
| 540 | void ggml_backend_load_all_from_pathcxx . . $ ggml_backend_load_all_from_path(85c52e162fed56f9).(const char * dir_path) { |
| 541 | #ifdef NDEBUG |
| 542 | bool silent = true; |
| 543 | #else |
| 544 | bool silent = false; |
| 545 | #endif |
| 546 | |
| 547 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("blas", silent, dir_path); |
| 548 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("zendnn", silent, dir_path); |
| 549 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("cann", silent, dir_path); |
| 550 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("cuda", silent, dir_path); |
| 551 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("hip", silent, dir_path); |
| 552 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("metal", silent, dir_path); |
| 553 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("rpc", silent, dir_path); |
| 554 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("sycl", silent, dir_path); |
| 555 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("vulkan", silent, dir_path); |
| 556 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("virtgpu", silent, dir_path); |
| 557 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("opencl", silent, dir_path); |
| 558 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("hexagon", silent, dir_path); |
| 559 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("musa", silent, dir_path); |
| 560 | ggml_backend_load_bestcxx . . $ ggml_backend_load_best(bc3db694b7fcc6f2).("cpu", silent, dir_path); |
| 561 | // check the environment variable GGML_BACKEND_PATH to load an out-of-tree backend |
| 562 | const char * backend_path = stdcxx . . $ std/::getenv("GGML_BACKEND_PATH"); |
| 563 | if (backend_path) { |
| 564 | ggml_backend_loadcxx . . $ ggml_backend_load(540634fdca863a2d).(backend_path); |
| 565 | } |
| 566 | } |