Skip to content

[Feat] Pin offloaded weights only after compile chooses the residents - #100

Open
wtr0504 wants to merge 4 commits into
SandAI-org:mainfrom
wtr0504:dev/lazy_pin
Open

wtr0504 wants to merge 4 commits into
SandAI-org:mainfrom
wtr0504:dev/lazy_pin

Conversation

@wtr0504

@wtr0504 wtr0504 commented Nov 9, 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

Opt-in offload_config.defer_host_materialization (env: MAGI_COMPILE_OFFLOAD_CONFIG__DEFER_HOST_MATERIALIZATION, default off). On a host-built model with graph_weight_offload, the first CUDA move no longer reads or pins offloadable weights. Each slot keeps its CPU original (for example a checkpoint mmap) and is bound to one shared zero placeholder. Compilation only sees that placeholder. After placement has chosen the resident set, and before the compiled graph runs, every deferred weight is copied once:

  • a resident weight is copied into its device storage and keeps no host copy;
  • every other weight, including one this graph does not read but that was deferred in the same move, is copied into pinned host memory.

Later compiles keep that resident set. A weight with no host copy is always charged to the residency budget first, and moving it back to the host is refused, so the model must stay on CUDA. Meta-built to_empty models are refused: that path would allocate host buffers before any checkpoint is read.

When offload_host_weight_sharing is not "off", the sharing collective moves from the first CUDA move to the end of every compile of the model (WORLD, Gloo), including ranks with nothing pending. A weight resident on every rank is not parked in the shared region. A weight resident on only some ranks is still written by the writer so the other ranks can share it; the writer itself keeps no host copy. A reader whose bytes do not match the shared copy falls back to a private pinned copy. Private copies happen after the last collective, one slot at a time, so a CPU original in anonymous memory is freed before the next copy.

Two pin-lifetime fixes come with that path:

  • Pinned requests of at least 64 MiB get an exact-size mmap registered with cudaHostRegister, instead of torch's power-of-two pinned allocator.
  • A rank-shared mapping is unregistered before it is unmapped. A reader that drops a chunk it keeps on the device can then pin a later chunk mapped at the same addresses.

ProfilingRuntimeEstimator logs a warm/sync measurement fallback on every rank, with the exception, so one rank cannot silently skip collectives the others still issue.

🧪 Test plan

  • tests/feature_tests/offload/test_deferred_host_materialization.py: the first CUDA move neither reads nor pins the weight; a meta model is refused before any slot is allocated; the first compile copies each deferred weight once, unread weights included; a device-only weight cannot move back to CPU.
  • tests/feature_tests/offload/test_h2d_reorder.py: a device-only weight spends the residency budget first, so a later compile cannot also buy out another load with that same budget.
  • tests/feature_tests/offload/test_rank_shared_host_weights.py: a weight resident on one rank is still shared with the others, and the writer keeps no host copy of it; a reader that drops chunks it keeps on the device can still pin later chunks.
  • CUDA feature tests above were not re-run in this environment.
wtr0504 and others added 2 commits November 11, 2026 19:02
The first CUDA move keeps each CPU original unread. After placement, resident weights are copied only onto the device, and the rest are pinned once.
wtr0504 and others added 2 commits November 11, 2026 20:28
…by parameter name alone

A compiled graph can read several deferred groups, each one module's first CUDA move, and their
parameter names are relative to that module, so two groups may both own a "weight". Keyed by name
alone, the planned source and the offloaded slot of that name could come from different groups:
a writer that kept the later one resident filled the shared chunk with its bytes and handed them
to the earlier, offloaded slot.

Every pending slot is now keyed by the order in which the graph's inputs first read its group plus
its parameter name, which each rank compiling the graph derives alike. A key that is still not
unique keeps its slots private.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
…n sharing is on

The shared path kept every CPU original in a local dict until it returned, so a completed slot's
original outlived the store's reference: the private copies after the shared rounds, or all of
them when no plan comes back (no process group, one rank, nothing identical), held every original
and every new host copy at once. The originals are now passed to the planner only for its
fingerprints, and a writer reads each one from the store when it fills its chunk.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant