Skip to content

Studio: decode the FLUX.2 / FLUX.3 VAE in row strips (36-43% less decode VRAM, same speed) - #17202

Merged
danielhanchen merged 3 commits into
mainfrom
flux3-vae-strips
Nov 9, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
flux3-vae-strips

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

What

FLUX.2 and FLUX.3 decode through AutoencoderKLFlux2. Studio's fused VAE path already fuses GroupNorm, SiLU and the bias/residual adds, but each residual block still materialises full-resolution intermediates. Each GroupNorm+SiLU output and each conv output exists at full size, so a 2048x2048 bf16 decode peaks at 5.6 GiB.

This PR adds diffusion_vae_strips.py, a row-strip decoder for that VAE, installed on top of the fused kernels:

  • Every GroupNorm computes its statistics over the whole feature map once (the existing Triton partials/combine kernels, now factored out as group_norm_stats). Normalisation, SiLU, the edge padding and the 3x3 conv then run on bounded row strips with a one-row halo. A normalised activation never exists at full size.
  • The decoder owns its residual stream:
    • Equal-channel blocks add conv2 into their input in place (one fp32 sum, one rounding, as before).
    • A block that halves the channels (512 -> 256, 256 -> 128) writes its output into the front half of its input's storage. Output rows [a, b) overwrite input rows [a/2, b/2), which only earlier strips read.
    • The blocks of one level share a single hidden buffer, which cuDNN writes strip by strip through cudnn_convolution.out, so no strip copy is made.
  • Upsample (nearest 2x written straight into the padded strip) and the final norm/SiLU/conv_out also run in strips.
  • Small decodes, where the largest pass fits in one strip, keep the fused path. There the strip path would only add launches and hold every buffer at full size.

The same idea is in the baseline. It computes global GroupNorm statistics, then does row-strip norm/SiLU/pad/conv with a decoder scratch buffer reused across levels.

Where it is on by default

