-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Reject non-finite fp16 loss_scale across config and ZeRO paths #7856
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
Draft
harshang03
wants to merge
1
commit into
deepspeedai:master
Choose a base branch
from
harshang03:fix/issue-7852-loss-scale-validation
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.
Draft
Changes from all commits
Commits
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,47 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import math | ||
| from numbers import Integral, Real | ||
|
|
||
|
|
||
| def _to_finite_float(value, *, name: str) -> float: | ||
| if isinstance(value, bool) or not isinstance(value, Real): | ||
| raise ValueError(f"{name} must be a real number, got {type(value).__name__}") | ||
|
|
||
| numeric_value = float(value) | ||
| if not math.isfinite(numeric_value): | ||
| raise ValueError(f"{name} must be finite, got {numeric_value}") | ||
| return numeric_value | ||
|
|
||
|
|
||
| def validate_loss_scale_value(value, *, name: str = "loss_scale", allow_dynamic_zero: bool = False) -> float: | ||
| """ | ||
| Validate static loss scale values. | ||
| A value of 0 is accepted only when it represents dynamic loss scaling mode. | ||
| """ | ||
| numeric_value = _to_finite_float(value, name=name) | ||
| if allow_dynamic_zero and numeric_value == 0.0: | ||
| return numeric_value | ||
| if numeric_value <= 0.0: | ||
| raise ValueError(f"{name} must be greater than 0, got {numeric_value}") | ||
| return numeric_value | ||
|
|
||
|
|
||
| def validate_positive_finite(value, *, name: str) -> float: | ||
| numeric_value = _to_finite_float(value, name=name) | ||
| if numeric_value <= 0.0: | ||
| raise ValueError(f"{name} must be greater than 0, got {numeric_value}") | ||
| return numeric_value | ||
|
|
||
|
|
||
| def validate_positive_int(value, *, name: str) -> int: | ||
| if isinstance(value, bool) or not isinstance(value, Integral): | ||
| raise ValueError(f"{name} must be an integer, got {type(value).__name__}") | ||
| int_value = int(value) | ||
| if int_value <= 0: | ||
| raise ValueError(f"{name} must be greater than 0, got {int_value}") | ||
| return int_value | ||
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
59 changes: 59 additions & 0 deletions
59
tests/unit/runtime/half_precision/test_loss_scale_validation.py
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,59 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| from types import SimpleNamespace | ||
| import pytest | ||
| import torch | ||
|
|
||
| from deepspeed.runtime.fp16.loss_scaler import ( | ||
| CONSECUTIVE_HYSTERESIS, | ||
| DELAYED_SHIFT, | ||
| INITIAL_LOSS_SCALE, | ||
| MIN_LOSS_SCALE, | ||
| SCALE_WINDOW, | ||
| CreateLossScaler, | ||
| LossScaleConfig, | ||
| ) | ||
| from deepspeed.runtime.zero.stage_1_and_2 import DeepSpeedZeroOptimizer | ||
| from deepspeed.runtime.zero.stage3 import DeepSpeedZeroOptimizer_Stage3 | ||
|
|
||
|
|
||
| def test_loss_scale_config_rejects_non_finite_static_loss_scale(): | ||
| with pytest.raises(ValueError, match="fp16.loss_scale must be finite"): | ||
| LossScaleConfig(low_precision_dtype=torch.float16, | ||
| dynamic_loss_scale=False, | ||
| static_loss_scale=float("inf"), | ||
| dynamic_loss_args=None) | ||
|
|
||
|
|
||
| def test_create_loss_scaler_rejects_non_finite_dynamic_init_scale(): | ||
| dynamic_loss_args = { | ||
| INITIAL_LOSS_SCALE: float("inf"), | ||
| SCALE_WINDOW: 1000, | ||
| DELAYED_SHIFT: 2, | ||
| CONSECUTIVE_HYSTERESIS: False, | ||
| MIN_LOSS_SCALE: 1.0, | ||
| } | ||
| with pytest.raises(ValueError, match="dynamic_loss_args\\['init_scale'\\] must be finite"): | ||
| CreateLossScaler(torch.float16, static_loss_scale=0, dynamic_scaling=True, dynamic_loss_args=dynamic_loss_args) | ||
|
|
||
|
|
||
| def test_stage1_override_loss_scale_validates_values(): | ||
| optimizer = SimpleNamespace(external_loss_scale=None, custom_loss_scaler=False) | ||
| with pytest.raises(ValueError, match="loss_scale must be finite"): | ||
| DeepSpeedZeroOptimizer.override_loss_scale(optimizer, float("inf")) | ||
|
|
||
| DeepSpeedZeroOptimizer.override_loss_scale(optimizer, 256.0) | ||
| assert optimizer.custom_loss_scaler is True | ||
| assert optimizer.external_loss_scale == 256.0 | ||
|
|
||
|
|
||
| def test_stage3_set_loss_scale_validates_values(): | ||
| optimizer = SimpleNamespace(loss_scaler=SimpleNamespace(cur_scale=1.0)) | ||
| with pytest.raises(ValueError, match="loss_scale must be greater than 0"): | ||
| DeepSpeedZeroOptimizer_Stage3._set_loss_scale(optimizer, 0) | ||
|
|
||
| DeepSpeedZeroOptimizer_Stage3._set_loss_scale(optimizer, 128.0) | ||
| assert optimizer.loss_scaler.cur_scale == 128.0 |
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
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 this can be in
deepspeed.utilssince it does not depend on runtime.