Repository navigation
Studio diffusion: keep the compiled int8 W8A8 linear at or below the eager peak, honor force-eager - #15456
Conversation
…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.
|
@codex review |
Codex Review SummaryThis 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. You're on a roll. 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". |
…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]
|
@codex review |
|
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". |
[skip ci]
…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>
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 << 27elements, 4x the eager1 << 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 withtorch.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 atorch.catcopy).Force-eager bypass.
_forward_fusedchecked Dynamo tracing and CUDA graph capture but not the sharedeager_forced()flag fromdiffusion_bg_compile, so a cold native int8 linear inside the Detailer'sauxiliary_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:
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 underauxiliary_pass()/force_eager(), and a CUDA test for peak at or below eager plus bit-equality. The CUDA skip guards usehas_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.