Repository navigation
Studio: pin Krea-2's reduction-config filter so fresh Studio homes render one seed the same way - #19077
Conversation
…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.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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.
…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.
Summary
Krea-2 now pins Inductor's reduction-config filter (
filter_reduction_configs = True) and keepscudnn.benchmarkoff 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_BLOCK2048 vs 8192). The winner is written into the compile cache under the Studio home, so: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.safetensorsandKrea-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):base_auto(r0, r1)base_int8(r0, r1),main_*_notrimForcing 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:
With this branch (current main plus the one-line family change, same A100 run as its main arm):
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.decodeduring the renders, plus a standalone decode of one fixed seeded latent through the samevae.decodeunder the render's cuDNN scope.cudnn.benchmark on (as before this change) vs off (this branch), default speed mode, 3 fresh homes each:
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_autotuneon, coordinate descent off, which is how every family that pins the filter already compiles in max mode):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_autotunestill 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 = Trueandcudnn_benchmark = Falseon thekrea-2family.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 carriesforce_filter_reduction_configs, andcudnn_benchmarkleaves 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
huggingface_hublackingget_cached_repo_tree).Not tested