Skip to content
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
18 changes: 17 additions & 1 deletion array_api_compat/torch/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,22 @@ def sign(x: Array, /) -> Array:
return out


def round(x: Array, /, **kwargs) -> Array:
# torch.round fails for complex inputs
# https://github.com/pytorch/pytorch/issues/58743#issuecomment-2727603845
if x.dtype.is_complex:
out = kwargs.pop('out', None)
res_r = torch.round(x.real, **kwargs)
res_i = torch.round(x.imag, **kwargs)
res = res_r + 1j*res_i
if out is not None:
out.copy_(res)
return out
return res
else:
return torch.round(x, **kwargs)


def meshgrid(*arrays: Array, indexing: Literal['xy', 'ij'] = 'xy') -> tuple[Array, ...]:
# torch <= 2.9 emits a UserWarning: "torch.meshgrid: in an upcoming release, it
# will be required to pass the indexing argument."
Expand All @@ -923,7 +939,7 @@ def meshgrid(*arrays: Array, indexing: Literal['xy', 'ij'] = 'xy') -> tuple[Arra
'permute_dims', 'bitwise_invert', 'newaxis', 'conj', 'add',
'atan2', 'bitwise_and', 'bitwise_left_shift', 'bitwise_or',
'bitwise_right_shift', 'bitwise_xor', 'copysign', 'count_nonzero',
'diff', 'divide',
'diff', 'divide', 'round',
'equal', 'floor_divide', 'greater', 'greater_equal', 'hypot',
'less', 'less_equal', 'logaddexp', 'maximum', 'minimum',
'multiply', 'not_equal', 'pow', 'remainder', 'subtract', 'max',
Expand Down
9 changes: 9 additions & 0 deletions tests/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,12 @@ def test_argsort_stable():

t = xp.zeros(50) # should be >16
assert xp.all(xp.argsort(t) == xp.arange(50))


def test_round():
"""Verify the out= argument of xp.round with complex inputs."""
x = torch.as_tensor([1.23456786]*3) + 3.456789j
o = torch.empty(3, dtype=torch.complex64)
r = xp.round(x, decimals=1, out=o)
assert xp.all(r == o)
assert r is o
1 change: 0 additions & 1 deletion torch-xfails.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ array_api_tests/test_statistical_functions.py::test_var


# These functions do not yet support complex numbers
array_api_tests/test_operators_and_elementwise_functions.py::test_round
array_api_tests/test_set_functions.py::test_unique_counts
array_api_tests/test_set_functions.py::test_unique_values

Expand Down
Loading