Skip to content

Studio diffusion: keep the compiled int8 W8A8 linear at or below the eager peak, honor force-eager - #15456

Merged
danielhanchen merged 4 commits into
mainfrom
fix/native-int8-fused-peak
Oct 27, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fix/native-int8-fused-peak

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two regressions from #15183 in studio/backend/core/inference/diffusion_native_quant.py.

Peak memory above eager. The compiled W8A8 linear used a chunk budget of 1 << 27 elements, 4x the eager 1 << 25, on the reasoning that it only holds the int32 accumulator. It also holds the output chunk at the same time, and the chunk outputs were then joined with torch.cat, so a large linear peaked above the eager path. For an 8192-row bf16 linear 3072 -> 14336 (Wan 2.2 5B sized) with rotation 256: 558 MiB before #15183, 672 MiB on main, so it runs out of memory with 650 MiB of headroom where the eager path fits.

Now the fused budget equals the eager one and the chunks are written into one preallocated output with copy_ (no list of parts plus a torch.cat copy).

Force-eager bypass. _forward_fused checked Dynamo tracing and CUDA graph capture but not the shared eager_forced() flag from diffusion_bg_compile, so a cold native int8 linear inside the Detailer's auxiliary_pass() compiled a graph despite the #15063 contract. It now returns to the eager kernels while the flag is set. The same check also applies while a background compile is running, so those generations run the int8 linears eagerly for that window.

Per-linear peak (MiB) and time (ms), shared card, 20-iteration means, so timings are indicative only:

shape (rows, in -> out), rotation before #15183 main this PR before ms main ms this PR ms
8192, 3072 -> 14336, 256 558.06 672.02 392.00 1.58 0.58 0.74
8192, 3072 -> 14336, 0 510.06 672.02 392.00 1.57 0.58 0.73
8192, 14336 -> 3072, 256 400.01 336.02 132.00 1.80 0.55 0.67
20000, 3072 -> 14336, 256, no bias 1210.94 1093.75 729.23 3.76 1.56 1.70
4096, 3072 -> 3072, 256 180.03 72.01 72.01 0.26 0.15 0.15

Outputs are bit-identical across all arms on every shape. Inside auxiliary_pass(): main makes 1 fused call and 1 compiled graph, this PR makes 0 and 0, and the layer goes back to the fused path after the context exits.

Tests in test_diffusion_native_quant_fused.py: chunks fill one output in row order, the fused peak never exceeds eager across widths, no fused call under auxiliary_pass() / force_eager(), and a CUDA test for peak at or below eager plus bit-equality. The CUDA skip guards use has_real_cuda(). Against main's module the new CPU tests give 5 failed, with this PR 21 passed, 4 skipped on CPU and all 25 passed on a GPU.

…eager peak, honor force-eager

The compiled linear used a 4x larger chunk budget than eager, but its int32
accumulator and output chunk are live together, so an 8192-row 3072->14336
linear peaked at 672 MiB against 558 MiB eager and OOMed where the parent fit.
Use the eager chunk budget and fill one preallocated output instead of
concatenating parts (392 MiB on that shape, bit-identical output).

Also return to the eager kernels when eager_forced() is set (Detailer
auxiliary_pass, in-flight background compile), so those passes no longer
compile the helper on the calling thread.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-10-27T00:34:42.437401Z 7dbd711 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. You're on a roll.

Reviewed commit: 998a2f318c

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

…chunk's dtype

Under autocast the compiled function can return a different dtype than the
input (fp32 activations under fp16 autocast with rotation give fp16); the
chunked path recast that into an input-dtype buffer while the unchunked path
and main returned fp16.

[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. 🎉

Reviewed commit: 7dbd711e63

ℹ️ 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 merged commit 7309599 into main Oct 27, 2026
@danielhanchen
danielhanchen deleted the fix/native-int8-fused-peak branch October 27, 2026 10:14
danielhanchen added a commit that referenced this pull request Oct 27, 2026
…utput rows (#15533)

#15456 capped the compiled int8 W8A8 linear's peak by running large
inputs in row chunks. Each chunk's result was a fresh tensor that was
then copied into the preallocated output. That cost an extra full write
per chunk and a chunk-sized temporary, so common Flux / Wan linears
became slower than before #15456 even when memory was ample.

This change compiles a second function, `_w8a8_rows_into`, that stores
each chunk's result straight into its row slice of the output. There is
no temporary and no second copy. The returning path from #15456 is kept
unchanged for the one case where the output dtype is not known up front:
a rotated layer under autocast, where the output is still allocated from
the first chunk's dtype.

## Results

Colab L4 (sm_89), torch 2.11, bf16. Times are the median of 7 repeats of
20 iterations, with half-range spread. A/A was run first and is inside
0.05 ms for every shape. Arms were interleaved ABBA. Peak is
`max_memory_allocated` above the pre-call baseline.

| linear | main ms | this PR ms | change | main peak MiB | this PR peak
MiB | output |
|---|---|---|---|---|---|---|
| 8192x3072 -> 14336, rot256, bias | 11.776 +-0.098 | 10.033 +-0.058 |
-14.8% | 392.0 | 342.0 | bitwise equal |
| 8192x14336 -> 3072, rot256, bias | 11.082 +-0.125 | 10.866 +-0.106 |
-1.9% | 132.0 | 132.0 | bitwise equal |
| 20000x3072 -> 14336, rot256, no bias | 29.840 +-0.187 | 25.793 +-0.226
| -13.6% | 729.7 | 675.4 | bitwise equal |
| 4608x3072 -> 12288, no rotation, bias | 5.726 +-0.175 | 4.943 +-0.166
| -13.7% | 270.0 | 222.8 | bitwise equal |

Outputs are bitwise identical to main, and the peak is equal or lower in
every case.

## Compile behaviour

`_w8a8_rows_into` is compiled with `dynamic=True, fullgraph=True`, like
`_w8a8_rows`. Chunk heights and slice offsets all reuse one graph: one
frame, no graph breaks. The new CPU test checks this with a compile
counter. Inductor's generated kernel stores directly into the slice
argument and the graph returns nothing, so no output-sized buffer is
allocated. It is a separate code object, so it does not use up
`_w8a8_rows`'s recompile budget. A compile failure in either function
still falls back to the eager kernels, exactly as before.

## Tests

Added to `studio/backend/tests/test_diffusion_native_quant_fused.py`:

- `test_chunks_are_stored_once_into_their_output_rows`: each chunk is
written once, into the returned output at its row offset, without
calling the returning function. Fails on main.
- `test_compiled_into_slice_is_one_graph_and_bit_identical`: the real
`torch.compile` gives one frame and no graph breaks over several heights
and offsets, and the result is bit-equal to the unchunked function.
Fails on main.
- `test_rotation_under_autocast_keeps_the_returning_path`: a rotated
layer under autocast keeps the dtype fix from #15456.

On CPU: 26 passed, 4 skipped (CUDA-only).

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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