Skip to content

feat(whistle_asr): run the decoder as a GGML step graph - #843

Merged
0xShug0 merged 13 commits into
0xShug0:mainfrom
BumpyClock:feat/whistle-asr-ggml-decoder
Oct 8, 2026
Merged

0xShug0 merged 13 commits into
0xShug0:mainfrom
BumpyClock:feat/whistle-asr-ggml-decoder

Conversation

@BumpyClock

@BumpyClock BumpyClock commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #824, as requested in its review. The autoregressive decoder now runs as one persistent GGML step graph on TransformerKVCache, and the host decoder code is gone.

  • The step graph is built from LinearModule, RMSNormModule, GemmaRMSNormModule, RoPEModule, SiluModule, SigmoidModule, SoftmaxModule and FastKVSetRowsModule, plus the Whistle-specific graph pieces the encoder already used. Those pieces moved from encoder.cpp into graph.cpp so both graphs share them: the weight loader, the mHC lane mixing with Sinkhorn normalization, the Kronecker-factored Hadamard MLP, and the per-head norm and RoPE. Each graph keeps its own attention code because the shared attention modules assume one head size and Whistle uses 48-wide queries/keys with 64-wide values.
  • runtime.cpp no longer has its own linear, RMS-norm, Hadamard, Sinkhorn, mHC and RoPE code, host copies of the decoder weights, or OpenMP handling. Only the engram table lookup (integer hashing over the token history) and the greedy argmax stay on the host; the engram projections and gates run in the graph.
  • Self-attention keys and values live in a TransformerKVCache written with FastKVSetRowsModule. A per-layer history of raw query/key/value projections feeds the three-tap mixing. Cross-attention keys and values come from the encoder graph and are copied into the decoder's tensors once per request. The graph is built once and reused across requests.
  • The decoder step graph runs on a CPU context of its own, with one thread, whatever backend the session uses. The encoder stays on the session backend (CPU or Metal). See "Where the decoder runs" below for the measurements behind that choice.
  • whistle_assets_test --dump and --reference now also write and compare per-step decoder logits, input tokens and the transcript, so later changes can be checked against this implementation.
  • No framework, GGML, converter or GGUF changes. No models, audio or logs are committed.

Where the decoder runs

A one-token step is 4738 small graph nodes, so its cost is per-node overhead rather than arithmetic. Measured on an Apple M3 Ultra with the 14.07 s sample, the 42 decoder steps took 134-140 ms on one CPU thread, 550-560 ms on Metal, 1014-1111 ms on a CPU context with 2 threads, and 1787-2098 ms on the CPU backend with 4 threads. Extra threads only add per-node synchronization. The shipped build therefore runs the step graph on a single-thread CPU context. The "decoder on session backend" column below is this branch with one local change, the decoder built on the session context; in a CPU session with one thread that is the same configuration as the shipped build.

Sample, CLI wall (metrics.wall_ms, 3 runs) Before (#824 host decoder) Decoder on session backend (not shipped) This PR
CPU 1 thread 494-528 ms same as this PR 465-472 ms
CPU 4 threads 238-242 ms 1933-2242 ms 282-283 ms
Metal 157-163 ms 618-629 ms 204-226 ms

One-thread CPU requests are slightly faster than before. Four-thread CPU and Metal requests are slower, because the old host decoder spread its matrix-vector products over OpenMP threads and the step graph cannot. Peak resident memory drops from 758 MiB (CPU) and 765 MiB (Metal) to 729 MiB and 720 MiB. If you would rather have the decoder on the session backend, runtime.cpp only needs to pass the session context to the decoder instead of its own, and the middle column shows the cost.

Time split, from the CLI --log timing output, three runs each. The encoder figure includes the mel frontend and the per-request encoder graph build. The decoder figure covers all 42 steps. The "before" columns come from a local build of main with the same two timing scalars added, measured on the same day.

Configuration Encoder before Encoder this PR Decoder before (host, OpenMP) Decoder this PR (graph, 1 thread)
CPU 1 thread 333-339 ms 330-336 ms 164-170 ms 134-140 ms
CPU 4 threads 140-158 ms 146-148 ms 103-106 ms 134-136 ms
Metal 59-70 ms 65-87 ms 97-102 ms 136-141 ms

The encoder path is unchanged within run-to-run variation. The whole difference is the decoder.

Numerical check

Before the port, whistle_assets_test --dump recorded the host decoder's per-step logits, tokens and transcript for the sample and the seven 20-26 s synthetic clips from the previous validation. At the final commit every comparison passes with identical tokens and transcripts on CPU 1 thread, CPU 4 threads, and Metal. Encoder tensors are bit-identical on CPU. Largest per-step decoder logits difference as a fraction of the reference's largest value:

Clip Steps CPU Metal (encoder on Metal)
sample 42 1.86e-6 1.53e-4
en 89 1.70e-6 1.66e-4
de 172 1.71e-6 1.43e-4
fr 156 1.75e-6 3.98e-4
es 177 1.55e-6 1.03e-4
it 162 1.75e-6 6.05e-4
nl 107 1.75e-6 5.43e-4
pl 212 1.60e-6 2.06e-4

The CPU difference of about 2e-6 comes from a different floating-point summation order. The Metal difference comes from the encoder's half-precision matmul staging, as in #824. The decoder itself runs on the CPU.

Other checks that passed:

  • ctest -R whistle_, 4 of 4. The integration tests also cover decoder reuse across requests, quiet and constant-RMS speech, silence, the 30-second limit and non-finite input.
  • The full sample check on Metal.
  • A two-request session on CPU and on Metal, with identical transcripts.
  • Two long clips on Metal: the 20.43 s English clip in 357 ms and the 22.94 s Polish clip in 761 ms.

Exact commands

Build (existing build/whistle configuration from #824: Ninja, Release, GGML_METAL=ON, OpenMP, AUDIOCPP_MODEL_SET=custom, AUDIOCPP_MODELS=whistle_asr, ENGINE_BUILD_TESTS=ON, AUDIOCPP_WHISTLE_TEST_MODEL set to the converted GGUF):

cmake --build build/whistle --target whistle_assets_test audiocpp_cli -j 8

Model: build/whistle/pr-prep/model/whistle-f32.gguf, converted as in #824 (same pinned revision and hashes).

# record the host decoder before the port (run on main + the test commit), then compare after
build/whistle/bin/whistle_assets_test build/whistle/pr-prep/model/whistle-f32.gguf assets/resources/sample_16k.wav --backend cpu --threads 1 --dump build/whistle/pr-prep/decoder-reference/sample
build/whistle/bin/whistle_assets_test build/whistle/pr-prep/model/whistle-f32.gguf assets/resources/sample_16k.wav --backend cpu --threads 1 --reference build/whistle/pr-prep/decoder-reference/sample --tolerance 1e-4
build/whistle/bin/whistle_assets_test build/whistle/pr-prep/model/whistle-f32.gguf assets/resources/sample_16k.wav --backend cpu --threads 4 --reference build/whistle/pr-prep/decoder-reference/sample --tolerance 1e-4
build/whistle/bin/whistle_assets_test build/whistle/pr-prep/model/whistle-f32.gguf assets/resources/sample_16k.wav --backend metal --threads 4 --reference build/whistle/pr-prep/decoder-reference/sample --tolerance 5e-3
ctest --test-dir build/whistle -R whistle_ --output-on-failure
build/whistle/bin/audiocpp_cli --task asr --family whistle_asr --backend cpu --threads 1 --model build/whistle/pr-prep/model/whistle-f32.gguf --audio assets/resources/sample_16k.wav --language en --metrics
build/whistle/bin/audiocpp_cli --task asr --family whistle_asr --backend cpu --threads 4 --model build/whistle/pr-prep/model/whistle-f32.gguf --audio assets/resources/sample_16k.wav --language en --metrics
build/whistle/bin/audiocpp_cli --task asr --family whistle_asr --backend metal --threads 4 --model build/whistle/pr-prep/model/whistle-f32.gguf --audio assets/resources/sample_16k.wav --language en --metrics
build/whistle/bin/audiocpp_cli --task asr --family whistle_asr --backend metal --threads 4 --model build/whistle/pr-prep/model/whistle-f32.gguf --request-sequence build/whistle/pr-prep/batch2.json --metrics

The same --reference runs were repeated for the seven long clips. Full details, including the memory and session numbers, are in the "GGML decoder step graph" section of tests/whistle_asr/VALIDATION.md.

Sample transcript on every configuration:

Some call me Nature. Others call me Mother Nature. I've been here for over 4.5 billion years, twenty two thousand five hundred times longer than you.

Known limitations and possible follow-ups

  • Metal and 4-thread CPU requests are slower than with the host decoder (table above); 1-thread CPU is slightly faster.
  • Cross-attention keys and values are read back from the encoder graph and uploaded to the decoder once per request; they could stay on the backend.
  • The engram tables stay on the host. Moving the hashed lookup into the graph would need a gather over token-history hashes.
  • The encoder graph is still built per request, as before.
  • An intermediate Sinkhorn form with fewer graph nodes was replaced before this PR because a whole-column underflow could produce NaN; see commit c2af1f5.
  • CUDA, Vulkan and HIP are still rejected at session creation; not validated here.
  • Same input set as feat(asr): add experimental Whistle community model #824 (sample plus seven synthetic clips), not a broad corpus.

Documentation

Add an optional decode observer to WhistleRuntime::transcribe and extend
whistle_assets_test --dump/--reference to write and check decoder_logits_<n>.f32,
decoder_tokens.i32 and decoder_transcript.txt. This records the host decoder as
the oracle for the upcoming GGML decoder port.
…coder

Move the weight loader and the Whistle-specific graph pieces (linear,
norms, RoPE, Sinkhorn, Kronecker, feature permutation, Hadamard MLP,
four-lane mHC mixing, lane mean) out of encoder.cpp into graph.cpp so
the decoder step graph can reuse them. Structural constants are now
sized per graph. Encoder outputs are bit-identical on CPU.
…rKVCache

The autoregressive decoder now runs as one persistent GGML step graph on
the session backend (CPU and Metal), built from the shared Whistle graph
helpers and framework modules. The host decoder math, the host copies of
the decoder weights and the OpenMP thread handling are removed from
runtime.cpp.

- Self-attention keys/values live in a TransformerKVCache written with
  FastKVSetRowsModule; a per-layer history of raw query/key/value
  projections feeds the three-tap mixing with the previous two steps.
- Cross-attention reads the encoder's keys/values, copied once per
  request into backend tensors laid out for the step graph, with a frame
  mask so the graph is built once for up to 375 frames.
- Engram rows are still gathered on the host from the token-history
  hashes; the projections, taps and gates run in the graph.
- The Sinkhorn normalization now carries column potentials (row softmax
  of logits plus potentials, then a column log-sum update), the same
  row-then-column log normalization with about a third of the graph
  nodes. Encoder outputs move from bit-identical to within 3.5e-6 of the
  reference dumps on CPU.
- Gemma-style norms store 1 + scale at load time (bit-identical).
- On CPU the step plan runs on one thread; per-op synchronization of
  one-token steps costs more than it saves. --threads still applies to
  the encoder graph.
- The tied embedding streams straight from the model file and the
  engram tables keep the raw f32 bytes, which lowers peak memory.

Decoder logits match the reference dumps within 2.2e-6 on CPU and
3.8e-4 on Metal for the sample and seven long clips, with identical
tokens and transcripts. Metal decoding is slower than the host decoder
was, because each step is about 1600 small kernel dispatches.
…context

A one-token decoder step is about 2600 small graph nodes, so its cost is
per-node overhead rather than arithmetic. Measured on an Apple M3 Ultra
with the 14 s sample (42 steps): about 6.5 ms per step on Metal, 3.3 ms
on one CPU thread, and 2 or 4 CPU threads roughly tripled or quadrupled
the step time through per-node synchronization.

The runtime now gives the decoder its own CPU execution context with one
thread, whatever backend the session uses, and the decoder no longer
toggles the backend thread count around its graph plan. The encoder keeps
the session backend and thread count.

Sample CLI wall time, three runs: CPU 1 thread 463-483 ms, CPU 4 threads
258-261 ms, Metal 198-200 ms (decoder on Metal: 336-361 ms). Per-step
logits match the reference dumps within 2.1e-6 on CPU and 3.8e-4 on
Metal for the sample and seven long clips, with identical transcripts.
…n cleanup

The column-potential form of the Sinkhorn normalization converted each
row to probabilities before the column step, so a column whose entries all
underflow produced log(0) and NaN. Go back to the row-then-column
log normalization with the transpose constant, which cannot underflow as a
whole pass and makes encoder outputs bit-identical to the reference dumps
again. The decoder step time is unchanged (about 139 ms for 42 steps on one
CPU thread) and the encoder-only time at 4 threads is the same within noise.

Also hold the decoder state buffer in a unique_ptr so a constructor failure
after allocation frees it, and make whistle_assets_test reject --dump and
--reference on the same directory or without a WAV file.
… again

The shared loader folded 1 + scale into the stored weights and used
RMSNormModule. Keep the checkpoint scales as they are and let
GemmaRMSNormModule add the one, as the merged encoder did. Encoder outputs
stay bit-identical and the decoder step time is unchanged.
Add the decoder section to the validation report with the reference dump
commands, per-clip logits differences, timing before and after the port,
process memory, the review fixes and the open follow-ups.
…e timing difference

Measured against a local build of main with the same timing scalars: the
encoder path is unchanged and the whole difference is the decoder, which
ran on OpenMP threads before and on one thread in the step graph now.
Replace the numbers from an earlier, smaller graph with measurements of the
graph in this branch on Metal, on 2 CPU threads and on the CPU backend with
4 threads. Name the CLI --log switch for the timing output and say which
graph pieces are shared.
@0xShug0

0xShug0 commented Oct 8, 2026

Copy link
Copy Markdown
Owner

@BumpyClock Thanks for the detailed information. The common pattern for other models is to share the same graph across GPU backends, while allowing CPU-specific graph optimizations. This model is very interesting: its unique architecture exposes some limitations in GGML. I'll merge this now and look into performance optimization later.

@0xShug0
0xShug0 merged commit adedaf2 into 0xShug0:main Oct 8, 2026
6 checks passed
@0xShug0

0xShug0 commented Oct 9, 2026

Copy link
Copy Markdown
Owner

@BumpyClock

I did some agressive GGML changes and still can't beat host code. Then I suddenly remembered that Audio8-TTS uses the similar idea in #444: moving its small, dispatch-bound AR graphs from Metal to CPU. I think host code could be the right solution; the question is when to use it. I'll test CUDA, our primary backend, to see whether host code benefits other backends or whether the graph still wins on CUDA.

Decoder Time
Original CPU host, 4 threads 74.61 ms
New CPU graph, 1 thread ~99 ms
New CPU graph, 4 threads ~455 ms
New Metal graph ~129 ms

@0xShug0

0xShug0 commented Oct 9, 2026

Copy link
Copy Markdown
Owner

@BumpyClock Tested on CUDA and Vulkan. The CUDA graph requires a minor module config correction to run.

Default 4 threads, avg 20 tests:

Backend Host decoder: CPU/OpenMP CPU GGML decoder graph: 1 thread GPU GGML decoder graph
CUDA 137.7 ms 185.5 ms 170.6 ms
Vulkan 135.1 ms 178.8 ms 222.5 ms

Overall, it looks like the host decoder is the best option across backends. Would you like to open a follow-up PR to restore it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants