| 85 | const char * value = std::getenv("ENGINE_VALIDATE_BACKEND_GRAPH"); |
| 86 | return value != nullptr && std::strcmp(value, "1") == 0; |
| 87 | }(); |
| 88 | return enabled; |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | } |
| 93 | |
| 94 | ggml_backend_t init_backend(const BackendConfig & config) { |
| 95 | ensure_backends_loaded(); |
| 96 | switch (config.type) { |
| 97 | case BackendType::Cpu: { |
| 98 | ggml_backend_t backend = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr); |
| 99 | if (backend == nullptr) { |
| 100 | throw std::runtime_error("Failed to initialize CPU backend"); |
| 101 | } |
| 102 | return backend; |
| 103 | } |
| 104 | case BackendType::Cuda: { |
| 105 | if (config.device < 0) { |
| 106 | throw std::runtime_error("CUDA backend requested with negative device index"); |
| 107 | } |
| 108 | ggml_backend_dev_t device = find_device_by_backend_type(BackendType::Cuda, config.device); |
| 109 | if (device == nullptr) { |
| 110 | throw std::runtime_error("CUDA backend requested but no CUDA device found"); |
| 111 | } |
| 112 | return ggml_backend_dev_init(device, nullptr); |
| 113 | } |
| 114 | case BackendType::Vulkan: { |
| 115 | if (config.device < 0) { |
| 116 | throw std::runtime_error("Vulkan backend requested with negative device index"); |
| 117 | } |
| 118 | ggml_backend_dev_t device = find_device_by_backend_type(BackendType::Vulkan, config.device); |
| 119 | if (device == nullptr) { |
| 120 | throw std::runtime_error("Vulkan backend requested but no Vulkan device found"); |
| 121 | } |
| 122 | return ggml_backend_dev_init(device, nullptr); |
| 123 | } |
| 124 | case BackendType::Metal: { |
| 125 | if (config.device < 0) { |
| 126 | throw std::runtime_error("Metal backend requested with negative device index"); |
| 127 | } |
| 128 | ggml_backend_dev_t device = find_device_by_backend_type(BackendType::Metal, config.device); |
| 129 | if (device == nullptr) { |
| 130 | throw std::runtime_error("Metal backend requested but no Metal device found"); |
| 131 | } |
| 132 | return ggml_backend_dev_init(device, nullptr); |
| 133 | } |
| 134 | case BackendType::BestAvailable: { |
| 135 | ggml_backend_t backend = ggml_backend_init_best(); |
| 136 | if (backend == nullptr) { |
| 137 | throw std::runtime_error("Failed to initialize best backend"); |
| 138 | } |
| 139 | return backend; |
| 140 | } |
| 141 | default: |
| 142 | throw std::runtime_error("Unsupported backend type"); |
| 143 | } |
| 144 | } |