card policy why
T4 (sm75), L4 (sm89) every decode measured within A/A noise or faster
other NVIDIA (A100 measured) only a decode the fused path would not fit in the free VRAM (the tile planner's own rule) A100 forced on is 4% slower at 2048^2, but beats more tiles
ROCm, MPS, XPU, CPU never the fused path itself is NVIDIA-only (runtime_ok)

UNSLOTH_FLUX_VAE_STRIPS=0 turns it off. =1 uses it for every decode on any NVIDIA card. The decode stays eager. When UNSLOTH_COMPILE_VAE forces the compiled decode, the fused path, and with it this one, is not installed. The strip path also falls back when torch.compiler.is_compiling(), so it adds no graph breaks or recompiles. Any non-OOM exception switches that VAE back to the fused path for good. An OOM propagates to the existing OOM retry, which decodes in smaller tiles.

Tile planner

diffusion_vae_tiling prices the strip decode with its own figure. The peak is 0.048 MiB per latent + four strip buffers (16-bit), which bounds every measured tile side from 48 to 256. Tiles that keep the fused path keep the fused 0.09 figure. The untiled-shortfall check in diffusion.py uses the same figure. FLUX.2 / FLUX.3 VAE, same free VRAM after the fp32 accumulator:

image free VRAM main: tile x count this PR: tile x count
2048^2 1.5 GiB 107x144 x6 144x144 x4
2048^2 2 GiB 107x144 x6 107x256 x3
2048^2 3 GiB 160x160 x4 160x256 x2
1536^2 3 GiB 112x192 x2 192x192 x1 (untiled)

Numbers

Real FLUX.2-klein VAE weights (the VAE FLUX.3 ships). The latent comes from encoding a real photo. This is decode-only time (CUDA events), untiled, 5 interleaved repeats after 2 warm-ups. BEFORE is current main (merge base) on its own VAE instance. The kill-switch arm is this PR with UNSLOTH_FLUX_VAE_STRIPS=0, which gives a second base sample (A/A).

Median ms [min, max]. Peak = max allocated during the decode, over what was allocated before it. Reserved = allocator peak.

card, dtype, image main kill switch (A/A) this PR peak alloc main -> PR peak reserved main -> PR
L4 bf16 1024^2 262.4 [260.1, 265.2] 260.9 [260.1, 263.4] 261.4 [251.1, 262.6] 1409 -> 898 MiB (-36%) 2438 -> 1702 MiB
L4 bf16 2048^2 1376.2 [1365.1, 1381.5] 1372.5 [1366.8, 1383.7] 1373.0 [1364.9, 1376.7] 5636 -> 3207 MiB (-43%) 7956 -> 4628 MiB
T4 fp16 1024^2 530.6 [528.8, 537.3] 529.3 [528.3, 536.8] 529.1 [526.1, 532.2] 1409 -> 1285 MiB (-9%) 2438 -> 1930 MiB
T4 fp16 2048^2 2865.5 [2716.3, 3029.4] 2988.6 [2817.7, 3099.2] 2849.8 [2707.5, 3009.9] 5636 -> 3593 MiB (-36%) 7958 -> 4758 MiB
T4 fp32 1024^2 (FLUX.3 on T4 runs fp32) 3990.7 [3958.2, 4082.0] 3973.8 [3956.2, 4074.7] 3979.4 [3950.0, 4088.5] 4868 -> 3077 MiB (-37%) 7364 -> 5014 MiB
T4 fp32 2048^2 21216 [21202, 21267] 21242 [21185, 21269] 20035 [20019, 20099] (-5.6%) 11273 -> 7694 MiB (-32%) 14508 -> 10410 MiB
A100 bf16 1024^2, forced (=1) 75.6 [70.7, 77.2] 75.4 [70.9, 77.1] 78.9 [74.5, 80.5] (+4%) 1409 -> 898 MiB 2438 -> 1702 MiB
A100 bf16 2048^2, forced (=1) 308.0 [304.4, 310.5] 308.3 [307.7, 309.6] 320.6 [320.1, 321.8] (+4%) 5636 -> 3207 MiB 7956 -> 4628 MiB

The A100 rows are why A100 does not take the strip decode by default.

Policy on a card outside the always-on list was checked on a T4 with that list emptied, fp16, 2048^2.

  • With plenty of free VRAM the decode keeps the fused path. No strip call was made, the output is bit-identical to main, and the peak is the same 5636 MiB.
  • With a ballast leaving 4.7 GiB free, main's untiled decode runs out of memory. This PR takes the strip path and decodes in 3593 MiB, with output bit-identical to the roomy decode.

uninstall restores the stock decoder forward.

Strip size is chosen per card: 2^24 elements on sm80+, 2^26 on Turing, whose implicit-GEMM convs lost 5-10% on shorter strips. That is why T4 fp16 saves less at 1024^2.

Per-latent decode peak (bf16, L4 and A100 identical), main vs this PR, by tile side: 32: 0.086 vs 0.086 (fused path kept), 48: 0.086 vs 0.078, 64: 0.086 vs 0.078, 96: 0.086 vs 0.060, 128: 0.086 vs 0.055, 160: 0.086 vs 0.052, 256: 0.086 vs 0.049 MiB.

Output error is at the rounding floor. Against a stock fp32 decode of the same latent, this PR's mean-abs error equals main's to 3 significant digits, and its max-abs and LPIPS are no worse.

card, dtype, image PR vs main max-abs / mean-abs LPIPS PR vs main vs stock fp32, main: max-abs / mean-abs / LPIPS vs stock fp32, PR
L4 bf16 1024^2 0.028 / 4.0e-4 1.8e-5 0.0347 / 1.2979e-3 / 7.57e-5 0.0298 / 1.2985e-3 / 7.39e-5
L4 bf16 2048^2 0.0078 / 2.3e-4 1.6e-5 0.0184 / 1.2421e-3 / 1.150e-4 0.0166 / 1.2422e-3 / 1.155e-4
A100 bf16 1024^2 / 2048^2 0 / 0 (bit-identical) 0 same as PR same as main
T4 fp16 1024^2 / 2048^2 0 / 0 (bit-identical) 0 3.2e-3 / 1.85e-4 / 1.0e-6 same
T4 fp32 1024^2 7.8e-6 / 1.7e-7 1.5e-12 9.4e-6 / 1.909e-7 1.18e-5 / 1.914e-7
T4 fp32 2048^2 5.1e-6 / 1.8e-7 2.5e-12 (fp32 reference OOM on 16 GB)

The kill switch is bit-identical to main on every arm.

Tests

  • studio/backend/tests/test_diffusion_vae_strips.py, CPU: the strip decoder with its Triton launches swapped for torch references matches the stock decoder in fp32 (max-abs 3e-6). This covers three channel layouts (equal blocks, 2x halving in place, a 4x shortcut transition), odd latent sizes (9x13, 5x17, 1x3), and one-row strips that exercise every halo, edge pad and the halving overwrite order. The tests also cover the halving block writing inside its input's storage, structural gating (padding, upsampler kind, accelerate hooks), install gating (CPU, env, other classes), the card / free-VRAM policy, and planner pricing.
  • On the GPU (L4, A100, T4), tiny random AutoencoderKLFlux2 against main and against a stock fp32 decode. That is 90 cases per card: 3 configs x fp32/fp16/bf16 x 5 latent sizes, at both 2^24 and 2^13 element strips. This PR's error against fp32 matches main's in every dtype. Kill switch: 0.0 on all.

…decode VRAM)

GroupNorm statistics stay global; normalisation, SiLU, edge padding and the 3x3 convs
run on bounded row strips with a one-row halo, so no normalised activation exists at
full size. Equal-channel residual blocks add into their input in place, channel-halving
blocks write their output into the front half of their input's storage, each level
shares one hidden buffer that cuDNN writes without a strip copy, and upsample and the
final norm/conv also run in strips. NVIDIA CUDA only, eager only, small tiles keep the
fused path. Kill switch UNSLOTH_FLUX_VAE_STRIPS=0. The tile planner prices the strip
decode with its own figure.
T4 (sm75) and L4 (sm89) take the strip decode for every FLUX.2 / FLUX.3 decode; other
cards (A100 measured 4% slower) take it only for a decode the fused path would not fit
in the free VRAM, the tile planner's own rule, where the alternative is more tiles.
UNSLOTH_FLUX_VAE_STRIPS=1 forces it on any NVIDIA card, 0 turns it off.
@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T03:47:52.004097Z dac888d 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

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: dac888dac5

ℹ️ 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".

danielhanchen added a commit that referenced this pull request Nov 9, 2026
…uff-unsloth wheel (#17211)

## Summary

The Backend CI step that installs the pinned `ruff-unsloth` wheel
retries 3 times with 5 s and 10 s gaps. Between 05:19 and 05:40 UTC
today, GitHub served intermittent 504s for the release asset, and every
attempt of an unlucky job landed inside one burst. That turned `Repo
tests (CPU, fsdp2 / gloo-cp / gloo-pp / gloo-tp / rest /
studio-install)` red on #17188, #17191, #17197, #17201 and #17202, while
sibling jobs in the same runs installed the wheel fine a minute later.

This raises the loop to 6 attempts with exponential backoff (10, 20, 40,
80, 160 s, about 5 minutes total). A healthy download still never
sleeps, and a lasting outage still fails the job with the same
`::error::` line, so the fixed-point formatter guard keeps failing
loudly in CI when the wheel is truly unavailable.

## Changes

- `.github/workflows/studio-backend-ci.yml`: 6 attempts, `sleep $((10 <<
(attempt - 1)))`.
- `tests/test_ruff_unsloth_install_retries.py`: runs the step's own
`run:` script under bash with `pip` and `sleep` faked on `PATH`, and
checks that
- 5 failed downloads in a row still install, after at least 300 s of
backoff,
- a download that never recovers still fails the job, in under 10
minutes,
  - a healthy download does not wait at all.

## Testing

- `pytest tests/test_ruff_unsloth_install_retries.py`: 3 passed.
- Against the old step from `main`, the burst test fails with
`ruff-unsloth install failed after 3 attempts`.
- The other tests that read `studio-backend-ci.yml` pass.
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.

1 participant