hyprstream_core/cli/
gpu_detect.rs1use hyprstream_tui::wizard::backend::{EnvironmentInfo, GpuKind, LibtorchVariant, RunMode};
8use std::path::Path;
9use tracing::debug;
10
11pub fn detect_gpu() -> GpuKind {
13 if let Ok(output) = std::process::Command::new("nvidia-smi")
15 .args(["--query-gpu=driver_version", "--format=csv,noheader"])
16 .output()
17 {
18 if output.status.success() {
19 let version = String::from_utf8_lossy(&output.stdout).trim().to_owned();
20 if !version.is_empty() {
21 let cuda_compat = driver_to_cuda_compat(&version);
22 debug!("Detected NVIDIA GPU: driver={version}, cuda_compat={cuda_compat}");
23 return GpuKind::Nvidia {
24 driver_version: version,
25 cuda_compat,
26 };
27 }
28 }
29 }
30
31 if let Ok(output) = std::process::Command::new("rocminfo").output() {
33 if output.status.success() {
34 let text = String::from_utf8_lossy(&output.stdout);
35 let version = parse_rocm_version(&text);
36 debug!("Detected AMD GPU: rocm_version={version:?}");
37 return GpuKind::Amd {
38 rocm_version: version,
39 };
40 }
41 }
42
43 debug!("No GPU detected");
44 GpuKind::None
45}
46
47fn driver_to_cuda_compat(driver_version: &str) -> String {
52 let mut parts = driver_version.split('.');
53 let major: u32 = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
54
55 if major >= 555 {
56 "cuda130".to_owned()
57 } else if major >= 535 {
58 "cuda128".to_owned()
59 } else {
60 "cpu".to_owned()
62 }
63}
64
65fn parse_rocm_version(rocminfo_output: &str) -> Option<String> {
67 for line in rocminfo_output.lines() {
69 if let Some(rest) = line.strip_prefix("ROCm Runtime Version:") {
70 let version = rest.trim().to_owned();
71 if !version.is_empty() {
72 return Some(version);
73 }
74 }
75 }
76 for line in rocminfo_output.lines() {
78 if line.contains("HSA Runtime Version") {
79 if let Some(v) = line.split(':').nth(1) {
80 let version = v.trim().to_owned();
81 if !version.is_empty() {
82 return Some(version);
83 }
84 }
85 }
86 }
87 None
88}
89
90pub fn detect_run_mode() -> RunMode {
92 if std::env::var("APPIMAGE").is_ok() {
93 RunMode::AppImage
94 } else if std::env::var("CARGO_MANIFEST_DIR").is_ok()
95 || std::env::var("HYPRSTREAM_LIBTORCH_CONFIGURED").is_ok()
96 {
97 RunMode::Development
98 } else {
99 RunMode::BareBinary
100 }
101}
102
103pub fn detect_installed_variant(data_dir: &Path) -> Option<LibtorchVariant> {
105 let active = std::fs::read_to_string(data_dir.join("active-backend")).ok()?;
106 variant_from_id(active.trim())
107}
108
109pub fn detect_active_version(data_dir: &Path) -> Option<String> {
114 std::fs::read_to_string(data_dir.join("active-version"))
115 .ok()
116 .map(|s| s.trim().to_owned())
117 .filter(|s| !s.is_empty())
118 .filter(|s| is_safe_version_string(s))
119}
120
121fn is_safe_version_string(v: &str) -> bool {
123 !v.is_empty()
124 && !v.contains("..")
125 && v.chars()
126 .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-')
127}
128
129pub fn variant_from_id(id: &str) -> Option<LibtorchVariant> {
131 match id {
132 "cpu" => Some(LibtorchVariant::Cpu),
133 "cuda128" => Some(LibtorchVariant::Cuda128),
134 "cuda130" => Some(LibtorchVariant::Cuda130),
135 "rocm71" => Some(LibtorchVariant::Rocm71),
136 _ => None,
137 }
138}
139
140pub fn recommend_variant(gpu: &GpuKind) -> LibtorchVariant {
142 match gpu {
143 GpuKind::Nvidia { cuda_compat, .. } => match cuda_compat.as_str() {
144 "cuda130" => LibtorchVariant::Cuda130,
145 "cuda128" => LibtorchVariant::Cuda128,
146 _ => LibtorchVariant::Cpu,
147 },
148 GpuKind::Amd {
149 rocm_version: Some(_),
150 } => LibtorchVariant::Rocm71,
151 _ => LibtorchVariant::Cpu,
152 }
153}
154
155pub fn detect_environment(data_dir: &Path) -> EnvironmentInfo {
157 let gpu = detect_gpu();
158 let run_mode = detect_run_mode();
159 let recommended = recommend_variant(&gpu);
160 let installed = detect_installed_variant(data_dir);
161
162 EnvironmentInfo {
163 run_mode,
164 gpu,
165 libtorch_path: std::env::var("LIBTORCH").ok(),
166 current_variant: LibtorchVariant::from_compiled(),
167 recommended_variant: recommended,
168 installed_variant: installed,
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175
176 #[test]
177 fn test_driver_to_cuda_compat() {
178 assert_eq!(driver_to_cuda_compat("555.42"), "cuda130");
179 assert_eq!(driver_to_cuda_compat("560.0"), "cuda130");
180 assert_eq!(driver_to_cuda_compat("535.104"), "cuda128");
181 assert_eq!(driver_to_cuda_compat("535.0"), "cuda128");
182 assert_eq!(driver_to_cuda_compat("530.0"), "cpu");
183 assert_eq!(driver_to_cuda_compat("470.82"), "cpu");
184 }
185
186 #[test]
187 fn test_parse_rocm_version() {
188 assert_eq!(
189 parse_rocm_version("ROCm Runtime Version: 7.1.0"),
190 Some("7.1.0".to_owned())
191 );
192 assert_eq!(
193 parse_rocm_version("some junk\nROCm Runtime Version: 6.2.0\nmore"),
194 Some("6.2.0".to_owned())
195 );
196 assert_eq!(parse_rocm_version("no version here"), None);
197 }
198
199 #[test]
200 fn test_variant_from_id() {
201 assert_eq!(variant_from_id("cpu"), Some(LibtorchVariant::Cpu));
202 assert_eq!(variant_from_id("cuda130"), Some(LibtorchVariant::Cuda130));
203 assert_eq!(variant_from_id("unknown"), None);
204 }
205
206 #[test]
207 fn test_recommend_variant() {
208 let nvidia = GpuKind::Nvidia {
209 driver_version: "555.0".to_owned(),
210 cuda_compat: "cuda130".to_owned(),
211 };
212 assert_eq!(recommend_variant(&nvidia), LibtorchVariant::Cuda130);
213
214 let amd = GpuKind::Amd {
215 rocm_version: Some("7.1.0".to_owned()),
216 };
217 assert_eq!(recommend_variant(&amd), LibtorchVariant::Rocm71);
218
219 assert_eq!(recommend_variant(&GpuKind::None), LibtorchVariant::Cpu);
220 }
221}