HIP icon indicating copy to clipboard operation
HIP copied to clipboard

[Issue]: HIP Graph replay reads stale device-pointer values when graph has ~200+ kernel nodes (gfx1201 / RDNA 4)

Open maeddesg opened this issue 7 months ago • 3 comments

Problem Description

I'm hitting an issue where kernels inside a captured HIP graph read stale values from device pointers during replay. The values behind the pointers are updated via hipMemcpyAsync on the same stream before hipGraphLaunch, but the kernels still see whatever was there at capture time.

The confusing part: a simple 1-kernel test case works fine. The problem only shows up once the graph gets large (~200 kernel nodes). I've spent a few days narrowing this down and I'm fairly confident it's not a user error at this point, but I can't rule it out completely without more eyes on it.

Setup

  • ROCm 7.2.1 (initially observed), reproduced on 7.2.2, HIP 7.2.53211-9999, AMD clang 22.0.0git
  • RX 9070 XT (gfx1201 / RDNA4)
  • amdgpu driver, kernel 7.0.0-1-cachyos, CachyOS (Arch-based)
  • Haven't been able to test on gfx1100 or older yet

What I'm doing

I'm working on a small LLM inference engine (ROCmForge) and I capture the full autoregressive decode step as a HIP graph. That's 28 transformer layers with ~7 kernels each (RMSNorm, GEMV, RoPE, attention, elementwise, etc.), plus lm_head and argmax at the end — roughly 200 kernel nodes total.

Two values change between replays: pos (current token position) and seq_len (how many KV cache entries to attend over). Since kernel args are frozen at capture time, I pass these as device pointers and update the pointed-to memory before each replay. Standard pattern, works the same way in CUDA.

Simplified kernel:

__global__ void attn_decode_kernel(
    float* __restrict__ out,
    const float* __restrict__ q,
    const half*  __restrict__ k_cache,
    const half*  __restrict__ v_cache,
    const int*   __restrict__ pos_ptr,
    const int*   __restrict__ seq_len_ptr,
    int num_heads, int num_kv_heads, int head_dim, float scale
) {
    const int pos = *pos_ptr;
    const int seq_len = *seq_len_ptr;
    // ... attention over seq_len positions ...
}

Host side:

// Capture with pos=0, seq_len=1
int h_state[2] = {0, 1};
hipMemcpyAsync(d_state, h_state, 8, hipMemcpyHostToDevice, stream);
hipStreamSynchronize(stream);

hipStreamBeginCapture(stream, hipStreamCaptureModeGlobal);
for (int layer = 0; layer < 28; layer++) {
    rms_norm_kernel<<<...>>>(d_hidden, d_norm_weight, ...);
    qkv_gemv_kernel<<<...>>>(d_qkv, d_hidden, d_qkv_weight, ...);
    rope_kernel<<<...>>>(..., &d_state[0], ...);
    attn_decode_kernel<<<...>>>(..., &d_state[0], &d_state[1], ...);
    ffn_kernels<<<...>>>(...);
}
lm_head_gemv<<<...>>>(...);
argmax_kernel<<<...>>>(...);
hipStreamEndCapture(stream, &graph);
hipGraphInstantiate(&exec, graph, nullptr, nullptr, 0);

// Later: update to pos=15, seq_len=16
h_state[0] = 15;  h_state[1] = 16;
hipMemcpyAsync(d_state, h_state, 8, hipMemcpyHostToDevice, stream);
hipGraphLaunch(exec, stream);
hipStreamSynchronize(stream);
// Kernel output is wrong — looks like it used seq_len=1, not 16

What I tried to isolate it

I tested four configurations, same kernels, same data:

Config Result
Direct launch, seq_len as immediate kernel arg correct
Direct launch, seq_len via device pointer correct
Graph replay (~200 nodes), seq_len via device pointer wrong
Graph replay (only 2 nodes: lm_head + argmax), layers direct correct

The third row is the problem. Same kernel, same pointer, same stream — just wrapped in a bigger graph.

I also added a readback right before hipGraphLaunch to confirm the memcpy landed:

pos=9: uploaded state=[9, 10]  -- device memory is correct
hidden before graph: [-0.023, 0.007, ...]  -- input looks fine  
hidden after graph: [1.61, 0.08, ...]  -- output is wrong (not NaN, just wrong values)
argmax token=52643  -- garbage

So the data is there on the device, the input to the graph is valid, but the output is wrong in a way that's consistent with the attention kernel using seq_len=1 instead of 10.

The confusing part: simple test passes

I wrote a minimal standalone test — single kernel that reads an int from a device pointer, graph capture, update value, replay:

$ hipcc -o graph_ptr_test graph_ptr_test.hip && ./graph_ptr_test
Test 1 (original value 42): Got 42 PASS
Test 2 (updated to 99): Got 99 PASS
Test 3 (10 sequential replays): PASS

All fine. So it's not a blanket "device pointer reads are inlined" issue. Something about the graph size or topology triggers it.

I haven't managed to build a self-contained reproducer that fails yet — the bug seems tied to having many kernel nodes. If it helps I can provide the full project to reproduce against a GGUF model file.

My guesses (probably wrong, but for context)

  • Some graph-level optimization inlines or caches device memory reads when node count is high?
  • Memory coherence issue when many kernels read from the same small allocation?
  • Kernel reordering within the graph that races with the pre-launch memcpy?
  • Something RDNA4-specific in the L2 / memory controller path?

No idea really. Could also be something dumb on my end that I'm not seeing.

Workaround

I run the layer kernels directly (they're the ones that read the device pointers) and only capture the tail (lm_head + argmax) as a graph. Works correctly. Performance hit is noticeable on small models (~650 → 220 tok/s for 0.5B) but negligible on 7B since those layers are bandwidth-bound anyway.

Standalone test (passes — included for reference)

#include <hip/hip_runtime.h>
#include <cstdio>

__global__ void read_device_ptr_kernel(
    int* __restrict__ d_output,
    const int* __restrict__ d_value_ptr
) {
    if (threadIdx.x == 0) {
        d_output[0] = *d_value_ptr;
    }
}

int main() {
    int* d_value;
    int* d_output;
    hipMalloc(&d_value, sizeof(int));
    hipMalloc(&d_output, sizeof(int));

    hipStream_t stream;
    hipStreamCreate(&stream);

    int h_value = 42;
    hipMemcpyAsync(d_value, &h_value, sizeof(int), hipMemcpyHostToDevice, stream);
    hipStreamSynchronize(stream);

    hipStreamBeginCapture(stream, hipStreamCaptureModeGlobal);
    read_device_ptr_kernel<<<1, 1, 0, stream>>>(d_output, d_value);
    hipGraph_t graph;
    hipStreamEndCapture(stream, &graph);

    hipGraphExec_t exec;
    hipGraphInstantiate(&exec, graph, nullptr, nullptr, 0);

    hipGraphLaunch(exec, stream);
    hipStreamSynchronize(stream);
    int h_output = 0;
    hipMemcpy(&h_output, d_output, sizeof(int), hipMemcpyDeviceToHost);
    printf("Test 1 (original value 42): Got %d %s\n",
           h_output, h_output == 42 ? "PASS" : "FAIL");

    h_value = 99;
    hipMemcpyAsync(d_value, &h_value, sizeof(int), hipMemcpyHostToDevice, stream);
    hipGraphLaunch(exec, stream);
    hipStreamSynchronize(stream);
    hipMemcpy(&h_output, d_output, sizeof(int), hipMemcpyDeviceToHost);
    printf("Test 2 (updated to 99): Got %d %s\n",
           h_output, h_output == 99 ? "PASS" : "FAIL");

    int all_pass = 1;
    for (int i = 1000; i < 1010; i++) {
        hipMemcpyAsync(d_value, &i, sizeof(int), hipMemcpyHostToDevice, stream);
        hipGraphLaunch(exec, stream);
        hipStreamSynchronize(stream);
        hipMemcpy(&h_output, d_output, sizeof(int), hipMemcpyDeviceToHost);
        if (h_output != i) {
            printf("Test 3 (expected %d): Got %d FAIL\n", i, h_output);
            all_pass = 0;
        }
    }
    if (all_pass) printf("Test 3 (10 sequential replays): PASS\n");

    hipGraphExecDestroy(exec);
    hipGraphDestroy(graph);
    hipStreamDestroy(stream);
    hipFree(d_value);
    hipFree(d_output);
    return 0;
}

Graph topology: strictly linear (chain of ~200 kernel nodes, no branches, no conditionals). All nodes on the same stream.

Note: hipGraphExecKernelNodeSetParams is not practical here — pos is read by ~100 of the 200 kernels (every RoPE + attention node). Updating 100 nodes per replay would negate the graph benefit. The device-pointer pattern is the standard approach in CUDA and works correctly on NVIDIA hardware. I haven't pinpointed the exact threshold, but the issue doesn't appear with ~10 nodes (tail-only graph) and reliably appears with ~200 nodes (full decode graph).

Full project for reproduction: https://github.com/maeddesg/ROCmForge To reproduce: build with cargo build --release --features gpu, load any 7B GGUF model, and enable graph capture via [flag/env].

Happy to provide more info or test things if that helps track this down.

Related (but different root cause): #3882 — memcpy dependency enforcement issue on gfx908. My issue is about device-pointer reads returning stale values during graph replay on gfx1201, not about dependency ordering. Both suggest that HIP Graph behavior with complex graphs needs attention.

Operating System

Cachyos (Arch Linux)

CPU

AMD Ryzen 9 7945HX with Radeon Graphics

GPU

gfx1201

ROCm Version

Rocm 7.2.2

ROCm Component

HIP

Steps to Reproduce

No response

(Optional for Linux users) Output of /opt/rocm/bin/rocminfo --support

rocminfo --suport ROCk module is loaded

HSA System Attributes

Runtime Version: 1.18 Runtime Ext Version: 1.15 System Timestamp Freq.: 1000.000000MHz Sig. Max Wait Duration: 18446744073709551615 (0xFFFFFFFFFFFFFFFF) (timestamp count) Machine Model: LARGE
System Endianness: LITTLE
Mwaitx: DISABLED XNACK enabled: NO DMAbuf Support: YES VMM Support: YES

==========
HSA Agents


Agent 1


Name: AMD Ryzen 9 7945HX with Radeon Graphics Uuid: CPU-XX
Marketing Name: AMD Ryzen 9 7945HX with Radeon Graphics Vendor Name: CPU
Feature: None specified
Profile: FULL_PROFILE
Float Round Mode: NEAR
Max Queue Number: 0(0x0)
Queue Min Size: 0(0x0)
Queue Max Size: 0(0x0)
Queue Type: MULTI
Node: 0
Device Type: CPU
Cache Info:
L1: 32768(0x8000) KB
Chip ID: 0(0x0)
ASIC Revision: 0(0x0)
Cacheline Size: 64(0x40)
Max Clock Freq. (MHz): 5610
BDFID: 0
Internal Node ID: 0
Compute Unit: 32
SIMDs per CU: 0
Shader Engines: 0
Shader Arrs. per Eng.: 0
WatchPts on Addr. Ranges:1
Memory Properties:
Features: None Pool Info:
Pool 1
Segment: GLOBAL; FLAGS: FINE GRAINED
Size: 65536532(0x3e80214) KB
Allocatable: TRUE
Alloc Granule: 4KB
Alloc Recommended Granule:4KB
Alloc Alignment: 4KB
Accessible by all: TRUE
Pool 2
Segment: GLOBAL; FLAGS: EXTENDED FINE GRAINED Size: 65536532(0x3e80214) KB
Allocatable: TRUE
Alloc Granule: 4KB
Alloc Recommended Granule:4KB
Alloc Alignment: 4KB
Accessible by all: TRUE
Pool 3
Segment: GLOBAL; FLAGS: KERNARG, FINE GRAINED Size: 65536532(0x3e80214) KB
Allocatable: TRUE
Alloc Granule: 4KB
Alloc Recommended Granule:4KB
Alloc Alignment: 4KB
Accessible by all: TRUE
Pool 4
Segment: GLOBAL; FLAGS: COARSE GRAINED
Size: 65536532(0x3e80214) KB
Allocatable: TRUE
Alloc Granule: 4KB
Alloc Recommended Granule:4KB
Alloc Alignment: 4KB
Accessible by all: TRUE
ISA Info:


Agent 2


Name: gfx1201
Uuid: GPU-42b8a8bfb04d8ec9
Marketing Name: AMD Radeon RX 9070 XT
Vendor Name: AMD
Feature: KERNEL_DISPATCH
Profile: BASE_PROFILE
Float Round Mode: NEAR
Max Queue Number: 128(0x80)
Queue Min Size: 64(0x40)
Queue Max Size: 131072(0x20000)
Queue Type: MULTI
Node: 1
Device Type: GPU
Cache Info:
L1: 32(0x20) KB
L2: 8192(0x2000) KB
L3: 65536(0x10000) KB
Chip ID: 30032(0x7550)
ASIC Revision: 1(0x1)
Cacheline Size: 256(0x100)
Max Clock Freq. (MHz): 2570
BDFID: 768
Internal Node ID: 1
Compute Unit: 64
SIMDs per CU: 2
Shader Engines: 4
Shader Arrs. per Eng.: 2
WatchPts on Addr. Ranges:4
Coherent Host Access: FALSE
Memory Properties:
Features: KERNEL_DISPATCH Fast F16 Operation: TRUE
Wavefront Size: 32(0x20)
Workgroup Max Size: 1024(0x400)
Workgroup Max Size per Dimension: x 1024(0x400)
y 1024(0x400)
z 1024(0x400)
Max Waves Per CU: 32(0x20)
Max Work-item Per CU: 1024(0x400)
Grid Max Size: 4294967295(0xffffffff)
Grid Max Size per Dimension: x 2147483647(0x7fffffff)
y 65535(0xffff)
z 65535(0xffff)
Max fbarriers/Workgrp: 32
Packet Processor uCode:: 218
SDMA engine uCode:: 662
IOMMU Support:: None
Pool Info:
Pool 1
Segment: GLOBAL; FLAGS: COARSE GRAINED
Size: 16695296(0xfec000) KB
Allocatable: TRUE
Alloc Granule: 4KB
Alloc Recommended Granule:2048KB
Alloc Alignment: 4KB
Accessible by all: FALSE
Pool 2
Segment: GROUP
Size: 64(0x40) KB
Allocatable: FALSE
Alloc Granule: 0KB
Alloc Recommended Granule:0KB
Alloc Alignment: 0KB
Accessible by all: FALSE
ISA Info:
ISA 1
Name: amdgcn-amd-amdhsa--gfx1201
Machine Models: HSA_MACHINE_MODEL_LARGE
Profiles: HSA_PROFILE_BASE
Default Rounding Mode: NEAR
Default Rounding Mode: NEAR
Fast f16: TRUE
Workgroup Max Size: 1024(0x400)
Workgroup Max Size per Dimension: x 1024(0x400)
y 1024(0x400)
z 1024(0x400)
Grid Max Size: 4294967295(0xffffffff)
Grid Max Size per Dimension: x 2147483647(0x7fffffff)
y 65535(0xffff)
z 65535(0xffff)
FBarrier Max Size: 32
ISA 2
Name: amdgcn-amd-amdhsa--gfx12-generic
Machine Models: HSA_MACHINE_MODEL_LARGE
Profiles: HSA_PROFILE_BASE
Default Rounding Mode: NEAR
Default Rounding Mode: NEAR
Fast f16: TRUE
Workgroup Max Size: 1024(0x400)
Workgroup Max Size per Dimension: x 1024(0x400)
y 1024(0x400)
z 1024(0x400)
Grid Max Size: 4294967295(0xffffffff)
Grid Max Size per Dimension: x 2147483647(0x7fffffff)
y 65535(0xffff)
z 65535(0xffff)
FBarrier Max Size: 32
*** Done ***

Additional Information

No response

maeddesg avatar Apr 19 '26 12:04 maeddesg

Hi @maeddesg Thanks for reaching out! I tried to reproduce your issue with a reproducer and the tests seem to be passing. Can you give it a try and see if it passes on your side as well?

I think we need the exact reproducer to reproduce the issue effectively here so see the problem... HIP-3887-graph-stale-reads.zip

tcgu-amd avatar Apr 29 '26 18:04 tcgu-amd

Hi, thanks for looking into this!

You're right — this was on our side. We went back and wrote a proper smoke test, and HIP Graph works correctly on gfx1201 with ROCm 7.2.2. The original issue was caused by a bug in our graph-capture setup, not in the HIP runtime.

Sorry for the noise, and thanks for the quick response! Closing this.

maeddesg avatar May 04 '26 04:05 maeddesg

@maeddesg Thanks for the update! Glad the issue is resolved for you!

tcgu-amd avatar May 04 '26 16:05 tcgu-amd