library
stringclasses
1 value
test_file
stringclasses
785 values
test_function
stringlengths
1
295
before
stringlengths
0
448k
after
stringlengths
0
487k
context_before
stringclasses
947 values
context_after
stringlengths
0
16.3k
commit_before
stringclasses
1 value
commit_after
stringclasses
1 value
change_type
stringclasses
3 values
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_errors
def test_errors(self): x = np.random.randn(1, 2, 3) assert_raises(np.AxisError, np.moveaxis, x, 3, 0) # 'source.*out of bounds', assert_raises(np.AxisError, np.moveaxis, x, -4, 0) # 'source.*out of bounds', assert_raises( np.AxisError, np.moveaxis, x, 0, 5 # 'destination.*out of bounds', ) assert_raises( ValueError, np.moveaxis, x, [0, 0], [0, 1] # 'repeated axis in `source`', ) assert_raises( ValueError, # 'repeated axis in `destination`', np.moveaxis, x, [0, 1], [1, 1], ) assert_raises( (ValueError, RuntimeError), # 'must have the same number', np.moveaxis, x, 0, [0, 1], ) assert_raises( (ValueError, RuntimeError), # 'must have the same number', np.moveaxis, x, [0, 1], [0], ) x = [1, 2, 3] result = np.moveaxis(x, 0, 0) assert_(x, list(result)) assert_(isinstance(result, np.ndarray))
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestMoveaxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_2x2
def test_2x2(self): u = [1, 2] v = [3, 4] z = -2 cp = np.cross(u, v) assert_equal(cp, z) cp = np.cross(v, u) assert_equal(cp, -z)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestCross(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_2x3
def test_2x3(self): u = [1, 2] v = [3, 4, 5] z = np.array([10, -5, -2]) cp = np.cross(u, v) assert_equal(cp, z) cp = np.cross(v, u) assert_equal(cp, -z)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestCross(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_empty_like
def test_empty_like(self): self.check_like_function(np.empty_like, None)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @skip(reason="implement order etc") # FIXME: make xfail @instantiate_parametrized_tests class TestLikeFuncs(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_dtype_str_bytes
def test_dtype_str_bytes(self, likefunc, dtype): # Regression test for gh-19860 a = np.arange(16).reshape(2, 8) b = a[:, ::2] # Ensure b is not contiguous. kwargs = {"fill_value": ""} if likefunc == np.full_like else {} result = likefunc(b, dtype=dtype, **kwargs) if dtype == str: assert result.strides == (16, 4) else: # dtype is bytes assert result.strides == (4, 1)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @skip(reason="implement order etc") # FIXME: make xfail @instantiate_parametrized_tests class TestLikeFuncs(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
_setup
def _setup(self): atol = self.atol rtol = self.rtol arr = np.array([100, 1000]) aran = np.arange(125).reshape((5, 5, 5)) self.all_close_tests = [ ([1, 0], [1, 0]), ([atol], [0]), ([1], [1 + rtol + atol]), (arr, arr + arr * rtol), (arr, arr + arr * rtol + atol), (aran, aran + aran * rtol), (np.inf, np.inf), (np.inf, [np.inf]), ([np.inf, -np.inf], [np.inf, -np.inf]), ] self.none_close_tests = [ ([np.inf, 0], [1, np.inf]), ([np.inf, -np.inf], [1, 0]), ([np.inf, np.inf], [1, -np.inf]), ([np.inf, np.inf], [1, 0]), ([np.nan, 0], [np.nan, -np.inf]), ([atol * 2], [0]), ([1], [1 + rtol + atol * 2]), (aran, aran + rtol * 1.1 * aran + atol * 1.1), (np.array([np.inf, 1]), np.array([0, np.inf])), ] self.some_close_tests = [ ([np.inf, 0], [np.inf, atol * 2]), ([atol, 1, 1e6 * (1 + 2 * rtol) + atol], [0, np.nan, 1e6]), (np.arange(3), [0, 1, 2.1]), (np.nan, [np.nan, np.nan, np.nan]), ([0], [atol, np.inf, -np.inf, np.nan]), (0, [atol, np.inf, -np.inf, np.nan]), ] self.some_close_results = [ [True, False], [True, False, False], [True, True, False], [False, False, False], [True, False, False, False], [True, False, False, False], ]
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestIsclose(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_float
def test_float(self): # offset for alignment test for i in range(4): assert_array_equal(self.f[i:] > 0, self.ef[i:]) assert_array_equal(self.f[i:] - 1 >= 0, self.ef[i:]) assert_array_equal(self.f[i:] == 0, ~self.ef[i:]) assert_array_equal(-self.f[i:] < 0, self.ef[i:]) assert_array_equal(-self.f[i:] + 1 <= 0, self.ef[i:]) r = self.f[i:] != 0 assert_array_equal(r, self.ef[i:]) r2 = self.f[i:] != np.zeros_like(self.f[i:]) r3 = 0 != self.f[i:] assert_array_equal(r, r2) assert_array_equal(r, r3) # check bool == 0x1 assert_array_equal(r.view(np.int8), r.astype(np.int8)) assert_array_equal(r2.view(np.int8), r2.astype(np.int8)) assert_array_equal(r3.view(np.int8), r3.astype(np.int8)) # isnan on amd64 takes the same code path assert_array_equal(np.isnan(self.nf[i:]), self.ef[i:]) assert_array_equal(np.isfinite(self.nf[i:]), ~self.ef[i:]) assert_array_equal(np.isfinite(self.inff[i:]), ~self.ef[i:]) assert_array_equal(np.isinf(self.inff[i:]), self.efnonan[i:]) assert_array_equal(np.signbit(self.signf[i:]), self.ef[i:])
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xfailIfTorchDynamo class TestBoolCmp(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_no_overwrite
def test_no_overwrite(self): d = np.ones(100) k = np.ones(3) np.correlate(d, k) assert_array_equal(d, np.ones(100)) assert_array_equal(k, np.ones(3))
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestCorrelate(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_complex
def test_complex(self): x = np.array([1, 2, 3, 4 + 1j], dtype=complex) y = np.array([-1, -2j, 3 + 1j], dtype=complex) r_z = np.array([3 - 1j, 6, 8 + 1j, 11 + 5j, -5 + 8j, -4 - 1j], dtype=complex) r_z = np.flip(r_z).conjugate() z = np.correlate(y, x, mode="full") assert_array_almost_equal(z, r_z)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestCorrelate(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_mode
def test_mode(self): d = np.ones(100) k = np.ones(3) default_mode = np.correlate(d, k, mode="valid") with assert_warns(DeprecationWarning): valid_mode = np.correlate(d, k, mode="v") assert_array_equal(valid_mode, default_mode) # integer mode with assert_raises(ValueError): np.correlate(d, k, mode=-1) assert_array_equal(np.correlate(d, k, mode=0), valid_mode) # illegal arguments with assert_raises(TypeError): np.correlate(d, k, mode=None)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestCorrelate(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_object
def test_object(self): d = [1.0] * 100 k = [1.0] * 3 assert_array_almost_equal(np.convolve(d, k)[2:-2], np.full(98, 3))
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestConvolve(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
generate_all_false
def generate_all_false(self, dtype): arr = np.zeros((2, 2), [("junk", "i1"), ("a", dtype)]) arr.setflags(write=False) a = arr["a"] assert_(not a.flags["C"]) assert_(not a.flags["F"]) assert_(not a.flags["O"]) assert_(not a.flags["W"]) assert_(not a.flags["A"]) return a
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestRequire(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
set_and_check_flag
def set_and_check_flag(self, flag, dtype, arr): if dtype is None: dtype = arr.dtype b = np.require(arr, dtype, [flag]) assert_(b.flags[flag]) assert_(b.dtype == dtype) # a further call to np.require ought to return the same array # unless OWNDATA is specified. c = np.require(b, None, [flag]) if flag[0] != "O": assert_(c is b) else: assert_(c.flags[flag])
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestRequire(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_require_each
def test_require_each(self): id = ["f8", "i4"] fd = [None, "f8", "c16"] for idtype, fdtype, flag in itertools.product(id, fd, self.flag_names): a = self.generate_all_false(idtype) self.set_and_check_flag(flag, fdtype, a)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestRequire(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_unknown_requirement
def test_unknown_requirement(self): a = self.generate_all_false("f8") assert_raises(KeyError, np.require, a, None, "Q")
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestRequire(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_non_array_input
def test_non_array_input(self): a = np.require([1, 2, 3, 4], "i4", ["C", "A", "O"]) assert_(a.flags["O"]) assert_(a.flags["C"]) assert_(a.flags["A"]) assert_(a.dtype == "i4") assert_equal(a, [1, 2, 3, 4])
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestRequire(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_C_and_F_simul
def test_C_and_F_simul(self): a = self.generate_all_false("f8") assert_raises(ValueError, np.require, a, None, ["C", "F"])
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestRequire(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_broadcast_in_args
def test_broadcast_in_args(self): # gh-5881 arrs = [ np.empty((6, 7)), np.empty((5, 6, 1)), np.empty((7,)), np.empty((5, 1, 7)), ] mits = [ np.broadcast(*arrs), np.broadcast(np.broadcast(*arrs[:0]), np.broadcast(*arrs[0:])), np.broadcast(np.broadcast(*arrs[:1]), np.broadcast(*arrs[1:])), np.broadcast(np.broadcast(*arrs[:2]), np.broadcast(*arrs[2:])), np.broadcast(arrs[0], np.broadcast(*arrs[1:-1]), arrs[-1]), ] for mit in mits: assert_equal(mit.shape, (5, 6, 7)) assert_equal(mit.ndim, 3) assert_equal(mit.nd, 3) assert_equal(mit.numiter, 4) for a, ia in zip(arrs, mit.iters): assert_(a is ia.base)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestBroadcast(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_broadcast_single_arg
def test_broadcast_single_arg(self): # gh-6899 arrs = [np.empty((5, 6, 7))] mit = np.broadcast(*arrs) assert_equal(mit.shape, (5, 6, 7)) assert_equal(mit.ndim, 3) assert_equal(mit.nd, 3) assert_equal(mit.numiter, 1) assert_(arrs[0] is mit.iters[0].base)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestBroadcast(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_number_of_arguments
def test_number_of_arguments(self): arr = np.empty((5,)) for j in range(35): arrs = [arr] * j if j > 32: assert_raises(ValueError, np.broadcast, *arrs) else: mit = np.broadcast(*arrs) assert_equal(mit.numiter, j)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestBroadcast(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_ctors.py
test_floating_overflow
def test_floating_overflow(self): """Strings containing an unrepresentable float overflow""" fhalf = np.half("1e10000") assert_equal(fhalf, np.inf) fsingle = np.single("1e10000") assert_equal(fsingle, np.inf) fdouble = np.double("1e10000") assert_equal(fdouble, np.inf) fhalf = np.half("-1e10000") assert_equal(fhalf, -np.inf) fsingle = np.single("-1e10000") assert_equal(fsingle, -np.inf) fdouble = np.double("-1e10000") assert_equal(fdouble, -np.inf)
import functools from unittest import skipIf as skipif import pytest from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_almost_equal, assert_equal import torch._numpy as np from torch._numpy.testing import assert_almost_equal, assert_equal skip = functools.partial(skipif, True) class TestFromString(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_ctors.py
test_bool
def test_bool(self): with pytest.raises(TypeError): np.bool_(False, garbage=True)
import functools from unittest import skipIf as skipif import pytest from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_almost_equal, assert_equal import torch._numpy as np from torch._numpy.testing import assert_almost_equal, assert_equal skip = functools.partial(skipif, True) class TestFromString(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_ctors.py
test_intp
def test_intp(self): # Ticket #99 assert_equal(1024, np.intp(1024))
import functools from unittest import skipIf as skipif import pytest from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_almost_equal, assert_equal import torch._numpy as np from torch._numpy.testing import assert_almost_equal, assert_equal skip = functools.partial(skipif, True) class TestFromInt(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_ctors.py
test_uint64_from_negative
def test_uint64_from_negative(self): # NumPy test was asserting a DeprecationWarning assert_equal(np.uint8(-2), np.uint8(254))
import functools from unittest import skipIf as skipif import pytest from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_almost_equal, assert_equal import torch._numpy as np from torch._numpy.testing import assert_almost_equal, assert_equal skip = functools.partial(skipif, True) class TestFromInt(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_ctors.py
test_complex
def test_complex(self, t1, t2): return self._do_test(t1, t2)
import functools from unittest import skipIf as skipif import pytest from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_almost_equal, assert_equal import torch._numpy as np from torch._numpy.testing import assert_almost_equal, assert_equal skip = functools.partial(skipif, True) int_types = [ subtest(np.byte, name="np_byte"), subtest(np.short, name="np_short"), subtest(np.intc, name="np_intc"), subtest(np.int_, name="np_int_"), subtest(np.longlong, name="np_longlong"), ] uint_types = [np.ubyte] float_types = [np.half, np.single, np.double] cfloat_types = [np.csingle, np.cdouble] @instantiate_parametrized_tests class TestArrayFromScalar(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_shape_mismatch_error_message
def test_shape_mismatch_error_message(self): with assert_raises( ValueError, match=r"arg 0 with shape \(1, 3\) and " r"arg 2 with shape \(2,\)", ): np.broadcast([[1, 2, 3]], [[4], [5]], [6, 7])
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number @xpassIfTorchDynamo # (reason="TODO") class TestBroadcast(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_zero_dimension
def test_zero_dimension(self): # Test resolution to issue #5663 a = np.zeros((3, 0)) b = np.zeros((0, 4)) td = np.tensordot(a, b, (1, 0)) assert_array_equal(td, np.dot(a, b))
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestTensordot(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_zero_dimension_einsum
def test_zero_dimension_einsum(self): # Test resolution to issue #5663 a = np.zeros((3, 0)) b = np.zeros((0, 4)) td = np.tensordot(a, b, (1, 0)) assert_array_equal(td, np.einsum("ij,jk", a, b))
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestTensordot(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numeric.py
test_zero_dimensional
def test_zero_dimensional(self): # gh-12130 arr_0d = np.array(1) ret = np.tensordot( arr_0d, arr_0d, ([], []) ) # contracting no axes is well defined assert_array_equal(ret, arr_0d)
import functools import itertools import math import platform import sys import warnings import numpy import pytest IS_WASM = False HAS_REFCOUNT = True import operator from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy.random import rand, randint, randn from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) import torch._numpy as np from torch._numpy.random import rand, randint, randn from torch._numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_warns, # assert_array_max_ulp, HAS_REFCOUNT, IS_WASM ) skip = functools.partial(skipif, True) from fractions import Fraction from numbers import Number class TestTensordot(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_scalar_loses1
def test_scalar_loses1(self): res = np.find_common_type(["f4", "f4", "i2"], ["f8"]) assert_(res == "f4")
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @xpassIfTorchDynamo # ( # reason="We do not disctinguish between scalar and array types." # " Thus, scalars can upcast arrays." # ) class TestCommonType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_scalar_loses2
def test_scalar_loses2(self): res = np.find_common_type(["f4", "f4"], ["i8"]) assert_(res == "f4")
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @xpassIfTorchDynamo # ( # reason="We do not disctinguish between scalar and array types." # " Thus, scalars can upcast arrays." # ) class TestCommonType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_scalar_wins
def test_scalar_wins(self): res = np.find_common_type(["f4", "f4", "i2"], ["c8"]) assert_(res == "c8")
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @xpassIfTorchDynamo # ( # reason="We do not disctinguish between scalar and array types." # " Thus, scalars can upcast arrays." # ) class TestCommonType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_scalar_wins2
def test_scalar_wins2(self): res = np.find_common_type(["u4", "i4", "i4"], ["f4"]) assert_(res == "f8")
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @xpassIfTorchDynamo # ( # reason="We do not disctinguish between scalar and array types." # " Thus, scalars can upcast arrays." # ) class TestCommonType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_scalar_wins3
def test_scalar_wins3(self): # doesn't go up to 'f16' on purpose res = np.find_common_type(["u8", "i8", "i8"], ["f8"]) assert_(res == "f8")
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @xpassIfTorchDynamo # ( # reason="We do not disctinguish between scalar and array types." # " Thus, scalars can upcast arrays." # ) class TestCommonType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_both_abstract
def test_both_abstract(self): assert_(np.issubdtype(np.floating, np.inexact)) assert_(not np.issubdtype(np.inexact, np.floating))
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) class TestIsSubDType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_methods.py
test_errors
def test_errors(self, ftype): assert_raises(OverflowError, ftype("inf").as_integer_ratio) assert_raises(OverflowError, ftype("-inf").as_integer_ratio) assert_raises(ValueError, ftype("nan").as_integer_ratio)
import fractions import functools import sys import types from typing import Any, Type from unittest import skipIf as skipif, SkipTest import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, ) import numpy as np from numpy.testing import assert_equal import torch._numpy as np from torch._numpy.testing import assert_equal skip = functools.partial(skipif, True) @skip(reason="XXX: scalar.as_integer_ratio not implemented") @instantiate_parametrized_tests class TestAsIntegerRatio(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_methods.py
test_roundtrip
def test_roundtrip(self, ftype, frac_vals, exp_vals): for frac, exp in zip(frac_vals, exp_vals): f = np.ldexp(ftype(frac), exp) assert f.dtype == ftype n, d = f.as_integer_ratio() try: nf = np.longdouble(n) df = np.longdouble(d) except (OverflowError, RuntimeWarning): # the values may not fit in any float type raise SkipTest("longdouble too small on this platform") # noqa: B904 assert_equal(nf / df, f, f"{n}/{d}")
import fractions import functools import sys import types from typing import Any, Type from unittest import skipIf as skipif, SkipTest import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, ) import numpy as np from numpy.testing import assert_equal import torch._numpy as np from torch._numpy.testing import assert_equal skip = functools.partial(skipif, True) @skip(reason="XXX: scalar.as_integer_ratio not implemented") @instantiate_parametrized_tests class TestAsIntegerRatio(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalar_methods.py
test_bit_count
def test_bit_count(self): for exp in [10, 17, 63]: a = 2**exp assert np.uint64(a).bit_count() == 1 assert np.uint64(a - 1).bit_count() == exp assert np.uint64(a ^ 63).bit_count() == 7 assert np.uint64((a - 1) ^ 510).bit_count() == exp - 8
import fractions import functools import sys import types from typing import Any, Type from unittest import skipIf as skipif, SkipTest import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, ) import numpy as np from numpy.testing import assert_equal import torch._numpy as np from torch._numpy.testing import assert_equal skip = functools.partial(skipif, True) @skip(reason="scalartype(...).bit_count() not implemented") @instantiate_parametrized_tests class TestBitCount(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarinherit.py
__new__
def __new__(cls, *args, **kwargs): return cls, args, kwargs
import functools from unittest import skipIf as skipif import pytest import torch._numpy as np from torch._numpy.testing import assert_ from torch.testing._internal.common_utils import run_tests, TestCase skip = functools.partial(skipif, True) class HasNew:
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarinherit.py
test_init
def test_init(self): x = B(1.0) assert_(str(x) == "1.0") y = C(2.0) assert_(str(y) == "2.0") z = D(3.0) assert_(str(z) == "3.0")
import functools from unittest import skipIf as skipif import pytest import torch._numpy as np from torch._numpy.testing import assert_ from torch.testing._internal.common_utils import run_tests, TestCase skip = functools.partial(skipif, True) @skip(reason="scalar repr: numpy plans to make it more explicit") class TestInherit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarinherit.py
test_init2
def test_init2(self): x = B0(1.0) assert_(str(x) == "1.0") y = C0(2.0) assert_(str(y) == "2.0")
import functools from unittest import skipIf as skipif import pytest import torch._numpy as np from torch._numpy.testing import assert_ from torch.testing._internal.common_utils import run_tests, TestCase skip = functools.partial(skipif, True) @skip(reason="scalar repr: numpy plans to make it more explicit") class TestInherit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_same
def test_same(self): for cls in (np.float32, np.int32): for w1, w2 in itertools.product(self.wrappers, repeat=2): assert_(np.issubdtype(w1(cls), w2(cls)))
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) class TestIsSubDType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_subclass
def test_subclass(self): # note we cannot promote floating to a dtype, as it would turn into a # concrete type for w in self.wrappers: assert_(np.issubdtype(w(np.float32), np.floating)) assert_(np.issubdtype(w(np.float64), np.floating))
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) class TestIsSubDType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_subclass_backwards
def test_subclass_backwards(self): for w in self.wrappers: assert_(not np.issubdtype(np.floating, w(np.float32))) assert_(not np.issubdtype(np.floating, w(np.float64)))
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) class TestIsSubDType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_sibling_class
def test_sibling_class(self): for w1, w2 in itertools.product(self.wrappers, repeat=2): assert_(not np.issubdtype(w1(np.float32), w2(np.float64))) assert_(not np.issubdtype(w1(np.float64), w2(np.float32)))
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) class TestIsSubDType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_nondtype_nonscalartype
def test_nondtype_nonscalartype(self): # See gh-14619 and gh-9505 which introduced the deprecation to fix # this. These tests are directly taken from gh-9505 assert not np.issubdtype(np.float32, "float64") assert not np.issubdtype(np.float32, "f8") assert not np.issubdtype(np.int32, "int64") # for the following the correct spellings are # np.integer, np.floating, or np.complexfloating respectively: assert not np.issubdtype(np.int8, int) # np.int8 is never np.int_ assert not np.issubdtype(np.float32, float) assert not np.issubdtype(np.complex64, complex) assert not np.issubdtype(np.float32, "float") assert not np.issubdtype(np.float64, "f") # Test the same for the correct first datatype and abstract one # in the case of int, float, complex: assert np.issubdtype(np.float64, "float64") assert np.issubdtype(np.float64, "f8") assert np.issubdtype(np.int64, "int64") assert np.issubdtype(np.int8, np.integer) assert np.issubdtype(np.float32, np.floating) assert np.issubdtype(np.complex64, np.complexfloating) assert np.issubdtype(np.float64, "float") assert np.issubdtype(np.float32, "f")
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) class TestIsSubDType(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_abstract
def test_abstract(self): assert_raises(ValueError, np.core.numerictypes.bitname, np.floating)
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @xpassIfTorchDynamo # ( # reason="We do not have (or need) np.core.numerictypes." # " Our type aliases are in _dtypes.py." # ) class TestBitName(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_platform_dependent_aliases
def test_platform_dependent_aliases(self): if np.int64 is np.int_: assert_("int64" in np.int_.__doc__) elif np.int64 is np.longlong: assert_("int64" in np.longlong.__doc__)
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @skip(reason="Docstrings for scalar types, not yet.") @skipif( sys.flags.optimize > 1, reason="no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1", ) class TestDocStrings(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_numerictypes.py
test_names_are_undersood_by_dtype
def test_names_are_undersood_by_dtype(self, t): """Test the dtype constructor maps names back to the type""" assert np.dtype(t.__name__).type is t
import functools import itertools import sys from unittest import skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import assert_ import torch._numpy as np from torch._numpy.testing import assert_ skip = functools.partial(skipif, True) @instantiate_parametrized_tests class TestScalarTypeNames(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarinherit.py
test_gh_15395
def test_gh_15395(self): # HasNew is the second base, so `np.float64` should have priority x = B1(1.0) assert_(str(x) == "1.0") # previously caused RecursionError!? with pytest.raises(TypeError): B1(1.0, 2.0)
import functools from unittest import skipIf as skipif import pytest import torch._numpy as np from torch._numpy.testing import assert_ from torch.testing._internal.common_utils import run_tests, TestCase skip = functools.partial(skipif, True) @skip(reason="scalar repr: numpy plans to make it more explicit") class TestInherit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_types
def test_types(self): for atype in types: a = atype(1) assert_(a == 1, f"error with {atype!r}: got {a!r}")
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestTypes(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_type_add
def test_type_add(self): # list of types for k, atype in enumerate(types): a_scalar = atype(3) a_array = np.array([3], dtype=atype) for l, btype in enumerate(types): b_scalar = btype(1) b_array = np.array([1], dtype=btype) c_scalar = a_scalar + b_scalar c_array = a_array + b_array # It was comparing the type numbers, but the new ufunc # function-finding mechanism finds the lowest function # to which both inputs can be cast - which produces 'l' # when you do 'q' + 'b'. The old function finding mechanism # skipped ahead based on the first argument, but that # does not produce properly symmetric results... assert_equal( c_scalar.dtype, c_array.dtype, "error with types (%d/'%s' + %d/'%s')" % (k, np.dtype(atype).name, l, np.dtype(btype).name), )
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestTypes(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_leak
def test_leak(self): # test leak of scalar objects # a leak would show up in valgrind as still-reachable of ~2.6MB for i in range(200000): np.add(1, 1)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestTypes(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_lower_align
def test_lower_align(self): # check data that is not aligned to element size # i.e doubles are aligned to 4 bytes on i386 d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64) o = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64) assert_almost_equal(d + d, d * 2) np.add(d, d, out=o) np.add(np.ones_like(d), d, out=o) np.add(d, np.ones_like(d), out=o) np.add(np.ones_like(d), d) np.add(d, np.ones_like(d))
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestBaseMath(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_small_types
def test_small_types(self): for t in [np.int8, np.int16, np.float16]: a = t(3) b = a**4 assert_(b == 81, f"error with {t!r}: got {b!r}")
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestPower(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_integers_to_negative_integer_power
def test_integers_to_negative_integer_power(self): # Note that the combination of uint64 with a signed integer # has common type np.float64. The other combinations should all # raise a ValueError for integer ** negative integer. exp = [np.array(-1, dt)[()] for dt in "bhil"] # 1 ** -1 possible special case base = [np.array(1, dt)[()] for dt in "bhilB"] for i1, i2 in itertools.product(base, exp): if i1.dtype != np.uint64: assert_raises(ValueError, operator.pow, i1, i2) else: res = operator.pow(i1, i2) assert_(res.dtype.type is np.float64) assert_almost_equal(res, 1.0) # -1 ** -1 possible special case base = [np.array(-1, dt)[()] for dt in "bhil"] for i1, i2 in itertools.product(base, exp): if i1.dtype != np.uint64: assert_raises(ValueError, operator.pow, i1, i2) else: res = operator.pow(i1, i2) assert_(res.dtype.type is np.float64) assert_almost_equal(res, -1.0) # 2 ** -1 perhaps generic base = [np.array(2, dt)[()] for dt in "bhilB"] for i1, i2 in itertools.product(base, exp): if i1.dtype != np.uint64: assert_raises(ValueError, operator.pow, i1, i2) else: res = operator.pow(i1, i2) assert_(res.dtype.type is np.float64) assert_almost_equal(res, 0.5)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestPower(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_branches
def test_branches(self): for t in [np.complex64, np.complex128]: # tupled (numerator, denominator, expected) # for testing as expected == numerator/denominator data = [] # trigger branch: real(fabs(denom)) > imag(fabs(denom)) # followed by else condition as neither are == 0 data.append(((2.0, 1.0), (2.0, 1.0), (1.0, 0.0))) # trigger branch: real(fabs(denom)) > imag(fabs(denom)) # followed by if condition as both are == 0 # is performed in test_zero_division(), so this is skipped # trigger else if branch: real(fabs(denom)) < imag(fabs(denom)) data.append(((1.0, 2.0), (1.0, 2.0), (1.0, 0.0))) for cases in data: n = cases[0] d = cases[1] ex = cases[2] result = t(complex(n[0], n[1])) / t(complex(d[0], d[1])) # check real and imag parts separately to avoid comparison # in array context, which does not account for signed zeros assert_equal(result.real, ex[0]) assert_equal(result.imag, ex[1])
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestComplexDivision(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_iinfo_long_values_1
def test_iinfo_long_values_1(self): for code in "bBh": with pytest.warns(DeprecationWarning): res = np.array(np.iinfo(code).max + 1, dtype=code) tgt = np.iinfo(code).min assert_(res == tgt)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestConversion(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_iinfo_long_values_2
def test_iinfo_long_values_2(self): for code in np.typecodes["AllInteger"]: res = np.array(np.iinfo(code).max, dtype=code) tgt = np.iinfo(code).max assert_(res == tgt) for code in np.typecodes["AllInteger"]: res = np.dtype(code).type(np.iinfo(code).max) tgt = np.iinfo(code).max assert_(res == tgt)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestConversion(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
overflow_error_func
def overflow_error_func(dtype): dtype(np.iinfo(dtype).max + 1) for code in [np.int_, np.longlong]: assert_raises((OverflowError, RuntimeError), overflow_error_func, code)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_numpy_scalar_relational_operators
def test_numpy_scalar_relational_operators(self): # All integer for dt1 in np.typecodes["AllInteger"]: assert_(1 > np.array(0, dtype=dt1)[()], f"type {dt1} failed") assert_(not 1 < np.array(0, dtype=dt1)[()], f"type {dt1} failed") for dt2 in np.typecodes["AllInteger"]: assert_( np.array(1, dtype=dt1)[()] > np.array(0, dtype=dt2)[()], f"type {dt1} and {dt2} failed", ) assert_( not np.array(1, dtype=dt1)[()] < np.array(0, dtype=dt2)[()], f"type {dt1} and {dt2} failed", ) # Signed integers and floats for dt1 in "bhl" + np.typecodes["Float"]: assert_(1 > np.array(-1, dtype=dt1)[()], f"type {dt1} failed") assert_(not 1 < np.array(-1, dtype=dt1)[()], f"type {dt1} failed") assert_(-1 == np.array(-1, dtype=dt1)[()], f"type {dt1} failed") for dt2 in "bhl" + np.typecodes["Float"]: assert_( np.array(1, dtype=dt1)[()] > np.array(-1, dtype=dt2)[()], f"type {dt1} and {dt2} failed", ) assert_( not np.array(1, dtype=dt1)[()] < np.array(-1, dtype=dt2)[()], f"type {dt1} and {dt2} failed", ) assert_( np.array(-1, dtype=dt1)[()] == np.array(-1, dtype=dt2)[()], f"type {dt1} and {dt2} failed", )
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestConversion(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_numpy_scalar_relational_operators_2
def test_numpy_scalar_relational_operators_2(self): # Unsigned integers for dt1 in "B": assert_(-1 < np.array(1, dtype=dt1)[()], f"type {dt1} failed") assert_(not -1 > np.array(1, dtype=dt1)[()], f"type {dt1} failed") assert_(-1 != np.array(1, dtype=dt1)[()], f"type {dt1} failed") # unsigned vs signed for dt2 in "bhil": assert_( np.array(1, dtype=dt1)[()] > np.array(-1, dtype=dt2)[()], f"type {dt1} and {dt2} failed", ) assert_( not np.array(1, dtype=dt1)[()] < np.array(-1, dtype=dt2)[()], f"type {dt1} and {dt2} failed", ) assert_( np.array(1, dtype=dt1)[()] != np.array(-1, dtype=dt2)[()], f"type {dt1} and {dt2} failed", )
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestConversion(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_scalar_comparison_to_none
def test_scalar_comparison_to_none(self): # Scalars should just return False and not give a warnings. # The comparisons are flagged by pep8, ignore that. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always", "", FutureWarning) assert_(np.float32(1) is not None) assert_(np.float32(1) is not None) assert_(len(w) == 0) # class TestRepr: # def test_repr(self): # for t in types: # val = t(1197346475.0137341) # val_repr = repr(val) # val2 = eval(val_repr) # assert_equal( val, val2 )
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestConversion(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
_test_type_repr
def _test_type_repr(self, t): finfo = np.finfo(t) last_fraction_bit_idx = finfo.nexp + finfo.nmant last_exponent_bit_idx = finfo.nexp storage_bytes = np.dtype(t).itemsize * 8 # could add some more types to the list below for which in ["small denorm", "small norm"]: # Values from https://en.wikipedia.org/wiki/IEEE_754 constr = np.array([0x00] * storage_bytes, dtype=np.uint8) if which == "small denorm": byte = last_fraction_bit_idx // 8 bytebit = 7 - (last_fraction_bit_idx % 8) constr[byte] = 1 << bytebit elif which == "small norm": byte = last_exponent_bit_idx // 8 bytebit = 7 - (last_exponent_bit_idx % 8) constr[byte] = 1 << bytebit else: raise ValueError("hmm") val = constr.view(t)[0] val_repr = repr(val) val2 = t(eval(val_repr)) if not (val2 == 0 and val < 1e-100): assert_equal(val, val2)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @xpassIfTorchDynamo # (reason="can delegate repr to pytorch") class TestRepr(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_float_repr
def test_float_repr(self): # long double test cannot work, because eval goes through a python # float for t in [np.float32, np.float64]: self._test_type_repr(t)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @xpassIfTorchDynamo # (reason="can delegate repr to pytorch") class TestRepr(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_mixed_types
def test_mixed_types(self): typelist = [ np.int8, np.int16, np.float16, np.float32, np.float64, np.int8, np.int16, np.int32, np.int64, ] for t1 in typelist: for t2 in typelist: a = t1(3) b = t2(2) result = a**b msg = f"error with {t1!r} and {t2!r}:" f"got {result!r}, expected {9!r}" if np.issubdtype(np.dtype(result), np.integer): assert_(result == 9, msg) else: assert_almost_equal(result, 9, err_msg=msg)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestPower(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_modular_power
def test_modular_power(self): # modular power is not implemented, so ensure it errors a = 5 b = 4 c = 10 expected = pow(a, b, c) # noqa: F841 for t in (np.int32, np.float32, np.complex64): # note that 3-operand power only dispatches on the first argument assert_raises(TypeError, operator.pow, t(a), b, c) assert_raises(TypeError, operator.pow, np.array(t(a)), b, c)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestPower(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
floordiv_and_mod
def floordiv_and_mod(x, y): return (x // y, x % y)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_float_modulus_exact
def test_float_modulus_exact(self): # test that float results are exact for small integers. This also # holds for the same integers scaled by powers of two. nlst = list(range(-127, 0)) plst = list(range(1, 128)) dividend = nlst + [0] + plst divisor = nlst + plst arg = list(itertools.product(dividend, divisor)) tgt = [divmod(*t) for t in arg] a, b = np.array(arg, dtype=int).T # convert exact integer results from Python to float so that # signed zero can be used, it is checked. tgtdiv, tgtrem = np.array(tgt, dtype=float).T tgtdiv = np.where((tgtdiv == 0.0) & ((b < 0) ^ (a < 0)), -0.0, tgtdiv) tgtrem = np.where((tgtrem == 0.0) & (b < 0), -0.0, tgtrem) for op in [floordiv_and_mod, divmod]: for dt in np.typecodes["Float"]: msg = f"op: {op.__name__}, dtype: {dt}" fa = a.astype(dt) fb = b.astype(dt) # use list comprehension so a_ and b_ are scalars div, rem = zip(*[op(a_, b_) for a_, b_ in zip(fa, fb)]) assert_equal(div, tgtdiv, err_msg=msg) assert_equal(rem, tgtrem, err_msg=msg)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @instantiate_parametrized_tests class TestModulus(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_float_modulus_corner_cases
def test_float_modulus_corner_cases(self, dt): if dt == "e": # FIXME: make xfail raise SkipTest("RuntimeError: 'nextafter_cpu' not implemented for 'Half'") b = np.array(1.0, dtype=dt) a = np.nextafter(np.array(0.0, dtype=dt), -b) rem = operator.mod(a, b) assert_(rem <= b, f"dt: {dt}") rem = operator.mod(-a, -b) assert_(rem >= -b, f"dt: {dt}") # Check nans, inf # with suppress_warnings() as sup: # sup.filter(RuntimeWarning, "invalid value encountered in remainder") # sup.filter(RuntimeWarning, "divide by zero encountered in remainder") # sup.filter(RuntimeWarning, "divide by zero encountered in floor_divide") # sup.filter(RuntimeWarning, "divide by zero encountered in divmod") # sup.filter(RuntimeWarning, "invalid value encountered in divmod") for dt in "efd": fone = np.array(1.0, dtype=dt) fzer = np.array(0.0, dtype=dt) finf = np.array(np.inf, dtype=dt) fnan = np.array(np.nan, dtype=dt) rem = operator.mod(fone, fzer) assert_(np.isnan(rem), f"dt: {dt}") # MSVC 2008 returns NaN here, so disable the check. # rem = operator.mod(fone, finf) # assert_(rem == fone, 'dt: %s' % dt) rem = operator.mod(fone, fnan) assert_(np.isnan(rem), f"dt: {dt}") rem = operator.mod(finf, fone) assert_(np.isnan(rem), f"dt: {dt}") for op in [floordiv_and_mod, divmod]: div, mod = op(fone, fzer) assert_(np.isinf(div)) and assert_(np.isnan(mod))
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @instantiate_parametrized_tests class TestModulus(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_zero_division
def test_zero_division(self): for t in [np.complex64, np.complex128]: a = t(0.0) b = t(1.0) assert_(np.isinf(b / a)) b = t(complex(np.inf, np.inf)) assert_(np.isinf(b / a)) b = t(complex(np.inf, np.nan)) assert_(np.isinf(b / a)) b = t(complex(np.nan, np.inf)) assert_(np.isinf(b / a)) b = t(complex(np.nan, np.nan)) assert_(np.isnan(b / a)) b = t(0.0) assert_(np.isnan(b / a))
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestComplexDivision(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_signed_zeros
def test_signed_zeros(self): for t in [np.complex64, np.complex128]: # tupled (numerator, denominator, expected) # for testing as expected == numerator/denominator data = ( ((0.0, -1.0), (0.0, 1.0), (-1.0, -0.0)), ((0.0, -1.0), (0.0, -1.0), (1.0, -0.0)), ((0.0, -1.0), (-0.0, -1.0), (1.0, 0.0)), ((0.0, -1.0), (-0.0, 1.0), (-1.0, 0.0)), ((0.0, 1.0), (0.0, -1.0), (-1.0, 0.0)), ((0.0, -1.0), (0.0, -1.0), (1.0, -0.0)), ((-0.0, -1.0), (0.0, -1.0), (1.0, -0.0)), ((-0.0, 1.0), (0.0, -1.0), (-1.0, -0.0)), ) for cases in data: n = cases[0] d = cases[1] ex = cases[2] result = t(complex(n[0], n[1])) / t(complex(d[0], d[1])) # check real and imag parts separately to avoid comparison # in array context, which does not account for signed zeros assert_equal(result.real, ex[0]) assert_equal(result.imag, ex[1])
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestComplexDivision(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
op_func
def op_func(self, other): return __op__
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] ops_with_names = [ ("__lt__", "__gt__", operator.lt, True), ("__le__", "__ge__", operator.le, True), ("__eq__", "__eq__", operator.eq, True), # Note __op__ and __rop__ may be identical here: ("__ne__", "__ne__", operator.ne, True), ("__gt__", "__lt__", operator.gt, True), ("__ge__", "__le__", operator.ge, True), ("__floordiv__", "__rfloordiv__", operator.floordiv, False), ("__truediv__", "__rtruediv__", operator.truediv, False), ("__add__", "__radd__", operator.add, False), ("__mod__", "__rmod__", operator.mod, False), ("__mul__", "__rmul__", operator.mul, False), ("__pow__", "__rpow__", operator.pow, False), ("__sub__", "__rsub__", operator.sub, False), ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
rop_func
def rop_func(self, other): return __rop__ myf_op = type("myf_op", (sctype,), {__op__: op_func, __rop__: rop_func}) # inheritance has to override, or this is correctly lost: res = op(myf_simple1(1), myf_simple2(2)) assert type(res) == sctype or type(res) == np.bool_ assert op(myf_simple1(1), myf_simple2(2)) == op(1, 2) # inherited # Two independent subclasses do not really define an order. This could # be attempted, but we do not since Python's `int` does neither: assert op(myf_op(1), myf_simple1(2)) == __op__ assert op(myf_simple1(1), myf_op(2)) == op(1, 2) # inherited
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] ops_with_names = [ ("__lt__", "__gt__", operator.lt, True), ("__le__", "__ge__", operator.le, True), ("__eq__", "__eq__", operator.eq, True), # Note __op__ and __rop__ may be identical here: ("__ne__", "__ne__", operator.ne, True), ("__gt__", "__lt__", operator.gt, True), ("__ge__", "__le__", operator.ge, True), ("__floordiv__", "__rfloordiv__", operator.floordiv, False), ("__truediv__", "__rtruediv__", operator.truediv, False), ("__add__", "__radd__", operator.add, False), ("__mod__", "__rmod__", operator.mod, False), ("__mul__", "__rmul__", operator.mul, False), ("__pow__", "__rpow__", operator.pow, False), ("__sub__", "__rsub__", operator.sub, False), ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
op_func
def op_func(self, other): return __op__
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] ops_with_names = [ ("__lt__", "__gt__", operator.lt, True), ("__le__", "__ge__", operator.le, True), ("__eq__", "__eq__", operator.eq, True), # Note __op__ and __rop__ may be identical here: ("__ne__", "__ne__", operator.ne, True), ("__gt__", "__lt__", operator.gt, True), ("__ge__", "__le__", operator.ge, True), ("__floordiv__", "__rfloordiv__", operator.floordiv, False), ("__truediv__", "__rtruediv__", operator.truediv, False), ("__add__", "__radd__", operator.add, False), ("__mod__", "__rmod__", operator.mod, False), ("__mul__", "__rmul__", operator.mul, False), ("__pow__", "__rpow__", operator.pow, False), ("__sub__", "__rsub__", operator.sub, False), ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
rop_func
def rop_func(self, other): return __rop__ myf_op = type("myf_op", (sctype,), {__op__: op_func, __rop__: rop_func}) # inheritance has to override, or this is correctly lost: res = op(myf_simple1(1), myf_simple2(2)) assert type(res) == sctype or type(res) == np.bool_ assert op(myf_simple1(1), myf_simple2(2)) == op(1, 2) # inherited # Two independent subclasses do not really define an order. This could # be attempted, but we do not since Python's `int` does neither: assert op(myf_op(1), myf_simple1(2)) == __op__ assert op(myf_simple1(1), myf_op(2)) == op(1, 2) # inherited
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] ops_with_names = [ ("__lt__", "__gt__", operator.lt, True), ("__le__", "__ge__", operator.le, True), ("__eq__", "__eq__", operator.eq, True), # Note __op__ and __rop__ may be identical here: ("__ne__", "__ne__", operator.ne, True), ("__gt__", "__lt__", operator.gt, True), ("__ge__", "__le__", operator.ge, True), ("__floordiv__", "__rfloordiv__", operator.floordiv, False), ("__truediv__", "__rtruediv__", operator.truediv, False), ("__add__", "__radd__", operator.add, False), ("__mod__", "__rmod__", operator.mod, False), ("__mul__", "__rmul__", operator.mul, False), ("__pow__", "__rpow__", operator.pow, False), ("__sub__", "__rsub__", operator.sub, False), ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_0D_array
def test_0D_array(self): a = array(1) b = array(2) res = [atleast_1d(a), atleast_1d(b)] desired = [array([1]), array([2])] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_1D_array
def test_1D_array(self): a = array([1, 2]) b = array([2, 3]) res = [atleast_1d(a), atleast_1d(b)] desired = [array([1, 2]), array([2, 3])] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_2D_array
def test_2D_array(self): a = array([[1, 2], [1, 2]]) b = array([[2, 3], [2, 3]]) res = [atleast_1d(a), atleast_1d(b)] desired = [a, b] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_3D_array
def test_3D_array(self): a = array([[1, 2], [1, 2]]) b = array([[2, 3], [2, 3]]) a = array([a, a]) b = array([b, b]) res = [atleast_1d(a), atleast_1d(b)] desired = [a, b] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_r1array
def test_r1array(self): """Test to make sure equivalent Travis O's r1array function""" assert atleast_1d(3).shape == (1,) assert atleast_1d(3j).shape == (1,) assert atleast_1d(3.0).shape == (1,) assert atleast_1d([[2, 3], [4, 5]]).shape == (2, 2)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_numpy_abs
def test_numpy_abs(self, dtype): self._test_abs_func(np.abs, dtype)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @instantiate_parametrized_tests class TestAbs(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_shift_all_bits
def test_shift_all_bits(self, type_code, op): """Shifts where the shift amount is the width of the type or wider""" # gh-2449 dt = np.dtype(type_code) nbits = dt.itemsize * 8 if dt in (np.dtype(np.uint64), np.dtype(np.uint32), np.dtype(np.uint16)): raise SkipTest("NYI: bitshift uint64") for val in [5, -5]: for shift in [nbits, nbits + 4]: val_scl = np.array(val).astype(dt)[()] shift_scl = dt.type(shift) res_scl = op(val_scl, shift_scl) if val_scl < 0 and op is operator.rshift: # sign bit is preserved assert_equal(res_scl, -1) else: if type_code in ("i", "l") and shift == np.iinfo(type_code).bits: # FIXME: make xfail raise SkipTest( "https://github.com/pytorch/pytorch/issues/70904" ) assert_equal(res_scl, 0) # Result on scalars should be the same as on arrays val_arr = np.array([val_scl] * 32, dtype=dt) shift_arr = np.array([shift] * 32, dtype=dt) res_arr = op(val_arr, shift_arr) assert_equal(res_arr, res_scl)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @instantiate_parametrized_tests class TestBitShifts(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_complex_hashes
def test_complex_hashes(self, type_code): # Test some complex valued hashes specifically: scalar = np.dtype(type_code).type for val in [np.pi + 1j, np.inf - 3j, 3j, 6.0 + 1j]: numpy_val = scalar(val) assert hash(complex(numpy_val)) == hash(numpy_val)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @skip(reason="Will rely on pytest for hashing") @instantiate_parametrized_tests class TestHash(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
recursionlimit
def recursionlimit(n): o = sys.getrecursionlimit() try: sys.setrecursionlimit(n) yield finally: sys.setrecursionlimit(o) @instantiate_parametrized_tests class TestScalarOpsMisc(TestCase): @xfail # (reason="pytorch does not warn on overflow") @parametrize("dtype", "Bbhil") @parametrize( "operation", [ lambda min, max: max + max, lambda min, max: min - max, lambda min, max: max * max, ], ) def test_scalar_integer_operation_overflow(self, dtype, operation): st = np.dtype(dtype).type min = st(np.iinfo(dtype).min) max = st(np.iinfo(dtype).max) with pytest.warns(RuntimeWarning, match="overflow encountered"): operation(min, max) @skip(reason="integer overflow UB: crashes pytorch under ASAN") @parametrize("dtype", "bhil") @parametrize( "operation", [ lambda min, neg_1: -min, lambda min, neg_1: abs(min), lambda min, neg_1: min * neg_1, subtest( lambda min, neg_1: min // neg_1, decorators=[skip(reason="broken on some platforms")], ), ], ) def test_scalar_signed_integer_overflow(self, dtype, operation): # The minimum signed integer can "overflow" for some additional operations st = np.dtype(dtype).type min = st(np.iinfo(dtype).min) neg_1 = st(-1) with pytest.warns(RuntimeWarning, match="overflow encountered"): operation(min, neg_1) @skipif(numpy.__version__ < "1.24", reason="NP_VER: fails on NumPy 1.23.x") @xpassIfTorchDynamo # (reason="pytorch does not warn on overflow") @parametrize("dtype", "B") def test_scalar_unsigned_integer_overflow(self, dtype): val = np.dtype(dtype).type(8) with pytest.warns(RuntimeWarning, match="overflow encountered"): -val zero = np.dtype(dtype).type(0) -zero # does not warn @xfail # (reason="pytorch raises RuntimeError on division by zero") @parametrize("dtype", np.typecodes["AllInteger"]) @parametrize( "operation", [ lambda val, zero: val // zero, lambda val, zero: val % zero, ], ) def test_scalar_integer_operation_divbyzero(self, dtype, operation): st = np.dtype(dtype).type val = st(100) zero = st(0) with pytest.warns(RuntimeWarning, match="divide by zero"): operation(val, zero) ops_with_names = [ ("__lt__", "__gt__", operator.lt, True), ("__le__", "__ge__", operator.le, True), ("__eq__", "__eq__", operator.eq, True), # Note __op__ and __rop__ may be identical here: ("__ne__", "__ne__", operator.ne, True), ("__gt__", "__lt__", operator.gt, True), ("__ge__", "__le__", operator.ge, True), ("__floordiv__", "__rfloordiv__", operator.floordiv, False), ("__truediv__", "__rtruediv__", operator.truediv, False), ("__add__", "__radd__", operator.add, False), ("__mod__", "__rmod__", operator.mod, False), ("__mul__", "__rmul__", operator.mul, False), ("__pow__", "__rpow__", operator.pow, False), ("__sub__", "__rsub__", operator.sub, False), ] @instantiate_parametrized_tests class TestScalarSubclassingMisc(TestCase): @skip(reason="We do not support subclassing scalars.") @parametrize("__op__, __rop__, op, cmp", ops_with_names) @parametrize("sctype", [np.float32, np.float64]) def test_subclass_deferral(self, sctype, __op__, __rop__, op, cmp): """ This test covers scalar subclass deferral. Note that this is exceedingly complicated, especially since it tends to fall back to the array paths and these additionally add the "array priority" mechanism. The behaviour was modified subtly in 1.22 (to make it closer to how Python scalars work). Due to its complexity and the fact that subclassing NumPy scalars is probably a bad idea to begin with. There is probably room for adjustments here. """ class myf_simple1(sctype): pass class myf_simple2(sctype): pass def op_func(self, other): return __op__ def rop_func(self, other): return __rop__ myf_op = type("myf_op", (sctype,), {__op__: op_func, __rop__: rop_func}) # inheritance has to override, or this is correctly lost: res = op(myf_simple1(1), myf_simple2(2)) assert type(res) == sctype or type(res) == np.bool_ assert op(myf_simple1(1), myf_simple2(2)) == op(1, 2) # inherited # Two independent subclasses do not really define an order. This could # be attempted, but we do not since Python's `int` does neither: assert op(myf_op(1), myf_simple1(2)) == __op__ assert op(myf_simple1(1), myf_op(2)) == op(1, 2) # inherited @skip(reason="We do not support subclassing scalars.") @parametrize("__op__, __rop__, op, cmp", ops_with_names) @parametrize("subtype", [float, int, complex, np.float16]) # @np._no_nep50_warning() def test_pyscalar_subclasses(self, subtype, __op__, __rop__, op, cmp): def op_func(self, other): return __op__ def rop_func(self, other): return __rop__ # Check that deferring is indicated using `__array_ufunc__`: myt = type( "myt", (subtype,), {__op__: op_func, __rop__: rop_func, "__array_ufunc__": None}, ) # Just like normally, we should never presume we can modify the float. assert op(myt(1), np.float64(2)) == __op__ assert op(np.float64(1), myt(2)) == __rop__ if op in {operator.mod, operator.floordiv} and subtype == complex: return # module is not support for complex. Do not test. if __rop__ == __op__: return # When no deferring is indicated, subclasses are handled normally. myt = type("myt", (subtype,), {__rop__: rop_func}) # Check for float32, as a float subclass float64 may behave differently res = op(myt(1), np.float16(2)) expected = op(subtype(1), np.float16(2)) assert res == expected assert type(res) == type(expected) res = op(np.float32(2), myt(1)) expected = op(np.float32(2), subtype(1)) assert res == expected assert type(res) == type(expected) if __name__ == "__main__": run_tests()
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ]
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_scalar_integer_operation_divbyzero
def test_scalar_integer_operation_divbyzero(self, dtype, operation): st = np.dtype(dtype).type val = st(100) zero = st(0) with pytest.warns(RuntimeWarning, match="divide by zero"): operation(val, zero)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @instantiate_parametrized_tests class TestScalarOpsMisc(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_seq_repeat
def test_seq_repeat(self): # Test that basic sequences get repeated when multiplied with # numpy integers. And errors are raised when multiplied with others. # Some of this behaviour may be controversial and could be open for # change. accepted_types = set(np.typecodes["AllInteger"]) deprecated_types = {"?"} forbidden_types = set(np.typecodes["All"]) - accepted_types - deprecated_types forbidden_types -= {"V"} # can't default-construct void scalars for seq_type in (list, tuple): seq = seq_type([1, 2, 3]) for numpy_type in accepted_types: i = np.dtype(numpy_type).type(2) assert_equal(seq * i, seq * int(i)) assert_equal(i * seq, int(i) * seq) for numpy_type in deprecated_types: i = np.dtype(numpy_type).type() assert_equal( assert_warns(DeprecationWarning, operator.mul, seq, i), seq * int(i) ) assert_equal( assert_warns(DeprecationWarning, operator.mul, i, seq), int(i) * seq ) for numpy_type in forbidden_types: i = np.dtype(numpy_type).type() assert_raises(TypeError, operator.mul, seq, i) assert_raises(TypeError, operator.mul, i, seq)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @skip(reason="Array scalars do not decay to python scalars.") class TestMultiply(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_no_seq_repeat_basic_array_like
def test_no_seq_repeat_basic_array_like(self): # Test that an array-like which does not know how to be multiplied # does not attempt sequence repeat (raise TypeError). # See also gh-7428. class ArrayLike: def __init__(self, arr): self.arr = arr def __array__(self): return self.arr # Test for simple ArrayLike above and memoryviews (original report) for arr_like in (ArrayLike(np.ones(3)), memoryview(np.ones(3))): assert_array_equal(arr_like * np.float32(3.0), np.full(3, 3.0)) assert_array_equal(np.float32(3.0) * arr_like, np.full(3, 3.0)) assert_array_equal(arr_like * np.int_(3), np.full(3, 3)) assert_array_equal(np.int_(3) * arr_like, np.full(3, 3))
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] @skip(reason="Array scalars do not decay to python scalars.") class TestMultiply(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
__init__
def __init__(self, arr): self.arr = arr
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class ArrayLike:
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
__array__
def __array__(self): return self.arr # Test for simple ArrayLike above and memoryviews (original report)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class ArrayLike:
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_exceptions
def test_exceptions(self): a = np.ones((), dtype=np.bool_)[()] # XXX: TypeError from numpy, RuntimeError from torch assert_raises((TypeError, RuntimeError), operator.neg, a)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestNegative(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_result
def test_result(self): types = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] # with suppress_warnings() as sup: # sup.filter(RuntimeWarning) for dt in types: a = np.ones((), dtype=dt)[()] if dt in np.typecodes["UnsignedInteger"]: st = np.dtype(dt).type max = st(np.iinfo(dt).max) assert_equal(operator.neg(a), max) else: assert_equal(operator.neg(a) + a, 0)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestNegative(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_exceptions
def test_exceptions(self): a = np.ones((), dtype=np.bool_)[()] # XXX: TypeError from numpy, RuntimeError from torch assert_raises((TypeError, RuntimeError), operator.neg, a)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestNegative(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_scalarmath.py
test_result
def test_result(self): types = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] # with suppress_warnings() as sup: # sup.filter(RuntimeWarning) for dt in types: a = np.ones((), dtype=dt)[()] if dt in np.typecodes["UnsignedInteger"]: st = np.dtype(dt).type max = st(np.iinfo(dt).max) assert_equal(operator.neg(a), max) else: assert_equal(operator.neg(a) + a, 0)
import contextlib import functools import itertools import operator import sys import warnings from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, skipIfTorchDynamo, slowTest as slow, subtest, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) import torch._numpy as np from torch._numpy.testing import ( _gen_alignment_data, assert_, assert_almost_equal, assert_equal, ) skip = functools.partial(skipif, True) IS_PYPY = False types = [ np.bool_, np.byte, np.ubyte, np.short, np.intc, np.int_, np.longlong, np.single, np.double, np.csingle, np.cdouble, ] floating_types = np.floating.__subclasses__() complex_floating_types = np.complexfloating.__subclasses__() objecty_things = [object(), None] reasonable_operators_for_scalars = [ operator.lt, operator.le, operator.eq, operator.ne, operator.ge, operator.gt, operator.add, operator.floordiv, operator.mod, operator.mul, operator.pow, operator.sub, operator.truediv, ] class TestNegative(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_0D_array
def test_0D_array(self): a = array(1) b = array(2) res = [atleast_1d(a), atleast_1d(b)] desired = [array([1]), array([2])] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_1D_array
def test_1D_array(self): a = array([1, 2]) b = array([2, 3]) res = [atleast_1d(a), atleast_1d(b)] desired = [array([1, 2]), array([2, 3])] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_2D_array
def test_2D_array(self): a = array([[1, 2], [1, 2]]) b = array([[2, 3], [2, 3]]) res = [atleast_1d(a), atleast_1d(b)] desired = [a, b] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_3D_array
def test_3D_array(self): a = array([[1, 2], [1, 2]]) b = array([[2, 3], [2, 3]]) a = array([a, a]) b = array([b, b]) res = [atleast_1d(a), atleast_1d(b)] desired = [a, b] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_r2array
def test_r2array(self): """Test to make sure equivalent Travis O's r2array function""" assert atleast_2d(3).shape == (1, 1) assert atleast_2d([3j, 1]).shape == (1, 2) assert atleast_2d([[[3, 1], [4, 5]], [[3, 5], [1, 2]]]).shape == (2, 2, 2)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast2d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/core/test_shape_base.py
test_0D_array
def test_0D_array(self): a = array(1) b = array(2) res = [atleast_1d(a), atleast_1d(b)] desired = [array([1]), array([2])] assert_array_equal(res, desired)
import functools from unittest import expectedFailure as xfail, skipIf as skipif import numpy import pytest from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) import numpy as np from numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array, atleast_1d, atleast_2d, atleast_3d, AxisError, concatenate, hstack, newaxis, stack, vstack, ) from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_PYPY = False class TestAtleast1d(TestCase): import operator
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added