← Back to Pull requests

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

Open
unknown opened this pull request with 519 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 26, 2026 16:11 UTC
Author

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, 264) 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 / 224 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 224 | 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(264, i4), equal(i8, -264) (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?
  • Diff: 519 lines (+389 / -130) across 6 files Reviewer: vlad-perevezentsev Review duration: 5821m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 519 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.374
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3202"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.7% (MEDIUM) · Higher = larger blast radius
    47.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.4% · Safe to merge
    25.4%