rigspark-core 3.1.0

Deterministic, offline LLM memory and VRAM sizing, KV-cache estimation, hardware-fit scoring and model catalog for rigspark.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Curated, offline catalog of local voice models: text-to-speech ("speak") and
//! speech-to-text ("listen").
//!
//! Kept separate from the LLM and generation catalogs. Fit covers weight memory only;
//! voice speed is never estimated because no sourced throughput data exists.
use crate::{
    catalog::{
        LICENSES, coordinates, date, digest, model_file, parameter_label, parse_document, require,
        timestamp,
    },
    reports::strip_control,
    sizing::{CpuArch, HEADROOM, Hardware, Platform, ValidationError, memory_capacity},
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
pub const VOICE_SCHEMA_VERSION: u8 = 1;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum VoiceKind {
    /// Text to speech: spoken replies and generated audio.
    Speak,
    /// Speech to text: dictation into the composer.
    Listen,
}
impl VoiceKind {
    pub fn name(self) -> &'static str {
        match self {
            Self::Speak => "speak",
            Self::Listen => "listen",
        }
    }
    pub fn parse(value: &str) -> Option<Self> {
        match value {
            "speak" => Some(Self::Speak),
            "listen" => Some(Self::Listen),
            _ => None,
        }
    }
}

/// Local engines RigSpark starts for a voice model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum VoiceEngine {
    /// whisper.cpp's `whisper-server`, loading a pinned, verified ggml file.
    WhisperCpp,
    /// `mlx_audio.server` on Apple Silicon; it fetches `engineModel` itself.
    MlxAudio,
}
impl VoiceEngine {
    pub fn name(self) -> &'static str {
        match self {
            Self::WhisperCpp => "whisper-cpp",
            Self::MlxAudio => "mlx-audio",
        }
    }
    pub fn kinds(self) -> &'static [VoiceKind] {
        match self {
            Self::WhisperCpp => &[VoiceKind::Listen],
            Self::MlxAudio => &[VoiceKind::Speak, VoiceKind::Listen],
        }
    }
    /// Whether the engine can run on this hardware at all.
    pub fn supports(self, hardware: &Hardware) -> bool {
        match self {
            Self::WhisperCpp => true,
            Self::MlxAudio => {
                hardware.platform == Platform::Darwin && hardware.arch == CpuArch::Arm64
            }
        }
    }
}

/// How `weightsBytes` was sourced.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum SizeBasis {
    /// The exact byte size of the pinned file.
    Pinned,
    /// An upper bound; the pinned SHA-256 still decides integrity.
    Ceiling,
    /// The rounded size shown on the model card; the engine downloads the weights.
    ModelCard,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct VoiceFile {
    pub repo: String,
    pub revision: String,
    pub file: String,
    pub sha256: String,
    pub bytes: u64,
    /// False when `bytes` is a ceiling rather than the exact size.
    pub exact_size: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Voice {
    pub id: String,
    pub label: String,
    /// The engine's language code for this voice.
    pub lang: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct VoiceModel {
    pub id: String,
    pub family: String,
    pub kind: VoiceKind,
    pub engine: VoiceEngine,
    pub params: String,
    pub license: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub release_date: Option<String>,
    #[serde(default)]
    pub default: bool,
    pub source: String,
    /// The pinned weight file RigSpark downloads and verifies (whisper.cpp).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file: Option<VoiceFile>,
    /// The repository id the engine loads by itself (mlx-audio).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub engine_model: Option<String>,
    pub weights_bytes: u64,
    pub size_basis: SizeBasis,
    pub languages: Vec<String>,
    #[serde(default)]
    pub voices: Vec<Voice>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct VoiceCatalog {
    pub schema_version: u8,
    pub generated_at: String,
    pub models: Vec<VoiceModel>,
}

fn https(value: &str) -> Result<(), ValidationError> {
    require(
        value.len() <= 2048
            && value.starts_with("https://")
            && value.len() > "https://".len()
            && !value
                .chars()
                .any(|ch| ch.is_control() || ch.is_whitespace()),
        "source links must be HTTPS",
    )
}
fn model_id(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= 128
        && value.as_bytes()[0].is_ascii_alphanumeric()
        && value.bytes().all(|byte| {
            byte.is_ascii_lowercase() || byte.is_ascii_digit() || b"._-:".contains(&byte)
        })
        && VoiceKind::parse(value).is_none()
}
fn short_token(value: &str, maximum: usize) -> bool {
    !value.is_empty()
        && value.len() <= maximum
        && value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"._-".contains(&byte))
}

impl VoiceModel {
    pub fn voice(&self, id: &str) -> Option<&Voice> {
        self.voices.iter().find(|voice| voice.id == id)
    }
    fn validate(&self) -> Result<(), ValidationError> {
        require(model_id(&self.id), "invalid voice model id")?;
        require(short_token(&self.family, 128), "invalid voice model family")?;
        require(parameter_label(&self.params), "invalid parameter label")?;
        require(
            LICENSES.contains(&self.license.as_str()),
            "voice catalog requires an allowlisted open-weight license",
        )?;
        if let Some(released) = &self.release_date {
            date(released)?;
        }
        https(&self.source)?;
        require(
            self.engine.kinds().contains(&self.kind),
            "engine cannot run this voice model kind",
        )?;
        require(
            self.weights_bytes > 0 && self.weights_bytes <= MAX_SAFE_INTEGER,
            "invalid weight size",
        )?;
        require(
            !self.languages.is_empty()
                && self.languages.len() <= 64
                && self
                    .languages
                    .iter()
                    .all(|language| short_token(language, 32)),
            "invalid voice model languages",
        )?;
        require(self.voices.len() <= 256, "too many voices")?;
        let mut voices = HashSet::new();
        for voice in &self.voices {
            require(
                short_token(&voice.id, 64)
                    && short_token(&voice.lang, 8)
                    && !voice.label.is_empty()
                    && voice.label.len() <= 128
                    && voices.insert(&voice.id),
                "invalid voice",
            )?;
        }
        match self.engine {
            VoiceEngine::WhisperCpp => {
                let file = self.file.as_ref().ok_or_else(|| {
                    ValidationError("whisper.cpp models need a pinned file".into())
                })?;
                require(
                    self.engine_model.is_none(),
                    "whisper.cpp models load a pinned file, not an engine model",
                )?;
                coordinates(&file.repo, &file.revision)?;
                model_file(&file.file, 256)?;
                require(
                    file.file.ends_with(".bin") && !file.file.contains('/'),
                    "whisper.cpp weights must be a top-level ggml .bin file",
                )?;
                digest(&file.sha256)?;
                // `file.bytes` must equal `weights_bytes`, which is range-checked above.
                require(
                    self.weights_bytes == file.bytes
                        && self.size_basis
                            == if file.exact_size {
                                SizeBasis::Pinned
                            } else {
                                SizeBasis::Ceiling
                            },
                    "weight size must match the pinned file",
                )?;
            }
            VoiceEngine::MlxAudio => {
                let repository = self.engine_model.as_deref().ok_or_else(|| {
                    ValidationError("mlx-audio models need an engine model".into())
                })?;
                require(self.file.is_none(), "mlx-audio downloads its own weights")?;
                require(
                    repository.len() <= 200
                        && repository.split('/').count() == 2
                        && repository.split('/').all(|part| short_token(part, 100)),
                    "invalid engine model repository",
                )?;
                require(
                    self.size_basis == SizeBasis::ModelCard,
                    "engine-downloaded weights are sized from the model card",
                )?;
            }
        }
        Ok(())
    }
}

impl VoiceCatalog {
    pub fn parse(raw: &str) -> Result<Self, ValidationError> {
        let mut result: Self = parse_document(raw)?;
        require(
            result.schema_version == VOICE_SCHEMA_VERSION
                && !result.models.is_empty()
                && result.models.len() <= 256,
            "unsupported or empty voice catalog",
        )?;
        timestamp(&result.generated_at)?;
        let mut ids = HashSet::new();
        let mut defaults = HashSet::new();
        let mut kinds = HashSet::new();
        for model in &result.models {
            model.validate()?;
            require(ids.insert(&model.id), "duplicate voice model id")?;
            kinds.insert(model.kind);
            if model.default {
                require(
                    defaults.insert(model.kind),
                    "each voice kind needs exactly one default model",
                )?;
            }
        }
        require(
            kinds == defaults,
            "each voice kind needs exactly one default model",
        )?;
        for model in &mut result.models {
            for voice in &mut model.voices {
                voice.label = strip_control(&voice.label);
            }
        }
        Ok(result)
    }
    pub fn bundled() -> Result<Self, ValidationError> {
        Self::parse(crate::VOICE_JSON)
    }
    /// Resolves `speak`/`listen` to that kind's default model, otherwise an exact id.
    pub fn resolve(&self, query: &str) -> Result<&VoiceModel, ValidationError> {
        let found = match VoiceKind::parse(query) {
            Some(kind) => self
                .models
                .iter()
                .find(|model| model.kind == kind && model.default),
            None => self.models.iter().find(|model| model.id == query),
        };
        found.ok_or_else(|| ValidationError("unknown voice model".into()))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum VoiceVerdict {
    Yes,
    No,
}

/// Memory-only fit of a voice model; speed is never estimated.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VoiceFit {
    pub verdict: VoiceVerdict,
    pub reason: &'static str,
    pub memory_kind: &'static str,
    pub budget_bytes: f64,
    pub weights_bytes: u64,
    /// Always `"unknown"`: no sourced voice throughput dataset exists.
    pub speed: &'static str,
}

pub fn fit(model: &VoiceModel, hardware: &Hardware) -> VoiceFit {
    let (memory_kind, usable_bytes) = memory_capacity(hardware);
    let budget_bytes = usable_bytes * (1.0 - HEADROOM);
    let (verdict, reason) = if !model.engine.supports(hardware) {
        (
            VoiceVerdict::No,
            "mlx-audio runs only on Apple Silicon Macs",
        )
    } else if model.weights_bytes as f64 <= budget_bytes {
        (VoiceVerdict::Yes, "weights fit in memory")
    } else {
        (VoiceVerdict::No, "weights exceed usable memory")
    };
    VoiceFit {
        verdict,
        reason,
        memory_kind,
        budget_bytes,
        weights_bytes: model.weights_bytes,
        speed: "unknown",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sizing::{CpuArch, Platform};

    fn hardware(platform: Platform, arch: CpuArch, ram_gib: f64) -> Hardware {
        Hardware {
            arch,
            platform,
            total_ram_bytes: ram_gib * 1073741824.0,
            free_ram_bytes: ram_gib * 1073741824.0,
            free_disk_bytes: 1e12,
            gpu: Vec::new(),
            unified_memory: None,
        }
    }
    fn catalog() -> serde_json::Value {
        serde_json::from_str(crate::VOICE_JSON).unwrap()
    }
    fn parse(value: &serde_json::Value) -> Result<VoiceCatalog, ValidationError> {
        VoiceCatalog::parse(&value.to_string())
    }

    #[test]
    fn the_bundled_catalog_has_one_default_per_kind() {
        let catalog = VoiceCatalog::bundled().unwrap();
        assert_eq!(catalog.resolve("speak").unwrap().id, "kokoro-82m");
        assert_eq!(
            catalog.resolve("listen").unwrap().id,
            "whisper-large-v3-turbo:q8_0"
        );
        assert_eq!(
            catalog.resolve("chatterbox").unwrap().kind,
            VoiceKind::Speak
        );
        assert!(catalog.resolve("missing").is_err());
        let kokoro = catalog.resolve("kokoro-82m").unwrap();
        assert_eq!(kokoro.voice("af_heart").unwrap().lang, "a");
        assert!(kokoro.voice("nobody").is_none());
    }

    #[test]
    fn engines_fit_only_where_they_run() {
        let catalog = VoiceCatalog::bundled().unwrap();
        let kokoro = catalog.resolve("speak").unwrap();
        let whisper = catalog.resolve("listen").unwrap();
        let mac = hardware(Platform::Darwin, CpuArch::Arm64, 16.0);
        let linux = hardware(Platform::Linux, CpuArch::X64, 16.0);
        assert_eq!(fit(kokoro, &mac).verdict, VoiceVerdict::Yes);
        assert_eq!(fit(kokoro, &linux).verdict, VoiceVerdict::No);
        assert_eq!(fit(whisper, &linux).verdict, VoiceVerdict::Yes);
        assert_eq!(fit(whisper, &linux).speed, "unknown");
        // 0.5 GiB of RAM cannot hold an 874 MB model.
        let tiny = hardware(Platform::Linux, CpuArch::X64, 0.5);
        assert_eq!(fit(whisper, &tiny).verdict, VoiceVerdict::No);
    }

    #[test]
    fn invalid_entries_are_refused() {
        let base = catalog();
        type Change = Box<dyn Fn(&mut serde_json::Value)>;
        let broken: Vec<(&str, Change)> = vec![
            (
                "license",
                Box::new(|c| c["models"][0]["license"] = "proprietary".into()),
            ),
            (
                "kind",
                Box::new(|c| c["models"][0]["engine"] = "whisper-cpp".into()),
            ),
            (
                "listen engine",
                Box::new(|c| c["models"][0]["kind"] = "listen".into()),
            ),
            (
                "two defaults",
                Box::new(|c| c["models"][1]["default"] = true.into()),
            ),
            (
                "no default",
                Box::new(|c| c["models"][0]["default"] = false.into()),
            ),
            (
                "duplicate",
                Box::new(|c| c["models"][1]["id"] = "kokoro-82m".into()),
            ),
            ("id", Box::new(|c| c["models"][1]["id"] = "Speak".into())),
            (
                "kind id",
                Box::new(|c| c["models"][1]["id"] = "speak".into()),
            ),
            (
                "source",
                Box::new(|c| c["models"][0]["source"] = "http://example.com".into()),
            ),
            (
                "sha",
                Box::new(|c| c["models"][2]["file"]["sha256"] = "00".into()),
            ),
            (
                "revision",
                Box::new(|c| c["models"][2]["file"]["revision"] = "main".into()),
            ),
            (
                "nested file",
                Box::new(|c| c["models"][2]["file"]["file"] = "a/b.bin".into()),
            ),
            (
                "extension",
                Box::new(|c| c["models"][2]["file"]["file"] = "model.gguf".into()),
            ),
            (
                "size mismatch",
                Box::new(|c| c["models"][2]["weightsBytes"] = 1.into()),
            ),
            (
                "basis",
                Box::new(|c| c["models"][2]["sizeBasis"] = "ceiling".into()),
            ),
            (
                "mlx file",
                Box::new(|c| c["models"][0]["file"] = c["models"][2]["file"].clone()),
            ),
            (
                "mlx basis",
                Box::new(|c| c["models"][0]["sizeBasis"] = "pinned".into()),
            ),
            (
                "mlx repo",
                Box::new(|c| c["models"][0]["engineModel"] = "one/two/three".into()),
            ),
            (
                "no repo",
                Box::new(|c| {
                    c["models"][0]
                        .as_object_mut()
                        .unwrap()
                        .remove("engineModel");
                }),
            ),
            (
                "no file",
                Box::new(|c| {
                    c["models"][2].as_object_mut().unwrap().remove("file");
                }),
            ),
            (
                "zero size",
                Box::new(|c| c["models"][0]["weightsBytes"] = 0.into()),
            ),
            (
                "languages",
                Box::new(|c| c["models"][0]["languages"] = serde_json::json!([])),
            ),
            (
                "voice",
                Box::new(|c| c["models"][0]["voices"][0]["id"] = "bad voice".into()),
            ),
            (
                "duplicate voice",
                Box::new(|c| c["models"][0]["voices"][1]["id"] = "af_heart".into()),
            ),
            (
                "params",
                Box::new(|c| c["models"][0]["params"] = "big".into()),
            ),
            (
                "date",
                Box::new(|c| c["models"][2]["releaseDate"] = "2024-13-01".into()),
            ),
            ("schema", Box::new(|c| c["schemaVersion"] = 2.into())),
            (
                "timestamp",
                Box::new(|c| c["generatedAt"] = "yesterday".into()),
            ),
            (
                "extra field",
                Box::new(|c| c["models"][0]["speed"] = "fast".into()),
            ),
        ];
        assert!(parse(&base).is_ok());
        for (label, change) in broken {
            let mut value = base.clone();
            change(&mut value);
            assert!(parse(&value).is_err(), "{label}");
        }
    }

    #[test]
    fn names_hardware_support_and_budgets_are_exact() {
        assert_eq!(VoiceKind::Speak.name(), "speak");
        assert_eq!(VoiceKind::Listen.name(), "listen");
        assert_eq!(VoiceEngine::WhisperCpp.name(), "whisper-cpp");
        assert_eq!(VoiceEngine::MlxAudio.name(), "mlx-audio");
        for (platform, arch, supported) in [
            (Platform::Darwin, CpuArch::Arm64, true),
            (Platform::Darwin, CpuArch::X64, false),
            (Platform::Linux, CpuArch::Arm64, false),
            (Platform::Linux, CpuArch::X64, false),
        ] {
            assert_eq!(
                VoiceEngine::MlxAudio.supports(&hardware(platform, arch, 16.0)),
                supported,
                "{supported}"
            );
        }
        let catalog = VoiceCatalog::bundled().unwrap();
        let linux = hardware(Platform::Linux, CpuArch::X64, 16.0);
        let (_, usable) = memory_capacity(&linux);
        let verdict = fit(catalog.resolve("listen").unwrap(), &linux);
        assert_eq!(verdict.budget_bytes, usable * 0.85);
        assert_eq!(verdict.memory_kind, "ram");
        assert_eq!(verdict.weights_bytes, 874188075);
    }

    #[test]
    fn source_links_must_be_whole_https_urls() {
        assert!(https("https://a").is_ok());
        for bad in ["https://", "https://a b", "https://a\u{7}", "http://a"] {
            assert!(https(bad).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn voice_labels_lose_control_characters() {
        let mut value = catalog();
        value["models"][0]["voices"][0]["label"] = "Heart\u{1b}[31m".into();
        let parsed = parse(&value).unwrap();
        assert_eq!(parsed.models[0].voices[0].label, "Heart");
    }
}