-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix potential memory issues when use deepspeed Z3 #6726
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
9 commits
Select commit
Hold shift + click to select a range
d75d1f5
Set "ds_grads_remaining" to 0 when module doesn't have this variable
wenbinc-Bin b83df91
Set "__n_available_params" to 0 in release_and_reset_all()
wenbinc-Bin 9f0d2ae
Add unit stage3 test for running model twice in one step
wenbinc-Bin ad60f94
Merge branch 'master' into fix_z3
tjruwase 8b30987
Merge branch 'master' into fix_z3
loadams 7834887
Merge branch 'master' into fix_z3
loadams 51a69c0
Merge branch 'master' into fix_z3
loadams 7cabf80
Fix dtype error
wenbinc-Bin ed40ac7
Merge branch 'master' into fix_z3
hwchen2017 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,53 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import deepspeed | ||
| import torch | ||
| from unit.common import DistributedTest, preferred_dtype | ||
| from unit.simple_model import SimpleModel, random_dataloader | ||
|
|
||
|
|
||
| class TestZ3MultipleModelCall(DistributedTest): | ||
| world_size = 1 | ||
|
|
||
| def test_z3_multiple_model_call(self): | ||
| config_dict = { | ||
| "train_micro_batch_size_per_gpu": 1, | ||
| "gradient_accumulation_steps": 1, | ||
| "steps_per_print": 1, | ||
| "zero_optimization": { | ||
| "stage": 3 | ||
| }, | ||
| "optimizer": { | ||
| "type": "Adam", | ||
| "params": { | ||
| "lr": 1e-3 | ||
| } | ||
| }, | ||
| } | ||
| if preferred_dtype() is torch.float16: | ||
| config_dict["fp16"] = {"enabled": True, "initial_scale_power": 8} | ||
| elif preferred_dtype() is torch.bfloat16: | ||
| config_dict["bf16"] = {"enabled": True} | ||
| hidden_dim, nlayers = 2048, 3 | ||
| model = SimpleModel(hidden_dim=hidden_dim, nlayers=nlayers) | ||
| model_engine, _, _, _ = deepspeed.initialize(config=config_dict, | ||
| model=model, | ||
| model_parameters=model.parameters()) | ||
| data_loader = iter( | ||
| random_dataloader(model=model_engine, total_samples=10, hidden_dim=hidden_dim, device=model_engine.device)) | ||
|
|
||
| for n, batch in enumerate(data_loader): | ||
| loss1 = model_engine(batch[0], batch[1]) | ||
| with torch.no_grad(): | ||
| loss2 = model_engine(batch[0], batch[1]) | ||
| loss = loss1 + loss2 | ||
| model_engine.backward(loss) | ||
| for name, submodule in model_engine.module.linears._modules.items(): | ||
| assert hasattr(submodule, "ds_grads_remaining"), \ | ||
| f"linears.{name} does not have variable ds_grads_remaining" | ||
| assert submodule.ds_grads_remaining == 0, \ | ||
| f"ds_grads_remaining of linears.{name} is not 0 ({submodule.ds_grads_remaining})" | ||
| model_engine.step() |
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.
Uh oh!
There was an error while loading. Please reload this page.