-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: add zero-guards for divisor/scale edge cases (#7838) #8133
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
liuyun7345
wants to merge
2
commits into
deepspeedai:master
Choose a base branch
from
liuyun7345:fix/zero-guards-7838
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
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
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,62 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| """Regression tests for zero/division safety gaps reported in #7838.""" | ||
|
|
||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from deepspeed.utils.groups import _ensure_divisibility | ||
| from deepspeed.utils.timer import ThroughputTimer | ||
| from deepspeed.inference.v2.inference_utils import ceil_div | ||
|
|
||
|
|
||
| class _DummyTimerConfig: | ||
| enabled = False | ||
| synchronized = False | ||
|
|
||
|
|
||
| def test_ensure_divisibility_rejects_zero_denominator(): | ||
| with pytest.raises(AssertionError, match="non-zero"): | ||
| _ensure_divisibility(8, 0) | ||
|
|
||
|
|
||
| def test_ensure_divisibility_accepts_valid_inputs(): | ||
| _ensure_divisibility(8, 2) | ||
| _ensure_divisibility(0, 4) | ||
|
|
||
|
|
||
| def test_ceil_div_rejects_zero_divisor(): | ||
| with pytest.raises(ValueError, match="non-zero"): | ||
| ceil_div(10, 0) | ||
|
|
||
|
|
||
| def test_ceil_div_matches_math_ceil(): | ||
| assert ceil_div(10, 3) == math.ceil(10 / 3) | ||
| assert ceil_div(9, 3) == 3 | ||
| assert ceil_div(1, 1) == 1 | ||
|
|
||
|
|
||
| def test_throughput_timer_rejects_zero_steps_per_output(): | ||
| with pytest.raises(ValueError, match="positive"): | ||
| ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=0) | ||
|
|
||
|
|
||
| def test_throughput_timer_rejects_negative_steps_per_output(): | ||
| with pytest.raises(ValueError, match="positive"): | ||
| ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=-1) | ||
|
|
||
|
|
||
| def test_throughput_timer_report_boundary_guards_mutated_zero(): | ||
| timer = ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=2) | ||
| timer.steps_per_output = 0 | ||
| with pytest.raises(ValueError, match="positive"): | ||
| timer._is_report_boundary() | ||
|
|
||
|
|
||
| def test_throughput_timer_report_boundary_none_is_safe(): | ||
| timer = ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=None) | ||
| assert timer._is_report_boundary() is False |
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.
Can you please share the usage case that found this issue?
Uh oh!
There was an error while loading. Please reload this page.
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.
Honest answer: there is no production usage case on my side. All four guards in this PR come from issue #7838, which was reported from code inspection with synthetic repros (
ceil_div(10, 0)raising a rawZeroDivisionError), not from a real workload failure.The case for keeping this guard is narrow:
ceil_divis a small shared utility, and the guard turns a crypticZeroDivisionErrorinto a clearValueErrorat the API boundary, with zero behavior change for valid inputs. That said, if you'd rather not add guards without a demonstrated trigger, I'm happy to drop this hunk.