Skip to content

Commit 1237ed4

Browse files
committed
ENH: torch: allow complex arguments to var and std
While at it, float `correction` seems to be supported, at least with torch 2.10?
1 parent 2865123 commit 1237ed4

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

src/array_api_compat/torch/_aliases.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
_promotion_table.update({(b, a): c for (a, b), c in _promotion_table.items()})
6464
_promotion_table.update({(a, a): a for a in _array_api_dtypes})
6565

66+
_real_dtype_for = {torch.complex64: torch.float32, torch.complex128: torch.float64}
67+
6668

6769
def _two_arg(f):
6870
@_wraps(f)
@@ -435,16 +437,18 @@ def std(x: Array,
435437
# https://github.com/pytorch/pytorch/issues/61492. We don't try to
436438
# implement it here for now.
437439

438-
if isinstance(correction, float):
439-
_correction = int(correction)
440-
if correction != _correction:
441-
raise NotImplementedError("float correction in torch std() is not yet supported")
442-
else:
443-
_correction = correction
440+
#if isinstance(correction, float):
441+
# _correction = int(correction)
442+
# if correction != _correction:
443+
# raise NotImplementedError("float correction in torch std() is not yet supported")
444+
#else:
445+
# _correction = correction
446+
_correction = correction
444447

445448
# https://github.com/pytorch/pytorch/issues/29137
446449
if axis == ():
447-
return torch.zeros_like(x)
450+
dtyp = _real_dtype_for[x.dtype] if x.is_complex() else x.dtype
451+
return torch.zeros_like(x, dtype=dtyp)
448452
if isinstance(axis, int):
449453
axis = (axis,)
450454
if axis is None:
@@ -471,7 +475,8 @@ def var(x: Array,
471475

472476
# https://github.com/pytorch/pytorch/issues/29137
473477
if axis == ():
474-
return torch.zeros_like(x)
478+
dtyp = _real_dtype_for[x.dtype] if x.is_complex() else x.dtype
479+
return torch.zeros_like(x, dtype=dtyp)
475480
if isinstance(axis, int):
476481
axis = (axis,)
477482
if axis is None:

0 commit comments

Comments
 (0)