Skip to content

perf(irodori_tts): read the RF context K/V cache in place - #1168

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
KKTTSPJ:pr/irodori-rf-context-kv-in-place
Nov 2, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
KKTTSPJ:pr/irodori-rf-context-kv-in-place

Conversation

@KKTTSPJ

@KKTTSPJ KKTTSPJ commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

The RF sampler computes every DiT layer's context K/V (text, speaker and caption tokens) once per request in the context graph. Each step graph then copied all of it into its own IO buffer with ggml_backend_tensor_copy; the single-branch graph over a guided cache even read the whole cache back to the host to upload its first branch. While sampling, the K/V was therefore held twice on the device, once at the guided batch size.

With this PR, step graphs read the context graph's outputs where they are. They use leaf tensors placed at the same addresses with ggml_backend_tensor_alloc; a plain use or a view of those outputs would pull the context graph's own nodes into the step graph. A single-branch graph over a guided cache reads the leading slice (the conditional branch). Step graphs are dropped whenever the context graph is rebuilt, so they never point at freed memory.

The computation is unchanged; only the IO copy, its device memory and the host round trip are removed. src/models/irodori_tts/rf_dit.cpp only.

How large the copy was

Per-graph buffers for one request (CUDA, Q8_0, 40 steps). The context has max_text_len (256) + speaker + max_caption_len (512) tokens, and reference audio adds speaker tokens (218 for a 34.7 s reference) and a speaker CFG branch:

Batch Context K/V Copy in the CFG step graph's IO buffer
v4 Small, no reference 2 181 MiB 195 MiB
v4 Small, reference audio 3 347 MiB 362 MiB
v4 Large (#1163), no reference 2 578 MiB 623 MiB
v4 Large (#1163), reference audio 3 1,109 MiB 1,155 MiB

Validation

RTX 5060 Ti (CUDA 12.8) and CPU, Windows.

Byte-identical output to main (baseline binary: main at dcec826b; the commits up to this branch's base 611131c8 only add WebGPU paths):

  • v4 Small, v4.1 Small, 500M v3 and 600M v3 VoiceDesign: no reference, reference audio, and caption where supported (11 cases, CUDA).
  • v4 Small, no reference, under each sampler path: no CFG, CFG at every step (guidance_min_t=0), the default, guidance_mode=joint, guidance_mode=alternating.
  • One --request-sequence session per binary with 10 requests that change the cache shape and the guidance mode between requests (no reference, reference, reference + caption, alternating, joint, caption only, guidance_min_t=0, guidance_scale=0, repeated requests), for v4.1 Small and v4.1 Small MeanFlow: 10 of 10 identical for each, on CUDA and on CPU.
  • v4 Large (feat(irodori_tts): support Irodori-TTS v4 Large (T5Gemma 2 text encoder) #1163) with this change against feat(irodori_tts): support Irodori-TTS v4 Large (T5Gemma 2 text encoder) #1163 alone: 18 of 18 identical (Q8_0 / Q4_K / v4 Small, short / long / reference, with and without codec chunking).

Peak GPU memory (increase over idle, sampled every 50 ms; CUDA, 40 steps; "chunked" = codec_decode_chunk_steps=100, codec_encode_chunk_steps=100). v4 Large rows are #1163 (now merged) with and without this change:

Short (6 s) Long (20 s) Reference audio (34.7 s)
v4 Large Q8_0, chunked 5,470 → 5,117 MiB 5,546 → 5,157 MiB 6,622 → 5,506 MiB
v4 Large Q4_K, chunked 4,275 → 3,931 MiB 4,320 → 3,957 MiB 5,428 → 4,293 MiB
v4 Large Q8_0, default 5,474 → 5,221 MiB 6,462 → 6,455 MiB 7,822 → 7,855 MiB
v4 Small Q8_0, chunked 2,408 → 2,390 MiB 2,411 → 2,393 MiB 2,366 → 2,366 MiB

The saving shows wherever sampling sets the peak. Without codec chunking, a long output or a reference encode sets it instead, so those cells do not move. v4 Small's K/V is small enough that the codec sets its peak in every case here, but the copy (195–362 MiB above) is gone there as well. Run times did not change.

🤖 Generated with Claude Code

The context graph computes every DiT layer's text / speaker / caption K/V
once per request. Each step graph then copied all of it into its own IO
buffer (the single-branch graph through a host round trip), so the K/V
was held twice on the device while sampling, once at the guided batch.

Step graphs now read the context graph's outputs directly, through leaf
tensors placed at the same addresses (a single-branch graph over a guided
cache uses the leading slice). A plain use or view of those outputs would
pull the context graph's nodes into the step graph. Step graphs are
dropped whenever the context graph is rebuilt, so they never outlive it.

The computation is unchanged and the output is byte-identical. The IO
copy, its device memory and the host round trip are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@0xShug0

0xShug0 commented Nov 2, 2026

Copy link
Copy Markdown
Owner

@KKTTSPJ Great improvement. Added some RTF datapoints:

Workload Main warm RTF PR warm RTF Time reduction
CUDA RF, default 0.03581 0.03388 5.4%
CUDA RF, retained graphs 0.03579 0.03318 7.3%
CUDA MeanFlow 0.01281 0.01276 Within variation
Vulkan RF 0.08666 0.07927 8.5%
CPU MeanFlow 0.90908 0.90580 Within variation

@0xShug0
0xShug0 merged commit bee4f36 into 0xShug0:main Nov 2, 2026
7 checks passed
@KKTTSPJ
KKTTSPJ deleted the pr/irodori-rf-context-kv-in-place branch November 2, 2026 07:31
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