1use crate::ModelSpec;
4use crate::speech::{self, CloneMode, SpeechProfile};
5use serde::{Deserialize, Serialize};
6
7use super::{Accel, GIB, Hardware};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case")]
13#[non_exhaustive]
14pub enum Purpose {
15 Chat,
16 Embedding,
17 Transcribe,
20 Speak,
24}
25
26impl Purpose {
27 pub const ALL: [Purpose; 4] = [Purpose::Chat, Purpose::Embedding, Purpose::Transcribe, Purpose::Speak];
29
30 pub fn as_str(&self) -> &'static str {
32 match self {
33 Purpose::Chat => "chat",
34 Purpose::Embedding => "embedding",
35 Purpose::Transcribe => "transcribe",
36 Purpose::Speak => "speak",
37 }
38 }
39
40 pub fn parse(name: &str) -> Option<Purpose> {
42 Purpose::ALL.into_iter().find(|p| p.as_str() == name)
43 }
44}
45
46impl std::fmt::Display for Purpose {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 f.write_str(self.as_str())
49 }
50}
51
52pub const VOXTRAL_MINI_3B: &str = "voxtral-mini-3b";
55
56pub const QWEN3_TTS_0_6B_CUSTOMVOICE: &str = "qwen3-tts-0.6b-customvoice";
61pub const QWEN3_TTS_0_6B_BASE: &str = "qwen3-tts-0.6b-base";
63pub const QWEN3_TTS_1_7B_CUSTOMVOICE: &str = "qwen3-tts-1.7b-customvoice";
65pub const QWEN3_TTS_1_7B_VOICEDESIGN: &str = "qwen3-tts-1.7b-voicedesign";
67pub const QWEN3_TTS_1_7B_BASE: &str = "qwen3-tts-1.7b-base";
69pub const KOKORO_82M: &str = "kokoro-82m";
71pub const VOXCPM2: &str = "voxcpm2";
73pub const FISH_AUDIO_DRAMA_3: &str = "fish-audio-drama-3";
78
79const QWEN3_TTS_TOKENIZER_BYTES: u64 = 682_293_092;
82const DRAMA_3_UPSTREAM: &str = "fishaudio/drama-3";
84
85#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87pub struct CatalogEntry {
88 pub spec: ModelSpec,
89 pub purpose: Purpose,
90 pub quant: String,
91 pub params_b: f32,
93 pub active_params_b: f32,
94 pub kv_bytes_per_token: u64,
96 pub context: u32,
98}
99
100impl CatalogEntry {
101 pub fn id(&self) -> &str {
102 &self.spec.id
103 }
104
105 pub fn footprint(&self, slots: u32) -> u64 {
109 let kv = self.kv_bytes_per_token * self.context as u64 * slots as u64;
110 let weights = self.spec.size + self.projector().map_or(0, |p| p.size) + self.companion_bytes();
111 (weights + kv) + (weights + kv) / 10
112 }
113
114 fn companion_bytes(&self) -> u64 {
115 match self.spec.id.as_str() {
116 QWEN3_TTS_0_6B_CUSTOMVOICE | QWEN3_TTS_0_6B_BASE | QWEN3_TTS_1_7B_CUSTOMVOICE | QWEN3_TTS_1_7B_VOICEDESIGN => QWEN3_TTS_TOKENIZER_BYTES,
117 _ => 0,
118 }
119 }
120
121 pub fn is_gguf(&self) -> bool {
126 self.spec.file.to_ascii_lowercase().ends_with(".gguf")
127 }
128
129 pub fn speaks(&self) -> bool {
131 self.purpose == Purpose::Speak
132 }
133
134 pub fn license(&self) -> Option<&'static str> {
138 match self.spec.id.as_str() {
139 FISH_AUDIO_DRAMA_3 => Some(speech::FISH_AUDIO_COMMUNITY),
140 VOXTRAL_MINI_3B
141 | QWEN3_TTS_0_6B_CUSTOMVOICE
142 | QWEN3_TTS_0_6B_BASE
143 | QWEN3_TTS_1_7B_CUSTOMVOICE
144 | QWEN3_TTS_1_7B_VOICEDESIGN
145 | QWEN3_TTS_1_7B_BASE
146 | KOKORO_82M
147 | VOXCPM2 => Some("apache-2.0"),
148 _ => None,
149 }
150 }
151
152 pub fn restricted(&self) -> bool {
157 speech::restricting_license(self.license()).is_some()
158 }
159
160 pub fn speech(&self) -> Option<SpeechProfile> {
179 if !self.speaks() {
180 return None;
181 }
182 let qwen_vllm = || speech::vllm_omni().with_languages(speech::QWEN3_TTS_LANGUAGES).with_sample_rate(24_000);
183 Some(match self.spec.id.as_str() {
184 QWEN3_TTS_0_6B_CUSTOMVOICE => qwen_vllm().with_presets(QWEN3_TTS_SPEAKERS),
185 QWEN3_TTS_1_7B_CUSTOMVOICE => qwen_vllm().with_presets(QWEN3_TTS_SPEAKERS).with_instructions(true),
186 QWEN3_TTS_1_7B_VOICEDESIGN => qwen_vllm().with_design(true),
187 QWEN3_TTS_0_6B_BASE => qwen_vllm().with_clone(CloneMode::AudioAndText),
188 QWEN3_TTS_1_7B_BASE => {
189 speech::llama_server().with_languages(speech::QWEN3_TTS_LANGUAGES).with_sample_rate(24_000).with_clone(CloneMode::Audio)
190 }
191 KOKORO_82M => speech::llama_server()
192 .with_presets(KOKORO_VOICES)
193 .with_languages(speech::KOKORO_LANGUAGES)
194 .with_raw(true)
195 .with_speed(true)
196 .with_sample_rate(24_000),
197 VOXCPM2 => speech::llama_server()
198 .with_design(true)
199 .with_clone(CloneMode::Audio)
200 .with_max_reference_seconds(30)
201 .with_languages(VOXCPM2_LANGUAGES)
202 .with_sample_rate(48_000),
203 FISH_AUDIO_DRAMA_3 => speech::llama_server()
204 .with_script_speakers(16)
205 .with_languages(DRAMA_3_LANGUAGES)
206 .with_sample_rate(44_100)
207 .with_license(speech::FISH_AUDIO_COMMUNITY),
208 _ => return None,
209 })
210 }
211
212 fn license_repos(&self) -> Vec<&str> {
215 let mut repos = vec![self.spec.repo.as_str()];
216 if self.spec.id == FISH_AUDIO_DRAMA_3 {
217 repos.push(DRAMA_3_UPSTREAM);
218 }
219 repos
220 }
221
222 pub fn projector(&self) -> Option<ModelSpec> {
228 match self.spec.id.as_str() {
229 VOXTRAL_MINI_3B => Some(spec(
230 "voxtral-mini-3b-mmproj",
231 "Voxtral Mini 3B audio encoder (Q8_0)",
232 "ggml-org/Voxtral-Mini-3B-2507-GGUF",
233 "mmproj-Voxtral-Mini-3B-2507-Q8_0.gguf",
234 715_714_080,
235 "4f24c4ef3ce929d02ed9d1cfb050ae9a7365f057c0ddec0d489580982ebe0d02",
236 )),
237 QWEN3_TTS_1_7B_BASE => Some(spec(
238 "qwen3-tts-1.7b-base-mmproj",
239 "Qwen3-TTS 1.7B Base speaker encoder and codec (Q8_0)",
240 "ggml-org/Qwen3-TTS-12Hz-1.7B-Base-GGUF",
241 "mmproj-Qwen3-TTS-12Hz-1.7B-Base-Q8_0.gguf",
242 446_422_912,
243 "6fd65188839bcd6ecc91b277ad471e22a0edfada4699a0fe82f1165c18cfcce2",
244 )),
245 KOKORO_82M => Some(spec(
246 "kokoro-82m-mmproj",
247 "Kokoro 82M decoder and voices (F16)",
248 "ggml-org/Kokoro-82M-GGUF",
249 "mmproj-Kokoro-82M-F16.gguf",
250 208_456_512,
251 "071d20f53b7c5d64b08a24bff5dfb77c2ea0df843a7c6c2356998a735ec6929d",
252 )),
253 VOXCPM2 => Some(spec(
254 "voxcpm2-mmproj",
255 "VoxCPM2 LocDiT and AudioVAE (BF16)",
256 "ggml-org/VoxCPM2-GGUF",
257 "mmproj-VoxCPM2-BF16.gguf",
258 2_007_741_504,
259 "2d0be367461d11e12a8544487491936a8f1d61d70d5313bb25dd50a4693be2ee",
260 )),
261 FISH_AUDIO_DRAMA_3 => Some(spec(
262 "fish-audio-drama-3-mmproj",
263 "Fish Audio Drama 3 codebook tower and codec (BF16)",
264 "ggml-org/drama-3-GGUF",
265 "mmproj-drama-3-BF16.gguf",
266 2_942_644_160,
267 "c29b9c90cf180cf81da39f31f023ba33641d54d7898e5e9be032c4d111ecbced",
268 )),
269 _ => None,
270 }
271 }
272
273 pub fn transcribes(&self) -> bool {
275 self.purpose == Purpose::Transcribe
276 }
277}
278
279fn spec(id: &str, label: &str, repo: &str, file: &str, size: u64, sha256: &str) -> ModelSpec {
280 ModelSpec { id: id.into(), label: label.into(), repo: repo.into(), file: file.into(), size, sha256: Some(sha256.into()) }
281}
282
283#[allow(clippy::too_many_arguments)]
284fn chat(spec: ModelSpec, quant: &str, params_b: f32, active_params_b: f32, kv_bytes_per_token: u64, context: u32) -> CatalogEntry {
285 CatalogEntry { spec, purpose: Purpose::Chat, quant: quant.into(), params_b, active_params_b, kv_bytes_per_token, context }
286}
287
288#[allow(clippy::too_many_arguments)]
289fn speaker_entry(spec: ModelSpec, quant: &str, params_b: f32, active_params_b: f32, kv_bytes_per_token: u64, context: u32) -> CatalogEntry {
290 CatalogEntry { spec, purpose: Purpose::Speak, quant: quant.into(), params_b, active_params_b, kv_bytes_per_token, context }
291}
292
293const QWEN3_TTS_SPEAKERS: [&str; 9] = ["vivian", "serena", "uncle_fu", "dylan", "eric", "ryan", "aiden", "ono_anna", "sohee"];
295
296const KOKORO_VOICES: [&str; 54] = [
300 "af_heart",
301 "af_alloy",
302 "af_aoede",
303 "af_bella",
304 "af_jessica",
305 "af_kore",
306 "af_nicole",
307 "af_nova",
308 "af_river",
309 "af_sarah",
310 "af_sky",
311 "am_adam",
312 "am_echo",
313 "am_eric",
314 "am_fenrir",
315 "am_liam",
316 "am_michael",
317 "am_onyx",
318 "am_puck",
319 "am_santa",
320 "bf_alice",
321 "bf_emma",
322 "bf_isabella",
323 "bf_lily",
324 "bm_daniel",
325 "bm_fable",
326 "bm_george",
327 "bm_lewis",
328 "ef_dora",
329 "em_alex",
330 "em_santa",
331 "ff_siwis",
332 "hf_alpha",
333 "hf_beta",
334 "hm_omega",
335 "hm_psi",
336 "if_sara",
337 "im_nicola",
338 "jf_alpha",
339 "jf_gongitsune",
340 "jf_nezumi",
341 "jf_tebukuro",
342 "jm_kumo",
343 "pf_dora",
344 "pm_alex",
345 "pm_santa",
346 "zf_xiaobei",
347 "zf_xiaoni",
348 "zf_xiaoxiao",
349 "zf_xiaoyi",
350 "zm_yunjian",
351 "zm_yunxi",
352 "zm_yunxia",
353 "zm_yunyang",
354];
355
356const VOXCPM2_LANGUAGES: [&str; 30] = [
358 "zh", "en", "ar", "my", "da", "nl", "fi", "fr", "de", "el", "he", "hi", "id", "it", "ja", "km", "ko", "lo", "ms", "no", "pl", "pt", "ru", "es",
359 "sw", "sv", "tl", "th", "tr", "vi",
360];
361
362const DRAMA_3_LANGUAGES: [&str; 83] = [
364 "zh", "en", "ja", "ko", "es", "pt", "ar", "ru", "fr", "de", "sv", "it", "tr", "no", "nl", "cy", "eu", "ca", "da", "gl", "ta", "hu", "fi", "pl",
365 "et", "hi", "la", "ur", "th", "vi", "jw", "bn", "yo", "sl", "cs", "sw", "nn", "he", "ms", "uk", "id", "kk", "bg", "lv", "my", "tl", "sk", "ne",
366 "fa", "af", "el", "bo", "hr", "ro", "sn", "mi", "yi", "am", "be", "km", "is", "az", "sd", "br", "sq", "ps", "mn", "ht", "ml", "sr", "sa", "te",
367 "ka", "bs", "pa", "lt", "kn", "si", "hy", "mr", "as", "gu", "fo",
368];
369
370pub fn catalog() -> Vec<CatalogEntry> {
375 let gemma = |id: &str| crate::catalog::find(id).expect("weirdnet-model ships Gemma 4");
376 vec![
377 chat(
378 spec(
379 "smollm2-135m",
380 "SmolLM2 135M instruct (Q8_0)",
381 "bartowski/SmolLM2-135M-Instruct-GGUF",
382 "SmolLM2-135M-Instruct-Q8_0.gguf",
383 144_811_360,
384 "5a1395716f7913741cc51d98581b9b1228d80987a9f7d3664106742eb06bba83",
385 ),
386 "Q8_0",
387 0.135,
388 0.135,
389 23_040,
390 8192,
391 ),
392 chat(gemma(crate::catalog::GEMMA_4_E2B), "Q4_0", 5.1, 2.0, 32_768, 8192),
393 chat(gemma(crate::catalog::GEMMA_4_E4B), "Q4_0", 8.0, 4.0, 65_536, 8192),
394 chat(
395 spec(
396 "qwen3-8b",
397 "Qwen3 8B (Q4_K_M)",
398 "unsloth/Qwen3-8B-GGUF",
399 "Qwen3-8B-Q4_K_M.gguf",
400 5_027_784_512,
401 "120307ba529eb2439d6c430d94104dabd578497bc7bfe7e322b5d9933b449bd4",
402 ),
403 "Q4_K_M",
404 8.2,
405 8.2,
406 147_456,
407 8192,
408 ),
409 chat(
410 spec(
411 "qwen3-14b",
412 "Qwen3 14B (Q4_K_M)",
413 "unsloth/Qwen3-14B-GGUF",
414 "Qwen3-14B-Q4_K_M.gguf",
415 9_001_753_984,
416 "5eaa0870bd81ed3b58a630a271234cfa604e43ffb3a19cd68e54a80dd9d52a66",
417 ),
418 "Q4_K_M",
419 14.8,
420 14.8,
421 163_840,
422 8192,
423 ),
424 chat(
425 spec(
426 "gpt-oss-20b",
427 "gpt-oss 20B (MXFP4)",
428 "ggml-org/gpt-oss-20b-GGUF",
429 "gpt-oss-20b-MXFP4.gguf",
430 12_109_566_624,
431 "27cd6c432c7672cb812a92f611cf3ba7bbc35928262bb1e1253ff4ee6ae35901",
432 ),
433 "MXFP4",
434 21.0,
435 3.6,
436 49_152,
437 8192,
438 ),
439 chat(
440 spec(
441 "qwen3-30b-a3b",
442 "Qwen3 30B-A3B (Q4_K_M)",
443 "unsloth/Qwen3-30B-A3B-GGUF",
444 "Qwen3-30B-A3B-Q4_K_M.gguf",
445 18_556_686_912,
446 "9f1a24700a339b09c06009b729b5c809e0b64c213b8af5b711b3dbdfd0c5ba48",
447 ),
448 "Q4_K_M",
449 30.5,
450 3.3,
451 98_304,
452 8192,
453 ),
454 chat(
455 spec(
456 "qwen3-32b",
457 "Qwen3 32B (Q4_K_M)",
458 "unsloth/Qwen3-32B-GGUF",
459 "Qwen3-32B-Q4_K_M.gguf",
460 19_762_150_048,
461 "8df67573b2c23484e02ec7af295e39bed7ee774f3771d5fda2978265b59370e7",
462 ),
463 "Q4_K_M",
464 32.8,
465 32.8,
466 262_144,
467 8192,
468 ),
469 CatalogEntry {
470 spec: spec(
471 "qwen3-embedding-0.6b",
472 "Qwen3 Embedding 0.6B (Q8_0)",
473 "Qwen/Qwen3-Embedding-0.6B-GGUF",
474 "Qwen3-Embedding-0.6B-Q8_0.gguf",
475 639_150_592,
476 "06507c7b42688469c4e7298b0a1e16deff06caf291cf0a5b278c308249c3e439",
477 ),
478 purpose: Purpose::Embedding,
479 quant: "Q8_0".into(),
480 params_b: 0.6,
481 active_params_b: 0.6,
482 kv_bytes_per_token: 114_688,
483 context: 2048,
484 },
485 CatalogEntry {
489 spec: spec(
490 VOXTRAL_MINI_3B,
491 "Voxtral Mini 3B speech to text (Q4_K_M)",
492 "ggml-org/Voxtral-Mini-3B-2507-GGUF",
493 "Voxtral-Mini-3B-2507-Q4_K_M.gguf",
494 2_473_001_920,
495 "4705be8ec22ca23d12632f4b4a3691faa95917d90a06d3cf3c3ec0e91958f1a8",
496 ),
497 purpose: Purpose::Transcribe,
498 quant: "Q4_K_M".into(),
499 params_b: 4.7,
500 active_params_b: 4.7,
501 kv_bytes_per_token: 122_880,
502 context: 16_384,
503 },
504 speaker_entry(
514 spec(
515 QWEN3_TTS_0_6B_CUSTOMVOICE,
516 "Qwen3-TTS 0.6B CustomVoice (BF16, vLLM-Omni)",
517 "Qwen/Qwen3-TTS-12Hz-0.6B-CustomVoice",
518 "model.safetensors",
519 1_811_626_576,
520 "bc3c7e785eb961179c25450d1acff03f839e0002f2f3a5aeb67b5735c0fa2adb",
521 ),
522 "BF16",
523 0.6,
524 0.6,
525 114_688,
526 4096,
527 ),
528 speaker_entry(
529 spec(
530 QWEN3_TTS_0_6B_BASE,
531 "Qwen3-TTS 0.6B Base, voice cloning (BF16, vLLM-Omni)",
532 "Qwen/Qwen3-TTS-12Hz-0.6B-Base",
533 "model.safetensors",
534 1_829_344_272,
535 "180b3b10eb1c9f1b4db7806d5475bae3071c0243c299d49926bab1da3b6946f6",
536 ),
537 "BF16",
538 0.6,
539 0.6,
540 114_688,
541 4096,
542 ),
543 speaker_entry(
544 spec(
545 QWEN3_TTS_1_7B_CUSTOMVOICE,
546 "Qwen3-TTS 1.7B CustomVoice (BF16, vLLM-Omni)",
547 "Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice",
548 "model.safetensors",
549 3_833_402_552,
550 "38b1d5971bdbd982b561cccec982669a53b0537c3cf5e9bd4778ed07bb2f5137",
551 ),
552 "BF16",
553 1.7,
554 1.7,
555 114_688,
556 4096,
557 ),
558 speaker_entry(
559 spec(
560 QWEN3_TTS_1_7B_VOICEDESIGN,
561 "Qwen3-TTS 1.7B VoiceDesign (BF16, vLLM-Omni)",
562 "Qwen/Qwen3-TTS-12Hz-1.7B-VoiceDesign",
563 "model.safetensors",
564 3_833_402_552,
565 "391e8db219f292c515297cdceeb43e4eae67cdde35fa57e79a6a8a532fca0522",
566 ),
567 "BF16",
568 1.7,
569 1.7,
570 114_688,
571 4096,
572 ),
573 speaker_entry(
574 spec(
575 QWEN3_TTS_1_7B_BASE,
576 "Qwen3-TTS 1.7B Base, voice cloning (Q8_0)",
577 "ggml-org/Qwen3-TTS-12Hz-1.7B-Base-GGUF",
578 "Qwen3-TTS-12Hz-1.7B-Base-Q8_0.gguf",
579 1_847_874_400,
580 "ac7931aeb2e7aad1a6ed6602d353a5679c9d096b18ce8204ac730a8408d572e1",
581 ),
582 "Q8_0",
583 1.7,
584 1.7,
585 114_688,
586 4096,
587 ),
588 speaker_entry(
591 spec(
592 KOKORO_82M,
593 "Kokoro 82M (F32)",
594 "ggml-org/Kokoro-82M-GGUF",
595 "Kokoro-82M-F32.gguf",
596 61_774_240,
597 "be19298184f1a2577eb20e4689f1e2b315cbf474c39e113f192991fef3c85c6e",
598 ),
599 "F32",
600 0.082,
601 0.082,
602 36_864,
603 512,
604 ),
605 speaker_entry(
609 spec(
610 VOXCPM2,
611 "VoxCPM2, voice design and cloning (Q8_0)",
612 "ggml-org/VoxCPM2-GGUF",
613 "VoxCPM2-Q8_0.gguf",
614 1_565_764_064,
615 "b09cd3afab454f23922632fd474aa0d5d667e128e8bf36e81e77ec7e71fac64b",
616 ),
617 "Q8_0",
618 2.0,
619 2.0,
620 28_672 + 6_144,
621 4096,
622 ),
623 speaker_entry(
628 spec(
629 FISH_AUDIO_DRAMA_3,
630 "Fish Audio Drama 3, multi-speaker (Q4_K_M)",
631 "ggml-org/drama-3-GGUF",
632 "drama-3-Q4_K_M.gguf",
633 20_556_053_760,
634 "5b727d669a7a555713403b7f6d744402dafd6fd37154f0fe834a5310548a8b34",
635 ),
636 "Q4_K_M",
637 35.0,
638 3.0,
639 20_480 + 13_312,
640 4096,
641 ),
642 ]
643}
644
645pub fn by_purpose(purpose: Purpose) -> Vec<CatalogEntry> {
647 catalog().into_iter().filter(|e| e.purpose == purpose).collect()
648}
649
650pub fn transcriber(budget: &Budget) -> Option<CatalogEntry> {
654 by_purpose(Purpose::Transcribe).into_iter().filter(|e| e.footprint(1) <= budget.usable_bytes).max_by(|a, b| a.params_b.total_cmp(&b.params_b))
655}
656
657pub fn speaker(budget: &Budget) -> Option<CatalogEntry> {
662 by_purpose(Purpose::Speak)
663 .into_iter()
664 .filter(|e| e.is_gguf() && !e.restricted() && e.footprint(1) <= budget.usable_bytes)
665 .max_by(|a, b| a.params_b.total_cmp(&b.params_b))
666}
667
668pub fn restricted_license(spec: &ModelSpec) -> Option<&'static str> {
674 catalog()
675 .into_iter()
676 .filter(CatalogEntry::restricted)
677 .find(|e| {
678 e.spec.id == spec.id
679 || e.projector().is_some_and(|p| p.id == spec.id)
680 || e.license_repos().iter().any(|r| r.eq_ignore_ascii_case(&spec.repo))
681 })
682 .and_then(|e| e.license())
683}
684
685pub fn find(id: &str) -> Option<CatalogEntry> {
687 catalog().into_iter().find(|e| e.spec.id == id)
688}
689
690#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
692pub struct Budget {
693 pub pool_bytes: u64,
695 pub reserve_bytes: u64,
697 pub usable_bytes: u64,
698}
699
700pub fn budget(hw: &Hardware, memory_budget: Option<u64>, reserve: Option<u64>) -> Budget {
703 let unified = hw.unified();
704 let pool_bytes = if unified { hw.ram_bytes } else { hw.gpus.first().map(|g| g.memory_bytes).unwrap_or(hw.ram_bytes) };
705 let reserve_bytes = if unified { reserve.unwrap_or_else(|| (hw.ram_bytes / 8).max(4 * GIB)) } else { 0 };
706 let usable = pool_bytes.saturating_sub(reserve_bytes) / 100 * 85;
707 Budget { pool_bytes, reserve_bytes, usable_bytes: memory_budget.unwrap_or(usable) }
708}
709
710#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
712pub struct Plan {
713 pub budget: Budget,
714 pub main: Option<CatalogEntry>,
715 pub standin: Option<CatalogEntry>,
716 pub embedding: Option<CatalogEntry>,
717 pub slots: u32,
719 pub notes: Vec<String>,
720}
721
722pub fn plan(hw: &Hardware, budget: Budget) -> Plan {
726 let slots = 2;
727 let cpu_only = hw.accel() == Accel::Cpu;
728 let mut notes = Vec::new();
729 let all = catalog();
730 let fits = |e: &&CatalogEntry| e.footprint(slots) <= budget.usable_bytes;
731 let main = all
732 .iter()
733 .filter(|e| e.purpose == Purpose::Chat && e.spec.id != "smollm2-135m")
734 .filter(|e| !cpu_only || e.active_params_b <= 4.0)
735 .filter(fits)
736 .max_by(|a, b| a.params_b.total_cmp(&b.params_b))
737 .cloned();
738 if cpu_only {
739 notes.push("CPU only: limited to models with at most 4B active parameters".into());
740 }
741 if main.is_none() {
742 notes.push("no catalog chat model fits this budget; add a smaller GGUF model".into());
743 }
744 let standin_id = if budget.usable_bytes >= 16 * GIB { crate::catalog::GEMMA_4_E4B } else { crate::catalog::GEMMA_4_E2B };
745 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();
746 let embedding = all.iter().find(|e| e.purpose == Purpose::Embedding).filter(|e| e.footprint(1) <= budget.usable_bytes).cloned();
747 Plan { budget, main, standin, embedding, slots, notes }
748}
749
750#[cfg(test)]
751mod tests {
752 use super::*;
753 use crate::hw::{Gpu, parse_nvidia_smi};
754
755 fn box_with(gpus: Vec<Gpu>, ram_gib: u64) -> Hardware {
756 Hardware { os: "linux".into(), arch: "x86_64".into(), cpu_threads: 16, ram_bytes: ram_gib * GIB, gpus }
757 }
758
759 #[test]
760 fn rtx_3060_gets_an_8b() {
761 let hw = box_with(parse_nvidia_smi("NVIDIA GeForce RTX 3060, 12288, 8.6, 615.78.08", 62 * GIB), 62);
762 let b = budget(&hw, None, None);
763 assert_eq!(b.pool_bytes, 12 * GIB);
764 let p = plan(&hw, b);
765 assert_eq!(p.main.unwrap().id(), "qwen3-8b");
766 assert_eq!(p.standin.unwrap().id(), "gemma-4-e2b");
767 assert!(p.embedding.is_some());
768 }
769
770 #[test]
771 fn spark_gets_a_big_model() {
772 let hw = box_with(parse_nvidia_smi("NVIDIA GB10, [N/A], 12.1, 580.95.05", 128 * GIB), 128);
773 let b = budget(&hw, None, None);
774 assert_eq!(b.reserve_bytes, 16 * GIB);
775 let p = plan(&hw, b);
776 assert_eq!(p.main.unwrap().id(), "qwen3-32b");
777 assert_eq!(p.standin.unwrap().id(), "gemma-4-e4b");
778 }
779
780 #[test]
781 fn cpu_box_prefers_few_active_params() {
782 let hw = box_with(Vec::new(), 62);
783 let p = plan(&hw, budget(&hw, None, None));
784 assert_eq!(p.main.unwrap().id(), "qwen3-30b-a3b");
785 }
786
787 #[test]
788 fn tiny_box_gets_no_main() {
789 let hw = box_with(Vec::new(), 4);
790 let p = plan(&hw, budget(&hw, Some(GIB), None));
791 assert!(p.main.is_none());
792 assert!(!p.notes.is_empty());
793 }
794
795 #[test]
796 fn override_budget() {
797 let hw = box_with(Vec::new(), 64);
798 assert_eq!(budget(&hw, Some(10 * GIB), None).usable_bytes, 10 * GIB);
799 assert_eq!(budget(&hw, None, Some(14 * GIB)).reserve_bytes, 14 * GIB);
800 }
801
802 #[test]
803 fn transcribe_is_a_purpose_of_its_own() {
804 assert_eq!(serde_json::to_string(&Purpose::Transcribe).unwrap(), "\"transcribe\"");
805 for p in Purpose::ALL {
806 assert_eq!(serde_json::from_str::<Purpose>(&format!("\"{p}\"")).unwrap(), p);
807 assert_eq!(Purpose::parse(p.as_str()), Some(p));
808 }
809 assert_eq!(Purpose::parse("speech"), None);
810 assert_eq!(serde_json::from_str::<Purpose>("\"chat\"").unwrap(), Purpose::Chat);
812 }
813
814 #[test]
815 fn the_catalog_has_a_pinned_speech_to_text_model() {
816 let stt = by_purpose(Purpose::Transcribe);
817 assert_eq!(stt.len(), 1);
818 let v = &stt[0];
819 assert_eq!((v.id(), v.quant.as_str(), v.context), (VOXTRAL_MINI_3B, "Q4_K_M", 16_384));
820 assert!(v.transcribes());
821 assert_eq!(find(VOXTRAL_MINI_3B).as_ref(), Some(v));
822 assert_eq!(v.spec.repo, "ggml-org/Voxtral-Mini-3B-2507-GGUF");
823 assert_eq!(v.spec.sha256.as_deref().map(str::len), Some(64));
824 let p = v.projector().expect("Voxtral needs its audio encoder");
825 assert_eq!((p.repo.as_str(), p.file.as_str(), p.size), (v.spec.repo.as_str(), "mmproj-Voxtral-Mini-3B-2507-Q8_0.gguf", 715_714_080));
826 assert!(p.sha256.as_deref().is_some_and(|h| h.len() == 64 && h.chars().all(|c| c.is_ascii_hexdigit())));
827 assert_ne!(p.file, v.spec.file);
828 assert_eq!(v.kv_bytes_per_token, 30 * 8 * 256 * 2);
830 let without = (v.spec.size + v.kv_bytes_per_token * 16_384) * 11 / 10;
832 assert!(v.footprint(1) > without);
833 assert!(catalog().iter().filter(|e| !e.transcribes() && !e.speaks()).all(|e| e.projector().is_none()));
834 let mut ids: Vec<_> = catalog().iter().map(|e| e.id().to_string()).collect();
836 ids.sort();
837 ids.dedup();
838 assert_eq!(ids.len(), catalog().len());
839 }
840
841 #[test]
842 fn plans_never_pick_the_transcriber_and_it_fits_on_its_own() {
843 for (gpus, ram) in [(parse_nvidia_smi("NVIDIA GeForce RTX 3060, 12288, 8.6, 615.78.08", 62 * GIB), 62), (Vec::new(), 62), (Vec::new(), 8)] {
844 let hw = box_with(gpus, ram);
845 let p = plan(&hw, budget(&hw, None, None));
846 for e in [&p.main, &p.standin, &p.embedding].into_iter().flatten() {
847 assert!(!e.transcribes() && !e.speaks(), "{} in a plan", e.id());
848 }
849 }
850 let hw = box_with(Vec::new(), 62);
851 assert_eq!(transcriber(&budget(&hw, None, None)).map(|e| e.spec.id), Some(VOXTRAL_MINI_3B.into()));
852 assert_eq!(transcriber(&budget(&hw, Some(GIB), None)), None, "too small a budget");
853 assert!(by_purpose(Purpose::Embedding).iter().all(|e| e.purpose == Purpose::Embedding));
854 }
855
856 #[test]
857 fn speak_is_a_purpose_of_its_own() {
858 assert_eq!(serde_json::to_string(&Purpose::Speak).unwrap(), "\"speak\"");
859 assert_eq!(Purpose::parse("speak"), Some(Purpose::Speak));
860 assert_eq!(Purpose::Speak.to_string(), "speak");
861 assert_eq!(Purpose::parse("tts"), None);
862 }
863
864 #[test]
865 fn the_catalog_has_cited_speech_models() {
866 let ids: Vec<_> = by_purpose(Purpose::Speak).iter().map(|e| e.id().to_string()).collect();
867 assert_eq!(
868 ids,
869 [
870 QWEN3_TTS_0_6B_CUSTOMVOICE,
871 QWEN3_TTS_0_6B_BASE,
872 QWEN3_TTS_1_7B_CUSTOMVOICE,
873 QWEN3_TTS_1_7B_VOICEDESIGN,
874 QWEN3_TTS_1_7B_BASE,
875 KOKORO_82M,
876 VOXCPM2,
877 FISH_AUDIO_DRAMA_3
878 ]
879 );
880 let hex = |h: &Option<String>| h.as_deref().is_some_and(|h| h.len() == 64 && h.chars().all(|c| c.is_ascii_hexdigit()));
881 for e in by_purpose(Purpose::Speak) {
882 assert!(e.speaks() && !e.transcribes());
883 assert!(hex(&e.spec.sha256), "{} pinned", e.id());
884 let s = e.speech().unwrap_or_else(|| panic!("{} has a profile", e.id()));
885 assert!(s.sample_rate.is_some() && !s.formats.is_empty() && s.languages.is_some(), "{}", e.id());
886 assert!(e.license().is_some());
887 if e.is_gguf() {
888 let p = e.projector().unwrap_or_else(|| panic!("{} needs its projector", e.id()));
889 assert!(hex(&p.sha256) && p.file.starts_with("mmproj-") && p.repo == e.spec.repo);
890 assert_eq!(s.formats, [crate::speech::AudioFormat::Wav, crate::speech::AudioFormat::Pcm16], "llama-server's formats");
891 assert!(e.footprint(1) > (e.spec.size + p.size) * 11 / 10);
892 } else {
893 assert!(e.projector().is_none());
894 assert!(e.spec.repo.starts_with("Qwen/") && e.spec.file == "model.safetensors");
895 assert!(e.footprint(1) > (e.spec.size + QWEN3_TTS_TOKENIZER_BYTES) * 11 / 10, "the speech tokenizer counts");
896 assert!(s.formats.len() == 5 && s.speed, "vLLM-Omni's formats and speed");
897 }
898 }
899 let q = |id| find(id).unwrap().speech().unwrap();
901 assert_eq!(q(QWEN3_TTS_1_7B_CUSTOMVOICE).presets.as_ref().map(Vec::len), Some(9));
902 assert!(q(QWEN3_TTS_1_7B_CUSTOMVOICE).instructions && !q(QWEN3_TTS_0_6B_CUSTOMVOICE).instructions);
903 assert!(q(QWEN3_TTS_1_7B_VOICEDESIGN).design && q(QWEN3_TTS_1_7B_VOICEDESIGN).clone.is_none());
904 assert_eq!(q(QWEN3_TTS_0_6B_BASE).clone, Some(CloneMode::AudioAndText));
905 assert_eq!(q(QWEN3_TTS_1_7B_BASE).clone, Some(CloneMode::Audio), "ggml-org's projector has the speaker encoder only");
906 let k = q(KOKORO_82M);
907 assert_eq!((k.presets.as_ref().map(Vec::len), k.presets.as_ref().and_then(|p| p.first()).map(String::as_str)), (Some(54), Some("af_heart")));
908 assert!(k.raw && k.speed);
909 assert!(q(VOXCPM2).design && q(VOXCPM2).clone == Some(CloneMode::Audio));
910 assert_eq!(q(VOXCPM2).languages.map(|l| l.len()), Some(30));
911 let d = q(FISH_AUDIO_DRAMA_3);
912 assert_eq!((d.script_speakers, d.clone, d.sample_rate), (Some(16), None, Some(44_100)));
913 assert_eq!(d.languages.map(|l| l.len()), Some(83));
914 let drama = find(FISH_AUDIO_DRAMA_3).unwrap();
916 assert!(drama.restricted() && drama.license() == Some(crate::speech::FISH_AUDIO_COMMUNITY));
917 assert_eq!(d.license.as_deref(), Some(crate::speech::FISH_AUDIO_COMMUNITY));
918 assert_eq!(catalog().iter().filter(|e| e.restricted()).count(), 1);
919 assert!(catalog().iter().filter(|e| !e.speaks()).all(|e| e.speech().is_none()));
920 }
921
922 #[test]
923 fn a_speaker_that_fits_and_never_a_restricted_one() {
924 let b = |usable_bytes| Budget { pool_bytes: usable_bytes, reserve_bytes: 0, usable_bytes };
925 assert_eq!(speaker(&b(128 * GIB)).map(|e| e.spec.id), Some(VOXCPM2.into()), "never Drama 3, however big");
926 assert_eq!(speaker(&b(GIB * 7 / 2)).map(|e| e.spec.id), Some(QWEN3_TTS_1_7B_BASE.into()));
927 assert_eq!(speaker(&b(GIB)).map(|e| e.spec.id), Some(KOKORO_82M.into()));
928 assert_eq!(speaker(&b(100 << 20)), None);
929 assert!(by_purpose(Purpose::Speak).iter().all(|e| e.footprint(1) > 0));
930 }
931
932 #[test]
933 fn downloads_under_a_restricted_license() {
934 let drama = find(FISH_AUDIO_DRAMA_3).unwrap();
935 let lic = Some(crate::speech::FISH_AUDIO_COMMUNITY);
936 assert_eq!(restricted_license(&drama.spec), lic);
937 assert_eq!(restricted_license(&drama.projector().unwrap()), lic);
938 let mut other = drama.spec.clone();
940 other.id = "my-drama".into();
941 other.file = "drama-3-Q8_0.gguf".into();
942 assert_eq!(restricted_license(&other), lic);
943 other.repo = "FishAudio/Drama-3".into();
944 assert_eq!(restricted_license(&other), lic);
945 for e in catalog().iter().filter(|e| e.spec.id != FISH_AUDIO_DRAMA_3) {
946 assert_eq!(restricted_license(&e.spec), None, "{}", e.id());
947 }
948 }
949}