Skip to content

Studio LTX-2.5: release the server's cached GPU blocks before an encode, so later prompts stop encoding on the CPU - #16804

Merged
danielhanchen merged 6 commits into
mainfrom
n5lx-te-stream-encode
Nov 6, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
n5lx-te-stream-encode

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

What

Since #16750, the LTX-2.5 Gemma 4 encoder worker streams its layers through the GPU when the whole encoder does not fit. Its room check is the load budget minus what the server process holds (torch.cuda.memory_reserved). After the first render, the server still holds that render's cached allocator blocks, so the room drops below the streamed-encode need and every new prompt after the first one encodes on the CPU (165-231 s per prompt on Colab L4 / A100 hosts).

This PR empties the server's CUDA (or XPU) cache right before a non-resident encode request, so the worker's room check counts only memory the server is using. Cache hits and resident encoders are untouched. Placement only: the worker's own routing (whole, streamed, CPU) is main's.

The first version of this PR also added its own per-module streaming in the worker; #16750 landed an equivalent layer-streamed encode first, so the worker now matches main and only the server-side release remains.

Numbers (Colab, LTX-2.5 two-stage distilled 1280x704x121, GGUF Q4_K_M DiT, 2 new prompts per arm, fresh process per arm, same VM per card)

Where each prompt encoded (worker log encoded on ... streamed):

card main this PR
A100-40 1st prompt: GPU streamed; every later prompt: CPU (both arms) every prompt GPU streamed (both arms)
L4 24 GB by encode time: 1st prompt 69.8 s, later prompts 227-231 s (CPU speed) every prompt 39-74 s
card main this PR
A100-40 encode per new prompt 165.8-171.4 s 6.4-6.5 s
clip per new prompt 189.7 / 191.2 / 191.3 / 195.3 s 30.1 / 30.4 / 30.4 / 30.4 s
s/step 1.110-1.111 1.109-1.112
L4 encode per new prompt 227.4 / 231.4 s 73.6 / 39.3 s (64.3 / 66.2 s on a second L4 VM, streamed: True)
clip per new prompt 319.5 / 323.3 s 166.4 / 132.2 s (157.8 / 159.6 s)
s/step 4.68-4.70 4.75

Frames: A/A bit-identical on both sides (A100, 16 of 16 frames, both prompts). main vs this PR: LPIPS 0.185 / 0.019, because main's later prompts used CPU bf16 math and this PR's use the GPU path that main already takes for the first prompt of a process.

Tests

tests/test_video_ltx25.py: a non-resident cache miss empties the cache before the server reservation is read and sent (fails on main); a resident encoder and a cache hit release nothing; no release on CPU / MPS / a host without a card. test_video_ltx25.py + test_video_ltx25_te_int8.py: 155 passed; the 7 failures are the same test IDs on main in this environment (two-stage i2v fakes, real-header converters).

…whole encoder does not fit

A non-resident encoder that cannot be onloaded whole (bf16 Gemma 4 on a 24 GB card next to a streamed DiT) ran
every encode on the CPU, minutes per new prompt. When one module at a time fits, each module's own weights now go to
the card just before its forward and back to the kept host copy after it; same weights, same math on the GPU.
UNSLOTH_LTX25_TE_STREAM=0 keeps the CPU encode.
…t encode; smaller streamed-encode margin

The worker judged its room against the server's reserved memory, which still held the previous render's cached
activations, so even a per-module streamed encode fell back to the CPU. The server now empties its cache before a
non-resident encode request (encodes run before the denoise), the streamed path asks only for two modules plus the
stacked hidden states, and a CPU fallback logs the room it saw.
@danielhanchen danielhanchen self-assigned this Nov 5, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T00:57:45.381827Z fa382f5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 586fa77b97

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…he server cache release only

#16750 added a layer-streamed encode to video_ltx25_te_worker.py, which supersedes this branch's per-module streaming, so the worker now matches main. What remains is the server side: empty the CUDA cache before a non-resident encode request, so the worker's room check (budget minus the server's reserved memory) does not count blocks the previous render only cached. Tests move to test_video_ltx25.py next to the client tests.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: fa382f5ba5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

