1 //! The target profile, checked against constructed hosts.2 3 use std::collections::BTreeSet;4 5 use inf3r_host::{Gpu, Host, Limits, Profile};6 7 const MIB: u64 = 1 << 20;8 const GB: u64 = 1024 * MIB;9 10 fn gpu_at(id: &str, compute: &str, bytes: u64) -> Gpu {11 Gpu {12 id: id.into(),13 name: "Synthetic GPU".into(),14 bus: "0000:05:00.0".into(),15 bytes: Some(bytes),16 free_bytes: Some(bytes),17 compute: Some(compute.into()),18 }19 }20 21 fn host(compute: &str) -> Host {22 Host {23 arch: "x86_64".into(),24 driver: Some("999.0.0".into()),25 cuda: Some("13.5".into()),26 gpus: vec![gpu_at("cuda:0", compute, 12 * GB)],27 sonames: ["libcudnn.so.9", "libcublas.so.12"]28 .into_iter()29 .map(str::to_string)30 .collect::<BTreeSet<_>>(),31 }32 }33 34 fn profile() -> Profile {35 Profile {36 name: "gpu".into(),37 backend: "cuda".into(),38 arch: vec!["x86_64".into()],39 compute: Some("sm00".into()),40 devices: 1,41 vram_mb: vec![1024],42 driver_min: Some("550".into()),43 cuda_min: Some("13.5".into()),44 requires: vec!["cublas".into(), "cudnn>=9".into()],45 limits: Limits::default(),46 }47 }48 49 #[test]50 fn a_met_profile_passes() {51 assert_eq!(profile().check(&host("sm00"), &["cuda:0"]), Ok(()));52 }53 54 #[test]55 fn a_higher_compute_requirement_fails_on_the_measured_device() {56 let profile = Profile {57 compute: Some("sm01".into()),58 ..profile()59 };60 let mismatch = profile.check(&host("sm00"), &["cuda:0"]).unwrap_err();61 assert_eq!(mismatch.field, "compute");62 assert_eq!(mismatch.required, "sm01");63 assert_eq!(mismatch.measured, "sm00");64 }65 66 #[test]67 fn a_mixed_host_checks_only_the_selected_card() {68 let mut host = host("sm02");69 host.gpus.push(gpu_at("cuda:1", "sm00", 12 * GB));70 71 // The profile targets the synthetic capability. The second card does not72 // make the bundle invalid, and its capability does not decide the check.73 assert_eq!(profile().check(&host, &["cuda:1"]), Ok(()));74 75 let mismatch = profile().check(&host, &["cuda:0"]).unwrap_err();76 assert_eq!(mismatch.field, "compute");77 assert_eq!(mismatch.measured, "sm02");78 }79 80 #[test]81 fn a_vram_requirement_follows_the_selected_index_not_the_host_index() {82 let mut host = host("sm00");83 host.gpus[0].bytes = Some(512 * MIB);84 host.gpus.push(gpu_at("cuda:1", "sm00", 12 * GB));85 86 assert_eq!(profile().check(&host, &["cuda:1"]), Ok(()));87 88 let mismatch = profile().check(&host, &["cuda:0"]).unwrap_err();89 assert_eq!(mismatch.field, "vram_mb");90 assert_eq!(mismatch.required, (1024 * MIB).to_string());91 assert_eq!(mismatch.measured, (512 * MIB).to_string());92 }93 94 #[test]95 fn a_selected_device_from_another_backend_is_refused() {96 let mut host = host("sm00");97 host.gpus.push(Gpu {98 id: "rocm:0".into(),99 name: "Synthetic GPU".into(),100 bus: "0000:0a:00.0".into(),101 bytes: Some(16 * GB),102 free_bytes: Some(16 * GB),103 compute: Some("gfx0000".into()),104 });105 106 let mismatch = profile().check(&host, &["rocm:0"]).unwrap_err();107 assert_eq!(mismatch.field, "backend");108 assert_eq!(mismatch.required, "cuda");109 assert_eq!(mismatch.measured, "rocm");110 }111 112 #[test]113 fn a_selected_device_that_the_host_does_not_have_is_named() {114 let mismatch = profile().check(&host("sm00"), &["cuda:9"]).unwrap_err();115 assert_eq!(mismatch.field, "devices");116 assert_eq!(mismatch.required, "cuda:9");117 assert_eq!(mismatch.measured, "absent");118 }119 120 #[test]121 fn a_bind_with_too_few_devices_is_refused_before_a_memory_check() {122 let profile = Profile {123 devices: 2,124 vram_mb: vec![1024, 1024],125 ..profile()126 };127 let mismatch = profile.check(&host("sm00"), &["cuda:0"]).unwrap_err();128 assert_eq!(mismatch.field, "devices");129 assert_eq!(mismatch.required, "2");130 assert_eq!(mismatch.measured, "1");131 }132 133 #[test]134 fn a_cuda_floor_failure_names_both_versions() {135 let profile = Profile {136 cuda_min: Some("14.0".into()),137 ..profile()138 };139 let mismatch = profile.check(&host("sm00"), &["cuda:0"]).unwrap_err();140 assert_eq!(mismatch.field, "cuda_min");141 assert_eq!(mismatch.required, "14.0");142 assert_eq!(mismatch.measured, "13.5");143 }144 145 #[test]146 fn a_host_without_a_cuda_version_cannot_meet_a_cuda_floor() {147 let mut host = host("sm00");148 host.cuda = None;149 let mismatch = profile().check(&host, &["cuda:0"]).unwrap_err();150 assert_eq!(mismatch.field, "cuda_min");151 assert_eq!(mismatch.measured, "unknown");152 }153 154 #[test]155 fn a_cpu_profile_needs_no_device() {156 let profile = Profile {157 backend: "cpu".into(),158 compute: None,159 devices: 0,160 vram_mb: Vec::new(),161 cuda_min: None,162 ..profile()163 };164 let mut host = host("sm00");165 host.gpus.clear();166 assert_eq!(profile.check(&host, &[] as &[&str]), Ok(()));167 }168 169 #[test]170 fn a_cpu_profile_can_name_the_cpu_device() {171 let profile = Profile {172 backend: "cpu".into(),173 compute: None,174 devices: 0,175 vram_mb: Vec::new(),176 cuda_min: None,177 ..profile()178 };179 let mut host = host("sm00");180 host.gpus = vec![Gpu {181 id: "cpu:0".into(),182 name: "CPU".into(),183 bus: "cpu".into(),184 bytes: Some(32 * GB),185 free_bytes: Some(20 * GB),186 compute: None,187 }];188 assert_eq!(profile.check(&host, &["cpu:0"]), Ok(()));189 }190 191 #[test]192 fn a_missing_device_fails_the_vram_check() {193 let mut host = host("sm00");194 host.gpus[0].bytes = Some(512 * MIB);195 let mismatch = profile().check(&host, &["cuda:0"]).unwrap_err();196 assert_eq!(mismatch.field, "vram_mb");197 assert_eq!(mismatch.required, (1024 * MIB).to_string());198 assert_eq!(mismatch.measured, (512 * MIB).to_string());199 }200 201 #[test]202 fn a_rocm_profile_checks_its_own_backend_and_capability() {203 let profile = Profile {204 backend: "rocm".into(),205 compute: Some("gfx0000".into()),206 vram_mb: vec![16000],207 cuda_min: None,208 driver_min: None,209 requires: Vec::new(),210 ..profile()211 };212 let mut host = host("sm00");213 host.gpus = vec![Gpu {214 id: "rocm:0".into(),215 name: "Synthetic GPU".into(),216 bus: "0000:0a:00.0".into(),217 bytes: Some(16 * GB),218 free_bytes: Some(16 * GB),219 compute: Some("gfx0000".into()),220 }];221 assert_eq!(profile.check(&host, &["rocm:0"]), Ok(()));222 }223 224 #[test]225 fn a_cuda_floor_on_another_backend_is_refused() {226 let profile = Profile {227 backend: "rocm".into(),228 compute: Some("gfx0000".into()),229 vram_mb: vec![16000],230 cuda_min: Some("13.5".into()),231 driver_min: None,232 requires: Vec::new(),233 ..profile()234 };235 let mut host = host("sm00");236 host.gpus = vec![Gpu {237 id: "rocm:0".into(),238 name: "Synthetic GPU".into(),239 bus: "0000:0a:00.0".into(),240 bytes: Some(16 * GB),241 free_bytes: Some(16 * GB),242 compute: Some("gfx0000".into()),243 }];244 let mismatch = profile.check(&host, &["rocm:0"]).unwrap_err();245 assert_eq!(mismatch.field, "cuda_min");246 assert_eq!(mismatch.measured, "rocm target");247 }248 249 #[test]250 fn a_profile_is_read_from_a_file_and_an_unknown_key_is_refused() {251 let dir = std::env::temp_dir().join(format!("inf3r-profile-{}", std::process::id()));252 let _ = std::fs::remove_dir_all(&dir);253 std::fs::create_dir_all(&dir).expect("a temporary directory");254 255 let path = dir.join("target.toml");256 std::fs::write(257 &path,258 "[target]\nname = \"gpu\"\nbackend = \"cuda\"\narch = [\"x86_64\"]\ncompute = \"sm00\"\ndevices = 1\nvram_mb = [1024]\ndriver_min = \"550\"\ncuda_min = \"13.5\"\nrequires = [\"cublas\", \"cudnn>=9\"]\n",259 )260 .expect("a target profile");261 let read = Profile::read(&path).expect("a valid profile");262 assert_eq!(read, profile());263 264 std::fs::write(&path, "[target]\nname = \"x\"\nmystery = 1\n").expect("a bad profile");265 let err = Profile::read(&path).expect_err("an unknown key is refused");266 assert!(err.to_string().contains("line 3"), "{err}");267 268 let _ = std::fs::remove_dir_all(&dir);269 }270 271 #[test]272 fn the_limits_section_is_kept_on_the_profile() {273 let dir = std::env::temp_dir().join(format!("inf3r-profile-limits-{}", std::process::id()));274 let _ = std::fs::remove_dir_all(&dir);275 std::fs::create_dir_all(&dir).expect("a temporary directory");276 let path = dir.join("target.toml");277 std::fs::write(278 &path,279 "[target]\nname = \"gpu\"\nbackend = \"cuda\"\narch = [\"x86_64\"]\ncompute = \"sm00\"\ndevices = 1\nvram_mb = [1024]\n\n[limits]\nmax_inflight = 8\nmax_part_bytes = 268435456\nload_ms = 9000\nstart_ms = 30000\naccounting = \"declared\"\n",280 )281 .expect("a target profile");282 let read = Profile::read(&path).expect("a valid profile");283 assert_eq!(284 read.limits,285 Limits {286 max_inflight: Some(8),287 max_part_bytes: Some(268435456),288 load_ms: Some(9000),289 start_ms: Some(30000),290 accounting: Some("declared".into()),291 vram_bytes: None,292 ram_bytes: None,293 }294 );295 let _ = std::fs::remove_dir_all(&dir);296 }297 298 #[test]299 fn a_limit_that_does_not_fit_its_field_is_refused_with_its_line() {300 let dir = std::env::temp_dir().join(format!("inf3r-profile-bad-limit-{}", std::process::id()));301 let _ = std::fs::remove_dir_all(&dir);302 std::fs::create_dir_all(&dir).expect("a temporary directory");303 let path = dir.join("target.toml");304 std::fs::write(305 &path,306 "[target]\nname = \"x\"\nbackend = \"cuda\"\narch = [\"x86_64\"]\ndevices = 1\nvram_mb = []\n\n[limits]\nmax_inflight = 70000\n",307 )308 .expect("a target profile");309 let err = Profile::read(&path).expect_err("an oversized limit is refused");310 assert!(err.to_string().contains("line 9"), "{err}");311 assert!(err.to_string().contains("max_inflight"), "{err}");312 let _ = std::fs::remove_dir_all(&dir);313 }314 315 #[test]316 fn a_split_target_has_one_device_count_and_one_ram_count() {317 let dir = std::env::temp_dir().join(format!("inf3r-profile-split-{}", std::process::id()));318 let _ = std::fs::remove_dir_all(&dir);319 std::fs::create_dir_all(&dir).expect("a temporary directory");320 let path = dir.join("target.toml");321 std::fs::write(322 &path,323 "[target]\nname = \"one\"\nbackend = \"cuda\"\narch = [\"x86_64\"]\ndevices = 1\nvram_mb = [8192]\n\n[limits]\naccounting = \"split\"\nvram_bytes = [7000000000]\nram_bytes = 23755318754\n",324 )325 .expect("a target profile");326 let read = Profile::read(&path).expect("a valid profile");327 assert_eq!(328 read.split_resident(),329 Some((vec![7_000_000_000], 23_755_318_754))330 );331 let _ = std::fs::remove_dir_all(&dir);332 }333 334 #[test]335 fn split_bytes_without_split_accounting_are_refused() {336 let dir = std::env::temp_dir().join(format!("inf3r-profile-split-bad-{}", std::process::id()));337 let _ = std::fs::remove_dir_all(&dir);338 std::fs::create_dir_all(&dir).expect("a temporary directory");339 let path = dir.join("target.toml");340 std::fs::write(341 &path,342 "[target]\nname = \"one\"\nbackend = \"cuda\"\narch = [\"x86_64\"]\ndevices = 1\nvram_mb = [8192]\n\n[limits]\nvram_bytes = [7000000000]\n",343 )344 .expect("a target profile");345 let err = Profile::read(&path).expect_err("split bytes need split accounting");346 assert!(err.to_string().contains("split"), "{err}");347 let _ = std::fs::remove_dir_all(&dir);348 }