Skip to content

Fix out-of-range Python integers in binary elementwise functions - #3202

Merged
antonwolfy merged 11 commits into
masterfrom
fix-out-of-range-py-int-in-binary-ufuncs
Dec 2, 2026
Merged

antonwolfy merged 11 commits into
masterfrom
fix-out-of-range-py-int-in-binary-ufuncs

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Binary elementwise functions raised OverflowError or ValueError for a Python integer that the array data type cannot hold, where NumPy returns a result:

import dpnp as np

x = np.array([1, 2], dtype=np.int32)
np.arctan2(x, 2**63)  # OverflowError: Python int too large to convert to C long
np.less(x, 2**64)  # ValueError: Unrecognized typenum 17 encountered.
np.logical_and(np.array([0, 1], dtype=np.int8), 300)  # OverflowError: Python integer 300 out of bounds for int8

This PR:

  • In 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, ldexp with a Python integer mantissa, and divide of a boolean array), and saves an allocation and a copy kernel per call.
  • Gives the comparison functions NumPy's result for an integer array and a Python integer outside the range of every integer data type, which depends on the sign of the integer alone. The array is compared with the maximum of its data type instead, so it is not cast.
  • Converts such an integer to a float in dpnp.divide.
  • Takes the truth value of a Python integer in dpnp.logical_and, dpnp.logical_or and dpnp.logical_xor after checking that it fits int64, as NumPy does. Outside that range they now raise OverflowError for every array data type, as NumPy does, where they used to return a result for a floating-point array.
  • Raises OverflowError instead of a misleading ValueError in the dpnp.tensor comparison functions, dpnp.tensor.divide, dpnp.tensor.isin and dpnp.tensor.searchsorted for a Python integer outside the int64 and uint64 ranges.

The Array API standard leaves all these cases unspecified: a Python integer outside the bounds of the array data type, integer arrays in atan2 and similar functions, and Python integers in the logical functions. Results of the cases the standard covers do not change.

Not changed: dpnp.float_power of an int8 array returns float32, and dpnp.ldexp of a Python integer and an integer array returns float64, where NumPy returns float64 and float16; both also happen with small integers. dpnp.searchsorted(x, 2**64) now raises OverflowError, where NumPy returns len(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:

Case GPU CPU
Python scalar the kernel reads natively: add, less, equal, logical_and, divide, x += 1 no change no change
Scalar that needed a cast: arctan2(i4, 2), hypot(i8, 3), divide(i4, 2), divide(b1, 2) about 10 % faster (20-40 µs per call) at n ≤ 4096, no change at 2**24 no change
logical_and(b1, 1) 2.3x / 2.1x / 1.6x faster 1.9x / 1.8x / 7.5x faster
less(i4, 2**64), less(2**64, i4), equal(i8, -2**64) (raised before) 1.3x / 1.1x / 1.0x the time of the in-range comparison 1.0-1.1x the time of the in-range comparison

"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, divide or logical function with a Python integer.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

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.
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/index.html

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_81 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

No base build to compare — fix-out-of-range-py-int-in-binary-ufuncs into master

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread CHANGELOG.md Resolved
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 vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

antonwolfy and others added 3 commits December 2, 2026 13:40
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.
@antonwolfy
antonwolfy merged commit 37716a1 into master Dec 2, 2026
45 checks passed
@antonwolfy
antonwolfy deleted the fix-out-of-range-py-int-in-binary-ufuncs branch December 2, 2026 21:51
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.

4 participants