Repository navigation
feat(whistle_asr): run the decoder as a GGML step graph - #843
Conversation
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.
|
@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. |
|
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.
|
|
@BumpyClock Tested on CUDA and Vulkan. The CUDA graph requires a minor module config correction to run. Default 4 threads, avg 20 tests:
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? |
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.LinearModule,RMSNormModule,GemmaRMSNormModule,RoPEModule,SiluModule,SigmoidModule,SoftmaxModuleandFastKVSetRowsModule, plus the Whistle-specific graph pieces the encoder already used. Those pieces moved fromencoder.cppintograph.cppso 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.cppno 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.TransformerKVCachewritten withFastKVSetRowsModule. 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.whistle_assets_test --dumpand--referencenow also write and compare per-step decoder logits, input tokens and the transcript, so later changes can be checked against this implementation.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.
metrics.wall_ms, 3 runs)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.cpponly 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
--logtiming 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 ofmainwith the same two timing scalars added, measured on the same day.The encoder path is unchanged within run-to-run variation. The whole difference is the decoder.
Numerical check
Before the port,
whistle_assets_test --dumprecorded 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: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.Exact commands
Build (existing
build/whistleconfiguration from #824: Ninja, Release,GGML_METAL=ON, OpenMP,AUDIOCPP_MODEL_SET=custom,AUDIOCPP_MODELS=whistle_asr,ENGINE_BUILD_TESTS=ON,AUDIOCPP_WHISTLE_TEST_MODELset to the converted GGUF):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 --metricsThe same
--referenceruns were repeated for the seven long clips. Full details, including the memory and session numbers, are in the "GGML decoder step graph" section oftests/whistle_asr/VALIDATION.md.Sample transcript on every configuration:
Known limitations and possible follow-ups
Documentation