[skip ci]
@danielhanchen danielhanchen changed the title Studio LTX-2.5: stream the Gemma 4 encoder through the GPU when it does not fit, instead of a CPU encode Studio LTX-2.5: release the server's cached GPU blocks before an encode, so later prompts stop encoding on the CPU Nov 6, 2026
danielhanchen added a commit that referenced this pull request Nov 6, 2026
… fixed 1 KiB/s (#16871)

## Summary

The Windows `GGUF inference smoke (API, tools, vision)` job runs
`tests/studio/test_mirror_fallback.ps1` first, and one of its checks
fails now and then on unrelated PRs. In the last day it hit the staging
runs for #16825 (int8 diffusion on RDNA3) and #16804 (LTX-2.5 encode),
and a rerun of the #16825 job passed:

```
  PASS  probe: gives up at its deadline, cancelling what has not answered
  FAIL  probe: a ranged 1 KiB answer is 206 with its speed
##[error]Process completed with exit code 1.
```

The check wanted the `/ok` probe's speed above 1024 bytes/s.
`Wait-MirrorProbe` measures speed as bytes read over the probe's own
clock, so for a 1 KiB answer that floor really means "answered in under
a second". The probe gives that batch of 6 URLs 1.5 s, and on a busy
Windows runner the local server can take longer than a second to answer.
The probe is then working correctly and the check still fails.

Reproduced locally with `pwsh`: delaying the test server's `/ok` answer
by 1.1 s, still inside the 1.5 s deadline, fails this check on main.

## Change

`tests/studio/test_mirror_fallback.ps1`, this one check. It now asks
for:

- status 206, as before;
- the whole 1 KiB in the probe's buffer (`Position -eq 1024`), which the
old check did not look at;
- a speed of at least 1024 bytes over the batch's total time from the
test's own stopwatch. The probe's clock starts after that stopwatch, so
a correct speed can never be below this bound. A zero, missing or
miscomputed speed still fails.

No installer code changes.

## Testing

`pwsh -File tests/studio/test_mirror_fallback.ps1`, Linux pwsh 7:

| case | main | this branch |
|---|---|---|
| as is | pass | pass, 0 FAIL lines in the whole file |
| `/ok` answers after 1.1 s (slow runner) | FAIL | pass |
| `/ok` answers 200 instead of 206 | FAIL | FAIL |
| `/ok` sends 512 of the 1024 bytes | pass (not caught) | FAIL |
@danielhanchen
danielhanchen merged commit 2821cbb into main Nov 6, 2026
@danielhanchen
danielhanchen deleted the n5lx-te-stream-encode branch November 6, 2026 05:07
Penguinjanator pushed a commit to Penguinjanator/unsloth that referenced this pull request Nov 6, 2026
…nslothai#16845)

## What

LTX-2.5 was outside the per-request DiT residency fit
(`video_stream_residency`) that Wan, HunyuanVideo-1.5 and LTX-2 (unslothai#16787)
use on the `group` / `streaming` tiers. On cards that stream the LTX-2.5
DiT (L4, 16-24 GiB budgets) every forward copied all 48 blocks from the
host while part of the card stayed idle: 11.55 GiB of H2D per forward
for GGUF Q4_K_M and 23.6 GiB for the INT8 DiT on an L4.

1. `ltx-2.5` joins `RESIDENT_FAMILIES` (exact name), so the leading
blocks stay resident per request, sized from the measured request peak
with the existing margin, and only the rest stream through the existing
prefetch.
2. LTX-2.5's Gemma 4 encoder runs in a worker process on the same card.
Before an encode that is not a cache hit,
`video_ltx25.lend_resident_dit_for_encode` streams resident blocks again
when that makes the encoder fit: the whole encoder if lending gets it
there, else the worker's own layer-streamed need (`stream_need_mib`,
reported in its ready message; unslothai#16750's streamed encode), and pins them
back right after the encode. The room check is the worker's
`_encode_room_mib`, so both processes compute it the same way.
3. A cancelled encode that borrowed blocks stops the worker before they
are pinned back (it may still be computing on the lent memory); the next
encode restarts it.

Placement only: the same weights and kernels run. Kill switch: the
existing `UNSLOTH_VIDEO_DIT_RESIDENT=0` (also disables the lending).

## Numbers (Colab L4 24 GB, LTX-2.5 two-stage distilled 1280x704x121,
GGUF Q4_K_M, 2 new prompts per arm, fresh process per arm, main
73984c4)

| arm | s/step | denoise s per new prompt | encode s per new prompt |
|---|---|---|---|
| main | 4.68-4.70 | 85.2 / 85.2 | 227.4 / 231.4 (CPU) |
| this PR, arm 1 | **3.82-3.84** | **76.2 / 76.3** | 81.3 / 82.1 (GPU) |
| this PR, arm 2 | **3.82-3.87** | **76.3 / 76.0** | 67.4 / 37.8 (GPU) |
| second VM: main + unslothai#16804 | 4.85-4.86 | 86.5 / 86.5 | 64.3 / 66.2 (GPU)
|
| second VM: this PR | **3.89-3.92** | **76.7 / 76.6** | 43.7 / 8.3
(GPU) |

s/step is 18-20% lower (A/A within 1.3%); the fit kept 4006 MiB (15
groups) resident. Frames against main + unslothai#16804 on the same VM:
bit-identical (16 of 16 frames, both prompts). Against main alone the
frames differ only because main encodes the later prompts on the CPU
(fixed separately by unslothai#16804; this branch's lending path also releases
the server cache when blocks are resident).

Earlier A/B on main e2daf16 (with the then-open encoder changes):
Q4_K_M 4.67 -> 3.86 s/step, INT8 2.13 -> 1.98, H2D per forward 11.55 ->
9.57 GiB (Q4) and 23.61 -> 21.88 GiB (INT8), frames bit-identical with
and without the change and with the kill switch, no-op on an A100-40
(nothing streams there).

The lending path did not engage on these cards (the encoder already fit
its streamed need once the server's cache was released); it is covered
by CPU tests.

## Tests

`tests/test_video_stream_residency.py` (ltx-2.5 applies on group /
streaming, not on model offload or a custom name; fails on main) and new
tests in `tests/test_video_ltx25.py`: blocks are lent only when the
encoder then fits whole or reaches the worker's streamed need, never for
an encoder that cannot stream, a resident encoder or a non-CUDA device;
the worker reports its streamed need only when it can stream; the encode
wrapper lends before a cache miss, restores after it (also on failure)
and skips cache hits; a cancelled encode stops a busy worker before the
restore. `test_video_ltx25.py` + `test_video_stream_residency.py`: 164
passed, 7 failed, the same 7 test IDs that fail on main in this
environment (two-stage i2v fakes, real-header converters).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant