Skip to content

Fix R-Fork weight loading on MI308X - #67

Merged
apinge merged 23 commits into
apinge:qwen38-flash-next-amd-current-fix-installfrom
yicyang:rfork-rocm-mi308x-pr
Nov 9, 2026
Merged

apinge merged 23 commits into
apinge:qwen38-flash-next-amd-current-fix-installfrom
yicyang:rfork-rocm-mi308x-pr

Conversation

@yicyang

@yicyang yicyang commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

R-Fork (--load-format remote_instance) starts a new instance by copying weights from a running seed instead of
reading the checkpoint. On MI308X with Qwen3.8-Flash-Next-PTPC-FP8, this cuts startup from about 126 s to about 67 s
(TP2). However, the seed holds post-processed weights, and the current loader does not account for that. As a result,
the new instance crashes or loads mismatched derived state. This PR fixes the RCCL (nccl) backend.

Changes

  • loader.py (NCCL backend only)
    • Run process_weights_after_loading before receiving, so the receiver has the seed's layout (fnuz FP8, PyHIP
      packing). Without it, the new instance crashes.
    • After receiving, rebuild state derived from the weights: gemma_weight via _rebind_parameter_aliases, and
      k/v_scale_float from the received tensors. A raw copy skips the hooks that compute these.
    • Raise if build_group fails. Otherwise the new instance hangs in the broadcast.
  • weight_exporter.py: return an error when the send group is missing, instead of crashing the seed with a
    KeyError.
  • SGLANG_DEBUG_WEIGHT_CHECKSUM_DIR (off by default): dump per-rank tensor checksums after loading, to verify
    that the copied weights match the seed.
  • scripts/:
    • launch_qwen38_flash_next_fp8_mi308x_rfork.sh (MODE=seed|rfork);
    • launch_qwen38_flash_next_fp8_mi308x_rfork_cross_container.sh, which starts the new instance in its own container on the same node.
  • benchmark/rfork_mi308x/:
    • startup benchmark (bench_startup.py, disk vs R-Fork);
    • correctness check (verify_rfork.py);
    • README and CROSS_CONTAINER.md with usage and results.

The transfer_engine (Mooncake) and modelexpress paths are unchanged.

Dependencies

None beyond the SGLang ROCm image.

  • The RCCL backend is torch.distributed's nccl backend. On ROCm PyTorch, that is RCCL, which ships with ROCm (/opt/rocm/lib/librccl.so; RCCL 2.30.4 in the tested image).
  • The HTTP calls to the seed use requests, which is already installed.

Requirements:

  • the new instance uses the same model and TP size as the seed;
  • group ports are passed as a JSON list with one port per rank, outside ip_local_port_range;
  • for a new instance in another container on the same node: --network=host --ipc=host and the GPU devices.

Test

Single node, 8x MI308X. Launch to ready is the mean of 2 runs, with kernel caches and page cache warm.

TP2 TP4
disk 126.9 s 125.9 s
R-Fork RCCL 66.8 s 79.1 s¹
R-Fork RCCL, new instance in another container 71.5 s (1 run)² -

VALIDATION_ROW

  • Every rank's tensor checksums match the seed.
  • GSM8K: 0.975-0.977 for the R-Fork instance, 0.978-0.981 for the disk-loaded seed.

¹ Measured before the last cleanup commits. Those commits removed debug-only code and the Mooncake changes; the NCCL path is unchanged.
² That run had NCCL_DEBUG=INFO on. The weight transfer itself took the same 3 s as within one container.

python3 benchmark/rfork_mi308x/verify_rfork.py --tp 2 --seed-gpus 0,1 --gpus 2,3 --out /tmp/verify_tp2
python3 benchmark/rfork_mi308x/bench_startup.py --prime --page-cache warm --repeat 2 \
  --gpus 2,3 --seed-gpus 0,1 --out /tmp/rfork_tp2

Known issue (pre-existing, not changed here): if a new instance dies after asking the seed to set up the transfer group
but before joining it, the seed blocks in the rendezvous for up to 30 minutes.

… FP8 on MI308X

- Client runs quant post-processing before receiving so tensor layout/dtype
  (fnuz, PyHIP GR packing) matches the seed.
- Recompute load-time derived buffers (gemma_weight) after the raw copy.
- Exchange a (name, shape, dtype) manifest digest before broadcasting; a
  mismatch now fails fast on both sides instead of deadlocking RCCL
  (seed RoPE caches grow at runtime).
- Seed no longer crashes when a send group failed to init; client checks
  build_group result.
- SGLANG_DEBUG_WEIGHT_CHECKSUM_DIR: per-rank post-load checksums.
- Client launch script and validation report (benchmark/rfork_mi308x).
…ke HIP R-Fork

- R-Fork transfer_engine: on ROCm register whole allocator segments, working
  around Mooncake HIP transport resolving addresses from the IPC allocation base
  (silently wrong weights otherwise); add standalone repro.
- R-Fork transfer_engine: recompute derived buffers after the copy.
- Benchmark tools: startup timer, per-model page-cache eviction, first-request
  latency, raw fastsafetensors throughput, weight-cache daemon shim.
- Report: kernel-cache transfer, storage loaders (fastsafetensors, runai,
  prefetch, no-mmap), Mooncake vs RCCL, weight_cache limitation.
… load_weights and full launch commands

SGLANG_DEBUG_WEIGHT_ITER_TIMING logs time blocked on the checkpoint iterator vs
time in load_weights, and splits fastsafetensors into file read and per-tensor
distribution.
Receivers run process_weights_after_loading before the copy, which sets
k_scale_float/v_scale_float from the -1 placeholders (1.0). The copy then
brings the seed's final k/v_scale tensors, but the floats consumed by the
attention backends stayed at 1.0 for checkpoints with non-unit KV scales.
Refresh them from the received tensors next to the alias rebind.
- warm page cache is now prepared (read in) and verified like cold; abort
  if residency is not reached (O_DIRECT loaders left "warm" runs cold)
- remove a stale log before launch so an old "fired up" is not taken as ready
- non-R-Fork runs use HIP_VISIBLE_DEVICES (fastsafetensors uses cuda:rank
  and ignores --base-gpu-id); R-Fork ranges must be contiguous, equal size,
  disjoint
- refuse to launch on GPUs with >2 GiB in use; interleave repeats
…TP4 rerun

Full rerun at 378743602 with the same scripts and table layout for TP2 and
TP4: startup matrix (6 configs x page cache warm/cold x 2), cold kernel
cache, per-rank checksums and GSM8K for disk/RCCL/Mooncake. README
shortened; adds script usage.
Drop what was only used for analysis or convenience:
- RCCL transfer manifest check (guarded a buffer-transfer variant that is
  not used; parameters-only transfer as before)
- SGLANG_DEBUG_WEIGHT_ITER_TIMING and the iterator/fastsafetensors timers
- rfork client launch script and analysis-only benchmark tools
@yicyang
yicyang requested a balanced review from Copilot October 27, 2026 07:01

Copilot AI 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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

6 open issues
What changed in this PR

Fixes remote-instance (“R-Fork”) weight loading correctness on MI308X/ROCm by making receiver-side weight layout match the seed, restoring derived state skipped by raw copies, and hardening error handling and diagnostics.

Changes:

  • Ensure remote-instance receivers pre-apply quant post-processing before raw weight transfer, and restore derived buffers/aliases after receiving.
  • Add ROCm-specific Mooncake HIP workaround to register full allocator segments to prevent shifted reads.
  • Add debug checksum dumping for post-load verification and add MI308X benchmark/correctness tooling.
File Description
python/​sglang/​srt/​model_loader/​remote_instance_weight_loader_utils.py On ROCm, registers whole allocator segments with Mooncake transfer engine to avoid shifted reads.
python/​sglang/​srt/​model_loader/​loader.py Pre-matches seed’s post-processed layout before transfer; restores derived state after transfer; raises on failed group build.
python/​sglang/​srt/​model_executor/​model_runner_components/​weight_exporter.py Returns a clear error if the send group is missing instead of raising KeyError.
python/​sglang/​srt/​model_executor/​model_runner_components/​load_model_utils.py Adds optional per-rank param/buffer checksum dumping controlled by an env var.
python/​sglang/​srt/​environ.py Introduces SGLANG_DEBUG_WEIGHT_CHECKSUM_DIR env var.
benchmark/​rfork_mi308x/​verify_rfork.py Script to compare per-tensor checksums across seed/client and run GSM8K for correctness.
benchmark/​rfork_mi308x/​mooncake_hip_offset_repro.py Minimal repro for Mooncake HIP offset bug motivating full-segment registration.
benchmark/​rfork_mi308x/​evict_model_cache.py Utility to measure/evict page-cache residency for model files.
benchmark/​rfork_mi308x/​bench_startup.py Benchmark harness for startup time across load modes/cache states on MI308X.
benchmark/​rfork_mi308x/​README.md Documents observed performance wins, correctness validation, and how to reproduce.

Review effort: Lite

@yicyang
yicyang marked this pull request as ready for review October 28, 2026 07:57
@apinge
apinge self-requested a review October 29, 2026 12:19
yicyang and others added 4 commits November 2, 2026 02:00
Starts the new instance in its own container on the same node (host network,
shared IPC, GPU devices) and runs MODE=rfork against a seed in another
container. CROSS_CONTAINER.md lists the requirements, usage and one TP2
measurement.
…over RCCL

- transfer_engine path is back to the base branch: no whole-segment
  registration workaround, no pre-receive processing or derived-state
  restore for it (both now NCCL only)
- remove mooncake_hip_offset_repro.py
- bench_startup.py: configs disk and rfork_nccl only; drop the kernel-cache
  option; place every run with HIP_VISIBLE_DEVICES
- verify_rfork.py: disk seed + RCCL instance only
- launcher renamed to launch_qwen38_flash_next_fp8_mi308x_rfork.sh with
  MODE=seed|rfork; cross-container launcher is RCCL only
- README: R-Fork scale-out effect, dependencies (none beyond ROCm), usage
@yicyang
yicyang marked this pull request as draft November 9, 2026 02:38
@yicyang
yicyang marked this pull request as ready for review November 9, 2026 05:23
@apinge

apinge commented Nov 9, 2026

Copy link
Copy Markdown
Owner

The test has passed according to doc benchmark/rfork_mi308x/README.md benchmark/rfork_mi308x/README.md

@apinge
apinge merged commit 6ca1975 into apinge:qwen38-flash-next-amd-current-fix-install Nov 9, 2026
1 check failed
apinge pushed a commit that referenced this pull request Nov 10, 2026
* Fix R-Fork weight loading on MI308X (#67)

* [AMD] Make R-Fork (remote_instance, nccl) work for Qwen3.8-Flash-Next FP8 on MI308X

- Client runs quant post-processing before receiving so tensor layout/dtype
  (fnuz, PyHIP GR packing) matches the seed.
- Recompute load-time derived buffers (gemma_weight) after the raw copy.
- Exchange a (name, shape, dtype) manifest digest before broadcasting; a
  mismatch now fails fast on both sides instead of deadlocking RCCL
  (seed RoPE caches grow at runtime).
- Seed no longer crashes when a send group failed to init; client checks
  build_group result.
- SGLANG_DEBUG_WEIGHT_CHECKSUM_DIR: per-rank post-load checksums.
- Client launch script and validation report (benchmark/rfork_mi308x).

* [AMD] Validate ModelExpress-style startup paths on MI308X; fix Mooncake HIP R-Fork

- R-Fork transfer_engine: on ROCm register whole allocator segments, working
  around Mooncake HIP transport resolving addresses from the IPC allocation base
  (silently wrong weights otherwise); add standalone repro.
- R-Fork transfer_engine: recompute derived buffers after the copy.
- Benchmark tools: startup timer, per-model page-cache eviction, first-request
  latency, raw fastsafetensors throughput, weight-cache daemon shim.
- Report: kernel-cache transfer, storage loaders (fastsafetensors, runai,
  prefetch, no-mmap), Mooncake vs RCCL, weight_cache limitation.

* benchmark/rfork_mi308x: add per-phase startup time breakdown

* Add weight-iterator timing debug switch; report per-rank disk read vs load_weights and full launch commands

SGLANG_DEBUG_WEIGHT_ITER_TIMING logs time blocked on the checkpoint iterator vs
time in load_weights, and splits fastsafetensors into file read and per-tensor
distribution.

* benchmark/rfork_mi308x: add bench_startup.py startup-time matrix

* benchmark/rfork_mi308x: add TP2 results and seed-stall finding

* benchmark/rfork_mi308x: correct runai/no-mmap memory note (peak only, KV unaffected)

* remote_instance: refresh KV-cache scale floats after receiving weights

Receivers run process_weights_after_loading before the copy, which sets
k_scale_float/v_scale_float from the -1 placeholders (1.0). The copy then
brings the seed's final k/v_scale tensors, but the floats consumed by the
attention backends stayed at 1.0 for checkpoints with non-unit KV scales.
Refresh them from the received tensors next to the alias rebind.

* benchmark/rfork_mi308x: make bench_startup cache/GPU state explicit

- warm page cache is now prepared (read in) and verified like cold; abort
  if residency is not reached (O_DIRECT loaders left "warm" runs cold)
- remove a stale log before launch so an old "fired up" is not taken as ready
- non-R-Fork runs use HIP_VISIBLE_DEVICES (fastsafetensors uses cuda:rank
  and ignores --base-gpu-id); R-Fork ranges must be contiguous, equal size,
  disjoint
- refuse to launch on GPUs with >2 GiB in use; interleave repeats

* benchmark/rfork_mi308x: add verify_rfork.py; rewrite README with TP2/TP4 rerun

Full rerun at 378743602 with the same scripts and table layout for TP2 and
TP4: startup matrix (6 configs x page cache warm/cold x 2), cold kernel
cache, per-rank checksums and GSM8K for disk/RCCL/Mooncake. README
shortened; adds script usage.

* benchmark/rfork_mi308x: clarify R-Fork cold page-cache cells were not run

* benchmark/rfork_mi308x: measure R-Fork configs with a cold page cache too

* Trim PR to necessary changes

Drop what was only used for analysis or convenience:
- RCCL transfer manifest check (guarded a buffer-transfer variant that is
  not used; parameters-only transfer as before)
- SGLANG_DEBUG_WEIGHT_ITER_TIMING and the iterator/fastsafetensors timers
- rfork client launch script and analysis-only benchmark tools

* benchmark/rfork_mi308x: README with usage and results only

* benchmark/rfork_mi308x: drop prefetch config and results (no gain measured)

* benchmark/rfork_mi308x: stop the seed when a run aborts

* load_model_utils: correct the checksum peak-memory comment

* scripts: add a reference launcher for the fast-startup options

* scripts: add a cross-container R-Fork reference launcher

Starts the new instance in its own container on the same node (host network,
shared IPC, GPU devices) and runs MODE=rfork against a seed in another
container. CROSS_CONTAINER.md lists the requirements, usage and one TP2
measurement.

* Drop Mooncake, storage loaders and seed-startup options; keep R-Fork over RCCL

- transfer_engine path is back to the base branch: no whole-segment
  registration workaround, no pre-receive processing or derived-state
  restore for it (both now NCCL only)
- remove mooncake_hip_offset_repro.py
- bench_startup.py: configs disk and rfork_nccl only; drop the kernel-cache
  option; place every run with HIP_VISIBLE_DEVICES
- verify_rfork.py: disk seed + RCCL instance only
- launcher renamed to launch_qwen38_flash_next_fp8_mi308x_rfork.sh with
  MODE=seed|rfork; cross-container launcher is RCCL only
- README: R-Fork scale-out effect, dependencies (none beyond ROCm), usage

* benchmark/rfork_mi308x: README results from the TP2 re-run at the RCCL-only head

* benchmark/rfork_mi308x: README TP2 results re-measured at the rebased head

---------

Co-authored-by: yicyang <[email protected]>

(cherry picked from commit 6ca1975 on qwen38-flash-next-amd-current-fix-install)
Conflicts resolved in loader.py (post_load_weights rename, import block) and
load_model_utils.py (PLE offload condition now read via get_exec()).
Signed-off-by: yicyang <[email protected]>
Signed-off-by: Ethan Yang <[email protected]>

* R-Fork: adapt the cherry-picked scripts to qwen4_v0.5.21

Launch scripts live in evaluation/ on this branch, so move the R-Fork
launchers there and point bench_startup.py at evaluation/. The base
launch script defaults MTP=1 here; R-Fork is validated without the MTP
draft model, so the R-Fork launcher and bench_startup.py default MTP=0.

Signed-off-by: yicyang <[email protected]>

* [CI] Add a same-container TP2 R-Fork case to the Qwen4 CI

Qwen3.8-Flash-TP2-RFork starts a TP2 seed from disk, then a TP2 instance
on two other idle GPUs that pulls the seed's weights over RCCL
(--load-format remote_instance). It fails unless the new instance loads
via R-Fork, every rank's per-tensor checksums match the seed, and the new
instance passes the TP2 GSM8K gate (reference 98.0%). The summary also
reports the new instance's startup time and load_weight. Under --pressure
it only checks loading and checksums.

Signed-off-by: yicyang <[email protected]>

* R-Fork cross-container launcher: drop the private workspace mount

The launcher no longer requires or mounts a host workspace directory. The
new container runs the image's SGLang at REPO_DIR (default /opt/sglang);
DOCKER_ARGS passes extra docker run arguments, e.g. a mount for a host
checkout used with REPO_DIR.

Signed-off-by: yicyang <[email protected]>

* [CI] Run the R-Fork case in the daily schedule only; drop README footnotes

- Qwen3.8-Flash-TP2-RFork is daily-only: PR runs skip it, the daily
  schedule and an explicit case selection run it. matrix_cases() in
  qwen4_ci.py now builds the Actions matrix and is unit tested.
- Under --pressure the R-Fork case also checks the complete seed and
  server logs after shutdown, like the other pressure cases.
- README: remove the measurement footnotes.

Signed-off-by: yicyang <[email protected]>

---------

Signed-off-by: yicyang <[email protected]>
Signed-off-by: Ethan Yang <[email protected]>
Co-authored-by: yicyang <[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.

3 participants