Repository navigation
voxcpm2: size KV step caches to the request, cut per-request zero-fills and per-token round trips - #921
voxcpm2: size KV step caches to the request, cut per-request zero-fills and per-token round trips#921sander110419 wants to merge 2 commits into
Conversation
|
@sander110419 The optimization does reduce memory: CUDA clone-stream peak drops 5,396 -> 4,160 MiB, but corrupted output (default path) in my tests.
before_main_cuda_correct.wav Restoring only per-step conditioning uploads makes all six CUDA outputs byte-identical to the working |
…lls, trim per-token round trips - VoxCPM2MiniCPMStepRuntime: build step graphs at the request's real need (prefill rows + max_tokens, 256-step buckets) instead of config.max_length; attention no longer runs over 8192 f32 rows per token regardless of content - TransformerKVCache: add reset_counters() and opt-in import_prefix_only; step runtimes zero caches once at build and import only the valid prefix (was: full-capacity zero fill + upload on reset and again after prefill) - batched text embedding (one get_rows per request instead of one synced single-token graph per prompt token) - step projection: split into emit/advance subset graphs; each call computes and downloads only the outputs it consumes (3 resp. 2 of 8) - CFM/DiT: upload mu/cond once per patch; precompute the sinusoidal step schedule once per request instead of per patch per step - AudioVAE: broadcast mul/div for snake alpha and sr-conditioning instead of materializing a full-size ggml_repeat copy per activation - prompt prefill: keep a small LRU of per-length graphs instead of rebuilding in place on every length change; a fresh rebuild perturbs ulp-level numerics enough to flip the autoregressive draw (observed as a shorter, faster, off-voice first request after any length change), so caching makes repeated same-length requests reproducible
7f9a4bb to
8cf697e
Compare
Yeah you are indeed correct, I'll fix it |
Hoisting mu/cond uploads to once per patch corrupted output on the default path in review testing (see PR discussion); restore the estimator's original per-call upload semantics. The request-invariant sinusoidal step schedule cache stays. Verified on-device: output remains byte-identical to the pre-hoist build on CUDA (Jetson, mem_saver=false), and the emit/advance split, KV sizing, prefix-only import, batched embedding and VAE broadcast changes are unaffected.
|
Thanks for the careful test — that was the decisive clue. I've pushed a commit that restores per-step mu/cond uploads in the CFM loop (the estimator keeps its original per-call upload semantics); the request-invariant sinusoidal schedule cache stays since it is pure CPU-side precompute. On my Jetson (CUDA, mem_saver=false) output remains byte-identical across the revert, so if your six-path matrix is easy to rerun, could you check this version on the default path? Everything else in the series is untouched: KV sizing, prefix-only import, batched embedding, emit/advance split, VAE broadcast ops. |
|
@sander110419 New regression... Cached prefill graphs corrupt A -> B -> A requests with
after_pr921_cuda_a_repeat_corrupted.wav |
|
I'll spend some more time on it, seems to indeed cause new issues |
Closes #917.
Summary
The voxcpm2 decode loop sized both KV caches at
config.max_length(8192, f32) and attended over the full cache every token, andTransformerKVCache.import_statezero-filled and uploaded the whole capacity twice per request (reset + prefill import). This PR sizes the step graphs to each request's real need, zeroes caches once at build, imports only the valid prefix, and trims several per-token host round trips in the generator. No behavior change: output is bit-identical before/after (see below).Root cause
ggml_contafter the transpose, f32->f16 conversion inside the fattn vec path, then a masked pass over all rows. On the VoxCPM2 config (28+8 layers, 2 KV heads x 128 ch) that is ~67 MB of traffic per layer-token, ~2.4 GB/token total.import_statewrotecache_steps * step_elemszeros per layer per request; ~1.2 GB moved per request for a few hundred valid rows.ggml_repeatper activation.Changes
VoxCPM2MiniCPMStepRuntime:ensure_capacity(prefill_rows + max_tokens)per request, 256-step buckets, rebuild only when a request needs more than the built capacity.max_lengthremains the hard cap.TransformerKVCache: newreset_counters()and opt-inimport_prefix_only(f32, non-ring; default path unchanged for other model families). voxcpm2 step runtimes zero caches once at build, then reset by counters and import only the valid prefix.get_rowsgraph per request (bucketed by 16) instead of a synced single-token compute per prompt token.ggml_repeatcopies.Update (second commit): added a small LRU of per-length prompt-prefill graphs in
VoxCPM2PromptPrefillRuntime. Rebuilding the prefill graph on every text-length change turned out to perturb ulp-level numerics enough to flip the autoregressive draw: on-device, the first request after a length change produced a shorter, faster, off-voice generation, while steady repeats were stable and reproducible. With the cache, mixed-length traffic stays on the warm draw: same input text now returns byte-identical audio regardless of what preceded it (verified across short/paragraph/medium alternation, per-sample diff). This also removes the per-length rebuild cost (~50-135 ms) from requests that revisit a recent length.Validation
Backend: CUDA, Jetson Xavier NX (Volta sm_72, CUDA 11.4, unified memory), voxcpm2 Q4_K GGUF (audio-cpp/audio.cpp-gguf), AudioVAE f16, streaming session, fixed seed 1234. Also compile-checked on x86_64 (CPU build), and
transformer_kv_ring_testpasses (ring + non-ring regression paths).Parity: output PCM bit-identical before/after (per-sample diff, maxdiff=0) on both a short text and a ~15 s paragraph, same fixed seed.
Long-lived session, same voice preset, multiple sequential requests including a long-form paragraph through the text chunker; step-cache capacity logged via the
voxcpm2.lm_step_cache_capacitytrace scalar (256 for the short requests, 768 for the paragraph) and reused across requests. Numbers are the median-looking steady state with an unrelated whisper.cpp server resident on the board:The lower allocation also removed the per-request spike that was getting the server OOM-killed on this 6.8 GB board with other services resident.
Known limits: CUDA graphs stay off on sm_72 (ggml disables them by arch; force-enabling crashes at
cudaGraphInstantiateon Tegra), so the per-token sync count is reduced but not eliminated. f16 KV storage was left out deliberately: after this change the remaining attention traffic is small and f32 keeps the numerics untouched.