Repository navigation
fix(EPv2): give a dispatch plan a default staging buffer its caller can replace - #764
Conversation
…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
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
FutureWarningset 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"], |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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__),
))There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
Summary
#703 moved the gfx125x dispatch staging into a buffer that the caller allocates and binds as
staging_base. A caller that buildsEpDispatchPlandirectly and binds none now fails at its first dispatch. aiter main'sMegaMoEGfx1250is 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_baseEpDispatchPlanthat stages starts with a defaultstaging_base. "Stages" meanssharedBytes > 0, the same testEpDispatchSpec::LaunchRawmakes.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.ep_staging_bytes()from what C++ resolved (plan.info).launch()is the generated one.FutureWarningnames the caller's line.close()also releases the buffer.maxRecvasEpMaxRecvdoes, somaxRecv=0andworldSize * maxTokPerRankshare a buffer.plan_api: two consistency fixes the default exposedLaunchGroupcompared an int default by value but a pointer-like default as a tagged address, so a tensor and itsdata_ptr()disagreed. Both now compare by the address that launch writes.Test
4x gfx1250,
rocm/fw-bringup-mlperf:gfx1250-atom-20261010, moric770a632+ this PR.tests/python/ops/dispatch_combine_v2/sharedBytes, so both branches also run on gfx942/gfx950bench_mega_moe.py, 58 layers, 1536 tokens/rankRuntimeError: ... staging_base was not boundat the first dispatch. After: PASS, with oneFutureWarningper rank pointing at aiter'splan.launch--combine base)launch()host cost after the first launch, ABI call stubbedgcdisabledFixes #757
🤖 Generated with Claude Code