Skip to content

Studio diffusion: keep the fused AdaLN census out of compiled FLUX.1 blocks (recompile limit on first render) - #17310

Merged
danielhanchen merged 2 commits into
mainfrom
studio-flux1-single-block-recompile
Nov 9, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-flux1-single-block-recompile

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

FLUX.1 (schnell and dev) loaded in Studio on NVIDIA Linux at the default speed tier fails its first render with

torch._dynamo hit config.recompile_limit (64)
   function: '_flux_single_forward' (diffusion_int8_fused.py:1120)
   last reason: 1/63: G['__import_core_dot_inference_dot_diffusion_rocm_fused'].COUNTS['adaln_stock'] == 139
     # COUNTS["adaln_stock"] += 1  # diffusion_rocm_fused.py:407 in _adaln_zero_single_forward
FailOnRecompileLimitHit: Hard failure due to fullgraph=True

Seen on a Colab A100 40GB with auto INT8 (the default there) at ed3eec6, in every repeat.

Root cause

Since #16760 the fused AdaLN forwards are installed on NVIDIA Linux off the off tier, including the compiled tiers. Under torch.compile, _fused_modulate correctly returns None so the stock math is traced. The engagement census COUNTS["adaln_stock"] += 1 still runs inside the trace, though. Dynamo treats a write to a module-global dict as a side effect: it guards on the current value (COUNTS['adaln_stock'] == 139) and replays the increment after the graph. The next call therefore always fails the guard, so every block call compiles a new graph.

FLUX.1 is the only family affected because its repeated blocks (FluxTransformerBlock, FluxSingleTransformerBlock) call AdaLayerNormZero / AdaLayerNormZeroSingle inside the regionally compiled region. FLUX.2 modulates inline and only uses AdaLayerNormContinuous in the eager norm_out. Z-Image, Qwen-Image and FLUX.3 never install these forwards. The single block hits the limit first (38 calls per step against 19 for the double block), under fullgraph=True. That kills the CUDA graph capture and then the eager fallback. The audit run's per-process stats show 121 unique graphs for FLUX.1, against 2 for FLUX.2 klein and Z-Image. This is not int8 specific: a bf16 FLUX.1 at the compiled tiers hits the same guard.

The RoPE wrapper in the same file and the fp8 unit-scale path already skip their counters while compiling. AdaLN was the one that did not.

Fix

The AdaLN census now goes through _count(), which only increments outside a trace (torch.compiler.is_compiling()). Eager counting is unchanged, there are no new graph breaks, fullgraph is kept, and compiled output does not change (the traced math was already the stock branch).

Tests

studio/backend/tests/test_diffusion_rocm_fused.py, CPU only:

  • test_compiled_adaln_never_recompiles_per_call (3 classes): compiles a call through each patched AdaLN class with fullgraph=True, dynamic=False under error_on_recompile, checks the output against eager and checks the census is untouched while compiled.
  • test_tiny_flux1_regional_compile_holds_its_graphs_across_steps: a tiny FluxTransformer2DModel with compile_repeated_blocks(fullgraph=True, dynamic=False) (Studio's FLUX.1 settings), three denoise steps; the graph count after step 1 must not grow.
main (1b88784) this branch
new tests 4 failed (RecompileError x3, recompile limit exceeded on the tiny FLUX.1) 4 passed
whole file 29 passed, 12 skipped (GPU) 33 passed, 12 skipped (GPU)

@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-09T20:05:52.645636Z d2b3ecf 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. More of your lovely PRs please.

Reviewed commit: ecf2eb477c

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

@NilayYadav NilayYadav self-assigned this Nov 9, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d2b3ecf54b

ℹ️ 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 e1e9a7c into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the studio-flux1-single-block-recompile branch November 9, 2026 20:08
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