Skip to content

perf(offload): replace manual inflight cap with retired-load pacing - #120

Merged
jiahy0825 merged 2 commits into
SandAI-org:mainfrom
wtr0504:fix/offload-retired-load-pacing
Nov 30, 2026
Merged

jiahy0825 merged 2 commits into
SandAI-org:mainfrom
wtr0504:fix/offload-retired-load-pacing

Conversation

@wtr0504

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

Replace the manual OffloadConfig.offload_max_inflight_load_mib byte cap from #114 with automatic retired-load pacing in magi::h2d_load. Live hoisted loads planned by H2dLoadReorder are never throttled at runtime, while storage-freed loads are synchronized in FIFO order before new allocations so CUDACachingAllocator reuses their blocks instead of accumulating dead cached memory when the host runs ahead of the device.

Problem with offload_max_inflight_load_mib

  1. Interferes with compile-time hoisting: H2dLoadReorder already budgets concurrent live H2D loads and resident weights through offload_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 exceeds offload_max_inflight_load_mib, serializing copies that the compiler scheduled to overlap.
  2. Requires manual per-model tuning and misses small-shard run-ahead: offload_max_inflight_load_mib defaulted to 0 (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:

  • Live phase (unthrottled): While loaded_weight or any view aliasing its untyped_storage() remains alive in Python, nothing enters _RETIRED_LOADS and no event is waited on. Every hoist decision made by H2dLoadReorder executes without host-side stalls.
  • Retired phase (double-buffered FIFO reclamation):
    • Attach 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_load records compute_release_event = compute_stream.record_event() alongside load_done_event.
    • On each non-resident _h2d_load, before torch.empty allocates a new device buffer, _RETIRED_LOADS.wait_before_alloc() drops completed entries via query() and, while len(_entries) > 1, pops the oldest entry and synchronizes both compute_release_event and load_done_event.
    • Keeping the single newest retired entry unsynchronized lets the host enqueue the next consumer kernel before blocking (avoiding compute-stream wakeup bubbles when a hoisted h2d_load immediately follows del), while guaranteeing that every earlier retired buffer and cross-stream event up to that point is reclaimable by CUDACachingAllocator before the next allocation.
  • Config & op schema cleanup:
    • Remove OffloadConfig.offload_max_inflight_load_mib and its plumbing through WeightTransferPipeline, insert_h2d_loads, and host_slot_of_load.
    • Restore magi::h2d_load schema to "h2d_load(Tensor shard, int slot) -> Tensor". (H2dLoadReorder(max_inflight_bytes=...) in the compile-time scheduler is unchanged.)

Validation

  • Updated 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_LOADS or call Event.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 next H2D_LOAD before allocation.
    • test_compiled_offload_reuses_retired_weight_buffers_when_host_runs_ahead: compiles a 24-layer stack (48 MiB total weights) with graph_weight_offload=True, stalls the GPU stream so the host runs ahead, and verifies output parity alongside bounded max_memory_reserved() growth (< 40% of total weight bytes; verified to fail at 40 MiB growth when pacing is disabled).
  • Full offload, reorder, link-sharing, cache, and overlap golden suites on NVIDIA H200:
    • 221 passed across test_weight_offload.py (re-verified at 106 passed after final simplification), test_h2d_reorder.py, test_link_sharing.py, test_offload_cache.py, and test_overlap_golden.py.
  • Repository pre-commit hooks passed on commit (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
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 30, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 30, 2026
…gment

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@wtr0504 wtr0504 added the ci:run Trigger CI integration tests label Nov 30, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 30, 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

@jiahy0825 jiahy0825 added the ci:run Trigger CI integration tests label Nov 30, 2026
@jiahy0825
jiahy0825 merged commit 31f8b7e into SandAI-org:main Nov 30, 2026
20 checks passed
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants