Skip to content

[Triton/Gluon] [FlyDSL] [CI] [Bugfix] Never share scratch buffers whose zero fill was recorded into a graph - #7126

Merged
vgokhale merged 11 commits into
mainfrom
fix-capture-scoped-zeroed-scratch
Nov 5, 2026
Merged

vgokhale merged 11 commits into
mainfrom
fix-capture-scoped-zeroed-scratch

Conversation

@Fangzhou-Ai

@Fangzhou-Ai Fangzhou-Ai commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Cached zero-initialized scratch buffers (split-K semaphores/counters, zero bias, top-k workspaces) whose first request happens inside a CUDA graph capture get their torch.zeros fill recorded into that one graph instead of executed. The buffer is still cached and handed to every later graph and eager caller with the same key, which then run on uninitialized memory until the recording graph happens to replay. For the gfx950 FlyDSL a16w16 split-K GEMM this is a deadlock. This PR adds graph_alloc.capture_scoped_cache and uses it at every such site.

Root cause

  • get_split_k_buffers(stream, device, split_k) is cached per stream. In vLLM's DeepSeek-V4.1 on ROCm, the indexer weights_proj GEMM (bf16 [M, 5120] x [5120, 32], FlyDSL split-K 8) first runs on an aux stream inside graph capture, so its semaphores are first allocated mid-capture. A traced server shows exactly one such site per rank. The comment below gives the call path.
  • The split-K protocol (SplitKProtocol.take_ticket) derives the launch tag as ticket // split_k + 1 and relies on every semaphore word being a multiple of split_k. Uninitialized words are not (in the persistent pool on a GPU that ran a previous process, all 256 words were non-zero, e.g. 2019269357943 ≡ 7 mod 8). The owner split and the waiting splits then derive different tags and spin forever.
  • Observed as intermittent hangs serving DeepSeek-V4.1-Flash (TP1×DP2, CUDA graphs) on MI355X: rocgdb shows the only resident waves on both GPUs are hgemm_bf16_..._kst4_... spinning in wait_until_initialized. It only reproduces when the recycled memory happens to be dirty, hence the intermittency.
  • The same pattern affects the other cached zero-initialized scratch: _get_preshuffle_split_buffers, gfx1250 _split_k_counters, the ASM a16w16 semaphore workspace, the a8w8 zero-bias buffer, the top-k workspaces / counters, the gfx950 batched a8w8 split counters, the gfx950 mxscale fused split-K (_fsk) arrival counters, and the Triton afp8wfp8 split counters. For example, a zero bias first built mid-capture would feed garbage bias to every other graph.

Fix

capture_scoped_cache (in aiter/utility/graph_alloc.py) replaces functools.cache / lru_cache at these sites:

  • Buffer built eagerly: it is already initialized, so it is cached and shared with captures exactly as before. This is the common case: no new graph nodes and no extra memory.
  • Buffer first built inside a capture: it is cached under that capture's ID (hipStreamGetCaptureInfo). Only the graph that recorded its zero fill ever uses it, and that graph re-zeroes it on every replay before using it, so correctness no longer depends on which graph replays first.
  • Which capture counts: the capture status and ID come from the stream the builder's fill runs on. That is the builder's stream argument, else the current stream of its device argument, not the ambient current device.
  • Preshuffle split-K buffers are split. The fully overwritten 16 MiB workspace stays one per (device, stream); only its zero-initialized semaphore is capture-scoped.
  • The AOT dry run answers "not capturing". The wheel build replays every tuned row on FakeTensors with the GPUs hidden, where hipStreamGetCaptureInfo fails with error 100; dry_run now stubs _capture_id as it already does is_current_stream_capturing. A new test in tests/test_flydsl_aot_replay.py covers it.
  • mxscale fused split-K buffers are split the same way. The partials workspace stays under functools.cache; only the per-tile arrival counters are capture-scoped. Both now come from persistent_alloc (before, a bare torch.zeros from whichever pool was active). The compile-only AOT path allocates directly.
  • The caches become unbounded. This also removes LRU eviction of a buffer that a captured graph may still point at; evicted memory would go back to the pool and be reused. Keys are bounded by streams × split_k / shapes. Capture-scoped entries cost one small zeroed buffer per capture that first needs it.
  • The Triton afp8wfp8 site keeps its pre-2.10 torch behavior: it returns None mid-capture unless an eager buffer exists.

Tests

op_tests/test_graph_alloc.py:

  • test_capture_scoped_cache_keeps_capture_built_buffers_private: capture-built buffers are per capture and never returned eagerly; eager-built ones are shared with captures.

  • test_capture_scoped_cache_checks_the_target_device_not_the_current_one (2 GPUs): a cuda:1 builder called with cuda:0 current while cuda:1 captures is capture-scoped.

  • test_split_k_gemm_whose_semaphores_are_first_used_inside_a_capture: an end-to-end repro (gfx950) that follows vLLM's pattern:

    • an eager warmup on the default stream only;
    • two captures of different M (largest first), each forking the real [M, 5120] x [5120, 32] split-K GEMM onto an aux stream with an event fork/join;
    • the aux stream's pool memory deliberately dirtied first;
    • the graph without the recorded zero fill replayed first.

    It checks results with checkAllclose at test_flydsl_hgemm's bf16 split-K tolerances. It runs in a subprocess with a timeout, because the failure mode is a GPU spin.

  • test_fused_split_k_gemm_whose_counters_are_first_used_inside_a_capture: the gfx950 mxfp8 _fsk GEMM (M=16, N=2304, K=6144, a tuned DSv4 row) in two aux-stream captures that share one graph pool. An earlier graph in that pool leaves 7s where the counters land, and the graph without the recorded zero fill replays first. Nothing spins, so it runs in-process.

.github/scripts/aiter_test.sh now runs op_tests/test_graph_alloc.py through pytest (the file has no __main__). The PR also carries #7039's one-line split_k fix for the existing private-pool test, so CI is green.

Results on MI355X (gfx950), torch 2.10 ROCm:

stock this PR
split-K repro, vLLM multi-stream pattern hang 5/5 (killed at 90 s) pass 3/3
pytest op_tests/test_graph_alloc.py (2 GPUs) n/a 7 passed
mxscale _fsk capture test fail 3/3 (~98% mismatch) pass 3/3
test_gemm_a8w8_mxfp8_bpreshuffle.py -mnk 1/8/16,2304,6144 (_fsk) err 0, latency at tuned values
pytest op_tests/triton_tests/gemm/basic/test_gemm_afp8wfp8.py -k "first_use_inside_capture or concurrent_streams or replay" 12 passed (before the rebase onto 96175f6)

End to end: vLLM DeepSeek-V4.1-Flash DPATP2 AgentX runs that previously stalled at first traffic or within ~20 min ran clean with the equivalent workaround applied. That covers 9 one-hour runs: DP2 c32 to c384, and DP4 c128/c256.

PR Self-Review

Reviewed revision

0b80b631f

Behavior and intent

Cached scratch buffers that kernels need initialized (zeroed split-K semaphores/counters, zero bias, top-k workspaces) are no longer shared when their first request happens inside a CUDA graph capture.

  • Why the existing behavior was insufficient: torch.zeros inside a capture is recorded into that one graph, not executed. Yet the buffer was cached and handed to every other graph and eager caller, which then ran on uninitialized memory. The gfx950 a16w16 split-K ticket protocol then derives mismatched launch tags and spins forever, as seen serving DeepSeek-V4.1 in vLLM.
  • What changes: capture_scoped_cache keeps a capture-built buffer private to its capture, keyed by the HIP capture ID of the stream the fill runs on. That graph re-zeroes it on every replay. Eagerly built buffers are shared exactly as before.

Validation and unverified scope

  • Validated (MI355X/gfx950, torch 2.10 ROCm):
    • pytest op_tests/test_graph_alloc.py: 7 passed (2 GPUs);
    • the mxscale _fsk capture test fails 3/3 with the old builder and passes with this PR; the _fsk mxfp8 GEMM shapes still pass with err 0;
    • AOT compile-only of an _fsk config;
    • the FlyDSL GEMM AOT replay (CPU, GPUs hidden): a split-K bf16 row replays through dry_run, and tests/test_flydsl_aot_replay.py passes 7/7;
    • the split-K repro hangs on stock AITER and passes with this PR (table above);
    • the Triton afp8wfp8 capture/stream/replay subset: 12 passed;
    • vLLM DSv4.1 DPATP2 one-hour serving runs with the equivalent workaround: no stalls in 9 runs.
  • Unverified:
    • gfx942 and gfx1250 (the gfx1250 _split_k_counters and gfx942 paths are only exercised by CI);
    • torch < 2.10, where persistent_alloc already refuses mid-capture allocation;
    • long-lived processes that destroy and recapture many graphs: capture-scoped entries are not freed when a graph is destroyed. Each is one small zeroed buffer.

PR-specific question

  • Why this lives in graph_alloc and is applied at every zero-initialized site: every such site has the same failure mode. Fixing only the split-K site would leave, for example, the zero-bias buffer feeding garbage bias to graphs.
  • Why a vLLM-side workaround is not enough: the capture-time key belongs to an aux stream that is only entered inside the capture, so pre-creating buffers before capture_begin cannot cover it.
  • Why not key by device alone: default and aux streams run split-K GEMMs concurrently inside one graph.

Changed op tests

  • op_tests/test_graph_alloc.py:
    • test_capture_scoped_cache_keeps_capture_built_buffers_private and ..._checks_the_target_device_not_the_current_one cover the cache contract. They fail before this PR because the helper does not exist; a stock builder would return the capture-built buffer to eager callers.
    • test_split_k_gemm_whose_semaphores_are_first_used_inside_a_capture hangs (times out) before this PR. No existing test captures a split-K GEMM whose semaphores are first allocated mid-capture on a dirty pool; test_flydsl_hgemm.py only runs eagerly or with buffers already cached.
    • test_fused_split_k_gemm_whose_counters_are_first_used_inside_a_capture fails before this PR (the graph without the zero fill leaves its tiles unwritten). It is the only test of the _fsk counters' first use inside a capture.
    • test_every_cached_scratch_comes_from_the_private_pool now also covers the mxscale fused split-K buffers.
    • The private-pool test's split_k argument fixes an existing failure (same change as [Test] Pass split_k to get_split_k_buffers in test_graph_alloc #7039).
    • The file is now dispatched through pytest in CI; before, CI ran it with python3 and executed nothing.
  • op_tests/triton_tests/gemm/basic/test_gemm_afp8wfp8.py: isolation only. test_fused_split_k_first_use_inside_capture swapped in a fresh dict for the removed _split_counters_by_stream; it now swaps in a fresh capture_scoped_cache around the same builder. Coverage is unchanged and not redundant: it is the only test of the fused Triton split-K counters' first use inside a capture.

Duplicate check

#7039 (test signature fix) and #6712 (AOT semaphore dtype) touch the same area but not the capture-recorded zero fill; #5393/#5430 moved these buffers to the persistent pool but did not address initialization inside a capture.

AI assistance (Claude) was used to investigate and write this change; I reviewed it and ran the tests above.

🤖 Generated with Claude Code

@Fangzhou-Ai
Fangzhou-Ai requested a review from a team October 29, 2026 04:50
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
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 gfx1201 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 7126 --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.

@github-actions github-actions Bot changed the title [FlyDSL] [Bugfix] Never share scratch buffers whose zero fill was recorded into a graph [Triton/Gluon] [FlyDSL] [Bugfix] Never share scratch buffers whose zero fill was recorded into a graph Oct 29, 2026
@Fangzhou-Ai
Fangzhou-Ai marked this pull request as draft October 29, 2026 04:55
@Boss2002n
Boss2002n requested a balanced review from Copilot October 29, 2026 05:33

Copilot AI 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.

🟡 Changes recommended

Capture handling can return buffers before their zero fill runs and retain an extra 16 MiB workspace per capture even after graph destruction, while CI skips the new regression tests.

4 open issues
What changed in this PR

This PR prevents AITER callers from sharing scratch buffers whose zero initialization was recorded into another CUDA graph. It addresses initialization safety beyond the existing private-memory-pool protection.

Changes:

  • Add capture-scoped caching while preserving reuse of eagerly initialized buffers.
  • Apply the cache to GEMM and top-k scratch allocations across Triton, FlyDSL, and ASM wrappers.
  • Add capture-isolation and split-K hang regressions, and update the Triton cache-isolation test.
File Description
op_tests/​triton_tests/​gemm/​basic/​test_gemm_afp8wfp8.py Reset the new counter cache for capture testing.
op_tests/​test_graph_alloc.py Add capture-isolation and subprocess hang regressions.
aiter/​utility/​graph_alloc.py Add capture-ID lookup and capture-scoped caching.
aiter/​ops/​triton/​gemm/​basic/​gemm_afp8wfp8.py Scope split counters by capture; preserve older-torch fallback.
aiter/​ops/​topk.py Apply capture-scoped caching to top-k workspace.
aiter/​ops/​gemm_op_a8w8.py Isolate capture-initialized zero-bias buffers.
aiter/​ops/​gemm_op_a16w16.py Isolate ASM semaphore workspace by capture.
aiter/​ops/​flydsl/​kernels/​topk/​topk_per_row_decode.py Scope cached top-k counters by capture.
aiter/​ops/​flydsl/​kernels/​gemm_a16w16_gfx950.py Scope gfx950 split-K buffers by capture.
aiter/​ops/​flydsl/​gemm_kernels.py Apply capture-scoped caching to preshuffle scratch.
aiter/​ops/​flydsl/​gemm_a16w16_gfx1250.py Scope gfx1250 split-K counters by capture.
aiter/​ops/​flydsl/​batched_gemm_a8w8_gfx950.py Scope batched GEMM split counters by capture.

Review effort: Balanced

Comment thread aiter/ops/flydsl/gemm_kernels.py Resolved
Comment thread aiter/utility/graph_alloc.py Resolved
Comment thread op_tests/test_graph_alloc.py Resolved
Comment thread op_tests/test_graph_alloc.py Resolved
@aiter-bot

aiter-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 0b80b631f09a · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

Cached scratch buffers that kernels need initialized (zeroed split-K semaphores/counters, zero bias, top-k workspaces) are no longer shared when their first request happens inside a CUDA graph capture.
- **Why the existing behavior was insufficient:** `torch.zeros` inside a capture is recorded into that one graph, not executed. Yet the buffer was cached and handed to every other graph and eager caller, which then ran on uninitialized memory. The gfx950 a16w16 split-K ticket protocol then derives mismatched launch tags and spins forever, as seen serving DeepSeek-V4.1 in vLLM.
- **What changes:** `capture_scoped_cache` keeps a capture-built buffer private to its capture, keyed by the HIP capture ID of the stream the fill runs on. That graph re-zeroes it on every replay. Eagerly built buffers are shared exactly as before.

Validation and unverified scope

- **Validated (MI355X/gfx950, torch 2.10 ROCm):**
  - `pytest op_tests/test_graph_alloc.py`: 7 passed (2 GPUs);
  - the mxscale `_fsk` capture test fails 3/3 with the old builder and passes with this PR; the `_fsk` mxfp8 GEMM shapes still pass with err 0;
  - AOT compile-only of an `_fsk` config;
  - the FlyDSL GEMM AOT replay (CPU, GPUs hidden): a split-K bf16 row replays through `dry_run`, and `tests/test_flydsl_aot_replay.py` passes 7/7;
  - the split-K repro hangs on stock AITER and passes with this PR (table above);
  - the Triton afp8wfp8 capture/stream/replay subset: 12 passed;
  - vLLM DSv4.1 DPATP2 one-hour serving runs with the equivalent workaround: no stalls in 9 runs.
- **Unverified:**
  - gfx942 and gfx1250 (the gfx1250 `_split_k_counters` and gfx942 paths are only exercised by CI);
  - torch < 2.10, where `persistent_alloc` already refuses mid-capture allocation;
  - long-lived processes that destroy and recapture many graphs: capture-scoped entries are not freed when a graph is destroyed. Each is one small zeroed buffer.

PR-specific question

- **Why this lives in `graph_alloc` and is applied at every zero-initialized site:** every such site has the same failure mode. Fixing only the split-K site would leave, for example, the zero-bias buffer feeding garbage bias to graphs.
- **Why a vLLM-side workaround is not enough:** the capture-time key belongs to an aux stream that is only entered inside the capture, so pre-creating buffers before `capture_begin` cannot cover it.
- **Why not key by device alone:** default and aux streams run split-K GEMMs concurrently inside one graph.

Changed op tests

- **`op_tests/test_graph_alloc.py`:**
  - `test_capture_scoped_cache_keeps_capture_built_buffers_private` and `..._checks_the_target_device_not_the_current_one` cover the cache contract. They fail before this PR because the helper does not exist; a stock builder would return the capture-built buffer to eager callers.
  - `test_split_k_gemm_whose_semaphores_are_first_used_inside_a_capture` hangs (times out) before this PR. No existing test captures a split-K GEMM whose semaphores are first allocated mid-capture on a dirty pool; `test_flydsl_hgemm.py` only runs eagerly or with buffers already cached.
  - `test_fused_split_k_gemm_whose_counters_are_first_used_inside_a_capture` fails before this PR (the graph without the zero fill leaves its tiles unwritten). It is the only test of the `_fsk` counters' first use inside a capture.
  - `test_every_cached_scratch_comes_from_the_private_pool` now also covers the mxscale fused split-K buffers.
  - The private-pool test's `split_k` argument fixes an existing failure (same change as #7039).
  - The file is now dispatched through pytest in CI; before, CI ran it with `python3` and executed nothing.
- **`op_tests/triton_tests/gemm/basic/test_gemm_afp8wfp8.py`:** isolation only. `test_fused_split_k_first_use_inside_capture` swapped in a fresh dict for the removed `_split_counters_by_stream`; it now swaps in a fresh `capture_scoped_cache` around the same builder. Coverage is unchanged and not redundant: it is the only test of the fused Triton split-K counters' first use inside a capture.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

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

@Fangzhou-Ai

Copy link
Copy Markdown
Contributor Author

Where vLLM's multi-stream reaches this GEMM, and why only inside a capture. I updated the e2e test to follow this pattern (6e0c9e6).

I traced the first in-capture request for split-K buffers in a DeepSeek-V4.1-Flash DPATP2 server (vLLM main 65ff14056a, MI355X). Each rank has exactly one site:

split_k=8 first request DURING CAPTURE on <aux stream>  shape=((1024, 5120), (5120, 32))
  deepseek_v41/amd/rocm.py  forward -> _forward_csa2_full
  multi_stream_utils.py     execute_in_parallel  (with torch.cuda.stream(aux_streams[i]))
  deepseek_v41/amd/rocm.py  indexer_weights_chain -> indexer.weights_proj(hidden_states)
  linear.py                 UnquantizedLinearMethod -> aiter tuned_gemm -> FlyDSL hgemm (ks8)
  1. The model owns three aux streams. They are created once in model.py#L522.
  2. In CSA layers, three GEMM chains overlap. _forward_csa2_full runs the Q projection on the default stream, and the compressor and the indexer weights_proj (L774) on aux streams 0 and 1. The fork/join is an event pair in execute_in_parallel. weights_proj is an unquantized bf16 linear, 5120 → 32 heads, and AITER's tuned config sends it to the FlyDSL a16w16 split-K kernel (t32x32x128x5_ks8).
  3. The overlap only exists inside graph capture on ROCm. RocmPlatform.enable_multi_stream_overlap returns true only while capturing, or for metadata without eager breaks (full graph or the profile run). Its docstring gives the reason: runtime HIP event waits in this overlap can hang, so forks are allowed only where they become static graph edges. Everywhere else, including the eager warmup before each capture, the layer takes the sequential fallback on the default stream.
  4. So the aux stream's split-K semaphores are first allocated mid-capture. get_split_k_buffers is cached per stream. In eager mode weights_proj runs on the default stream, whose buffers are properly zeroed. The first time it runs on aux stream 1, at the largest capture size, its (stream, split_k=8) key is new, so torch.zeros is recorded into that one graph instead of executing.
    • Every smaller capture size then reuses the same cached buffer without a zero fill.
    • In serving, whichever smaller-batch graph replays before the largest one runs the ticket protocol on uninitialized memory.
    • On a GPU that previously ran another process, that memory is non-zero (e.g. 2019269357943 ≡ 7 mod 8). The owner and waiter splits derive different launch tags and spin. rocgdb at the hang shows only hgemm_bf16_t32x32x64x9_kst4_* waves resident on both GPUs.
  5. This explains the symptoms. The hang is intermittent because it depends on recycled memory contents and on which batch-size graph replays first. It started with the FlyDSL bf16 tuned configs that use split-K. Disabling QuickReduce or other comm changes didn't affect it.

Why fix it in AITER rather than in vLLM:

  • Pre-creating buffers before capture_begin doesn't work, because the capture-time key belongs to an aux stream that is only entered inside the capture. I tried this first and it still allocated mid-capture.
  • Keying by device alone is unsafe, because the default and aux streams run split-K GEMMs concurrently inside the graph.
  • capture_scoped_cache gives a buffer first built inside a capture to that capture only. That graph zeroes it on every replay, and eagerly built buffers are shared as before.

Updated test (test_split_k_gemm_whose_semaphores_are_first_used_inside_a_capture):

  • an eager warmup on the default stream only;
  • two captures (M = 1024, then 512) that fork the real [M, 5120] x [5120, 32] split-K-8 GEMM onto an aux stream with an event fork/join;
  • the aux stream's pool memory dirtied first;
  • the smaller-M graph, which has no zero fill, replayed first.

On MI355X: stock AITER hangs 3/3 (killed at 90 s); this PR passes 3/3.

@Fangzhou-Ai
Fangzhou-Ai marked this pull request as ready for review October 29, 2026 22:24
Fangzhou-Ai and others added 3 commits October 29, 2026 22:27
…orded into a graph

Cached zero-initialized scratch (split-K semaphores and counters, zero bias,
top-k workspaces) is built lazily with torch.zeros under persistent_alloc. When
the first request for a key comes from inside a CUDA graph capture, e.g. a
stream only seen while capturing, the zero fill is recorded into that graph
instead of running. The buffer is then cached and handed to every later graph
and eager caller with the same key, which run on uninitialized memory until the
recording graph happens to replay.

For the gfx950 a16w16 split-K GEMM this deadlocks: the launch tag is
ticket // split_k + 1, so a semaphore word that is not a multiple of split_k
makes the owner and the waiting splits derive different tags and spin forever.
Seen as intermittent hangs serving DeepSeek-V4.1-Flash with vLLM on MI355X.

Add capture_scoped_cache to graph_alloc: eagerly built buffers are cached and
shared as before; a buffer first built inside a capture is cached for that
capture only (keyed by hipStreamGetCaptureInfo's id), so the graph that zeroes
it is the only user and re-zeroes it on every replay before use. Apply it to
every cached zero-initialized scratch site. The caches become unbounded, which
also removes LRU eviction of buffers a captured graph may still point at.

Signed-off-by: Fangzhou Ai <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
….1 hits it

vLLM's ROCm DeepSeek-V4.1 CSA attention forks work onto aux streams only
inside graph capture, so the indexer weights_proj GEMM (bf16 [M, 5120] x
[5120, 32], split-K 8) first runs on an aux stream mid-capture, once per
capture size. Model that: eager warmup on the default stream only, then two
captures of different M that fork the GEMM onto an aux stream with an event
fork/join, and replay the graph without the recorded zero fill first.

Signed-off-by: Fangzhou Ai <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… workspace, CI

- capture_scoped_cache now asks HIP about the stream the builder's fill runs
  on (its stream argument, else the current stream of its device argument),
  not the ambient current device: a builder for cuda:1 called while cuda:0 is
  current and cuda:1 is capturing was treated as eager.
- Split the preshuffle split-K buffers: the fully overwritten 16 MiB
  workspace stays one per (device, stream) and is never evicted; only the
  zero-initialized semaphore is capture-scoped.
- The split-K repro checks with checkAllclose at test_flydsl_hgemm's bf16
  split-K tolerances; add a two-GPU test for the target-device case.
- Run op_tests/test_graph_alloc.py through pytest in aiter_test.sh (it has
  no __main__), and pass split_k in the private-pool test (same fix as #7039).

Signed-off-by: Fangzhou Ai <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Fangzhou-Ai
Fangzhou-Ai force-pushed the fix-capture-scoped-zeroed-scratch branch from 6e0c9e6 to 1ca541f Compare October 29, 2026 22:33
@github-actions github-actions Bot changed the title [Triton/Gluon] [FlyDSL] [Bugfix] Never share scratch buffers whose zero fill was recorded into a graph [Triton/Gluon] [FlyDSL] [CI] [Bugfix] Never share scratch buffers whose zero fill was recorded into a graph Oct 29, 2026
@github-actions github-actions Bot added the CI label Oct 29, 2026
@Fangzhou-Ai

Copy link
Copy Markdown
Contributor Author

CI note: the Triton MI35X shard 5 failure (test_batched_gemm_afp4_wfp4[...-2-256-3584-2112], one NaN row per case) is pre-existing and unrelated to this PR:

  • Same cases fail on main: main's own Triton runs fail them at this PR's base 96175f600 (run 40965021247, job 123768200508) and on later main commits.
  • No shared code: batched_gemm_afp4wfp4 and its test import nothing this PR changes.
  • Likely fix: K=2112 takes the ragged-K path, which matches [Triton/Gluon] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 #7047 (Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4).

The Standard Tests that include op_tests/test_graph_alloc.py are still pending.

Dewei-Wang-sh
Dewei-Wang-sh previously approved these changes Nov 2, 2026
@XiaobingSuper

Copy link
Copy Markdown
Contributor

No blocking issues @ 51bb039. One site of the same class is not covered; can it be folded in here?

_fused_splitk_buffers builds the fused split-K arrival counters with a bare torch.zeros. Built inside a capture, they start dirty for other graphs, the last-arrival check never matches, and the tile silently keeps stale output.

Fix as for the preshuffle split here: workspace under functools.cache, counters in a capture_scoped_cache builder under persistent_alloc.

Reachability

Same trigger as weights_proj: a key first requested inside a capture, e.g. on an aux stream. The key includes M, so per-size eager warmup on the capture stream avoids it. The dsv4, dsv41, kimik3 and minimax_m3 tuned CSVs select _fsk kernels. Not reproduced. The current lru_cache(maxsize=1024) can also evict a buffer a captured graph still points at.

Fangzhou-Ai and others added 2 commits November 2, 2026 21:36
…nters

_fused_splitk_buffers built its per-tile arrival counters with a bare
torch.zeros under lru_cache. Built first inside a capture (e.g. on an aux
stream), the zero fill is recorded into that one graph, yet the buffer was
handed to every later graph and eager call with the same key; the counters
then never reach split_k - 1 and the tiles keep stale output. It also came
from the graph's private pool, and LRU eviction could free a buffer a
captured graph still points at.

Split it as the preshuffle split-K buffers: the fully overwritten workspace
stays one per key under functools.cache, and only the counters are
capture-scoped; both come from persistent_alloc. The compile-only AOT path
allocates directly, since it has no HIP stream to query.

test_fused_split_k_gemm_whose_counters_are_first_used_inside_a_capture
fails 3/3 on the old builder (98% mismatch) and passes with this change.

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

Copy link
Copy Markdown
Contributor Author

@XiaobingSuper Folded in at 50740bb. The workspace stays one per key under functools.cache + persistent_alloc (fully written before each arrival; no LRU eviction). Only the arrival counters are capture-scoped, under persistent_alloc too. The AOT/compile-only path allocates directly.

I reproduced it with the real _fsk kernel: test_fused_split_k_gemm_whose_counters_are_first_used_inside_a_capture (M=16, N=2304, K=6144, two aux-stream captures sharing one graph pool, with the counters' memory dirtied by another graph in it). It fails 3/3 on the old builder (~98% mismatch) and passes with this change.

…roed-scratch

# Conflicts:
#	aiter/ops/flydsl/mxscale_preshuffle_kernels.py
Fangzhou-Ai and others added 3 commits November 4, 2026 16:22
The wheel build replays every tuned GEMM row through the runtime on FakeTensors
with the GPUs hidden. Split-K rows build their semaphores through
capture_scoped_cache, which asks hipStreamGetCaptureInfo which capture the
stream is in; with no device that returns error 100, so 3502 rows failed and
the FlyDSL GEMM AOT step raised.

dry_run now answers "not capturing" for _capture_id, as it already does for
torch.cuda.is_current_stream_capturing: a replay never captures.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@vgokhale
vgokhale merged commit 5361651 into main Nov 5, 2026
@vgokhale
vgokhale deleted the fix-capture-scoped-zeroed-scratch branch November 5, 2026 18:51
jhartika-amd pushed a commit to jhartika-amd/aiter that referenced this pull request Nov 9, 2026
Brings aiter.utility.graph_alloc.capture_scoped_cache (ROCm#7126).

Signed-off-by: Jouni Hartikainen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants