pytorch/test/torch_np/test_function_base.py
Evgeni Burovski 7abeb92796 make sure all torch._numpy tests run on CI (#108762)
- Add `if __name__ == "__main__": run_tests()` stanzas to test files in `torch_np` folder so that these tests run on CI
- Skip / xfail things smoked out by this change
- remove a stray python file which should not have been added to tests in the first place.
- fix einsum if opt_einsum is present
- add skips for older numpies

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108762
Approved by: https://github.com/lezcano
2023-09-09 20:05:27 +00:00

100 lines
3.4 KiB
Python

# Owner(s): ["module: dynamo"]
import pytest
import torch._numpy as np
from pytest import raises as assert_raises
from torch._numpy.testing import assert_array_equal, assert_equal
class TestArange:
def test_infinite(self):
assert_raises(
(RuntimeError, ValueError), np.arange, 0, np.inf
) # "size exceeded",
def test_nan_step(self):
assert_raises(
(RuntimeError, ValueError), np.arange, 0, 1, np.nan
) # "cannot compute length",
def test_zero_step(self):
assert_raises(ZeroDivisionError, np.arange, 0, 10, 0)
assert_raises(ZeroDivisionError, np.arange, 0.0, 10.0, 0.0)
# empty range
assert_raises(ZeroDivisionError, np.arange, 0, 0, 0)
assert_raises(ZeroDivisionError, np.arange, 0.0, 0.0, 0.0)
def test_require_range(self):
assert_raises(TypeError, np.arange)
assert_raises(TypeError, np.arange, step=3)
assert_raises(TypeError, np.arange, dtype="int64")
@pytest.mark.xfail(reason="XXX: arange(start=0, stop, step=1)")
def test_require_range_2(self):
assert_raises(TypeError, np.arange, start=4)
def test_start_stop_kwarg(self):
keyword_stop = np.arange(stop=3)
keyword_zerotostop = np.arange(start=0, stop=3)
keyword_start_stop = np.arange(start=3, stop=9)
assert len(keyword_stop) == 3
assert len(keyword_zerotostop) == 3
assert len(keyword_start_stop) == 6
assert_array_equal(keyword_stop, keyword_zerotostop)
@pytest.mark.xfail(reason="XXX: arange(..., dtype=bool)")
def test_arange_booleans(self):
# Arange makes some sense for booleans and works up to length 2.
# But it is weird since `arange(2, 4, dtype=bool)` works.
# Arguably, much or all of this could be deprecated/removed.
res = np.arange(False, dtype=bool)
assert_array_equal(res, np.array([], dtype="bool"))
res = np.arange(True, dtype="bool")
assert_array_equal(res, [False])
res = np.arange(2, dtype="bool")
assert_array_equal(res, [False, True])
# This case is especially weird, but drops out without special case:
res = np.arange(6, 8, dtype="bool")
assert_array_equal(res, [True, True])
with pytest.raises(TypeError):
np.arange(3, dtype="bool")
@pytest.mark.parametrize("which", [0, 1, 2])
def test_error_paths_and_promotion(self, which):
args = [0, 10, 2] # start, stop, and step
args[which] = np.float64(2.0) # should ensure float64 output
assert np.arange(*args).dtype == np.float64
# Cover stranger error path, test only to achieve code coverage!
args[which] = [None, []]
with pytest.raises((ValueError, RuntimeError)):
# Fails discovering start dtype
np.arange(*args)
class TestAppend:
# tests taken from np.append docstring
def test_basic(self):
result = np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
assert_equal(result, np.arange(1, 10, dtype=int))
# When `axis` is specified, `values` must have the correct shape.
result = np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
assert_equal(result, np.arange(1, 10, dtype=int).reshape((3, 3)))
with pytest.raises((RuntimeError, ValueError)):
np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
if __name__ == "__main__":
from torch._dynamo.test_case import run_tests
run_tests()