whisperforge-core 0.5.5

GPU-accelerated Whisper model inference with streaming audio, quantization, and KV-cached decoding
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
/// End-to-end streaming pipeline integration test.
///
/// Run with:
///   cargo test --release -p whisperforge-core --test stream_integration -- --ignored --nocapture
///
/// Requires:
///   models/tiny_en_converted/model.{mpk,cfg}
///   models/tiny_en_converted/tokenizer.json
///   models/silero_vad.onnx  (or will be auto-downloaded)
///   test_data/LJ001-0001_16k.wav
///
/// Expected transcript fragment (LJ001-0001):
///   "Printing, in the only sense with which we are at present concerned,
///    differs from most if not from all the arts and crafts..."
use anyhow::Result;
use burn_flex::{Flex, FlexDevice};
use ringbuf::traits::Consumer as _;
use std::path::PathBuf;
use std::thread;
use std::time::Duration;
use tokenizers::Tokenizer;
use whisperforge_core::{
    Chunker, CommitDelta, Committer, EndpointConfig, Endpointer, FakeMic, PromptContext,
    QualityGate, SileroVad, Whisper, WindowConfig, compute_mel_from_samples, decode_window,
    ensure_silero_model, passes_quality_gate, stream_decode::DecodeContext,
};

type B = Flex<f32>;

fn workspace_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("workspace root")
        .to_path_buf()
}

#[test]
#[ignore]
fn test_stream_pipeline_on_ljspeech() -> Result<()> {
    let root = workspace_root();
    let models_dir = root.join("models");
    let audio_path = root.join("test_data/LJ001-0001_16k.wav");

    // Skip if required files are absent. Per-model layout: `<name>/model.{mpk,cfg}`
    // + `<name>/tokenizer.json`.
    let model_dir = models_dir.join("tiny_en_converted");
    let model_mpk = model_dir.join("model.mpk");
    let tokenizer_path = model_dir.join("tokenizer.json");
    if !model_mpk.exists() {
        eprintln!("SKIP: {} not found", model_mpk.display());
        return Ok(());
    }
    if !tokenizer_path.exists() {
        eprintln!("SKIP: {} not found", tokenizer_path.display());
        return Ok(());
    }
    if !audio_path.exists() {
        eprintln!("SKIP: {} not found", audio_path.display());
        return Ok(());
    }

    let device = FlexDevice;
    let base = model_dir.join("model");
    let model: Whisper<B> =
        whisperforge_core::load::load_whisper(base.to_str().expect("valid path"), &device)?;

    let tokenizer = Tokenizer::from_file(&tokenizer_path)
        .map_err(|e| anyhow::anyhow!("load tokenizer: {e}"))?;

    let tok = |s: &str, fb: u32| tokenizer.token_to_id(s).unwrap_or(fb);
    let sot_token = tok("<|startoftranscript|>", 50258);
    let language_token = tok("<|en|>", 50259);
    let transcribe_token = tok("<|transcribe|>", 50359);
    let eot_token = tok("<|endoftext|>", 50257);
    let no_speech_token = tok("<|nospeech|>", 50362);
    let notimestamps_token = tok("<|notimestamps|>", 50363);
    let timestamp_begin_token = 50364u32;
    let prevtext_token_id = tokenizer.token_to_id("<|prevtext|>");

    let vad_path = ensure_silero_model(&models_dir)?;
    let vad = SileroVad::open(&vad_path)?;

    let window_cfg = WindowConfig {
        max_window_secs: 28.0,
        stride_secs: 1.0,
        vad_threshold: 0.5,
        min_speech_secs: 0.25,
    };
    let mut chunker = Chunker::new(window_cfg, vad);
    let mut committer = Committer::new();
    let mut endpointer = Endpointer::new(EndpointConfig {
        silence_secs: 2.0,
        punct_silence_secs: 0.8,
        min_utterance_secs: 0.5,
    });
    let mut prompt_ctx = PromptContext::new(60, prevtext_token_id);

    let (fake_mic, _feeder_handle) = FakeMic::open(&audio_path, false)?;

    const WINDOW_SAMPLES: usize = 480_000;
    const SILENCE_FRAME: [f32; 512] = [0.0f32; 512];
    let mut drain_buf = vec![0.0f32; 4096];
    let mut committed_text = String::new();
    let mut endpoint_fired_mid_stream = false;
    let mut silence_pushed_secs = 0.0f32;

    loop {
        let n = fake_mic.consumer.lock().unwrap().pop_slice(&mut drain_buf);

        let window = if n > 0 {
            silence_pushed_secs = 0.0;
            match chunker.push(&drain_buf[..n]) {
                Some(w) => w,
                None => continue,
            }
        } else {
            // No new audio. Synthesise silence so the chunker keeps striding and the
            // endpointer can fire naturally after speech stops. Shut down once the file
            // is exhausted and trailing silence has comfortably exceeded silence_secs.
            if fake_mic.is_done() && silence_pushed_secs > 3.0 {
                break;
            }
            silence_pushed_secs += SILENCE_FRAME.len() as f32 / 16_000.0;
            thread::sleep(Duration::from_millis(1));
            match chunker.push(&SILENCE_FRAME) {
                Some(w) => w,
                None => continue,
            }
        };

        let emits = if window.had_speech {
            let mut padded = window.samples.clone();
            padded.resize(WINDOW_SAMPLES, 0.0);
            let mel =
                compute_mel_from_samples::<B>(&padded, 400, 160, 80, &device).expect("compute mel");
            let encoder_out = model.forward_encoder(mel);
            let ctx = DecodeContext {
                prompt_tokens: prompt_ctx.prompt_tokens(),
                language_token,
                task_token: transcribe_token,
                sot_token,
                eot_token,
                no_speech_token,
                notimestamps_token,
                timestamp_begin_token,
                max_new_tokens: 128,
                no_speech_threshold: 0.6,
            };
            decode_window::<B>(&model, encoder_out, &ctx, &tokenizer, &device)?
        } else {
            vec![]
        };

        // Same confidence gate as the live stream main loop (faster-whisper defaults).
        let emits = if !emits.is_empty() && !passes_quality_gate(&emits, &QualityGate::default()) {
            Vec::new()
        } else {
            emits
        };

        let (commit_delta, _tentative) = committer.ingest(emits);
        if let CommitDelta::Committed { ref new_text, .. } = commit_delta
            && !new_text.is_empty()
        {
            committed_text.push_str(new_text);
            eprintln!("committed: {new_text}");
        }

        if endpointer.step(&window, committer.committed_text()) || window.forced_eou {
            let final_delta = committer.finalize_utterance();
            if let CommitDelta::Committed { ref new_text, .. } = final_delta
                && !new_text.is_empty()
            {
                committed_text.push_str(new_text);
            }
            endpoint_fired_mid_stream = true;
            prompt_ctx.update_after_eou(committer.committed_tokens(), 128);
            endpointer.reset();
            chunker.reset_utterance();
        }
    }

    // Finalize any remaining partial.
    let final_delta = committer.finalize_utterance();
    if let CommitDelta::Committed { ref new_text, .. } = final_delta
        && !new_text.is_empty()
    {
        committed_text.push_str(new_text);
    }

    eprintln!("Full committed transcript: {committed_text}");

    // Tightened UAT (post Phase F remediation):
    // (1) At least one endpoint event must fire mid-stream — driven by `Endpointer::step`
    //     seeing growing trailing silence — rather than only via the shutdown-path
    //     `finalize_utterance`. This exercises B3 (silence-tick) end-to-end.
    // (2) The committed transcript must cover the LJ001-0001 ground truth modulo minor ASR
    //     variance — measured as token-level overlap against a curated keyword set drawn
    //     from the reference sentence.
    //
    // Reference: "Printing, in the only sense with which we are at present concerned,
    //             differs from most if not from all the arts and crafts represented in
    //             the Exhibition"
    assert!(
        endpoint_fired_mid_stream,
        "no endpoint event fired during the stream — silence-tick / endpointer is broken"
    );

    let lower = committed_text.to_lowercase();
    let keywords = [
        "printing",
        "only",
        "sense",
        "which",
        "present",
        "concerned",
        "differs",
        "most",
        "from",
        "all",
        "arts",
        "crafts",
        "represented",
        "exhibition",
    ];
    let hits: Vec<&str> = keywords
        .iter()
        .copied()
        .filter(|kw| lower.contains(kw))
        .collect();
    assert!(
        hits.len() >= 10,
        "expected ≥10/14 LJ001-0001 keywords; got {} ({:?}) in transcript: {committed_text:?}",
        hits.len(),
        hits
    );

    Ok(())
}

/// Long-form continuity test for B9 (timestamp-anchored trim).
///
/// Forces a small max_window_secs so the 10 s LJ001-0001 clip hits the cap repeatedly.
/// Verifies that the cap-hit trim path keeps the utterance going as one continuous
/// commit stream (no premature endpoint events, no duplicated text at the trim seam),
/// and that at least one trim event actually fires.
#[test]
#[ignore]
fn test_stream_pipeline_long_form_trim() -> Result<()> {
    let root = workspace_root();
    let models_dir = root.join("models");
    let audio_path = root.join("test_data/LJ001-0001_16k.wav");

    let model_dir = models_dir.join("tiny_en_converted");
    let model_mpk = model_dir.join("model.mpk");
    let tokenizer_path = model_dir.join("tokenizer.json");
    if !model_mpk.exists() || !tokenizer_path.exists() || !audio_path.exists() {
        eprintln!("SKIP: missing model or audio fixtures");
        return Ok(());
    }

    let device = FlexDevice;
    let base = model_dir.join("model");
    let model: Whisper<B> =
        whisperforge_core::load::load_whisper(base.to_str().expect("valid path"), &device)?;
    let tokenizer = Tokenizer::from_file(&tokenizer_path)
        .map_err(|e| anyhow::anyhow!("load tokenizer: {e}"))?;

    let tok = |s: &str, fb: u32| tokenizer.token_to_id(s).unwrap_or(fb);
    let sot_token = tok("<|startoftranscript|>", 50258);
    let language_token = tok("<|en|>", 50259);
    let transcribe_token = tok("<|transcribe|>", 50359);
    let eot_token = tok("<|endoftext|>", 50257);
    let no_speech_token = tok("<|nospeech|>", 50362);
    let notimestamps_token = tok("<|notimestamps|>", 50363);
    let timestamp_begin_token = 50364u32;
    let prevtext_token_id = tokenizer.token_to_id("<|prevtext|>");

    let vad_path = ensure_silero_model(&models_dir)?;
    let vad = SileroVad::open(&vad_path)?;

    // max_window_secs = 5.0 forces cap-hits within the 10 s clip; stride_secs = 1.0 gives
    // multiple opportunities to commit a timestamp before each cap.
    let window_cfg = WindowConfig {
        max_window_secs: 5.0,
        stride_secs: 1.0,
        vad_threshold: 0.5,
        min_speech_secs: 0.25,
    };
    let mut chunker = Chunker::new(window_cfg, vad);
    let mut committer = Committer::new();
    let mut endpointer = Endpointer::new(EndpointConfig {
        silence_secs: 2.0,
        punct_silence_secs: 0.8,
        min_utterance_secs: 0.5,
    });
    let mut prompt_ctx = PromptContext::new(60, prevtext_token_id);

    let (fake_mic, _feeder_handle) = FakeMic::open(&audio_path, false)?;

    const WINDOW_SAMPLES: usize = 480_000;
    const SILENCE_FRAME: [f32; 512] = [0.0f32; 512];
    let mut drain_buf = vec![0.0f32; 4096];
    let mut committed_text = String::new();
    let mut trim_count: usize = 0;
    let mut endpoint_count: usize = 0;
    let mut silence_pushed_secs = 0.0f32;

    loop {
        let n = fake_mic.consumer.lock().unwrap().pop_slice(&mut drain_buf);

        let window = if n > 0 {
            silence_pushed_secs = 0.0;
            match chunker.push(&drain_buf[..n]) {
                Some(w) => w,
                None => continue,
            }
        } else {
            if fake_mic.is_done() && silence_pushed_secs > 3.0 {
                break;
            }
            silence_pushed_secs += SILENCE_FRAME.len() as f32 / 16_000.0;
            thread::sleep(Duration::from_millis(1));
            match chunker.push(&SILENCE_FRAME) {
                Some(w) => w,
                None => continue,
            }
        };

        let emits = if window.had_speech {
            let mut padded = window.samples.clone();
            padded.resize(WINDOW_SAMPLES, 0.0);
            let mel =
                compute_mel_from_samples::<B>(&padded, 400, 160, 80, &device).expect("compute mel");
            let encoder_out = model.forward_encoder(mel);
            let ctx = DecodeContext {
                prompt_tokens: prompt_ctx.prompt_tokens(),
                language_token,
                task_token: transcribe_token,
                sot_token,
                eot_token,
                no_speech_token,
                notimestamps_token,
                timestamp_begin_token,
                max_new_tokens: 32,
                no_speech_threshold: 0.6,
            };
            decode_window::<B>(&model, encoder_out, &ctx, &tokenizer, &device)?
        } else {
            vec![]
        };

        // Same confidence gate as the live stream main loop (faster-whisper defaults).
        let emits = if !emits.is_empty() && !passes_quality_gate(&emits, &QualityGate::default()) {
            Vec::new()
        } else {
            emits
        };

        let (commit_delta, _tentative) = committer.ingest(emits);
        if let CommitDelta::Committed { ref new_text, .. } = commit_delta
            && !new_text.is_empty()
        {
            committed_text.push_str(new_text);
            eprintln!("committed: {new_text}");
        }

        let endpoint_fires = endpointer.step(&window, committer.committed_text());
        if endpoint_fires || window.forced_eou {
            let final_delta = committer.finalize_utterance();
            if let CommitDelta::Committed { ref new_text, .. } = final_delta
                && !new_text.is_empty()
            {
                committed_text.push_str(new_text);
            }
            endpoint_count += 1;
            prompt_ctx.update_after_eou(committer.committed_tokens(), 128);
            endpointer.reset();
            chunker.reset_utterance();
        } else if window.cap_hit {
            // Same stride-based trim logic as the live stream main loop.
            // Trim 1.5 s — leaves 3.5 s of overlap between consecutive strides so the
            // non-causal encoder produces stable enough output for LCP to commit content.
            // Half-buffer (2.5 s) trim was too aggressive — LCP regressed at the seam and
            // committed nothing between trims.
            let target_samples = (1.5_f32 * 16_000.0) as usize;
            let has_commits = !committer.committed_tokens().is_empty();
            let trimmed = if has_commits {
                chunker.trim_oldest(target_samples)
            } else {
                0
            };
            if trimmed > 0 {
                let trim_delta = committer.on_trim();
                if let CommitDelta::Committed { ref new_text, .. } = trim_delta
                    && !new_text.is_empty()
                {
                    committed_text.push_str(new_text);
                    eprintln!("trim-commit: {new_text}");
                }
                trim_count += 1;
                eprintln!("cap-hit trim: trimmed={trimmed} samples");
            }
        }
    }

    let final_delta = committer.finalize_utterance();
    if let CommitDelta::Committed { ref new_text, .. } = final_delta
        && !new_text.is_empty()
    {
        committed_text.push_str(new_text);
    }

    eprintln!("Long-form committed transcript: {committed_text}");
    eprintln!("trim_count={trim_count}  endpoint_count={endpoint_count}");

    // (1) At least one trim must have fired — the whole point of this test.
    assert!(
        trim_count >= 1,
        "expected ≥1 cap-hit trim event over a 10 s clip with 5 s cap, got 0"
    );

    // (2) Coverage of the LJ001 sentence — same keyword set as the short-form test, but
    //     since the trim path is the primary continuation mechanism here, this verifies
    //     no content is lost across trims.
    let lower = committed_text.to_lowercase();
    let keywords = [
        "printing",
        "sense",
        "concerned",
        "differs",
        "arts",
        "crafts",
        "exhibition",
    ];
    let hits: Vec<&str> = keywords
        .iter()
        .copied()
        .filter(|kw| lower.contains(kw))
        .collect();
    assert!(
        hits.len() >= 5,
        "expected ≥5/7 LJ001 keywords after long-form trim; got {} ({:?}) in: {committed_text:?}",
        hits.len(),
        hits
    );

    // (3) No egregious duplication at the trim seam: no 4-word phrase should appear twice.
    //     A trim that overshoots and lets the next decode re-derive committed text would
    //     produce repeated 4-word phrases.
    let words: Vec<&str> = lower.split_whitespace().collect();
    if words.len() >= 8 {
        for i in 0..words.len().saturating_sub(3) {
            let phrase = words[i..i + 4].join(" ");
            let count = lower.matches(&phrase).count();
            assert!(
                count <= 1,
                "4-word phrase '{phrase}' appears {count} times — trim seam duplication: {committed_text:?}"
            );
        }
    }

    Ok(())
}