Repository navigation
Initialize CPU-offloaded EMA directly in pinned host storage - #5282
Conversation
Signed-off-by: ZenAlexa <[email protected]>
|
|
|
@claude review this PR |
|
Claude finished @tianyu-l's task in 1m 28s —— View job Review: Initialize CPU-offloaded EMA directly in pinned host storage
Verdict: I didn't find any correctness problems, and the change is a clear win. With What I checked
Suggestions (non-blocking)
Not verified hereI didn't run the GPU tests or pre-commit in this job because the runner has no GPU. The PR description reports both GPU suites passing on H100. The CI job for |
|
@ZenAlexa thanks, could you at least fix 1 and 3, after rebase |
|
fixed 1 and 3, and merged current main. the test edits now use the moved paths. |
Motivation
Initialize CPU-offloaded EMA copies directly in their final pinned host storage.
offload_to_cpu=Truecurrently constructs every parameter and selected-buffer shadow on the accelerator first, then converts the complete set to pinned CPU tensors. Multiple EMA decays amplify this initialization peak.For
Kconfigured EMA copies andBbytes of tracked local parameters and buffers, the current path retainsK*Bbytes of accelerator shadows before offloading starts. The final EMA storage belongs on the CPU under the existing configuration. A model that fits with CPU-offloaded EMA can exhaust accelerator memory during EMA construction.This follows the multi-copy interface added in #5074 and the direct pinned-storage approach used by #5107.
Changes
_EMAParamOptimizerconstructor for both parameters and selected buffers.get_local_tensorhelper for its local shard. Allocate a separate pinned CPU tensor for every EMA key and copy directly into it.detach().clone()path unchanged.The copy uses the default blocking behavior, so each host shadow is readable when construction returns. Every EMA key retains independent storage; changing a live weight or another EMA copy leaves that shadow unchanged. Checkpoint keys, configured decays, half-life schedules, tracked-buffer selection, and dtypes keep their existing owners.
Resource model
For dense local tensors, direct allocation removes the accumulated
K*Baccelerator clones. It also removes the pageable-CPU-to-pinned-CPU copy at initialization. Final pinned storage remainsK*B, and initialization copies the source once per EMA key. No new configuration, dispatch threshold, or dependency is introduced.Non-dense cross-device copies may use per-tensor contiguous staging inside PyTorch. The claim is removal of the accumulated EMA clones; the platform's copy temporaries retain their own layout-dependent cost. The update pipeline and checkpoint materialization keep their existing memory and transfer behavior.
Validation
current revision
merged main
65aaaa960698ab5d6e98c874177b41b9c30e9c58at92e824be6b71360e62759ae479cccb24b56588db. restored the dtensor pin-memory note and shortened the checkpoint docstring. the ema execution code is unchanged from6c3ab4f7; the two test docstring edits now follow main's directory layout.on python 3.12.14 and pytorch 2.14.1 cpu, the existing ema and reshard suites passed all 44 tests and 44 subtests. changed-file formatting, syntax, license, docstring and spelling checks passed. pyrefly 0.45.1 reported zero errors in the changed production file.
upstream gpu ci ran this head with pytorch
2.16.0.dev20261011+cu132. all 13 ema gpu cases passed. the full single-gpu group returned 181 passed, 46 skipped and 44 deselected. the multi-gpu group returned 34 passed, 11 skipped, 225 deselected and 1 xfailed. upstream lint also passed, including pyrefly and the link checker. integration tests are still running.historical h100 comparison
Compared main
432b95dcf55b3840d7f63ca24a0c585701e70c36with6c3ab4f7965733e312456e578843f6a37baf42eeon H100 80 GB HBM3, using Torch2.16.0.dev20261010+cu132, Triton3.9.0+gitfc8d31a5, Python 3.12 and driver 580.95.05. Both checkouts use the full project dependency manifest in the same offline environment;pip checkand actual model imports pass.Initialization memory and time
Four fresh processes run in main/candidate/candidate/main order. Each process performs the same ten cases in the same order after an 8 MiB warmup. Each case times one complete
EMA.Config.build()plus CUDA completion; live parameter/buffer creation and output verification are outside timing. Allocator caches can carry across cases. The table reports the arithmetic mean of the two process observations per arm. Both observations agree on every memory peak.B is the logical size of tracked local tensors; K is the number of EMA copies. Extra peak is
max_memory_allocated - memory_allocated_before_build, with the peak reset after the live tensors exist. Final pinned storage is identical between arms at K×B.The holed input has 128 MiB of underlying live storage for 64 MiB of logical data. Its absolute GPU peak falls from 320 to 136 MiB; the remaining 8 MiB is a per-tensor copy temporary. For contiguous B256/K3, absolute peak falls from 1,024 to 256 MiB and final pinned storage remains 768 MiB. The two process build times are 520.445/484.515 ms on main and 258.904/262.822 ms in the candidate.
A separate fresh-process capacity scenario imposes a 2 GiB PyTorch allocator budget with 1 GiB of live tracked tensors and K=3. Main reaches OOM during EMA construction, after the complete source exists. The candidate completes with a 1 GiB GPU allocated peak and 3 GiB of independent pinned shadows. That budget excludes CUDA context and external allocations.
GPU-resident controls with offload_to_cpu=False
The clone path and measured memory peaks retain their existing behavior. The small control below records a 0.110 ms increase; construction speedups apply to CPU offload.
Values, ownership and training
seed=42, deterministic algorithms enabled and TF32 disabled. The six-layer, dim-256, vocab-2048 model has 6,163,712 parameters and uses the realOptim.steppath, fused AdamW, clipping, LR scheduling, two fixed decays, one half-life EMA and a tracked buffer.Distributed contract
Both arms pass nine scenarios on each of two H100 ranks using native NCCL:
lerpcalculations. Placements cover uneven and evenShard(0), an empty local shard,Replicate, and transposedShard(1).Existing regression coverage and static checks
Both arms pass the existing CPU EMA and reshard suites, 44 tests each, and the existing native GPU EMA suite, 13 tests each with no skips:
Formatting, flake8, AST, license, docstring and spelling checks pass for both changed files, along with
git diff --check. Full-tree pre-commit retains main's existing F401 attorchtitan/experiments/graph_trainer/tests/test_graph_pp_passes.py:84. A separate Linux pyrefly run reports the same six optional-modeltransformersimport errors on both arms and zero EMA-file diagnostics. The link checker has no installed binary in the local hook environment.The measurements cover EMA construction and the exercised training/checkpoint contracts. GH200 NVLink-C2C performance and full multi-node training throughput remain unmeasured.