Skip to content

Studio: pin Krea-2's reduction-config filter so fresh Studio homes render one seed the same way - #19077

Merged
danielhanchen merged 2 commits into
mainfrom
studio-int8-first-load-consistency
Nov 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-int8-first-load-consistency

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Krea-2 now pins Inductor's reduction-config filter (filter_reduction_configs = True) and keeps cudnn.benchmark off for its renders (cudnn_benchmark = False), as FLUX.2 Klein and FLUX.3 Klein already do. In the default speed mode one seed now renders one image in every fresh Studio home (measured on A100 40 GB and L4). Max speed mode is outside that claim (see below).

What was wrong

A Krea-2 load compiles its denoiser blocks with torch.compile. On a cold compile cache, Inductor benchmarks several configs for a few reduction kernels in the loading process and keeps the fastest. Two of those configs are nearly tied on an A100 and sum in different orders (for one kernel, R0_BLOCK 2048 vs 8192). The winner is written into the compile cache under the Studio home, so:

  • two fresh Studio homes on the same machine can render the same seed differently, and
  • every later load of a home repeats whatever its first load picked.

In the #18813 accuracy recheck (A100 40 GB, Krea-2-Turbo INT8, 1024 px, 8 steps, 12 prompts) this showed up as the first INT8 load of a fresh machine rendering differently from later loads of the same code: LPIPS 0.146 mean, 0.57 max, with identical status and identical weights. It was not tied to the first load. It was tied to the compile cache each home started with.

Evidence (Colab A100 40 GB, Krea-2-Turbo INT8, 1024 px, 8 steps, guidance 0, seeds 1000+i, every load a fresh process)

Weights: every INT8 load read the same hosted Krea-2-Turbo-INT8.safetensors and Krea-2-Turbo-text_encoder-INT8-ConvRot.safetensors; transformer, text encoder and VAE weight digests were identical in every cell, so the split is in compute, not in weights.

Same code, fresh homes, different picks (tree 9840f8f, before the text-row trim, which has the same reduction shapes as main with UNSLOTH_DIFFUSION_KREA2_TEXT_TRIM=0):

Run Fresh homes Image vs fp32 reference (4 prompts, LPIPS)
recheck VM base_auto (r0, r1) 0.2526
recheck VM base_int8 (r0, r1), main_*_notrim 0.3499
VM 2 4 fresh homes (auto and int8) 0.2526
VM 3 1 fresh home 0.3499

Forcing Inductor to keep the slowest config of every autotuned reduction (4 kernels) moves an image from one variant to the other, and the filter removes the dependence:

Cell (VM 3) Reductions forced to slowest vs fp32 Same as default
default 0 0.3499
slowest pick 4 0.2526 no (LPIPS 0.231)
filter on 0 (nothing left to benchmark) 0.3499 yes, bit-identical
filter on + slowest pick 0 0.3499 yes, bit-identical

With this branch (current main plus the one-line family change, same A100 run as its main arm):

Cell Text-row trim Reductions benchmarked vs fp32 Matches
main default on yes 0.3433
this branch, 2 fresh homes on none 0.3433 main default, bit-identical
this branch, slowest pick on none (0 forced) 0.3433 main default, bit-identical
main default off yes 0.3499
main, slowest pick off 4 forced 0.3499 main default
this branch, 2 fresh homes + slowest pick off none (0 forced) 0.3499 main default, bit-identical

So the change keeps today's main images unchanged on this card and removes the per-home benchmark that could move them.

On current main itself the split did not reproduce in what was tried: 9 fresh homes with the trim on (3 VMs) and 2 with it off rendered one image per setting, and the slowest-pick override did not move pixels there. The race is still live on main (4 reduction kernels are benchmarked in every fresh home and their picks differ between homes), but the picks seen so far landed on configs that sum the same way for these shapes. Other resolutions, prompt lengths, cards and torch versions change both the shapes and the timings.

Which variant is right: neither. Both are fp32 reductions over the same values in a different order; an 8-step distilled sampler amplifies the last-bit difference into a visibly different image. Against an fp32 reference (every DiT linear in fp32 on the published weights, TF32 off) the 0.2526 variant happened to be closer on these prompts (12 prompts: 0.170 vs 0.219), but that is the luck of one reduction order on one prompt set, not a precision gain. The fix makes the choice deterministic; it does not pick a more accurate kernel.

Speed: per-image steady time on the A100 is unchanged within run-to-run spread (trim on: main 3.92 s, this branch 3.92 to 3.94 s; trim off: main 4.34 s, this branch 4.34 s; 3 timed renders per cell, not an exclusive benchmark).

cuDNN conv picks and max speed mode (Colab L4 and A100 40 GB)

Same setup as above (INT8, 1024 px, 8 steps, guidance 0, 3 prompts, seeds 1000+i, torch 2.14.1), every cell a fresh process with its own fresh Studio home, Inductor cache and Triton cache, cells interleaved. Each cell rendered 6 images (3 prompts twice); "steady" is the median of renders 2 to 6. VAE decode is timed and hashed inside vae.decode during the renders, plus a standalone decode of one fixed seeded latent through the same vae.decode under the render's cuDNN scope.

cudnn.benchmark on (as before this change) vs off (this branch), default speed mode, 3 fresh homes each:

Card Arm Images across homes In-render VAE decode across homes Fixed-latent VAE decode across homes First render Steady render (per home) Steady VAE decode First VAE decode
L4 on 1 variant 1 variant 3 variants (3 of 3 homes differ) 42.3 to 49.0 s 13.44, 13.35, 13.26 s 142 ms 5.4 to 7.4 s
L4 off 1 variant 1 variant 1 variant 44.5 to 44.7 s 13.25, 13.29, 13.32 s 143 ms 3.9 s
A100 40 GB on 1 variant 1 variant 1 variant 30.9 to 34.2 s 3.400, 3.407, 3.396 s 34.7 ms 4.5 to 6.2 s
A100 40 GB off 1 variant 1 variant 1 variant 30.0 to 30.1 s 3.408, 3.407, 3.416 s 36.3 ms 3.6 s

So on L4 the per-process cuDNN pick decoded the same latent three different ways in three fresh homes, and with it off all three matched. On the A100 both settings matched. Off skips about 1 to 1.5 s of conv benchmarking in the first render; the steady VAE decode is 1.6 ms slower on the A100 (under 0.05% of a 3.4 s render) and unchanged on L4, and steady render time is within the home-to-home spread on both cards. On L4 the off setting decodes these latents slightly differently from the on setting (a different conv algorithm), so L4 images move once versus main; on the A100 the images are bit-identical to the on setting.

Max speed mode, main (no filter, mode="max-autotune-no-cudagraphs", coordinate descent on) vs this branch (filter on, max_autotune on, coordinate descent off, which is how every family that pins the filter already compiles in max mode):

Card Arm Fresh homes Images across homes First render Steady render (per home)
L4 main 2 2 variants (the homes differ on every prompt) 84.1, 84.8 s 13.23, 13.23 s
L4 this branch 3 1 variant 46.6 to 46.9 s 13.25, 13.26, 13.24 s
A100 40 GB main 2 1 variant 56.2, 56.7 s 3.386, 3.390 s
A100 40 GB this branch 3 1 variant 31.5 s 3.431, 3.433, 3.440 s

On the A100 the branch's max mode is 1.3% slower per image (0.045 s), outside the home-to-home spread (under 0.3%); on L4 the difference is inside the spread. Keeping coordinate descent in max mode would bring back the per-home retiming that split main's max mode on L4 above, and the branch's first render is 25 to 38 s faster because the cold compile benchmarks less, so the A100 per-image cost is repaid only after about 550 images in one process. The branch keeps the existing max-mode policy.

Max speed mode is outside the one-seed-one-image claim: max_autotune still benchmarks GEMM templates in every fresh process. It rendered one image across 3 fresh homes on each card here, but that is not guaranteed.

Peak VRAM (one render process, after load): A100 40 GB 18.5 GiB allocated, 20.5 to 21.3 GiB reserved, 21.9 GiB peak on the card (fully resident); L4 7.6 GiB allocated, 10.1 to 10.8 GiB reserved, 11.1 GiB peak on the card (Studio chose CPU offload on L4). Identical in both cuDNN arms and both max-mode arms apart from the reserved figure.

Changes

  • studio/backend/core/inference/diffusion_families.py: filter_reduction_configs = True and cudnn_benchmark = False on the krea-2 family.
  • tests/test_diffusion_reduction_filter_arch.py: Krea-2 filters on every arch, the flag reaches the compile call, and cudnn.benchmark is off (fails on main, passes here).
  • tests/test_diffusion_speed.py: Krea-2 joins the cudnn.benchmark opt-out set.
  • tests/fixtures/diffusion_family_data_freeze.json, tests/fixtures/diffusion_autoopt_parity.json: the krea-2 rows only (the compile call now carries force_filter_reduction_configs, and cudnn_benchmark leaves the applied set).

The compile-cache key already includes the filter, so existing Krea-2 compile caches are rebuilt once on the next load.

Tests

  • New tests: 7 failed on main, 7 pass here.
  • Every backend test file that mentions krea, the reduction filter or the compile cache (95 files) run on main and here: the failing test IDs are identical apart from the 7 new ones (46 pre-existing failures on this host come from local package versions, for example huggingface_hub lacking get_cached_repo_tree).

Not tested

  • Cards other than A100 40 GB and L4 (H100, B200, consumer cards): both settings apply on every arch by design, as for the other pinned families, but their effect there is unmeasured.
  • Whether the in-render VAE decode (as opposed to the fixed-latent decode) can split with cudnn.benchmark on: it matched across 3 homes on both cards here.
  • Which of the two max-mode differences (filter on, coordinate descent off) accounts for the 1.3% A100 max-mode cost: they were measured together.
  • Resolutions other than 1024 px, CFG, Krea-2-Raw, LoRA, and offloaded loads on cards other than L4.
  • FLUX.2 Klein 4B and Qwen-Image-2.1 INT8 (2 prompts each, 3 loads each across fresh and reused homes) showed no split on main; both already pin the filter.

…nder one seed the same way

A cold compile cache benchmarks near-tied Inductor reduction configs in its own
process and keeps the winner in the Studio home, so two fresh homes on one A100
rendered the same Krea-2 INT8 seed two ways (LPIPS 0.146 mean over 12 prompts),
and every later load of a home repeated its first pick. Forcing the slowest
reduction config reproduces the other image exactly; with the filter on, the
default and slowest picks render bit-identical images.

Krea-2 now sets filter_reduction_configs on every arch, as FLUX.2 Klein,
FLUX.3 Klein and Qwen-Image-2.1 already do.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T15:56:49.353713Z 4209430 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 1ad21b6347

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

… across fresh homes

With cudnn.benchmark on, each process benchmarks the VAE convs and keeps its own
pick. On an L4 one fixed latent decoded through the Krea-2 VAE gave 3 different
outputs in 3 fresh Studio homes; with it off all 3 matched (A100 matched either
way). Off also skips about 1 to 1.5 s of conv benchmarking on the first render,
and steady render time is unchanged within run-to-run spread on L4 and A100.

Krea-2 now sets cudnn_benchmark = False, as the FLUX.2 and FLUX.3 Klein families
already do. The autoopt parity rows for krea-2 drop cudnn_benchmark from the
applied set; nothing else changes.

[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. Swish!

Reviewed commit: 42094308d7

ℹ️ 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
danielhanchen merged commit f398357 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the studio-int8-first-load-consistency branch November 23, 2026 17:18
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…og (#19125)

## Problem


`studio/backend/tests/test_diffusion_backend.py::test_family_render_cudnn_scope_overrides_an_inherited_benchmark`
is red on `main` in Backend CI `(Python 3.13, diffusion)`. It fails for
every PR based on `main` since #19077, for example #19122:

```
with family_render_cudnn_scope(detect_family("krea/Krea-2-Turbo"), "cuda"):
    assert cudnn.benchmark is True  # a family that keeps the benchmark is left alone
AssertionError: assert False is True
```

## Cause

#19077 is correct. It sets `cudnn_benchmark = False` on Krea-2 on
purpose: the per-process VAE conv pick decoded one fixed latent three
different ways across three fresh L4 homes.

The test is wrong. It used Krea-2 as its hard-coded example of a family
that keeps `cudnn.benchmark` on, so pinning Krea-2 broke it. The same
would happen whenever another family gets pinned.

## Fix

Tests only. The test now reads both lists from `_FAMILIES` instead of
naming a family:

- for every family with `cudnn_benchmark = False`, the scope holds the
flag off inside and restores it after;
- for every family that keeps it, the scope leaves the flag alone;
- the FLUX.2 Klein case and the CPU-device case are unchanged;
- it asserts that both lists are non-empty, so it cannot pass vacuously.

## Verification

- The test passes; the whole file passes serially (694).
- Sabotage A: make `family_render_cudnn_scope` ignore the family flag.
The test fails, naming `qwen-image-2.1`, a family that keeps the
benchmark.
- Sabotage B: make the scope never hold the flag off. The test fails on
the first opt-out.
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…e faster cuDNN pin (#19110)

## Summary

On A100 and L4 class cards, `auto` attention keeps native SDPA when
torch's own dispatch picks FLASH (#17290). That check asked torch with
an unmasked query. Krea 2 masks every block attention call (its text
rows are padded to a 128-row bucket and masked as keys), and torch's
FLASH kernel takes no mask, so on those cards every Krea 2 block call
actually ran mem-efficient attention (`fmha_cutlassF`). On an L4 that
kernel was 34% of a Turbo render.

This PR makes the check ask the way the real call is made. For DiTs that
mask every block call (`MASKED_ATTENTION_DITS`, Krea 2 for now), both
native-dispatch probes (the A100 / L4 flash rule and the Blackwell cuDNN
rule) now pass a boolean key-padding mask to `torch._fused_sdp_choice`.
Torch answers mem-efficient there, so `auto` keeps the cuDNN pin for
Krea 2. Nothing else changes:

- unmasked DiTs are probed exactly as before and keep native flash;
- if a future torch ever runs FLASH with a mask, the probe sees it and
`auto` goes back to native;
- an explicit attention pick, `speed_mode=off`, and
`UNSLOTH_DIFFUSION_NATIVE_FLASH_ATTN=0` behave as before.

## Kernel measurements (masked calls, native dispatch vs the cuDNN pin)

Same inputs, a key-padding mask, bf16, CUDA events, median of
interleaved rounds. Error is relative L2 against an fp32 math reference
with TF32 off.

| shape (B, H, S, D) | A100 native ms | A100 cuDNN ms | L4 native ms |
L4 cuDNN ms | rel L2 native / cuDNN |
|---|---|---|---|---|---|
| Krea 2 1024 (1, 48, 4352, 128) | 5.81 | 3.33 (0.57x) | 14.77 | 8.45
(0.57x) | 0.002278 / 0.002276 |
| Krea 2 1024, CFG pair (2, 48, 4352, 128) | 11.33 | 6.32 (0.56x) |
29.83 | 16.81 (0.56x) | 0.002276 / 0.002274 |
| Krea 2 2048 (1, 48, 16640, 128) | 80.98 | 43.91 (0.54x) | 214.97 |
117.89 (0.55x) | 0.002306 / 0.002305 |

## End to end: Krea-2-Turbo, 1024 px, 8 steps, AUTO load (int8 DiT and
encoder)

Colab, one VM per card, a fresh process per arm, arms interleaved (ref,
base, head, head, base, ref). Base is the merge base `b94d31044f`; head
is this branch. Both trees carry the Krea 2 reduction-config pin from
#19077, so Inductor's per-home reduction benchmark cannot split the two
arms. 8 prompts, seeds 1000 to 1007, one warm-up render then 8 timed
renders.

Engagement: base logs `auto keeps native SDPA (flash attention measured
faster ...)` and its 32 attention processors run the native backend;
head logs `backend=_native_cudnn` and all 32 processors carry
`_native_cudnn`.

| card | base s/image (A/A) | head s/image (A/A) | change | peak
reserved base / head |
|---|---|---|---|---|
| A100 40 GB | 3.450 / 3.447 | 2.892 / 2.892 | -16.1% | 20198 / 20184
MiB |
| L4 (fresh Studio home per arm, 3 arms each) | 13.627 / 13.558 / 13.677
| 11.639 / 11.646 / 11.723 | -14.5% (medians 13.627 vs 11.646; A/A
spread 0.9% / 0.7%) | 9604 / 9590 MiB |

## Accuracy against an fp32 attention reference

The reference arm runs the same load and weights with every diffusers
attention backend replaced by fp32 math attention (q, k, v upcast, TF32
off, explicit masked softmax), so it differs from base and head only in
the DiT's attention. Distance is LPIPS (alex) and PSNR of each arm's
image to the reference image of the same prompt and seed.

| card | LPIPS to ref, base | LPIPS to ref, head | paired head minus
base (mean / median) | head closer | PSNR base / head | A/A spread |
|---|---|---|---|---|---|---|
| A100 40 GB | 0.1318 | 0.1203 | -0.0114 / -0.0155 | 6 / 8 prompts |
22.18 / 22.94 dB | base, head and ref each bit-identical across
processes |
| L4 | 0.1788 | 0.1365 | -0.0423 / -0.0289 | 6 / 8 prompts | 20.46 /
21.49 dB | base 0.00009 LPIPS, head bit-identical |

Head is no further from the reference than base on either card. On the
L4 accuracy VM the head tree also carried a memory-planner row that was
later dropped from this PR; it only changes where the DiT sits (streamed
vs uploaded per call), and on another L4 VM that placement change alone
moved no pixel beyond the 0.0001 LPIPS process-to-process variants base
shows by itself. The absolute distances are large because an 8-step
distilled sampler amplifies any last-bit attention difference into a
visibly different image; the kernel-level error above is the same for
both kernels.

## Other families that mask every block call

| family | masked block calls | this PR |
|---|---|---|
| Krea 2 | every call (text padding) | cuDNN pin on A100 / L4 |
| Chroma | every call (T5 padding as keys and queries) | unchanged:
cuDNN measured 0.62x (A100) / 0.74x (L4) per call, and 15.6% faster per
image on an A100, but its pinned renders were not repeatable across
processes (2 of 8 identical) while base was, and it moved slightly
further from the fp32 reference (LPIPS 0.0069 to 0.0110 vs 0.0064) |
| HunyuanImage-2.1 | every call | unchanged: cuDNN 0.57x per call on
both cards, not measured end to end |
| Ideogram 4 | every call | unchanged: head_dim 256 has no cuDNN kernel
on this torch, so the pin would be refused anyway |
| Qwen-Image, Z-Image, HunyuanVideo-1.5, Qwen-Image-2.1, LTX-2 | only
some calls (uneven CFG pairs, prefill text segments, a disabled trim) |
unchanged |

## Tests

`tests/test_diffusion_attention_masked_probe.py` (11 cases): a masked
DiT keeps the cuDNN pin on SM80 and SM89, an unmasked DiT still runs
native flash, a masked DiT goes native once torch would run FLASH with a
mask, a mixed pipeline asks masked, masked and unmasked answers are
cached apart, the Blackwell probe asks masked too, and the probe really
passes a boolean key-padding mask. Seven of them fail on main and all
pass here; the existing native-flash, Blackwell and cuDNN head-dim
suites pass unchanged.

## Not covered

- Blackwell: checked on SM100 (B200, torch 2.12.1, correctness only):
torch's dispatch is cuDNN for both the masked and unmasked probe at head
dim 64 and 128, so on SM100 this PR changes nothing. SM120 (RTX PRO 6000
/ RTX 50) was not checked.
- Hopper and SM86 cards already kept the cuDNN pin for Krea 2 and are
unchanged.
- Torch builds older than 2.13 were not run; the masked cuDNN path is
the one Krea 2 already used on every card outside A100 / L4.
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