Skip to content

BUG: torch: meshgrid defaults to indexing="xy" #322

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

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions array_api_compat/torch/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from functools import reduce as _reduce, wraps as _wraps
from builtins import all as _builtin_all, any as _builtin_any
from typing import Any, List, Optional, Sequence, Tuple, Union
from typing import Any, List, Optional, Sequence, Tuple, Union, Literal

import torch

Expand Down Expand Up @@ -828,6 +828,12 @@ def sign(x: Array, /) -> Array:
return out


def meshgrid(*arrays: Array, indexing: Literal['xy', 'ij'] = 'xy') -> List[Array]:
# enforce the default of 'xy'
# TODO: is the return type a list or a tuple
return list(torch.meshgrid(*arrays, indexing='xy'))


__all__ = ['__array_namespace_info__', 'asarray', 'result_type', 'can_cast',
'permute_dims', 'bitwise_invert', 'newaxis', 'conj', 'add',
'atan2', 'bitwise_and', 'bitwise_left_shift', 'bitwise_or',
Expand All @@ -844,6 +850,6 @@ def sign(x: Array, /) -> Array:
'UniqueAllResult', 'UniqueCountsResult', 'UniqueInverseResult',
'unique_all', 'unique_counts', 'unique_inverse', 'unique_values',
'matmul', 'matrix_transpose', 'vecdot', 'tensordot', 'isdtype',
'take', 'take_along_axis', 'sign', 'finfo', 'iinfo', 'repeat']
'take', 'take_along_axis', 'sign', 'finfo', 'iinfo', 'repeat', 'meshgrid']

_all_ignore = ['torch', 'get_xp']
17 changes: 17 additions & 0 deletions tests/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,20 @@ def test_gh_273(self, default_dt, dtype_a, dtype_b):
assert dtype_1 == dtype_2
finally:
torch.set_default_dtype(prev_default)


def test_meshgrid():
"""Verify that array_api_compat.torch.meshgrid defaults to indexing='xy'."""

x, y = xp.asarray([1, 2]), xp.asarray([4])

X, Y = xp.meshgrid(x, y)

# output of torch.meshgrid(x, y, indexing='xy') -- indexing='ij' is different
X_xy, Y_xy = xp.asarray([[1, 2]]), xp.asarray([[4, 4]])

assert X.shape == X_xy.shape
assert xp.all(X == X_xy)

assert Y.shape == Y_xy.shape
assert xp.all(Y == Y_xy)
Loading