1use rigspark_core::{catalog::CatalogModel, reports::strip_control, sizing::Hardware};
5use rigspark_runtime::hf_add::{best_fit, fits};
6
7#[derive(Debug)]
8pub enum Selection<'a> {
9 Chosen(&'a CatalogModel),
10 TooLarge(&'a CatalogModel),
12 NoneFits,
13 Unknown(String),
14}
15
16pub fn select<'a>(
18 entries: &'a [CatalogModel],
19 quant: Option<&str>,
20 hardware: &Hardware,
21 bypass: bool,
22) -> Selection<'a> {
23 match quant {
24 Some(quant) => match entries
25 .iter()
26 .find(|model| model.quantizations[0].name.eq_ignore_ascii_case(quant))
27 {
28 None => Selection::Unknown(quant.to_ascii_uppercase()),
29 Some(model) if bypass || fits(model, hardware) => Selection::Chosen(model),
30 Some(model) => Selection::TooLarge(model),
31 },
32 None => best_fit(entries, hardware).map_or(Selection::NoneFits, Selection::Chosen),
33 }
34}
35
36impl PartialEq for Selection<'_> {
37 fn eq(&self, other: &Self) -> bool {
38 match (self, other) {
39 (Self::Chosen(left), Self::Chosen(right))
40 | (Self::TooLarge(left), Self::TooLarge(right)) => left.id == right.id,
41 (Self::NoneFits, Self::NoneFits) => true,
42 (Self::Unknown(left), Self::Unknown(right)) => left == right,
43 _ => false,
44 }
45 }
46}
47
48pub fn choice_table(entries: &[CatalogModel], hardware: &Hardware, chosen: Option<&str>) -> String {
50 let mut text = String::from(" Quant Download Fits this machine\n");
51 for model in entries {
52 let quant = &model.quantizations[0];
53 let marker = if chosen == Some(model.id.as_str()) {
54 "▸"
55 } else {
56 " "
57 };
58 text.push_str(&format!(
59 "{marker} {:<11} {:>8.1} GB {}\n",
60 strip_control(&quant.name),
61 quant.disk_bytes / 1e9,
62 if fits(model, hardware) { "yes" } else { "no" }
63 ));
64 }
65 text
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use rigspark_core::sizing::{CpuArch, Platform};
72
73 fn entries() -> Vec<CatalogModel> {
74 let base = rigspark_core::catalog::Catalog::parse(rigspark_core::MODELS_JSON)
75 .unwrap()
76 .models
77 .into_iter()
78 .find(|model| model.id == "qwen2.5:0.5b")
79 .unwrap();
80 [("Q4_K_M", 4e8), ("Q8_0", 6e8)]
81 .into_iter()
82 .map(|(name, bytes)| {
83 let mut model = base.clone();
84 model.id = format!("hf.co/a/b:{}", name.to_ascii_lowercase());
85 model.quantizations[0].name = name.into();
86 model.quantizations[0].disk_bytes = bytes;
87 model.quantizations[0].min_ram_bytes = bytes;
88 model.quantizations[0].min_vram_bytes = bytes;
89 model
90 })
91 .collect()
92 }
93
94 fn machine(gib: f64) -> Hardware {
95 Hardware {
96 arch: CpuArch::Arm64,
97 platform: Platform::Darwin,
98 total_ram_bytes: gib * 1073741824.0,
99 free_ram_bytes: gib * 1073741824.0,
100 free_disk_bytes: 500e9,
101 gpu: vec![],
102 unified_memory: Some(true),
103 }
104 }
105
106 #[test]
107 fn explicit_quants_are_honoured_or_refused_and_the_default_is_the_best_fit() {
108 let all = entries();
109 let roomy = machine(64.0);
110 assert_eq!(
111 select(&all, None, &roomy, false),
112 Selection::Chosen(&all[1])
113 );
114 assert_eq!(
115 select(&all, Some("q4_k_m"), &roomy, false),
116 Selection::Chosen(&all[0])
117 );
118 assert_eq!(
119 select(&all, Some("Q3_K_S"), &roomy, false),
120 Selection::Unknown("Q3_K_S".into())
121 );
122 let tiny = machine(0.1);
123 assert_eq!(select(&all, None, &tiny, false), Selection::NoneFits);
124 assert_eq!(
125 select(&all, Some("Q8_0"), &tiny, false),
126 Selection::TooLarge(&all[1])
127 );
128 assert_eq!(
129 select(&all, Some("Q8_0"), &tiny, true),
130 Selection::Chosen(&all[1])
131 );
132 }
133
134 #[test]
135 fn the_table_marks_the_choice_and_fit() {
136 let all = entries();
137 let table = choice_table(&all, &machine(64.0), Some("hf.co/a/b:q8_0"));
138 assert!(table.contains("▸ Q8_0"), "{table}");
139 assert!(table.contains(" Q4_K_M"), "{table}");
140 assert_eq!(table.matches("yes").count(), 2, "{table}");
141 }
142}