1use anyhow::Result;
17use rlx_core::{validate_sam_device, validate_standard_device};
18use rlx_runtime::Device;
19use std::str::FromStr;
20
21pub fn parse_device(s: &str) -> Result<Device> {
26 Device::from_str(s).map_err(|e| anyhow::anyhow!("{e}"))
27}
28
29pub fn parse_standard_device(family: &str, s: &str) -> Result<Device> {
31 let d = parse_device(s)?;
32 validate_standard_device(family, d)?;
33 Ok(d)
34}
35
36pub fn parse_lm_device(family: &str, s: &str) -> Result<Device> {
38 rlx_core::resolve_lm_device_str(family, s)
39}
40
41pub fn parse_llama32_device(s: &str) -> Result<Device> {
42 parse_lm_device("llama32", s)
43}
44
45pub fn parse_gemma_device(s: &str) -> Result<Device> {
46 parse_lm_device("gemma", s)
47}
48
49pub fn parse_qwen35_device(s: &str) -> Result<Device> {
50 parse_standard_device("qwen35", s)
51}
52
53pub fn parse_llada2_device(s: &str) -> Result<Device> {
54 parse_standard_device("llada2", s)
55}
56
57pub fn parse_sam_device(family: &str, s: &str) -> Result<Device> {
59 let d = match s {
60 "tpu" => Device::Tpu,
61 other => parse_device(other)?,
62 };
63 validate_sam_device(family, d)?;
64 Ok(d)
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[cfg(feature = "coreml")]
72 #[test]
73 fn parse_gemma_coreml_device() {
74 assert_eq!(parse_gemma_device("coreml").unwrap(), Device::Ane);
75 assert_eq!(parse_gemma_device("ane").unwrap(), Device::Ane);
76 }
77
78 #[test]
79 fn parse_gemma_auto_device() {
80 assert_eq!(
81 parse_gemma_device("auto").unwrap(),
82 rlx_core::pick_lm_device()
83 );
84 }
85}