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/test_ndarray_methods.py
|
test_transpose_method
|
def test_transpose_method(self):
a = np.array([[1, 2], [3, 4]])
assert_equal(a.transpose(), [[1, 3], [2, 4]])
assert_equal(a.transpose(None), [[1, 3], [2, 4]])
assert_raises((RuntimeError, ValueError), lambda: a.transpose(0))
assert_raises((RuntimeError, ValueError), lambda: a.transpose(0, 0))
assert_raises((RuntimeError, ValueError), lambda: a.transpose(0, 1, 2))
assert a.transpose().tensor._base is a.tensor
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestTranspose(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_iter_1d
|
def test_iter_1d(self):
# numpy generates array scalars, we do 0D arrays
a = np.arange(5)
lst = list(a)
assert all(type(x) == np.ndarray for x in lst), f"{[type(x) for x in lst]}"
assert all(x.ndim == 0 for x in lst)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestIter(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_iter_2d
|
def test_iter_2d(self):
# numpy iterates over the 0th axis
a = np.arange(5)[None, :]
lst = list(a)
assert len(lst) == 1
assert type(lst[0]) == np.ndarray
assert_equal(lst[0], np.arange(5))
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestIter(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_nep50_examples.py
|
test_nep50_exceptions
|
def test_nep50_exceptions(self, example):
old, new = examples[example]
if new == Exception:
with assert_raises(OverflowError):
eval(example)
else:
result = eval(example)
if new is unchanged:
new = old
assert_allclose(result, new, atol=1e-16)
assert result.dtype == new.dtype
# ### Directly compare to numpy ###
|
import itertools
from unittest import skipIf as skipif, SkipTest
import numpy as _np
import torch._numpy as tnp
from torch._numpy import ( # noqa: F401
array,
bool_,
complex128,
complex64,
float32,
float64,
inf,
int16,
int32,
int64,
uint8,
)
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TestCase,
)
uint16 = uint8 # can be anything here, see below
from pytest import raises as assert_raises
unchanged = None
examples = {
"uint8(1) + 2": (int64(3), uint8(3)),
"array([1], uint8) + int64(1)": (array([2], uint8), array([2], int64)),
"array([1], uint8) + array(1, int64)": (array([2], uint8), array([2], int64)),
"array([1.], float32) + float64(1.)": (
array([2.0], float32),
array([2.0], float64),
),
"array([1.], float32) + array(1., float64)": (
array([2.0], float32),
array([2.0], float64),
),
"array([1], uint8) + 1": (array([2], uint8), unchanged),
"array([1], uint8) + 200": (array([201], uint8), unchanged),
"array([100], uint8) + 200": (array([44], uint8), unchanged),
"array([1], uint8) + 300": (array([301], uint16), Exception),
"uint8(1) + 300": (int64(301), Exception),
"uint8(100) + 200": (int64(301), uint8(44)), # and RuntimeWarning
"float32(1) + 3e100": (float64(3e100), float32(inf)), # and RuntimeWarning [T7]
"array([1.0], float32) + 1e-14 == 1.0": (array([True]), unchanged),
"array([0.1], float32) == float64(0.1)": (array([True]), array([False])),
"array(1.0, float32) + 1e-14 == 1.0": (array(False), array(True)),
"array([1.], float32) + 3": (array([4.0], float32), unchanged),
"array([1.], float32) + int64(3)": (array([4.0], float32), array([4.0], float64)),
"3j + array(3, complex64)": (array(3 + 3j, complex128), array(3 + 3j, complex64)),
"float32(1) + 1j": (array(1 + 1j, complex128), array(1 + 1j, complex64)),
"int32(1) + 5j": (array(1 + 5j, complex128), unchanged),
# additional examples from the NEP text
"int16(2) + 2": (int64(4), int16(4)),
"int16(4) + 4j": (complex128(4 + 4j), unchanged),
"float32(5) + 5j": (complex128(5 + 5j), complex64(5 + 5j)),
"bool_(True) + 1": (int64(2), unchanged),
"True + uint8(2)": (uint8(3), unchanged),
}
@skipif(not HAVE_NUMPY, reason="NumPy not found")
@instantiate_parametrized_tests
class TestNEP50Table(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_nep50_examples.py
|
test_compare_ufuncs
|
def test_compare_ufuncs(self, name, scalar, array):
if name in corners and (
array.dtype.name in corners[name]
or tnp.asarray(scalar).dtype.name in corners[name]
):
raise SkipTest(f"{name}(..., dtype=array.dtype)")
try:
state = _np._get_promotion_state()
_np._set_promotion_state("weak")
if name in ["matmul", "modf", "divmod", "ldexp"]:
return
ufunc = getattr(tnp, name)
ufunc_numpy = getattr(_np, name)
try:
result = ufunc(scalar, array)
except RuntimeError:
# RuntimeError: "bitwise_xor_cpu" not implemented for 'ComplexDouble' etc
result = None
try:
result_numpy = ufunc_numpy(scalar, array.tensor.numpy())
except TypeError:
# TypeError: ufunc 'hypot' not supported for the input types
result_numpy = None
if result is not None and result_numpy is not None:
assert result.tensor.numpy().dtype == result_numpy.dtype
finally:
_np._set_promotion_state(state)
|
import itertools
from unittest import skipIf as skipif, SkipTest
import numpy as _np
import torch._numpy as tnp
from torch._numpy import ( # noqa: F401
array,
bool_,
complex128,
complex64,
float32,
float64,
inf,
int16,
int32,
int64,
uint8,
)
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TestCase,
)
uint16 = uint8 # can be anything here, see below
from pytest import raises as assert_raises
unchanged = None
examples = {
"uint8(1) + 2": (int64(3), uint8(3)),
"array([1], uint8) + int64(1)": (array([2], uint8), array([2], int64)),
"array([1], uint8) + array(1, int64)": (array([2], uint8), array([2], int64)),
"array([1.], float32) + float64(1.)": (
array([2.0], float32),
array([2.0], float64),
),
"array([1.], float32) + array(1., float64)": (
array([2.0], float32),
array([2.0], float64),
),
"array([1], uint8) + 1": (array([2], uint8), unchanged),
"array([1], uint8) + 200": (array([201], uint8), unchanged),
"array([100], uint8) + 200": (array([44], uint8), unchanged),
"array([1], uint8) + 300": (array([301], uint16), Exception),
"uint8(1) + 300": (int64(301), Exception),
"uint8(100) + 200": (int64(301), uint8(44)), # and RuntimeWarning
"float32(1) + 3e100": (float64(3e100), float32(inf)), # and RuntimeWarning [T7]
"array([1.0], float32) + 1e-14 == 1.0": (array([True]), unchanged),
"array([0.1], float32) == float64(0.1)": (array([True]), array([False])),
"array(1.0, float32) + 1e-14 == 1.0": (array(False), array(True)),
"array([1.], float32) + 3": (array([4.0], float32), unchanged),
"array([1.], float32) + int64(3)": (array([4.0], float32), array([4.0], float64)),
"3j + array(3, complex64)": (array(3 + 3j, complex128), array(3 + 3j, complex64)),
"float32(1) + 1j": (array(1 + 1j, complex128), array(1 + 1j, complex64)),
"int32(1) + 5j": (array(1 + 5j, complex128), unchanged),
# additional examples from the NEP text
"int16(2) + 2": (int64(4), int16(4)),
"int16(4) + 4j": (complex128(4 + 4j), unchanged),
"float32(5) + 5j": (complex128(5 + 5j), complex64(5 + 5j)),
"bool_(True) + 1": (int64(2), unchanged),
"True + uint8(2)": (uint8(3), unchanged),
}
weaks = (True, 1, 2.0, 3j)
non_weaks = (
tnp.asarray(True),
tnp.uint8(1),
tnp.int8(1),
tnp.int32(1),
tnp.int64(1),
tnp.float32(1),
tnp.float64(1),
tnp.complex64(1),
tnp.complex128(1),
)
corners = {
"true_divide": ["bool_", "uint8", "int8", "int16", "int32", "int64"],
"divide": ["bool_", "uint8", "int8", "int16", "int32", "int64"],
"arctan2": ["bool_", "uint8", "int8", "int16", "int32", "int64"],
"copysign": ["bool_", "uint8", "int8", "int16", "int32", "int64"],
"heaviside": ["bool_", "uint8", "int8", "int16", "int32", "int64"],
"ldexp": ["bool_", "uint8", "int8", "int16", "int32", "int64"],
"power": ["uint8"],
"nextafter": ["float32"],
}
@skipif(not HAVE_NUMPY, reason="NumPy not found")
@instantiate_parametrized_tests
class TestCompareToNumpy(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_random.py
|
control_stream
|
def control_stream(use_numpy=False):
with config.patch(use_numpy_random_stream=use_numpy):
yield
@instantiate_parametrized_tests
class TestScalarReturn(TestCase):
@parametrize("use_numpy", [True, False])
@parametrize(
"func",
[
tnp.random.normal,
tnp.random.rand,
partial(tnp.random.randint, 0, 5),
tnp.random.randn,
subtest(tnp.random.random, name="random_random"),
subtest(tnp.random.random_sample, name="random_sample"),
tnp.random.sample,
tnp.random.uniform,
],
)
def test_rndm_scalar(self, func, use_numpy):
# default `size` means a python scalar return
with control_stream(use_numpy):
r = func()
assert isinstance(r, (int, float))
@parametrize("use_numpy", [True, False])
@parametrize(
"func",
[
tnp.random.normal,
tnp.random.rand,
partial(tnp.random.randint, 0, 5),
tnp.random.randn,
subtest(tnp.random.random, name="random_random"),
subtest(tnp.random.random_sample, name="random_sample"),
tnp.random.sample,
tnp.random.uniform,
],
)
def test_rndm_array(self, func, use_numpy):
with control_stream(use_numpy):
if func in (tnp.random.rand, tnp.random.randn):
r = func(10)
else:
r = func(size=10)
assert isinstance(r, tnp.ndarray)
@instantiate_parametrized_tests
class TestShuffle(TestCase):
@parametrize("use_numpy", [True, False])
def test_1d(self, use_numpy):
ax = tnp.asarray([1, 2, 3, 4, 5, 6])
ox = ax.copy()
tnp.random.seed(1234)
tnp.random.shuffle(ax)
assert isinstance(ax, tnp.ndarray)
assert not (ax == ox).all()
@parametrize("use_numpy", [True, False])
def test_2d(self, use_numpy):
# np.shuffle only shuffles the first axis
ax = tnp.asarray([[1, 2, 3], [4, 5, 6]])
ox = ax.copy()
tnp.random.seed(1234)
tnp.random.shuffle(ax)
assert isinstance(ax, tnp.ndarray)
assert not (ax == ox).all()
@parametrize("use_numpy", [True, False])
def test_shuffle_list(self, use_numpy):
# on eager, we refuse to shuffle lists
# under dynamo, we always fall back to numpy
# NB: this means that the random stream is different for
# shuffling a list or an array when USE_NUMPY_STREAM == False
x = [1, 2, 3]
with pytest.raises(NotImplementedError):
tnp.random.shuffle(x)
@instantiate_parametrized_tests
class TestChoice(TestCase):
@parametrize("use_numpy", [True, False])
def test_choice(self, use_numpy):
kwds = dict(size=3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
with control_stream(use_numpy):
tnp.random.seed(12345)
x = tnp.random.choice(5, **kwds)
tnp.random.seed(12345)
x_1 = tnp.random.choice(tnp.arange(5), **kwds)
assert_equal(x, x_1)
class TestNumpyGlobal(TestCase):
def test_numpy_global(self):
with control_stream(use_numpy=True):
tnp.random.seed(12345)
x = tnp.random.uniform(0, 1, size=11)
# check that the stream is identical to numpy's
_np.random.seed(12345)
x_np = _np.random.uniform(0, 1, size=11)
assert_equal(x, tnp.asarray(x_np))
# switch to the pytorch stream, variates differ
with control_stream(use_numpy=False):
tnp.random.seed(12345)
x_1 = tnp.random.uniform(0, 1, size=11)
assert not (x_1 == x).all()
if __name__ == "__main__":
run_tests()
|
from contextlib import contextmanager
from functools import partial
import numpy as _np
import pytest
import torch._dynamo.config as config
import torch._numpy as tnp
from torch._numpy.testing import assert_equal
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TestCase,
)
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_random.py
|
test_rndm_array
|
def test_rndm_array(self, func, use_numpy):
with control_stream(use_numpy):
if func in (tnp.random.rand, tnp.random.randn):
r = func(10)
else:
r = func(size=10)
assert isinstance(r, tnp.ndarray)
|
from contextlib import contextmanager
from functools import partial
import numpy as _np
import pytest
import torch._dynamo.config as config
import torch._numpy as tnp
from torch._numpy.testing import assert_equal
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TestCase,
)
@instantiate_parametrized_tests
class TestScalarReturn(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_np_vs_ndarray_positional
|
def test_np_vs_ndarray_positional(self, arr_method, np_method):
a = np.arange(6).reshape((2, 3))
arg_method = getattr(a, arr_method)
# check positional args
out1 = np.zeros(2, dtype=int)
out2 = np.zeros(2, dtype=int)
assert_equal(arg_method(1, out1), np_method(a, 1, out2))
assert_equal(out1, out2)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
@instantiate_parametrized_tests
class TestArgmaxArgminCommon(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_combinations
|
def test_combinations(self, data):
arr, pos = data
# with suppress_warnings() as sup:
# sup.filter(RuntimeWarning,
# "invalid value encountered in reduce")
# if np.asarray(arr).dtype.kind in "c":
# pytest.xfail(reason="'max_values_cpu' not implemented for 'ComplexDouble'")
val = np.max(arr)
assert_equal(np.argmax(arr), pos) # , err_msg="%r" % arr)
assert_equal(arr[np.argmax(arr)], val) # , err_msg="%r" % arr)
# add padding to test SIMD loops
rarr = np.repeat(arr, 129)
rpos = pos * 129
assert_equal(np.argmax(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmax(rarr)], val, err_msg=f"{rarr!r}")
padd = np.repeat(np.min(arr), 513)
rarr = np.concatenate((arr, padd))
rpos = pos
assert_equal(np.argmax(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmax(rarr)], val, err_msg=f"{rarr!r}")
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
@instantiate_parametrized_tests
class TestArgmax(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_maximum_signed_integers
|
def test_maximum_signed_integers(self):
a = np.array([1, 2**7 - 1, -(2**7)], dtype=np.int8)
assert_equal(np.argmax(a), 1)
a = np.array([1, 2**15 - 1, -(2**15)], dtype=np.int16)
assert_equal(np.argmax(a), 1)
a = np.array([1, 2**31 - 1, -(2**31)], dtype=np.int32)
assert_equal(np.argmax(a), 1)
a = np.array([1, 2**63 - 1, -(2**63)], dtype=np.int64)
assert_equal(np.argmax(a), 1)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
@instantiate_parametrized_tests
class TestArgmax(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_combinations
|
def test_combinations(self, data):
arr, pos = data
# with suppress_warnings() as sup:
# sup.filter(RuntimeWarning,
# "invalid value encountered in reduce")
# if np.asarray(arr).dtype.kind in "c":
# pytest.xfail(reason="'max_values_cpu' not implemented for 'ComplexDouble'")
val = np.max(arr)
assert_equal(np.argmax(arr), pos) # , err_msg="%r" % arr)
assert_equal(arr[np.argmax(arr)], val) # , err_msg="%r" % arr)
# add padding to test SIMD loops
rarr = np.repeat(arr, 129)
rpos = pos * 129
assert_equal(np.argmax(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmax(rarr)], val, err_msg=f"{rarr!r}")
padd = np.repeat(np.min(arr), 513)
rarr = np.concatenate((arr, padd))
rpos = pos
assert_equal(np.argmax(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmax(rarr)], val, err_msg=f"{rarr!r}")
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
@instantiate_parametrized_tests
class TestArgmax(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_minimum_signed_integers
|
def test_minimum_signed_integers(self):
a = np.array([1, -(2**7), -(2**7) + 1, 2**7 - 1], dtype=np.int8)
assert_equal(np.argmin(a), 1)
a = np.array([1, -(2**15), -(2**15) + 1, 2**15 - 1], dtype=np.int16)
assert_equal(np.argmin(a), 1)
a = np.array([1, -(2**31), -(2**31) + 1, 2**31 - 1], dtype=np.int32)
assert_equal(np.argmin(a), 1)
a = np.array([1, -(2**63), -(2**63) + 1, 2**63 - 1], dtype=np.int64)
assert_equal(np.argmin(a), 1)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
@instantiate_parametrized_tests
class TestArgmin(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_basic
|
def test_basic(self):
a = [3, 4, 5, 10, -3, -5, 6.0]
assert_equal(np.amax(a), 10.0)
b = [[3, 6.0, 9.0], [4, 10.0, 5.0], [8, 3.0, 2.0]]
assert_equal(np.amax(b, axis=0), [8.0, 10.0, 9.0])
assert_equal(np.amax(b, axis=1), [9.0, 10.0, 8.0])
arr = np.asarray(a)
assert_equal(np.amax(arr), arr.max())
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestAmax(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_basic
|
def test_basic(self):
a = [3, 4, 5, 10, -3, -5, 6.0]
assert_equal(np.amax(a), 10.0)
b = [[3, 6.0, 9.0], [4, 10.0, 5.0], [8, 3.0, 2.0]]
assert_equal(np.amax(b, axis=0), [8.0, 10.0, 9.0])
assert_equal(np.amax(b, axis=1), [9.0, 10.0, 8.0])
arr = np.asarray(a)
assert_equal(np.amax(arr), arr.max())
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestAmax(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_contains
|
def test_contains(self):
a = np.arange(12).reshape(3, 4)
assert 2 in a
assert 42 not in a
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestContains(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_extra_methods
|
def test_extra_methods(self, name):
a = np.ones(3)
with pytest.raises(AttributeError):
getattr(a, name)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
@instantiate_parametrized_tests
class TestNoExtraMethods(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_logical_xor
|
def test_logical_xor(self):
assert_allclose(
np.logical_xor(0.5, 0.6),
logical_xor(0.5, 0.6),
atol=1e-7,
check_dtype=False,
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_matmul
|
def test_matmul(self):
assert_allclose(
np.matmul([0.5], [0.6]), matmul([0.5], [0.6]), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_maximum
|
def test_maximum(self):
assert_allclose(
np.maximum(0.5, 0.6), maximum(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_minimum
|
def test_minimum(self):
assert_allclose(
np.minimum(0.5, 0.6), minimum(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_remainder
|
def test_remainder(self):
assert_allclose(
np.remainder(0.5, 0.6), remainder(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_multiply
|
def test_multiply(self):
assert_allclose(
np.multiply(0.5, 0.6), multiply(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_nextafter
|
def test_nextafter(self):
assert_allclose(
np.nextafter(0.5, 0.6), nextafter(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_not_equal
|
def test_not_equal(self):
assert_allclose(
np.not_equal(0.5, 0.6), not_equal(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_power
|
def test_power(self):
assert_allclose(
np.power(0.5, 0.6), power(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_right_shift
|
def test_right_shift(self):
assert_allclose(
np.right_shift(5, 6), right_shift(5, 6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_hypot
|
def test_hypot(self):
assert_allclose(
np.hypot(0.5, 0.6), hypot(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_lcm
|
def test_lcm(self):
assert_allclose(np.lcm(5, 6), lcm(5, 6), atol=1e-7, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_ldexp
|
def test_ldexp(self):
assert_allclose(np.ldexp(0.5, 6), ldexp(0.5, 6), atol=1e-7, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_left_shift
|
def test_left_shift(self):
assert_allclose(
np.left_shift(5, 6), left_shift(5, 6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_less
|
def test_less(self):
assert_allclose(np.less(0.5, 0.6), less(0.5, 0.6), atol=1e-7, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_less_equal
|
def test_less_equal(self):
assert_allclose(
np.less_equal(0.5, 0.6), less_equal(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_logaddexp
|
def test_logaddexp(self):
assert_allclose(
np.logaddexp(0.5, 0.6), logaddexp(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_logaddexp2
|
def test_logaddexp2(self):
assert_allclose(
np.logaddexp2(0.5, 0.6), logaddexp2(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_logical_and
|
def test_logical_and(self):
assert_allclose(
np.logical_and(0.5, 0.6),
logical_and(0.5, 0.6),
atol=1e-7,
check_dtype=False,
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_binary_ufuncs.py
|
test_logical_or
|
def test_logical_or(self):
assert_allclose(
np.logical_or(0.5, 0.6), logical_or(0.5, 0.6), atol=1e-7, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestBinaryUfuncBasic(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_random.py
|
test_shuffle_list
|
def test_shuffle_list(self, use_numpy):
# on eager, we refuse to shuffle lists
# under dynamo, we always fall back to numpy
# NB: this means that the random stream is different for
# shuffling a list or an array when USE_NUMPY_STREAM == False
x = [1, 2, 3]
with pytest.raises(NotImplementedError):
tnp.random.shuffle(x)
|
from contextlib import contextmanager
from functools import partial
import numpy as _np
import pytest
import torch._dynamo.config as config
import torch._numpy as tnp
from torch._numpy.testing import assert_equal
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TestCase,
)
@instantiate_parametrized_tests
class TestShuffle(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_random.py
|
test_choice
|
def test_choice(self, use_numpy):
kwds = dict(size=3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
with control_stream(use_numpy):
tnp.random.seed(12345)
x = tnp.random.choice(5, **kwds)
tnp.random.seed(12345)
x_1 = tnp.random.choice(tnp.arange(5), **kwds)
assert_equal(x, x_1)
|
from contextlib import contextmanager
from functools import partial
import numpy as _np
import pytest
import torch._dynamo.config as config
import torch._numpy as tnp
from torch._numpy.testing import assert_equal
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TestCase,
)
@instantiate_parametrized_tests
class TestChoice(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_random.py
|
test_numpy_global
|
def test_numpy_global(self):
with control_stream(use_numpy=True):
tnp.random.seed(12345)
x = tnp.random.uniform(0, 1, size=11)
# check that the stream is identical to numpy's
_np.random.seed(12345)
x_np = _np.random.uniform(0, 1, size=11)
assert_equal(x, tnp.asarray(x_np))
# switch to the pytorch stream, variates differ
with control_stream(use_numpy=False):
tnp.random.seed(12345)
x_1 = tnp.random.uniform(0, 1, size=11)
assert not (x_1 == x).all()
|
from contextlib import contextmanager
from functools import partial
import numpy as _np
import pytest
import torch._dynamo.config as config
import torch._numpy as tnp
from torch._numpy.testing import assert_equal
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TestCase,
)
class TestNumpyGlobal(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_basic
|
def test_basic(self):
x = np.arange(-2, 3)
assert_equal(np.flatnonzero(x), [0, 1, 3, 4])
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestFlatnonzero(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_basic
|
def test_basic(self):
x = np.arange(-2, 3)
assert_equal(np.flatnonzero(x), [0, 1, 3, 4])
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestFlatnonzero(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_nd
|
def test_nd(self):
y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]]
assert np.any(y1)
assert_equal(np.any(y1, axis=0), [1, 1, 0])
assert_equal(np.any(y1, axis=1), [0, 1, 1])
assert_equal(np.any(y1), True)
assert isinstance(np.any(y1, axis=1), np.ndarray)
# YYY: deduplicate
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestAny(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_method_vs_function
|
def test_method_vs_function(self):
y = np.array([[0, 1, 0, 3], [1, 0, 2, 0]])
assert_equal(np.any(y), y.any())
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestAny(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_basic
|
def test_basic(self):
x = np.arange(-2, 3)
assert_equal(np.flatnonzero(x), [0, 1, 3, 4])
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestFlatnonzero(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_nd
|
def test_nd(self):
y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]]
assert np.any(y1)
assert_equal(np.any(y1, axis=0), [1, 1, 0])
assert_equal(np.any(y1, axis=1), [0, 1, 1])
assert_equal(np.any(y1), True)
assert isinstance(np.any(y1, axis=1), np.ndarray)
# YYY: deduplicate
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestAny(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_method_vs_function
|
def test_method_vs_function(self):
y = np.array([[0, 1, 0, 3], [1, 0, 2, 0]])
assert_equal(np.any(y), y.any())
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestAny(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_sum_where
|
def test_sum_where(self):
# More extensive tests done in test_reduction_with_where.
assert_equal(np.sum([[1.0, 2.0], [3.0, 4.0]], where=[True, False]), 4.0)
assert_equal(
np.sum([[1.0, 2.0], [3.0, 4.0]], axis=0, initial=5.0, where=[True, False]),
[9.0, 5.0],
)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import warnings
@instantiate_parametrized_tests
class TestSum(TestCase):
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_out_scalar
|
def test_out_scalar(self, func):
# out no axis: scalar
if func in fails_out_arg:
raise SkipTest(f"{func.__name__} does not have out= arg.")
a = np.arange(2 * 3 * 4).reshape((2, 3, 4))
result = func(a)
out = np.empty_like(result)
result_with_out = func(a, out=out)
assert result_with_out is out
assert_array_equal(result, result_with_out)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import warnings
import warnings
parametrize_axis = parametrize(
"axis", [0, 1, 2, -1, -2, (0, 1), (1, 0), (0, 1, 2), (1, -1, 0)]
)
parametrize_func = parametrize(
"func",
[
np.any,
np.all,
np.argmin,
np.argmax,
np.min,
np.max,
np.mean,
np.sum,
np.prod,
np.std,
np.var,
np.count_nonzero,
],
)
fails_axes_tuples = {
np.any,
np.all,
np.argmin,
np.argmax,
np.prod,
}
fails_out_arg = {
np.count_nonzero,
}
restricts_dtype_casts = {np.var, np.std}
fails_empty_tuple = {np.argmin, np.argmax}
@instantiate_parametrized_tests
class TestGenericReductions(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_ravel_method
|
def test_ravel_method(self):
a = np.array([[0, 1], [2, 3]])
assert_equal(a.ravel(), [0, 1, 2, 3])
assert a.ravel().tensor._base is a.tensor
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestRavel(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_nonzero_trivial
|
def test_nonzero_trivial(self):
assert_equal(np.nonzero(np.array([])), ([],))
assert_equal(np.array([]).nonzero(), ([],))
assert_equal(np.nonzero(np.array([0])), ([],))
assert_equal(np.array([0]).nonzero(), ([],))
assert_equal(np.nonzero(np.array([1])), ([0],))
assert_equal(np.array([1]).nonzero(), ([0],))
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestNonzero(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_nonzero_onedim
|
def test_nonzero_onedim(self):
x = np.array([1, 0, 2, -1, 0, 0, 8])
assert_equal(np.nonzero(x), ([0, 2, 3, 6],))
assert_equal(x.nonzero(), ([0, 2, 3, 6],))
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestNonzero(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_nonzero_twodim
|
def test_nonzero_twodim(self):
x = np.array([[0, 1, 0], [2, 0, 3]])
assert_equal(np.nonzero(x), ([0, 1, 1], [1, 0, 2]))
assert_equal(x.nonzero(), ([0, 1, 1], [1, 0, 2]))
x = np.eye(3)
assert_equal(np.nonzero(x), ([0, 1, 2], [0, 1, 2]))
assert_equal(x.nonzero(), ([0, 1, 2], [0, 1, 2]))
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestNonzero(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_sparse
|
def test_sparse(self):
# test special sparse condition boolean code path
for i in range(20):
c = np.zeros(200, dtype=bool)
c[i::20] = True
assert_equal(np.nonzero(c)[0], np.arange(i, 200 + i, 20))
assert_equal(c.nonzero()[0], np.arange(i, 200 + i, 20))
c = np.zeros(400, dtype=bool)
c[10 + i : 20 + i] = True
c[20 + i * 2] = True
assert_equal(
np.nonzero(c)[0],
np.concatenate((np.arange(10 + i, 20 + i), [20 + i * 2])),
)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestNonzero(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ndarray_methods.py
|
test_array_method
|
def test_array_method(self):
# Tests that the array method
# call to nonzero works
m = np.array([[1, 0, 0], [4, 0, 6]])
tgt = [[0, 1, 1], [0, 0, 2]]
assert_equal(m.nonzero(), tgt)
|
import itertools
from unittest import expectedFailure as xfail, skipIf as skipif
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
class TestNonzero(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_keepdims_out
|
def test_keepdims_out(self, func, axis):
if func in fails_out_arg:
raise SkipTest(f"{func.__name__} does not have out= arg.")
if func in fails_axes_tuples:
raise SkipTest(f"{func.__name__} does not hangle tuple axis.")
d = np.ones((3, 5, 7, 11))
if axis is None:
shape_out = (1,) * d.ndim
else:
axis_norm = _util.normalize_axis_tuple(axis, d.ndim)
shape_out = tuple(
1 if i in axis_norm else d.shape[i] for i in range(d.ndim)
)
out = np.empty(shape_out)
result = func(d, axis=axis, keepdims=True, out=out)
assert result is out
assert_equal(result.shape, shape_out)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import warnings
import warnings
parametrize_axis = parametrize(
"axis", [0, 1, 2, -1, -2, (0, 1), (1, 0), (0, 1, 2), (1, -1, 0)]
)
parametrize_func = parametrize(
"func",
[
np.any,
np.all,
np.argmin,
np.argmax,
np.min,
np.max,
np.mean,
np.sum,
np.prod,
np.std,
np.var,
np.count_nonzero,
],
)
fails_axes_tuples = {
np.any,
np.all,
np.argmin,
np.argmax,
np.prod,
}
fails_out_arg = {
np.count_nonzero,
}
restricts_dtype_casts = {np.var, np.std}
fails_empty_tuple = {np.argmin, np.argmax}
@instantiate_parametrized_tests
class TestGenericReductions(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_scalars_0D_arrays.py
|
test_decay_to_py_scalar
|
def test_decay_to_py_scalar(self, value):
# NumPy distinguishes array scalars and 0D arrays. For instance
# `scalar * list` is equivalent to `int(scalar) * list`, but
# `0D array * list` is equivalent to `0D array * np.asarray(list)`.
# Our scalars follow 0D array behavior (because they are 0D arrays)
lst = [1, 2, 3]
product = value * lst
assert isinstance(product, np.ndarray)
assert product.shape == (3,)
assert_equal(product, [42, 42 * 2, 42 * 3])
# repeat with right-mulitply
product = lst * value
assert isinstance(product, np.ndarray)
assert product.shape == (3,)
assert_equal(product, [42, 42 * 2, 42 * 3])
|
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_value = parametrize(
"value",
[
subtest(np.int64(42), name="int64"),
subtest(np.array(42), name="array"),
subtest(np.asarray(42), name="asarray"),
subtest(np.asarray(np.int64(42)), name="asarray_int"),
],
)
@instantiate_parametrized_tests
class TestArrayScalars(TestCase):
import math
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_mean
|
def test_mean(self):
A = [[1, 2, 3], [4, 5, 6]]
assert np.mean(A) == 3.5
assert np.all(np.mean(A, 0) == np.array([2.5, 3.5, 4.5]))
assert np.all(np.mean(A, 1) == np.array([2.0, 5.0]))
# XXX: numpy emits a warning on empty slice
assert np.isnan(np.mean([]))
m = np.asarray(A)
assert np.mean(A) == m.mean()
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestMean(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_mean_values
|
def test_mean_values(self):
# rmat = np.random.random((4, 5))
rmat = np.arange(20, dtype=float).reshape((4, 5))
cmat = rmat + 1j * rmat
import warnings
with warnings.catch_warnings():
warnings.simplefilter("error")
for mat in [rmat, cmat]:
for axis in [0, 1]:
tgt = mat.sum(axis=axis)
res = np.mean(mat, axis=axis) * mat.shape[axis]
assert_allclose(res, tgt)
for axis in [None]:
tgt = mat.sum(axis=axis)
res = np.mean(mat, axis=axis) * mat.size
assert_allclose(res, tgt)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestMean(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_mean_where
|
def test_mean_where(self):
a = np.arange(16).reshape((4, 4))
wh_full = np.array(
[
[False, True, False, True],
[True, False, True, False],
[True, True, False, False],
[False, False, True, True],
]
)
wh_partial = np.array([[False], [True], [True], [False]])
_cases = [
(1, True, [1.5, 5.5, 9.5, 13.5]),
(0, wh_full, [6.0, 5.0, 10.0, 9.0]),
(1, wh_full, [2.0, 5.0, 8.5, 14.5]),
(0, wh_partial, [6.0, 7.0, 8.0, 9.0]),
]
for _ax, _wh, _res in _cases:
assert_allclose(a.mean(axis=_ax, where=_wh), np.array(_res))
assert_allclose(np.mean(a, axis=_ax, where=_wh), np.array(_res))
a3d = np.arange(16).reshape((2, 2, 4))
_wh_partial = np.array([False, True, True, False])
_res = [[1.5, 5.5], [9.5, 13.5]]
assert_allclose(a3d.mean(axis=2, where=_wh_partial), np.array(_res))
assert_allclose(np.mean(a3d, axis=2, where=_wh_partial), np.array(_res))
with pytest.warns(RuntimeWarning) as w:
assert_allclose(
a.mean(axis=1, where=wh_partial), np.array([np.nan, 5.5, 9.5, np.nan])
)
with pytest.warns(RuntimeWarning) as w:
assert_equal(a.mean(where=False), np.nan)
with pytest.warns(RuntimeWarning) as w:
assert_equal(np.mean(a, where=False), np.nan)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
class TestMean(TestCase):
import warnings
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_sum
|
def test_sum(self):
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
tgt = [[6], [15], [24]]
out = np.sum(m, axis=1, keepdims=True)
assert_equal(tgt, out)
am = np.asarray(m)
assert_equal(np.sum(m), am.sum())
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import warnings
@instantiate_parametrized_tests
class TestSum(TestCase):
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_sum_stability
|
def test_sum_stability(self):
a = np.ones(500, dtype=np.float32)
zero = np.zeros(1, dtype="float32")[0]
assert_allclose((a / 10.0).sum() - a.size / 10.0, zero, atol=1.5e-4)
a = np.ones(500, dtype=np.float64)
assert_allclose((a / 10.0).sum() - a.size / 10.0, 0.0, atol=1.5e-13)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import warnings
@instantiate_parametrized_tests
class TestSum(TestCase):
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_reductions.py
|
test_sum_dtypes_warnings
|
def test_sum_dtypes_warnings(self):
for dt in (int, np.float16, np.float32, np.float64):
for v in (0, 1, 2, 7, 8, 9, 15, 16, 19, 127, 128, 1024, 1235):
# warning if sum overflows, which it does in float16
import warnings
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", RuntimeWarning)
tgt = dt(v * (v + 1) / 2)
overflow = not np.isfinite(tgt)
assert_equal(len(w), 1 * overflow)
d = np.arange(1, v + 1, dtype=dt)
assert_almost_equal(np.sum(d), tgt)
assert_equal(len(w), 2 * overflow)
assert_almost_equal(np.sum(np.flip(d)), tgt)
assert_equal(len(w), 3 * overflow)
|
from unittest import skipIf, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
import numpy.core.numeric as _util # for normalize_axis_tuple
from numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import torch._numpy as np
from torch._numpy import _util
from torch._numpy.testing import (
assert_allclose,
assert_almost_equal,
assert_array_equal,
assert_equal,
)
import warnings
@instantiate_parametrized_tests
class TestSum(TestCase):
import warnings
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_radians
|
def test_radians(self):
assert_allclose(np.radians(0.5), radians(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_reciprocal
|
def test_reciprocal(self):
assert_allclose(
np.reciprocal(0.5), reciprocal(0.5), atol=1e-14, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_rint
|
def test_rint(self):
assert_allclose(np.rint(0.5), rint(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_sign
|
def test_sign(self):
assert_allclose(np.sign(0.5), sign(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_signbit
|
def test_signbit(self):
assert_allclose(np.signbit(0.5), signbit(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_sin
|
def test_sin(self):
assert_allclose(np.sin(0.5), sin(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_sinh
|
def test_sinh(self):
assert_allclose(np.sinh(0.5), sinh(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_sqrt
|
def test_sqrt(self):
assert_allclose(np.sqrt(0.5), sqrt(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_square
|
def test_square(self):
assert_allclose(np.square(0.5), square(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_tan
|
def test_tan(self):
assert_allclose(np.tan(0.5), tan(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_isinf
|
def test_isinf(self):
assert_allclose(np.isinf(0.5), isinf(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_isnan
|
def test_isnan(self):
assert_allclose(np.isnan(0.5), isnan(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_log
|
def test_log(self):
assert_allclose(np.log(0.5), log(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_log10
|
def test_log10(self):
assert_allclose(np.log10(0.5), log10(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_log1p
|
def test_log1p(self):
assert_allclose(np.log1p(0.5), log1p(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_log2
|
def test_log2(self):
assert_allclose(np.log2(0.5), log2(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_logical_not
|
def test_logical_not(self):
assert_allclose(
np.logical_not(0.5), logical_not(0.5), atol=1e-14, check_dtype=False
)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_negative
|
def test_negative(self):
assert_allclose(np.negative(0.5), negative(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_positive
|
def test_positive(self):
assert_allclose(np.positive(0.5), positive(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_rad2deg
|
def test_rad2deg(self):
assert_allclose(np.rad2deg(0.5), rad2deg(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_cos
|
def test_cos(self):
assert_allclose(np.cos(0.5), cos(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_cosh
|
def test_cosh(self):
assert_allclose(np.cosh(0.5), cosh(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_deg2rad
|
def test_deg2rad(self):
assert_allclose(np.deg2rad(0.5), deg2rad(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_degrees
|
def test_degrees(self):
assert_allclose(np.degrees(0.5), degrees(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_exp
|
def test_exp(self):
assert_allclose(np.exp(0.5), exp(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_exp2
|
def test_exp2(self):
assert_allclose(np.exp2(0.5), exp2(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_expm1
|
def test_expm1(self):
assert_allclose(np.expm1(0.5), expm1(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_fabs
|
def test_fabs(self):
assert_allclose(np.fabs(0.5), fabs(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_floor
|
def test_floor(self):
assert_allclose(np.floor(0.5), floor(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_isfinite
|
def test_isfinite(self):
assert_allclose(np.isfinite(0.5), isfinite(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_scalars_0D_arrays.py
|
test_scalar_comparisons
|
def test_scalar_comparisons(self):
scalar = np.int64(42)
arr = np.array(42)
assert arr == scalar
assert arr >= scalar
assert arr <= scalar
assert scalar == 42
assert arr == 42
# @xfailIfTorchDynamo
|
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_value = parametrize(
"value",
[
subtest(np.int64(42), name="int64"),
subtest(np.array(42), name="array"),
subtest(np.asarray(42), name="asarray"),
subtest(np.asarray(np.int64(42)), name="asarray_int"),
],
)
@instantiate_parametrized_tests
class TestArrayScalars(TestCase):
import math
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_scalars_0D_arrays.py
|
test_is_not_scalar
|
def test_is_not_scalar(self, value):
assert not np.isscalar(value)
|
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_value = parametrize(
"value",
[
subtest(np.int64(42), name="int64"),
subtest(np.array(42), name="array"),
subtest(np.asarray(42), name="asarray"),
subtest(np.asarray(np.int64(42)), name="asarray_int"),
],
)
@instantiate_parametrized_tests
class TestIsScalar(TestCase):
import math
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ufuncs_basic.py
|
test_x_and_out_broadcast
|
def test_x_and_out_broadcast(self, ufunc):
x = self.get_x(ufunc)
out = np.empty((x.shape[0], x.shape[0]))
x_b = np.broadcast_to(x, out.shape)
res_out = ufunc(x, out=out)
res_bcast = ufunc(x_b)
# TODO: switching the order causes a graph break, failing the test.
# See test/dynamo/test_misc.py -k test_numpy_graph_break
assert res_out is out
assert_equal(res_out, res_bcast)
out = np.empty((1, x.shape[0]))
x_b = np.broadcast_to(x, out.shape)
res_out = ufunc(x, out=out)
res_bcast = ufunc(x_b)
assert res_out is out
assert_equal(res_out, res_bcast)
|
import operator
from unittest import skipIf as skip, SkipTest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_unary_ufuncs = parametrize("ufunc", [np.sin])
parametrize_casting = parametrize(
"casting", ["no", "equiv", "safe", "same_kind", "unsafe"]
)
@instantiate_parametrized_tests
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ufuncs_basic.py
|
test_xy_and_out_broadcast
|
def test_xy_and_out_broadcast(self, ufunc):
x, y = self.get_xy(ufunc)
y = y[:, None]
out = np.empty((2, y.shape[0], x.shape[0]))
x_b = np.broadcast_to(x, out.shape)
y_b = np.broadcast_to(y, out.shape)
res_out = ufunc(x, y, out=out)
res_bcast = ufunc(x_b, y_b)
# TODO: switching the order causes a graph break, failing the test.
# See test/dynamo/test_misc.py -k test_numpy_graph_break
assert res_out is out
assert_equal(res_out, res_bcast)
|
import operator
from unittest import skipIf as skip, SkipTest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_unary_ufuncs = parametrize("ufunc", [np.sin])
parametrize_casting = parametrize(
"casting", ["no", "equiv", "safe", "same_kind", "unsafe"]
)
ufunc_op_iop_numeric = [
(np.add, operator.__add__, operator.__iadd__),
(np.subtract, operator.__sub__, operator.__isub__),
(np.multiply, operator.__mul__, operator.__imul__),
]
ufuncs_with_dunders = [ufunc for ufunc, _, _ in ufunc_op_iop_numeric]
numeric_binary_ufuncs = [
np.float_power,
np.power,
]
no_complex = [
np.floor_divide,
np.hypot,
np.arctan2,
np.copysign,
np.fmax,
np.fmin,
np.fmod,
np.heaviside,
np.logaddexp,
np.logaddexp2,
np.maximum,
np.minimum,
]
parametrize_binary_ufuncs = parametrize(
"ufunc", ufuncs_with_dunders + numeric_binary_ufuncs + no_complex
)
@instantiate_parametrized_tests
class TestBinaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ufuncs_basic.py
|
test_other_array_bcast
|
def test_other_array_bcast(self, ufunc, op, iop):
"""Test op/rop/iop with broadcasting"""
# __op__
a = np.array([1, 2, 3])
result_op = op(a, a[:, None])
result_ufunc = ufunc(a, a[:, None])
assert result_op.shape == result_ufunc.shape
assert_equal(result_op, result_ufunc)
if result_op.dtype != result_ufunc.dtype:
assert result_op.dtype == result_ufunc.dtype
# __rop__
a = np.array([1, 2, 3])
result_op = op(a[:, None], a)
result_ufunc = ufunc(a[:, None], a)
assert result_op.shape == result_ufunc.shape
assert_equal(result_op, result_ufunc)
if result_op.dtype != result_ufunc.dtype:
assert result_op.dtype == result_ufunc.dtype
# __iop__ : in-place ops (`self += other` etc) do not broadcast self
b = a[:, None].copy()
with assert_raises((ValueError, RuntimeError)): # XXX ValueError in numpy
iop(a, b)
# however, `self += other` broadcasts other
aa = np.broadcast_to(a, (3, 3)).copy()
aa0 = aa.copy()
result = iop(aa, a)
result_ufunc = ufunc(aa0, a)
assert result.shape == result_ufunc.shape
assert_equal(result, result_ufunc)
if result_op.dtype != result_ufunc.dtype:
assert result_op.dtype == result_ufunc.dtype
|
import operator
from unittest import skipIf as skip, SkipTest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_unary_ufuncs = parametrize("ufunc", [np.sin])
parametrize_casting = parametrize(
"casting", ["no", "equiv", "safe", "same_kind", "unsafe"]
)
ufunc_op_iop_numeric = [
(np.add, operator.__add__, operator.__iadd__),
(np.subtract, operator.__sub__, operator.__isub__),
(np.multiply, operator.__mul__, operator.__imul__),
]
ufuncs_with_dunders = [ufunc for ufunc, _, _ in ufunc_op_iop_numeric]
numeric_binary_ufuncs = [
np.float_power,
np.power,
]
no_complex = [
np.floor_divide,
np.hypot,
np.arctan2,
np.copysign,
np.fmax,
np.fmin,
np.fmod,
np.heaviside,
np.logaddexp,
np.logaddexp2,
np.maximum,
np.minimum,
]
parametrize_binary_ufuncs = parametrize(
"ufunc", ufuncs_with_dunders + numeric_binary_ufuncs + no_complex
)
dtypes_numeric = [np.int32, np.float32, np.float64, np.complex128]
@instantiate_parametrized_tests
class TestNdarrayDunderVsUfunc(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ufuncs_basic.py
|
test_binary_ufunc_dtype
|
def test_binary_ufunc_dtype(self):
# default computation uses float64:
r64 = np.add(1, 1e-15)
assert r64.dtype == "float64"
assert r64 - 1 > 0
# force the float32 dtype: loss of precision
r32 = np.add(1, 1e-15, dtype="float32")
assert r32.dtype == "float32"
assert r32 == 1
# now force the cast
rb = np.add(1.0, 1e-15, dtype=bool, casting="unsafe")
assert rb.dtype == bool
|
import operator
from unittest import skipIf as skip, SkipTest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_unary_ufuncs = parametrize("ufunc", [np.sin])
parametrize_casting = parametrize(
"casting", ["no", "equiv", "safe", "same_kind", "unsafe"]
)
ufunc_op_iop_numeric = [
(np.add, operator.__add__, operator.__iadd__),
(np.subtract, operator.__sub__, operator.__isub__),
(np.multiply, operator.__mul__, operator.__imul__),
]
ufuncs_with_dunders = [ufunc for ufunc, _, _ in ufunc_op_iop_numeric]
numeric_binary_ufuncs = [
np.float_power,
np.power,
]
no_complex = [
np.floor_divide,
np.hypot,
np.arctan2,
np.copysign,
np.fmax,
np.fmin,
np.fmod,
np.heaviside,
np.logaddexp,
np.logaddexp2,
np.maximum,
np.minimum,
]
parametrize_binary_ufuncs = parametrize(
"ufunc", ufuncs_with_dunders + numeric_binary_ufuncs + no_complex
)
dtypes_numeric = [np.int32, np.float32, np.float64, np.complex128]
class TestUfuncDtypeKwd(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_ufuncs_basic.py
|
test_binary_ufunc_dtype_and_out
|
def test_binary_ufunc_dtype_and_out(self):
# all in float64: no precision loss
out64 = np.empty(2, dtype=np.float64)
r64 = np.add([1.0, 2.0], 1.0e-15, out=out64)
assert (r64 != [1.0, 2.0]).all()
assert r64.dtype == np.float64
# all in float32: loss of precision, result is float32
out32 = np.empty(2, dtype=np.float32)
r32 = np.add([1.0, 2.0], 1.0e-15, dtype=np.float32, out=out32)
assert (r32 == [1, 2]).all()
assert r32.dtype == np.float32
# dtype is float32, so computation is in float32: precision loss
# the result is then cast to float64
out64 = np.empty(2, dtype=np.float64)
r = np.add([1.0, 2.0], 1.0e-15, dtype=np.float32, out=out64)
assert (r == [1, 2]).all()
assert r.dtype == np.float64
# Internal computations are in float64, but the final cast to out.dtype
# truncates the precision => precision loss.
out32 = np.empty(2, dtype=np.float32)
r = np.add([1.0, 2.0], 1.0e-15, dtype=np.float64, out=out32)
assert (r == [1, 2]).all()
assert r.dtype == np.float32
|
import operator
from unittest import skipIf as skip, SkipTest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
TEST_WITH_TORCHDYNAMO,
TestCase,
)
import numpy as np
from numpy.testing import assert_equal
import torch._numpy as np
from torch._numpy.testing import assert_equal
parametrize_unary_ufuncs = parametrize("ufunc", [np.sin])
parametrize_casting = parametrize(
"casting", ["no", "equiv", "safe", "same_kind", "unsafe"]
)
ufunc_op_iop_numeric = [
(np.add, operator.__add__, operator.__iadd__),
(np.subtract, operator.__sub__, operator.__isub__),
(np.multiply, operator.__mul__, operator.__imul__),
]
ufuncs_with_dunders = [ufunc for ufunc, _, _ in ufunc_op_iop_numeric]
numeric_binary_ufuncs = [
np.float_power,
np.power,
]
no_complex = [
np.floor_divide,
np.hypot,
np.arctan2,
np.copysign,
np.fmax,
np.fmin,
np.fmod,
np.heaviside,
np.logaddexp,
np.logaddexp2,
np.maximum,
np.minimum,
]
parametrize_binary_ufuncs = parametrize(
"ufunc", ufuncs_with_dunders + numeric_binary_ufuncs + no_complex
)
dtypes_numeric = [np.int32, np.float32, np.float64, np.complex128]
class TestUfuncDtypeKwd(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/test_unary_ufuncs.py
|
test_absolute
|
def test_absolute(self):
assert_allclose(np.absolute(0.5), absolute(0.5), atol=1e-14, check_dtype=False)
|
import numpy as np
from torch._numpy._ufuncs import * # noqa: F403
from torch._numpy.testing import assert_allclose
from torch.testing._internal.common_utils import run_tests, TestCase
class TestUnaryUfuncs(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.