-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add and Remove ZeRO 3 Hooks #5658
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d36ecdf
adding hooks context manager
93bd49e
Merge branch 'master' into jomayeri/zero3-hooks
jomayeri 318929c
format changes
jomayeri 6b75fca
Merge branch 'master' into jomayeri/zero3-hooks
tjruwase daca236
Merge branch 'master' into jomayeri/zero3-hooks
jomayeri 63fa13c
running precommit checks
jomayeri be067ae
Merge branch 'master' into jomayeri/zero3-hooks
jomayeri 65d6699
Merge branch 'master' into jomayeri/zero3-hooks
HeyangQin 00870e3
remove circular dependency
jomayeri 3dd6ebe
adding unwrap unittest
jomayeri a71bb00
Merge branch 'master' into jomayeri/zero3-hooks
jomayeri bfb86ff
Merge branch 'master' into jomayeri/zero3-hooks
jomayeri b3405f0
tests and precommit
jomayeri c10cca7
Merge branch 'master' into jomayeri/zero3-hooks
tjruwase b1dc01b
Merge branch 'master' into jomayeri/zero3-hooks
jomayeri 153ccd6
format changes
jomayeri aa09c08
Merge branch 'master' into jomayeri/zero3-hooks
loadams 5e0be49
Update formatting
loadams 7c4ffcb
Merge branch 'master' into jomayeri/zero3-hooks
tjruwase 2cf0b14
Merge branch 'master' into jomayeri/zero3-hooks
tjruwase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import deepspeed | ||
| from deepspeed.runtime.zero import unwrap_model_for_generation | ||
| from deepspeed.accelerator import get_accelerator | ||
|
|
||
| from unit.common import DistributedTest | ||
| from unit.simple_model import SimpleModel | ||
|
|
||
| config = { | ||
| "train_batch_size": 2, | ||
| "steps_per_print": 1, | ||
| "optimizer": { | ||
| "type": "Adam", | ||
| "params": { | ||
| "lr": 0.00015 | ||
| } | ||
| }, | ||
| "zero_optimization": { | ||
| "stage": 3, | ||
| "stage3_param_persistence_threshold": 1, | ||
| "offload_param": { | ||
| "device": "cpu", | ||
| "pin_memory": True | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if get_accelerator().is_fp16_supported(): | ||
| config["fp16"] = {"enabled": True, "loss_scale": 138.} | ||
| elif get_accelerator().is_bf16_supported(): | ||
| config["bf16"] = {"enabled": True} | ||
|
|
||
|
|
||
| class TestUnwrapModel(DistributedTest): | ||
| # gather across more than 1 gpu | ||
| world_size = 2 | ||
|
|
||
| def test(self): | ||
|
|
||
| def hooks_exist(engine): | ||
| if engine.optimizer is not None and hasattr(engine.optimizer, "parameter_offload"): | ||
| optimizer_offload = engine.optimizer.parameter_offload | ||
| elif engine.optimizer is not None: | ||
| optimizer_offload = engine.optimizer | ||
|
|
||
| hooks = 0 | ||
| for hook in optimizer_offload.forward_hooks: | ||
| hooks += 1 | ||
| if hooks > 0: | ||
| return True | ||
| return False | ||
|
|
||
| model = SimpleModel(hidden_dim=100) | ||
| engine, _, _, _ = deepspeed.initialize(args=None, model=model, config=config) | ||
|
|
||
| with unwrap_model_for_generation(engine): | ||
| # assert no hooks | ||
| assert not hooks_exist(engine) | ||
| # assert parameters gathered | ||
| assert model.linears[0].weight.numel() != 0, "GatheredParameters should give a non-0-sized tensor" | ||
|
|
||
| # assert hooks | ||
| assert hooks_exist(engine) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems my comment here was missed?