Skip to content

fix(EPv2): give a dispatch plan a default staging buffer its caller can replace - #764

Merged
jhchouuu merged 2 commits into
ROCm:mainfrom
jhchouuu:jhchou/ep-dispatch-auto-staging
Oct 14, 2026
Merged

jhchouuu merged 2 commits into
ROCm:mainfrom
jhchouuu:jhchou/ep-dispatch-auto-staging

Conversation

@jhchouuu

@jhchouuu jhchouuu commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

#703 moved the gfx125x dispatch staging into a buffer that the caller allocates and binds as staging_base. A caller that builds EpDispatchPlan directly and binds none now fails at its first dispatch. aiter main's MegaMoEGfx1250 is such a caller, and ATOM's EP+DPA path runs it (#757).

This keeps that caller working and changes nothing for callers that bind their own buffer.

A default staging_base

  • An EpDispatchPlan that stages starts with a default staging_base. "Stages" means sharedBytes > 0, the same test EpDispatchSpec::LaunchRaw makes.
  • A caller's bind(staging_base=...) replaces the default before it is ever read. mori's op layer and aiter #6430 both bind their own buffer, so they never touch it.
  • Left in place, the default resolves when the first launch builds the args struct:
    • It takes a buffer sized by ep_staging_bytes() from what C++ resolved (plan.info).
    • The buffer is shared by the unbound plans of that layout on the device, as an op's plans share the op's.
    • The buffer's address replaces the default. Later launches therefore reuse the cached struct like any bound plan, and launch() is the generated one.
  • Allocation is refused inside a graph capture. A plan first launched inside a capture reuses the buffer its layout already has.
  • A FutureWarning names the caller's line.
  • The default holds its plan weakly and the plan holds the buffer, so both are freed by refcount. close() also releases the buffer.
  • A launch group compares the default without resolving it: by its layout, or by its buffer's address once the layout has one. Building a group allocates nothing.
  • The layout resolves maxRecv as EpMaxRecv does, so maxRecv=0 and worldSize * maxTokPerRank share a buffer.

plan_api: two consistency fixes the default exposed

  • On the cached launch path, a non-int bound default was written after the launch arguments. An argument shadowing it therefore won only on the launch that built the struct. It now wins on every launch, as it already did against an int default.
  • LaunchGroup compared an int default by value but a pointer-like default as a tagged address, so a tensor and its data_ptr() disagreed. Both now compare by the address that launch writes.

Test

4x gfx1250, rocm/fw-bringup-mlperf:gfx1250-atom-20261010, mori c770a632 + this PR.

result
tests/python/ops/dispatch_combine_v2/ 53 passed, 18 skipped (EP8 needs 8 GPUs). The new tests fake sharedBytes, so both branches also run on gfx942/gfx950
aiter main MegaMoE: mori dispatch + fused combine, bench_mega_moe.py, 58 layers, 1536 tokens/rank Before: RuntimeError: ... staging_base was not bound at the first dispatch. After: PASS, with one FutureWarning per rank pointing at aiter's plan.launch
aiter #6430 (binds its own buffer), same bench, topk 9 PASS, no warning. Logits identical with and without this PR; per-layer time within run-to-run noise
mori op layer (--combine base) PASS, no warning
launch() host cost after the first launch, ABI call stubbed About 800 ns each for the generated plan, a caller-bound plan and a fallback plan
Unclosed, launched fallback plan with gc disabled Plan and buffer freed by refcount

Fixes #757

🤖 Generated with Claude Code

…an replace

ROCm#703 made the gfx125x dispatch stage through a buffer its caller allocates with
ep_staging_bytes() and binds as staging_base, and refuses an unbound launch on
the host. Callers that build EpDispatchPlan directly and predate it -- aiter
main's MegaMoEGfx1250, which ATOM's EP+DPA path runs -- now fail at their first
dispatch (ROCm#757).

An EpDispatchPlan that stages (sharedBytes > 0, the test
EpDispatchSpec::LaunchRaw makes) now starts with a default staging_base. A
caller that binds its own replaces it before it is ever read, so mori's op layer
and aiter #6430 are unaffected. Left in place, the default resolves at the first
launch: it takes a buffer sized by ep_staging_bytes() from what C++ resolved
(plan.info), shared by the unbound plans of that layout on the device as an
op's plans share the op's, and binds its address in its own place, so later
launches reuse the cached launch struct like any bound plan. launch() itself is
the generated one. The default holds its plan weakly and the plan holds the
buffer, so both go by refcount; close() gives it back too. The buffer is not
allocated inside a graph capture, and a FutureWarning names the caller's line.

plan_api:
- On the cached path a non-int bound default was written after the launch
  arguments, so an argument shadowing it won only on the launch that built the
  struct. It now wins every time, as an int default already did.
- LaunchGroup compared an int default by value but a pointer-like one as a
  tagged address, so a tensor and its data_ptr() -- or a resolved and an
  unresolved fallback -- disagreed. Both now compare by the address launch
  writes.

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

@kawhil-amd kawhil-amd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Five comments below, ranked P1–P5. P1 and P2 are worth resolving before merge; P3–P5 are minor / nits.

continue
try:
out[name] = ("p", _as_ptr(v))
out[name] = _as_ptr(v)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 [important]: _bound_defaults resolves _FallbackStaging as a side effect

_as_ptr calls .data_ptr() on anything that has the method, including _FallbackStaging. That means calling make_launch_group([a, b]) before either plan has launched will allocate the fallback buffer, issue a FutureWarning, and mutate b._defaults["stagingBase"] to an int — all inside what looks like a comparison operation, and with the warning pointing at make_launch_group rather than the launch site.

One option: give _FallbackStaging a different method name (e.g. resolve()) so it doesn't satisfy the .data_ptr() duck-type, and let _bound_defaults detect it explicitly and fall through to ("o", id(v)). The group then compares unresolved fallbacks by identity, which is conservative but safe — they only merge once both have launched and their defaults have settled to ints.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d0f581e, though not by comparing by identity. With identity, two unlaunched plans of one layout could not be grouped until both had launched. That is the order dependence an earlier round removed.

Instead, _bound_defaults asks a default for group_key() when it has one. The fallback answers with its buffer's address once its layout has a buffer, and with the layout until then. Building a group now allocates nothing, warns nothing and rebinds nothing.

test_a_group_compares_defaults_by_the_address_launch_writes covers three cases:

  • two unlaunched plans, with FutureWarning set to error and the cache asserted empty;
  • a group where only one plan has launched, with the other still holding its fallback afterwards;
  • a different layout, which is still refused.

key = (
device,
info["worldSize"],
info["maxRecv"],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 [important]: raw maxRecv=0 in the cache key breaks buffer sharing

Two plans with maxRecv=0 and maxRecv=worldSize*maxTokPerRank have the same effective buffer size (C++ EpMaxRecv resolves them identically), but this key treats them as different layouts, so each allocates its own buffer and the sharing guarantee is silently lost.

Suggestion: normalise the key, e.g.:

effective_recv = max_recv if max_recv > 0 else world_size * max_tok_per_rank
key = (device, world_size, effective_recv, scale_bytes, max_tok_per_rank)

or use the computed nbytes as the key dimension (size is what actually determines buffer compatibility).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d0f581e: the key resolves maxRecv with EpMaxRecv's ternary, so maxRecv=0 and worldSize * maxTokPerRank share one buffer. test_unbound_plans_of_one_layout_share_one_buffer now includes a max_recv=0 plan.

I kept the layout as the key rather than nbytes. Plans of different layouts that happen to need the same size still get separate buffers, as before.

if w in arg_names
# A launch argument wins over the default it shadows, as in
# `merged` above; re-writing the default after it would not.
and w not in passed and (not isinstance(v, int) or w in arg_blobs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 [minor]: passed is a one-shot snapshot; a non-int default is permanently shadowed

passed is computed from the args present at struct-build time and used to exclude keys from _dyn_defs permanently. If arg X (a non-int default) is passed on the first launch, it is excluded from _dyn_defs forever. A subsequent launch that omits X will leave the struct at the first launch's value, silently ignoring the bound default.

For staging_base this is harmless (the address is stable after resolution). For other non-int defaults it is a latent correctness trap. The tests only cover the all-or-nothing cases (always-pass or never-pass). Worth a comment noting the constraint, or a test that documents the behaviour for the alternating case so it doesn't surprise a future reader.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The struct cache is keyed on the set of argument names (shape = tuple(args) in _launch_buf). A launch that omits an argument it passed before is a different set, so the struct is rebuilt from merged, which holds the default, and passed / _dyn_defs are recomputed along with it. The alternating case therefore writes the default again.

test_a_launch_argument_wins_over_a_bound_default_every_launch now runs pass → pass (cached) → omit → pass and checks the field after each launch.

def _caller_stacklevel() -> int:
# The caller's warnings.warn stacklevel for the first frame outside mori.
level, frame = 1, sys._getframe(1)
while frame is not None and frame.f_code.co_filename.startswith(_BINDING_DIRS):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4 [minor]: startswith without a path separator can over-match

If a binding-dir path is /foo/mori/jit/v2, a file at /foo/mori/jit/v2_extra/bar.py would pass the check and be incorrectly skipped. Adding os.sep to each entry fixes it:

_BINDING_DIRS = tuple(d + os.sep for d in (
    os.path.dirname(plan_api.__file__),
    os.path.dirname(__file__),
))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d0f581e: each entry is os.path.join(dirname, ""), so it ends with the separator.


def close(self) -> None:
super().close()
self._fallback_staging = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5 [nit]: close() before first launch sets a non-existent attribute

_fallback_staging is only assigned in _FallbackStaging.data_ptr(), so a plan that is closed before its first launch has no such attribute. Setting self._fallback_staging = None quietly creates it, which works but is easy to misread as releasing something that was held.

A hasattr guard or initialising self._fallback_staging = None in __init__ (alongside the bind) would make the intent explicit. A test covering close() before launch would also confirm no crash and no stale state.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d0f581e: __init__ sets _fallback_staging = None. Added test_closing_an_unlaunched_plan_allocates_nothing.

Review follow-ups on the staging fallback:
- A launch group compares a fallback staging_base by its group_key(): the
  buffer's address once its layout has one, the layout until then. Building a
  group no longer allocates, warns or rebinds, and two unlaunched plans of one
  layout still group.
- The fallback key resolves maxRecv as EpMaxRecv does, so maxRecv=0 and
  worldSize * maxTokPerRank share one buffer.
- Each _BINDING_DIRS entry ends with a separator, so a sibling directory with
  the same prefix is not taken for mori's.
- EpDispatchPlan starts with _fallback_staging = None, so close() before the
  first launch releases nothing it did not hold.

Tests cover each, and a launch that omits an argument it passed before.

Co-Authored-By: Claude Opus 5.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.

[EPv2] staging_base binding required by #703 breaks callers that go through aiter MegaMoE (ATOM EP4+DPA)

2 participants