Skip to content

dpctl could not properly handle NaN edges in clip function #1489

Closed
@antonwolfy

Description

@antonwolfy

dpctl raises an exception for either any edge as NaN or both edges as NaN passed to dpctl.tensor.clip function.
While NumPy accept any of below combinations without an issue:

a = numpy.arange(7)

numpy.clip(a, numpy.nan, None)
numpy.clip(a, None, numpy.nan)
numpy.clip(a, numpy.nan, numpy.nan)
numpy.clip(a, -2, numpy.nan)
numpy.clip(a, numpy.nan, 10)

The below is example of errors raised by dpctl:

dpctl.__version__
# Out: '0.15.1dev2+28.gebf94b04cf'

# create an array
a = dpt.arange(7)

# clip with any edge as NaN, where another is None
dpt.clip(a, numpy.nan, None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 dpt.clip(a, numpy.nan, None)

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_clip.py:433, in clip(x, min, max, out, order)
    429     raise ValueError(
    430         "only one of `min` and `max` is permitted to be `None`"
    431     )
    432 elif max is None:
--> 433     return _clip_none(x, min, out, order, tei._maximum)
    434 elif min is None:
    435     return _clip_none(x, max, out, order, tei._minimum)

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_clip.py:249, in _clip_none(x, val, out, order, _binary_fn)
    247 _fp16 = sycl_dev.has_aspect_fp16
    248 _fp64 = sycl_dev.has_aspect_fp64
--> 249 if not _can_cast(val_dtype, res_dt, _fp16, _fp64):
    250     raise ValueError(
    251         f"function 'clip' does not support input types "
    252         f"({x_dtype}, {val_dtype}), "
    253         "and the inputs could not be safely coerced to any "
    254         "supported types according to the casting rule ''safe''."
    255     )
    257 orig_out = out

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_type_utils.py:103, in _can_cast(from_, to_, _fp16, _fp64)
     98 def _can_cast(from_: dpt.dtype, to_: dpt.dtype, _fp16: bool, _fp64: bool):
     99     """
    100     Can `from_` be cast to `to_` safely on a device with
    101     fp16 and fp64 aspects as given?
    102     """
--> 103     can_cast_v = dpt.can_cast(from_, to_)  # ask NumPy
    104     if _fp16 and _fp64:
    105         return can_cast_v

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_manipulation_functions.py:706, in can_cast(from_, to, casting)
    701     raise TypeError("Expected dtype type.")
    703 dtype_to = dpt.dtype(to)
    705 dtype_from = (
--> 706     from_.dtype if isinstance(from_, dpt.usm_ndarray) else dpt.dtype(from_)
    707 )
    709 _supported_dtype([dtype_from, dtype_to])
    711 return np.can_cast(dtype_from, dtype_to, casting)

TypeError: Tuple must have size 2, but has size 1

# clip with both edges as NaNs
dpt.clip(a, numpy.nan, numpy.nan)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 dpt.clip(a, numpy.nan, numpy.nan)

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_clip.py:531, in clip(x, min, max, out, order)
    523 buf1_dt, buf2_dt, res_dt = _check_clip_dtypes(
    524     x_dtype,
    525     min_dtype,
    526     max_dtype,
    527     sycl_dev,
    528 )
    530 if res_dt is None:
--> 531     raise ValueError(
    532         f"function '{clip}' does not support input types "
    533         f"({x_dtype}, {min_dtype}, {max_dtype}), "
    534         "and the inputs could not be safely coerced to any "
    535         "supported types according to the casting rule ''safe''."
    536     )
    538 orig_out = out
    539 if out is not None:

ValueError: function '<function clip at 0x7fade5dad310>' does not support input types (int64, float32, float32), and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''.

# clip with any edge as NaN, where another is a number
dpt.clip(a, -2, numpy.nan)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[12], line 1
----> 1 dpt.clip(a, -2, numpy.nan)

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_clip.py:531, in clip(x, min, max, out, order)
    523 buf1_dt, buf2_dt, res_dt = _check_clip_dtypes(
    524     x_dtype,
    525     min_dtype,
    526     max_dtype,
    527     sycl_dev,
    528 )
    530 if res_dt is None:
--> 531     raise ValueError(
    532         f"function '{clip}' does not support input types "
    533         f"({x_dtype}, {min_dtype}, {max_dtype}), "
    534         "and the inputs could not be safely coerced to any "
    535         "supported types according to the casting rule ''safe''."
    536     )
    538 orig_out = out
    539 if out is not None:

ValueError: function '<function clip at 0x7fade5dad310>' does not support input types (int64, int64, float32), and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''.

Metadata

Metadata

Assignees

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