Skip to content

ROCm Linux: rebuild libhsa-runtime64 with the KFD CWSR wave count - #255

Merged
danielhanchen merged 3 commits into
masterfrom
rocm-rebuild-hsa-runtime-kfd-wave-count
Oct 12, 2026
Merged

danielhanchen merged 3 commits into
masterfrom
rocm-rebuild-hsa-runtime-kfd-wave-count

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Linux ROCm prebuilds ship TheRock's libhsa-runtime64 unchanged. Since TheRock 7.14 (rocm-systems 745ebf1, ROCm/rocm-systems#6125), its libhsakmt sizes each queue's CWSR debug area from KFD topology, cu_count * simd_per_cu * max_waves_per_simd, while amdkfd (kfd_queue.c get_num_waves) still uses cu_count * 32 below gfx1250. Since Linux 6.12 (629568d25fea) amdkfd validates that area when a queue is created:

  • on the non-SVM path (kfd_queue_buffer_get) the mapping must match exactly, so any disagreement is refused;
  • on the SVM path (kfd_queue_buffer_svm_get) the range only has to cover the expected size, so only a smaller area is refused.

HIP reports a refused queue as ROCm error: out of memory at the first hipStreamCreateWithFlags, before any model memory is touched. This is ROCm/TheRock#7472 (gfx1012, Linux 6.12). The upstream fix, ROCm/rocm-systems#11299, is open and not merged. As of 10.2.0a20261009 (rocm-systems 8a343ff) the nightlies, rocm-systems develop and therock-10.1 all still have the topology formula, and the kernel (mainline and amd-staging-drm-next) still uses 32.

Who is affected

rocminfo Max Waves Per CU (simd_per_cu * max_waves_per_simd, from the IP discovery table) is 32 on every gfx10.3, gfx11 and gfx12 part we found reports for, so those match amdkfd today. The parts that disagree are gfx10.1, which report 40:

  • gfx1010 (RX 5700 / 5600), gfx1011 (Pro V520), gfx1012 (RX 5500), gfx1013 (BC-250).

We do not publish a gfx101x bundle, so this reaches users who run the gfx103X bundle on a Navi1x card with HSA_OVERRIDE_GFX_VERSION=10.3.0, a common workaround, on Linux 6.12 or newer when the save area takes the non-SVM path. It also guards any future part whose firmware reports a value other than 16 waves per SIMD.

This does not explain unslothai/unsloth#12205: the Radeon 680M (gfx1035) reports 32 waves per CU, so it has no mismatch. That issue is still being investigated separately.

Change

One new step in the Linux gfx103X leg only, after the ROCm libraries are copied and before RPATH is set. That is the only bundle a gfx10.1 card reaches (through HSA_OVERRIDE_GFX_VERSION=10.3.0); the gfx110X, gfx1150, gfx1151, gfx120X, gfx908 and gfx90a bundles keep TheRock's library byte for byte.

  1. Read the rocm-systems pin (and any TheRock patches) from /opt/rocm/share/therock/therock_manifest.json.
  2. Sparse fetch projects/rocr-runtime at that pin, apply TheRock's patches, then the fix(hsakmt): match KFD CWSR wave sizing before gfx1250 ROCm/rocm-systems#11299 change: wave_num = cu_num * 32 for gfxv < GFX_VERSION_GFX1250. The step fails loudly if the pin already carries it or get_num_waves() changed upstream.
  3. Build hsa-runtime64 with the nightly's own clang and its bundled sysdeps (rocm_sysdeps headers and pkgconfig), so it links the same librocm_sysdeps_* as the shipped library.
  4. Fail if the rebuilt library is missing any ROCR_* export the shipped one has, or if libamdhip64.so.7 has undefined symbols against it. Then replace every libhsa-runtime64.so* copy in the bundle (the earlier copy flattens the symlinks into three files). The existing bundle completeness check still runs afterwards.

Windows legs are untouched: they ship no ROCr.

Testing

  • The bundled runtime in b11160-mix-a6922cc is code-identical (.text/.rodata/.dynsym) to TheRock 10.1.0a20260822, pin d134b78, which contains 745ebf1.
  • The exact run: script of this step, extracted from the YAML, was run against that nightly and a copy of the shipped gfx1151 bundle: pin read from the manifest, patch applied, build OK, 280 of 280 ROCR_* exports present, ldd -r clean, all three library copies replaced.
  • Newest nightly 10.2.0a20261009 (pin 8a343ff): its TheRock rocm-systems patch and the wave count patch both apply with the step's sparse checkout. Not built.
  • Hosted ubuntu-22.04 build, then a gfx1151 (Strix Halo) differential run: https://github.com/oobabooga/unsloth/actions/runs/37787382574. max_waves_per_simd was fed to libhsakmt through an fopen shim while amdkfd kept its real value (16), recreating the gfx10.1 disagreement (20) and the opposite one (8). Each runtime ran llama-completion on a tiny GGUF with -ngl 99:
mode shipped runtime unpatched rebuild this step's rebuild
real topology GPU, OK GPU, OK GPU, OK, identical text
real topology, HSA_USE_SVM=0 GPU, OK GPU, OK GPU, OK
20 waves/SIMD (larger, SVM) GPU, OK GPU, OK GPU, OK
20 waves/SIMD, HSA_USE_SVM=0 out of memory at hipStreamCreateWithFlags same GPU, OK
8 waves/SIMD (smaller, SVM) out of memory at hipStreamCreateWithFlags same GPU, OK
check shipped runtime rebuilt runtime
greedy, 256 tokens, temp 0 OK OK, identical text
llama-server --parallel 4, 8 concurrent requests 8/8, 151.7 tok/s median per request 8/8, 151.8 tok/s
llama-bench pp512, ABBA x3, median (min-max) 6815.2 (6752.7-6838.7) t/s 6811.4 (6766.9-6856.1) t/s, -0.1%
llama-bench tg128, ABBA x3, median (min-max) 213.9 (213.6-214.6) t/s 214.0 (213.9-214.8) t/s, +0.0%

Not covered

  • Real gfx10.1 silicon; the reproduction is a topology spoof on gfx1151.
  • The gfx103X bundle itself on RDNA2 hardware: the rebuilt runtime is the same file for every bundle, but it only ran inside the gfx1151 bundle.
  • A full prebuild run of this workflow; only the step was exercised, standalone and on a hosted runner. It adds one ROCr build, a few minutes, to each Linux leg.
  • A build against a 10.2 nightly; only the patch application was checked there.

TheRock 7.14 and later (rocm-systems 745ebf1) size each queue's CWSR debug
area from KFD topology, while amdkfd keeps 32 waves per CU below gfx1250.
When the two disagree amdkfd refuses the queue and llama.cpp aborts with
"ROCm error: out of memory" at hipStreamCreateWithFlags
(unslothai/unsloth#12205, ROCm/TheRock#7472).

Rebuild only libhsa-runtime64 from the nightly's own rocm-systems pin with
the ROCm/rocm-systems#11299 wave count, check it exports every ROCR symbol
the shipped one does and that libamdhip64 resolves against it, and replace
every copy in the bundle.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T15:40:46.311296Z 1b69ba4 PR opened
🔒 Security Review ✅ Completed 2026-10-08T15:39:55.174586Z 1b69ba4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1b69ba4b52

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1011 to +1012
if new in text:
sys.exit("the pinned rocm-systems already carries the fix: delete this step")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve already-compatible ROCm pins

When a caller selects a historical rocm_version from before rocm-systems commit 745ebf1, queues.c already contains this 32-wave branch, so new in text terminates the job instead of leaving the compatible runtime unchanged. Explicit version pins are a documented workflow input (the input description even gives a dated version), so rebuilding such a release now always fails; treat the already-patched text as a no-op and only rebuild when the vulnerable old form is present.

Useful? React with 👍 / 👎.

danielhanchen and others added 2 commits October 9, 2026 06:43
The disagreeing parts are gfx10.1 (40 waves per CU), which reach a Linux
bundle through HSA_OVERRIDE_GFX_VERSION=10.3.0 on the gfx103X bundle. Every
other leg serves GPUs that report 32 waves per CU, so keep TheRock's library
byte for byte there.

This branch was previously deployed

1 inactive deployment
release-signing — 075ac33c Deployed Oct 12, 2026 by shimmyshimmer
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.

2 participants