Skip to content

dpctl.tensor.result_type raises ValueError #1874

Closed

Description

@vtavana

The following example raises a ValueError in dpctl while it works fine in NumPy.

import dpctl.tensor as dpt, numpy
dtype = numpy.dtype(numpy.float64)
scalar = dtype.type(3)
numpy.result_type(scalar, scalar)
# dtype('float64')

>>> dpt.result_type(scalar, scalar)    
# File ~/micromamba/envs/dpnp_2024/lib/python3.11/site-packages/dpctl/tensor/_type_utils.py:785, in result_type(*arrays_and_dtypes)
#    783         res_dt = _to_device_supported_dtype_impl(res_dt, has_fp16, has_fp64)
#    784 else:
#--> 785     res_dt = np.result_type(*dtypes)
#    786     if weak_dtypes:
#    787         weak_dt_obj = [wdt.get() for wdt in weak_dtypes]
#ValueError: at least one array or dtype is required

Activity

  1. self-assigned this
    on Oct 22, 2024
  2. oleksandr-pavlyk commented on Oct 22, 2024

    @oleksandr-pavlyk
    Contributor

    Don't you think this is as designed? dpctl can not combine two scalars, and so it is reasonable for result_type to raise for such inputs as well.

  3. vtavana commented on Oct 22, 2024

    @vtavana
    ContributorAuthor

    Don't you think this is as designed? dpctl can not combine two scalars, and so it is reasonable for result_type to raise for such inputs as well.

    It does not raise an error for all the cases, for instance, it works fine with float32.

    import dpctl.tensor as dpt, numpy
    dtype = numpy.dtype(numpy.float32)
    scalar = dtype.type(3)
    dpt.result_type(scalar, scalar) 
    # dtype('float32')
  4. oleksandr-pavlyk commented on Oct 22, 2024

    @oleksandr-pavlyk
    Contributor

    I think the real issue is that dpt.result_type does not raise if none of the arguments is an array. I intend to fix that.

    The discrepancy you found with np.float32 and np.float64 comes from np.dtype(np.float64).type being Python native built-in type float, whereas np.dtype(np.float32).type is a custom scalar type np.float32.

    dpt.result_type treats Python scalars bool, int, float and complex as weak types as per NEP-0050, but treats other types as possible inputs to dpt.dtype constructor.

  5. oleksandr-pavlyk commented on Oct 23, 2024

    @oleksandr-pavlyk
    Contributor

    Ok, the result_type implementation has an issue when all inputs of weak dtypes, i.e. Python scalars

    In [2]: dpt.result_type(1, 1.0)
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    Cell In[2], line 1
    ----> 1 dpt.result_type(1, 1.0)
    
    File ~/repos/dpctl/dpctl/tensor/_type_utils.py:783, in result_type(*arrays_and_dtypes)
        781         res_dt = _to_device_supported_dtype_impl(res_dt, has_fp16, has_fp64)
        782 else:
    --> 783     res_dt = np.result_type(*dtypes)
        784     if weak_dtypes:
        785         weak_dt_obj = [wdt.get() for wdt in weak_dtypes]
    
    ValueError: at least one array or dtype is required
    
  6. added a commit that references this issue on Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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