Skip to content

Commit d9dab91

Browse files
committed
Adds a test for overflow behavior in _divide_by_scalar
The scalar is expected to overflow to infinity when cast to the array's type and the result should be 0 (for a finite numerator)
1 parent 3077e73 commit d9dab91

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

dpctl/tests/elementwise/test_divide.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import dpctl
2323
import dpctl.tensor as dpt
24+
from dpctl.tensor._tensor_elementwise_impl import _divide_by_scalar
2425
from dpctl.tensor._type_utils import _can_cast
2526
from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported
27+
from dpctl.utils import SequentialOrderManager
2628

2729
from .utils import (
2830
_all_dtypes,
@@ -271,3 +273,26 @@ def test_divide_gh_1711():
271273
assert isinstance(res, dpt.usm_ndarray)
272274
assert res.dtype.kind == "f"
273275
assert dpt.allclose(res, dpt.asarray(3, dtype="i4") / -2)
276+
277+
278+
# don't test for overflowing double as Python won't cast
279+
# an Python integer of that size to a Python float
280+
@pytest.mark.parametrize("fp_dt", [dpt.float16, dpt.float32])
281+
def test_divide_by_scalar_overflow(fp_dt):
282+
q = get_queue_or_skip()
283+
skip_if_dtype_not_supported(fp_dt, q)
284+
285+
x = dpt.ones(10, dtype=fp_dt, sycl_queue=q)
286+
out = dpt.empty_like(x)
287+
288+
max_exp = np.finfo(fp_dt).maxexp
289+
sca = 2**max_exp
290+
291+
_manager = SequentialOrderManager[q]
292+
dep_evs = _manager.submitted_events
293+
_, ev = _divide_by_scalar(
294+
src=x, scalar=sca, dst=out, sycl_queue=q, depends=dep_evs
295+
)
296+
ev.wait()
297+
298+
assert dpt.all(out == 0)

0 commit comments

Comments
 (0)