Repository navigation
[ZeRO] Keep the offload overflow flag on the device #8963
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
Open
0z5a
wants to merge
2
commits into
deepspeedai:master
Choose a base branch
from
0z5a:offload-overflow-device-flag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| import deepspeed | ||
| from deepspeed.accelerator import get_accelerator | ||
| from deepspeed.runtime.zero.stage_1_and_2 import DeepSpeedZeroOptimizer | ||
| from deepspeed.utils import safe_set_full_grad | ||
| from unit.common import DistributedTest | ||
| from unit.simple_model import SimpleModel, random_dataloader | ||
|
|
||
|
|
||
| class _NoTruthTest(torch.Tensor): | ||
| """A flag that fails if anything tests its truth value, which would synchronize the host.""" | ||
|
|
||
| def __bool__(self): | ||
| raise AssertionError("the offload overflow flag was truth-tested") | ||
|
|
||
|
|
||
| def _offload_engine(zero_stage, check_grad_overflow): | ||
| config = { | ||
| "train_micro_batch_size_per_gpu": 1, | ||
| "steps_per_print": 1, | ||
| "optimizer": { | ||
| "type": "Adam", | ||
| "params": { | ||
| "lr": 1e-4, | ||
| "torch_adam": True | ||
| } | ||
| }, | ||
| "bf16": { | ||
| "enabled": True, | ||
| "check_grad_overflow": check_grad_overflow | ||
| }, | ||
| "zero_optimization": { | ||
| "stage": zero_stage, | ||
| "offload_optimizer": { | ||
| "device": "cpu" | ||
| } | ||
| }, | ||
| } | ||
| model = SimpleModel(10) | ||
| engine, _, _, _ = deepspeed.initialize(config=config, model=model, model_parameters=model.parameters()) | ||
| return engine | ||
|
|
||
|
|
||
| def _step_with_grad(engine, batch, grad_value): | ||
| engine.backward(engine(batch[0], batch[1])) | ||
| for p in engine.parameters(): | ||
| safe_set_full_grad(p, torch.full_like(p, grad_value)) | ||
| engine.step() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("zero_stage", [1, 2]) | ||
| class TestOffloadOverflowFlag(DistributedTest): | ||
| world_size = 2 | ||
|
|
||
| def test_flag_accumulates_on_device(self, zero_stage): | ||
| if not get_accelerator().is_bf16_supported(): | ||
| pytest.skip("bf16 is not supported") | ||
| engine = _offload_engine(zero_stage, check_grad_overflow=True) | ||
| optimizer = engine.optimizer | ||
| original = DeepSpeedZeroOptimizer._has_inf_or_nan | ||
| DeepSpeedZeroOptimizer._has_inf_or_nan = staticmethod(lambda x: original(x).as_subclass(_NoTruthTest)) | ||
| try: | ||
| optimizer.reset_cpu_buffers() | ||
| device = get_accelerator().current_device_name() | ||
| for value in (0.5, -2.0): | ||
| optimizer.update_offload_overflow_tracker(torch.full((4, ), value, device=device)) | ||
| assert torch.is_tensor(optimizer.local_overflow) | ||
| assert not optimizer.has_overflow() | ||
|
|
||
| optimizer.update_offload_overflow_tracker(torch.tensor([1.0, float("inf")], device=device)) | ||
| optimizer.update_offload_overflow_tracker(torch.full((4, ), 1.0, device=device)) | ||
| assert optimizer.has_overflow() | ||
|
|
||
| optimizer.reset_cpu_buffers() | ||
| assert optimizer.local_overflow is False | ||
| assert not optimizer.has_overflow() | ||
| finally: | ||
| DeepSpeedZeroOptimizer._has_inf_or_nan = original | ||
|
|
||
| def test_overflow_still_skips_step(self, zero_stage): | ||
| if not get_accelerator().is_bf16_supported(): | ||
| pytest.skip("bf16 is not supported") | ||
| engine = _offload_engine(zero_stage, check_grad_overflow=True) | ||
| loader = random_dataloader(model=engine, | ||
| total_samples=2, | ||
| hidden_dim=10, | ||
| device=engine.device, | ||
| dtype=torch.bfloat16) | ||
| batches = list(loader) | ||
| _step_with_grad(engine, batches[0], 0.01) | ||
| assert engine.skipped_steps == 0 | ||
| _step_with_grad(engine, batches[1], float("nan")) | ||
| assert engine.skipped_steps == 1 | ||
|
|
||
| def test_no_check_when_disabled(self, zero_stage): | ||
| if not get_accelerator().is_bf16_supported(): | ||
| pytest.skip("bf16 is not supported") | ||
| engine = _offload_engine(zero_stage, check_grad_overflow=False) | ||
| original = DeepSpeedZeroOptimizer._has_inf_or_nan | ||
|
|
||
| def unexpected(x): | ||
| raise AssertionError("gradients were checked for overflow with check_grad_overflow=False") | ||
|
|
||
| DeepSpeedZeroOptimizer._has_inf_or_nan = staticmethod(unexpected) | ||
| try: | ||
| loader = random_dataloader(model=engine, | ||
| total_samples=1, | ||
| hidden_dim=10, | ||
| device=engine.device, | ||
| dtype=torch.bfloat16) | ||
| _step_with_grad(engine, next(iter(loader)), float("inf")) | ||
| assert engine.skipped_steps == 0 | ||
| assert engine.optimizer.local_overflow is False | ||
| finally: | ||
| DeepSpeedZeroOptimizer._has_inf_or_nan = original | ||
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
Oops, something went wrong.
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.
Moved to
tests/unit/v1/zeroin 5257815; the selector now picks it in both workflows.