Skip to content

[RFC #7240] Build the plugin config snapshot at import, not on first dispatch - #7848

Merged
zufayu merged 1 commit into
mainfrom
codex/warm-config-at-import
Nov 12, 2026
Merged

zufayu merged 1 commit into
mainfrom
codex/warm-config-at-import

Conversation

@zufayu

@zufayu zufayu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

What breaks

A routed op's first call in a process acquires a lock and reads an environment variable, because the config snapshot is built lazily:

snapshot = _config_snapshot
if snapshot is None:              # once per process
    with _config_lock:            # Dynamo cannot trace a lock
        snapshot = _swap_config_locked()   # nor the os.getenv inside it

torch.compile traces through the dispatcher — routing replaces a direct call with a Python wrapper, so whatever that wrapper does becomes part of the trace. That first call therefore graph-breaks, and the compiled region comes out as two graphs instead of one.

A framework that permits one backend invocation per compilation is then handed a second one and asserts. ATOM's is VllmBackend can only be called once.

Measured, not inferred

A counting backend, each case in its own process (the breakage is first-call-only and torch._dynamo state is global):

graphs
plain, unrouted function 1
routed op, snapshot unbuilt 2
routed op, snapshot already built 1

Nothing else on the dispatch path breaks, so building the snapshot eagerly is the whole fix. Confirmed on the same harness: 2 → 1.

The kill switch does not avoid it

AITER_PLUGINS=(unset)   ->  2 graphs
AITER_PLUGINS=0         ->  2 graphs

AITER_PLUGINS is read inside the function that breaks, so turning plugins off does not turn this off. Worth knowing before anyone uses it as a control in an A/B: it will not go green, and that is not evidence the plugin is uninvolved.

The change

Three lines at the end of the module. The hot path was already a plain global read for every call after the first; this makes the first one no different.

The lazy branch stays in _config() because _reset_for_testing clears the snapshot, and a test that clears it must not become the call that rebuilds it inside somebody's graph.

tests/test_plugin_overhead.py is unchanged and still passes at 0.58 of one Signature.bind.

Why not the alternatives

why not
torch.compiler.disable on _dispatch breaks the graph on every call rather than the first
allow_in_graph on the wrapper hides the whole dispatcher from Dynamo, including the provider path, to fix something that is not in it
move torch_compile_guard outside @route per op fixes ops one at a time and changes custom-op registration semantics
relax the framework's assertion turns an explicit compile error into a wrong cached graph

This one is in the interface, so all twelve routed ops are covered at once.

Tests

tests/test_plugin_compile.py, in subprocesses:

  • test_a_routed_op_compiles_into_a_single_graph — one compiled region, one graph.
  • test_the_config_snapshot_exists_before_any_dispatch — read before anything dispatches, since a lazily built snapshot would also be set by the time a model has run. Asking afterwards proves nothing, and the first version of this test made exactly that mistake.

Removing the fix fails both. 117 tests pass across the plugin suites; black and ruff clean.

What this does not cover

Only the provider-disabled path is measured. With a provider enabled, dispatch goes on through _eligible and _ask_provider, which I have not checked for graph breaks. That does not affect anyone today — nothing ships a provider — but it is the next thing to measure before one does.

Provenance

This is the coverage #7465 and #7728 lacked. Every test of this interface so far has been eager, and an eager test cannot see a graph boundary: the op returns the right answer either way. The defect is in the interface (#7465); #7728 is what put a routed op inside a compiled region and exposed it.

It is not established that this is the cause of any particular downstream failure — that needs a run with TORCH_LOGS=+dynamo on the affected machine, which this PR does not have.

🤖 Generated with Claude Code

A routed op's first call in a process acquired a lock and read an
environment variable, because the config snapshot was built lazily. Dynamo
can trace neither, so that call graph-breaks: the region compiles as two
graphs instead of one, and a framework that permits a single backend
invocation per compilation is handed a second and asserts.

Measured with a counting backend, each case in its own process:

    plain function                      1 graph
    routed op, snapshot unbuilt         2 graphs   <- the break
    routed op, snapshot built           1 graph

Nothing else on the dispatch path breaks, so building the snapshot at
import is the whole fix. The hot path was already a plain global read for
every call after the first; this makes the first one no different.

The lazy branch in _config() stays, because _reset_for_testing clears the
snapshot and a test that clears it must not become the call that rebuilds
it inside somebody's graph.

tests/test_plugin_compile.py pins both halves -- one graph per compiled
region, and nothing left to build when dispatch starts -- in subprocesses,
since the breakage is first-call-only and torch._dynamo state is global.
Removing the fix fails both.

This is the coverage #7465 and #7728 lacked: every test of the interface
so far has been eager, and an eager test cannot see a graph boundary.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7848 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@zufayu
zufayu marked this pull request as ready for review November 12, 2026 02:16
@zufayu
zufayu requested a review from a team November 12, 2026 02:16
@aiter-bot

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 64880e03a6b5 · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What is the most important boundary or failure mode introduced by this change, and what evidence covers it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — missing ### Reviewed revision in the PR description
  • FAIL — missing ### Behavior and intent in the PR description
  • FAIL — missing ### Validation and unverified scope in the PR description
  • FAIL — missing ### PR-specific question in the PR description
  • FAIL — missing ### Changed op tests in the PR description
  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — Behavior and intent has no substantive answer
  • FAIL — Validation and unverified scope has no substantive answer
  • FAIL — PR-specific question has no substantive answer

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@zufayu

zufayu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Reproduced on GPU, with the failure it was inferred from

The PR body measures graph counts. On an 8-GPU gfx942 box I reproduced the downstream symptom itself, using the real dispatcher and a backend carrying the same single-invocation constraint ATOM's does:

main (lazy snapshot):   torch._dynamo.exc.BackendCompilerFailed:
                          AssertionError: VllmBackend can only be called once
this PR (eager):        succeeds, backend invoked once

So the defect does not merely split a graph in the abstract — it produces that exact error.

The trigger condition, measured

The break is first-call-only, and that turns out to matter more than it sounds:

one routed op called eagerly before the compiled region backend invoked result
no 2 BackendCompilerFailed
yes (any routed op) 1 fine

A single eager dispatch anywhere before compilation builds the snapshot and protects everything after it. So whether a given deployment hits this depends on where its first routed call lands, not on how many routed ops it uses.

That gives a one-line check that needs no new wheel, for anyone currently red:

import aiter.plugin
aiter.plugin._config()     # or any eager call to a routed op

early in startup. If the failure disappears, this is the cause and that is also an immediate workaround; if it does not, this PR is not the fix for that failure and the search should continue elsewhere. Worth running before a pin-the-wheel A/B, because the answer is binary and arrives in one run.

What this PR does not fix

With a provider enabled, dispatch continues past the fast path into _eligible / _ask_provider / Decision, and that still breaks:

graphs
provider disabled (everyone today) 1
provider enabled 2

Nobody ships a provider yet, so this is not urgent — but it is the configuration the interface exists for, and it will surface the day one arrives.

torch._dynamo.allow_in_graph on the routed wrapper removes it (measured: enabled path 2 → 1), and is not a safe blanket answer: it presents the call to Dynamo as a pure function, and routed ops are not all pure. rmsnorm_with_add declares mutates_args=["out", "residual_out"], fused_qk_norm_rope_cache_quant_shuffle returns None and writes through its arguments, and several more write into a caller-supplied out= when given one. Treating those as functional under compile trades a loud failure for a silent wrong answer.

The shape that is correct for a mutating op is the one three routed ops already have: registration as a custom op with its mutations declared, i.e. torch_compile_guard outside @route rather than inside. Making that uniform is a change per op with real thought required per op, and it is not this PR.

Environment

gfx942, torch 2.9.1, 8 GPUs. The native in the probe is plain torch rather than a kernel: the installed AITER on that box cannot load module_aiter_core.so (GLIBCXX_3.4.31 missing), and the break happens during tracing, before any kernel would run.

@zufayu

zufayu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Reproduced with a real routed op, and the reason it is this set of ops

Earlier evidence here used synthetic ops because the box could not import AITER. That is fixed (flydsl was 0.4.1 against a tree wanting >=0.5.0), so these are real aiter ops with real kernels on gfx942.

aiter.pertoken_quant inside a compiled region, backend carrying the same single-invocation constraint ATOM's does:

snapshot lazy  (today's main)   backend invoked 2x  ->  BackendCompilerFailed
snapshot eager (this PR)        backend invoked 1x  ->  succeeds

So the fix holds end to end on a real op, not only on a probe.

Why some routed ops were never at risk

The same test with aiter.rms_norm does not break, even with the snapshot cold — one backend invocation either way.

The difference is not the op, it is what a caller reaches:

what the public name is Dynamo sees
rms_norm, rmsnorm2d_fwd a torch_compile_guard-registered op that calls _rms_norm_fwd_dispatch inside an opaque node; never enters the dispatcher
pertoken_quant, inverse_rope_group_quant, fused_qk_norm_rope_cache_quant_shuffle, mla_decode_fwd_v4_nm the routed function itself, @route and nothing else the dispatcher, traced

I had this wrong in my own earlier analysis on #7150 and in the first comment here: I classified exposure by the decorator on the routed function, when what matters is whether a guard sits between the caller and the dispatcher. For rmsnorm the route is on a private dispatch helper with guarded public spellings above it; for the others the public name is the routed function.

That also answers a question I could not answer before — why a deployment using routed ops for weeks only broke recently. The routed ops it was already using were the shielded kind.

Schema feasibility on the real ops

Relevant to #7850 rather than this PR, measured now that the package imports — of fifteen routed ops, eight infer a schema today and could become opaque given a fake; two more (batched_gemm_a8w8_mxscale, _bpreshuffle) are already opaque because their guard is outside the route. The remaining five need type annotations first, and pa_persistent_fwd takes a QuantType enum that schema inference rejects.

Environment

gfx942, torch 2.9.1, AITER at this branch's base with its built kernels. flydsl upgraded to 0.5.0 locally to let the tree import; that is a change to this box, not to the repo.

@zufayu
zufayu merged commit 57b5d7e into main Nov 12, 2026
@zufayu
zufayu deleted the codex/warm-config-at-import branch November 12, 2026 04:48
zufayu added a commit that referenced this pull request Nov 12, 2026
#7848 landed the eager config snapshot in the same file this branch
rewrites. Merged cleanly; verified both halves survive rather than
assuming an absent conflict marker means an intact result -- the snapshot
is built at import, _as_custom_op is intact, and the first registered op
still comes back as an OpOverloadPacket.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
zufayu added a commit that referenced this pull request Nov 12, 2026
#7850)

* feat(plugin): let a routed op be an opaque node instead of traced Python

torch.compile traces through the routing wrapper, so a routed op puts this
module's dispatch inside the caller's graph. Anything in there that Dynamo
cannot follow splits that graph, and a framework permitting one backend
invocation per compilation then fails. #7848 removed the one such thing on
the fast path; with a provider enabled, dispatch continues into provider
selection and the break comes back.

`route(..., fake=...)` registers the op with torch instead: one node, and
nothing inside it reachable from the caller's graph. Measured, provider
enabled, against a counting backend:

    traced wrapper   2 graphs
    registered       1 graph, provider's kernel served

Registration is opt-in per op and failure to register is an outcome rather
than an error. No fake, an unannotated parameter, an argument no schema can
express -- each leaves the op exactly as it is today. Nothing regresses for
any op at any point, and an op becomes protected the day it acquires what
registration needs. No op in the tree passes `fake=` yet, so this change
alters the behaviour of nothing; it makes the behaviour available.

`mutates_args="unknown"` rather than a per-op list: a list has to be right
for every op anyone adds later, and under-declaring is undefined behaviour
rather than an error. "unknown" is never wrong, only conservative. It was
not observed to change results either way under inductor, and the comment
says so rather than claiming it prevents a failure that has been seen.

tests/test_plugin_custom_op.py, in subprocesses, pins: one graph for a
registered op; an op writing through an argument still correct with the
compiled region reading that buffer; a provider still served; and each way
of being unregisterable leaving the op traced and correct.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Register the first routed op, and fix a test that proved nothing

`fused_qk_norm_rope_cache_quant_shuffle` returns None and writes through
its arguments, so its fake is `return None` -- correct by inspection
rather than by shape arithmetic, which makes it the right op to be first.
Its public name is the routed function with no guard above it, so it was
one of the ops whose dispatch a caller's compiled graph could reach.

A/B against op_tests/test_fused_qk_norm_rope_cache_quant.py on gfx942:
6 failed, 5 passed, 4 errors both with and without the registration --
identical, so the failures are this box (a gfx950 path) and the change
introduces none of them. A deselected test is broken on main: it takes
eleven positional arguments pytest never supplies.

The test fix matters more than it looks. tests/test_plugin_custom_op.py
asked `hasattr(op, "_schema")` to tell a registered op from a Python
wrapper. `_schema` is on an OpOverload, not on the OpOverloadPacket that
route hands back, so that answered False either way and the assertion
that an unregisterable op stays traced passed without checking anything.
Now it asks the type, and forcing registration without a fake fails it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
vpietila-amd pushed a commit that referenced this pull request Nov 12, 2026
…ch (#7848)

A routed op's first call in a process acquired a lock and read an
environment variable, because the config snapshot was built lazily. Dynamo
can trace neither, so that call graph-breaks: the region compiles as two
graphs instead of one, and a framework that permits a single backend
invocation per compilation is handed a second and asserts.

Measured with a counting backend, each case in its own process:

    plain function                      1 graph
    routed op, snapshot unbuilt         2 graphs   <- the break
    routed op, snapshot built           1 graph

Nothing else on the dispatch path breaks, so building the snapshot at
import is the whole fix. The hot path was already a plain global read for
every call after the first; this makes the first one no different.

The lazy branch in _config() stays, because _reset_for_testing clears the
snapshot and a test that clears it must not become the call that rebuilds
it inside somebody's graph.

tests/test_plugin_compile.py pins both halves -- one graph per compiled
region, and nothing left to build when dispatch starts -- in subprocesses,
since the breakage is first-call-only and torch._dynamo state is global.
Removing the fix fails both.

This is the coverage #7465 and #7728 lacked: every test of the interface
so far has been eager, and an eager test cannot see a graph boundary.

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
vpietila-amd pushed a commit that referenced this pull request Nov 12, 2026
#7850)

* feat(plugin): let a routed op be an opaque node instead of traced Python

torch.compile traces through the routing wrapper, so a routed op puts this
module's dispatch inside the caller's graph. Anything in there that Dynamo
cannot follow splits that graph, and a framework permitting one backend
invocation per compilation then fails. #7848 removed the one such thing on
the fast path; with a provider enabled, dispatch continues into provider
selection and the break comes back.

`route(..., fake=...)` registers the op with torch instead: one node, and
nothing inside it reachable from the caller's graph. Measured, provider
enabled, against a counting backend:

    traced wrapper   2 graphs
    registered       1 graph, provider's kernel served

Registration is opt-in per op and failure to register is an outcome rather
than an error. No fake, an unannotated parameter, an argument no schema can
express -- each leaves the op exactly as it is today. Nothing regresses for
any op at any point, and an op becomes protected the day it acquires what
registration needs. No op in the tree passes `fake=` yet, so this change
alters the behaviour of nothing; it makes the behaviour available.

`mutates_args="unknown"` rather than a per-op list: a list has to be right
for every op anyone adds later, and under-declaring is undefined behaviour
rather than an error. "unknown" is never wrong, only conservative. It was
not observed to change results either way under inductor, and the comment
says so rather than claiming it prevents a failure that has been seen.

tests/test_plugin_custom_op.py, in subprocesses, pins: one graph for a
registered op; an op writing through an argument still correct with the
compiled region reading that buffer; a provider still served; and each way
of being unregisterable leaving the op traced and correct.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Register the first routed op, and fix a test that proved nothing

`fused_qk_norm_rope_cache_quant_shuffle` returns None and writes through
its arguments, so its fake is `return None` -- correct by inspection
rather than by shape arithmetic, which makes it the right op to be first.
Its public name is the routed function with no guard above it, so it was
one of the ops whose dispatch a caller's compiled graph could reach.

A/B against op_tests/test_fused_qk_norm_rope_cache_quant.py on gfx942:
6 failed, 5 passed, 4 errors both with and without the registration --
identical, so the failures are this box (a gfx950 path) and the change
introduces none of them. A deselected test is broken on main: it takes
eleven positional arguments pytest never supplies.

The test fix matters more than it looks. tests/test_plugin_custom_op.py
asked `hasattr(op, "_schema")` to tell a registered op from a Python
wrapper. `_schema` is on an OpOverload, not on the OpOverloadPacket that
route hands back, so that answered False either way and the assertion
that an unregisterable op stays traced passed without checking anything.
Now it asks the type, and forcing registration without a fake fails it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

3 participants