Skip to content

Studio int8 folds: do not fold an add with a non-unit alpha - #17794

Merged
danielhanchen merged 1 commit into
mainfrom
studio-int8-residual-fold-alpha
Nov 15, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-int8-residual-fold-alpha

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #17680. The native W8A8 post-grad pass folds residual + linear(x) * gate into the GEMM epilogue, but its matcher read only the two positional operands of aten.add.Tensor and ignored the keyword-only alpha. A graph such as torch.add(residual, y * gate, alpha=2) (which is residual + 2 * y * gate) was folded into residual + y * gate, a silent wrong result. The GELU and norm folds had the same gap on the adds they match (1 + tanh(...), a + 0.044715 * a^3, 1 + scale, n * (1 + scale) + shift, mean + eps).

Changes

  • _fn (the target check every matcher uses) now rejects an add whose alpha is not exactly 1. Every fold rebuilds the unit-coefficient add, so a scaled add simply stays as traced.
  • The norm fold also refuses an aten.mean.dim carrying a non-fp32 dtype, since the prologue always computes the fp32 statistic.
  • The pass uuid moves from share.v2 to share.v3, so any FX graph folded before this check misses the cache instead of being reused.

The default LTX-2.5 style graphs have no alpha kwarg on these adds, so they fold exactly as before (the existing fold tests are unchanged and pass). The check is compile-time only: it runs while Inductor rewrites the graph and adds no per-call host work.

Tests

studio/backend/tests/test_diffusion_native_w8a8_cuda.py:

  • test_folds_refuse_a_scaled_add: for every matched add in the three folds and alpha in (2, -1, 0.5), the fold returns 0 and the graph output equals the scaled eager result bit for bit; alpha=1 still folds.
  • test_norm_fold_refuses_a_low_precision_mean and test_pass_uuid_names_the_alpha_aware_folds.
Arm Result
origin/main 36 passed, 20 failed (all 20 are the new tests)
this branch 56 passed

A standalone repro that installs the real pass in post_grad_custom_pre_pass and runs torch.compile(fullgraph=True) on CPU gave 5 instead of 9 on main for alpha=2; on this branch it gives 9 with 0 folds.

No GPU run: default inputs take the same folds as before, so the GPU path is unchanged.

The residual, GELU and norm folds in the native W8A8 post-grad pass
matched aten.add without reading its alpha kwarg and rebuilt the plain
x + y, so torch.add(x, y, alpha=2) folded into x + y and changed the
result. _fn now refuses an add whose alpha is not 1, the norm fold
refuses a mean with a non-fp32 dtype, and the pass uuid moves to v3 so
graphs folded before this check miss the FX graph cache.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T05:17:01.827891Z 18c6847 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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 18c68471db

ℹ️ 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 232485d into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the studio-int8-residual-fold-alpha branch November 15, 2026 07:00
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