speechcore 0.1.1

Reusable Rust speech-to-text runtime with audio capture, VAD, backend selection, model provisioning, and transcript streaming.
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
use crate::backend::onnx_utils::{load_session, OnnxSessionOptions};
use anyhow::{Context, Result};
use ort::session::{Input, Output, Session};
use ort::value::ValueType;
use parking_lot::Mutex;
use serde::Deserialize;
use std::path::{Path, PathBuf};

const PREPROCESS_ONNX: &str = "preprocess.onnx";
const ENCODER_ONNX: &str = "encode.onnx";
const DECODER_UNCACHED_ONNX: &str = "uncached_decode.onnx";
const DECODER_CACHED_ONNX: &str = "cached_decode.onnx";
const TOKENIZER_FILENAME: &str = "tokenizer.json";
const MERGED_ENCODER_ONNX: &str = "encoder_model.onnx";
const MERGED_DECODER_ONNX: &str = "decoder_model_merged.onnx";
const MERGED_PREPROCESSOR_CONFIG: &str = "preprocessor_config.json";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MoonshineLayout {
    Legacy,
    Merged,
}

pub struct MoonshineModel {
    pub layout: MoonshineLayout,
    pub preprocessor_config: Option<MoonshinePreprocessorConfig>,
    pub flavor: Option<MoonshineFlavor>,
    pub preprocess: Option<Mutex<Session>>,
    pub encoder: Mutex<Session>,
    pub decoder: Mutex<Session>,
    pub decoder_cached: Option<Mutex<Session>>,
    pub preprocess_input: String,
    pub preprocess_output: String,
    pub encoder_input: String,
    pub encoder_attention_mask: Option<String>,
    pub encoder_output: String,
    pub decoder_input_ids: String,
    pub decoder_encoder_states: String,
    pub decoder_encoder_attention_mask: Option<String>,
    pub decoder_logits: String,
    pub decoder_use_cache_branch: Option<(String, ort::tensor::TensorElementType)>,
    pub decoder_cached_input_ids: Option<String>,
    pub decoder_cached_encoder_states: Option<String>,
    pub decoder_cached_past_inputs: Vec<String>,
    pub decoder_cached_logits: Option<String>,
    pub decoder_cached_present_outputs: Vec<String>,
}

pub struct MoonshineModelPaths {
    pub preprocess: PathBuf,
    pub encoder: PathBuf,
    pub decoder: PathBuf,
    pub decoder_cached: PathBuf,
    pub preprocessor_config: PathBuf,
}

impl MoonshineModel {
    pub fn resolve_paths(model_dir: impl AsRef<Path>) -> Result<MoonshineModelPaths> {
        let model_dir = model_dir.as_ref();
        let preprocess = model_dir.join(PREPROCESS_ONNX);
        let encoder = model_dir.join(ENCODER_ONNX);
        let decoder = model_dir.join(DECODER_UNCACHED_ONNX);
        let decoder_cached = model_dir.join(DECODER_CACHED_ONNX);
        let preprocessor_config = model_dir.join(MERGED_PREPROCESSOR_CONFIG);

        Ok(MoonshineModelPaths {
            preprocess,
            encoder,
            decoder,
            decoder_cached,
            preprocessor_config,
        })
    }

    pub fn load(model_dir: impl AsRef<Path>, options: &OnnxSessionOptions) -> Result<Self> {
        let model_dir = model_dir.as_ref();
        let paths = Self::resolve_paths(model_dir)?;
        let flavor = resolve_flavor(model_dir);

        let merged_encoder = model_dir.join(MERGED_ENCODER_ONNX);
        let merged_decoder = model_dir.join(MERGED_DECODER_ONNX);
        let merged_preprocessor = paths.preprocessor_config.clone();

        let (layout, encoder_path, decoder_path, preprocess_path, preprocessor_config) =
            if merged_encoder.exists() && merged_decoder.exists() {
                (
                    MoonshineLayout::Merged,
                    merged_encoder,
                    merged_decoder,
                    None,
                    Some(load_preprocessor_config(&merged_preprocessor)?),
                )
            } else {
                if !paths.preprocess.exists() {
                    return Err(anyhow::anyhow!(
                        "Missing Moonshine preprocessor model: {}",
                        paths.preprocess.display()
                    ));
                }
                if !paths.encoder.exists() {
                    return Err(anyhow::anyhow!(
                        "Missing Moonshine encoder model: {}",
                        paths.encoder.display()
                    ));
                }
                if !paths.decoder.exists() {
                    return Err(anyhow::anyhow!(
                        "Missing Moonshine decoder model: {}",
                        paths.decoder.display()
                    ));
                }
                (
                    MoonshineLayout::Legacy,
                    paths.encoder.clone(),
                    paths.decoder.clone(),
                    Some(paths.preprocess.clone()),
                    None,
                )
            };
        let tokenizer_path = model_dir.join(TOKENIZER_FILENAME);
        if !tokenizer_path.exists() {
            return Err(anyhow::anyhow!(
                "Missing Moonshine tokenizer: {}",
                tokenizer_path.display()
            ));
        }

        let preprocess = match preprocess_path {
            Some(path) => Some(
                load_session(&path, options)
                    .with_context(|| "Failed to load Moonshine preprocess.onnx")?,
            ),
            None => None,
        };
        let encoder = load_session(&encoder_path, options).with_context(|| {
            if layout == MoonshineLayout::Merged {
                "Failed to load Moonshine encoder_model.onnx"
            } else {
                "Failed to load Moonshine encode.onnx"
            }
        })?;
        let decoder = load_session(&decoder_path, options).with_context(|| {
            if layout == MoonshineLayout::Merged {
                "Failed to load Moonshine decoder_model_merged.onnx"
            } else {
                "Failed to load Moonshine uncached_decode.onnx"
            }
        })?;

        let decoder_cached = if paths.decoder_cached.exists() {
            Some(
                load_session(&paths.decoder_cached, options)
                    .with_context(|| "Failed to load Moonshine cached_decode.onnx")?,
            )
        } else {
            None
        };

        let (preprocess_input, preprocess_output) = if let Some(ref preprocess) = preprocess {
            (
                resolve_input_name(
                    &preprocess.inputs,
                    &["input_values", "audio", "input"],
                    "preprocess input",
                )?,
                resolve_output_name(
                    &preprocess.outputs,
                    &["input_features", "features", "output"],
                    "preprocess output",
                )?,
            )
        } else {
            ("input_values".to_string(), "input_features".to_string())
        };

        let encoder_input = resolve_input_name(
            &encoder.inputs,
            &["input_features", "input_values", "features", "input"],
            "encoder input",
        )?;
        let encoder_attention_mask =
            resolve_optional_input_name(&encoder.inputs, &["attention_mask"]);
        let encoder_output = resolve_output_name(
            &encoder.outputs,
            &["encoder_hidden_states", "last_hidden_state", "output"],
            "encoder output",
        )?;

        let decoder_input_ids = resolve_input_name(
            &decoder.inputs,
            &["input_ids", "tokens", "decoder_input_ids"],
            "decoder input_ids",
        )?;
        let decoder_encoder_states = resolve_input_name(
            &decoder.inputs,
            &[
                "encoder_hidden_states",
                "encoder_outputs",
                "encoder_hidden_state",
            ],
            "decoder encoder_hidden_states",
        )?;
        let decoder_encoder_attention_mask =
            resolve_optional_input_name(&decoder.inputs, &["encoder_attention_mask"]);
        let decoder_use_cache_branch =
            resolve_optional_input_type(&decoder.inputs, &["use_cache_branch", "use_cache"]);
        let decoder_logits =
            resolve_output_name(&decoder.outputs, &["logits", "output"], "decoder logits")?;

        let (
            decoder_cached_input_ids,
            decoder_cached_encoder_states,
            decoder_cached_past_inputs,
            decoder_cached_logits,
            decoder_cached_present_outputs,
        ) = if let Some(cached_session) = decoder_cached.as_ref() {
            let cached_input_ids = resolve_input_name(
                &cached_session.inputs,
                &["input_ids", "tokens", "decoder_input_ids"],
                "cached decoder input_ids",
            )?;
            let cached_encoder_states = resolve_input_name(
                &cached_session.inputs,
                &[
                    "encoder_hidden_states",
                    "encoder_outputs",
                    "encoder_hidden_state",
                ],
                "cached decoder encoder_hidden_states",
            )?;

            let past_inputs = cached_session
                .inputs
                .iter()
                .filter(|input| is_cache_tensor(&input.name))
                .map(|input| input.name.clone())
                .collect::<Vec<_>>();

            let present_outputs = cached_session
                .outputs
                .iter()
                .filter(|output| is_present_tensor(&output.name))
                .map(|output| output.name.clone())
                .collect::<Vec<_>>();

            let cached_logits = resolve_output_name(
                &cached_session.outputs,
                &["logits", "output"],
                "cached decoder logits",
            )?;

            (
                Some(cached_input_ids),
                Some(cached_encoder_states),
                past_inputs,
                Some(cached_logits),
                present_outputs,
            )
        } else {
            (None, None, Vec::new(), None, Vec::new())
        };

        Ok(Self {
            layout,
            preprocessor_config,
            flavor,
            preprocess: preprocess.map(Mutex::new),
            encoder: Mutex::new(encoder),
            decoder: Mutex::new(decoder),
            decoder_cached: decoder_cached.map(Mutex::new),
            preprocess_input,
            preprocess_output,
            encoder_input,
            encoder_attention_mask,
            encoder_output,
            decoder_input_ids,
            decoder_encoder_states,
            decoder_encoder_attention_mask,
            decoder_logits,
            decoder_use_cache_branch,
            decoder_cached_input_ids,
            decoder_cached_encoder_states,
            decoder_cached_past_inputs,
            decoder_cached_logits,
            decoder_cached_present_outputs,
        })
    }

    pub fn validate_model_dir(model_dir: impl AsRef<Path>) -> Result<()> {
        let paths = Self::resolve_paths(model_dir.as_ref())?;

        let merged_encoder = model_dir.as_ref().join(MERGED_ENCODER_ONNX);
        let merged_decoder = model_dir.as_ref().join(MERGED_DECODER_ONNX);
        let merged_preprocessor = model_dir.as_ref().join(MERGED_PREPROCESSOR_CONFIG);

        if merged_encoder.exists() && merged_decoder.exists() {
            if !merged_preprocessor.exists() {
                return Err(anyhow::anyhow!(
                    "Missing Moonshine preprocessor config: {}",
                    merged_preprocessor.display()
                ));
            }
        } else {
            if !paths.preprocess.exists() {
                return Err(anyhow::anyhow!(
                    "Missing Moonshine preprocessor model: {}",
                    paths.preprocess.display()
                ));
            }
            if !paths.encoder.exists() {
                return Err(anyhow::anyhow!(
                    "Missing Moonshine encoder model: {}",
                    paths.encoder.display()
                ));
            }
            if !paths.decoder.exists() {
                return Err(anyhow::anyhow!(
                    "Missing Moonshine decoder model: {}",
                    paths.decoder.display()
                ));
            }
        }

        let tokenizer_path = model_dir.as_ref().join(TOKENIZER_FILENAME);
        if !tokenizer_path.exists() {
            return Err(anyhow::anyhow!(
                "Missing Moonshine tokenizer: {}",
                tokenizer_path.display()
            ));
        }

        Ok(())
    }
}

fn resolve_input_name(inputs: &[Input], candidates: &[&str], label: &str) -> Result<String> {
    resolve_name(
        inputs
            .iter()
            .map(|input| input.name.as_str())
            .collect::<Vec<_>>(),
        candidates,
        label,
    )
}

fn resolve_output_name(outputs: &[Output], candidates: &[&str], label: &str) -> Result<String> {
    resolve_name(
        outputs
            .iter()
            .map(|output| output.name.as_str())
            .collect::<Vec<_>>(),
        candidates,
        label,
    )
}

fn resolve_optional_input_name(inputs: &[Input], candidates: &[&str]) -> Option<String> {
    for candidate in candidates {
        for input in inputs {
            if input.name.eq_ignore_ascii_case(candidate) {
                return Some(input.name.clone());
            }
        }
    }
    for candidate in candidates {
        let candidate_lower = candidate.to_lowercase();
        for input in inputs {
            if input.name.to_lowercase().contains(&candidate_lower) {
                return Some(input.name.clone());
            }
        }
    }
    None
}

fn resolve_optional_input_type(
    inputs: &[Input],
    candidates: &[&str],
) -> Option<(String, ort::tensor::TensorElementType)> {
    for candidate in candidates {
        for input in inputs {
            if input.name.eq_ignore_ascii_case(candidate) {
                if let ValueType::Tensor { ty, .. } = input.input_type {
                    return Some((input.name.clone(), ty));
                }
            }
        }
    }
    for candidate in candidates {
        let candidate_lower = candidate.to_lowercase();
        for input in inputs {
            if input.name.to_lowercase().contains(&candidate_lower) {
                if let ValueType::Tensor { ty, .. } = input.input_type {
                    return Some((input.name.clone(), ty));
                }
            }
        }
    }
    None
}

fn resolve_name(names: Vec<&str>, candidates: &[&str], label: &str) -> Result<String> {
    if names.len() == 1 {
        return Ok(names[0].to_string());
    }

    for candidate in candidates {
        for name in &names {
            if name.eq_ignore_ascii_case(candidate) {
                return Ok((*name).to_string());
            }
        }
    }

    for candidate in candidates {
        let candidate_lower = candidate.to_lowercase();
        for name in &names {
            if name.to_lowercase().contains(&candidate_lower) {
                return Ok((*name).to_string());
            }
        }
    }

    Err(anyhow::anyhow!(
        "Unable to resolve {} (candidates: {:?}, available: {:?})",
        label,
        candidates,
        names
    ))
}

fn is_cache_tensor(name: &str) -> bool {
    let name = name.to_lowercase();
    name.contains("past") || name.contains("key_values")
}

fn is_present_tensor(name: &str) -> bool {
    let name = name.to_lowercase();
    name.contains("present") || name.contains("key_values")
}

pub fn cached_input_shape(inputs: &[Input], name: &str) -> Result<Vec<i64>> {
    let input = inputs
        .iter()
        .find(|input| input.name == name)
        .ok_or_else(|| anyhow::anyhow!("Cached decoder missing input: {}", name))?;

    match &input.input_type {
        ValueType::Tensor { shape, .. } => Ok(shape.to_vec()),
        _ => Err(anyhow::anyhow!(
            "Cached decoder input is not a tensor: {}",
            name
        )),
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct MoonshinePreprocessorConfig {
    pub do_normalize: bool,
    pub feature_extractor_type: String,
    pub feature_size: usize,
    pub padding_side: String,
    pub padding_value: f32,
    pub return_attention_mask: bool,
    pub sampling_rate: usize,
}

fn load_preprocessor_config(path: &Path) -> Result<MoonshinePreprocessorConfig> {
    let contents = std::fs::read_to_string(path).with_context(|| {
        format!(
            "Failed to read Moonshine preprocessor config: {}",
            path.display()
        )
    })?;
    let config: MoonshinePreprocessorConfig =
        serde_json::from_str(&contents).with_context(|| {
            format!(
                "Failed to parse Moonshine preprocessor config: {}",
                path.display()
            )
        })?;
    Ok(config)
}

#[derive(Debug, Clone, Copy)]
pub struct MoonshineFlavor {
    pub token_rate: usize,
    pub num_layers: usize,
    pub num_key_value_heads: usize,
    pub head_dim: usize,
}

fn resolve_flavor(model_dir: &Path) -> Option<MoonshineFlavor> {
    let model_id = model_dir.file_name()?.to_str()?;
    let model_id = model_id
        .strip_prefix("moonshine-")
        .and_then(|name| name.strip_suffix("-onnx"))
        .unwrap_or(model_id);

    match model_id {
        "tiny" => Some(MoonshineFlavor {
            token_rate: 6,
            num_layers: 6,
            num_key_value_heads: 8,
            head_dim: 36,
        }),
        "base" => Some(MoonshineFlavor {
            token_rate: 6,
            num_layers: 8,
            num_key_value_heads: 8,
            head_dim: 52,
        }),
        "tiny-ar" | "tiny-zh" | "tiny-ja" | "tiny-ko" | "tiny-vi" => Some(MoonshineFlavor {
            token_rate: 13,
            num_layers: 6,
            num_key_value_heads: 8,
            head_dim: 36,
        }),
        "tiny-uk" => Some(MoonshineFlavor {
            token_rate: 8,
            num_layers: 6,
            num_key_value_heads: 8,
            head_dim: 36,
        }),
        "base-es" => Some(MoonshineFlavor {
            token_rate: 6,
            num_layers: 8,
            num_key_value_heads: 8,
            head_dim: 52,
        }),
        _ => None,
    }
}