Skip to content

Attention: let the SDPA GQA probe trace under torch.compile(dynamic=True) - #14779

Merged
danielhanchen merged 1 commit into
mainfrom
dh/s3-gqa-probe-dynamic
Oct 22, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/s3-gqa-probe-dynamic

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The SDPA GQA routing added in #14158 asks torch which kernel a call would get. That probe, _sdpa_gqa_state (and _sdpa_backend_state for the varlen choice), runs through torch.compiler.assume_constant_result, and dynamo can only pass such a function Python constants. #14398 handled the boolean arguments. Under torch.compile(fullgraph=True, dynamic=True) the head dim and head counts are still SymInts, so compilation fails before the forward:

torch._dynamo.exc.Unsupported: assume_constant_result argument conversion failed
  Cannot convert argument of type SymNodeVariable to a Python constant

Repro: run_attention plus backward, noncausal GQA on CUDA, Q (2, 8, 32, 64), K/V (2, 2, 32, 64), no mask, SDPA backend. Before #14158 both static and dynamic fullgraph passed.

Fix: a small _constant_probe wrapper replaces the two bare assume_constant_result wraps. It passes integer arguments through operator.index before the call. Under dynamo that specialises the head sizes, which are fixed per model. Batch and sequence length stay dynamic. Plain ints pass through unchanged, so eager and static-shape routing is identical. functools.wraps is avoided on purpose: assume_constant_result marks the wrapped function in place, and copying its __dict__ would mark the wrapper too.

Tests (tests/test_attention_dispatch_compiled_block.py):

  • CPU: _sdpa_gqa_state and _torch_varlen_beats_segments traced fullgraph, static and dynamic=True, match eager with 0 graph breaks and one graph across batch / length changes.
  • CUDA: real run_attention forward plus backward in bf16 GQA, compiled fullgraph static and dynamic with Inductor. Outputs and gradients match eager, with 0 graph breaks and one graph under dynamic=True.

Results on B200, torch 2.13.0 and 2.14.1:

main this PR
fullgraph, static pass pass
fullgraph, dynamic=True fails before forward pass, 0 graph breaks
attention dispatch tests (GPU) 4 failed, 69 passed 1 failed, 72 passed

The remaining failure, test_plain_causal_attention_traces_with_a_dynamic_length[xformers], is already on main and fails only on a GPU host: the xformers fp32 downcast gate looks at the host GPU, not at the tensor device. It is left for a separate change.

…rue)

The GQA and backend probes are assume_constant_result functions, and dynamo can
only pass them Python constants. With dynamic=True the head dim and head counts
are SymInts, so compilation failed before the forward with
'assume_constant_result argument conversion failed'. Specialise integer
arguments with operator.index before the call: head sizes are fixed per model,
batch and length stay dynamic, and plain ints pass through unchanged so every
static-shape decision is the same.
@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-22T20:45:31.634899Z f2f0fd6 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: f2f0fd62b4

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

Copy link
Copy Markdown
Member Author

pre-commit.ci run

@danielhanchen
danielhanchen merged commit 32650d2 into main Oct 22, 2026
3 of 30 checks passed
@danielhanchen
danielhanchen deleted the dh/s3-gqa-probe-dynamic branch October 22, 2026 23:08
danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
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