Skip to content

Fix clip for Numpy 2.0 #1744

@ndgrigorian

Description

@ndgrigorian

clip currently fails for out-of-bounds Python integers due to Numpy 2.0 changes:

In [21]: dpt.clip(dpt.asarray([0, 255], dtype=dpt.uint8), 0, 4550)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Cell In[21], line 1
----> 1 dpt.clip(dpt.asarray([0, 255], dtype=dpt.uint8), 0, 4550)

File ~/repos/dpctl/dpctl/tensor/_clip.py:634, in clip(x, min, max, out, order)
    632     a_max = max
    633 else:
--> 634     a_max = dpt.asarray(max, dtype=max_dtype, sycl_queue=exec_q)
    636 if order == "A":
    637     order = (
    638         "F"
    639         if all(
   (...)
    647         else "C"
    648     )

File ~/repos/dpctl/dpctl/tensor/_ctors.py:649, in asarray(obj, dtype, device, copy, usm_type, sycl_queue, order)
    644     raise ValueError(
    645         f"Converting {type(obj)} to usm_ndarray requires a copy"
    646     )
    647 # obj is a scalar, create 0d array
    648 return _asarray_from_numpy_ndarray(
--> 649     np.asarray(obj, dtype=dtype),
    650     dtype=dtype,
    651     usm_type=usm_type,
    652     sycl_queue=sycl_queue,
    653     order="C",
    654 )

OverflowError: Python integer 4550 out of bounds for uint8

Numpy has been doing the same and has changed this in numpy/numpy#26892

dpctl should implement a similar work-around

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions