Repository navigation
Conversation
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
force-pushed
the
dev/lazy_pin
branch
from
November 11, 2026 11:38
32ce52b to
3682193
Compare
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🗂️ PR Category
📝 Description
Opt-in
offload_config.defer_host_materialization(env:MAGI_COMPILE_OFFLOAD_CONFIG__DEFER_HOST_MATERIALIZATION, default off). On a host-built model withgraph_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: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_emptymodels are refused: that path would allocate host buffers before any checkpoint is read.When
offload_host_weight_sharingis 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:
mmapregistered withcudaHostRegister, instead of torch's power-of-two pinned allocator.ProfilingRuntimeEstimatorlogs 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.