added --sdmaingpu allowing image models to be independently placed on any gpu

This commit is contained in:
Concedo
2026-03-21 17:34:12 +08:00
parent a3d3800f3e
commit fdfb713d91
5 changed files with 61 additions and 11 deletions
+23 -1
View File
@@ -199,7 +199,29 @@ public:
void init_backend() {
#ifdef SD_USE_CUDA
LOG_DEBUG("Using CUDA backend");
backend = ggml_backend_cuda_init(0);
size_t device = 0; //kcpp: ported device selection from vulkan
const int device_count = ggml_backend_cuda_get_device_count();
if (device_count) {
const char* SD_VK_DEVICE = getenv("SD_VK_DEVICE");
if (SD_VK_DEVICE != nullptr) {
std::string sd_vk_device_str = SD_VK_DEVICE;
try {
device = std::stoull(sd_vk_device_str);
} catch (const std::invalid_argument&) {
LOG_WARN("SD_VK_DEVICE environment variable is not a valid integer (%s). Falling back to device 0.", SD_VK_DEVICE);
device = 0;
} catch (const std::out_of_range&) {
LOG_WARN("SD_VK_DEVICE environment variable value is out of range for `unsigned long long` type (%s). Falling back to device 0.", SD_VK_DEVICE);
device = 0;
}
if (device >= device_count) {
LOG_WARN("Cannot find targeted cuda device (%llu). Falling back to device 0.", device);
device = 0;
}
}
LOG_INFO("CUDA: Using device %llu", device);
}
backend = ggml_backend_cuda_init(device);
#endif
#ifdef SD_USE_METAL
LOG_DEBUG("Using Metal backend");