Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mdhaber opened this issue Mar 16, 2025 · 1 comment · Fixed by #277
Closed

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

mdhaber opened this issue Mar 16, 2025 · 1 comment · Fixed by #277
Milestone

Comments

@mdhaber
Copy link

mdhaber commented Mar 16, 2025

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!

@ev-br
Copy link
Member

ev-br commented Mar 17, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants