1use crate::ModelSpec;
4use serde::{Deserialize, Serialize};
5
6use super::{Accel, GIB, Hardware};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11#[non_exhaustive]
12pub enum Purpose {
13 Chat,
14 Embedding,
15}
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct CatalogEntry {
20 pub spec: ModelSpec,
21 pub purpose: Purpose,
22 pub quant: String,
23 pub params_b: f32,
25 pub active_params_b: f32,
26 pub kv_bytes_per_token: u64,
28 pub context: u32,
30}
31
32impl CatalogEntry {
33 pub fn id(&self) -> &str {
34 &self.spec.id
35 }
36
37 pub fn footprint(&self, slots: u32) -> u64 {
40 let kv = self.kv_bytes_per_token * self.context as u64 * slots as u64;
41 (self.spec.size + kv) + (self.spec.size + kv) / 10
42 }
43}
44
45fn spec(id: &str, label: &str, repo: &str, file: &str, size: u64, sha256: &str) -> ModelSpec {
46 ModelSpec { id: id.into(), label: label.into(), repo: repo.into(), file: file.into(), size, sha256: Some(sha256.into()) }
47}
48
49#[allow(clippy::too_many_arguments)]
50fn chat(spec: ModelSpec, quant: &str, params_b: f32, active_params_b: f32, kv_bytes_per_token: u64, context: u32) -> CatalogEntry {
51 CatalogEntry { spec, purpose: Purpose::Chat, quant: quant.into(), params_b, active_params_b, kv_bytes_per_token, context }
52}
53
54pub fn catalog() -> Vec<CatalogEntry> {
57 let gemma = |id: &str| crate::catalog::find(id).expect("weirdnet-model ships Gemma 4");
58 vec![
59 chat(
60 spec(
61 "smollm2-135m",
62 "SmolLM2 135M instruct (Q8_0)",
63 "bartowski/SmolLM2-135M-Instruct-GGUF",
64 "SmolLM2-135M-Instruct-Q8_0.gguf",
65 144_811_360,
66 "5a1395716f7913741cc51d98581b9b1228d80987a9f7d3664106742eb06bba83",
67 ),
68 "Q8_0",
69 0.135,
70 0.135,
71 23_040,
72 8192,
73 ),
74 chat(gemma(crate::catalog::GEMMA_4_E2B), "Q4_0", 5.1, 2.0, 32_768, 8192),
75 chat(gemma(crate::catalog::GEMMA_4_E4B), "Q4_0", 8.0, 4.0, 65_536, 8192),
76 chat(
77 spec(
78 "qwen3-8b",
79 "Qwen3 8B (Q4_K_M)",
80 "unsloth/Qwen3-8B-GGUF",
81 "Qwen3-8B-Q4_K_M.gguf",
82 5_027_784_512,
83 "120307ba529eb2439d6c430d94104dabd578497bc7bfe7e322b5d9933b449bd4",
84 ),
85 "Q4_K_M",
86 8.2,
87 8.2,
88 147_456,
89 8192,
90 ),
91 chat(
92 spec(
93 "qwen3-14b",
94 "Qwen3 14B (Q4_K_M)",
95 "unsloth/Qwen3-14B-GGUF",
96 "Qwen3-14B-Q4_K_M.gguf",
97 9_001_753_984,
98 "5eaa0870bd81ed3b58a630a271234cfa604e43ffb3a19cd68e54a80dd9d52a66",
99 ),
100 "Q4_K_M",
101 14.8,
102 14.8,
103 163_840,
104 8192,
105 ),
106 chat(
107 spec(
108 "gpt-oss-20b",
109 "gpt-oss 20B (MXFP4)",
110 "ggml-org/gpt-oss-20b-GGUF",
111 "gpt-oss-20b-MXFP4.gguf",
112 12_109_566_624,
113 "27cd6c432c7672cb812a92f611cf3ba7bbc35928262bb1e1253ff4ee6ae35901",
114 ),
115 "MXFP4",
116 21.0,
117 3.6,
118 49_152,
119 8192,
120 ),
121 chat(
122 spec(
123 "qwen3-30b-a3b",
124 "Qwen3 30B-A3B (Q4_K_M)",
125 "unsloth/Qwen3-30B-A3B-GGUF",
126 "Qwen3-30B-A3B-Q4_K_M.gguf",
127 18_556_686_912,
128 "9f1a24700a339b09c06009b729b5c809e0b64c213b8af5b711b3dbdfd0c5ba48",
129 ),
130 "Q4_K_M",
131 30.5,
132 3.3,
133 98_304,
134 8192,
135 ),
136 chat(
137 spec(
138 "qwen3-32b",
139 "Qwen3 32B (Q4_K_M)",
140 "unsloth/Qwen3-32B-GGUF",
141 "Qwen3-32B-Q4_K_M.gguf",
142 19_762_150_048,
143 "8df67573b2c23484e02ec7af295e39bed7ee774f3771d5fda2978265b59370e7",
144 ),
145 "Q4_K_M",
146 32.8,
147 32.8,
148 262_144,
149 8192,
150 ),
151 CatalogEntry {
152 spec: spec(
153 "qwen3-embedding-0.6b",
154 "Qwen3 Embedding 0.6B (Q8_0)",
155 "Qwen/Qwen3-Embedding-0.6B-GGUF",
156 "Qwen3-Embedding-0.6B-Q8_0.gguf",
157 639_150_592,
158 "06507c7b42688469c4e7298b0a1e16deff06caf291cf0a5b278c308249c3e439",
159 ),
160 purpose: Purpose::Embedding,
161 quant: "Q8_0".into(),
162 params_b: 0.6,
163 active_params_b: 0.6,
164 kv_bytes_per_token: 114_688,
165 context: 2048,
166 },
167 ]
168}
169
170pub fn find(id: &str) -> Option<CatalogEntry> {
172 catalog().into_iter().find(|e| e.spec.id == id)
173}
174
175#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
177pub struct Budget {
178 pub pool_bytes: u64,
180 pub reserve_bytes: u64,
182 pub usable_bytes: u64,
183}
184
185pub fn budget(hw: &Hardware, memory_budget: Option<u64>, reserve: Option<u64>) -> Budget {
188 let unified = hw.unified();
189 let pool_bytes = if unified { hw.ram_bytes } else { hw.gpus.first().map(|g| g.memory_bytes).unwrap_or(hw.ram_bytes) };
190 let reserve_bytes = if unified { reserve.unwrap_or_else(|| (hw.ram_bytes / 8).max(4 * GIB)) } else { 0 };
191 let usable = pool_bytes.saturating_sub(reserve_bytes) / 100 * 85;
192 Budget { pool_bytes, reserve_bytes, usable_bytes: memory_budget.unwrap_or(usable) }
193}
194
195#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
197pub struct Plan {
198 pub budget: Budget,
199 pub main: Option<CatalogEntry>,
200 pub standin: Option<CatalogEntry>,
201 pub embedding: Option<CatalogEntry>,
202 pub slots: u32,
204 pub notes: Vec<String>,
205}
206
207pub fn plan(hw: &Hardware, budget: Budget) -> Plan {
211 let slots = 2;
212 let cpu_only = hw.accel() == Accel::Cpu;
213 let mut notes = Vec::new();
214 let all = catalog();
215 let fits = |e: &&CatalogEntry| e.footprint(slots) <= budget.usable_bytes;
216 let main = all
217 .iter()
218 .filter(|e| e.purpose == Purpose::Chat && e.spec.id != "smollm2-135m")
219 .filter(|e| !cpu_only || e.active_params_b <= 4.0)
220 .filter(fits)
221 .max_by(|a, b| a.params_b.total_cmp(&b.params_b))
222 .cloned();
223 if cpu_only {
224 notes.push("CPU only: limited to models with at most 4B active parameters".into());
225 }
226 if main.is_none() {
227 notes.push("no catalog chat model fits; add a smaller GGUF with `ymirctl setup --model PATH`".into());
228 }
229 let standin_id = if budget.usable_bytes >= 16 * GIB { crate::catalog::GEMMA_4_E4B } else { crate::catalog::GEMMA_4_E2B };
230 let standin = all.iter().find(|e| e.spec.id == standin_id).filter(fits).filter(|e| main.as_ref().is_none_or(|m| m.spec.id != e.spec.id)).cloned();
231 let embedding = all.iter().find(|e| e.purpose == Purpose::Embedding).filter(|e| e.footprint(1) <= budget.usable_bytes).cloned();
232 Plan { budget, main, standin, embedding, slots, notes }
233}
234
235#[cfg(test)]
236mod tests {
237 use super::*;
238 use crate::hw::{Gpu, parse_nvidia_smi};
239
240 fn box_with(gpus: Vec<Gpu>, ram_gib: u64) -> Hardware {
241 Hardware { os: "linux".into(), arch: "x86_64".into(), cpu_threads: 16, ram_bytes: ram_gib * GIB, gpus }
242 }
243
244 #[test]
245 fn rtx_3060_gets_an_8b() {
246 let hw = box_with(parse_nvidia_smi("NVIDIA GeForce RTX 3060, 12288, 8.6, 615.78.08", 62 * GIB), 62);
247 let b = budget(&hw, None, None);
248 assert_eq!(b.pool_bytes, 12 * GIB);
249 let p = plan(&hw, b);
250 assert_eq!(p.main.unwrap().id(), "qwen3-8b");
251 assert_eq!(p.standin.unwrap().id(), "gemma-4-e2b");
252 assert!(p.embedding.is_some());
253 }
254
255 #[test]
256 fn spark_gets_a_big_model() {
257 let hw = box_with(parse_nvidia_smi("NVIDIA GB10, [N/A], 12.1, 580.95.05", 128 * GIB), 128);
258 let b = budget(&hw, None, None);
259 assert_eq!(b.reserve_bytes, 16 * GIB);
260 let p = plan(&hw, b);
261 assert_eq!(p.main.unwrap().id(), "qwen3-32b");
262 assert_eq!(p.standin.unwrap().id(), "gemma-4-e4b");
263 }
264
265 #[test]
266 fn cpu_box_prefers_few_active_params() {
267 let hw = box_with(Vec::new(), 62);
268 let p = plan(&hw, budget(&hw, None, None));
269 assert_eq!(p.main.unwrap().id(), "qwen3-30b-a3b");
270 }
271
272 #[test]
273 fn tiny_box_gets_no_main() {
274 let hw = box_with(Vec::new(), 4);
275 let p = plan(&hw, budget(&hw, Some(GIB), None));
276 assert!(p.main.is_none());
277 assert!(!p.notes.is_empty());
278 }
279
280 #[test]
281 fn override_budget() {
282 let hw = box_with(Vec::new(), 64);
283 assert_eq!(budget(&hw, Some(10 * GIB), None).usable_bytes, 10 * GIB);
284 assert_eq!(budget(&hw, None, Some(14 * GIB)).reserve_bytes, 14 * GIB);
285 }
286}