Skip to content

torch.result_type results are order-dependent when default dtype is float64 #273

Closed

Description

@mdhaber

torch.result_type results are order-dependent when default dtype is float64.

from array_api_compat import torch as xp
xp.set_default_dtype(xp.float64)

dtypes = ['float32', 'float64', 'int32', 'int64', 'complex64', 'complex128']
for dtype_a in dtypes:
    for dtype_b in dtypes:
        a = xp.asarray([2, 1], dtype=getattr(xp, dtype_a))
        b = xp.asarray([1, -1], dtype=getattr(xp, dtype_b))
        dtype_1 = xp.result_type(a, b, 1.0)
        dtype_2 = xp.result_type(b, a, 1.0)
        if dtype_1 != dtype_2:
            print('dtype_1 != dtype_2:', dtype_1, dtype_2)

# dtype_1 != dtype_2: torch.float64 torch.float32
# dtype_1 != dtype_2: torch.float64 torch.float32
# dtype_1 != dtype_2: torch.float32 torch.float64
# dtype_1 != dtype_2: torch.complex64 torch.complex128
# dtype_1 != dtype_2: torch.float32 torch.float64
# dtype_1 != dtype_2: torch.complex64 torch.complex128
# dtype_1 != dtype_2: torch.complex128 torch.complex64
# dtype_1 != dtype_2: torch.complex128 torch.complex64

Thanks @ev-br for figuring out that it was dependent on the default dtype!

Activity

  1. ev-br commented on Mar 17, 2025

    @ev-br
    Member

    The root cause seems to be this:

    In [1]: import torch
    
    In [2]: torch.set_default_dtype(torch.float64)
    
    In [3]: dt_a, dt_b = torch.float32, torch.int32
    
    In [4]: a, b = torch.empty(1, dtype=dt_a), torch.empty(1, dtype=dt_b)
    
    In [5]: torch.result_type(a, 1.0)
    Out[5]: torch.float32
    
    In [6]: torch.result_type(b, 1.0)
    Out[6]: torch.float64
    

    This is, strictly speaking, unspecified by the Array API spec because it combines a python float and an integer array.
    This is still something we should fix in array-api-compat, and relatively quickly.
    I think we'll release array-api-compat==1.11.2 once data-apis/array-api-tests#349 and #277 land.

  2. added this to the 1.12 milestone on Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions