Skip to content

Fix OverflowError in comparison functions and divide for out-of-range integer scalars - #3017

Open
antonwolfy wants to merge 4 commits into
masterfrom
fix/int-scalar-comparison-overflow
Open

Fix OverflowError in comparison functions and divide for out-of-range integer scalars#3017
antonwolfy wants to merge 4 commits into
masterfrom
fix/int-scalar-comparison-overflow

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Comparison ufuncs (dpnp.equal, dpnp.not_equal, dpnp.less, dpnp.less_equal, dpnp.greater, dpnp.greater_equal) and dpnp.divide raised OverflowError when an integer array was compared with (or divided by) a Python int scalar outside the array dtype's range. NumPy handles these cases and returns a well-defined result, so this was a parity gap.

For example:

import dpnp
dpnp.less(dpnp.array([0, 1, 2**63, 2**64 - 1], dtype="u8"), -1)
# Out: OverflowError: Python integer -1 out of bounds for uint64

dpnp.divide(dpnp.array([0, 2, 2**63], dtype="u8"), -1)
# Out: OverflowError: Python integer -1 out of bounds for uint64

NumPy instead evaluates the comparison against the true value of the scalar (-1 is less than any uint64, so the result is all-False) and returns floating-point results for the division.

These functions used the default weak-type resolver _resolve_weak_types, which forces a Python integer scalar into the other operand's dtype before dispatch. When the scalar does not fit that dtype (a negative value against an unsigned array, or a value larger than the signed range), the coercion overflows and raises.

This PR:

  • Pass weak_type_resolver=_resolve_weak_types_all_py_ints to the six comparison ufuncs in dpnp_iface_logic.py.
  • Pass the same resolver to dpnp.divide in dpnp_iface_mathematical.py (also covers dpnp.true_divide).
  • Un-skip test test_binary_array_pyscalar_int, which now passes

dpnp.floor_divide is intentionally left unchanged: NumPy itself raises OverflowError for out-of-range integer scalars there, and dpnp already matches that behavior.

  • 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?

Wire the `_resolve_weak_types_all_py_ints` weak-type resolver into the
`equal`, `not_equal`, `less`, `less_equal`, `greater`, and `greater_equal`
ufuncs. Previously these used the default `_resolve_weak_types`, which
forced a Python integer scalar into the array's dtype and raised
`OverflowError` when the value was out of range (e.g. comparing a `uint64`
array against `-1`, or an `int64` array against `2**64-1`). The resolver
promotes such scalars via `numpy.min_scalar_type`, matching NumPy.

Un-skip the vendored cupy `test_binary_array_pyscalar_int` test, which now
passes, and replace the `SAT-8549` skip on `test_binary_array_pyscalar_int_and_bool`
with a reason explaining the intentional NumPy-parity divergence from cupy.
`dpnp.divide` (and thus `dpnp.true_divide`) raised `OverflowError` when
dividing an integer array by a Python integer scalar outside the array
dtype's range, e.g. `dpnp.divide(uint64_array, -1)`, while NumPy returns
floating-point results. Pass `_resolve_weak_types_all_py_ints` as the
weak-type resolver, matching NumPy and the vendored `dpnp.tensor.divide`.

`floor_divide` is intentionally left unchanged: NumPy itself raises
`OverflowError` there, and dpnp already matches that behavior.
@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 11, 2026
@antonwolfy antonwolfy self-assigned this Aug 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev4=py314h509198e_7 ran successfully.
Passed: 1375
Failed: 2
Skipped: 5

@antonwolfy
antonwolfy marked this pull request as ready for review August 11, 2026 17:17
@coveralls

coveralls commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

No base build to compare — fix/int-scalar-comparison-overflow into master

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.

3 participants