diff --git a/src/program/generate.cpp b/src/program/generate.cpp index ccaec33d..98a47411 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -646,6 +646,9 @@ void usage() { " --pack DIR the pack directory (default pack/full)\n" " --tokens LIST the prompt as comma-separated token IDS (required)\n" " --tokens-file PATH pretokenized prompt, commas or whitespace (alternative to --tokens)\n" + " --gpu LIST run on these GPUs, comma-separated nvidia-smi/PCI indices (default: every\n" + " visible one); sets CUDA_VISIBLE_DEVICES/CUDA_DEVICE_ORDER inside the engine,\n" + " so a caller never has to export environment variables\n" " --ple-gguf PATH required PLE table (original second GGUF shard); with --native, the model's\n" " shard that holds per_layer_token_embd.weight when not given\n" " --no-ple explicit diagnostic ablation of the PLE layer\n" @@ -1525,6 +1528,20 @@ int main(int argc, char** argv) { setenv("CUDA_MODULE_LOADING", "EAGER", 0); #endif } + // --gpu LIST pins the visible GPUs (nvidia-smi/PCI order) from the command line instead of the caller's + // environment: CUDA_VISIBLE_DEVICES is read at the first CUDA call, so this has to happen here, before + // anything else. The value itself is consumed again by the option loop below. + for (int i = 1; i + 1 < argc; ++i) { + if (std::string(argv[i]) != "--gpu") continue; +#if defined(_WIN32) + _putenv_s("CUDA_DEVICE_ORDER", "PCI_BUS_ID"); + _putenv_s("CUDA_VISIBLE_DEVICES", argv[i + 1]); +#else + setenv("CUDA_DEVICE_ORDER", "PCI_BUS_ID", 1); + setenv("CUDA_VISIBLE_DEVICES", argv[i + 1], 1); +#endif + break; + } Options o; bool have_tokens = false; bool have_logits_stride = false; @@ -1536,6 +1553,7 @@ int main(int argc, char** argv) { }; bool parsed = true; if (a == "--help" || a == "-h") { usage(); return 0; } + else if (a == "--gpu") { (void) next("--gpu"); } // applied at startup, before any CUDA call else if (a == "--pack") o.pack = next("--pack"); else if (a == "--tokens") { if (have_tokens) { std::fprintf(stderr, "supply one token input only\n"); return 2; }