Skip to content

Studio diffusion: Ada FP8 unit-scale dispatcher installs only where it engages, chunked eager scaling - #14665

Merged
danielhanchen merged 3 commits into
mainfrom
recheck_ada_fp8
Oct 30, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
recheck_ada_fp8

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #13793 (sm_89 precise-accumulate fp8 via a unit-scale _scaled_mm and an fp32 scale epilogue). It fixes four problems.

  1. Installed where it can never engage. install() ran from _make_quant_config for every fp8 config, and from _load_prequant_checkpoint for every prequant checkpoint, int8 and nvfp4 included. On any Ada host it installed in auto mode even under Studio's default fast accumulate, where the dispatcher never engages. Installing sets the process-wide torch.compiler.config.cache_key_tag, which environment_fingerprint also adds to the Studio bundle key. So after one fp8 or prequant load, every later compile in the process got a second cache key, a bf16 or GGUF model included. The cost was one extra cold compile per model and load order. It never caused a wrong hit.
  2. Inconsistent kill switch. The compile-cache switch key reads UNSLOTH_DIFFUSION_FP8_UNIT_SCALE=none as off, but _mode() read it as auto and installed anyway.
  3. Eager peak memory. The eager epilogue acc * a_scale.float() materialized a full fp32 [M, N] buffer and then a second bf16 output. That is four times the output size, with no memory admission check.
  4. Wrong error bound in the docs. The module docstring stated a relative error of at most 2^-9. bf16 has 8 significand bits, so round-to-nearest unit roundoff is 2^-8. That bounds the absolute output error by 2^-8 of |acc * a_scale * b_scale|. A bias that cancels the product leaves the output's relative error unbounded: an exact dot product of 257 with scale 1/257 and bias -1 gives -0.0039 instead of 0. The PR Studio: run Ada FP8 precise accumulate through a unit-scale GEMM plus a fused scale epilogue #13793 description repeats the 2^-9 figure.

Fix

  • install(fast_accum=...) and a new can_engage(): in auto mode it no longer installs for a load running fast accumulate. Precise and unknown accumulate still install, and so does force.
    • _make_quant_config(fp8) passes the resolved accumulate.
    • The ComfyUI prequant branch passes the load's resolved accumulate.
    • _load_prequant_checkpoint reads the checkpoint metadata. int8 checkpoints never install. fp8 baked at fast accumulate (the builder's default) does not install. Precise fp8, nvfp4 (a policy over fp8) and checkpoints with no recorded accumulate install, as before.
  • _mode() uses the same off and on words as the compile-cache switch key, so none is off in both.
  • Eager epilogue: the fp32 product is formed one row chunk at a time (about 16M elements, 64 MiB) and written back into the bf16 accumulator. The elementwise ops and their order are unchanged, so the output is bitwise the same. Under torch.compile the fused expression is kept as is.
  • The docstring now states the correct bound.

Residual, by design: once a precise-accumulate fp8 model has installed the dispatcher, the tag stays for the rest of the process. That is required for the cache to stay consistent with the patched torchao entry point.

Measured

B200, force mode (UNSLOTH_DIFFUSION_FP8_UNIT_SCALE=1, so the dispatcher runs off sm_89), eager, precise accumulate, bias on. Values are peak allocation above the inputs.

shape (M x K x N) torchao row-wise main dispatcher this branch
4608 x 3072 x 18432 162 MiB 648 MiB 226 MiB
4096 x 3072 x 3072 24 MiB 96 MiB 72 MiB

The output checksum is identical between main and this branch on both shapes.

Tests

tests/test_diffusion_fp8_unit_scale.py. Each new test fails on main and passes here:

  • test_auto_never_installs_for_fast_accumulate
  • test_fp8_quant_config_installs_only_for_precise_accumulate
  • test_prequant_checkpoint_installs_only_when_its_fp8_layers_can_engage (the int8 and fp8-fast cases; the precise, nvfp4 and unrecorded cases pass on both)
  • test_every_off_word_of_the_compile_cache_key_turns_the_dispatcher_off
  • test_eager_epilogue_writes_into_the_accumulator_bitwise_as_before (with and without bias): the output aliases the accumulator and is bitwise equal to the old fused expression.

The whole file passes, including the GPU test test_compiled_graph_after_a_cached_rowwise_one_uses_the_dispatcher, so the compiled graph still takes the dispatcher.

…r fp32 buffer

The sm_89 dispatcher installed on every Ada fp8 config and every prequant
load (int8 and nvfp4 included), even under the default fast accumulate where
it never engages, and installing tags the process-wide torch compile cache
key, splitting later compiles' caches. It now installs only for precise or
unknown accumulate (or force), reading the checkpoint metadata for prequant
loads. The kill switch reads 'none' as off, like the compile-cache key.

The eager epilogue formed a full fp32 [M, N] buffer plus a second bf16 output.
It now writes back into the bf16 accumulator a row chunk at a time, with the
same ops in the same order (bitwise the same output); compiled graphs keep the
fused expression. B200 force mode, 4608x3072x18432: 648 -> 226 MiB peak above
the inputs (row-wise 162).

The stated error bound was 2^-9; bf16 unit roundoff is 2^-8, and it bounds the
absolute error only.
@danielhanchen danielhanchen self-assigned this Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-30T10:21:47.990901Z 0b359be 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. Keep it up!

Reviewed commit: 2826ed621b

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

… guard alongside the metadata/fast_accum install gate

[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: 0b359be6d5

ℹ️ 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 487ee60 into main Oct 30, 2026
@danielhanchen
danielhanchen deleted the recheck_ada_fp8 branch October 30, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant