Skip to content

[ROCm] Stage large pageable H2D copies instead of pinning them in place - #37720

Merged
HaiShaw merged 1 commit into
sgl-project:mainfrom
JohnQin-llm:fix/rocm-pageable-h2d-staging
Sep 6, 2026
Merged

HaiShaw merged 1 commit into
sgl-project:mainfrom
JohnQin-llm:fix/rocm-pageable-h2d-staging

Conversation

@JohnQin-llm

@JohnQin-llm JohnQin-llm commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Loading GLM-5.2 onto 4× MI355X takes 8 minutes 26 seconds. Almost none of that is spent copying — the GPUs are stalled, waiting. With this change the same load takes 40 seconds.

Here is the chain, and each step is ordinary behaviour on its own:

  1. The weight loader mmaps the safetensors files, so every tensor's source is a file-backed page in host memory.
  2. Above GPU_PINNED_MIN_XFER_SIZE the HIP runtime skips its own staging buffer and instead registers those pages with the GPU, DMA-ing straight out of them. That threshold defaults to 1 MB (ROCclr flags.hpp), and GLM-5.2's tensors run to 1903 MB, so essentially every one of them takes this path.
  3. Registered pages get an MMU notifier. The kernel now has to tell the GPU driver whenever it touches them — page cache reclaim, readahead, or the munmap when the loader finishes a shard.
  4. On that notification KFD suspends this process's GPU queues on every device it holds, until the range is restored. About 10 ms each time.

Step 1 makes step 4 fire once per tensor. GLM-5.2 has 117410 of them, so the loader spends most of its wall clock suspended rather than copying — with no error and no warning, which is why this looks like "loading is just slow on ROCm".

Raising the default from 1 MB to 4 GiB — above the largest tensor — puts step 2 back on the staging path. Nothing gets registered, so steps 3 and 4 never happen.

Impact

Seconds to Load weight end, per rank:

per rank slowest
tp=2, before → after 232.7 / 300.4 → 41.6 / 43.4 300.4 → 43.4 s
tp=4, before → after 139.8 / 140.4 / 445.4 / 505.7 → 38.7 / 39.3 / 39.8 / 40.4 505.7 → 40.4 s

Note the before-row at tp=4: two ranks finish in 140 s and two take 3× longer. That spread — 366 s of it — collapses to 1.7 s, which is what you would expect if the cause is an eviction storm some ranks fall into rather than anything about the ranks themselves.

Evidence

Read from the KFD SMI event channel during a tp=4 load: 315780 eviction events, trigger 100% USERPTR. All of them fall inside [Load weight begin, Load weight end]; steady-state serving records zero. The two slow ranks see 39352 each, the two fast ones 122 each — 39352 is one per logical weight (117410 / 3). Inter-arrival times cluster at p50 10.9 ms with p10–p90 of 8.8–14.1 ms, far too narrow for a random stream: it is a saturated invalidate → evict → restore loop.

Two controls, since a 12× speedup on a load path invites doubt:

  • The load really completes. 197.71 GB resident per rank afterwards, identical to the unpatched run, and the server generates correct output.
  • It is not a warm page cache. A control run with a warm cache and the change reverted reproduced 229.2 / 312.1 s.

Modifications

One setdefault in handle_amd_specifics, which already guards on get_platform().is_hip, so nothing changes off ROCm. setdefault rather than an assignment, so an operator who has tuned the threshold keeps their value — the test pins that, along with the HIP and non-HIP cases.

Checklist

  • Format the code and run unit tests
  • Add unit tests covering the change

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #33722540716
Latest PR Test (Extra): ❌ Run #33722540504
Latest PR Test (AMD ROCm 7.2): ❌ Run #33722540717

@github-actions github-actions Bot added the amd label Sep 3, 2026
@JohnQin-llm
JohnQin-llm force-pushed the fix/rocm-pageable-h2d-staging branch from 5e10bea to 59c5910 Compare September 3, 2026 06:05
Weight loading on ROCm spends most of its wall clock with the GPU queues
suspended, with no error and no warning. On MI355X loading GLM-5.2 (408 GB,
117410 tensors) the slowest rank takes 505.7 s at tp=4 and 300.4 s at tp=2.
With this change they take 40.4 s and 43.4 s.

Above GPU_PINNED_MIN_XFER_SIZE the HIP runtime stops staging a large pageable
host-to-device copy through its own buffer and registers the source pages with
the GPU instead, DMA-ing directly out of them. Registered host memory carries an
MMU notifier, so any kernel-side touch of that mapping -- page cache reclaim,
readahead, the munmap when the loader moves to the next shard -- makes KFD
suspend the process's queues on every device it holds until the range is
restored, at roughly 10 ms an event. The safetensors mmap path gives every
tensor a file-backed source, so this fires once per tensor.

Raising the default threshold past the largest tensor makes the runtime stage
these copies, and it then registers nothing.

    tp=4, per rank                          slowest
    before   139.8 / 140.4 / 445.4 / 505.7   505.7 s
    after     38.7 /  39.3 /  39.8 /  40.4    40.4 s

12.5x on the slowest rank, and the spread across ranks collapses from 366 s to
1.7 s. Server startup to "Uvicorn running" at tp=4 was 12m43s before it.

Evidence, read from the KFD SMI event channel at tp=4: 315780 eviction events,
trigger 100% USERPTR, all of them inside [Load weight begin, Load weight end]
with steady-state serving recording zero. 39352 on each of the two slow ranks
against 122 on each of the two fast ones -- one per logical weight
(117410 tensors / 3). Inter-arrival p10/p50/p90 of 8.8 / 10.9 / 14.1 ms, far too
narrow for a random event stream; it is a saturated invalidate/evict/restore
loop.

Two controls, since a 12x speedup on a load path invites suspicion. The load is
complete: 197.71 GB resident per rank afterwards, identical to the unpatched
run, and the server generates correct output. The speedup is not page cache
warmth: a control run with a warm cache and the change reverted reproduced
229.2 / 312.1 s.

setdefault rather than an assignment, so an operator who has tuned the threshold
keeps their value; the test pins that.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@HaiShaw

HaiShaw commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 6, 2026
@HaiShaw

HaiShaw commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

@JohnQinAMD Can you post the results - the local run of test added?

@JohnQin-llm

Copy link
Copy Markdown
Contributor Author

@JohnQinAMD Can you post the results - the local run of test added?

@HaiShaw Local run of the added test, on MI355X / ROCm 7.2.0: Thanks

$ python3 -m pytest test/registered/unit/test_rocm_pageable_h2d_staging.py -v
============================= test session starts ==============================
platform linux -- Python 3.10.12, pytest-9.0.3, pluggy-1.6.0
rootdir: /.../sglang/test
configfile: pytest.ini
collecting ... collected 3 items

test_rocm_pageable_h2d_staging.py::TestRocmPageableH2DStaging::test_absent_off_hip PASSED [ 33%]
test_rocm_pageable_h2d_staging.py::TestRocmPageableH2DStaging::test_an_operators_value_survives PASSED [ 66%]
test_rocm_pageable_h2d_staging.py::TestRocmPageableH2DStaging::test_set_on_hip PASSED [100%]

======================== 3 passed, 3 warnings in 9.41s =========================

Environment: torch 2.9.1+rocm7.2.0.git7e1940d4, hip 7.2.26015-fc0010cf6a, ROCm 7.2.0.

@HaiShaw HaiShaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ROCm specific

@HaiShaw
HaiShaw merged commit 2c05ed4 into sgl-project:main Sep 6, 2026
224 of 261 checks passed
hdt98 pushed a commit to hdt98/sglang that referenced this pull request Sep 10, 2026
JohnQin-llm added a commit to JohnQin-llm/vllm-amd that referenced this pull request Sep 17, 2026
The weight loader mmaps the safetensors, so every tensor's H2D source is a
file-backed page. Above GPU_PINNED_MIN_XFER_SIZE the HIP runtime registers
those pages with the GPU rather than staging the copy, and each registration's
MMU notifier makes KFD suspend the process's GPU queues. There is no error and
no warning, just a slow loader. Raise the threshold so the staging path is
taken for weight-sized transfers.

Measured on 4x MI355X (gfx950), ROCm 7.2.3, TP4, DeepSeek-V4.1-Flash, a 475 GiB
checkpoint that is fully page-cached (/proc/diskstats shows 0 MB/s read for the
whole load, so this is not I/O):

  Loading weights took 231.75 s   without this change
  Loading weights took 108.31 s   with this change, nothing else set

setdefault, so an operator's own value survives. It is a HIP runtime flag, not
a vLLM one, so this is engine-agnostic; the SGLang counterpart is
sgl-project/sglang#37720, which reports a larger factor because that checkpoint
has many more, much smaller tensors.

Declared in additional_env_vars because check_and_update_config runs on the
driver, and the Ray executor copies env vars to workers by allowlist:
GPU_PINNED_MIN_XFER_SIZE matches none of DEFAULT_ENV_VAR_PREFIXES or
DEFAULT_EXTRA_ENV_VARS, so without it a Ray deployment would keep the slow path
on every worker.

Signed-off-by: JohnQinAMD <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

amd run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants