-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] ORTModule memory refinement #8979
Closed
Closed
Conversation
This file contains 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
I might missed requirements of DDP onto torch grad accumulator 's post hook. Not sure whether change 2 benefits real models before investing more. |
…o pengwa/custom_fnc_mem
…o pengwa/custom_fnc_mem
…ons compilation (cherry picked from commit b125e80)
…o pengwa/custom_fnc_mem
This issue has been automatically marked as stale due to inactivity and will be closed in 7 days if no further activity occurs. If further support is needed, please provide an update and/or more details. |
This was referenced Dec 21, 2023
pengwa
added a commit
that referenced
this pull request
Jan 16, 2024
## Dependency #19007 ## ORTModule memory efficient gradient management Previously I have tried to solve the coarsed-grained gradient accumulation/update problem in ORTModule with #8979, while that resolution somehow is not fully validated with DDP or there is user hooks on the gradient accumulation on torch parameter. This PR is addressing the problem in the similar approach as PR 8979, e.g. trigger gradient accumulation once ORT computed the grad, but instead of use a AccumulateGrad op, this time with a ONNX operator PythonOp, internally it will call param.backward(grad), which will help handle all related hooks correctly. ## Design Check the details from https://microsoftapc-my.sharepoint.com/:p:/g/personal/pengwa_microsoft_com/EaaBq4EzsFhOmsDEXCG7Ba4Bb9bwd0O2sFV_JXJ4jBLYLA?e=7Sz2g8&nav=eyJzSWQiOjI3MSwiY0lkIjozMjE4NzI1NDIzfQ ## Convergence Validation: ![image](https://github.com/microsoft/onnxruntime/assets/10530022/ccf3a213-e815-4b23-b759-165033b2d9fe) differences are on mostly 0.000x, sometimes 0.00x, which may comes from the different order gradient apply happens before or after this change (on deepspeed zero stage 2) ## TODO Consolidate the logic with Stage3's similar logic.
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.
Description: memory refinement
On a simplified models constructed by stacking autograd.Function instances. So most of computations runs on PyTorch kernels, this is a good baseline to compare PyTorch with ORT. From memory profiling, we see ORT takes 16% more memory than PyTorch runs, obviously there are some bugs.
ORTModuleFunction holds all the calculated gradients until backward completed, BUT Pytorch will accumulate the calculated gradients into param.grad immediately once gradient computations comes to any of the leaf node (AccumulateGrad function).
The commit (29eebc2) in this PR, tries to 1). use similar idea of #8993 (cut off the connection between ORTModuleFunction and its inputs' AccumulateGrad gradient function. then PyTorch will not not accumulate the ORTModuleFunction backward outputs into param.grad. 2). we do the gradient in-place update (into param.grad) on the ONNX graph.
With the changes, for some cases the memory consumptions are in parity between ORT and PyTorch. More detailed benchmarks come later.
TODO: I might missed requirements of DDP onto torch grad accumulator 's post hook. Not sure whether change 2 benefits real models before investing more. So currently multiple GPU run might be failed using this branch.
Benchmark
command: python bench.py --batch 1024 --hidden 8194 --layer 12 --tag test --ort
PT:
ORT (master):
ORT (this PR):
Conclusion:
With this PR, the memory allocation (MA) and max memory allocation (MAX_MA) are aligned with PyTorch runs.
Motivation and Context