Skip to content

[w2] Prep: ROCm CLR agent-scope fence patch for HIP graph replay (env-gated), Dockerfile, build script - #175

Merged
rafasmour merged 2 commits into
masterfrom
opt/w2-rocm-clr-fence-patch
Dec 4, 2026
Merged

rafasmour merged 2 commits into
masterfrom
opt/w2-rocm-clr-fence-patch

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

What / why

Prep only (no GPU used, nothing built or deployed, no production image touched): an env-gated patch for the HIP runtime (CLR) so interior kernel packets of a HIP graph replay can use agent-scope (or, measurement-only, no) acquire/release fences, plus a Dockerfile and build script for a throwaway image rdna3-clr-fence:local. It tests whether cache maintenance is part of the ~2 us per-dependent-launch idle (TheRock#9241).

Pinned runtime (read from rdna3-build:local): ROCm 10.1.0 (TheRock b324869f, 10.1.0rc3), HIP 7.16.26385, CLR = projects/clr of ROCm/rocm-systems @ 7da43026e4f2ef7600433c38c820732f7a205d22 (no tag exists for that pin; fetched by SHA).

Important finding (premise check)

In that CLR, AMD_OPT_FLUSH defaults to 1, which already gives agent-scope acquire+release on kernel dispatch packet headers for non-gfx12 (gfx1100 included). Graph replay uses headers captured at instantiate time and replays them through VirtualGPU::dispatchAqlPacketBatchFlat; graph barrier packets use scope NONE. So HIP_GRAPH_AGENT_FENCE=1 is expected to be close to a no-op. Not verified on the binary (needs a GPU). Cheaper first test with the stock runtime: AMD_OPT_FLUSH=0 (system scope everywhere) versus default; commands in the README. If there is no difference the task can be dropped without building anything.

Patch (tools/rdna3-w2/rocm-clr-fence-patch/clr-agent-scope-fence.patch)

  • rocclr/utils/flags.hpp: new flag HIP_GRAPH_AGENT_FENCE (default 0).
  • rocclr/device/rocm/rocvirtual.cpp dispatchAqlPacketBatchFlat: when set, interior kernel packets get agent fences (1) or none (2, UNSAFE, ceiling measurement only); first packet keeps system acquire, last packet system release; barrier bit untouched; unset = stock.
  • Plan deviation: no change to hip_graph*.cpp; that function is the single replay choke point and batches are already split at uncaptured nodes (memcpy, host node, child graph).
  • Phase 2 (drop barrier bit for independent branches) documented only.

Env switch

HIP_GRAPH_AGENT_FENCE=1 (patched runtime only; 2 = unsafe ceiling). Default off. No change to the llama.cpp sources.

Checks done (CPU)

  • git apply --check against a fresh sparse checkout of the pin: OK.
  • g++ -fsyntax-only of rocvirtual.cpp in rdna3-build:local (stub X11/GL/EGL headers): rc=0, no new warnings. Full CLR build NOT run (not in this wave).
  • docker build --check on the Dockerfile: no warnings; bash -n build-clr.sh: OK.

Risks

Memory-ordering bugs give silent wrong results; validate with full test2 and KLD (must be exactly 0) before any use. Dockerfile apt list and cmake flags are untested for the full build. Never tag the image llama-cpp-rocm:* (script refuses). No ggml-org interaction.

GPU validation tonight (NOT YET RUN)

Stock runtime first (no image needed):

GPURUN_OPTS='-e AMD_OPT_FLUSH=0' ./gpublock.sh ./lb.sh g-flush0 $B0 $G $GA -p 0 -n 128 -r 5
./gpublock.sh ./lb.sh g-flush1 $B0 $G $GA -p 0 -n 128 -r 5

Only if the user builds the image first (bash build-bins/w2/rocm-clr-fence-patch/build-clr.sh, CPU, long):

GPURUN_IMAGE=rdna3-clr-fence:local GPURUN_OPTS='-e HIP_GRAPH_AGENT_FENCE=1' ./gpusmall.sh ./test2.sh w2-clr-all $B0 - '' '.'   # 0 FAIL
GPURUN_IMAGE=rdna3-clr-fence:local GPURUN_OPTS='-e HIP_GRAPH_AGENT_FENCE=1' ./gpublock.sh ./lb.sh g-clr $B0 $G $GA -p 0 -n 128 -r 5
GPURUN_IMAGE=rdna3-clr-fence:local ./gpublock.sh ./lb.sh g-clr-off $B0 $G $GA -p 0 -n 128 -r 5
./gpublock.sh ./kl.sh g-clr-base $B0 $G base $GA
GPURUN_IMAGE=rdna3-clr-fence:local ./gpublock.sh env KL_ENV=HIP_GRAPH_AGENT_FENCE=1 ./kl.sh g-clr $B0 $G test $GA   # KLD exactly 0

Idle: tr.sh with GPURUN_IMAGE set, compared with B0. Abandon if idle drops by less than 30%.

Expected gain (estimate, unproven): at most ~1-2 ms/token on Gemma, ~0.5 ms on Nemotron if fence scope is the cause; given the finding above, likely zero.

…, Dockerfile and build script

Prep only: patch against ROCm/rocm-systems@7da43026 (projects/clr, the CLR of
ROCm 10.1.0), Dockerfile.clr-patched, build-clr.sh and README with the
correctness argument, failure modes and test plan. Nothing is built or deployed.

Assisted-by: Claude Sonnet 5.5
Assisted-by: Claude Sonnet 5.5
@rafasmour
rafasmour merged commit 25f0def into master Dec 4, 2026
2 checks passed
@rafasmour

Copy link
Copy Markdown
Owner Author

Reverted from master on 2026-12-04 (w2 build crashed Qwen4-single at >20k ctx; no measured gain). Commit preserved on branch archive/w2-25f0def; will be re-landed individually after GPU validation.

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