Skip to content

fix: add zero-guards for divisor/scale edge cases (#7838) - #8133

Open
liuyun7345 wants to merge 2 commits into
deepspeedai:masterfrom
liuyun7345:fix/zero-guards-7838
Open

liuyun7345 wants to merge 2 commits into
deepspeedai:masterfrom
liuyun7345:fix/zero-guards-7838

Conversation

@liuyun7345

Copy link
Copy Markdown

Summary

Fixes #7838 by adding explicit zero/non-finite guards at the four reported sites so DeepSpeed fails early with a clear error instead of raising raw ZeroDivisionError or silently propagating inf/nan.

This picks up the inactive draft #7855 (no updates since 2026-02-17).

Changes

  1. deepspeed/utils/groups.py — _ensure_divisibility asserts denominator != 0 before modulo.
  2. deepspeed/utils/timer.py — ThroughputTimer rejects non-positive steps_per_output in __init__, and _is_report_boundary re-checks if the attribute is later mutated.
  3. deepspeed/inference/v2/inference_utils.py — ceil_div raises ValueError when b == 0.
  4. op_builder/hpu/fp_quantizer.py — dequantize rejects zero / non-finite scales before computing 1.0 / scale.
  5. tests/unit/utils/test_zero_guards.py — regression tests for each failure mode (HPU builder tests skip if that module is unavailable).

Test plan

  • _ensure_divisibility(8, 0) raises clear AssertionError
  • valid _ensure_divisibility inputs still pass
  • ceil_div(10, 0) raises ValueError
  • ceil_div matches math.ceil for valid inputs
  • ThroughputTimer(..., steps_per_output=0/-1) raises ValueError
  • mutated steps_per_output=0 is caught in _is_report_boundary
  • HPU dequantize rejects zero / NaN / zero-scalar scales before HPU op

Fixes #7838

Comment thread tests/unit/utils/test_zero_guards.py Outdated
pytest.skip("HPU FPQuantizer builder is not available")


def test_hpu_fp_quantizer_dequantize_rejects_zero_scale():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests seems to be hpu-specific. Can they not be generalized?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — generalizing is possible, and I'll do it. The 1.0 / scale inversion currently only exists in the HPU FPQuantizer.dequantize, but the same failure mode applies to all backends: quantize computes scale = q_range / max_vals, so inf/nan in the input drives max_vals to inf and silently produces scale = 0.

I'll move the zero/non-finite scale validation up into the shared front-end FP_Quantize.dequantize (deepspeed/ops/fp_quantizer/quantize.py) so it covers CUDA and HPU alike, and relocate the test into tests/unit/ops/fp_quantizer/test_fp_quant.py using the existing accelerator-agnostic pattern (deepspeed.ops.op_builder.FPQuantizerBuilder + module-level skip via __compatible_ops__). The direct op_builder.hpu import goes away.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — pushed as 1098c0a. The zero/non-finite scale validation now lives in the shared front-end FP_Quantize.dequantize / selective_dequantize (deepspeed/ops/fp_quantizer/quantize.py), and the regression tests moved to tests/unit/ops/fp_quantizer/test_fp_quant.py using the accelerator-agnostic pattern (deepspeed.ops.op_builder.FPQuantizerBuilder + module-level skip via __compatible_ops__), exercised through the public FP_Quantize.dequantize API. The HPU-only guard in op_builder/hpu/fp_quantizer.py and the direct op_builder.hpu import in the tests are both gone.

"""
Return ceil(a / b).
"""
if b == 0:

Copy link
Copy Markdown
Collaborator

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?

@liuyun7345 liuyun7345 Jul 31, 2026 •

Copy link
Copy Markdown
Author

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 raw ZeroDivisionError), not from a real workload failure.

The case for keeping this guard is narrow: ceil_div is a small shared utility, and the guard turns a cryptic ZeroDivisionError into a clear ValueError at 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.

liuyun7345 added a commit to liuyun7345/DeepSpeed that referenced this pull request Jul 31, 2026
…ai#8133)

Move the zero/non-finite scale validation out of the HPU-only
`op_builder/hpu/fp_quantizer.py` and into the shared front-end
`FP_Quantize.dequantize` / `selective_dequantize` in
`deepspeed/ops/fp_quantizer/quantize.py`. The same `1.0 / scale`
failure mode applies to every backend (CUDA included): `quantize`
computes `scale = q_range / max_vals`, so inf/nan in the input
silently yields `scale = 0` and corrupts dequantized output. One
guard at the API boundary covers all backends.

Relocate the regression tests from `tests/unit/utils/test_zero_guards.py`
into `tests/unit/ops/fp_quantizer/test_fp_quant.py` using the existing
accelerator-agnostic pattern (`deepspeed.ops.op_builder.FPQuantizerBuilder`
+ module-level skip via `__compatible_ops__`), exercising the guard
through the public `FP_Quantize.dequantize` API. Drop the direct
`op_builder.hpu` import and the now-unused `torch` import from
`test_zero_guards.py`.

Signed-off-by: liuyun7345 <[email protected]>
Co-authored-by: Cursor <[email protected]>
@liuyun7345

Copy link
Copy Markdown
Author

Hi @sfc-gh-truwase, gentle ping on this one.

The July 30 feedback has been addressed in 1098c0a: the zero/non-finite scale validation now lives in the shared FP_Quantize.dequantize / selective_dequantize front-end (deepspeed/ops/fp_quantizer/quantize.py), and the regression tests moved to tests/unit/ops/fp_quantizer/test_fp_quant.py with an accelerator-agnostic pattern.

The two red checks are from the July 31 run and fail with ModuleNotFoundError: No module named 'modal' before reaching the tests — recent runs of the same checks on other PRs are green, so this looks like a transient CI environment issue. A re-run should clear them.

Prevent ZeroDivisionError and silent non-finite propagation at four
reported sites:

1. groups._ensure_divisibility: reject denominator == 0 before modulo
2. ThroughputTimer: reject non-positive steps_per_output at init and
   report-boundary time
3. inference_utils.ceil_div: reject divisor == 0
4. FP_Quantize.dequantize/selective_dequantize: reject zero/non-finite
   scales before 1/scale, at the shared front-end so every backend is
   covered

Adds accelerator-agnostic regression tests for each failure mode.

Picks up the inactive draft deepspeedai#7855. Fixes deepspeedai#7838.

Rebased onto master, resolving the overlap with deepspeedai#8950 in
deepspeed/ops/fp_quantizer/quantize.py and
tests/unit/ops/fp_quantizer/test_fp_quant.py: both sets of new tests
are kept, and the accidentally duplicated guard tests from the previous
head are deduplicated.

Co-authored-by: Cursor <[email protected]>
Signed-off-by: liuyun7345 <[email protected]>
@liuyun7345
liuyun7345 force-pushed the fix/zero-guards-7838 branch from 1098c0a to eedfd6b Compare November 6, 2026 16:44
@liuyun7345

Copy link
Copy Markdown
Author

Hi @sfc-gh-truwase, gentle follow-up on this one.

Since the last ping, master has moved underneath the PR — #8950 touched the same regions of deepspeed/ops/fp_quantizer/quantize.py and tests/unit/ops/fp_quantizer/test_fp_quant.py — so I rebased onto latest master: the _validate_scale guards now sit alongside #8950's _validate_shape checks, and both sets of new tests are kept. The red modal-torch-latest checks were from the July 31 run and predate #8950's workflow fix; CI has been re-triggered on the new head.

The July 30 feedback remains addressed (shared FP_Quantize.dequantize / selective_dequantize front-end, accelerator-agnostic tests in tests/unit/ops/fp_quantizer/test_fp_quant.py). Could you take another look when you get a chance?

@liuyun7345

Copy link
Copy Markdown
Author

Hi @sfc-gh-truwase, a CUDA validation update for this PR.

I pushed the follow-up fix in 40197c0 (fix: validate encoded FPQuantizer scales). During A100 testing, I found that the previous guard treated zero bytes in valid uint8-encoded float32 scale metadata as zero scale values. The guard now decodes that metadata as float32 for validation, while leaving the original metadata passed to the CUDA backend unchanged.

The regression tests cover valid metadata containing zero bytes, rejection of +0/-0/NaN/+Inf/-Inf through both full and selective dequantization, and invalid scalar scale inputs.

On a single A100-SXM4-80GB (Python 3.11.15, PyTorch 2.13.0+cu130, NVIDIA driver 580.178.04, nvcc 13.2.51), the local fixed-source run completed with:

  • Full tests/unit/ops/fp_quantizer/test_fp_quant.py: 24 passed.
  • tests/unit/utils/test_zero_guards.py: 8 passed.
  • Total: 32 passed, 0 failed, 0 skipped (5.04 s).

This run used eedfd6b plus the local scale-validation and regression-test changes, before the follow-up commit was created. It is local CUDA validation, not a report of passing upstream CI. HPU validation remains untested.

Could you review the updated guard and tests when you have a chance? Thank you.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Multiple missing zero-guards cause ZeroDivisionError / non-finite values across DeepSpeed (4 locations)

2 participants