|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# DeepSpeed Team |
| 5 | + |
| 6 | +"""Regression tests for zero/division safety gaps reported in #7838.""" |
| 7 | + |
| 8 | +import math |
| 9 | + |
| 10 | +import pytest |
| 11 | +import torch |
| 12 | + |
| 13 | +from deepspeed.utils.groups import _ensure_divisibility |
| 14 | +from deepspeed.utils.timer import ThroughputTimer |
| 15 | +from deepspeed.inference.v2.inference_utils import ceil_div |
| 16 | + |
| 17 | + |
| 18 | +class _DummyTimerConfig: |
| 19 | + enabled = False |
| 20 | + synchronized = False |
| 21 | + |
| 22 | + |
| 23 | +def test_ensure_divisibility_rejects_zero_denominator(): |
| 24 | + with pytest.raises(AssertionError, match="non-zero"): |
| 25 | + _ensure_divisibility(8, 0) |
| 26 | + |
| 27 | + |
| 28 | +def test_ensure_divisibility_accepts_valid_inputs(): |
| 29 | + _ensure_divisibility(8, 2) |
| 30 | + _ensure_divisibility(0, 4) |
| 31 | + |
| 32 | + |
| 33 | +def test_ceil_div_rejects_zero_divisor(): |
| 34 | + with pytest.raises(ValueError, match="non-zero"): |
| 35 | + ceil_div(10, 0) |
| 36 | + |
| 37 | + |
| 38 | +def test_ceil_div_matches_math_ceil(): |
| 39 | + assert ceil_div(10, 3) == math.ceil(10 / 3) |
| 40 | + assert ceil_div(9, 3) == 3 |
| 41 | + assert ceil_div(1, 1) == 1 |
| 42 | + |
| 43 | + |
| 44 | +def test_throughput_timer_rejects_zero_steps_per_output(): |
| 45 | + with pytest.raises(ValueError, match="positive"): |
| 46 | + ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=0) |
| 47 | + |
| 48 | + |
| 49 | +def test_throughput_timer_rejects_negative_steps_per_output(): |
| 50 | + with pytest.raises(ValueError, match="positive"): |
| 51 | + ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=-1) |
| 52 | + |
| 53 | + |
| 54 | +def test_throughput_timer_report_boundary_guards_mutated_zero(): |
| 55 | + timer = ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=2) |
| 56 | + timer.steps_per_output = 0 |
| 57 | + with pytest.raises(ValueError, match="positive"): |
| 58 | + timer._is_report_boundary() |
| 59 | + |
| 60 | + |
| 61 | +def test_throughput_timer_report_boundary_none_is_safe(): |
| 62 | + timer = ThroughputTimer(_DummyTimerConfig(), batch_size=1, steps_per_output=None) |
| 63 | + assert timer._is_report_boundary() is False |
| 64 | + |
| 65 | + |
| 66 | +def _import_hpu_fp_quantizer_builder(): |
| 67 | + try: |
| 68 | + from op_builder.hpu.fp_quantizer import FPQuantizerBuilder |
| 69 | + return FPQuantizerBuilder |
| 70 | + except ImportError: |
| 71 | + pytest.skip("HPU FPQuantizer builder is not available") |
| 72 | + |
| 73 | + |
| 74 | +def test_hpu_fp_quantizer_dequantize_rejects_zero_scale(): |
| 75 | + FPQuantizerBuilder = _import_hpu_fp_quantizer_builder() |
| 76 | + scale = torch.tensor([0.0, 1.0]) |
| 77 | + fp_out = torch.empty(2, 4) |
| 78 | + input_q = torch.empty(2, 4) |
| 79 | + with pytest.raises(ValueError, match="finite non-zero"): |
| 80 | + FPQuantizerBuilder.dequantize(fp_out, input_q, scale, group_size=4, q_mantisa_bits=3, q_exponent_bits=4) |
| 81 | + |
| 82 | + |
| 83 | +def test_hpu_fp_quantizer_dequantize_rejects_nonfinite_scale(): |
| 84 | + FPQuantizerBuilder = _import_hpu_fp_quantizer_builder() |
| 85 | + scale = torch.tensor([float("nan"), 1.0]) |
| 86 | + fp_out = torch.empty(2, 4) |
| 87 | + input_q = torch.empty(2, 4) |
| 88 | + with pytest.raises(ValueError, match="finite non-zero"): |
| 89 | + FPQuantizerBuilder.dequantize(fp_out, input_q, scale, group_size=4, q_mantisa_bits=3, q_exponent_bits=4) |
| 90 | + |
| 91 | + |
| 92 | +def test_hpu_fp_quantizer_dequantize_rejects_zero_scalar_scale(): |
| 93 | + FPQuantizerBuilder = _import_hpu_fp_quantizer_builder() |
| 94 | + fp_out = torch.empty(2, 4) |
| 95 | + input_q = torch.empty(2, 4) |
| 96 | + with pytest.raises(ValueError, match="finite non-zero"): |
| 97 | + FPQuantizerBuilder.dequantize(fp_out, input_q, 0.0, group_size=4, q_mantisa_bits=3, q_exponent_bits=4) |
0 commit comments