|
| 1 | +// Device placement configuration for model backends. |
| 2 | +// |
| 3 | +// Describes which backend device(s) to use for a model. Supports: |
| 4 | +// - Single-GPU: backend + gpu fields, exposed as cuda:0 / hip:0 / auto:0 |
| 5 | +// - Multi-GPU layer-split: one backend + layer_split_gpus + optional weights |
| 6 | +// - Peer access between GPUs |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include "placement_backend.h" |
| 11 | + |
| 12 | +#include <cstdlib> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +namespace dflash::common { |
| 17 | + |
| 18 | +struct DevicePlacement { |
| 19 | + PlacementBackend backend = PlacementBackend::Auto; |
| 20 | + int gpu = 0; // primary GPU (single-GPU mode) |
| 21 | + |
| 22 | + // Multi-GPU layer-split. Empty = single GPU mode. |
| 23 | + std::vector<int> layer_split_gpus; // GPU IDs for each shard |
| 24 | + std::vector<double> layer_split_weights; // proportional layer distribution (optional) |
| 25 | + |
| 26 | + bool peer_access = false; // enable CUDA/HIP peer access between GPUs |
| 27 | + int max_ctx = 8192; // max KV cache context length |
| 28 | + |
| 29 | + bool is_layer_split() const { return layer_split_gpus.size() > 1; } |
| 30 | + |
| 31 | + int primary_gpu() const { |
| 32 | + return layer_split_gpus.empty() ? gpu : layer_split_gpus[0]; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +inline std::string placement_device_name(const DevicePlacement & device) { |
| 37 | + return std::string(placement_backend_name(device.backend)) + ":" + |
| 38 | + std::to_string(device.primary_gpu()); |
| 39 | +} |
| 40 | + |
| 41 | +inline bool parse_placement_device(const std::string & value, |
| 42 | + DevicePlacement & out) { |
| 43 | + const std::size_t sep = value.find(':'); |
| 44 | + if (sep == std::string::npos || sep == 0 || sep + 1 >= value.size()) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + PlacementBackend backend = PlacementBackend::Auto; |
| 49 | + if (!parse_placement_backend(value.substr(0, sep), backend)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + const std::string gpu_text = value.substr(sep + 1); |
| 54 | + char * end = nullptr; |
| 55 | + long gpu = std::strtol(gpu_text.c_str(), &end, 10); |
| 56 | + if (end == gpu_text.c_str() || *end != '\0' || gpu < 0) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + out.backend = backend; |
| 61 | + out.gpu = static_cast<int>(gpu); |
| 62 | + out.layer_split_gpus.clear(); |
| 63 | + out.layer_split_weights.clear(); |
| 64 | + return true; |
| 65 | +} |
| 66 | + |
| 67 | +inline bool parse_placement_device_list(const std::string & value, |
| 68 | + DevicePlacement & out) { |
| 69 | + if (value.empty()) return false; |
| 70 | + |
| 71 | + std::vector<int> gpus; |
| 72 | + PlacementBackend backend = PlacementBackend::Auto; |
| 73 | + bool have_backend = false; |
| 74 | + |
| 75 | + std::size_t begin = 0; |
| 76 | + while (begin < value.size()) { |
| 77 | + const std::size_t end = value.find(',', begin); |
| 78 | + const std::string item = value.substr( |
| 79 | + begin, |
| 80 | + end == std::string::npos ? std::string::npos : end - begin); |
| 81 | + if (item.empty()) return false; |
| 82 | + |
| 83 | + DevicePlacement parsed; |
| 84 | + if (!parse_placement_device(item, parsed)) return false; |
| 85 | + if (!have_backend) { |
| 86 | + backend = parsed.backend; |
| 87 | + have_backend = true; |
| 88 | + } else if (parsed.backend != backend) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + gpus.push_back(parsed.gpu); |
| 92 | + |
| 93 | + if (end == std::string::npos) break; |
| 94 | + begin = end + 1; |
| 95 | + } |
| 96 | + |
| 97 | + if (gpus.empty()) return false; |
| 98 | + out.backend = backend; |
| 99 | + out.gpu = gpus[0]; |
| 100 | + out.layer_split_gpus = gpus.size() > 1 ? gpus : std::vector<int>{}; |
| 101 | + out.layer_split_weights.clear(); |
| 102 | + return true; |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace dflash::common |
0 commit comments