Repository navigation
Preserve pinned host memory for CPU-offloaded FP32 params - #4547
Merged
chtruong814 merged 1 commit intoAug 21, 2026
Merged
chtruong814 merged 1 commit into
chtruong814 merged 1 commit into
Conversation
Orange-Lv
marked this pull request as ready for review
April 30, 2026 02:53
Contributor
|
/ok to test 7687e8e |
dimapihtar
enabled auto-merge
June 9, 2026 13:10
yaoyu-33
approved these changes
Jul 22, 2026
jiemingz
approved these changes
Jul 23, 2026
Contributor
|
/ ok to test fa99fc9 |
guihong-nv
approved these changes
Jul 23, 2026
Contributor
|
/ok to test 517c792 |
Contributor
|
/ok to test 83819f1 |
Contributor
|
/ok to test 845e8c1 |
Contributor
|
/ok to test 845e8c1 |
Contributor
|
Dear @Orange-Lv (and others, too, I believe? Not sure how merge commits are handled WRT this), could you add a DCO to your commits using |
Contributor
|
/ok to test 4b34420 |
Contributor
|
/ok to test 2b8af82 |
Contributor
|
/ok to test f6c5eff |
Signed-off-by: Maanu Grover <maanug@nvidia.com>
Contributor
|
/ok to test 5f73bc8 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/32488873419 |
3 of 6 tasks
14 of 21 tasks
2 tasks done
This branch was previously 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.
Summary
This PR preserves pinned host memory for CPU-offloaded FP32 params in
HybridDeviceOptimizer.In the
optimizer_cpu_offload=True+param_update_in_fp32=Truepath, the offloaded CPU param is first created as pinned host memory, but a laterclone().float()creates a new pageable CPU tensor. As a result, the main HtoD copy-back path can fall back to pageable host memory.This change re-applies
pin_memory()to the final FP32 CPU param used for GPU copy-back whenpin_cpu_params=True.Problem
In
megatron/core/optimizer/cpu_offloading/hybrid_optimizer.py, the offloaded CPU param is created like this:But in the FP32 update path, it is later replaced with:
The second
clone().float()produces a new CPU tensor and does not preserve the original pinned-memory property.This matters because
HybridDeviceOptimizerdoes not only offload optimizer state. For CPU-side optimizer execution, it also:That final copy-back happens here:
If
param.datais pageable instead of pinned, the HtoD path is slower and may show up as pageable host memory in traces.Change
Re-apply
pin_memory()after the FP32 clone for CPU-offloaded params whenpin_cpu_params=True:This keeps the change minimal and only affects the CPU-offload + FP32 path.
Validation
1. Functional correctness
Ran a short GPT2-1.5B mock-data training with:
optimizer_cpu_offload=Trueoptimizer_offload_fraction=1.0Smoke test:
train-iters=20Observed log:
iter 1:17858.3 ms,loss 1.113179E+01iter 10:5016.5 ms,loss 1.063229E+01iter 20:3606.6 ms,loss 9.541001E+002. Trace evidence
Compared
nsystraces before and after the patch.Before:
(HTOD, Pageable -> Device): 20374 calls(HTOD, Pinned -> Device): 1360 callsAfter:
(HTOD, Pinned -> Device): 6200 calls(HTOD, Pageable -> Device): 513 callsSo the main HtoD copy-back path changed from
Pageable -> DevicetoPinned -> Device.Average duration for the dominant HtoD path:
2,362,300 ns(~2.36 ms)819,103 ns(~0.82 ms)This indicates the patch not only changes the memory kind shown in traces, but also improves the HtoD copy-back path.
Notes
The before/after
nsyscaptures were not identical in total duration, so aggregate percentages should be interpreted qualitatively.However, the key result is robust:
Why this is safe
This change does not expand pinning globally. It only preserves pinning for the final FP32 CPU tensor used in GPU copy-back, and only when:
pin_cpu_params=True.So this aligns runtime behavior with the existing user-facing configuration rather than introducing a broader semantic change.