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/lib/test_index_tricks.py
test_c_
def test_c_(self): a = np.c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])] assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestC(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_basic
def test_basic(self): assert_equal(np.unravel_index(2, (2, 2)), (1, 0)) # test that new shape argument works properly assert_equal(np.unravel_index(indices=2, shape=(2, 2)), (1, 0)) # test that an invalid second keyword argument # is properly handled, including the old name `dims`. with assert_raises(TypeError): np.unravel_index(indices=2, hape=(2, 2)) with assert_raises(TypeError): np.unravel_index(2, hape=(2, 2)) with assert_raises(TypeError): np.unravel_index(254, ims=(17, 94)) with assert_raises(TypeError): np.unravel_index(254, dims=(17, 94)) assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2) assert_equal(np.unravel_index(254, (17, 94)), (2, 66)) assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254) assert_raises(ValueError, np.unravel_index, -1, (2, 2)) assert_raises(TypeError, np.unravel_index, 0.5, (2, 2)) assert_raises(ValueError, np.unravel_index, 4, (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (-3, 1), (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (2, 1), (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (0, -3), (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (0, 2), (2, 2)) assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.0), (2, 2)) assert_equal(np.unravel_index((2 * 3 + 1) * 6 + 4, (4, 3, 6)), [2, 1, 4]) assert_equal(np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2 * 3 + 1) * 6 + 4) arr = np.array([[3, 6, 6], [4, 5, 1]]) assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37]) assert_equal(np.ravel_multi_index(arr, (7, 6), order="F"), [31, 41, 13]) assert_equal(np.ravel_multi_index(arr, (4, 6), mode="clip"), [22, 23, 19]) assert_equal( np.ravel_multi_index(arr, (4, 4), mode=("clip", "wrap")), [12, 13, 13] ) assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621) assert_equal( np.unravel_index(np.array([22, 41, 37]), (7, 6)), [[3, 6, 6], [4, 5, 1]] ) assert_equal( np.unravel_index(np.array([31, 41, 13]), (7, 6), order="F"), [[3, 6, 6], [4, 5, 1]], ) assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xpassIfTorchDynamo # (reason="unravel_index not implemented") @instantiate_parametrized_tests class TestRavelUnravelIndex(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_tall_matrix
def test_tall_matrix(self): a = np.zeros((10, 3), dtype=int) fill_diagonal(a, 5) assert_array_equal( a, np.array( [ [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ] ), )
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestFillDiagonal(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_tall_matrix_wrap
def test_tall_matrix_wrap(self): a = np.zeros((10, 3), dtype=int) fill_diagonal(a, 5, True) assert_array_equal( a, np.array( [ [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0], ] ), )
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestFillDiagonal(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_wide_matrix
def test_wide_matrix(self): a = np.zeros((3, 10), dtype=int) fill_diagonal(a, 5) assert_array_equal( a, np.array( [ [5, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 5, 0, 0, 0, 0, 0, 0, 0], ] ), )
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestFillDiagonal(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_operate_4d_array
def test_operate_4d_array(self): a = np.zeros((3, 3, 3, 3), dtype=int) fill_diagonal(a, 4) i = np.array([0, 1, 2]) assert_equal(np.where(a != 0), (i, i, i, i))
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestFillDiagonal(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_low_dim_handling
def test_low_dim_handling(self): # raise error with low dimensionality a = np.zeros(3, dtype=int) with assert_raises(ValueError): fill_diagonal(a, 5)
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestFillDiagonal(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_hetero_shape_handling
def test_hetero_shape_handling(self): # raise error with high dimensionality and # shape mismatch a = np.zeros((3, 3, 7, 3), dtype=int) with assert_raises(ValueError): fill_diagonal(a, 2)
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestFillDiagonal(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_broadcast
def test_broadcast(self): """Test that non-indexing dimensions are broadcast in both directions""" a = np.ones((3, 4, 1)) ai = np.ones((1, 2, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, (3, 2, 5))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_broadcast
def test_broadcast(self): """Test that non-indexing dimensions are broadcast in both directions""" a = np.ones((3, 4, 1)) ai = np.ones((1, 2, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, (3, 2, 5))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_simple
def test_simple(self): a = np.ones((20, 10), "d") assert_array_equal(apply_along_axis(len, 0, a), len(a) * np.ones(a.shape[1]))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_simple101
def test_simple101(self): a = np.ones((10, 101), "d") assert_array_equal(apply_along_axis(len, 0, a), len(a) * np.ones(a.shape[1]))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_3d
def test_3d(self): a = np.arange(27).reshape((3, 3, 3)) assert_array_equal( apply_along_axis(np.sum, 0, a), [[27, 30, 33], [36, 39, 42], [45, 48, 51]] )
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_scalar_array
def test_scalar_array(self, cls=np.ndarray): a = np.ones((6, 3)).view(cls) res = apply_along_axis(np.sum, 0, a) assert_(isinstance(res, cls)) assert_array_equal(res, np.array([6, 6, 6]).view(cls))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
sum_to_0d
def sum_to_0d(x): """Sum x, returning a 0d array of the same class""" assert_equal(x.ndim, 1) return np.squeeze(np.sum(x, keepdims=True)) a = np.ones((6, 3)).view(cls) res = apply_along_axis(sum_to_0d, 0, a) assert_(isinstance(res, cls)) assert_array_equal(res, np.array([6, 6, 6]).view(cls)) res = apply_along_axis(sum_to_0d, 1, a) assert_(isinstance(res, cls)) assert_array_equal(res, np.array([3, 3, 3, 3, 3, 3]).view(cls))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
f1to2
def f1to2(x): """produces an asymmetric non-square matrix from x""" assert_equal(x.ndim, 1) return (x[::-1] * x[1:, None]).view(cls) a2d = np.arange(6 * 3).reshape((6, 3)) # 2d insertion along first axis actual = apply_along_axis(f1to2, 0, a2d) expected = np.stack( [f1to2(a2d[:, i]) for i in range(a2d.shape[1])], axis=-1 ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) # 2d insertion along last axis actual = apply_along_axis(f1to2, 1, a2d) expected = np.stack( [f1to2(a2d[i, :]) for i in range(a2d.shape[0])], axis=0 ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) # 3d insertion along middle axis a3d = np.arange(6 * 5 * 3).reshape((6, 5, 3)) actual = apply_along_axis(f1to2, 1, a3d) expected = np.stack( [ np.stack([f1to2(a3d[i, :, j]) for i in range(a3d.shape[0])], axis=0) for j in range(a3d.shape[2]) ], axis=-1, ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
f1to2
def f1to2(x): """produces an asymmetric non-square matrix from x""" assert_equal(x.ndim, 1) return (x[::-1] * x[1:, None]).view(cls) a2d = np.arange(6 * 3).reshape((6, 3)) # 2d insertion along first axis actual = apply_along_axis(f1to2, 0, a2d) expected = np.stack( [f1to2(a2d[:, i]) for i in range(a2d.shape[1])], axis=-1 ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) # 2d insertion along last axis actual = apply_along_axis(f1to2, 1, a2d) expected = np.stack( [f1to2(a2d[i, :]) for i in range(a2d.shape[0])], axis=0 ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) # 3d insertion along middle axis a3d = np.arange(6 * 5 * 3).reshape((6, 5, 3)) actual = apply_along_axis(f1to2, 1, a3d) expected = np.stack( [ np.stack([f1to2(a3d[i, :, j]) for i in range(a3d.shape[0])], axis=0) for j in range(a3d.shape[2]) ], axis=-1, ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_more_mixed_type
def test_more_mixed_type(self): g = r_[-10.1, np.array([1]), np.array([2, 3, 4]), 10.0] assert_(g.dtype == "f8")
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xfail # (reason="r_ not implemented") class TestConcatenator(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_complex_step
def test_complex_step(self): # Regression test for #12262 g = r_[0:36:100j] assert_(g.shape == (100,)) # Related to #16466 g = r_[0 : 36 : np.complex64(100j)] assert_(g.shape == (100,))
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xfail # (reason="r_ not implemented") class TestConcatenator(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_2d
def test_2d(self): b = np.random.rand(5, 5) c = np.random.rand(5, 5) d = r_["1", b, c] # append columns assert_(d.shape == (5, 10)) assert_array_equal(d[:, :5], b) assert_array_equal(d[:, 5:], c) d = r_[b, c] assert_(d.shape == (10, 5)) assert_array_equal(d[:5, :], b) assert_array_equal(d[5:, :], c)
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xfail # (reason="r_ not implemented") class TestConcatenator(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_basic
def test_basic(self): assert_equal(np.unravel_index(2, (2, 2)), (1, 0)) # test that new shape argument works properly assert_equal(np.unravel_index(indices=2, shape=(2, 2)), (1, 0)) # test that an invalid second keyword argument # is properly handled, including the old name `dims`. with assert_raises(TypeError): np.unravel_index(indices=2, hape=(2, 2)) with assert_raises(TypeError): np.unravel_index(2, hape=(2, 2)) with assert_raises(TypeError): np.unravel_index(254, ims=(17, 94)) with assert_raises(TypeError): np.unravel_index(254, dims=(17, 94)) assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2) assert_equal(np.unravel_index(254, (17, 94)), (2, 66)) assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254) assert_raises(ValueError, np.unravel_index, -1, (2, 2)) assert_raises(TypeError, np.unravel_index, 0.5, (2, 2)) assert_raises(ValueError, np.unravel_index, 4, (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (-3, 1), (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (2, 1), (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (0, -3), (2, 2)) assert_raises(ValueError, np.ravel_multi_index, (0, 2), (2, 2)) assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.0), (2, 2)) assert_equal(np.unravel_index((2 * 3 + 1) * 6 + 4, (4, 3, 6)), [2, 1, 4]) assert_equal(np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2 * 3 + 1) * 6 + 4) arr = np.array([[3, 6, 6], [4, 5, 1]]) assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37]) assert_equal(np.ravel_multi_index(arr, (7, 6), order="F"), [31, 41, 13]) assert_equal(np.ravel_multi_index(arr, (4, 6), mode="clip"), [22, 23, 19]) assert_equal( np.ravel_multi_index(arr, (4, 4), mode=("clip", "wrap")), [12, 13, 13] ) assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621) assert_equal( np.unravel_index(np.array([22, 41, 37]), (7, 6)), [[3, 6, 6], [4, 5, 1]] ) assert_equal( np.unravel_index(np.array([31, 41, 13]), (7, 6), order="F"), [[3, 6, 6], [4, 5, 1]], ) assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xpassIfTorchDynamo # (reason="unravel_index not implemented") @instantiate_parametrized_tests class TestRavelUnravelIndex(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_regression_1
def test_regression_1(self): # ticket #1196 a = np.arange(2) assert_equal(a[:-1], a[s_[:-1]]) assert_equal(a[:-1], a[index_exp[:-1]])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestIndexExpression(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_simple_1
def test_simple_1(self): a = np.random.rand(4, 5, 6) assert_equal(a[:, :3, [1, 2]], a[index_exp[:, :3, [1, 2]]]) assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestIndexExpression(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_regression_1
def test_regression_1(self): # ticket #1196 a = np.arange(2) assert_equal(a[:-1], a[s_[:-1]]) assert_equal(a[:-1], a[index_exp[:-1]])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestIndexExpression(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_shape_and_dtype
def test_shape_and_dtype(self): sizes = (4, 5, 3, 2) # Test both lists and arrays for func in (range, np.arange): arrays = ix_(*[func(sz) for sz in sizes]) for k, (a, sz) in enumerate(zip(arrays, sizes)): assert_equal(a.shape[k], sz) assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k)) assert_(np.issubdtype(a.dtype, np.integer))
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xfail # (reason="ix_ not implemented") class TestIx_(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_bool
def test_bool(self): bool_a = [True, False, True, True] (int_a,) = np.nonzero(bool_a) assert_equal(ix_(bool_a)[0], int_a)
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) @xfail # (reason="ix_ not implemented") class TestIx_(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
sample_1d
def sample_1d(x): return x[1], x[0] res = np.apply_along_axis(sample_1d, 1, np.array([[1, 2], [3, 4]])) assert_array_equal(res, np.array([[2, 1], [4, 3]]))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_empty
def test_empty(self): """Test everything is ok with empty results, even with inserted dims""" a = np.ones((3, 4, 5)) ai = np.ones((3, 0, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, ai.shape)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
never_call
def never_call(x): assert_(False) # should never be reached a = np.empty((0, 0)) assert_raises(ValueError, np.apply_along_axis, never_call, 0, a) assert_raises(ValueError, np.apply_along_axis, never_call, 1, a) # but it's sometimes ok with some non-zero dimensions
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
empty_to_1
def empty_to_1(x): assert_(len(x) == 0) return 1 a = np.empty((10, 0)) actual = np.apply_along_axis(empty_to_1, 1, a) assert_equal(actual, np.ones(10)) assert_raises(ValueError, np.apply_along_axis, empty_to_1, 0, a)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_with_iterable_object
def test_with_iterable_object(self): # from issue 5248 d = np.array([[{1, 11}, {2, 22}, {3, 33}], [{4, 44}, {5, 55}, {6, 66}]]) actual = np.apply_along_axis(lambda a: set.union(*a), 0, d) expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}]) assert_equal(actual, expected) # issue 8642 - assert_equal doesn't detect this! for i in np.ndindex(actual.shape): assert_equal(type(actual[i]), type(expected[i]))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_simple
def test_simple(self): a = np.ones((20, 10), "d") assert_array_equal(apply_along_axis(len, 0, a), len(a) * np.ones(a.shape[1]))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_functionality
def test_functionality(self): s = (2, 3, 4, 5) a = np.empty(s) for axis in range(-5, 4): b = expand_dims(a, axis) assert_(b.shape[axis] == 1) assert_(np.squeeze(b).shape == s)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestExpandDims(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_axis_tuple
def test_axis_tuple(self): a = np.empty((3, 3, 3)) assert np.expand_dims(a, axis=(0, 1, 2)).shape == (1, 1, 1, 3, 3, 3) assert np.expand_dims(a, axis=(0, -1, -2)).shape == (1, 3, 3, 3, 1, 1) assert np.expand_dims(a, axis=(0, 3, 5)).shape == (1, 3, 3, 1, 3, 1) assert np.expand_dims(a, axis=(0, -3, -5)).shape == (1, 1, 3, 1, 3, 3)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestExpandDims(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_axis_out_of_range
def test_axis_out_of_range(self): s = (2, 3, 4, 5) a = np.empty(s) assert_raises(np.AxisError, expand_dims, a, -6) assert_raises(np.AxisError, expand_dims, a, 5) a = np.empty((3, 3, 3)) assert_raises(np.AxisError, expand_dims, a, (0, -6)) assert_raises(np.AxisError, expand_dims, a, (0, 5))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestExpandDims(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_repeated_axis
def test_repeated_axis(self): a = np.empty((3, 3, 3)) assert_raises(ValueError, expand_dims, a, axis=(1, 1))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestExpandDims(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_integer_0_split
def test_integer_0_split(self): a = np.arange(10) assert_raises(ValueError, array_split, a, 0)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_integer_split
def test_integer_split(self): a = np.arange(10) res = array_split(a, 1) desired = [np.arange(10)] compare_results(res, desired) res = array_split(a, 2) desired = [np.arange(5), np.arange(5, 10)] compare_results(res, desired) res = array_split(a, 3) desired = [np.arange(4), np.arange(4, 7), np.arange(7, 10)] compare_results(res, desired) res = array_split(a, 4) desired = [np.arange(3), np.arange(3, 6), np.arange(6, 8), np.arange(8, 10)] compare_results(res, desired) res = array_split(a, 5) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 6), np.arange(6, 8), np.arange(8, 10), ] compare_results(res, desired) res = array_split(a, 6) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 6), np.arange(6, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 7) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 8) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 9) desired = [ np.arange(2), np.arange(2, 3), np.arange(3, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 10) desired = [ np.arange(1), np.arange(1, 2), np.arange(2, 3), np.arange(3, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 11) desired = [ np.arange(1), np.arange(1, 2), np.arange(2, 3), np.arange(3, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), np.array([]), ] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_integer_split_2D_rows
def test_integer_split_2D_rows(self): a = np.array([np.arange(10), np.arange(10)]) res = array_split(a, 3, axis=0) tgt = [np.array([np.arange(10)]), np.array([np.arange(10)]), np.zeros((0, 10))] compare_results(res, tgt) assert_(a.dtype.type is res[-1].dtype.type) # Same thing for manual splits: res = array_split(a, [0, 1], axis=0) tgt = [np.zeros((0, 10)), np.array([np.arange(10)]), np.array([np.arange(10)])] compare_results(res, tgt) assert_(a.dtype.type is res[-1].dtype.type)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_integer_split_2D_cols
def test_integer_split_2D_cols(self): a = np.array([np.arange(10), np.arange(10)]) res = array_split(a, 3, axis=-1) desired = [ np.array([np.arange(4), np.arange(4)]), np.array([np.arange(4, 7), np.arange(4, 7)]), np.array([np.arange(7, 10), np.arange(7, 10)]), ] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_integer_split_2D_rows_greater_max_int32
def test_integer_split_2D_rows_greater_max_int32(self): a = np.broadcast_to([0], (1 << 32, 2)) res = array_split(a, 4) chunk = np.broadcast_to([0], (1 << 30, 2)) tgt = [chunk] * 4 for i in range(len(tgt)): assert_equal(res[i].shape, tgt[i].shape)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_index_split_simple
def test_index_split_simple(self): a = np.arange(10) indices = [1, 5, 7] res = array_split(a, indices, axis=-1) desired = [np.arange(0, 1), np.arange(1, 5), np.arange(5, 7), np.arange(7, 10)] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_index_split_low_bound
def test_index_split_low_bound(self): a = np.arange(10) indices = [0, 5, 7] res = array_split(a, indices, axis=-1) desired = [np.array([]), np.arange(0, 5), np.arange(5, 7), np.arange(7, 10)] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_index_split_high_bound
def test_index_split_high_bound(self): a = np.arange(10) indices = [0, 5, 7, 10, 12] res = array_split(a, indices, axis=-1) desired = [ np.array([]), np.arange(0, 5), np.arange(5, 7), np.arange(7, 10), np.array([]), np.array([]), ] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestArraySplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_equal_split
def test_equal_split(self): a = np.arange(10) res = split(a, 2) desired = [np.arange(5), np.arange(5, 10)] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestSplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_diag_indices
def test_diag_indices(self): di = diag_indices(4) a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) a[di] = 100 assert_array_equal( a, np.array( [[100, 2, 3, 4], [5, 100, 7, 8], [9, 10, 100, 12], [13, 14, 15, 100]] ), ) # Now, we create indices to manipulate a 3-d array: d3 = diag_indices(2, 3) # And use it to set the diagonal of a zeros array to 1: a = np.zeros((2, 2, 2), dtype=int) a[d3] = 1 assert_array_equal(a, np.array([[[1, 0], [0, 0]], [[0, 0], [0, 1]]]))
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestDiagIndices(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_diag_indices_from
def test_diag_indices_from(self): x = np.random.random((4, 4)) r, c = diag_indices_from(x) assert_array_equal(r, np.arange(4)) assert_array_equal(c, np.arange(4))
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestDiagIndicesFrom(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_error_small_input
def test_error_small_input(self): x = np.ones(7) with assert_raises(ValueError): diag_indices_from(x)
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestDiagIndicesFrom(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_error_shape_mismatch
def test_error_shape_mismatch(self): x = np.zeros((3, 3, 2, 3), dtype=int) with assert_raises(ValueError): diag_indices_from(x)
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestDiagIndicesFrom(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_index_tricks.py
test_ndindex
def test_ndindex(self): x = list(ndindex(1, 2, 3)) expected = [ix for ix, e in ndenumerate(np.zeros((1, 2, 3)))] assert_array_equal(x, expected) x = list(ndindex((1, 2, 3))) assert_array_equal(x, expected) # Test use of scalars and tuples x = list(ndindex((3,))) assert_array_equal(x, list(ndindex(3))) # Make sure size argument is optional x = list(ndindex()) assert_equal(x, [()]) x = list(ndindex(())) assert_equal(x, [()]) # Make sure 0-sized ndindex works correctly x = list(ndindex(*[0])) assert_equal(x, [])
import functools from unittest import expectedFailure as xfail, skipIf from pytest import raises as assert_raises # , assert_raises_regex, from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xpassIfTorchDynamo, ) skip = functools.partial(skipIf, True) import numpy as np from numpy import diag_indices, diag_indices_from, fill_diagonal, index_exp, s_ from numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises_regex, ) import torch._numpy as np from torch._numpy import ( diag_indices, diag_indices_from, fill_diagonal, index_exp, s_, ) from torch._numpy.testing import ( assert_, assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_equal, ) class TestNdIndex(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
_add_keepdims
def _add_keepdims(func): """hack in keepdims behavior into a function taking an axis""" @functools.wraps(func) def wrapped(a, axis, **kwargs): res = func(a, axis=axis, **kwargs) if axis is None: axis = 0 # res is now a scalar, so we can insert this anywhere return np.expand_dims(res, axis=axis) return wrapped class TestTakeAlongAxis(TestCase): def test_argequivalent(self): """Test it translates from arg<func> to <func>""" a = rand(3, 4, 5) funcs = [ (np.sort, np.argsort, {}), (_add_keepdims(np.min), _add_keepdims(np.argmin), {}), (_add_keepdims(np.max), _add_keepdims(np.argmax), {}), # FIXME (np.partition, np.argpartition, dict(kth=2)), ] for func, argfunc, kwargs in funcs: for axis in list(range(a.ndim)) + [None]: a_func = func(a, axis=axis, **kwargs) ai_func = argfunc(a, axis=axis, **kwargs) assert_equal(a_func, take_along_axis(a, ai_func, axis=axis)) def test_invalid(self): """Test it errors when indices has too few dimensions""" a = np.ones((10, 10)) ai = np.ones((10, 2), dtype=np.intp) # sanity check take_along_axis(a, ai, axis=1) # not enough indices assert_raises( (ValueError, RuntimeError), take_along_axis, a, np.array(1), axis=1 ) # bool arrays not allowed assert_raises( (IndexError, RuntimeError), take_along_axis, a, ai.astype(bool), axis=1 ) # float arrays not allowed assert_raises( (IndexError, RuntimeError), take_along_axis, a, ai.astype(float), axis=1 ) # invalid axis assert_raises(np.AxisError, take_along_axis, a, ai, axis=10) def test_empty(self): """Test everything is ok with empty results, even with inserted dims""" a = np.ones((3, 4, 5)) ai = np.ones((3, 0, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, ai.shape) def test_broadcast(self): """Test that non-indexing dimensions are broadcast in both directions""" a = np.ones((3, 4, 1)) ai = np.ones((1, 2, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, (3, 2, 5)) class TestPutAlongAxis(TestCase): def test_replace_max(self): a_base = np.array([[10, 30, 20], [60, 40, 50]]) for axis in list(range(a_base.ndim)) + [None]: # we mutate this in the loop a = a_base.copy() # replace the max with a small value i_max = _add_keepdims(np.argmax)(a, axis=axis) put_along_axis(a, i_max, -99, axis=axis) # find the new minimum, which should max i_min = _add_keepdims(np.argmin)(a, axis=axis) assert_equal(i_min, i_max) @xpassIfTorchDynamo # ( # reason="RuntimeError: Expected index [1, 2, 5] to be smaller than self [3, 4, 1] apart from dimension 1") def test_broadcast(self): """Test that non-indexing dimensions are broadcast in both directions""" a = np.ones((3, 4, 1)) ai = np.arange(10, dtype=np.intp).reshape((1, 2, 5)) % 4 put_along_axis(a, ai, 20, axis=1) assert_equal(take_along_axis(a, ai, axis=1), 20) @xpassIfTorchDynamo # (reason="apply_along_axis not implemented") class TestApplyAlongAxis(TestCase): def test_simple(self): a = np.ones((20, 10), "d") assert_array_equal(apply_along_axis(len, 0, a), len(a) * np.ones(a.shape[1])) def test_simple101(self): a = np.ones((10, 101), "d") assert_array_equal(apply_along_axis(len, 0, a), len(a) * np.ones(a.shape[1])) def test_3d(self): a = np.arange(27).reshape((3, 3, 3)) assert_array_equal( apply_along_axis(np.sum, 0, a), [[27, 30, 33], [36, 39, 42], [45, 48, 51]] ) def test_scalar_array(self, cls=np.ndarray): a = np.ones((6, 3)).view(cls) res = apply_along_axis(np.sum, 0, a) assert_(isinstance(res, cls)) assert_array_equal(res, np.array([6, 6, 6]).view(cls)) def test_0d_array(self, cls=np.ndarray): def sum_to_0d(x): """Sum x, returning a 0d array of the same class""" assert_equal(x.ndim, 1) return np.squeeze(np.sum(x, keepdims=True)) a = np.ones((6, 3)).view(cls) res = apply_along_axis(sum_to_0d, 0, a) assert_(isinstance(res, cls)) assert_array_equal(res, np.array([6, 6, 6]).view(cls)) res = apply_along_axis(sum_to_0d, 1, a) assert_(isinstance(res, cls)) assert_array_equal(res, np.array([3, 3, 3, 3, 3, 3]).view(cls)) def test_axis_insertion(self, cls=np.ndarray): def f1to2(x): """produces an asymmetric non-square matrix from x""" assert_equal(x.ndim, 1) return (x[::-1] * x[1:, None]).view(cls) a2d = np.arange(6 * 3).reshape((6, 3)) # 2d insertion along first axis actual = apply_along_axis(f1to2, 0, a2d) expected = np.stack( [f1to2(a2d[:, i]) for i in range(a2d.shape[1])], axis=-1 ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) # 2d insertion along last axis actual = apply_along_axis(f1to2, 1, a2d) expected = np.stack( [f1to2(a2d[i, :]) for i in range(a2d.shape[0])], axis=0 ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) # 3d insertion along middle axis a3d = np.arange(6 * 5 * 3).reshape((6, 5, 3)) actual = apply_along_axis(f1to2, 1, a3d) expected = np.stack( [ np.stack([f1to2(a3d[i, :, j]) for i in range(a3d.shape[0])], axis=0) for j in range(a3d.shape[2]) ], axis=-1, ).view(cls) assert_equal(type(actual), type(expected)) assert_equal(actual, expected) def test_axis_insertion_ma(self): def f1to2(x): """produces an asymmetric non-square matrix from x""" assert_equal(x.ndim, 1) res = x[::-1] * x[1:, None] return np.ma.masked_where(res % 5 == 0, res) a = np.arange(6 * 3).reshape((6, 3)) res = apply_along_axis(f1to2, 0, a) assert_(isinstance(res, np.ma.masked_array)) assert_equal(res.ndim, 3) assert_array_equal(res[:, :, 0].mask, f1to2(a[:, 0]).mask) assert_array_equal(res[:, :, 1].mask, f1to2(a[:, 1]).mask) assert_array_equal(res[:, :, 2].mask, f1to2(a[:, 2]).mask) def test_tuple_func1d(self): def sample_1d(x): return x[1], x[0] res = np.apply_along_axis(sample_1d, 1, np.array([[1, 2], [3, 4]])) assert_array_equal(res, np.array([[2, 1], [4, 3]])) def test_empty(self): # can't apply_along_axis when there's no chance to call the function def never_call(x): assert_(False) # should never be reached a = np.empty((0, 0)) assert_raises(ValueError, np.apply_along_axis, never_call, 0, a) assert_raises(ValueError, np.apply_along_axis, never_call, 1, a) # but it's sometimes ok with some non-zero dimensions def empty_to_1(x): assert_(len(x) == 0) return 1 a = np.empty((10, 0)) actual = np.apply_along_axis(empty_to_1, 1, a) assert_equal(actual, np.ones(10)) assert_raises(ValueError, np.apply_along_axis, empty_to_1, 0, a) @skip # TypeError: descriptor 'union' for 'set' objects doesn't apply to a 'numpy.int64' object def test_with_iterable_object(self): # from issue 5248 d = np.array([[{1, 11}, {2, 22}, {3, 33}], [{4, 44}, {5, 55}, {6, 66}]]) actual = np.apply_along_axis(lambda a: set.union(*a), 0, d) expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}]) assert_equal(actual, expected) # issue 8642 - assert_equal doesn't detect this! for i in np.ndindex(actual.shape): assert_equal(type(actual[i]), type(expected[i])) @xfail # (reason="apply_over_axes not implemented") class TestApplyOverAxes(TestCase): def test_simple(self): a = np.arange(24).reshape(2, 3, 4) aoa_a = apply_over_axes(np.sum, a, [0, 2]) assert_array_equal(aoa_a, np.array([[[60], [92], [124]]])) class TestExpandDims(TestCase): def test_functionality(self): s = (2, 3, 4, 5) a = np.empty(s) for axis in range(-5, 4): b = expand_dims(a, axis) assert_(b.shape[axis] == 1) assert_(np.squeeze(b).shape == s) def test_axis_tuple(self): a = np.empty((3, 3, 3)) assert np.expand_dims(a, axis=(0, 1, 2)).shape == (1, 1, 1, 3, 3, 3) assert np.expand_dims(a, axis=(0, -1, -2)).shape == (1, 3, 3, 3, 1, 1) assert np.expand_dims(a, axis=(0, 3, 5)).shape == (1, 3, 3, 1, 3, 1) assert np.expand_dims(a, axis=(0, -3, -5)).shape == (1, 1, 3, 1, 3, 3) def test_axis_out_of_range(self): s = (2, 3, 4, 5) a = np.empty(s) assert_raises(np.AxisError, expand_dims, a, -6) assert_raises(np.AxisError, expand_dims, a, 5) a = np.empty((3, 3, 3)) assert_raises(np.AxisError, expand_dims, a, (0, -6)) assert_raises(np.AxisError, expand_dims, a, (0, 5)) def test_repeated_axis(self): a = np.empty((3, 3, 3)) assert_raises(ValueError, expand_dims, a, axis=(1, 1)) class TestArraySplit(TestCase): def test_integer_0_split(self): a = np.arange(10) assert_raises(ValueError, array_split, a, 0) def test_integer_split(self): a = np.arange(10) res = array_split(a, 1) desired = [np.arange(10)] compare_results(res, desired) res = array_split(a, 2) desired = [np.arange(5), np.arange(5, 10)] compare_results(res, desired) res = array_split(a, 3) desired = [np.arange(4), np.arange(4, 7), np.arange(7, 10)] compare_results(res, desired) res = array_split(a, 4) desired = [np.arange(3), np.arange(3, 6), np.arange(6, 8), np.arange(8, 10)] compare_results(res, desired) res = array_split(a, 5) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 6), np.arange(6, 8), np.arange(8, 10), ] compare_results(res, desired) res = array_split(a, 6) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 6), np.arange(6, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 7) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 8) desired = [ np.arange(2), np.arange(2, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 9) desired = [ np.arange(2), np.arange(2, 3), np.arange(3, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 10) desired = [ np.arange(1), np.arange(1, 2), np.arange(2, 3), np.arange(3, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), ] compare_results(res, desired) res = array_split(a, 11) desired = [ np.arange(1), np.arange(1, 2), np.arange(2, 3), np.arange(3, 4), np.arange(4, 5), np.arange(5, 6), np.arange(6, 7), np.arange(7, 8), np.arange(8, 9), np.arange(9, 10), np.array([]), ] compare_results(res, desired) def test_integer_split_2D_rows(self): a = np.array([np.arange(10), np.arange(10)]) res = array_split(a, 3, axis=0) tgt = [np.array([np.arange(10)]), np.array([np.arange(10)]), np.zeros((0, 10))] compare_results(res, tgt) assert_(a.dtype.type is res[-1].dtype.type) # Same thing for manual splits: res = array_split(a, [0, 1], axis=0) tgt = [np.zeros((0, 10)), np.array([np.arange(10)]), np.array([np.arange(10)])] compare_results(res, tgt) assert_(a.dtype.type is res[-1].dtype.type) def test_integer_split_2D_cols(self): a = np.array([np.arange(10), np.arange(10)]) res = array_split(a, 3, axis=-1) desired = [ np.array([np.arange(4), np.arange(4)]), np.array([np.arange(4, 7), np.arange(4, 7)]), np.array([np.arange(7, 10), np.arange(7, 10)]), ] compare_results(res, desired) def test_integer_split_2D_default(self): """This will fail if we change default axis""" a = np.array([np.arange(10), np.arange(10)]) res = array_split(a, 3) tgt = [np.array([np.arange(10)]), np.array([np.arange(10)]), np.zeros((0, 10))] compare_results(res, tgt) assert_(a.dtype.type is res[-1].dtype.type) # perhaps should check higher dimensions @skipif(not IS_64BIT, reason="Needs 64bit platform") def test_integer_split_2D_rows_greater_max_int32(self): a = np.broadcast_to([0], (1 << 32, 2)) res = array_split(a, 4) chunk = np.broadcast_to([0], (1 << 30, 2)) tgt = [chunk] * 4 for i in range(len(tgt)): assert_equal(res[i].shape, tgt[i].shape) def test_index_split_simple(self): a = np.arange(10) indices = [1, 5, 7] res = array_split(a, indices, axis=-1) desired = [np.arange(0, 1), np.arange(1, 5), np.arange(5, 7), np.arange(7, 10)] compare_results(res, desired) def test_index_split_low_bound(self): a = np.arange(10) indices = [0, 5, 7] res = array_split(a, indices, axis=-1) desired = [np.array([]), np.arange(0, 5), np.arange(5, 7), np.arange(7, 10)] compare_results(res, desired) def test_index_split_high_bound(self): a = np.arange(10) indices = [0, 5, 7, 10, 12] res = array_split(a, indices, axis=-1) desired = [ np.array([]), np.arange(0, 5), np.arange(5, 7), np.arange(7, 10), np.array([]), np.array([]), ] compare_results(res, desired) class TestSplit(TestCase): # The split function is essentially the same as array_split, # except that it test if splitting will result in an # equal split. Only test for this case. def test_equal_split(self): a = np.arange(10) res = split(a, 2) desired = [np.arange(5), np.arange(5, 10)] compare_results(res, desired) def test_unequal_split(self): a = np.arange(10) assert_raises(ValueError, split, a, 3) class TestColumnStack(TestCase): def test_non_iterable(self): assert_raises(TypeError, column_stack, 1) def test_1D_arrays(self): # example from docstring a = np.array((1, 2, 3)) b = np.array((2, 3, 4)) expected = np.array([[1, 2], [2, 3], [3, 4]]) actual = np.column_stack((a, b)) assert_equal(actual, expected) def test_2D_arrays(self): # same as hstack 2D docstring example a = np.array([[1], [2], [3]]) b = np.array([[2], [3], [4]]) expected = np.array([[1, 2], [2, 3], [3, 4]]) actual = np.column_stack((a, b)) assert_equal(actual, expected) def test_generator(self): # numpy 1.24 emits a warning but we don't # with assert_warns(FutureWarning): column_stack([np.arange(3) for _ in range(2)]) class TestDstack(TestCase): def test_non_iterable(self): assert_raises(TypeError, dstack, 1) def test_0D_array(self): a = np.array(1) b = np.array(2) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired) def test_1D_array(self): a = np.array([1]) b = np.array([2]) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired) def test_2D_array(self): a = np.array([[1], [2]]) b = np.array([[1], [2]]) res = dstack([a, b]) desired = np.array( [ [[1, 1]], [ [ 2, 2, ] ], ] ) assert_array_equal(res, desired) def test_2D_array2(self): a = np.array([1, 2]) b = np.array([1, 2]) res = dstack([a, b]) desired = np.array([[[1, 1], [2, 2]]]) assert_array_equal(res, desired) def test_generator(self): # numpy 1.24 emits a warning but we don't # with assert_warns(FutureWarning): dstack([np.arange(3) for _ in range(2)]) # array_split has more comprehensive test of splitting. # only do simple test on hsplit, vsplit, and dsplit class TestHsplit(TestCase): """Only testing for integer splits.""" def test_non_iterable(self): assert_raises(ValueError, hsplit, 1, 1) def test_0D_array(self): a = np.array(1) try: hsplit(a, 2) assert_(0) except ValueError: pass def test_1D_array(self): a = np.array([1, 2, 3, 4]) res = hsplit(a, 2) desired = [np.array([1, 2]), np.array([3, 4])] compare_results(res, desired) def test_2D_array(self): a = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) res = hsplit(a, 2) desired = [np.array([[1, 2], [1, 2]]), np.array([[3, 4], [3, 4]])] compare_results(res, desired) class TestVsplit(TestCase): """Only testing for integer splits.""" def test_non_iterable(self): assert_raises(ValueError, vsplit, 1, 1) def test_0D_array(self): a = np.array(1) assert_raises(ValueError, vsplit, a, 2) def test_1D_array(self): a = np.array([1, 2, 3, 4]) try: vsplit(a, 2) assert_(0) except ValueError: pass def test_2D_array(self): a = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) res = vsplit(a, 2) desired = [np.array([[1, 2, 3, 4]]), np.array([[1, 2, 3, 4]])] compare_results(res, desired) class TestDsplit(TestCase): # Only testing for integer splits. def test_non_iterable(self): assert_raises(ValueError, dsplit, 1, 1) def test_0D_array(self): a = np.array(1) assert_raises(ValueError, dsplit, a, 2) def test_1D_array(self): a = np.array([1, 2, 3, 4]) assert_raises(ValueError, dsplit, a, 2) def test_2D_array(self): a = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) try: dsplit(a, 2) assert_(0) except ValueError: pass def test_3D_array(self): a = np.array([[[1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4]]]) res = dsplit(a, 2) desired = [ np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]]), np.array([[[3, 4], [3, 4]], [[3, 4], [3, 4]]]), ] compare_results(res, desired) class TestSqueeze(TestCase): def test_basic(self): a = rand(20, 10, 10, 1, 1) b = rand(20, 1, 10, 1, 20) c = rand(1, 1, 20, 10) assert_array_equal(np.squeeze(a), np.reshape(a, (20, 10, 10))) assert_array_equal(np.squeeze(b), np.reshape(b, (20, 10, 20))) assert_array_equal(np.squeeze(c), np.reshape(c, (20, 10))) # Squeezing to 0-dim should still give an ndarray a = [[[1.5]]] res = np.squeeze(a) assert_equal(res, 1.5) assert_equal(res.ndim, 0) assert type(res) is np.ndarray @xfailIfTorchDynamo def test_basic_2(self): aa = np.ones((3, 1, 4, 1, 1)) assert aa.squeeze().tensor._base is aa.tensor def test_squeeze_axis(self): A = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]]] assert_equal(np.squeeze(A).shape, (3, 3)) assert_equal(np.squeeze(A, axis=()), A) assert_equal(np.squeeze(np.zeros((1, 3, 1))).shape, (3,)) assert_equal(np.squeeze(np.zeros((1, 3, 1)), axis=0).shape, (3, 1)) assert_equal(np.squeeze(np.zeros((1, 3, 1)), axis=-1).shape, (1, 3)) assert_equal(np.squeeze(np.zeros((1, 3, 1)), axis=2).shape, (1, 3)) assert_equal(np.squeeze([np.zeros((3, 1))]).shape, (3,)) assert_equal(np.squeeze([np.zeros((3, 1))], axis=0).shape, (3, 1)) assert_equal(np.squeeze([np.zeros((3, 1))], axis=2).shape, (1, 3)) assert_equal(np.squeeze([np.zeros((3, 1))], axis=-1).shape, (1, 3)) def test_squeeze_type(self): # Ticket #133 a = np.array([3]) b = np.array(3) assert type(a.squeeze()) is np.ndarray assert type(b.squeeze()) is np.ndarray @skip(reason="XXX: order='F' not implemented") def test_squeeze_contiguous(self): # Similar to GitHub issue #387 a = np.zeros((1, 2)).squeeze() b = np.zeros((2, 2, 2), order="F")[:, :, ::2].squeeze() assert_(a.flags.c_contiguous) assert_(a.flags.f_contiguous) assert_(b.flags.f_contiguous) @xpassIfTorchDynamo # (reason="XXX: noop in torch, while numpy raises") def test_squeeze_axis_handling(self): with assert_raises(ValueError): np.squeeze(np.array([[1], [2], [3]]), axis=0) @instantiate_parametrized_tests class TestKron(TestCase): def test_basic(self): # Using 0-dimensional ndarray a = np.array(1) b = np.array([[1, 2], [3, 4]]) k = np.array([[1, 2], [3, 4]]) assert_array_equal(np.kron(a, b), k) a = np.array([[1, 2], [3, 4]]) b = np.array(1) assert_array_equal(np.kron(a, b), k) # Using 1-dimensional ndarray a = np.array([3]) b = np.array([[1, 2], [3, 4]]) k = np.array([[3, 6], [9, 12]]) assert_array_equal(np.kron(a, b), k) a = np.array([[1, 2], [3, 4]]) b = np.array([3]) assert_array_equal(np.kron(a, b), k) # Using 3-dimensional ndarray a = np.array([[[1]], [[2]]]) b = np.array([[1, 2], [3, 4]]) k = np.array([[[1, 2], [3, 4]], [[2, 4], [6, 8]]]) assert_array_equal(np.kron(a, b), k) a = np.array([[1, 2], [3, 4]]) b = np.array([[[1]], [[2]]]) k = np.array([[[1, 2], [3, 4]], [[2, 4], [6, 8]]]) assert_array_equal(np.kron(a, b), k) @skip(reason="NP_VER: fails on CI") @parametrize( "shape_a,shape_b", [ ((1, 1), (1, 1)), ((1, 2, 3), (4, 5, 6)), ((2, 2), (2, 2, 2)), ((1, 0), (1, 1)), ((2, 0, 2), (2, 2)), ((2, 0, 0, 2), (2, 0, 2)), ], ) def test_kron_shape(self, shape_a, shape_b): a = np.ones(shape_a) b = np.ones(shape_b) normalised_shape_a = (1,) * max(0, len(shape_b) - len(shape_a)) + shape_a normalised_shape_b = (1,) * max(0, len(shape_a) - len(shape_b)) + shape_b expected_shape = np.multiply(normalised_shape_a, normalised_shape_b) k = np.kron(a, b) assert np.array_equal(k.shape, expected_shape), "Unexpected shape from kron" class TestTile(TestCase): def test_basic(self): a = np.array([0, 1, 2]) b = [[1, 2], [3, 4]] assert_equal(tile(a, 2), [0, 1, 2, 0, 1, 2]) assert_equal(tile(a, (2, 2)), [[0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2]]) assert_equal(tile(a, (1, 2)), [[0, 1, 2, 0, 1, 2]]) assert_equal(tile(b, 2), [[1, 2, 1, 2], [3, 4, 3, 4]]) assert_equal(tile(b, (2, 1)), [[1, 2], [3, 4], [1, 2], [3, 4]]) assert_equal( tile(b, (2, 2)), [[1, 2, 1, 2], [3, 4, 3, 4], [1, 2, 1, 2], [3, 4, 3, 4]] ) def test_tile_one_repetition_on_array_gh4679(self): a = np.arange(5) b = tile(a, 1) b += 2 assert_equal(a, np.arange(5)) def test_empty(self): a = np.array([[[]]]) b = np.array([[], []]) c = tile(b, 2).shape d = tile(a, (3, 2, 5)).shape assert_equal(c, (2, 0)) assert_equal(d, (3, 2, 0)) def test_kroncompare(self): reps = [(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)] shape = [(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)] for s in shape: b = randint(0, 10, size=s) for r in reps: a = np.ones(r, b.dtype) large = tile(b, r) klarge = kron(a, b) assert_equal(large, klarge) @xfail # Maybe implement one day class TestMayShareMemory(TestCase): def test_basic(self): d = np.ones((50, 60)) d2 = np.ones((30, 60, 6)) assert_(np.may_share_memory(d, d)) assert_(np.may_share_memory(d, d[::-1])) assert_(np.may_share_memory(d, d[::2])) assert_(np.may_share_memory(d, d[1:, ::-1])) assert_(not np.may_share_memory(d[::-1], d2)) assert_(not np.may_share_memory(d[::2], d2)) assert_(not np.may_share_memory(d[1:, ::-1], d2)) assert_(np.may_share_memory(d2[1:, ::-1], d2)) # Utility
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
wrapped
def wrapped(a, axis, **kwargs): res = func(a, axis=axis, **kwargs) if axis is None: axis = 0 # res is now a scalar, so we can insert this anywhere return np.expand_dims(res, axis=axis) return wrapped
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_argequivalent
def test_argequivalent(self): """Test it translates from arg<func> to <func>""" a = rand(3, 4, 5) funcs = [ (np.sort, np.argsort, {}), (_add_keepdims(np.min), _add_keepdims(np.argmin), {}), (_add_keepdims(np.max), _add_keepdims(np.argmax), {}), # FIXME (np.partition, np.argpartition, dict(kth=2)), ] for func, argfunc, kwargs in funcs: for axis in list(range(a.ndim)) + [None]: a_func = func(a, axis=axis, **kwargs) ai_func = argfunc(a, axis=axis, **kwargs) assert_equal(a_func, take_along_axis(a, ai_func, axis=axis))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_invalid
def test_invalid(self): """Test it errors when indices has too few dimensions""" a = np.ones((10, 10)) ai = np.ones((10, 2), dtype=np.intp) # sanity check take_along_axis(a, ai, axis=1) # not enough indices assert_raises( (ValueError, RuntimeError), take_along_axis, a, np.array(1), axis=1 ) # bool arrays not allowed assert_raises( (IndexError, RuntimeError), take_along_axis, a, ai.astype(bool), axis=1 ) # float arrays not allowed assert_raises( (IndexError, RuntimeError), take_along_axis, a, ai.astype(float), axis=1 ) # invalid axis assert_raises(np.AxisError, take_along_axis, a, ai, axis=10)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_empty
def test_empty(self): """Test everything is ok with empty results, even with inserted dims""" a = np.ones((3, 4, 5)) ai = np.ones((3, 0, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, ai.shape)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_unequal_split
def test_unequal_split(self): a = np.arange(10) assert_raises(ValueError, split, a, 3)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestSplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_non_iterable
def test_non_iterable(self): assert_raises(TypeError, column_stack, 1)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_1D_arrays
def test_1D_arrays(self): # example from docstring a = np.array((1, 2, 3)) b = np.array((2, 3, 4)) expected = np.array([[1, 2], [2, 3], [3, 4]]) actual = np.column_stack((a, b)) assert_equal(actual, expected)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_2D_arrays
def test_2D_arrays(self): # same as hstack 2D docstring example a = np.array([[1], [2], [3]]) b = np.array([[2], [3], [4]]) expected = np.array([[1, 2], [2, 3], [3, 4]]) actual = np.column_stack((a, b)) assert_equal(actual, expected)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_generator
def test_generator(self): # numpy 1.24 emits a warning but we don't # with assert_warns(FutureWarning): column_stack([np.arange(3) for _ in range(2)])
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_non_iterable
def test_non_iterable(self): assert_raises(TypeError, column_stack, 1)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_0D_array
def test_0D_array(self): a = np.array(1) b = np.array(2) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_1D_array
def test_1D_array(self): a = np.array([1]) b = np.array([2]) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_2D_array
def test_2D_array(self): a = np.array([[1], [2]]) b = np.array([[1], [2]]) res = dstack([a, b]) desired = np.array( [ [[1, 1]], [ [ 2, 2, ] ], ] ) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_2D_array2
def test_2D_array2(self): a = np.array([1, 2]) b = np.array([1, 2]) res = dstack([a, b]) desired = np.array([[[1, 1], [2, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_kron_shape
def test_kron_shape(self, shape_a, shape_b): a = np.ones(shape_a) b = np.ones(shape_b) normalised_shape_a = (1,) * max(0, len(shape_b) - len(shape_a)) + shape_a normalised_shape_b = (1,) * max(0, len(shape_a) - len(shape_b)) + shape_b expected_shape = np.multiply(normalised_shape_a, normalised_shape_b) k = np.kron(a, b) assert np.array_equal(k.shape, expected_shape), "Unexpected shape from kron"
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 @instantiate_parametrized_tests class TestKron(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_tile_one_repetition_on_array_gh4679
def test_tile_one_repetition_on_array_gh4679(self): a = np.arange(5) b = tile(a, 1) b += 2 assert_equal(a, np.arange(5))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTile(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_empty
def test_empty(self): """Test everything is ok with empty results, even with inserted dims""" a = np.ones((3, 4, 5)) ai = np.ones((3, 0, 5), dtype=np.intp) actual = take_along_axis(a, ai, axis=1) assert_equal(actual.shape, ai.shape)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTakeAlongAxis(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_kroncompare
def test_kroncompare(self): reps = [(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)] shape = [(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)] for s in shape: b = randint(0, 10, size=s) for r in reps: a = np.ones(r, b.dtype) large = tile(b, r) klarge = kron(a, b) assert_equal(large, klarge)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestTile(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
compare_results
def compare_results(res, desired): """Compare lists of arrays.""" if len(res) != len(desired): raise ValueError("Iterables have different lengths") # See also PEP 618 for Python 3.10 for x, y in zip(res, desired): assert_array_equal(x, y) if __name__ == "__main__": run_tests()
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
get_mat
def get_mat(n): data = np.arange(n) # data = np.add.outer(data, data) data = data[:, None] + data[None, :] return data class TestEye(TestCase): def test_basic(self): assert_equal( eye(4), array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) ) assert_equal( eye(4, dtype="f"), array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], "f"), ) assert_equal(eye(3) == 1, eye(3, dtype=bool)) def test_diag(self): assert_equal( eye(4, k=1), array([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]) ) assert_equal( eye(4, k=-1), array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]), ) def test_2d(self): assert_equal(eye(4, 3), array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])) assert_equal(eye(3, 4), array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])) def test_diag2d(self): assert_equal(eye(3, 4, k=2), array([[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]])) assert_equal( eye(4, 3, k=-2), array([[0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 1, 0]]) ) def test_eye_bounds(self): assert_equal(eye(2, 2, 1), [[0, 1], [0, 0]]) assert_equal(eye(2, 2, -1), [[0, 0], [1, 0]]) assert_equal(eye(2, 2, 2), [[0, 0], [0, 0]]) assert_equal(eye(2, 2, -2), [[0, 0], [0, 0]]) assert_equal(eye(3, 2, 2), [[0, 0], [0, 0], [0, 0]]) assert_equal(eye(3, 2, 1), [[0, 1], [0, 0], [0, 0]]) assert_equal(eye(3, 2, -1), [[0, 0], [1, 0], [0, 1]]) assert_equal(eye(3, 2, -2), [[0, 0], [0, 0], [1, 0]]) assert_equal(eye(3, 2, -3), [[0, 0], [0, 0], [0, 0]]) def test_bool(self): assert_equal(eye(2, 2, dtype=bool), [[True, False], [False, True]]) @xpassIfTorchDynamo # (reason="TODO: implement order=non-default") def test_order(self): mat_c = eye(4, 3, k=-1) mat_f = eye(4, 3, k=-1, order="F") assert_equal(mat_c, mat_f) assert mat_c.flags.c_contiguous assert not mat_c.flags.f_contiguous assert not mat_f.flags.c_contiguous assert mat_f.flags.f_contiguous class TestDiag(TestCase): def test_vector(self): vals = (100 * arange(5)).astype("l") b = zeros((5, 5)) for k in range(5): b[k, k] = vals[k] assert_equal(diag(vals), b) b = zeros((7, 7)) c = b.copy() for k in range(5): b[k, k + 2] = vals[k] c[k + 2, k] = vals[k] assert_equal(diag(vals, k=2), b) assert_equal(diag(vals, k=-2), c) def test_matrix(self): self.check_matrix(vals=(100 * get_mat(5) + 1).astype("l")) def check_matrix(self, vals): b = zeros((5,)) for k in range(5): b[k] = vals[k, k] assert_equal(diag(vals), b) b = b * 0 for k in range(3): b[k] = vals[k, k + 2] assert_equal(diag(vals, 2), b[:3]) for k in range(3): b[k] = vals[k + 2, k] assert_equal(diag(vals, -2), b[:3]) @xpassIfTorchDynamo # (reason="TODO implement orders") def test_fortran_order(self): vals = array((100 * get_mat(5) + 1), order="F", dtype="l") self.check_matrix(vals) def test_diag_bounds(self): A = [[1, 2], [3, 4], [5, 6]] assert_equal(diag(A, k=2), []) assert_equal(diag(A, k=1), [2]) assert_equal(diag(A, k=0), [1, 4]) assert_equal(diag(A, k=-1), [3, 6]) assert_equal(diag(A, k=-2), [5]) assert_equal(diag(A, k=-3), []) def test_failure(self): assert_raises((ValueError, RuntimeError), diag, [[[1]]]) class TestFliplr(TestCase): def test_basic(self): assert_raises((ValueError, RuntimeError), fliplr, ones(4)) a = get_mat(4) # b = a[:, ::-1] b = np.flip(a, 1) assert_equal(fliplr(a), b) a = [[0, 1, 2], [3, 4, 5]] b = [[2, 1, 0], [5, 4, 3]] assert_equal(fliplr(a), b) class TestFlipud(TestCase): def test_basic(self): a = get_mat(4) # b = a[::-1, :] b = np.flip(a, 0) assert_equal(flipud(a), b) a = [[0, 1, 2], [3, 4, 5]] b = [[3, 4, 5], [0, 1, 2]] assert_equal(flipud(a), b) @instantiate_parametrized_tests class TestHistogram2d(TestCase): def test_simple(self): x = array([0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891]) y = array([0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673]) xedges = np.linspace(0, 1, 10) yedges = np.linspace(0, 1, 10) H = histogram2d(x, y, (xedges, yedges))[0] answer = array( [ [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ] ) assert_array_equal(H.T, answer) H = histogram2d(x, y, xedges)[0] assert_array_equal(H.T, answer) H, xedges, yedges = histogram2d(list(range(10)), list(range(10))) assert_array_equal(H, eye(10, 10)) assert_array_equal(xedges, np.linspace(0, 9, 11)) assert_array_equal(yedges, np.linspace(0, 9, 11)) def test_asym(self): x = array([1, 1, 2, 3, 4, 4, 4, 5]) y = array([1, 3, 2, 0, 1, 2, 3, 4]) H, xed, yed = histogram2d(x, y, (6, 5), range=[[0, 6], [0, 5]], density=True) answer = array( [ [0.0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 1], ] ) assert_array_almost_equal(H, answer / 8.0, 3) assert_array_equal(xed, np.linspace(0, 6, 7)) assert_array_equal(yed, np.linspace(0, 5, 6)) def test_density(self): x = array([1, 2, 3, 1, 2, 3, 1, 2, 3]) y = array([1, 1, 1, 2, 2, 2, 3, 3, 3]) H, xed, yed = histogram2d(x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], density=True) answer = array([[1, 1, 0.5], [1, 1, 0.5], [0.5, 0.5, 0.25]]) / 9.0 assert_array_almost_equal(H, answer, 3) def test_all_outliers(self): r = np.random.rand(100) + 1.0 + 1e6 # histogramdd rounds by decimal=6 H, xed, yed = histogram2d(r, r, (4, 5), range=([0, 1], [0, 1])) assert_array_equal(H, 0) def test_empty(self): a, edge1, edge2 = histogram2d([], [], bins=([0, 1], [0, 1])) # assert_array_max_ulp(a, array([[0.]])) assert_allclose(a, np.array([[0.0]]), atol=1e-15) a, edge1, edge2 = histogram2d([], [], bins=4) # assert_array_max_ulp(a, np.zeros((4, 4))) assert_allclose(a, np.zeros((4, 4)), atol=1e-15) @xpassIfTorchDynamo # (reason="pytorch does not support bins = [int, array]") def test_binparameter_combination(self): x = array([0, 0.09207008, 0.64575234, 0.12875982, 0.47390599, 0.59944483, 1]) y = array([0, 0.14344267, 0.48988575, 0.30558665, 0.44700682, 0.15886423, 1]) edges = (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) H, xe, ye = histogram2d(x, y, (edges, 4)) answer = array( [ [2.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], ] ) assert_array_equal(H, answer) assert_array_equal(ye, array([0.0, 0.25, 0.5, 0.75, 1])) H, xe, ye = histogram2d(x, y, (4, edges)) answer = array( [ [1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], ] ) assert_array_equal(H, answer) assert_array_equal(xe, array([0.0, 0.25, 0.5, 0.75, 1])) @skip(reason="NP_VER: fails on CI with older NumPy") @parametrize("x_len, y_len", [(10, 11), (20, 19)]) def test_bad_length(self, x_len, y_len): x, y = np.ones(x_len), np.ones(y_len) with pytest.raises(ValueError, match="x and y must have the same length."): histogram2d(x, y) class TestTri(TestCase): def test_dtype(self): out = array([[1, 0, 0], [1, 1, 0], [1, 1, 1]]) assert_array_equal(tri(3), out) assert_array_equal(tri(3, dtype=bool), out.astype(bool)) def test_tril_triu_ndim2(self): for dtype in np.typecodes["AllFloat"] + np.typecodes["AllInteger"]: a = np.ones((2, 2), dtype=dtype) b = np.tril(a) c = np.triu(a) assert_array_equal(b, [[1, 0], [1, 1]]) assert_array_equal(c, b.T) # should return the same dtype as the original array assert_equal(b.dtype, a.dtype) assert_equal(c.dtype, a.dtype) def test_tril_triu_ndim3(self): for dtype in np.typecodes["AllFloat"] + np.typecodes["AllInteger"]: a = np.array( [ [[1, 1], [1, 1]], [[1, 1], [1, 0]], [[1, 1], [0, 0]], ], dtype=dtype, ) a_tril_desired = np.array( [ [[1, 0], [1, 1]], [[1, 0], [1, 0]], [[1, 0], [0, 0]], ], dtype=dtype, ) a_triu_desired = np.array( [ [[1, 1], [0, 1]], [[1, 1], [0, 0]], [[1, 1], [0, 0]], ], dtype=dtype, ) a_triu_observed = np.triu(a) a_tril_observed = np.tril(a) assert_array_equal(a_triu_observed, a_triu_desired) assert_array_equal(a_tril_observed, a_tril_desired) assert_equal(a_triu_observed.dtype, a.dtype) assert_equal(a_tril_observed.dtype, a.dtype) def test_tril_triu_with_inf(self): # Issue 4859 arr = np.array([[1, 1, np.inf], [1, 1, 1], [np.inf, 1, 1]]) out_tril = np.array([[1, 0, 0], [1, 1, 0], [np.inf, 1, 1]]) out_triu = out_tril.T assert_array_equal(np.triu(arr), out_triu) assert_array_equal(np.tril(arr), out_tril) def test_tril_triu_dtype(self): # Issue 4916 # tril and triu should return the same dtype as input for c in "efdFDBbhil?": # np.typecodes["All"]: arr = np.zeros((3, 3), dtype=c) assert_equal(np.triu(arr).dtype, arr.dtype) assert_equal(np.tril(arr).dtype, arr.dtype) @xfail # (reason="TODO: implement mask_indices") def test_mask_indices(self): # simple test without offset iu = mask_indices(3, np.triu) a = np.arange(9).reshape(3, 3) assert_array_equal(a[iu], array([0, 1, 2, 4, 5, 8])) # Now with an offset iu1 = mask_indices(3, np.triu, 1) assert_array_equal(a[iu1], array([1, 2, 5])) @xfail # (reason="np.tril_indices == our tuple(tril_indices)") def test_tril_indices(self): # indices without and with offset il1 = tril_indices(4) il2 = tril_indices(4, k=2) il3 = tril_indices(4, m=5) il4 = tril_indices(4, k=2, m=5) a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) b = np.arange(1, 21).reshape(4, 5) # indexing: assert_array_equal(a[il1], array([1, 5, 6, 9, 10, 11, 13, 14, 15, 16])) assert_array_equal(b[il3], array([1, 6, 7, 11, 12, 13, 16, 17, 18, 19])) # And for assigning values: a[il1] = -1 assert_array_equal( a, array([[-1, 2, 3, 4], [-1, -1, 7, 8], [-1, -1, -1, 12], [-1, -1, -1, -1]]), ) b[il3] = -1 assert_array_equal( b, array( [ [-1, 2, 3, 4, 5], [-1, -1, 8, 9, 10], [-1, -1, -1, 14, 15], [-1, -1, -1, -1, 20], ] ), ) # These cover almost the whole array (two diagonals right of the main one): a[il2] = -10 assert_array_equal( a, array( [ [-10, -10, -10, 4], [-10, -10, -10, -10], [-10, -10, -10, -10], [-10, -10, -10, -10], ] ), ) b[il4] = -10 assert_array_equal( b, array( [ [-10, -10, -10, 4, 5], [-10, -10, -10, -10, 10], [-10, -10, -10, -10, -10], [-10, -10, -10, -10, -10], ] ), ) @xfail # (reason="np.triu_indices == our tuple(triu_indices)") class TestTriuIndices(TestCase): def test_triu_indices(self): iu1 = triu_indices(4) iu2 = triu_indices(4, k=2) iu3 = triu_indices(4, m=5) iu4 = triu_indices(4, k=2, m=5) a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) b = np.arange(1, 21).reshape(4, 5) # Both for indexing: assert_array_equal(a[iu1], array([1, 2, 3, 4, 6, 7, 8, 11, 12, 16])) assert_array_equal( b[iu3], array([1, 2, 3, 4, 5, 7, 8, 9, 10, 13, 14, 15, 19, 20]) ) # And for assigning values: a[iu1] = -1 assert_array_equal( a, array( [[-1, -1, -1, -1], [5, -1, -1, -1], [9, 10, -1, -1], [13, 14, 15, -1]] ), ) b[iu3] = -1 assert_array_equal( b, array( [ [-1, -1, -1, -1, -1], [6, -1, -1, -1, -1], [11, 12, -1, -1, -1], [16, 17, 18, -1, -1], ] ), ) # These cover almost the whole array (two diagonals right of the # main one): a[iu2] = -10 assert_array_equal( a, array( [ [-1, -1, -10, -10], [5, -1, -1, -10], [9, 10, -1, -1], [13, 14, 15, -1], ] ), ) b[iu4] = -10 assert_array_equal( b, array( [ [-1, -1, -10, -10, -10], [6, -1, -1, -10, -10], [11, 12, -1, -1, -10], [16, 17, 18, -1, -1], ] ), ) class TestTrilIndicesFrom(TestCase): def test_exceptions(self): assert_raises(ValueError, tril_indices_from, np.ones((2,))) assert_raises(ValueError, tril_indices_from, np.ones((2, 2, 2))) # assert_raises(ValueError, tril_indices_from, np.ones((2, 3))) class TestTriuIndicesFrom(TestCase): def test_exceptions(self): assert_raises(ValueError, triu_indices_from, np.ones((2,))) assert_raises(ValueError, triu_indices_from, np.ones((2, 2, 2))) # assert_raises(ValueError, triu_indices_from, np.ones((2, 3))) class TestVander(TestCase): def test_basic(self): c = np.array([0, 1, -2, 3]) v = vander(c) powers = np.array( [[0, 0, 0, 0, 1], [1, 1, 1, 1, 1], [16, -8, 4, -2, 1], [81, 27, 9, 3, 1]] ) # Check default value of N: assert_array_equal(v, powers[:, 1:]) # Check a range of N values, including 0 and 5 (greater than default) m = powers.shape[1] for n in range(6): v = vander(c, N=n) assert_array_equal(v, powers[:, m - n : m]) def test_dtypes(self): c = array([11, -12, 13], dtype=np.int8) v = vander(c) expected = np.array([[121, 11, 1], [144, -12, 1], [169, 13, 1]]) assert_array_equal(v, expected) c = array([1.0 + 1j, 1.0 - 1j]) v = vander(c, N=3) expected = np.array([[2j, 1 + 1j, 1], [-2j, 1 - 1j, 1]]) # The data is floating point, but the values are small integers, # so assert_array_equal *should* be safe here (rather than, say, # assert_array_almost_equal). assert_array_equal(v, expected) if __name__ == "__main__": run_tests()
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True)
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_basic
def test_basic(self): assert_equal( eye(4), array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) ) assert_equal( eye(4, dtype="f"), array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], "f"), ) assert_equal(eye(3) == 1, eye(3, dtype=bool))
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestEye(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_0D_array
def test_0D_array(self): a = np.array(1) b = np.array(2) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_1D_array
def test_1D_array(self): a = np.array([1]) b = np.array([2]) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_2D_array
def test_2D_array(self): a = np.array([[1], [2]]) b = np.array([[1], [2]]) res = dstack([a, b]) desired = np.array( [ [[1, 1]], [ [ 2, 2, ] ], ] ) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_3D_array
def test_3D_array(self): a = np.array([[[1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4]]]) res = dsplit(a, 2) desired = [ np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]]), np.array([[[3, 4], [3, 4]], [[3, 4], [3, 4]]]), ] compare_results(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDsplit(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_basic_2
def test_basic_2(self): aa = np.ones((3, 1, 4, 1, 1)) assert aa.squeeze().tensor._base is aa.tensor
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestSqueeze(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_squeeze_axis
def test_squeeze_axis(self): A = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]]] assert_equal(np.squeeze(A).shape, (3, 3)) assert_equal(np.squeeze(A, axis=()), A) assert_equal(np.squeeze(np.zeros((1, 3, 1))).shape, (3,)) assert_equal(np.squeeze(np.zeros((1, 3, 1)), axis=0).shape, (3, 1)) assert_equal(np.squeeze(np.zeros((1, 3, 1)), axis=-1).shape, (1, 3)) assert_equal(np.squeeze(np.zeros((1, 3, 1)), axis=2).shape, (1, 3)) assert_equal(np.squeeze([np.zeros((3, 1))]).shape, (3,)) assert_equal(np.squeeze([np.zeros((3, 1))], axis=0).shape, (3, 1)) assert_equal(np.squeeze([np.zeros((3, 1))], axis=2).shape, (1, 3)) assert_equal(np.squeeze([np.zeros((3, 1))], axis=-1).shape, (1, 3))
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestSqueeze(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_squeeze_axis_handling
def test_squeeze_axis_handling(self): with assert_raises(ValueError): np.squeeze(np.array([[1], [2], [3]]), axis=0)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestSqueeze(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_diag
def test_diag(self): assert_equal( eye(4, k=1), array([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]) ) assert_equal( eye(4, k=-1), array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]), )
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestEye(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_2d
def test_2d(self): assert_equal(eye(4, 3), array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])) assert_equal(eye(3, 4), array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]))
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestEye(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_diag2d
def test_diag2d(self): assert_equal(eye(3, 4, k=2), array([[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]])) assert_equal( eye(4, 3, k=-2), array([[0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 1, 0]]) )
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestEye(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_eye_bounds
def test_eye_bounds(self): assert_equal(eye(2, 2, 1), [[0, 1], [0, 0]]) assert_equal(eye(2, 2, -1), [[0, 0], [1, 0]]) assert_equal(eye(2, 2, 2), [[0, 0], [0, 0]]) assert_equal(eye(2, 2, -2), [[0, 0], [0, 0]]) assert_equal(eye(3, 2, 2), [[0, 0], [0, 0], [0, 0]]) assert_equal(eye(3, 2, 1), [[0, 1], [0, 0], [0, 0]]) assert_equal(eye(3, 2, -1), [[0, 0], [1, 0], [0, 1]]) assert_equal(eye(3, 2, -2), [[0, 0], [0, 0], [1, 0]]) assert_equal(eye(3, 2, -3), [[0, 0], [0, 0], [0, 0]])
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestEye(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_order
def test_order(self): mat_c = eye(4, 3, k=-1) mat_f = eye(4, 3, k=-1, order="F") assert_equal(mat_c, mat_f) assert mat_c.flags.c_contiguous assert not mat_c.flags.f_contiguous assert not mat_f.flags.c_contiguous assert mat_f.flags.f_contiguous
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestEye(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_vector
def test_vector(self): vals = (100 * arange(5)).astype("l") b = zeros((5, 5)) for k in range(5): b[k, k] = vals[k] assert_equal(diag(vals), b) b = zeros((7, 7)) c = b.copy() for k in range(5): b[k, k + 2] = vals[k] c[k + 2, k] = vals[k] assert_equal(diag(vals, k=2), b) assert_equal(diag(vals, k=-2), c)
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestDiag(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_matrix
def test_matrix(self): self.check_matrix(vals=(100 * get_mat(5) + 1).astype("l"))
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestDiag(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_fortran_order
def test_fortran_order(self): vals = array((100 * get_mat(5) + 1), order="F", dtype="l") self.check_matrix(vals)
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestDiag(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_generator
def test_generator(self): # numpy 1.24 emits a warning but we don't # with assert_warns(FutureWarning): column_stack([np.arange(3) for _ in range(2)])
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_non_iterable
def test_non_iterable(self): assert_raises(TypeError, column_stack, 1)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_0D_array
def test_0D_array(self): a = np.array(1) b = np.array(2) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_1D_array
def test_1D_array(self): a = np.array([1]) b = np.array([2]) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_2D_array
def test_2D_array(self): a = np.array([[1], [2]]) b = np.array([[1], [2]]) res = dstack([a, b]) desired = np.array( [ [[1, 1]], [ [ 2, 2, ] ], ] ) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_non_iterable
def test_non_iterable(self): assert_raises(TypeError, column_stack, 1)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_0D_array
def test_0D_array(self): a = np.array(1) b = np.array(2) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_1D_array
def test_1D_array(self): a = np.array([1]) b = np.array([2]) res = dstack([a, b]) desired = np.array([[[1, 2]]]) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_2D_array
def test_2D_array(self): a = np.array([[1], [2]]) b = np.array([[1], [2]]) res = dstack([a, b]) desired = np.array( [ [[1, 1]], [ [ 2, 2, ] ], ] ) assert_array_equal(res, desired)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestDstack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_shape_base_.py
test_non_iterable
def test_non_iterable(self): assert_raises(TypeError, column_stack, 1)
import functools import sys from unittest import expectedFailure as xfail, skipIf as skipif from pytest import raises as assert_raises from torch.testing._internal.common_utils import ( instantiate_parametrized_tests, parametrize, run_tests, TEST_WITH_TORCHDYNAMO, TestCase, xfailIfTorchDynamo, xpassIfTorchDynamo, ) import numpy as np from numpy import ( apply_along_axis, array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from numpy.random import rand, randint from numpy.testing import assert_, assert_array_equal, assert_equal import torch._numpy as np from torch._numpy import ( array_split, column_stack, dsplit, dstack, expand_dims, hsplit, kron, put_along_axis, split, take_along_axis, tile, vsplit, ) from torch._numpy.random import rand, randint from torch._numpy.testing import assert_, assert_array_equal, assert_equal skip = functools.partial(skipif, True) IS_64BIT = sys.maxsize > 2**32 class TestColumnStack(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_bad_length
def test_bad_length(self, x_len, y_len): x, y = np.ones(x_len), np.ones(y_len) with pytest.raises(ValueError, match="x and y must have the same length."): histogram2d(x, y)
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) @instantiate_parametrized_tests class TestHistogram2d(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_dtype
def test_dtype(self): out = array([[1, 0, 0], [1, 1, 0], [1, 1, 1]]) assert_array_equal(tri(3), out) assert_array_equal(tri(3, dtype=bool), out.astype(bool))
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestTri(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added
torch
test/torch_np/numpy_tests/lib/test_twodim_base.py
test_tril_triu_ndim2
def test_tril_triu_ndim2(self): for dtype in np.typecodes["AllFloat"] + np.typecodes["AllInteger"]: a = np.ones((2, 2), dtype=dtype) b = np.tril(a) c = np.triu(a) assert_array_equal(b, [[1, 0], [1, 1]]) assert_array_equal(c, b.T) # should return the same dtype as the original array assert_equal(b.dtype, a.dtype) assert_equal(c.dtype, a.dtype)
import functools from unittest import expectedFailure as xfail, skipIf as skipif 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 ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) import torch._numpy as np from torch._numpy import ( arange, array, diag, eye, fliplr, flipud, histogram2d, ones, tri, tril_indices, tril_indices_from, triu_indices, triu_indices_from, vander, zeros, ) from torch._numpy.testing import ( assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal, ) skip = functools.partial(skipif, True) class TestTri(TestCase):
c263bd43e8e8502d4726643bc6fd046f0130ac0e
32f585d9346e316e554c8d9bf7548af9f62141fc
added