data-apis / array-api-compat

Compatibility layer for common array libraries to support the Array API
https://data-apis.org/array-api-compat/
MIT License
69 stars 22 forks source link

BUG: torch.std: UnboundLocalError #116

Closed lucascolley closed 5 months ago

lucascolley commented 5 months ago

Found by @mdhaber in https://github.com/scipy/scipy/pull/20285

In [1]: from array_api_compat import array_namespace

In [2]: import torch

In [3]: x = torch.asarray([1, 2, 3., 3, 4.5])

In [4]: x
Out[4]: tensor([1.0000, 2.0000, 3.0000, 3.0000, 4.5000])

In [5]: xp = array_namespace(x)

In [6]: xp
Out[6]: <module 'array_api_compat.torch' from '/Users/lucascolley/programming/array-api-compat/array_api_compat/torch/__init__.py'>

In [7]: xp.std(x, correction=1)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[7], line 1
----> 1 xp.std(x, correction=1)

File ~/programming/array-api-compat/array_api_compat/torch/_aliases.py:396, in std(x, axis, correction, keepdims, **kwargs)
    392     axis = (axis,)
    393 if axis is None:
    394     # torch doesn't support keepdims with axis=None
    395     # (https://github.com/pytorch/pytorch/issues/71209)
--> 396     res = torch.std(x, tuple(range(x.ndim)), correction=_correction, **kwargs)
    397     res = _axis_none_keepdims(res, x.ndim, keepdims)
    398     return res

UnboundLocalError: cannot access local variable '_correction' where it is not associated with a value
asmeurer commented 5 months ago

Ah, this was missed because float correction raises a NotImplementedError, so the whole test was skipped.