Skip to content

! on AMD GPUs: an experimental HIP/ROCm lane works #891

Closed

Description

@costamatheus97

What Bend should do

I built a third GPU lane on top of commit 8008146, for AMD cards through HIP and HIPRTC. It runs. The patch is 545 lines (comp.ts +376 −11, main.ts +9 −3).

Tested on one card only: Radeon RX 7800 XT (gfx1101, 16 GB), ROCm 7.2.1, WSL2.

The port

HIPRTC compiles the CUDA lane's device code as written: __global__, __shared__ (including the extern __shared__ hold[] sized at launch), __syncthreads(), __threadfence(), __clz, atomicAdd/Sub/CAS. I checked the barrier, shared memory and CAS on the card with 128 × 128 lanes. Two differences:

  • HIPRTC defines __HIPCC_RTC__, not __CUDACC_RTC__. One BEND_RTC macro replaces the six tests.
  • HIPRTC ignores -default-device, so plain functions compile as host functions. #pragma clang force_cuda_host_device begin fixes it.

The host side is a BEND_HIP block beside BEND_CUDA, nearly one-to-one, with the same hash-keyed .gpu cache. AMD's clang 22 has #embed, and cc_find accepts its version string.

The memory model

Consumer Radeons report concurrentManagedAccess = 0: managed memory is pinned host RAM read over PCIe, and nothing migrates. gpu_probe would refuse the card. Relaxing the probe works but is unusable: lanes keep their stacks, rings and free lists in that block, so queens takes 47 s and tree-bitonic 41 s.

The corpus is index-based, though: every Loc is a word index, and lanes derive their pointers from H + offset at kernel entry. So the device can hold it at another address. My second mode keeps a twin of the corpus in VRAM. Each ! copies in the header, the live ring planes, the static image, the heap under the bump and the live bank entries; runs every round on the device, exchanging only the header; then copies back. CPU and GPU never run together, so this is legal. queens drops from 47 s to 1.4 s of device time.

Numbers

bench/runtime/, perf.ts protocol and spans, 16 CPU threads against the HIP lane with the VRAM twin. Wall seconds, single runs on a desktop, so ±10%. Every row prints the pinned checksum on both lanes.

bench 16 threads HIP wall HIP device passes
gameoflife 1.50 0.27 0.02
nbody 0.65 0.26 0.01
raytrace 0.87 0.34 0.09
kmeans 0.93 0.42 0.15
mandelbrot 0.54 0.28 0.02
merkle 0.63 0.39 0.02
tree-bitonic 2.29 1.87 1.48
bfs 0.45 0.65 0.26
tree-matmul 0.56 0.80 0.22
symreg 0.55 0.92 0.67
terrain 0.41 0.96 0.55
editdist 0.36 1.00 0.66
hashmap 0.48 1.13 0.45
lexer 0.41 1.24 0.94
tree-radix 0.87 1.38 0.28
queens 1.17 1.63 1.39

Bimodal, as in the paper. On the divergent programs all device time is inside one work kernel; growth, barriers and packing are about 0 ms. 128 groups was best or near-best on 7 of 10 programs swept; editdist and terrain ran 2 to 3 times faster at 32.

The 11 runnable ! tests print the same bytes as the CPU lane in both memory modes. I cannot run gates/test.ts; a local copy of its rules gives 1299 / 1302 with the patch and 1300 / 1302 without. The difference is a TCP test with no ! that is flaky here on the unpatched tree too.

Weak points

  • One card, one OS. No native Linux, no second AMD part, no medians.
  • You have no AMD hardware in the gate, so you could not test this lane.
  • The memory mode is an environment variable. It should be decided in the probe, with the span from free VRAM.
  • The whole heap under the bump is copied both ways on every !; tree-radix moves 3.7 GB over three turns.
  • Under WSL, ROCm spends 0.5 s at first use searching Windows directories on $PATH. I trim PATH around the first transfer, which is a hack.
  • effs/window_frame.c still has only its CUDA device path.
  • The patch carries a debug counter and two tuning variables. I have not checked gates/repo.ts's size cap.

The question

Would you want an AMD lane in the tree, given you cannot gate it? If yes, I would fix the points above before sending anything. If not, one finding stands without AMD: the corpus is position-independent, so a VRAM-twin mode is open to any card without migrating managed memory.

Why

I don’t have an NVIDIA GPU 🥴

Activity

  1. added
    enhancement
    on Sep 20, 2026
  2. AdrielSantana commented on Sep 20, 2026

    @AdrielSantana

    Bumping this. Unfortunately, I have an AMD GPU too.

  3. costamatheus97 commented on Sep 20, 2026

    @costamatheus97
    ContributorAuthor

    Update: a second pass cut most of the copy and start-up cost. The lane now beats 16 CPU threads on 10 of the 16 bench/runtime/ programs (7 before), and every remaining loss is device time, not overhead. The patch is now 731 lines (comp.ts +546 −13, main.ts +9 −3).

    The heap is lazy now. Before, each ! copied the whole heap under the bump up and back down; tree-radix moved 3.7 GB over three turns. Now, after a device turn, the heap's 2 MB chunks are made inaccessible with mprotect and nothing is downloaded. The first host touch of a chunk faults and downloads that chunk alone; a chunk the host writes is marked dirty, and the next ! uploads only dirty chunks. The result of a ! is usually a few words, so tree-radix now moves 17 MB. The fault handler is chained in front of the stack-guard handler in pool_stack. Tracking by the bump alone is unsound: heap_free and heap_alloc rewrite freed slots in place under the old bump.

    Start-up. My port zeroed 143 MB of a fresh mmap that was already zero: 0.10 s of a 0.25 s floor. Removed. The floor is now about 0.15 s, nearly all inside ROCm.

    Wall seconds, medians of 3 runs, same protocol as above, all checksums correct:

    bench 16 threads HIP before HIP now device passes
    gameoflife 1.36 0.27 0.16 0.02
    mandelbrot 0.68 0.28 0.16 0.01
    raytrace 0.90 0.34 0.23 0.09
    merkle 0.64 0.40 0.18 0.01
    kmeans 0.95 0.45 0.27 0.13
    nbody 0.65 0.26 0.19 0.01
    tree-radix 0.83 1.37 0.49 0.27
    tree-matmul 0.56 0.80 0.40 0.22
    tree-bitonic 2.26 1.86 1.73 1.55
    bfs 0.47 0.63 0.43 0.25
    symreg 0.64 0.90 0.74 0.60
    hashmap 0.48 1.14 0.59 0.41
    queens 1.19 1.62 1.49 1.34
    terrain 0.49 0.97 0.67 0.49
    editdist 0.36 1.06 0.80 0.63
    lexer 0.49 1.25 1.16 0.97

    The stock sizes understate uniform work. They are 1 s programs, so the 0.15 s floor dominates. At 16 times the input, wall time against 16 threads: gameoflife 32x faster, nbody 21x, mandelbrot 14x, raytrace 10x, kmeans 8x. Device time scales linearly. queens and tree-bitonic stay near 1x at any size.

    The slow programs are two different problems. I counted dispatches per lane in the work kernel. queens, symreg and editdist are imbalanced: in queens the mean lane does 9% of the longest lane's work, and in symreg one lane runs 3.8 times longer than all the others. lexer and tree-bitonic are balanced to 99%, so their cost is per dispatch and re-balancing cannot help. I tried in-kernel stealing from neighbouring rings: no gain, because after growth every lane holds exactly one task and there is nothing queued to steal. Growing for extra rounds to make smaller tasks made queens slower (1.5 s to 3.7 s).

    Checks on the final code. The 11 runnable ! tests are byte-identical to the CPU lane in both memory modes. My local copy of the gate gives 1299 / 1302, the same three failures as before. Two multi-turn tests where the host frees, rebuilds and extends device-built trees between ! calls match a Python model. For concurrent faults I ran two tests 150 times each with 16 host threads folding a device-built tree: 300 of 300 correct, with about 41,000 faults waiting on the handler's lock and 32,000 finding their chunk already served by another thread.

    New weak point. The handler calls hipMemcpy from SIGSEGV. The fault can only come from Bend's own evaluation code, never from inside ROCm, but POSIX does not promise it. A helper thread doing the download would be the clean form. The corpus is also a memfd now, so no transparent huge pages; I see no loss on these benchmarks.

    One thing I noticed while testing, in unpatched behaviour: once the host pool has forked, a later ! in the same program runs on the CPU, not the device. With a sequential host phase between them, both ! calls reach the device.

  4. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Thank you for this. It is a serious piece of work: the HIPRTC path, the VRAM-twin mode for consumer Radeons, and the benchmark table are exactly what a port needs, and the fact that queens went from 47 s to 1.4 s says you understood the runtime.

    We cannot take it into the repo, and the reason is capacity, not quality. We are a very small team; every line in comp.ts is a line we have to understand, test on hardware we own, and keep working through every runtime change, and we have no AMD hardware in the test cluster. A GPU lane we cannot run is a lane we would break without knowing. So this is a wontfix on our side for now, with real regret.

    What we can do: if you open a PR against a branch named hip (rather than main), we will keep it there in this repo so people can find and build it, and we will link it from the issue. When the runtime settles and we have a way to test on AMD, this is the first thing we would look at.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

  5. added 18 commits that reference this issue on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions