Skip to content

[Dev]: Add rank-shared host weight offload - #99

Merged
wtr0504 merged 9 commits into
SandAI-org:mainfrom
wtr0504:dev/offload_shm
Nov 7, 2026
Merged

wtr0504 merged 9 commits into
SandAI-org:mainfrom
wtr0504:dev/offload_shm

Conversation

@wtr0504

@wtr0504 wtr0504 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

🗂️ PR Category

  • ✨ New Feature
  • 🚀 Optimization (performance, memory, etc.)
  • 💥 Breaking Change
  • 🐛 Bug Fix
  • 🛠️ Development / Refactoring
  • 📚 Documentation
  • 🧹 Chore (Dependencies, CI/CD, Configuration, etc.)
  • 🧪 Testing

Description

Replicated models using host weight offload keep a pinned CPU copy of every weight on every rank. This change adds opt-in rank-shared host regions so ranks on the same machine can share one physical copy of identical weights while keeping pinned mappings for asynchronous H2D transfers.

Set offload_config.offload_host_weight_sharing to:

  • off (default): keep a private host copy per rank.
  • per_numa_node: share one copy among the ranks whose GPUs attach to the same NUMA node, with pages preferred on that node.
  • per_host_interleaved: share one copy per host, with pages interleaved across its NUMA nodes.

NUMA placement is best effort: if the mempolicy syscalls are unavailable or denied, a warning is logged and the current policy is kept. Unknown GPU NUMA affinity falls back to host grouping with the default policy.

Scope

Sharing applies to eligible unsharded CPU weights parked on a host-built model's first CUDA move. FSDP shards, DTensors, and weights materialized through to_empty keep their existing per-rank behavior. Ranks match weights by name, shape, dtype, and a sampled fingerprint; readers then compare the actual bytes before adopting a shared copy, so a fingerprint collision only costs a private copy.

How parking works

When sharing is enabled, parking is a WORLD collective over the Gloo group: every rank must move the same compiled models to CUDA in the same order, including ranks with no eligible CPU weights (they take part with empty offers).

  1. Plan (one gather). Each rank sends its hostname, sharing group, weight descriptors, and any local collection error. Each group's identical weights are laid out in the writer's order and split into chunks of about 256 MiB; a weight larger than that forms its own chunk.
  2. Round k parks chunk k of every group at once. Groups with fewer chunks take part with an empty one.
    • The group's writer creates an anonymous memfd, allocates it with posix_fallocate under the NUMA policy, maps and pins it, and fills it.
    • Exchange 1 (publish): all ranks agree on the writers' allocation and fill results, and readers learn the writer's /proc/<pid>/fd/<fd> path.
    • Readers map and pin the region, compare bytes, and hand off to the shared buffers.
    • Exchange 2 (handoff): all ranks confirm every handoff; only then does the writer close its memfd. Existing mappings keep the memory alive.
  3. Private weights park last. No collective follows, so a local failure there cannot strand peers.

A failure on any rank in planning, writer allocation, mapping or fill, or reader mapping or handoff is exchanged so that every rank raises the same error instead of hanging in a later collective.

Memory and capacity

  • Within a round, each host's transient pageable-plus-shared peak grows by at most one chunk per sharing group on that host (for example 2 × 256 MiB with per_numa_node on a two-socket host).
  • No files are created under MAGI_SHARED_BIN_PATH, and memfd regions are not limited by /dev/shm size.
  • If posix_fallocate returns ENOSPC, that chunk's weights stay private on every rank of the group, and the other chunks and groups continue.
  • Pages are allocated up front, so running out of capacity shows up as ENOSPC instead of a SIGBUS while filling the region.
  • During parking, each rank uses its host's CPUs divided by the number of ranks on that host as intra-op threads (never more than the current setting), so concurrent copies and comparisons do not oversubscribe the host. The previous setting is restored afterwards.

Requirements

  • Ranks of a sharing group must share a PID namespace, because readers open the writer's memfd through /proc/<pid>/fd/<fd>.
  • CUDA must be able to register memfd mappings (cudaHostRegister) and libcudart must be loadable; otherwise parking fails with an error that suggests offload_host_weight_sharing='off'.

H2D microbenchmark

A standalone H2D benchmark on 8× NVIDIA H200 GPUs and two Xeon Platinum 8558 CPUs compared the three placement modes. It was measured before the parking-protocol refactor; page placement is unchanged by it. Each active GPU repeatedly copied its 256 MiB source into its own device buffer; each per-GPU result is the median of five windows of 20 copies after warmup. The 1-GPU case uses GPU 0, the 4-GPU case uses GPUs 0–3 on NUMA node 0, and the 8-GPU case spans both NUMA nodes. This is a single run on one host, not an application throughput measurement.

Mode 1 GPU: GPU 0 (GB/s) 4 GPUs: average per GPU (GB/s) 8 GPUs: average per GPU (GB/s)
off 55.42 53.65 53.34
per_numa_node 54.97 54.22 53.28
per_host_interleaved 54.65 54.87 50.56

Page placement was checked through /proc/self/numa_maps: private and per-NUMA pages were local to each GPU's node, while host-interleaved pages were split evenly across the two nodes. Results can vary with topology, concurrent activity, transfer size, and copy scheduling.

Validation

At 9c58b96, on 8× RTX 5090 across two NUMA nodes: 204 passed.

  • test_rank_shared_host_weights.py (21 tests) covers:
    • physical sharing and NUMA page placement for both modes, including denied NUMA policy;
    • fingerprint collisions and allocation-time ENOSPC fallback;
    • failure propagation from six stages (planning, writer create, writer map, fill, reader map, reader handoff);
    • ranks with no eligible weights, CUDA-resident weights, or DTensors still joining parking;
    • pure planning tests for group intersection, alignment, and chunk splitting;
    • the per-host peak bound with concurrent groups;
    • compiled inference with the public compile config for both modes, whose outputs must match private offload bit for bit.
  • Regression: test_ep_shared_memory.py, test_weight_offload.py, test_h2d_reorder.py, test_offload_cache.py.
  • Commit hooks pass with the repository-pinned Black 23.3.0, isort 5.10.1, and autoflake 2.3.1.

Run from the repository root on a CUDA host:

PYTHONPATH=$PWD python -m pytest -q tests/feature_tests/offload/test_rank_shared_host_weights.py
PYTHONPATH=$PWD python -m pytest -q tests/feature_tests/test_ep_shared_memory.py tests/feature_tests/offload/test_weight_offload.py tests/feature_tests/offload/test_h2d_reorder.py tests/feature_tests/cache/test_offload_cache.py
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 5, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 5, 2026
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 5, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 5, 2026
@wtr0504 wtr0504 changed the title Add rank-shared host weight offload Nov 6, 2026
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 6, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 6, 2026
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 6, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 6, 2026
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 6, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 6, 2026
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 6, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 6, 2026
wtr0504 and others added 4 commits November 7, 2026 03:20
Park every sharing group's chunk k in WORLD round k instead of advancing
groups one at a time, with two exchanges per round (writer publish, reader
handoff). Plan inputs and their collection errors share one gather.

Remove capacity checks that no longer apply to memfd regions: the
MAGI_SHARED_BIN_PATH budget, MemAvailable/cgroup headroom and the per-chunk
capacity gather. An allocation-time ENOSPC still keeps that chunk private.

Plan chunks directly instead of laying out a full region and re-splitting it,
give the writer memfd a single close point, park private weights after the
last round, and drop the comparison timing log.

Co-authored-by: Cursor <cursoragent@cursor.com>
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 7, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 7, 2026

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

LGTM

@wtr0504
wtr0504 merged commit ad66700 into SandAI-org:main Nov 7, 2026
21 checks passed
cennn pushed a commit that referenced this pull request Nov 12, 2026
The graph weight offload that came in from main (Offload V2, #80 / #99) called torch.cuda and
compared device types with "cuda" directly: its H2D stream, events and synchronizes, the bandwidth
probe, the stand-in device, the move probe that decides parking, and the NUMA lookup. They now go
through PLATFORM, so MUSA parks and streams weights the same way. cuda_device_numa_node becomes
device_numa_node and returns None for a runtime that reports no PCI address.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants