From 173936e63e4a41d88d7f20ff89e1a5d9fe8d4711 Mon Sep 17 00:00:00 2001 From: CJ Pais Date: Tue, 20 Oct 2026 00:56:42 -0700 Subject: [PATCH 1/3] moonshine_streaming: encode one-shot input in bounded windows The one-shot (and batch) encoder ran the whole utterance as one graph: one dense [T_enc, T_enc] f32 mask per layer on host and device, plus [T_enc, T_enc, n_heads] attention scores. Memory grew with T^2 - 12 / 20 / 26 GB of footprint for tiny / small / medium on 5 minutes of audio. Encode in windows of 1024 emitted frames (~20 s) instead, each carrying the encoder's full receptive field: cumulative_left_context + the frontend conv pad on the left, cumulative_right_context on the right. Every layer attends through a finite sliding window and the convs are causal, so this is exact in the mathematical sense - the same algebra the streaming path already relies on and parity-tests. Window cuts sit on encoder-frame boundaries so both strided convs stay phase-aligned. Inputs that fit one window take the old single-pass path unchanged. Not bit-exact: Metal's kernels order reductions by a frame's position in the graph, so windows that don't start at frame 0 differ at ~5e-4 of the activation range, uniformly, with no rise at the cuts. Shortening the left context by 10 or 30 frames produces 1e-2 / 4e-1 spikes at every cut, so the check would catch a missing-context bug. The residual is about half of what toggling flash attention does to the single pass. Peak footprint, Metal, Q8_0, inside the model's 81.9 s input range: 30 s 60 s 80 s tiny 303 -> 240 669 -> 283 1040 -> 305 small 510 -> 413 1125 -> 515 1734 -> 561 medium 742 -> 609 1612 -> 786 2465 -> 863 Transcripts are byte-identical to main on jfk and 30/60/80/120 s for all three sizes; stream_parity passes with and without a forced 64-frame window. TRANSCRIBE_MOONSHINE_STREAMING_ENC_WINDOW overrides the window size. Beyond 81.9 s (4096 encoder frames) the adapter's pos_emb table runs out: that is a separate, pre-existing bug (OOB get_rows: CPU aborts, Metal reads garbage) and not fixed here. --- docs/environment-variables.md | 1 + src/arch/moonshine_streaming/model.cpp | 129 ++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index f9a58a55..9fa946e6 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -35,6 +35,7 @@ of tests. | `TRANSCRIBE_DUMP_DIR=` | Enable the per-stage tensor dumper; writes `.f32` + `.json` per dumped tensor into ``. The basis for the numerical-comparison harness (`scripts/compare_tensors.py`). | | `TRANSCRIBE_PERF_DEBUG` | Print a per-stage timing breakdown to stderr (DEBUG log) on the families that profile (`cohere`, `granite`, `canary`, `canary_qwen`, `moonshine`, `moonshine_streaming`, `moss`, `qwen3_asr`, `whisper`). For whisper, a value containing `cpu` or `all` additionally prints the CPU sub-section breakdown. | | `TRANSCRIBE_VOXTRAL_REALTIME_STREAM_TIMING` | Print a per-component streaming wall-time breakdown at stream finalize (voxtral_realtime). | +| `TRANSCRIBE_MOONSHINE_STREAMING_ENC_WINDOW=` | Encoder frames emitted per window by the moonshine_streaming one-shot / batch encoder (default 1024, ~20 s). Windowing bounds encoder memory; it is mathematically exact (each window carries the full sliding-window context) and float-level differences stay at the backend's reduction-order noise. Set small to exercise the windowed path on short clips. Read at each run. | | `TRANSCRIBE_TEST_DEV_INIT_THROW=` | Fault injection: backend device init (`ggml_backend_dev_init`) throws for devices whose name contains `` (`*` matches every device). Exercises throw → skip → CPU-fallback in backend probing; an explicit backend request fails with `TRANSCRIBE_ERR_BACKEND`. Used by `backend_init_throw_unit` and `scripts/ci/vulkan_degradation_check.py`. | | `TRANSCRIBE_TEST_TEARDOWN_THROW` | Fault injection: any non-empty value injects a throw after each real free inside the `transcribe::safe_*` teardown wrappers, proving containment without leaking the handle. Used by `teardown_safety_unit`. | diff --git a/src/arch/moonshine_streaming/model.cpp b/src/arch/moonshine_streaming/model.cpp index 7a6f150c..313aa5e1 100644 --- a/src/arch/moonshine_streaming/model.cpp +++ b/src/arch/moonshine_streaming/model.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -529,6 +530,126 @@ transcribe_status encode_window_to_host(MoonshineStreamingSession * cc, return TRANSCRIBE_OK; } +// Encoder frames a windowed encode emits per window (see encode_pcm_to_host). +// 1024 frames is ~20 s of audio at 50 frames/s. With medium's 210 + 4 frames +// of left context and 12 of right, a window is ~1250 frames: the per-layer +// masks and attention scores are ~6 MB each instead of growing with T^2, and +// the context overhead is ~20%. +constexpr int k_encode_window_emit_frames = 1024; + +// TRANSCRIBE_MOONSHINE_STREAMING_ENC_WINDOW= overrides the emit size, +// so the windowed path can be exercised (and checked against the single-pass +// encoder) on short clips. Values below 1 are ignored. +int encode_window_emit_frames() { + if (const char * v = transcribe::env::str("TRANSCRIBE_MOONSHINE_STREAMING_ENC_WINDOW"); v != nullptr) { + char * end = nullptr; + const long n = std::strtol(v, &end, 10); + if (end != v && n >= 1 && n <= (1 << 20)) { + return static_cast(n); + } + } + return k_encode_window_emit_frames; +} + +// Encode a whole utterance in bounded windows and stitch the result. +// +// The one-shot encoder used to run the whole utterance as one graph, which +// materialises one dense [T_enc, T_enc] mask per layer (host and device) plus +// [T_enc, T_enc, n_heads] attention scores: T^2 growth, 12-26 GB of footprint +// for 5 minutes of audio, and an abort at 10. +// +// Windowing is exact for this architecture, not an approximation. Every +// encoder layer attends through a finite sliding window (L_i, R_i) and the +// frontend convs are causal with a bounded receptive field, so the encoder +// output at frame t depends only on conv-stack frames +// [t - L_total, t + R_total] (cumulative_*_context), which in turn depend only +// on PCM from k_frontend_pad_enc_frames further left. A window that carries +// that much context on each side reproduces the single-pass output for its +// emitted frames; the streaming path (flush_stable_frames) relies on the same +// algebra and is parity-tested against one-shot. Window cuts land on +// encoder-frame boundaries (samples_per_encoder_frame PCM samples), which keeps +// both strided convs phase-aligned with the single pass. +// +// "Reproduces" is mathematical, not bitwise: backend kernels (Metal flash +// attention, softmax, matmul tiling) group their reductions by a frame's +// position within the graph, so a window that does not start at frame 0 sums +// in a different order. Measured on tiny: <= ~5e-4 of the activation range, +// uniform across the window with no rise near the cuts (a missing-context bug +// shows up as a spike at each cut), and about half of what toggling flash +// attention already does to the single pass. +// +// Short inputs (one window's worth) take the single-pass path unchanged. +// `pcm` must already be right-padded to a multiple of frame_len. +transcribe_status encode_pcm_to_host(MoonshineStreamingSession * cc, + MoonshineStreamingModel * cm, + const float * pcm, + int n_samples, + bool emit_dumps, + std::vector & out_enc_host, + int & out_T_enc) { + const auto & hp = cm->hparams; + const int T_total = encoder_t_enc(hp, n_samples); + const int emit = encode_window_emit_frames(); + const int L = cumulative_left_context(hp) + k_frontend_pad_enc_frames; + const int R = cumulative_right_context(hp); + const int spf = samples_per_encoder_frame(hp); + + if (T_total <= 0 || T_total <= emit + L + R || spf <= 0) { + return encode_window_to_host(cc, cm, pcm, n_samples, emit_dumps, out_enc_host, out_T_enc); + } + + const int enc_h = hp.enc_d_model; + const size_t total = static_cast(enc_h) * static_cast(T_total); + out_enc_host.assign(total, 0.0f); + + // Dump points describe a single encoder graph; a windowed encode has one + // per window, so it emits none rather than a misleading last window. + if (emit_dumps) { + log_msg(TRANSCRIBE_LOG_LEVEL_DEBUG, + "moonshine_streaming: windowed encode (%d frames in windows of %d), encoder dumps skipped", T_total, + emit); + } + + std::vector win_host; + for (int emit_start = 0; emit_start < T_total; emit_start += emit) { + if (cc->poll_abort()) { + return TRANSCRIBE_ERR_ABORTED; + } + const int emit_end = std::min(T_total, emit_start + emit); + const int win_start = std::max(0, emit_start - L); + const int win_end = std::min(T_total, emit_end + R); + + // The last window ends at the padded end of the input, exactly as the + // single pass does; every other one ends on a frame boundary. + const int64_t pcm_start = static_cast(win_start) * spf; + const int64_t pcm_end = (win_end == T_total) ? n_samples : static_cast(win_end) * spf; + if (pcm_end <= pcm_start || pcm_end > n_samples) { + return TRANSCRIBE_ERR_INVALID_ARG; + } + + int win_T = 0; + if (auto st = encode_window_to_host(cc, cm, pcm + pcm_start, static_cast(pcm_end - pcm_start), + /*emit_dumps=*/false, win_host, win_T); + st != TRANSCRIBE_OK) { + return st; + } + if (win_T != win_end - win_start) { + log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, + "moonshine_streaming: encoder window [%d, %d) produced %d frames, expected %d", win_start, win_end, + win_T, win_end - win_start); + return TRANSCRIBE_ERR_GGUF; + } + + const size_t src_off = static_cast(enc_h) * static_cast(emit_start - win_start); + const size_t dst_off = static_cast(enc_h) * static_cast(emit_start); + const size_t n_float = static_cast(enc_h) * static_cast(emit_end - emit_start); + std::memcpy(out_enc_host.data() + dst_off, win_host.data() + src_off, n_float * sizeof(float)); + } + + out_T_enc = T_total; + return TRANSCRIBE_OK; +} + // Apply the adapter (pos_emb get_rows + add, plus optional proj) to an // encoder slice. The pos_ids supplied to the graph are absolute frame // positions starting at `abs_frame_offset`, so the slice can sit @@ -1168,8 +1289,8 @@ transcribe_status run_one_shot_inner(MoonshineStreamingSession * cc, std::vector enc_host; int T_enc = 0; - if (auto st = encode_window_to_host(cc, cm, pcm_padded.data(), n_samples_padded, - /*emit_dumps=*/true, enc_host, T_enc); + if (auto st = encode_pcm_to_host(cc, cm, pcm_padded.data(), n_samples_padded, + /*emit_dumps=*/true, enc_host, T_enc); st != TRANSCRIBE_OK) { return st; } @@ -1866,8 +1987,8 @@ transcribe_status run_batch(transcribe_session * session, std::vector pcm_padded = right_pad_pcm(pcm[b], n_samples[b], hp.enc_frame_len); std::vector enc_host; int t_enc_b = 0; - if (encode_window_to_host(cc, cm, pcm_padded.data(), static_cast(pcm_padded.size()), - /*emit_dumps=*/false, enc_host, t_enc_b) != TRANSCRIBE_OK || + if (encode_pcm_to_host(cc, cm, pcm_padded.data(), static_cast(pcm_padded.size()), + /*emit_dumps=*/false, enc_host, t_enc_b) != TRANSCRIBE_OK || t_enc_b <= 0) { continue; } From f701ca077ceb80c08fcbc2d1279a441c8038bd4b Mon Sep 17 00:00:00 2001 From: CJ Pais Date: Tue, 20 Oct 2026 02:15:35 -0700 Subject: [PATCH 2/3] moonshine_streaming: enforce the 81.9 s input cap instead of reading OOB The adapter adds pos_emb[frame] to every encoder frame from a table of dec_max_position_embeddings (4096) rows - 4096 encoder frames, 81.92 s of audio per decode. The reference raises IndexError past it. The port had no bound, so a longer input fed frame indices >= 4096 to ggml_get_rows: - CPU: GGML_ASSERT(i01 >= 0 && i01 < ne01) aborts the process (rc 134), for one-shot input over 82 s and for any stream that runs past 82 s - a host app dictating for a minute and a half on a CPU backend crashes. - Metal: silent out-of-bounds reads, i.e. garbage transcripts (, repetition loops on long files). Enforce the cap with the contract docs/input-limits.md already defines for an absolute position wall (voxtral_realtime): - transcribe_run: input whose padded length needs more than 4096 encoder frames is rejected with TRANSCRIBE_ERR_INPUT_TOO_LONG before encoding. - transcribe_run_batch: the same, per utterance (the batched GPU path now carries a per-row fail status instead of hardcoding INVALID_ARG); the serial path inherits it from run(). - Streaming: emission stops at frame 4096. The stream is flagged transcribe_was_truncated() with a single WARN, later audio is dropped (and not buffered), and feeds/finalize keep returning TRANSCRIBE_OK. A stream that ends inside the last right-context window past the cap is handled at finalize the same way. - apply_adapter_window refuses any frame range outside the table, as the backstop that keeps get_rows in bounds whatever a caller does. - capabilities.max_audio_ms reports 81920 (it advertised ~1024 s, derived from the decoder token cap, which is not the binding limit). Docs move moonshine_streaming from the soft-window bucket ("output-bound, ~17 min") to the hard-cap bucket and spell out the streaming behaviour. Verified on tiny/small/medium: 4096 frames (81.92 s) runs, 4097 is rejected, on Metal and CPU; a 120 s stream on CPU (which aborted before) finalizes OK, truncated, and its transcript equals one-shot over the first 81.92 s. tests/moonshine_streaming_batch_truncation is replaced by tests/moonshine_streaming_input_cap: its long row (love-loss.wav, 197 s) only "truncated" because the decode was running on out-of-bounds positions. The new gated test pins the boundary, one-shot rejection, the per-row batch status, and a stream past the cap (exact transcript on the default backend, word for word on CPU, whose shape-dependent kernels can move a comma), on both backends. --- docs/input-limits.md | 18 +- docs/models/moonshine-streaming.md | 23 +- src/arch/moonshine_streaming/model.cpp | 142 ++++++++-- .../moonshine_streaming/moonshine_streaming.h | 4 + tests/CMakeLists.txt | 22 +- .../moonshine_streaming_batch_truncation.cpp | 142 ---------- tests/moonshine_streaming_input_cap.cpp | 248 ++++++++++++++++++ 7 files changed, 412 insertions(+), 187 deletions(-) delete mode 100644 tests/moonshine_streaming_batch_truncation.cpp create mode 100644 tests/moonshine_streaming_input_cap.cpp diff --git a/docs/input-limits.md b/docs/input-limits.md index 1c330eb7..bd8963be 100644 --- a/docs/input-limits.md +++ b/docs/input-limits.md @@ -74,7 +74,7 @@ need and do not have a length gate. | Families | Limit source | Behavior | | --- | --- | --- | -| qwen3_asr, canary_qwen, funasr_nano, granite, granite_nar, voxtral, cohere, canary | decoder context window (`dec_max_position_embeddings` / `dec_max_seq`), or the encoder positional table (`enc_pos_emb_max_len`, for cohere/canary) — all from GGUF | KV cache grows to fit, clamped to the model's true max. Over-length input is **rejected before the decode** (or before the encoder, where the encoder table is the binding limit) with `TRANSCRIBE_ERR_INPUT_TOO_LONG`. | +| qwen3_asr, canary_qwen, funasr_nano, granite, granite_nar, voxtral, cohere, canary, moonshine_streaming | decoder context window (`dec_max_position_embeddings` / `dec_max_seq`), or an encoder-side positional table (`enc_pos_emb_max_len` for cohere/canary; the adapter `pos_emb` table, 4096 frames = 81.9 s, for moonshine_streaming) — all from GGUF | KV cache grows to fit, clamped to the model's true max. Over-length input is **rejected before the decode** (or before the encoder, where the encoder table is the binding limit) with `TRANSCRIBE_ERR_INPUT_TOO_LONG`. A moonshine_streaming *stream* that reaches its cap is truncated instead (see "Streaming is the exception"). | A clip's sample count deterministically fixes its decoder prefill size or encoder frame count, so the library checks the relevant bound before running @@ -107,7 +107,7 @@ on sequences which are identical and are obviously looping | Families | Window | Behavior | | --- | --- | --- | -| sensevoice (~30 s), medasr (~400 s), moonshine (output-bound, ~48 s), moonshine_streaming (output-bound, ~17 min) | training / positional window | Any length is accepted; past the window the library emits a `WARN` (degraded accuracy is possible) and proceeds. `max_audio_ms` reports the window as advisory. | +| sensevoice (~30 s), medasr (~400 s), moonshine (output-bound, ~48 s) | training / positional window | Any length is accepted; past the window the library emits a `WARN` (degraded accuracy is possible) and proceeds. `max_audio_ms` reports the window as advisory. | These families have no hard architectural wall but were trained on a bounded window; beyond it, accuracy degrades rather than failing. The library does not @@ -119,11 +119,12 @@ Moonshine is the honest edge case in this bucket: its cap is on *output* audio length — a dense short clip can hit it too. It is reported via `transcribe_was_truncated()` and a `WARN` (and, offline, the hard `TRANSCRIBE_ERR_OUTPUT_TRUNCATED` status) when the cap is reached. -`moonshine_streaming` has the same output-bound shape with a much larger window -(`dec_max_position_embeddings = 4096`, ≈ 17 min); because it also streams, its -truncation follows the streaming rule below — `stream_finalize` still returns -`TRANSCRIBE_OK` and the truncation surfaces only through -`transcribe_was_truncated()`. +`moonshine_streaming` used to be listed here as output-bound (≈ 17 min). It is +not: its encoder adapter adds a position embedding per encoder frame from a +4096-entry table, so input past 81.9 s has no defined output (the reference +raises an index error, and the port used to read past the table). It is now a +hard-cap family (bucket 2) for offline input, and a stream that reaches the cap +follows the streaming rule below. ## Context sizing and the `n_ctx` knob @@ -194,7 +195,8 @@ IDLE/ACTIVE/FINISHED/FAILED), and `stream_feed` / `stream_finalize` return the status of *that step*, not a verdict on the whole transcript. So when a streaming decode reaches its context cap (e.g. `voxtral_realtime` at its absolute position limit — hours of continuous audio, or `moonshine_streaming` -at its output window), the stream does **not** fail and `stream_finalize` +at its 81.9 s input cap, past which it ignores further audio), the stream does +**not** fail and `stream_finalize` returns `TRANSCRIBE_OK`; the truncation is surfaced through `transcribe_was_truncated(session)` and a `WARN`. This is deliberate: forcing a stream into a failed terminal state on truncation would discard the diff --git a/docs/models/moonshine-streaming.md b/docs/models/moonshine-streaming.md index 82af534e..336b7bae 100644 --- a/docs/models/moonshine-streaming.md +++ b/docs/models/moonshine-streaming.md @@ -64,12 +64,23 @@ each per-variant doc has direct download links. ## Input limits -No input-length limit, but the decoder is capped at its output window — about -**17 minutes** of typical speech (a 4,096-token decode window, shared across -variants). Offline, a transcript that reaches the cap is returned with -`TRANSCRIBE_ERR_OUTPUT_TRUNCATED` (partial text retained); when streaming, the -stream keeps its committed text and sets `transcribe_was_truncated()` while -finalize still returns OK. See the [input-length contract](../input-limits.md). +**81.9 s of audio per decode** (all variants). The encoder adapter adds a +learned position embedding to every encoder frame from a 4,096-entry table, and +one encoder frame is 20 ms; the reference implementation has no defined output +past it either. `transcribe_capabilities::max_audio_ms` reports 81920. + +- **Offline** (`transcribe_run`, `transcribe_run_batch`): longer input is + rejected up front with `TRANSCRIBE_ERR_INPUT_TOO_LONG` (per utterance in a + batch). Split long recordings before transcribing them. +- **Streaming**: a stream that runs past 81.9 s keeps its transcript up to the + cap, ignores later audio, sets `transcribe_was_truncated()` and logs one + `WARN`; feeds and finalize still return `TRANSCRIBE_OK`. Start a new stream + (for example at a pause) to keep transcribing. + +Within that window the decoder can still stop early: a transcript that reaches +the generation budget is returned with `TRANSCRIBE_ERR_OUTPUT_TRUNCATED` +offline (partial text retained), or flagged via `transcribe_was_truncated()` +when streaming. See the [input-length contract](../input-limits.md). ## Quick start diff --git a/src/arch/moonshine_streaming/model.cpp b/src/arch/moonshine_streaming/model.cpp index 313aa5e1..04e0c384 100644 --- a/src/arch/moonshine_streaming/model.cpp +++ b/src/arch/moonshine_streaming/model.cpp @@ -79,22 +79,39 @@ MoonshineStreamingModel::~MoonshineStreamingModel() { plan.primary_kind = transcribe::BackendKind::Unknown; } -// Input-length contract (see docs/input-limits.md): like moonshine, the wall -// is on *output*, not input. The encoder takes any PCM length; the decode loop -// stops at the decoder position cap (dec_max_position_embeddings, e.g. 4096) -// before EOS, silently truncating. On a cap-exit we set -// transcribe_was_truncated() and WARN (same as qwen3_asr). - -// Advisory transcribe_capabilities::max_audio_ms: the audio the output budget -// (dec_max_position_embeddings tokens) covers at ~4 output tokens/sec. 0 means -// unknown/unbounded. Advisory only — does not reject. +// Input-length contract (see docs/input-limits.md). Two walls: +// +// - Input: the adapter adds pos_emb[frame] to every encoder frame, from a +// table of dec_max_position_embeddings rows (weights.cpp pins the +// adapter.pos_emb.weight shape to it; 4096 for every published size). +// That is 4096 encoder frames = 81.92 s of audio per decode, and the +// reference raises IndexError past it. A frame index beyond the table +// used to reach ggml_get_rows out of bounds: GGML_ASSERT abort on CPU, +// garbage reads on Metal. One-shot / batch input past it is rejected with +// TRANSCRIBE_ERR_INPUT_TOO_LONG; a stream stops consuming audio at the +// cap and reports transcribe_was_truncated() (the voxtral_realtime +// contract for an absolute position cap). +// - Output: the decode loop stops at the decoder position cap (also +// dec_max_position_embeddings tokens) or the duration budget before EOS; +// that sets transcribe_was_truncated() and WARNs. +int adapter_max_frames(const MoonshineStreamingHParams & hp) { + return hp.dec_max_position_embeddings; +} + +// transcribe_capabilities::max_audio_ms: the input wall above, in ms of +// 16 kHz audio (two stride-2 convs over frame_len-sample frames, so one +// encoder frame is 4 * frame_len samples). Also capped by the advisory audio +// the output budget covers at ~4 tokens/sec, which is far larger. 0 means +// unknown. constexpr int k_tokens_per_sec = 4; // rough speech-rate estimate; advisory only int64_t moonshine_streaming_max_audio_ms(const MoonshineStreamingHParams & hp) { - if (hp.dec_max_position_embeddings <= 0) { + if (hp.dec_max_position_embeddings <= 0 || hp.enc_frame_len <= 0) { return 0; } - return static_cast(hp.dec_max_position_embeddings) * 1000 / k_tokens_per_sec; + const int64_t output_ms = static_cast(hp.dec_max_position_embeddings) * 1000 / k_tokens_per_sec; + const int64_t input_ms = static_cast(adapter_max_frames(hp)) * 4 * hp.enc_frame_len * 1000 / 16000; + return std::min(output_ms, input_ms); } bool kv_cache_init(MoonshineStreamingKvCache & cache, @@ -676,6 +693,15 @@ transcribe_status apply_adapter_window(MoonshineStreamingSession * cc, const int enc_h = hp.enc_d_model; const int dec_h = hp.dec_d_model; + // Every caller bounds its frames to the table first; this is the backstop + // that keeps ggml_get_rows in range no matter what (see adapter_max_frames). + if (static_cast(abs_frame_offset) + n_frames > adapter_max_frames(hp)) { + log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, + "moonshine_streaming adapter: frames [%d, %d) exceed the %d-entry position table", abs_frame_offset, + abs_frame_offset + n_frames, adapter_max_frames(hp)); + return TRANSCRIBE_ERR_INPUT_TOO_LONG; + } + const int64_t t_start = ggml_time_us(); if (auto st = ensure_sched(cc, cm); st != TRANSCRIBE_OK) { @@ -1287,6 +1313,16 @@ transcribe_status run_one_shot_inner(MoonshineStreamingSession * cc, std::vector pcm_padded = right_pad_pcm(pcm, n_samples, hp.enc_frame_len); const int n_samples_padded = static_cast(pcm_padded.size()); + // Reject before encoding: past the adapter's position table the model has + // no defined output (see adapter_max_frames). + if (const int T_needed = encoder_t_enc(hp, n_samples_padded); T_needed > adapter_max_frames(hp)) { + log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, + "moonshine_streaming run: input too long - %d encoder frames exceed the model's %d-frame limit " + "(%.1f s). Shorten the audio or stream it. See transcribe_capabilities.max_audio_ms.", + T_needed, adapter_max_frames(hp), static_cast(moonshine_streaming_max_audio_ms(hp)) / 1000.0); + return TRANSCRIBE_ERR_INPUT_TOO_LONG; + } + std::vector enc_host; int T_enc = 0; if (auto st = encode_pcm_to_host(cc, cm, pcm_padded.data(), n_samples_padded, @@ -1399,11 +1435,39 @@ transcribe_status decode_partial(MoonshineStreamingSession * cc, if (auto st = decode_from_kv_cache(cc, cm, T_enc, params, emit_dumps, interim); st != TRANSCRIBE_OK) { return st; } + // decode_from_kv_cache resets the truncation flags to describe its own + // transcript; a stream that hit the input cap stays truncated regardless. + if (cc->stream_input_capped) { + cc->was_truncated = true; + } cc->stream_last_decoded_T = T_enc; cc->enc_T = T_enc; return TRANSCRIBE_OK; } +// Once every frame the adapter can position has been emitted, the stream is +// at the model's input cap (see adapter_max_frames): flag it truncated, warn +// once, and release the PCM nothing will read again. Later feeds drop their +// audio; finalize decodes what was emitted. +void mark_stream_input_capped_if_full(MoonshineStreamingSession * cc, MoonshineStreamingModel * cm) { + if (cc->stream_input_capped || cc->stream_T_emitted < adapter_max_frames(cm->hparams)) { + return; + } + cc->stream_input_capped = true; + cc->was_truncated = true; + transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_WARN, + "moonshine_streaming stream: input reached the model's %d-frame limit (%.1f s of audio); " + "later audio is ignored and the transcript ends there. See " + "transcribe_capabilities.max_audio_ms.", + adapter_max_frames(cm->hparams), + static_cast(moonshine_streaming_max_audio_ms(cm->hparams)) / 1000.0); + // Keep stream_pcm_start_sample pointing past the dropped samples so the + // stream never looks empty to stream_finalize. + cc->stream_pcm_start_sample += static_cast(cc->stream_pcm_buffer.size()); + cc->stream_pcm_buffer.clear(); + cc->stream_pcm_buffer.shrink_to_fit(); +} + std::vector token_ids_from_rows(const std::vector & tokens) { std::vector ids; ids.reserve(tokens.size()); @@ -1535,6 +1599,7 @@ transcribe_status stream_begin(transcribe_session * session, cc->stream_cross_v_committed.assign(static_cast(hp.dec_n_layers), std::vector{}); cc->stream_T_emitted = 0; cc->stream_last_decoded_T = 0; + cc->stream_input_capped = false; cc->stream_token_id_history.clear(); cc->stream_L_total_frames = cumulative_left_context(hp); cc->stream_R_total_frames = cumulative_right_context(hp); @@ -1699,9 +1764,25 @@ transcribe_status stream_feed(transcribe_session * session, return TRANSCRIBE_ERR_ABORTED; } - cc->stream_pcm_buffer.insert(cc->stream_pcm_buffer.end(), pcm, pcm + n_samples); cc->stream_audio_input_us += samples_to_us(n_samples); + // Past the input cap nothing more can be transcribed (see + // adapter_max_frames): drop the audio instead of buffering it, keep the + // last transcript, and report the stream as fully consumed. + if (cc->stream_input_capped) { + cc->stream_audio_committed_us = cc->stream_audio_input_us; + if (update != nullptr) { + update->result_changed = false; + update->revision = cc->stream_revision; + update->input_received_ms = us_to_ms(cc->stream_audio_input_us); + update->audio_committed_ms = us_to_ms(cc->stream_audio_committed_us); + update->buffered_ms = 0; + } + return TRANSCRIBE_OK; + } + + cc->stream_pcm_buffer.insert(cc->stream_pcm_buffer.end(), pcm, pcm + n_samples); + const int spf = cc->stream_samples_per_enc_frame; // total_pcm and available_frames are computed in ABSOLUTE frames // (the buffer holds samples [pcm_start_sample, ...)). @@ -1710,7 +1791,7 @@ transcribe_status stream_feed(transcribe_session * session, const int R = cc->stream_R_total_frames; const int L = cc->stream_L_total_frames; - const int stable_frames = available_frames - R; + const int stable_frames = std::min(available_frames - R, adapter_max_frames(cm->hparams)); bool result_changed = false; if (stable_frames > cc->stream_T_emitted) { const int emit_start = cc->stream_T_emitted; @@ -1720,6 +1801,7 @@ transcribe_status stream_feed(transcribe_session * session, if (auto st = flush_stable_frames(cc, cm, win_start, win_end, emit_start, emit_end); st != TRANSCRIBE_OK) { return st; } + mark_stream_input_capped_if_full(cc, cm); cc->stream_audio_committed_us = static_cast(cc->stream_T_emitted) * static_cast(spf) * 1000000LL / k_sample_rate_hz; @@ -1830,16 +1912,25 @@ transcribe_status stream_finalize(transcribe_session * session, transcribe_strea (st == TRANSCRIBE_OK) ? 0 : us_to_ms(cc->stream_audio_input_us - cc->stream_audio_committed_us); }; - if (T_total > cc->stream_T_emitted) { + // The tail can still run past the input cap when the stream ended inside + // the last right-context window; emit up to the cap and report the rest + // as truncated (see adapter_max_frames). + const int T_emit_end = std::min(T_total, adapter_max_frames(hp)); + if (T_emit_end > cc->stream_T_emitted) { const int emit_start = cc->stream_T_emitted; - const int emit_end = T_total; + const int emit_end = T_emit_end; const int win_start = std::max(0, emit_start - cc->stream_L_total_frames); - const int win_end = emit_end; + // Right context: the real audio past the cap when there is any, the end + // of the stream otherwise - exactly what a mid-stream flush would see. + const int win_end = std::min(T_total, emit_end + cc->stream_R_total_frames); if (auto st = flush_stable_frames(cc, cm, win_start, win_end, emit_start, emit_end); st != TRANSCRIBE_OK) { write_update(st); return st; } } + if (T_total > adapter_max_frames(hp)) { + mark_stream_input_capped_if_full(cc, cm); + } const int T_enc = cc->stream_T_emitted; if (T_enc <= 0) { @@ -1868,9 +1959,9 @@ transcribe_status stream_finalize(transcribe_session * session, transcribe_strea write_update(st); return st; } - } else if (cc->was_truncated) { + } else if (cc->was_truncated && (cc->stopped_on_repetition || !cc->stream_input_capped)) { // The last feed's interim decode is the final transcript, and it only - // logged its stop at DEBUG. + // logged its stop at DEBUG. (An input-cap stop already warned once.) transcribe::log_msg( TRANSCRIBE_LOG_LEVEL_WARN, "moonshine_streaming stream: the final transcript %s before end-of-stream; it may be " @@ -1919,6 +2010,7 @@ void stream_reset(transcribe_session * session) { } cc->stream_T_emitted = 0; cc->stream_last_decoded_T = 0; + cc->stream_input_capped = false; cc->stream_token_id_history.clear(); } @@ -1973,6 +2065,7 @@ transcribe_status run_batch(transcribe_session * session, // ----- Pass 1: serial per-utterance encoder + adapter ----- std::vector valid(n, 0); + std::vector fail_status(n, TRANSCRIBE_ERR_INVALID_ARG); std::vector> adapter_hosts(n); std::vector T_enc(n, 0); int T_enc_max = 0; @@ -1985,6 +2078,15 @@ transcribe_status run_batch(transcribe_session * session, continue; } std::vector pcm_padded = right_pad_pcm(pcm[b], n_samples[b], hp.enc_frame_len); + if (const int T_needed = encoder_t_enc(hp, static_cast(pcm_padded.size())); + T_needed > adapter_max_frames(hp)) { + log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, + "moonshine_streaming run_batch: utterance %d input too long - %d encoder frames exceed the " + "model's %d-frame limit. See transcribe_capabilities.max_audio_ms.", + b, T_needed, adapter_max_frames(hp)); + fail_status[b] = TRANSCRIBE_ERR_INPUT_TOO_LONG; + continue; + } std::vector enc_host; int t_enc_b = 0; if (encode_pcm_to_host(cc, cm, pcm_padded.data(), static_cast(pcm_padded.size()), @@ -2006,7 +2108,7 @@ transcribe_status run_batch(transcribe_session * session, if (T_enc_max <= 0) { for (int b = 0; b < n; ++b) { transcribe_session::ResultSet rs; - rs.status = TRANSCRIBE_ERR_INVALID_ARG; + rs.status = fail_status[b]; cc->batch_results.push_back(std::move(rs)); } return TRANSCRIBE_OK; @@ -2273,7 +2375,7 @@ transcribe_status run_batch(transcribe_session * session, for (int b = 0; b < n; ++b) { transcribe_session::ResultSet rs; if (!valid[b]) { - rs.status = TRANSCRIBE_ERR_INVALID_ARG; + rs.status = fail_status[b]; cc->batch_results.push_back(std::move(rs)); continue; } diff --git a/src/arch/moonshine_streaming/moonshine_streaming.h b/src/arch/moonshine_streaming/moonshine_streaming.h index cf08d28b..aad072e5 100644 --- a/src/arch/moonshine_streaming/moonshine_streaming.h +++ b/src/arch/moonshine_streaming/moonshine_streaming.h @@ -166,6 +166,10 @@ struct MoonshineStreamingSession final : public transcribe_session { // stream_feed / stream_finalize skip a redundant decode when no // new encoder frames have been committed since the previous one. int32_t stream_last_decoded_T = 0; + // Set once stream_T_emitted reaches the adapter's position table + // (model.cpp adapter_max_frames): further audio is dropped and the + // stream reports transcribe_was_truncated(). Reset at stream_begin. + bool stream_input_capped = false; // Recent partial-decode token id sequences, used to compute the // longest common prefix that's safe to mark committed across feeds. // Reset at stream_begin; updated after every successful partial decode. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 04008a32..02286afa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1027,30 +1027,30 @@ if(TRANSCRIBE_BUILD_REAL_MODEL_TESTS) SKIP_RETURN_CODE 77) endif() -# Per-utterance OUTPUT_TRUNCATED in transcribe_run_batch (moonshine_streaming). +# moonshine_streaming input cap (81.92 s, the adapter position table): one-shot, +# run_batch and streaming past it, on the default backend and CPU. # Skipped (RC 77) when TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF is unset. if(TRANSCRIBE_BUILD_REAL_MODEL_TESTS) - add_executable(transcribe_moonshine_streaming_batch_truncation - moonshine_streaming_batch_truncation.cpp) + add_executable(transcribe_moonshine_streaming_input_cap + moonshine_streaming_input_cap.cpp) - target_link_libraries(transcribe_moonshine_streaming_batch_truncation + target_link_libraries(transcribe_moonshine_streaming_input_cap PRIVATE transcribe transcribe-common-example) - target_compile_definitions(transcribe_moonshine_streaming_batch_truncation PRIVATE + target_compile_definitions(transcribe_moonshine_streaming_input_cap PRIVATE "TRANSCRIBE_TEST_SAMPLES_DIR=\"${CMAKE_SOURCE_DIR}/samples\"") - transcribe_apply_warnings(transcribe_moonshine_streaming_batch_truncation) + transcribe_apply_warnings(transcribe_moonshine_streaming_input_cap) - add_test(NAME transcribe_moonshine_streaming_batch_truncation - COMMAND transcribe_moonshine_streaming_batch_truncation) - set_tests_properties(transcribe_moonshine_streaming_batch_truncation PROPERTIES + add_test(NAME transcribe_moonshine_streaming_input_cap + COMMAND transcribe_moonshine_streaming_input_cap) + set_tests_properties(transcribe_moonshine_streaming_input_cap PROPERTIES SKIP_RETURN_CODE 77) endif() # Per-utterance OUTPUT_TRUNCATED in transcribe_run / transcribe_run_batch for a # causal_lm (LLM-decoder) family. Guards the shared src/causal_lm batched step -# loop's truncation detection (the encoder-decoder twin is covered by -# transcribe_moonshine_streaming_batch_truncation). Skipped (RC 77) when +# loop's truncation detection. Skipped (RC 77) when # TRANSCRIBE_QWEN3_ASR_0_6B_GGUF is unset. if(TRANSCRIBE_BUILD_REAL_MODEL_TESTS) add_executable(transcribe_qwen3_asr_batch_truncation diff --git a/tests/moonshine_streaming_batch_truncation.cpp b/tests/moonshine_streaming_batch_truncation.cpp deleted file mode 100644 index c569f632..00000000 --- a/tests/moonshine_streaming_batch_truncation.cpp +++ /dev/null @@ -1,142 +0,0 @@ -// moonshine_streaming_batch_truncation.cpp - real-model gated test that -// transcribe_run_batch reports output truncation PER UTTERANCE. -// -// Moonshine's cap is on output (max_length decode tokens), not input, so a -// long clip runs the decoder into the cap before end-of-stream. In a batch, -// that must surface as a per-utterance cut-short status on the affected row -// (with its partial text retained), while a short row that finishes normally -// stays TRANSCRIBE_OK and the whole-batch call still returns OK. -// transcribe_was_truncated() is also set. See docs/input-limits.md. -// -// Past its window the tiny model can fall into a loop before it reaches the -// cap, and then the repetition guard stops it first. Which stop fires depends -// on the model's output, so row 1 accepts either cut-short status. -// -// Batch makeup: -// row 0 = jfk.wav (~11 s) -> completes under the cap -> OK -// row 1 = love-loss.wav (~197 s) -> exceeds the cap -> OUTPUT_TRUNCATED / OUTPUT_REPETITION -// -// Gating: -// - TRANSCRIBE_BUILD_REAL_MODEL_TESTS (CMake, default OFF) builds it. -// - At runtime, TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF points at a tiny -// GGUF. If unset/missing (or a sample is missing), exits 77 ("skipped"). - -#include "transcribe.h" -#include "wav.h" - -#include - -#include -#include -#include -#include - -namespace { - -int g_failures = 0; - -#define CHECK(cond) \ - do { \ - if (!(cond)) { \ - std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ - ++g_failures; \ - } \ - } while (0) - -#define CHECK_EQ_INT(actual, expected) \ - do { \ - const long long _a = static_cast(actual); \ - const long long _e = static_cast(expected); \ - if (_a != _e) { \ - std::fprintf(stderr, "FAIL %s:%d: %s = %lld, expected %lld\n", __FILE__, __LINE__, #actual, _a, _e); \ - ++g_failures; \ - } \ - } while (0) - -bool file_exists(const std::string & path) { - struct stat st{}; - return ::stat(path.c_str(), &st) == 0; -} - -bool load_sample(const std::string & name, std::vector & pcm) { - const std::string path = std::string(TRANSCRIBE_TEST_SAMPLES_DIR) + "/" + name; - if (!file_exists(path)) { - std::fprintf(stderr, "skipping: %s missing\n", path.c_str()); - return false; - } - std::string err; - if (!transcribe_cli::load_wav_mono_16k(path, pcm, err) || pcm.empty()) { - std::fprintf(stderr, "failed to load %s: %s\n", path.c_str(), err.c_str()); - return false; - } - return true; -} - -} // namespace - -int main() { - const char * model_path = std::getenv("TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF"); - if (model_path == nullptr || *model_path == '\0' || !file_exists(model_path)) { - std::fprintf(stderr, - "skipping: TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF unset " - "or missing\n"); - return 77; - } - - std::vector pcm_short, pcm_long; - if (!load_sample("jfk.wav", pcm_short)) { - return 77; - } - if (!load_sample("love-loss.wav", pcm_long)) { - return 77; - } - - transcribe_model_load_params mp; - transcribe_model_load_params_init(&mp); - struct transcribe_model * model = nullptr; - if (transcribe_model_load_file(model_path, &mp, &model) != TRANSCRIBE_OK) { - std::fprintf(stderr, "model load failed: %s\n", model_path); - return 1; - } - - transcribe_session_params sp; - transcribe_session_params_init(&sp); - struct transcribe_session * s = nullptr; - if (transcribe_session_init(model, &sp, &s) != TRANSCRIBE_OK) { - std::fprintf(stderr, "session init failed\n"); - transcribe_model_free(model); - return 1; - } - - const float * pcms[2] = { pcm_short.data(), pcm_long.data() }; - const int lens[2] = { (int) pcm_short.size(), (int) pcm_long.size() }; - - // The whole-batch call succeeds even though a row truncates. - CHECK(transcribe_run_batch(s, pcms, lens, 2, nullptr) == TRANSCRIBE_OK); - CHECK_EQ_INT(transcribe_batch_n_results(s), 2); - - // Row 0 (short) completes; row 1 (long) is cut short at the output cap or - // by the repetition guard. - CHECK(transcribe_batch_status(s, 0) == TRANSCRIBE_OK); - const transcribe_status st1 = transcribe_batch_status(s, 1); - CHECK(st1 == TRANSCRIBE_ERR_OUTPUT_TRUNCATED || st1 == TRANSCRIBE_ERR_OUTPUT_REPETITION); - - // Both rows keep their (partial, for row 1) transcript. - for (int i = 0; i < 2; ++i) { - const char * text = transcribe_batch_full_text(s, i); - CHECK(text != nullptr && text[0] != '\0'); - } - - // The supplemental flag is set whenever any row truncated. - CHECK(transcribe_was_truncated(s) == true); - - transcribe_session_free(s); - transcribe_model_free(model); - - if (g_failures > 0) { - std::fprintf(stderr, "moonshine_streaming_batch_truncation: %d failures\n", g_failures); - return EXIT_FAILURE; - } - std::fprintf(stdout, "moonshine_streaming_batch_truncation: ok\n"); - return EXIT_SUCCESS; -} diff --git a/tests/moonshine_streaming_input_cap.cpp b/tests/moonshine_streaming_input_cap.cpp new file mode 100644 index 00000000..7e00d69a --- /dev/null +++ b/tests/moonshine_streaming_input_cap.cpp @@ -0,0 +1,248 @@ +// moonshine_streaming_input_cap.cpp - real-model gated test of the +// moonshine_streaming input cap. +// +// The adapter adds pos_emb[frame] to every encoder frame from a table of +// dec_max_position_embeddings (4096) rows: 4096 encoder frames = 81.92 s of +// audio per decode. Past it the model is undefined (the reference raises +// IndexError), and the port used to read the table out of bounds - a +// GGML_ASSERT abort on CPU, garbage on Metal. This pins the contract +// (docs/input-limits.md), on the default backend and on CPU: +// +// 1. capabilities.max_audio_ms reports the 81.92 s wall. +// 2. One-shot: exactly 4096 frames runs; 4097 is TRANSCRIBE_ERR_INPUT_TOO_LONG, +// rejected before encoding, with no transcript and no abort. +// 3. transcribe_run_batch: an over-cap row is a per-utterance +// INPUT_TOO_LONG while the other rows complete and the whole call +// returns OK. On a GPU backend this goes through the batched path. +// 4. Streaming past the cap: every feed and finalize return OK, the stream +// is flagged transcribe_was_truncated(), audio past the cap is ignored, +// and the final transcript equals one-shot over the first 81.92 s +// (exactly on the default backend; word for word on CPU, see words_of). +// +// Gating: +// - TRANSCRIBE_BUILD_REAL_MODEL_TESTS (CMake, default OFF) builds it. +// - At runtime, TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF points at a tiny +// GGUF. If unset/missing (or a sample is missing), exits 77 ("skipped"). + +#include "transcribe.h" +#include "transcribe/moonshine_streaming.h" +#include "wav.h" + +#include + +#include +#include +#include +#include +#include +#include + +namespace { + +int g_failures = 0; + +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + ++g_failures; \ + } \ + } while (0) + +#define CHECK_EQ_INT(actual, expected) \ + do { \ + const long long _a = static_cast(actual); \ + const long long _e = static_cast(expected); \ + if (_a != _e) { \ + std::fprintf(stderr, "FAIL %s:%d: %s = %lld, expected %lld\n", __FILE__, __LINE__, #actual, _a, _e); \ + ++g_failures; \ + } \ + } while (0) + +// 4096 encoder frames x 4 embedder frames x 80 samples. +constexpr int k_cap_samples = 4096 * 4 * 80; + +bool file_exists(const std::string & path) { + struct stat st{}; + return ::stat(path.c_str(), &st) == 0; +} + +bool load_sample(const std::string & name, std::vector & pcm) { + const std::string path = std::string(TRANSCRIBE_TEST_SAMPLES_DIR) + "/" + name; + if (!file_exists(path)) { + std::fprintf(stderr, "skipping: %s missing\n", path.c_str()); + return false; + } + std::string err; + if (!transcribe_cli::load_wav_mono_16k(path, pcm, err) || pcm.empty()) { + std::fprintf(stderr, "failed to load %s: %s\n", path.c_str(), err.c_str()); + return false; + } + return true; +} + +// Lower-cased words with punctuation stripped. On CPU, ggml's kernels pick +// shape-dependent paths, so a stream's encoder slices and the one-shot graph +// can round differently and the decoder may place a comma differently (seen: +// "college," vs "college"). The words are what proves the stream covered +// exactly the first 81.92 s. +std::vector words_of(const std::string & text) { + std::vector out; + std::string cur; + for (const char c : text) { + if (std::isalnum(static_cast(c)) != 0 || c == '\'') { + cur.push_back(static_cast(std::tolower(static_cast(c)))); + } else if (!cur.empty()) { + out.push_back(cur); + cur.clear(); + } + } + if (!cur.empty()) { + out.push_back(cur); + } + return out; +} + +std::string full_text(transcribe_session * s) { + const char * t = transcribe_full_text(s); + return t == nullptr ? std::string() : std::string(t); +} + +// Stream `pcm` in 2 s chunks. Returns the final transcript; every step must +// succeed even past the cap. +std::string stream_all(transcribe_session * s, const std::vector & pcm) { + transcribe_run_params rp; + transcribe_run_params_init(&rp); + transcribe_stream_params sp; + transcribe_stream_params_init(&sp); + transcribe_moonshine_streaming_stream_ext ms; + transcribe_moonshine_streaming_stream_ext_init(&ms); + ms.min_decode_interval_ms = 1000; + sp.family = &ms.ext; + CHECK(transcribe_stream_begin(s, &rp, &sp) == TRANSCRIBE_OK); + + constexpr size_t k_chunk = 32000; + for (size_t pos = 0; pos < pcm.size(); pos += k_chunk) { + const size_t take = std::min(k_chunk, pcm.size() - pos); + transcribe_stream_update upd; + transcribe_stream_update_init(&upd); + CHECK(transcribe_stream_feed(s, pcm.data() + pos, static_cast(take), &upd) == TRANSCRIBE_OK); + } + transcribe_stream_update fin; + transcribe_stream_update_init(&fin); + CHECK(transcribe_stream_finalize(s, &fin) == TRANSCRIBE_OK); + return full_text(s); +} + +void check_backend(const char * model_path, + transcribe_backend_request backend, + const char * label, + bool exact_text, + const std::vector & pcm_short, + const std::vector & pcm_long) { + std::fprintf(stdout, "-- %s\n", label); + transcribe_model_load_params mp; + transcribe_model_load_params_init(&mp); + mp.backend = backend; + struct transcribe_model * model = nullptr; + if (transcribe_model_load_file(model_path, &mp, &model) != TRANSCRIBE_OK) { + std::fprintf(stderr, "FAIL %s: model load failed\n", label); + ++g_failures; + return; + } + + // 1. Advertised limit. + transcribe_capabilities caps; + transcribe_capabilities_init(&caps); + CHECK(transcribe_model_get_capabilities(model, &caps) == TRANSCRIBE_OK); + CHECK_EQ_INT(caps.max_audio_ms, 81920); + + transcribe_session_params sp; + transcribe_session_params_init(&sp); + struct transcribe_session * s = nullptr; + if (transcribe_session_init(model, &sp, &s) != TRANSCRIBE_OK) { + std::fprintf(stderr, "FAIL %s: session init failed\n", label); + ++g_failures; + transcribe_model_free(model); + return; + } + + // 2. One-shot at and just past the cap. A single extra 80-sample frame + // (after padding) pushes T_enc to 4097. + const std::vector at_cap(pcm_long.begin(), pcm_long.begin() + k_cap_samples); + const std::vector over_cap(pcm_long.begin(), pcm_long.begin() + k_cap_samples + 1); + // At the cap the input is accepted; whether the tiny model's greedy decode + // then finishes cleanly is a property of the audio, not of the cap. + const transcribe_status at_cap_st = transcribe_run(s, at_cap.data(), static_cast(at_cap.size()), nullptr); + CHECK(at_cap_st == TRANSCRIBE_OK || at_cap_st == TRANSCRIBE_ERR_OUTPUT_TRUNCATED || + at_cap_st == TRANSCRIBE_ERR_OUTPUT_REPETITION); + const std::string at_cap_text = full_text(s); + CHECK(!at_cap_text.empty()); + CHECK(transcribe_run(s, over_cap.data(), static_cast(over_cap.size()), nullptr) == + TRANSCRIBE_ERR_INPUT_TOO_LONG); + CHECK(full_text(s).empty()); + + // 3. Batch: the over-cap row fails alone. + const float * pcms[2] = { pcm_short.data(), pcm_long.data() }; + const int lens[2] = { static_cast(pcm_short.size()), static_cast(pcm_long.size()) }; + CHECK(transcribe_run_batch(s, pcms, lens, 2, nullptr) == TRANSCRIBE_OK); + CHECK_EQ_INT(transcribe_batch_n_results(s), 2); + CHECK(transcribe_batch_status(s, 0) == TRANSCRIBE_OK); + CHECK(transcribe_batch_status(s, 1) == TRANSCRIBE_ERR_INPUT_TOO_LONG); + const char * row0 = transcribe_batch_full_text(s, 0); + CHECK(row0 != nullptr && row0[0] != '\0'); + + // 4. Streaming far past the cap. + const std::string streamed = stream_all(s, pcm_long); + CHECK(transcribe_was_truncated(s) == true); + const bool same = exact_text ? (streamed == at_cap_text) : (words_of(streamed) == words_of(at_cap_text)); + if (!same) { + std::fprintf(stderr, + "FAIL %s: streamed transcript differs from one-shot over the first 81.92 s\n stream: %s\n" + " one-shot: %s\n", + label, streamed.c_str(), at_cap_text.c_str()); + ++g_failures; + } + + // A stream that ends inside the cap is not truncated by it. + const std::string short_stream = stream_all(s, pcm_short); + CHECK(transcribe_was_truncated(s) == false); + CHECK(!short_stream.empty()); + + transcribe_session_free(s); + transcribe_model_free(model); +} + +} // namespace + +int main() { + const char * model_path = std::getenv("TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF"); + if (model_path == nullptr || *model_path == '\0' || !file_exists(model_path)) { + std::fprintf(stderr, + "skipping: TRANSCRIBE_MOONSHINE_STREAMING_TINY_GGUF unset " + "or missing\n"); + return 77; + } + + std::vector pcm_short, pcm_long; + if (!load_sample("jfk.wav", pcm_short)) { + return 77; + } + if (!load_sample("dots-full.wav", pcm_long)) { // ~306 s, far past the cap + return 77; + } + if (pcm_long.size() <= static_cast(k_cap_samples) + 1) { + std::fprintf(stderr, "dots-full.wav is shorter than the cap; test needs > 81.92 s\n"); + return 1; + } + + check_backend(model_path, TRANSCRIBE_BACKEND_AUTO, "default backend", /*exact_text=*/true, pcm_short, pcm_long); + check_backend(model_path, TRANSCRIBE_BACKEND_CPU, "cpu", /*exact_text=*/false, pcm_short, pcm_long); + + if (g_failures > 0) { + std::fprintf(stderr, "moonshine_streaming_input_cap: %d failures\n", g_failures); + return EXIT_FAILURE; + } + std::fprintf(stdout, "moonshine_streaming_input_cap: ok\n"); + return EXIT_SUCCESS; +} From f14871466f3aadcdc2c064c73bf1c33cddc651b3 Mon Sep 17 00:00:00 2001 From: CJ Pais Date: Tue, 20 Oct 2026 03:32:26 -0700 Subject: [PATCH 3/3] tests: tighten the moonshine_streaming cap test - Compare the streamed transcript to one-shot exactly on the default backend (byte for byte proves the stream consumed the first 81.92 s and nothing else); on CPU assert only the invariants, since ggml's shape-dependent kernels can move a comma between a windowed and a single-pass run. Drops the word-level comparison helper, which was the weak part of the test. - Feed 90 s instead of the whole 306 s sample: enough to cross the cap. (The runtime is dominated by the at-cap encode's masks either way, ~37 s.) --- tests/moonshine_streaming_input_cap.cpp | 39 ++++++++----------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/tests/moonshine_streaming_input_cap.cpp b/tests/moonshine_streaming_input_cap.cpp index 7e00d69a..05640976 100644 --- a/tests/moonshine_streaming_input_cap.cpp +++ b/tests/moonshine_streaming_input_cap.cpp @@ -17,7 +17,7 @@ // 4. Streaming past the cap: every feed and finalize return OK, the stream // is flagged transcribe_was_truncated(), audio past the cap is ignored, // and the final transcript equals one-shot over the first 81.92 s -// (exactly on the default backend; word for word on CPU, see words_of). +// (byte for byte on the default backend; the invariants above on CPU). // // Gating: // - TRANSCRIBE_BUILD_REAL_MODEL_TESTS (CMake, default OFF) builds it. @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -81,28 +80,6 @@ bool load_sample(const std::string & name, std::vector & pcm) { return true; } -// Lower-cased words with punctuation stripped. On CPU, ggml's kernels pick -// shape-dependent paths, so a stream's encoder slices and the one-shot graph -// can round differently and the decoder may place a comma differently (seen: -// "college," vs "college"). The words are what proves the stream covered -// exactly the first 81.92 s. -std::vector words_of(const std::string & text) { - std::vector out; - std::string cur; - for (const char c : text) { - if (std::isalnum(static_cast(c)) != 0 || c == '\'') { - cur.push_back(static_cast(std::tolower(static_cast(c)))); - } else if (!cur.empty()) { - out.push_back(cur); - cur.clear(); - } - } - if (!cur.empty()) { - out.push_back(cur); - } - return out; -} - std::string full_text(transcribe_session * s) { const char * t = transcribe_full_text(s); return t == nullptr ? std::string() : std::string(t); @@ -137,7 +114,7 @@ std::string stream_all(transcribe_session * s, const std::vector & pcm) { void check_backend(const char * model_path, transcribe_backend_request backend, const char * label, - bool exact_text, + bool compare_text, const std::vector & pcm_short, const std::vector & pcm_long) { std::fprintf(stdout, "-- %s\n", label); @@ -195,8 +172,13 @@ void check_backend(const char * model_path, // 4. Streaming far past the cap. const std::string streamed = stream_all(s, pcm_long); CHECK(transcribe_was_truncated(s) == true); - const bool same = exact_text ? (streamed == at_cap_text) : (words_of(streamed) == words_of(at_cap_text)); - if (!same) { + // On CUDA/Metal the stream's windows and the one-shot graph go through the + // same kernels, so the two transcripts match byte for byte and that is the + // assertion worth making: it proves the stream consumed exactly the first + // 81.92 s and nothing else. On CPU, ggml picks shape-dependent kernel paths, + // so a windowed run and a single-pass run can round differently and the + // decoder may move a comma; there the invariants above are the assertion. + if (compare_text && streamed != at_cap_text) { std::fprintf(stderr, "FAIL %s: streamed transcript differs from one-shot over the first 81.92 s\n stream: %s\n" " one-shot: %s\n", @@ -235,6 +217,9 @@ int main() { std::fprintf(stderr, "dots-full.wav is shorter than the cap; test needs > 81.92 s\n"); return 1; } + // 90 s is enough to cross the 81.92 s cap and cheap enough that the CPU + // pass (a full greedy decode per feed) does not dominate the suite. + pcm_long.resize(static_cast(90 * 16000)); check_backend(model_path, TRANSCRIBE_BACKEND_AUTO, "default backend", /*exact_text=*/true, pcm_short, pcm_long); check_backend(model_path, TRANSCRIBE_BACKEND_CPU, "cpu", /*exact_text=*/false, pcm_short, pcm_long);