Repository navigation
Fix out-of-range Python integers in binary elementwise functions - #3202
Conversation
Binary functions raised `OverflowError` or `ValueError` for a Python integer that the array data type cannot hold, where NumPy returns a result, e.g. `dpnp.arctan2(x, 2**63)` or `dpnp.less(x, 2**64)` for an integer array `x`, and `dpnp.logical_and(x, 300)` for an `int8` array `x`. - `dpnp.tensor`: a scalar operand is converted once, to the data type the kernel reads, instead of to the resolved data type and then into a buffer of the kernel's type. This fixes the functions that compute in floating point (`arctan2`, `hypot`, `copysign`, `heaviside`, `logaddexp`, `logaddexp2`, `nextafter`, `float_power`, `ldexp` with a Python integer mantissa, and `divide` of a boolean array), and saves a 0-d allocation and a copy kernel per call. - `DPNPBinaryFunc` takes a `py_int_converter` that replaces a Python integer argument before its data type is resolved: the comparison functions compare an integer array with an integer outside the range of every integer data type as with the infinity of its sign, `dpnp.divide` converts such an integer to a float, and `dpnp.logical_and`, `dpnp.logical_or` and `dpnp.logical_xor` take the truth value of an integer in the `int64` range and raise `OverflowError` outside it, as NumPy does.
… dpnp.tensor
`_resolve_weak_types_all_py_ints` resolved a Python integer outside the range of the array data type to `numpy.min_scalar_type` of it, which is the `object` data type for an integer outside the `int64` and `uint64` ranges. The `dpnp.tensor` comparison functions, `dpnp.tensor.divide` and `dpnp.tensor.searchsorted` then raised a misleading `ValueError` ("Unrecognized typenum 17 encountered.", "Dpctl doesn't support dtype object."). They now raise `OverflowError` naming the integer.
…casting it The comparison functions replaced a Python integer outside the range of every integer data type with an infinity, which cast the integer array to a floating-point data type. NumPy's result depends on the sign of the integer alone, so `DPNPBinaryComparisonFunc` computes it from the sign and returns the array compared with the maximum of its data type, which is true or false for every element and keeps the checks of `out` and the layout of any comparison. With `dtype` given, the integer goes through the usual path. `py_int_for_divide` checks the range of the integer before the data type of the other argument, which most calls then skip.
|
View rendered docs @ https://intelpython.github.io/dpnp/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_81 ran successfully. |
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
I tested it on GPU and CPU: the new tests pass and fail on master, and comparisons, divide, the logical and floating-point functions now match NumPy for out-of-range integers. A few small comments below.
For a Python integer outside the range of every integer data type, `DPNPBinaryComparisonFunc` passed the arguments it does not support on to `dpnp.less_equal` or `dpnp.greater`, so the error named a function the user did not call: `dpnp.less(a, 2**70, casting="no")` raised about `less_equal`. The checks of `DPNPBinaryFunc.__call__` move to a `_check_args` method, which the shortcut calls first; only `out` and `order` go on to the comparison it delegates to.
`py_int_for_divide` repeated the checks of `_is_out_of_int_range` and `_is_int_array`.
The first entry read as if every binary function now accepted such an integer, but `dpnp.add(int8_array, 300)` still raises `OverflowError`, as in NumPy, so it names the comparison functions, `dpnp.divide`, the logical functions and those computing in floating point. The second one adds `dpnp.tensor.isin`, which now raises `OverflowError` instead of `ValueError` too.
…-int-in-binary-ufuncs # Conflicts: # CHANGELOG.md
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
No more comments from my side
LGTM
Thank you @antonwolfy for fixing out-of-range Python integers in binary functions
It needs a rebase on master: it conflicts in CHANGELOG.md.
There was a problem hiding this comment.
🟡 Changes recommended
Scalar preprocessing before explicit dtype casting causes logical functions to raise OverflowError for large integers instead of honoring the requested boolean conversion.
1 open issue
What changed in this PR
Improves NumPy compatibility for out-of-range Python integers in dpnp’s binary elementwise operations.
Changes:
- Converts scalar operands directly to the kernel’s input dtype.
- Adds integer handling for comparisons, division and logical operations, with clearer tensor overflow errors.
- Adds regression tests and changelog entries.
| File | Description |
|---|---|
dpnp/tests/test_logic.py |
Tests large integers in comparisons and logical operations. |
dpnp/tests/test_binary_ufuncs.py |
Tests large integers in floating-point operations. |
dpnp/tests/tensor/elementwise/test_type_utils.py |
Tests integer dtype resolution and overflow. |
dpnp/tests/tensor/elementwise/test_less.py |
Tests comparison overflow errors. |
dpnp/tests/tensor/elementwise/test_atan2.py |
Tests large-integer scalar conversion. |
dpnp/tests/tensor/elementwise/test_add.py |
Tests scalar casting with overlapping output. |
dpnp/tensor/_type_utils.py |
Raises overflow when no integer dtype fits. |
dpnp/tensor/_elementwise_common.py |
Converts scalars directly to kernel dtypes. |
dpnp/dpnp_iface_mathematical.py |
Enables integer preprocessing for division. |
dpnp/dpnp_iface_logic.py |
Connects comparison and logical integer handling. |
dpnp/dpnp_algo/dpnp_elementwise_common.py |
Implements scalar preprocessing and comparison handling. |
CHANGELOG.md |
Documents compatibility fixes and overflow behavior. |
Review effort: Balanced
NumPy converts the Python integer to `int64` before `dtype` applies, so `numpy.logical_and(numpy.array([True]), 2**63, dtype=bool)` raises `OverflowError`, as dpnp now does; `test_logical_out_of_int64_range` checks it for both libraries.

Binary elementwise functions raised
OverflowErrororValueErrorfor a Python integer that the array data type cannot hold, where NumPy returns a result:This PR:
dpnp.tensor, converts a scalar operand once, to the data type the kernel reads, instead of to the resolved data type and then into a 0-d buffer of the kernel's data type. This fixes the functions that compute in floating point for an integer or boolean array (arctan2,hypot,copysign,heaviside,logaddexp,logaddexp2,nextafter,float_power,ldexpwith a Python integer mantissa, anddivideof a boolean array), and saves an allocation and a copy kernel per call.dpnp.divide.dpnp.logical_and,dpnp.logical_oranddpnp.logical_xorafter checking that it fitsint64, as NumPy does. Outside that range they now raiseOverflowErrorfor every array data type, as NumPy does, where they used to return a result for a floating-point array.OverflowErrorinstead of a misleadingValueErrorin thedpnp.tensorcomparison functions,dpnp.tensor.divide,dpnp.tensor.isinanddpnp.tensor.searchsortedfor a Python integer outside theint64anduint64ranges.The Array API standard leaves all these cases unspecified: a Python integer outside the bounds of the array data type, integer arrays in
atan2and similar functions, and Python integers in the logical functions. Results of the cases the standard covers do not change.Not changed:
dpnp.float_powerof anint8array returnsfloat32, anddpnp.ldexpof a Python integer and an integer array returnsfloat64, where NumPy returnsfloat64andfloat16; both also happen with small integers.dpnp.searchsorted(x, 2**64)now raisesOverflowError, where NumPy returnslen(x).Performance against master on an Intel Data Center GPU Max 1100 (Level Zero) and a Xeon Platinum 8480+ (OpenCL CPU device), n = 1 / 4096 / 2**24 elements, 8 rounds of fresh processes in alternating order:
add,less,equal,logical_and,divide,x += 1arctan2(i4, 2),hypot(i8, 3),divide(i4, 2),divide(b1, 2)logical_and(b1, 1)less(i4, 2**64),less(2**64, i4),equal(i8, -2**64)(raised before)"No change" means within the run-to-run variation of untouched code (GPU 3-9 %, CPU 3-49 %, largest at small n). At 2**24 on the GPU, the cases that make float64 temporaries vary by up to 50 % between processes on both master and this branch; their medians stay within 5 %. The added Python cost is 53 ns per binary call and up to 0.2 µs for a comparison,
divideor logical function with a Python integer.