Repository navigation
perf(offload): replace manual inflight cap with retired-load pacing - #120
Merged
jiahy0825 merged 2 commits intoNov 30, 2026
Merged
Conversation
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
…gment Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
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
Replace the manual
OffloadConfig.offload_max_inflight_load_mibbyte cap from #114 with automatic retired-load pacing inmagi::h2d_load. Live hoisted loads planned byH2dLoadReorderare never throttled at runtime, while storage-freed loads are synchronized in FIFO order before new allocations soCUDACachingAllocatorreuses their blocks instead of accumulating dead cached memory when the host runs ahead of the device.Problem with
offload_max_inflight_load_mibH2dLoadReorderalready budgets concurrent live H2D loads and resident weights throughoffload_max_device_weight_mib. Counting all issued loads against a separate runtime byte cap blocks the host mid-hoist whenever the scheduler legitimately hoists multiple weights whose sum exceedsoffload_max_inflight_load_mib, serializing copies that the compiler scheduled to overlap.offload_max_inflight_load_mibdefaulted to0(disabled). Even when set, a byte cap sized for large weights still allows the host to run many layers ahead when FSDP shards are small, keeping cross-stream buffers (record_stream) pinned in the caching allocator until device execution catches up.Fix
Split an offloaded weight's runtime lifecycle into two phases in
magi_compiler/passes/weight_offload/runtime/h2d_op.py:loaded_weightor any view aliasing itsuntyped_storage()remains alive in Python, nothing enters_RETIRED_LOADSand no event is waited on. Every hoist decision made byH2dLoadReorderexecutes without host-side stalls.weakref.finalize(loaded_weight.untyped_storage(), _RETIRED_LOADS.record_retired_load, ...)(atexit = False). When Inductor's generated wrapper deletes the last reference/view after launching the final consumer kernel,record_retired_loadrecordscompute_release_event = compute_stream.record_event()alongsideload_done_event._h2d_load, beforetorch.emptyallocates a new device buffer,_RETIRED_LOADS.wait_before_alloc()drops completed entries viaquery()and, whilelen(_entries) > 1, pops the oldest entry and synchronizes bothcompute_release_eventandload_done_event.h2d_loadimmediately followsdel), while guaranteeing that every earlier retired buffer and cross-stream event up to that point is reclaimable byCUDACachingAllocatorbefore the next allocation.OffloadConfig.offload_max_inflight_load_miband its plumbing throughWeightTransferPipeline,insert_h2d_loads, andhost_slot_of_load.magi::h2d_loadschema to"h2d_load(Tensor shard, int slot) -> Tensor". (H2dLoadReorder(max_inflight_bytes=...)in the compile-time scheduler is unchanged.)Validation
tests/feature_tests/offload/test_weight_offload.py:test_h2d_load_paces_retired_loads_without_throttling_live_loads: verifies that multiple live hoisted loads and live tensor views never enter_RETIRED_LOADSor callEvent.synchronize(), that a single retired load stays unsynchronized, and that retiring a second load synchronizes the oldest entry (compute_release_event+load_done_event) on the nextH2D_LOADbefore allocation.test_compiled_offload_reuses_retired_weight_buffers_when_host_runs_ahead: compiles a 24-layer stack (48 MiBtotal weights) withgraph_weight_offload=True, stalls the GPU stream so the host runs ahead, and verifies output parity alongside boundedmax_memory_reserved()growth (< 40%of total weight bytes; verified to fail at40 MiBgrowth when pacing is disabled).test_weight_offload.py(re-verified at 106 passed after final simplification),test_h2d_reorder.py,test_link_sharing.py,test_offload_cache.py, andtest_overlap_golden.py.copyright_checker,black,isort,autoflake).PYTHONPATH=$PWD python3 -m pytest \ tests/feature_tests/offload/test_weight_offload.py \ tests/feature_tests/offload/test_h2d_reorder.py \ tests/feature_tests/offload/test_link_sharing.py \ tests/feature_tests/cache/test_offload_cache.py \ tests/feature_tests/test_overlap_golden.py