Skip to content

[Bugfix] Bind load_hip_runtime to the HIP runtime already loaded - #6398

Merged
zufayu merged 14 commits into
ROCm:mainfrom
omirosh:fix/load-hip-runtime-loaded-copy
Oct 22, 2026
Merged

zufayu merged 14 commits into
ROCm:mainfrom
omirosh:fix/load-hip-runtime-loaded-copy

Conversation

@omirosh

@omirosh omirosh commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

This bug fix makes load_hip_runtime() bind to the HIP runtime that torch already loaded. Today it opens libamdhip64.so by name, and on ROCm 10 images that adds a second HIP runtime to the process and breaks other ctypes HIP users. With UncachedIpcHeap imported, two of three SGLang server starts of Qwen3.8 at TP8 on MI355X failed in decode graph capture:

RuntimeError: vattn_asm: hipModuleLaunchKernel failed: 709 (context is destroyed)

ROCm 10 ships the HIP runtime (_rocm_sdk_core) and the toolchain (_rocm_sdk_devel) as separate wheels. Torch maps the core libamdhip64.so.7. When _rocm_sdk_devel/lib is on LD_LIBRARY_PATH, CDLL("libamdhip64.so") maps the devel copy as a second runtime.

sgl-project/sglang#39513 fixed this 709 in SGLang's vattn_asm. It binds to the libamdhip64 in /proc/self/maps and assumes that only torch's copy is mapped. load_hip_runtime() breaks that assumption. SGLang's vattn_asm, smallm_moe_gfx950 and engram can then bind the devel copy and launch on a torch stream through the wrong runtime.

In SGLang the failure shows up in vattn_asm. It runs gfx950 decode, MTP verify and draft extend at page size 16, so runs without MTP hit it too. sgl-project/sglang#39513 reports that the rocm720 and rocm724 images map one runtime, so this change does nothing there.

Technical Details

The fix is in aiter/dist/device_communicators/vmm_allocator.py. Its CPU-only test is tests/test_vmm_allocator_load_hip.py.

  • load_hip_runtime() first looks for a libamdhip64.so* in /proc/self/maps and returns a CDLL of that path. It reads the file as bytes and decodes each line with os.fsdecode(), so a mapped file name that is not valid UTF-8 does not stop the lookup. The kernel writes a newline in a maps path as \012, so the lookup decodes it. If none is mapped, or the mapped path fails to load, it runs the soname search as before (as asm_utils.load_hip() does).
  • If the mapped path fails to load, load_hip_runtime() logs a warning before the soname search, because that search can map a second runtime.
  • If the process has no /proc/self/maps, load_hip_runtime() falls back to the soname search instead of raising.
  • The module no longer calls load_hip_runtime() at import. A cached _hip() loads its handle on the first VMM call. Before, an import of vmm_allocator that ran before torch mapped its runtime found nothing in /proc/self/maps and mapped the second copy by name. load_hip_runtime() itself stays uncached.

UncachedIpcHeap._load_hip() and the two VMM paths in custom_all_reduce.py call load_hip_runtime(). All three run after torch has initialized HIP, so they get torch's copy.

Rocm/aiter5182 adds a shared loader in aiter/jit/utils/hip_runtime.py and moves vmm_allocator.py, chip_info.py and rocm_version.py onto it. That loader finds the loaded copy by soname with RTLD_NOLOAD. Whichever PR lands second should merge the two lookups in the shared loader. smi_monitor.py also loads by soname, but only the op-test harness (aiter/test_common.py) uses it.

Test Plan

CPU: python3 -m pytest -q tests/test_vmm_allocator_load_hip.py. The test fakes /proc/self/maps and mocks ctypes.CDLL. It covers the mapped lookup, the (deleted) suffix, the \012 decode, a mapped file name that is not valid UTF-8, the fallback and its warning, a missing /proc/self/maps, and no load at import.

GPU: one MI355X GPU, the stock SGLang ROCm 10 image rocm/sgl-dev:v0.5.21-rocm10-mi35x-20261009 (SGLang main, aiter e7d2453f2), no other patches. Each run is a fresh process that runs SGLang's vattn_asm side-stream and graph-capture test, either alone or after aiter's HIP loader (UncachedIpcHeap._load_hip()). 20 runs per case, once with the image's vmm_allocator.py and once with this PR's mounted over it. The table below is from 2026-10-21, with vmm_allocator.py at ce9f1e8 (the final loader). The runs on 2026-10-14 with 1f30f43 gave the same result.
repro.py:

import sys, unittest, torch
from sglang.srt.distributed.device_communicators.cuda_wrapper import find_loaded_library
torch.cuda.current_device()
if sys.argv[1] == "aiter":
    from aiter.ops.flydsl.quick_allreduce_int4_ipc import UncachedIpcHeap
    UncachedIpcHeap._load_hip()
mapped = sorted({l.split()[-1] for l in open("/proc/self/maps") if "libamdhip64" in l})
print(mapped, find_loaded_library("libamdhip64"))
sys.path.insert(0, "/sgl-workspace/sglang/test/registered/amd")
import test_vattn_segplan_mi35x as t
name = "TestVattnSegPlan.test_launches_on_a_side_stream_and_under_graph_capture"
unittest.TextTestRunner().run(unittest.defaultTestLoader.loadTestsFromName(name, t))

python3 repro.py stock runs the SGLang test without the aiter loader. python3 repro.py aiter runs the aiter loader first.
Full server: a TP8 Qwen3.8 server on the same image, with a peer buffer allocated through UncachedIpcHeap (sgl-project/sglang#43516), through decode graph capture and serving.

Test Result

CPU: 8 passed.

GPU:

Case libamdhip64 mapped vattn_asm binds Test
SGLang only, aiter loader not run 1 _rocm_sdk_core 20/20 pass
aiter loader, main 2 _rocm_sdk_devel 20/20 fail: vattn_asm: hipModuleLaunchKernel failed: 709 (context is destroyed)
aiter loader, this PR 1 _rocm_sdk_core 20/20 pass

With this PR only one libamdhip64 is mapped, and the 709 no longer reproduces. The first row is the control: SGLang alone maps one runtime and passes.
The full server shows the same (run on 2026-10-14 with 1f30f43). Without this PR it hit the 709 in decode graph capture. With it, the server came up and served requests, and every rank mapped only _rocm_sdk_core/lib/libamdhip64.so.7.

Submission Checklist

ROCm 10 pip images ship the HIP runtime in _rocm_sdk_core and a second
copy in _rocm_sdk_devel. Torch maps the core copy. When -devel is on
LD_LIBRARY_PATH, CDLL("libamdhip64.so") maps the devel copy as a second
runtime. Other ctypes HIP users that take the copy from /proc/self/maps
can then bind the wrong one. Their launches fail with
hipErrorContextIsDestroyed.

Bind to the copy that is already in /proc/self/maps. Keep the soname
search as the fallback. Add a CPU-only test.

Co-authored-by: Cursor <[email protected]>
@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: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 6398 --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.

omirosh and others added 2 commits October 14, 2026 10:13
…ssing /proc/self/maps

vmm_allocator called load_hip_runtime() at import. If the module is imported
before torch maps its HIP runtime, /proc/self/maps has no libamdhip64 yet, so
the call falls back to the soname and can still map a second runtime on ROCm 10
split-wheel images. The module now loads its handle on first use through a
cached _hip(). load_hip_runtime() itself stays uncached, as before.

An unreadable /proc/self/maps (no procfs) now falls back to the soname search
instead of raising at import.

Co-authored-by: Cursor <[email protected]>
@omirosh
omirosh marked this pull request as ready for review October 14, 2026 07:20
@omirosh
omirosh requested review from a team and a balanced review from Copilot October 14, 2026 07:20
@github-actions github-actions Bot changed the title [HIP] [Bugfix] Bind load_hip_runtime to the HIP runtime already loaded [Bugfix] Bind load_hip_runtime to the HIP runtime already loaded Oct 14, 2026

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.

🟢 Approval recommended

The implementation is focused and its changed loader behaviors are directly covered by regression tests.

0 open findings

What changed in this PR

Updates HIP runtime loading to reuse Torch’s mapped runtime and avoid duplicate ROCm runtimes.

Changes:

  • Prefer mapped libamdhip64.so* paths with safe fallback.
  • Lazily cache the runtime handle.
  • Add CPU-only loader regression tests.
File Description
aiter/​dist/​device_communicators/​vmm_allocator.py Implements mapped-runtime lookup and lazy loading.
op_tests/​test_vmm_allocator_load_hip.py Tests lookup, fallback, deleted paths, and import behavior.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 07:36

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

Procfs-escaped pathnames are passed directly to CDLL, breaking environments whose runtime path contains escaped characters.

0 open findings

🧠 Review effort: Balanced

The kernel writes a newline in a maps path as "\012". Decode it before CDLL.
Keep trailing spaces in the path. Add a CPU test for an escaped path.

Co-authored-by: Cursor <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 08:35

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.

🔵 Needs a closer look

Low-level runtime binding requires final human review because the complete head lacks reproducible end-to-end GPU validation.

0 open findings

🧠 Review effort: Balanced

@zufayu

zufayu commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

The fix itself looks reasonable, but op_tests should remain focused on operator/GPU tests. op_tests/test_vmm_allocator_load_hip.py is a CPU-only unit test that only mocks /proc/self/maps and ctypes.CDLL; placing it under op_tests/test_*.py also makes the GPU Standard Test shards collect it.

Please remove this CPU-only test from op_tests (or move it to an appropriate CPU unit-test suite if/when one is available), and validate the change thoroughly on the target ROCm 10/SGLang environment. Please report the manual validation results, including confirmation that only one libamdhip64 is mapped and that the previous hipModuleLaunchKernel failed: 709 (context is destroyed) failure no longer reproduces.

op_tests holds operator tests, and the GPU Standard Test shards collect
every op_tests/test_*.py. aiter has no CPU unit-test suite yet.

Co-authored-by: Cursor <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 09:24

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.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

@omirosh

omirosh commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

@zufayu thanks. I removed the CPU-only test in 77e7b41, so the PR now changes only vmm_allocator.py.

I validated it on one MI355X GPU with the stock SGLang ROCm 10 image rocm/sgl-dev:v0.5.21-rocm10-mi35x-20261009 (SGLang main, aiter e7d2453f2) and no other patches. Each run is a fresh process that runs SGLang's vattn_asm side-stream and graph-capture test, either alone or after aiter's HIP loader (UncachedIpcHeap._load_hip()). 20 runs per case:

Case libamdhip64 mapped vattn_asm binds Test
SGLang only, aiter loader not run 1 _rocm_sdk_core 20/20 pass
aiter loader, main 2 _rocm_sdk_devel 20/20 fail: vattn_asm: hipModuleLaunchKernel failed: 709 (context is destroyed)
aiter loader, this PR 1 _rocm_sdk_core 20/20 pass

With this PR only one libamdhip64 is mapped, and the 709 no longer reproduces. The first row is the control: SGLang alone maps one runtime and passes.

A full server shows the same. A TP8 Qwen3.8 server on the same image, with a peer buffer allocated through UncachedIpcHeap (sgl-project/sglang#43516), hit the 709 in decode graph capture without this PR. With it, the server came up and served requests, and every rank mapped only _rocm_sdk_core/lib/libamdhip64.so.7.

repro.py

import sys, unittest, torch
from sglang.srt.distributed.device_communicators.cuda_wrapper import find_loaded_library

torch.cuda.current_device()
if sys.argv[1] == "aiter":
    from aiter.ops.flydsl.quick_allreduce_int4_ipc import UncachedIpcHeap
    UncachedIpcHeap._load_hip()
mapped = sorted({l.split()[-1] for l in open("/proc/self/maps") if "libamdhip64" in l})
print(mapped, find_loaded_library("libamdhip64"))

sys.path.insert(0, "/sgl-workspace/sglang/test/registered/amd")
import test_vattn_segplan_mi35x as t
name = "TestVattnSegPlan.test_launches_on_a_side_stream_and_under_graph_capture"
unittest.TextTestRunner().run(unittest.defaultTestLoader.loadTestsFromName(name, t))

python3 repro.py stock runs the SGLang test without the aiter loader. python3 repro.py aiter runs the aiter loader first. Run it once with the image's vmm_allocator.py and once with this PR's mounted over it.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 10: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.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 10: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.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

Copilot AI balanced review requested due to automatic review settings October 15, 2026 14:54

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.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

Copilot AI balanced review requested due to automatic review settings October 20, 2026 12:39

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.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

Copilot AI balanced review requested due to automatic review settings October 20, 2026 12:39

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.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

@zufayu

zufayu commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

[ROCm/aiter] PR #6398 — [Bugfix] Bind load_hip_runtime to the HIP runtime already loaded

vmm_allocator's HIP runtime loader now binds to the libamdhip64 copy already mapped in /proc/self/maps (torch's) instead of dlopening a second runtime by soname, and the load moves from import time to the first VMM call.

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — the changed path is runtime code executed in the serving path (vmm_allocator.py backs the gfx1250 VMM IPC paths in custom_all_reduce.py and the quick_allreduce IPC heap), but the PR ships no test target: the CPU-only test was removed at review request and not relocated, so there was nothing for the validator to run
Perf (advisory): NOT RUN — the PR changes runtime code but ships no test or perf target; no benchmark entry point exists for a host-side ctypes loader that runs once per process

⚠️ [verified] The PR ships zero CI coverage for the new loader logic in vmm_allocator.py: the CPU-only test (op_tests/test_vmm_allocator_load_hip.py) was removed at review request and not relocated, even though tests/ is a mock-based CPU unit-test suite that CI already runs (python3 -m pytest -q tests, aiter-test.yaml:953) — so nothing guards the maps-first lookup, the \012 decode, the (deleted) handling, or the lazy _hip() load against regression. Author must relocate the removed test into tests/ (or land it with the #5182 shared loader) so the lookup logic stays exercised.

⚠️ [verified] The soname-only ctypes.CDLL("libamdhip64.so") load this PR fixes in vmm_allocator.py still sits unchanged in aiter/jit/utils/chip_info.py:574 (lru_cached, so the wrong copy stays bound for the process lifetime once hit), aiter/dist/device_communicators/rocm_version.py:108, and aiter/smi_monitor.py:201 — on the same ROCm 10 image with _rocm_sdk_devel/lib on LD_LIBRARY_PATH, any path reaching those loaders can re-map the second runtime and bring back the 709 failure class the PR describes. Author must land the planned shared loader (#5182) porting this maps-first lookup to all of them, or extend this PR to the three sites.

📝 [verified] In vmm_allocator.py, if the mapped libamdhip64.so* is unlinked while still mapped (the (deleted) case the new code strips), every ctypes.CDLL(path) in the maps loop fails and except OSError: pass silently drops the error before falling back to the soname search — which can map the devel copy and re-introduce the second runtime, while the final OSError reports only the soname candidates, never that a mapped copy was found and failed to load. Reviewer should ask whether the mapped-path failure should be logged or named in the final error so this fallback stays diagnosable.

omirosh and others added 2 commits October 21, 2026 10:20
If CDLL() of the mapped libamdhip64 path raises OSError, load_hip_runtime()
falls back to the soname search. That search can map a second runtime.
Log the error once, so that the fallback is visible.

Co-authored-by: Cursor <[email protected]>
tests/ is the CPU unit-test suite, and CI runs it with
"python3 -m pytest -q tests". Add a case for the fallback: the mapped
path fails to load, and the loader logs a warning and tries the soname.

Co-authored-by: Cursor <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 21, 2026 07:20

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

Strict text decoding can make an unrelated mapped filename abort HIP runtime loading.

No open issues 🎉

Review effort: Balanced

A text-mode read decodes /proc/self/maps as strict UTF-8. One mapped
file name that is not valid UTF-8 then raises UnicodeDecodeError, and
load_hip_runtime() fails. Read the file as bytes and decode each line
with os.fsdecode(). ctypes.CDLL encodes the path back to the same bytes.

Co-authored-by: Cursor <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 21, 2026 07:27

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.

🔵 Needs a closer look

Reported GPU validation predates the final loader changes, so runtime identity safety still needs human confirmation.

No open issues 🎉

Review effort: Balanced

@omirosh

omirosh commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

I repeated the single-GPU repro on 2026-10-21 with ce9f1e8: 20/20 pass, one libamdhip64 mapped, and vattn_asm binds _rocm_sdk_core. aiter main still fails 20/20 with the 709.

@zufayu zufayu 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.

The targeted ROCm 10 duplicate-HIP-runtime failure is fixed, the loader regression tests are now under tests/, the final head was revalidated against the 709 reproducer and full TP8 server, and the required CI is green. LGTM.

@zufayu
zufayu merged commit 6ba8f8c into ROCm:main Oct 22, 2026
eppaneamd added a commit to eppaneamd/sglang that referenced this pull request Nov 14, 2026
On ROCm 10 images that ship the HIP runtime in both _rocm_sdk_core and _rocm_sdk_devel, with the devel copy on
LD_LIBRARY_PATH, AITER before ROCm/aiter#6398 maps a second HIP runtime beside torch's. hipIpcGetMemHandle then
rejects torch's allocation, and loading fails in SymmetricArena. The server-arguments row now names the requirement.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Rony Leppänen <[email protected]>
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.

5 participants