pytorch/test/torch_np/test_function_base.py
lezcano a9dca53438 NumPy support in torch.compile (#106211)
RFC: https://github.com/pytorch/rfcs/pull/54
First commit is the contents of https://github.com/Quansight-Labs/numpy_pytorch_interop/

We have already been using this in core for the last few months as a external dependency. This PR pulls all these into core.

In the next commits, I do a number of things in this order
- Fix a few small issues
- Make the tests that this PR adds pass
- Bend backwards until lintrunner passes
- Remove the optional dependency on `torch_np` and simply rely on the upstreamed code
- Fix a number dynamo tests that were passing before (they were not tasting anything I think) and are not passing now.

Missing from this PR (but not blocking):
- Have a flag that deactivates tracing NumPy functions and simply breaks. There used to be one but after the merge stopped working and I removed it. @lezcano to investigate.
- https://github.com/pytorch/pytorch/pull/106431#issuecomment-1667079543. @voznesenskym to submit a fix after we merge.

All the tests in `tests/torch_np` take about 75s to run.

This was a work by @ev-br, @rgommers @honno and I. I did not create this PR via ghstack (which would have been convenient) as this is a collaboration, and ghstack doesn't allow for shared contributions.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/106211
Approved by: https://github.com/ezyang
2023-08-11 00:39:32 +00:00

94 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)