library
stringclasses 1
value | test_file
stringclasses 785
values | test_function
stringlengths 1
295
| before
stringlengths 0
448k
| after
stringlengths 0
487k
| context_before
stringclasses 947
values | context_after
stringlengths 0
16.3k
| commit_before
stringclasses 1
value | commit_after
stringclasses 1
value | change_type
stringclasses 3
values |
---|---|---|---|---|---|---|---|---|---|
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_kwargs
|
def test_kwargs(self):
x = np.array([0, 0])
np.putmask(x, [0, 1], [-1, -2])
assert_array_equal(x, [0, -2])
x = np.array([0, 0])
np.putmask(x, mask=[0, 1], values=[-1, -2])
assert_array_equal(x, [0, -2])
x = np.array([0, 0])
np.putmask(x, values=[-1, -2], mask=[0, 1])
assert_array_equal(x, [0, -2])
with pytest.raises(TypeError):
np.putmask(a=x, values=[-1, -2], mask=[0, 1])
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestPutmask(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
tst_basic
|
def tst_basic(self, x, T, mask, val):
np.putmask(x, mask, val)
assert_equal(x[mask], np.array(val, T))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestPutmask(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ip_types
|
def test_ip_types(self):
unchecked_types = [bytes, str, np.void]
x = np.random.random(1000) * 100
mask = x < 40
for val in [-100, 0, 15]:
for types in "efdFDBbhil?":
for T in types:
if T not in unchecked_types:
if val < 0 and np.dtype(T).kind == "u":
val = np.iinfo(T).max - 99
self.tst_basic(x.copy().astype(T), T, mask, val)
# Also test string of a length which uses an untypical length
dt = np.dtype("S3")
self.tst_basic(x.astype(dt), dt.type, mask, dt.type(val)[:3])
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestPutmask(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test__complex__should_not_work
|
def test__complex__should_not_work(self):
dtypes = [
"i1",
"i2",
"i4",
"i8",
"u1",
"f",
"d",
"F",
"D",
"?",
]
for dt in dtypes:
a = np.array([1, 2, 3], dtype=dt)
assert_raises((TypeError, ValueError), complex, a)
c = np.array([(1.0, 3), (2e-3, 7)], dtype=dt)
assert_raises((TypeError, ValueError), complex, c)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@instantiate_parametrized_tests
class TestMethods(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_array_contains
|
def test_array_contains(self):
assert_(4.0 in np.arange(16.0).reshape(4, 4))
assert_(20.0 not in np.arange(16.0).reshape(4, 4))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestCequenceMethods(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_inplace
|
def test_inplace(self):
# test refcount 1 inplace conversion
assert_array_almost_equal(np.array([0.5]) * np.array([1.0, 2.0]), [0.5, 1.0])
d = np.array([0.5, 0.5])[::2]
assert_array_almost_equal(d * (d * np.array([1.0, 2.0])), [0.25, 0.5])
a = np.array([0.5])
b = np.array([0.5])
c = a + b
c = a - b
c = a * b
c = a / b
assert_equal(a, b)
assert_almost_equal(c, 1.0)
c = a + b * 2.0 / b * a - a / b
assert_equal(a, b)
assert_equal(c, 0.5)
# true divide
a = np.array([5])
b = np.array([3])
c = (a * a) / b
assert_almost_equal(c, 25 / 3, decimal=5)
assert_equal(a, 5)
assert_equal(b, 3)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestBinop(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_test_zero_rank
|
def test_test_zero_rank(self):
x = np.array([1, 2, 3])
assert_(isinstance(x[0], (np.int_, np.ndarray)))
assert_(type(x[0, ...]) is np.ndarray)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestSubscripting(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_list
|
def test_list(self):
x = np.ones((1, 1))
x[:, [0]] = 2.0
assert_array_equal(x, np.array([[2.0]]))
x = np.ones((1, 1, 1))
x[:, :, [0]] = 2.0
assert_array_equal(x, np.array([[[2.0]]]))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestFancyIndexing(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_tuple
|
def test_tuple(self):
x = np.ones((1, 1))
x[:, (0,)] = 2.0
assert_array_equal(x, np.array([[2.0]]))
x = np.ones((1, 1, 1))
x[:, :, (0,)] = 2.0
assert_array_equal(x, np.array([[[2.0]]]))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestFancyIndexing(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_mask
|
def test_mask(self):
x = np.array([1, 2, 3, 4])
m = np.array([0, 1, 0, 0], bool)
assert_array_equal(x[m], np.array([2]))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestFancyIndexing(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_mask2
|
def test_mask2(self):
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
m = np.array([0, 1], bool)
m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
assert_array_equal(x[m], np.array([[5, 6, 7, 8]]))
assert_array_equal(x[m2], np.array([2, 5]))
assert_array_equal(x[m3], np.array([2]))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestFancyIndexing(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_assign_mask
|
def test_assign_mask(self):
x = np.array([1, 2, 3, 4])
m = np.array([0, 1, 0, 0], bool)
x[m] = 5
assert_array_equal(x, np.array([1, 5, 3, 4]))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestFancyIndexing(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_assign_mask2
|
def test_assign_mask2(self):
xorig = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
m = np.array([0, 1], bool)
m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
x = xorig.copy()
x[m] = 10
assert_array_equal(x, np.array([[1, 2, 3, 4], [10, 10, 10, 10]]))
x = xorig.copy()
x[m2] = 10
assert_array_equal(x, np.array([[1, 10, 3, 4], [10, 6, 7, 8]]))
x = xorig.copy()
x[m3] = 10
assert_array_equal(x, np.array([[1, 10, 3, 4], [5, 6, 7, 8]]))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestFancyIndexing(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_no_dgemv_2
|
def test_no_dgemv_2(self, func, dtype):
# check for unaligned data
dt = np.dtype(dtype)
a = np.zeros(8 * dt.itemsize // 2 + 1, dtype="int16")[1:].view(dtype)
a = a.reshape(2, 4)
b = a[0]
# make sure it is not aligned
assert_(a.__array_interface__["data"][0] % dt.itemsize != 0)
ret1 = func(a, b)
ret2 = func(a.copy(), b.copy())
assert_equal(ret1, ret2)
ret1 = func(b.T, a.T)
ret2 = func(b.T.copy(), a.T.copy())
assert_equal(ret1, ret2)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@instantiate_parametrized_tests
class TestMethods(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_matmul_out
|
def test_matmul_out(self):
# overlapping memory
a = np.arange(18).reshape(2, 3, 3)
b = np.matmul(a, a)
c = np.matmul(a, a, out=a)
assert_(c is a)
assert_equal(c, b)
a = np.arange(18).reshape(2, 3, 3)
c = np.matmul(a, a, out=a[::-1, ...])
assert_(c.base is a.base)
assert_equal(c, b)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@instantiate_parametrized_tests
class TestMethods(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
tmp_filename
|
def tmp_filename(self, tmp_path, request):
# This fixture covers two cases:
# one where the filename is a string and
# another where it is a pathlib object
filename = tmp_path / "file"
if request.param == "string":
filename = str(filename)
return filename
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_nofile
|
def test_nofile(self):
# this should probably be supported as a file
# but for now test for proper errors
b = io.BytesIO()
assert_raises(OSError, np.fromfile, b, np.uint8, 80)
d = np.ones(7)
assert_raises(OSError, lambda x: x.tofile(b), d)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_bool_fromstring
|
def test_bool_fromstring(self):
v = np.array([True, False, True, False], dtype=np.bool_)
y = np.fromstring("1 0 -2.3 0.0", sep=" ", dtype=np.bool_)
assert_array_equal(v, y)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_uint64_fromstring
|
def test_uint64_fromstring(self):
d = np.fromstring(
"9923372036854775807 104783749223640", dtype=np.uint64, sep=" "
)
e = np.array([9923372036854775807, 104783749223640], dtype=np.uint64)
assert_array_equal(d, e)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_int64_fromstring
|
def test_int64_fromstring(self):
d = np.fromstring("-25041670086757 104783749223640", dtype=np.int64, sep=" ")
e = np.array([-25041670086757, 104783749223640], dtype=np.int64)
assert_array_equal(d, e)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_fromstring_count0
|
def test_fromstring_count0(self):
d = np.fromstring("1,2", sep=",", dtype=np.int64, count=0)
assert d.shape == (0,)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_empty_files_text
|
def test_empty_files_text(self, tmp_filename):
with open(tmp_filename, "w") as f:
pass
y = np.fromfile(tmp_filename)
assert_(y.size == 0, "Array not empty")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_empty_files_binary
|
def test_empty_files_binary(self, tmp_filename):
with open(tmp_filename, "wb") as f:
pass
y = np.fromfile(tmp_filename, sep=" ")
assert_(y.size == 0, "Array not empty")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_roundtrip_file
|
def test_roundtrip_file(self, x, tmp_filename):
with open(tmp_filename, "wb") as f:
x.tofile(f)
# NB. doesn't work with flush+seek, due to use of C stdio
with open(tmp_filename, "rb") as f:
y = np.fromfile(f, dtype=x.dtype)
assert_array_equal(y, x.flat)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_roundtrip
|
def test_roundtrip(self, x, tmp_filename):
x.tofile(tmp_filename)
y = np.fromfile(tmp_filename, dtype=x.dtype)
assert_array_equal(y, x.flat)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_roundtrip_dump_pathlib
|
def test_roundtrip_dump_pathlib(self, x, tmp_filename):
p = Path(tmp_filename)
x.dump(p)
y = np.load(p, allow_pickle=True)
assert_array_equal(y, x)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_roundtrip_binary_str
|
def test_roundtrip_binary_str(self, x):
s = x.tobytes()
y = np.frombuffer(s, dtype=x.dtype)
assert_array_equal(y, x.flat)
s = x.tobytes("F")
y = np.frombuffer(s, dtype=x.dtype)
assert_array_equal(y, x.flatten("F"))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_roundtrip_str
|
def test_roundtrip_str(self, x):
x = x.real.ravel()
s = "@".join(map(str, x))
y = np.fromstring(s, sep="@")
# NB. str imbues less precision
nan_mask = ~np.isfinite(x)
assert_array_equal(x[nan_mask], y[nan_mask])
assert_array_almost_equal(x[~nan_mask], y[~nan_mask], decimal=5)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_roundtrip_repr
|
def test_roundtrip_repr(self, x):
x = x.real.ravel()
s = "@".join(map(repr, x))
y = np.fromstring(s, sep="@")
assert_array_equal(x, y)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_unseekable_fromfile
|
def test_unseekable_fromfile(self, x, tmp_filename):
# gh-6246
x.tofile(tmp_filename)
def fail(*args, **kwargs):
raise OSError("Can not tell or seek")
with open(tmp_filename, "rb", buffering=0) as f:
f.seek = fail
f.tell = fail
assert_raises(OSError, np.fromfile, f, dtype=x.dtype)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
fail
|
def fail(*args, **kwargs):
raise OSError("Can not tell or seek")
with open(tmp_filename, "rb", buffering=0) as f:
f.seek = fail
f.tell = fail
assert_raises(OSError, np.fromfile, f, dtype=x.dtype)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_io_open_unbuffered_fromfile
|
def test_io_open_unbuffered_fromfile(self, x, tmp_filename):
# gh-6632
x.tofile(tmp_filename)
with open(tmp_filename, "rb", buffering=0) as f:
y = np.fromfile(f, dtype=x.dtype)
assert_array_equal(y, x.flat)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_largish_file
|
def test_largish_file(self, tmp_filename):
# check the fallocate path on files > 16MB
d = np.zeros(4 * 1024**2)
d.tofile(tmp_filename)
assert_equal(os.path.getsize(tmp_filename), d.nbytes)
assert_array_equal(d, np.fromfile(tmp_filename))
# check offset
with open(tmp_filename, "r+b") as f:
f.seek(d.nbytes)
d.tofile(f)
assert_equal(os.path.getsize(tmp_filename), d.nbytes * 2)
# check append mode (gh-8329)
open(tmp_filename, "w").close() # delete file contents
with open(tmp_filename, "ab") as f:
d.tofile(f)
assert_array_equal(d, np.fromfile(tmp_filename))
with open(tmp_filename, "ab") as f:
d.tofile(f)
assert_equal(os.path.getsize(tmp_filename), d.nbytes * 2)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_io_open_buffered_fromfile
|
def test_io_open_buffered_fromfile(self, x, tmp_filename):
# gh-6632
x.tofile(tmp_filename)
with open(tmp_filename, "rb", buffering=-1) as f:
y = np.fromfile(f, dtype=x.dtype)
assert_array_equal(y, x.flat)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_file_position_after_fromfile
|
def test_file_position_after_fromfile(self, tmp_filename):
# gh-4118
sizes = [
io.DEFAULT_BUFFER_SIZE // 8,
io.DEFAULT_BUFFER_SIZE,
io.DEFAULT_BUFFER_SIZE * 8,
]
for size in sizes:
with open(tmp_filename, "wb") as f:
f.seek(size - 1)
f.write(b"\0")
for mode in ["rb", "r+b"]:
err_msg = "%d %s" % (size, mode)
with open(tmp_filename, mode) as f:
f.read(2)
np.fromfile(f, dtype=np.float64, count=1)
pos = f.tell()
assert_equal(pos, 10, err_msg=err_msg)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_file_position_after_tofile
|
def test_file_position_after_tofile(self, tmp_filename):
# gh-4118
sizes = [
io.DEFAULT_BUFFER_SIZE // 8,
io.DEFAULT_BUFFER_SIZE,
io.DEFAULT_BUFFER_SIZE * 8,
]
for size in sizes:
err_msg = "%d" % (size,)
with open(tmp_filename, "wb") as f:
f.seek(size - 1)
f.write(b"\0")
f.seek(10)
f.write(b"12")
np.array([0], dtype=np.float64).tofile(f)
pos = f.tell()
assert_equal(pos, 10 + 2 + 8, err_msg=err_msg)
with open(tmp_filename, "r+b") as f:
f.read(2)
f.seek(0, 1) # seek between read&write required by ANSI C
np.array([0], dtype=np.float64).tofile(f)
pos = f.tell()
assert_equal(pos, 10, err_msg=err_msg)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_load_object_array_fromfile
|
def test_load_object_array_fromfile(self, tmp_filename):
# gh-12300
with open(tmp_filename, "w") as f:
# Ensure we have a file with consistent contents
pass
with open(tmp_filename, "rb") as f:
assert_raises_regex(
ValueError,
"Cannot read into object array",
np.fromfile,
f,
dtype=object,
)
assert_raises_regex(
ValueError,
"Cannot read into object array",
np.fromfile,
tmp_filename,
dtype=object,
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
dup_str
|
def dup_str(fd):
return "abc"
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
dup_bigint
|
def dup_bigint(fd):
return 2**68
old_dup = os.dup
try:
with open(tmp_filename, "wb") as f:
x.tofile(f)
for dup, exc in ((dup_str, TypeError), (dup_bigint, OSError)):
os.dup = dup
assert_raises(exc, np.fromfile, f)
finally:
os.dup = old_dup
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
decimal_sep_localization
|
def decimal_sep_localization(self, request):
"""
Including this fixture in a test will automatically
execute it with both types of decimal separator.
So::
def test_decimal(decimal_sep_localization):
pass
is equivalent to the following two tests::
def test_decimal_period_separator():
pass
def test_decimal_comma_separator():
with CommaDecimalPointLocale():
pass
"""
if request.param == "period":
yield
elif request.param == "comma":
with CommaDecimalPointLocale():
yield
else:
raise AssertionError(request.param)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_decimal
|
def test_decimal(decimal_sep_localization):
pass
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
|||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_decimal_period_separator
|
def test_decimal_period_separator():
pass
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
|||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_decimal_comma_separator
|
def test_decimal_comma_separator():
with CommaDecimalPointLocale():
pass
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
|||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_nan
|
def test_nan(self):
input_arr = np.array([-2.0, np.nan, 0.5, 3.0, 0.25, np.nan])
result = input_arr.clip(-1, 1)
expected = np.array([-1.0, np.nan, 0.5, 1.0, 0.25, np.nan])
assert_array_equal(result, expected)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestClip(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_inf
|
def test_inf(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"inf +inf -inf infinity -Infinity iNfInItY -inF",
[np.inf, np.inf, -np.inf, np.inf, -np.inf, np.inf, -np.inf],
tmp_filename,
sep=" ",
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_numbers
|
def test_numbers(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"1.234 -1.234 .3 .3e55 -123133.1231e+133",
[1.234, -1.234, 0.3, 0.3e55, -123133.1231e133],
tmp_filename,
sep=" ",
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_big_binary
|
def test_big_binary(self):
"""Test workarounds for 32-bit limit for MSVC fwrite, fseek, and ftell
These normally would hang doing something like this.
See : https://github.com/numpy/numpy/issues/2256
"""
if sys.platform != "win32" or "[GCC " in sys.version:
return
try:
# before workarounds, only up to 2**32-1 worked
fourgbplus = 2**32 + 2**16
testbytes = np.arange(8, dtype=np.int8)
n = len(testbytes)
flike = tempfile.NamedTemporaryFile()
f = flike.file
np.tile(testbytes, fourgbplus // testbytes.nbytes).tofile(f)
flike.seek(0)
a = np.fromfile(f, dtype=np.int8)
flike.close()
assert_(len(a) == fourgbplus)
# check only start and end for speed:
assert_((a[:n] == testbytes).all())
assert_((a[-n:] == testbytes).all())
except (MemoryError, ValueError):
pass
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_string
|
def test_string(self, tmp_filename):
self._check_from(b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, sep=",")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_counted_string
|
def test_counted_string(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, count=4, sep=","
)
self._check_from(b"1,2,3,4", [1.0, 2.0, 3.0], tmp_filename, count=3, sep=",")
self._check_from(
b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, count=-1, sep=","
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_string_with_ws
|
def test_string_with_ws(self, tmp_filename):
self._check_from(
b"1 2 3 4 ", [1, 2, 3, 4], tmp_filename, dtype=int, sep=" "
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_counted_string_with_ws
|
def test_counted_string_with_ws(self, tmp_filename):
self._check_from(
b"1 2 3 4 ", [1, 2, 3], tmp_filename, count=3, dtype=int, sep=" "
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ascii
|
def test_ascii(self, tmp_filename, decimal_sep_localization):
self._check_from(b"1 , 2 , 3 , 4", [1.0, 2.0, 3.0, 4.0], tmp_filename, sep=",")
self._check_from(
b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, dtype=float, sep=","
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_basic
|
def test_basic(self):
sk = np.array([0, -0.1, 0.1])
res = 250 * sk[:, np.newaxis]
assert_almost_equal(res.ravel(), 250 * sk)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestNewaxis(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_array_base
|
def test_array_base(self, obj):
# Objects (including NumPy arrays), which do not use the
# `release_buffer` slot should be directly used as a base object.
# See also gh-21612
if isinstance(obj, str):
# @parametrize breaks with bytes objects
obj = bytes(obj, enconding="latin-1")
new = np.frombuffer(obj)
assert new.base is obj
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestFromBuffer(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_mmap_close
|
def test_mmap_close(self):
# The old buffer protocol was not safe for some things that the new
# one is. But `frombuffer` always used the old one for a long time.
# Checks that it is safe with the new one (using memoryviews)
with tempfile.TemporaryFile(mode="wb") as tmp:
tmp.write(b"asdf")
tmp.flush()
mm = mmap.mmap(tmp.fileno(), 0)
arr = np.frombuffer(mm, dtype=np.uint8)
with pytest.raises(BufferError):
mm.close() # cannot close while array uses the buffer
del arr
mm.close()
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestFromBuffer(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_contiguous
|
def test_contiguous(self):
testpassed = False
try:
self.a.flat[12] = 100.0
except ValueError:
testpassed = True
assert_(testpassed)
assert_(self.a.flat[12] == 12.0)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip # (reason="TODO") # FIXME: skip -> xfail (a0.shape = (4, 5) raises)
class TestFlat(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_discontiguous
|
def test_discontiguous(self):
testpassed = False
try:
self.b.flat[4] = 100.0
except ValueError:
testpassed = True
assert_(testpassed)
assert_(self.b.flat[4] == 12.0)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip # (reason="TODO") # FIXME: skip -> xfail (a0.shape = (4, 5) raises)
class TestFlat(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_refcount
|
def test_refcount(self):
# includes regression test for reference count error gh-13165
inds = [np.intp(0), np.array([True] * self.a.size), np.array([0]), None]
indtype = np.dtype(np.intp)
rc_indtype = sys.getrefcount(indtype)
for ind in inds:
rc_ind = sys.getrefcount(ind)
for _ in range(100):
try:
self.a.flat[ind]
except IndexError:
pass
assert_(abs(sys.getrefcount(ind) - rc_ind) < 50)
assert_(abs(sys.getrefcount(indtype) - rc_indtype) < 50)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip # (reason="TODO") # FIXME: skip -> xfail (a0.shape = (4, 5) raises)
class TestFlat(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_index_getset
|
def test_index_getset(self):
it = np.arange(10).reshape(2, 1, 5).flat
with pytest.raises(AttributeError):
it.index = 10
for _ in it:
pass
# Check the value of `.index` is updated correctly (see also gh-19153)
# If the type was incorrect, this would show up on big-endian machines
assert it.index == it.base.size
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip # (reason="TODO") # FIXME: skip -> xfail (a0.shape = (4, 5) raises)
class TestFlat(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_var_where
|
def test_var_where(self):
a = np.arange(25).reshape((5, 5))
wh_full = np.array(
[
[False, True, False, True, True],
[True, False, True, True, False],
[True, True, False, False, True],
[False, True, True, False, True],
[True, False, True, True, False],
]
)
wh_partial = np.array([[False], [True], [True], [False], [True]])
_cases = [
(0, True, [50.0, 50.0, 50.0, 50.0, 50.0]),
(1, True, [2.0, 2.0, 2.0, 2.0, 2.0]),
]
for _ax, _wh, _res in _cases:
assert_allclose(a.var(axis=_ax, where=_wh), np.array(_res))
assert_allclose(np.var(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 = [[0.25, 0.25], [0.25, 0.25]]
assert_allclose(a3d.var(axis=2, where=_wh_partial), np.array(_res))
assert_allclose(np.var(a3d, axis=2, where=_wh_partial), np.array(_res))
assert_allclose(
np.var(a, axis=1, where=wh_full), np.var(a[wh_full].reshape((5, 3)), axis=1)
)
assert_allclose(
np.var(a, axis=0, where=wh_partial), np.var(a[wh_partial[:, 0]], axis=0)
)
with pytest.warns(RuntimeWarning) as w:
assert_equal(a.var(where=False), np.nan)
with pytest.warns(RuntimeWarning) as w:
assert_equal(np.var(a, where=False), np.nan)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_std_where
|
def test_std_where(self):
a = np.arange(25).reshape((5, 5))[::-1]
whf = np.array(
[
[False, True, False, True, True],
[True, False, True, False, True],
[True, True, False, True, False],
[True, False, True, True, False],
[False, True, False, True, True],
]
)
whp = np.array([[False], [False], [True], [True], [False]])
_cases = [
(0, True, 7.07106781 * np.ones(5)),
(1, True, 1.41421356 * np.ones(5)),
(0, whf, np.array([4.0824829, 8.16496581, 5.0, 7.39509973, 8.49836586])),
(0, whp, 2.5 * np.ones(5)),
]
for _ax, _wh, _res in _cases:
assert_allclose(a.std(axis=_ax, where=_wh), _res)
assert_allclose(np.std(a, axis=_ax, where=_wh), _res)
a3d = np.arange(16).reshape((2, 2, 4))
_wh_partial = np.array([False, True, True, False])
_res = [[0.5, 0.5], [0.5, 0.5]]
assert_allclose(a3d.std(axis=2, where=_wh_partial), np.array(_res))
assert_allclose(np.std(a3d, axis=2, where=_wh_partial), np.array(_res))
assert_allclose(
a.std(axis=1, where=whf), np.std(a[whf].reshape((5, 3)), axis=1)
)
assert_allclose(
np.std(a, axis=1, where=whf), (a[whf].reshape((5, 3))).std(axis=1)
)
assert_allclose(a.std(axis=0, where=whp), np.std(a[whp[:, 0]], axis=0))
assert_allclose(np.std(a, axis=0, where=whp), (a[whp[:, 0]]).std(axis=0))
with pytest.warns(RuntimeWarning) as w:
assert_equal(a.std(where=False), np.nan)
with pytest.warns(RuntimeWarning) as w:
assert_equal(np.std(a, where=False), np.nan)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_basic
|
def test_basic(self):
sk = np.array([0, -0.1, 0.1])
res = 250 * sk[:, np.newaxis]
assert_almost_equal(res.ravel(), 250 * sk)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestNewaxis(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_vdot_array_order
|
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order="C")
b = np.array([[1, 2], [3, 4]], order="F")
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestVdot(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_vdot_uncontiguous_2
|
def test_vdot_uncontiguous_2(self):
# test order='F' separately
for size in [2, 1000]:
# Different sizes match different branches in vdot.
a = np.zeros((size, 2, 2))
b = np.zeros((size, 2, 2))
a[:, 0, 0] = np.arange(size)
b[:, 0, 0] = np.arange(size) + 1
# Make a and b uncontiguous:
a = a[..., 0]
b = b[..., 0]
assert_equal(np.vdot(a.copy("F"), b), np.vdot(a.flatten(), b.flatten()))
assert_equal(np.vdot(a, b.copy("F")), np.vdot(a.flatten(), b.flatten()))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestVdot(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dotmatmat
|
def test_dotmatmat(self):
A = self.A
res = np.dot(A.transpose(), A)
tgt = np.array([[1.45046013, 0.86323640], [0.86323640, 0.84934569]])
assert_almost_equal(res, tgt, decimal=self.N)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestDot(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_malformed
|
def test_malformed(self, tmp_filename, decimal_sep_localization):
with assert_warns(DeprecationWarning):
self._check_from(b"1.234 1,234", [1.234, 1.0], tmp_filename, sep=" ")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_long_sep
|
def test_long_sep(self, tmp_filename):
self._check_from(b"1_x_3_x_4_x_5", [1, 3, 4, 5], tmp_filename, sep="_x_")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dtype
|
def test_dtype(self, tmp_filename):
v = np.array([1, 2, 3, 4], dtype=np.int_)
self._check_from(b"1,2,3,4", v, tmp_filename, sep=",", dtype=np.int_)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dtype_bool
|
def test_dtype_bool(self, tmp_filename):
# can't use _check_from because fromstring can't handle True/False
v = np.array([True, False, True, False], dtype=np.bool_)
s = b"1,0,-2.3,0"
with open(tmp_filename, "wb") as f:
f.write(s)
y = np.fromfile(tmp_filename, sep=",", dtype=np.bool_)
assert_(y.dtype == "?")
assert_array_equal(y, v)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_tofile_sep
|
def test_tofile_sep(self, tmp_filename, decimal_sep_localization):
x = np.array([1.51, 2, 3.51, 4], dtype=float)
with open(tmp_filename, "w") as f:
x.tofile(f, sep=",")
with open(tmp_filename) as f:
s = f.read()
# assert_equal(s, '1.51,2.0,3.51,4.0')
y = np.array([float(p) for p in s.split(",")])
assert_array_equal(x, y)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_tofile_format
|
def test_tofile_format(self, tmp_filename, decimal_sep_localization):
x = np.array([1.51, 2, 3.51, 4], dtype=float)
with open(tmp_filename, "w") as f:
x.tofile(f, sep=",", format="%.2f")
with open(tmp_filename) as f:
s = f.read()
assert_equal(s, "1.51,2.00,3.51,4.00")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_tofile_cleanup
|
def test_tofile_cleanup(self, tmp_filename):
x = np.zeros((10), dtype=object)
with open(tmp_filename, "wb") as f:
assert_raises(OSError, lambda: x.tofile(f, sep=""))
# Dup-ed file handle should be closed or remove will fail on Windows OS
os.remove(tmp_filename)
# Also make sure that we close the Python handle
assert_raises(OSError, lambda: x.tofile(tmp_filename))
os.remove(tmp_filename)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_fromfile_subarray_binary
|
def test_fromfile_subarray_binary(self, tmp_filename):
# Test subarray dtypes which are absorbed into the shape
x = np.arange(24, dtype="i4").reshape(2, 3, 4)
x.tofile(tmp_filename)
res = np.fromfile(tmp_filename, dtype="(3,4)i4")
assert_array_equal(x, res)
x_str = x.tobytes()
with assert_warns(DeprecationWarning):
# binary fromstring is deprecated
res = np.fromstring(x_str, dtype="(3,4)i4")
assert_array_equal(x, res)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_parsing_subarray_unsupported
|
def test_parsing_subarray_unsupported(self, tmp_filename):
# We currently do not support parsing subarray dtypes
data = "12,42,13," * 50
with pytest.raises(ValueError):
expected = np.fromstring(data, dtype="(3,)i", sep=",")
with open(tmp_filename, "w") as f:
f.write(data)
with pytest.raises(ValueError):
np.fromfile(tmp_filename, dtype="(3,)i", sep=",")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_read_shorter_than_count_subarray
|
def test_read_shorter_than_count_subarray(self, tmp_filename):
# Test that requesting more values does not cause any problems
# in conjunction with subarray dimensions being absorbed into the
# array dimension.
expected = np.arange(511 * 10, dtype="i").reshape(-1, 10)
binary = expected.tobytes()
with pytest.raises(ValueError):
with pytest.warns(DeprecationWarning):
np.fromstring(binary, dtype="(10,)i", count=10000)
expected.tofile(tmp_filename)
res = np.fromfile(tmp_filename, dtype="(10,)i", count=10000)
assert_array_equal(res, expected)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@skip(reason="dont worry about IO")
class TestIO(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_basic
|
def test_basic(self):
sk = np.array([0, -0.1, 0.1])
res = 250 * sk[:, np.newaxis]
assert_almost_equal(res.ravel(), 250 * sk)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestNewaxis(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_int_shape
|
def test_int_shape(self):
x = np.eye(3)
if IS_PYPY:
x.resize(3, refcheck=False)
else:
x.resize(3)
assert_array_equal(x, np.eye(3)[0, :])
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestResize(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_none_shape
|
def test_none_shape(self):
x = np.eye(3)
x.resize(None)
assert_array_equal(x, np.eye(3))
x.resize()
assert_array_equal(x, np.eye(3))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestResize(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_0d_shape
|
def test_0d_shape(self):
# to it multiple times to test it does not break alloc cache gh-9216
for i in range(10):
x = np.empty((1,))
x.resize(())
assert_equal(x.shape, ())
assert_equal(x.size, 1)
x = np.empty(())
x.resize((1,))
assert_equal(x.shape, (1,))
assert_equal(x.size, 1)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestResize(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_zeros_appended
|
def test_zeros_appended(self):
x = np.eye(3)
if IS_PYPY:
x.resize(2, 3, 3, refcheck=False)
else:
x.resize(2, 3, 3)
assert_array_equal(x[0], np.eye(3))
assert_array_equal(x[1], np.zeros((3, 3)))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestResize(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_check_weakref
|
def test_check_weakref(self):
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
xref = weakref.ref(x)
assert_raises(ValueError, x.resize, (5, 1))
del xref # avoid pyflakes unused variable warning.
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestResize(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
_mean
|
def _mean(a, **args):
return a.mean(**args)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
_var
|
def _var(a, **args):
return a.var(**args)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
_std
|
def _std(a, **args):
return a.std(**args)
@instantiate_parametrized_tests
class TestStats(TestCase):
funcs = [_mean, _var, _std]
def setUp(self):
np.random.seed(3)
self.rmat = np.random.random((4, 5))
self.cmat = self.rmat + 1j * self.rmat
def test_python_type(self):
for x in (np.float16(1.0), 1, 1.0, 1 + 0j):
assert_equal(np.mean([x]), 1.0)
assert_equal(np.std([x]), 0.0)
assert_equal(np.var([x]), 0.0)
def test_keepdims(self):
mat = np.eye(3)
for f in self.funcs:
for axis in [0, 1]:
res = f(mat, axis=axis, keepdims=True)
assert_(res.ndim == mat.ndim)
assert_(res.shape[axis] == 1)
for axis in [None]:
res = f(mat, axis=axis, keepdims=True)
assert_(res.shape == (1, 1))
def test_out(self):
mat = np.eye(3)
for f in self.funcs:
out = np.zeros(3)
tgt = f(mat, axis=1)
res = f(mat, axis=1, out=out)
assert_almost_equal(res, out)
assert_almost_equal(res, tgt)
out = np.empty(2)
assert_raises(ValueError, f, mat, axis=1, out=out)
out = np.empty((2, 2))
assert_raises(ValueError, f, mat, axis=1, out=out)
def test_dtype_from_input(self):
icodes = np.typecodes["AllInteger"]
fcodes = np.typecodes["AllFloat"]
# integer types
for f in self.funcs:
for c in icodes:
mat = np.eye(3, dtype=c)
tgt = np.float64
res = f(mat, axis=1).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None).dtype.type
assert_(res is tgt)
# mean for float types
for f in [_mean]:
for c in fcodes:
mat = np.eye(3, dtype=c)
tgt = mat.dtype.type
res = f(mat, axis=1).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None).dtype.type
assert_(res is tgt)
# var, std for float types
for f in [_var, _std]:
for c in fcodes:
mat = np.eye(3, dtype=c)
# deal with complex types
tgt = mat.real.dtype.type
res = f(mat, axis=1).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None).dtype.type
assert_(res is tgt)
def test_dtype_from_dtype(self):
mat = np.eye(3)
# stats for integer types
# FIXME:
# this needs definition as there are lots places along the line
# where type casting may take place.
# for f in self.funcs:
# for c in np.typecodes['AllInteger']:
# tgt = np.dtype(c).type
# res = f(mat, axis=1, dtype=c).dtype.type
# assert_(res is tgt)
# # scalar case
# res = f(mat, axis=None, dtype=c).dtype.type
# assert_(res is tgt)
# stats for float types
for f in self.funcs:
for c in np.typecodes["AllFloat"]:
tgt = np.dtype(c).type
res = f(mat, axis=1, dtype=c).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None, dtype=c).dtype.type
assert_(res is tgt)
def test_ddof(self):
for f in [_var]:
for ddof in range(3):
dim = self.rmat.shape[1]
tgt = f(self.rmat, axis=1) * dim
res = f(self.rmat, axis=1, ddof=ddof) * (dim - ddof)
for f in [_std]:
for ddof in range(3):
dim = self.rmat.shape[1]
tgt = f(self.rmat, axis=1) * np.sqrt(dim)
res = f(self.rmat, axis=1, ddof=ddof) * np.sqrt(dim - ddof)
assert_almost_equal(res, tgt)
assert_almost_equal(res, tgt)
def test_ddof_too_big(self):
dim = self.rmat.shape[1]
for f in [_var, _std]:
for ddof in range(dim, dim + 2):
# with warnings.catch_warnings(record=True) as w:
# warnings.simplefilter('always')
res = f(self.rmat, axis=1, ddof=ddof)
assert_(not (res < 0).any())
# assert_(len(w) > 0)
# assert_(issubclass(w[0].category, RuntimeWarning))
def test_empty(self):
A = np.zeros((0, 3))
for f in self.funcs:
for axis in [0, None]:
# with warnings.catch_warnings(record=True) as w:
# warnings.simplefilter('always')
assert_(np.isnan(f(A, axis=axis)).all())
# assert_(len(w) > 0)
# assert_(issubclass(w[0].category, RuntimeWarning))
for axis in [1]:
# with warnings.catch_warnings(record=True) as w:
# warnings.simplefilter('always')
assert_equal(f(A, axis=axis), np.zeros([]))
def test_mean_values(self):
for mat in [self.rmat, self.cmat]:
for axis in [0, 1]:
tgt = mat.sum(axis=axis)
res = _mean(mat, axis=axis) * mat.shape[axis]
assert_almost_equal(res, tgt)
for axis in [None]:
tgt = mat.sum(axis=axis)
res = _mean(mat, axis=axis) * np.prod(mat.shape)
assert_almost_equal(res, tgt)
def test_mean_float16(self):
# This fail if the sum inside mean is done in float16 instead
# of float32.
assert_(_mean(np.ones(100000, dtype="float16")) == 1)
def test_mean_axis_error(self):
# Ensure that AxisError is raised instead of IndexError when axis is
# out of bounds, see gh-15817.
with assert_raises(np.AxisError):
np.arange(10).mean(axis=2)
@xpassIfTorchDynamo # (reason="implement 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)
def test_var_values(self):
for mat in [self.rmat, self.cmat]:
for axis in [0, 1, None]:
msqr = _mean(mat * mat.conj(), axis=axis)
mean = _mean(mat, axis=axis)
tgt = msqr - mean * mean.conjugate()
res = _var(mat, axis=axis)
assert_almost_equal(res, tgt)
@parametrize(
"complex_dtype, ndec",
(
("complex64", 6),
("complex128", 7),
),
)
def test_var_complex_values(self, complex_dtype, ndec):
# Test fast-paths for every builtin complex type
for axis in [0, 1, None]:
mat = self.cmat.copy().astype(complex_dtype)
msqr = _mean(mat * mat.conj(), axis=axis)
mean = _mean(mat, axis=axis)
tgt = msqr - mean * mean.conjugate()
res = _var(mat, axis=axis)
assert_almost_equal(res, tgt, decimal=ndec)
def test_var_dimensions(self):
# _var paths for complex number introduce additions on views that
# increase dimensions. Ensure this generalizes to higher dims
mat = np.stack([self.cmat] * 3)
for axis in [0, 1, 2, -1, None]:
msqr = _mean(mat * mat.conj(), axis=axis)
mean = _mean(mat, axis=axis)
tgt = msqr - mean * mean.conjugate()
res = _var(mat, axis=axis)
assert_almost_equal(res, tgt)
@skip(reason="endianness")
def test_var_complex_byteorder(self):
# Test that var fast-path does not cause failures for complex arrays
# with non-native byteorder
cmat = self.cmat.copy().astype("complex128")
cmat_swapped = cmat.astype(cmat.dtype.newbyteorder())
assert_almost_equal(cmat.var(), cmat_swapped.var())
def test_var_axis_error(self):
# Ensure that AxisError is raised instead of IndexError when axis is
# out of bounds, see gh-15817.
with assert_raises(np.AxisError):
np.arange(10).var(axis=2)
@xpassIfTorchDynamo # (reason="implement var(..., where=...)")
def test_var_where(self):
a = np.arange(25).reshape((5, 5))
wh_full = np.array(
[
[False, True, False, True, True],
[True, False, True, True, False],
[True, True, False, False, True],
[False, True, True, False, True],
[True, False, True, True, False],
]
)
wh_partial = np.array([[False], [True], [True], [False], [True]])
_cases = [
(0, True, [50.0, 50.0, 50.0, 50.0, 50.0]),
(1, True, [2.0, 2.0, 2.0, 2.0, 2.0]),
]
for _ax, _wh, _res in _cases:
assert_allclose(a.var(axis=_ax, where=_wh), np.array(_res))
assert_allclose(np.var(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 = [[0.25, 0.25], [0.25, 0.25]]
assert_allclose(a3d.var(axis=2, where=_wh_partial), np.array(_res))
assert_allclose(np.var(a3d, axis=2, where=_wh_partial), np.array(_res))
assert_allclose(
np.var(a, axis=1, where=wh_full), np.var(a[wh_full].reshape((5, 3)), axis=1)
)
assert_allclose(
np.var(a, axis=0, where=wh_partial), np.var(a[wh_partial[:, 0]], axis=0)
)
with pytest.warns(RuntimeWarning) as w:
assert_equal(a.var(where=False), np.nan)
with pytest.warns(RuntimeWarning) as w:
assert_equal(np.var(a, where=False), np.nan)
def test_std_values(self):
for mat in [self.rmat, self.cmat]:
for axis in [0, 1, None]:
tgt = np.sqrt(_var(mat, axis=axis))
res = _std(mat, axis=axis)
assert_almost_equal(res, tgt)
@xpassIfTorchDynamo # (reason="implement std(..., where=...)")
def test_std_where(self):
a = np.arange(25).reshape((5, 5))[::-1]
whf = np.array(
[
[False, True, False, True, True],
[True, False, True, False, True],
[True, True, False, True, False],
[True, False, True, True, False],
[False, True, False, True, True],
]
)
whp = np.array([[False], [False], [True], [True], [False]])
_cases = [
(0, True, 7.07106781 * np.ones(5)),
(1, True, 1.41421356 * np.ones(5)),
(0, whf, np.array([4.0824829, 8.16496581, 5.0, 7.39509973, 8.49836586])),
(0, whp, 2.5 * np.ones(5)),
]
for _ax, _wh, _res in _cases:
assert_allclose(a.std(axis=_ax, where=_wh), _res)
assert_allclose(np.std(a, axis=_ax, where=_wh), _res)
a3d = np.arange(16).reshape((2, 2, 4))
_wh_partial = np.array([False, True, True, False])
_res = [[0.5, 0.5], [0.5, 0.5]]
assert_allclose(a3d.std(axis=2, where=_wh_partial), np.array(_res))
assert_allclose(np.std(a3d, axis=2, where=_wh_partial), np.array(_res))
assert_allclose(
a.std(axis=1, where=whf), np.std(a[whf].reshape((5, 3)), axis=1)
)
assert_allclose(
np.std(a, axis=1, where=whf), (a[whf].reshape((5, 3))).std(axis=1)
)
assert_allclose(a.std(axis=0, where=whp), np.std(a[whp[:, 0]], axis=0))
assert_allclose(np.std(a, axis=0, where=whp), (a[whp[:, 0]]).std(axis=0))
with pytest.warns(RuntimeWarning) as w:
assert_equal(a.std(where=False), np.nan)
with pytest.warns(RuntimeWarning) as w:
assert_equal(np.std(a, where=False), np.nan)
class TestVdot(TestCase):
def test_basic(self):
dt_numeric = np.typecodes["AllFloat"] + np.typecodes["AllInteger"]
dt_complex = np.typecodes["Complex"]
# test real
a = np.eye(3)
for dt in dt_numeric:
b = a.astype(dt)
res = np.vdot(b, b)
assert_(np.isscalar(res))
assert_equal(np.vdot(b, b), 3)
# test complex
a = np.eye(3) * 1j
for dt in dt_complex:
b = a.astype(dt)
res = np.vdot(b, b)
assert_(np.isscalar(res))
assert_equal(np.vdot(b, b), 3)
# test boolean
b = np.eye(3, dtype=bool)
res = np.vdot(b, b)
assert_(np.isscalar(res))
assert_equal(np.vdot(b, b), True)
@xpassIfTorchDynamo # (reason="implement order='F'")
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order="C")
b = np.array([[1, 2], [3, 4]], order="F")
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
def test_vdot_uncontiguous(self):
for size in [2, 1000]:
# Different sizes match different branches in vdot.
a = np.zeros((size, 2, 2))
b = np.zeros((size, 2, 2))
a[:, 0, 0] = np.arange(size)
b[:, 0, 0] = np.arange(size) + 1
# Make a and b uncontiguous:
a = a[..., 0]
b = b[..., 0]
assert_equal(np.vdot(a, b), np.vdot(a.flatten(), b.flatten()))
assert_equal(np.vdot(a, b.copy()), np.vdot(a.flatten(), b.flatten()))
assert_equal(np.vdot(a.copy(), b), np.vdot(a.flatten(), b.flatten()))
@xpassIfTorchDynamo # (reason="implement order='F'")
def test_vdot_uncontiguous_2(self):
# test order='F' separately
for size in [2, 1000]:
# Different sizes match different branches in vdot.
a = np.zeros((size, 2, 2))
b = np.zeros((size, 2, 2))
a[:, 0, 0] = np.arange(size)
b[:, 0, 0] = np.arange(size) + 1
# Make a and b uncontiguous:
a = a[..., 0]
b = b[..., 0]
assert_equal(np.vdot(a.copy("F"), b), np.vdot(a.flatten(), b.flatten()))
assert_equal(np.vdot(a, b.copy("F")), np.vdot(a.flatten(), b.flatten()))
@instantiate_parametrized_tests
class TestDot(TestCase):
def setUp(self):
np.random.seed(128)
# Numpy and pytorch random streams differ, so inline the
# values from numpy 1.24.1
# self.A = np.random.rand(4, 2)
self.A = np.array(
[
[0.86663704, 0.26314485],
[0.13140848, 0.04159344],
[0.23892433, 0.6454746],
[0.79059935, 0.60144244],
]
)
# self.b1 = np.random.rand(2, 1)
self.b1 = np.array([[0.33429937], [0.11942846]])
# self.b2 = np.random.rand(2)
self.b2 = np.array([0.30913305, 0.10972379])
# self.b3 = np.random.rand(1, 2)
self.b3 = np.array([[0.60211331, 0.25128496]])
# self.b4 = np.random.rand(4)
self.b4 = np.array([0.29968129, 0.517116, 0.71520252, 0.9314494])
self.N = 7
def test_dotmatmat(self):
A = self.A
res = np.dot(A.transpose(), A)
tgt = np.array([[1.45046013, 0.86323640], [0.86323640, 0.84934569]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotmatvec(self):
A, b1 = self.A, self.b1
res = np.dot(A, b1)
tgt = np.array([[0.32114320], [0.04889721], [0.15696029], [0.33612621]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotmatvec2(self):
A, b2 = self.A, self.b2
res = np.dot(A, b2)
tgt = np.array([0.29677940, 0.04518649, 0.14468333, 0.31039293])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecmat(self):
A, b4 = self.A, self.b4
res = np.dot(b4, A)
tgt = np.array([1.23495091, 1.12222648])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecmat2(self):
b3, A = self.b3, self.A
res = np.dot(b3, A.transpose())
tgt = np.array([[0.58793804, 0.08957460, 0.30605758, 0.62716383]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecmat3(self):
A, b4 = self.A, self.b4
res = np.dot(A.transpose(), b4)
tgt = np.array([1.23495091, 1.12222648])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecvecouter(self):
b1, b3 = self.b1, self.b3
res = np.dot(b1, b3)
tgt = np.array([[0.20128610, 0.08400440], [0.07190947, 0.03001058]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecvecinner(self):
b1, b3 = self.b1, self.b3
res = np.dot(b3, b1)
tgt = np.array([[0.23129668]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotcolumnvect1(self):
b1 = np.ones((3, 1))
b2 = [5.3]
res = np.dot(b1, b2)
tgt = np.array([5.3, 5.3, 5.3])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotcolumnvect2(self):
b1 = np.ones((3, 1)).transpose()
b2 = [6.2]
res = np.dot(b2, b1)
tgt = np.array([6.2, 6.2, 6.2])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecscalar(self):
np.random.seed(100)
# Numpy guarantees the random stream, and we don't. So inline the
# values from numpy 1.24.1
# b1 = np.random.rand(1, 1)
b1 = np.array([[0.54340494]])
# b2 = np.random.rand(1, 4)
b2 = np.array([[0.27836939, 0.42451759, 0.84477613, 0.00471886]])
res = np.dot(b1, b2)
tgt = np.array([[0.15126730, 0.23068496, 0.45905553, 0.00256425]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_dotvecscalar2(self):
np.random.seed(100)
# b1 = np.random.rand(4, 1)
b1 = np.array([[0.54340494], [0.27836939], [0.42451759], [0.84477613]])
# b2 = np.random.rand(1, 1)
b2 = np.array([[0.00471886]])
res = np.dot(b1, b2)
tgt = np.array([[0.00256425], [0.00131359], [0.00200324], [0.00398638]])
assert_almost_equal(res, tgt, decimal=self.N)
def test_all(self):
dims = [(), (1,), (1, 1)]
dout = [(), (1,), (1, 1), (1,), (), (1,), (1, 1), (1,), (1, 1)]
for dim, (dim1, dim2) in zip(dout, itertools.product(dims, dims)):
b1 = np.zeros(dim1)
b2 = np.zeros(dim2)
res = np.dot(b1, b2)
tgt = np.zeros(dim)
assert_(res.shape == tgt.shape)
assert_almost_equal(res, tgt, decimal=self.N)
@skip(reason="numpy internals")
def test_dot_2args(self):
from numpy.core.multiarray import dot
a = np.array([[1, 2], [3, 4]], dtype=float)
b = np.array([[1, 0], [1, 1]], dtype=float)
c = np.array([[3, 2], [7, 4]], dtype=float)
d = dot(a, b)
assert_allclose(c, d)
@skip(reason="numpy internals")
def test_dot_3args(self):
from numpy.core.multiarray import dot
np.random.seed(22)
f = np.random.random_sample((1024, 16))
v = np.random.random_sample((16, 32))
r = np.empty((1024, 32))
for i in range(12):
dot(f, v, r)
if HAS_REFCOUNT:
assert_equal(sys.getrefcount(r), 2)
r2 = dot(f, v, out=None)
assert_array_equal(r2, r)
assert_(r is dot(f, v, out=r))
v = v[:, 0].copy() # v.shape == (16,)
r = r[:, 0].copy() # r.shape == (1024,)
r2 = dot(f, v)
assert_(r is dot(f, v, r))
assert_array_equal(r2, r)
@skip(reason="numpy internals")
def test_dot_3args_errors(self):
from numpy.core.multiarray import dot
np.random.seed(22)
f = np.random.random_sample((1024, 16))
v = np.random.random_sample((16, 32))
r = np.empty((1024, 31))
assert_raises(ValueError, dot, f, v, r)
r = np.empty((1024,))
assert_raises(ValueError, dot, f, v, r)
r = np.empty((32,))
assert_raises(ValueError, dot, f, v, r)
r = np.empty((32, 1024))
assert_raises(ValueError, dot, f, v, r)
assert_raises(ValueError, dot, f, v, r.T)
r = np.empty((1024, 64))
assert_raises(ValueError, dot, f, v, r[:, ::2])
assert_raises(ValueError, dot, f, v, r[:, :32])
r = np.empty((1024, 32), dtype=np.float32)
assert_raises(ValueError, dot, f, v, r)
r = np.empty((1024, 32), dtype=int)
assert_raises(ValueError, dot, f, v, r)
@xpassIfTorchDynamo # (reason="TODO order='F'")
def test_dot_array_order(self):
a = np.array([[1, 2], [3, 4]], order="C")
b = np.array([[1, 2], [3, 4]], order="F")
res = np.dot(a, a)
# integer arrays are exact
assert_equal(np.dot(a, b), res)
assert_equal(np.dot(b, a), res)
assert_equal(np.dot(b, b), res)
@skip(reason="TODO: nbytes, view, __array_interface__")
def test_accelerate_framework_sgemv_fix(self):
def aligned_array(shape, align, dtype, order="C"):
d = dtype(0)
N = np.prod(shape)
tmp = np.zeros(N * d.nbytes + align, dtype=np.uint8)
address = tmp.__array_interface__["data"][0]
for offset in range(align):
if (address + offset) % align == 0:
break
tmp = tmp[offset : offset + N * d.nbytes].view(dtype=dtype)
return tmp.reshape(shape, order=order)
def as_aligned(arr, align, dtype, order="C"):
aligned = aligned_array(arr.shape, align, dtype, order)
aligned[:] = arr[:]
return aligned
def assert_dot_close(A, X, desired):
assert_allclose(np.dot(A, X), desired, rtol=1e-5, atol=1e-7)
m = aligned_array(100, 15, np.float32)
s = aligned_array((100, 100), 15, np.float32)
np.dot(s, m) # this will always segfault if the bug is present
testdata = itertools.product((15, 32), (10000,), (200, 89), ("C", "F"))
for align, m, n, a_order in testdata:
# Calculation in double precision
A_d = np.random.rand(m, n)
X_d = np.random.rand(n)
desired = np.dot(A_d, X_d)
# Calculation with aligned single precision
A_f = as_aligned(A_d, align, np.float32, order=a_order)
X_f = as_aligned(X_d, align, np.float32)
assert_dot_close(A_f, X_f, desired)
# Strided A rows
A_d_2 = A_d[::2]
desired = np.dot(A_d_2, X_d)
A_f_2 = A_f[::2]
assert_dot_close(A_f_2, X_f, desired)
# Strided A columns, strided X vector
A_d_22 = A_d_2[:, ::2]
X_d_2 = X_d[::2]
desired = np.dot(A_d_22, X_d_2)
A_f_22 = A_f_2[:, ::2]
X_f_2 = X_f[::2]
assert_dot_close(A_f_22, X_f_2, desired)
# Check the strides are as expected
if a_order == "F":
assert_equal(A_f_22.strides, (8, 8 * m))
else:
assert_equal(A_f_22.strides, (8 * n, 8))
assert_equal(X_f_2.strides, (8,))
# Strides in A rows + cols only
X_f_2c = as_aligned(X_f_2, align, np.float32)
assert_dot_close(A_f_22, X_f_2c, desired)
# Strides just in A cols
A_d_12 = A_d[:, ::2]
desired = np.dot(A_d_12, X_d_2)
A_f_12 = A_f[:, ::2]
assert_dot_close(A_f_12, X_f_2c, desired)
# Strides in A cols and X
assert_dot_close(A_f_12, X_f_2, desired)
@slow
@parametrize("dtype", [np.float64, np.complex128])
@requires_memory(free_bytes=18e9) # complex case needs 18GiB+
def test_huge_vectordot(self, dtype):
# Large vector multiplications are chunked with 32bit BLAS
# Test that the chunking does the right thing, see also gh-22262
data = np.ones(2**30 + 100, dtype=dtype)
res = np.dot(data, data)
assert res == 2**30 + 100
class MatmulCommon:
"""Common tests for '@' operator and numpy.matmul."""
# Should work with these types. Will want to add
# "O" at some point
types = "?bhilBefdFD"
def test_exceptions(self):
dims = [
((1,), (2,)), # mismatched vector vector
(
(
2,
1,
),
(2,),
), # mismatched matrix vector
((2,), (1, 2)), # mismatched vector matrix
((1, 2), (3, 1)), # mismatched matrix matrix
((1,), ()), # vector scalar
((), (1)), # scalar vector
((1, 1), ()), # matrix scalar
((), (1, 1)), # scalar matrix
((2, 2, 1), (3, 1, 2)), # cannot broadcast
]
for dt, (dm1, dm2) in itertools.product(self.types, dims):
a = np.ones(dm1, dtype=dt)
b = np.ones(dm2, dtype=dt)
assert_raises((RuntimeError, ValueError), self.matmul, a, b)
def test_shapes(self):
dims = [
((1, 1), (2, 1, 1)), # broadcast first argument
((2, 1, 1), (1, 1)), # broadcast second argument
((2, 1, 1), (2, 1, 1)), # matrix stack sizes match
]
for dt, (dm1, dm2) in itertools.product(self.types, dims):
a = np.ones(dm1, dtype=dt)
b = np.ones(dm2, dtype=dt)
res = self.matmul(a, b)
assert_(res.shape == (2, 1, 1))
# vector vector returns scalars.
for dt in self.types:
a = np.ones((2,), dtype=dt)
b = np.ones((2,), dtype=dt)
c = self.matmul(a, b)
assert_(np.array(c).shape == ())
def test_result_types(self):
mat = np.ones((1, 1))
vec = np.ones((1,))
for dt in self.types:
m = mat.astype(dt)
v = vec.astype(dt)
for arg in [(m, v), (v, m), (m, m)]:
res = self.matmul(*arg)
assert_(res.dtype == dt)
@xpassIfTorchDynamo # (reason="no scalars")
def test_result_types_2(self):
# in numpy, vector vector returns scalars
# we return a 0D array instead
for dt in self.types:
v = np.ones((1,)).astype(dt)
if dt != "O":
res = self.matmul(v, v)
assert_(type(res) is np.dtype(dt).type)
def test_scalar_output(self):
vec1 = np.array([2])
vec2 = np.array([3, 4]).reshape(1, -1)
tgt = np.array([6, 8])
for dt in self.types[1:]:
v1 = vec1.astype(dt)
v2 = vec2.astype(dt)
res = self.matmul(v1, v2)
assert_equal(res, tgt)
res = self.matmul(v2.T, v1)
assert_equal(res, tgt)
# boolean type
vec = np.array([True, True], dtype="?").reshape(1, -1)
res = self.matmul(vec[:, 0], vec)
assert_equal(res, True)
def test_vector_vector_values(self):
vec1 = np.array([1, 2])
vec2 = np.array([3, 4]).reshape(-1, 1)
tgt1 = np.array([11])
tgt2 = np.array([[3, 6], [4, 8]])
for dt in self.types[1:]:
v1 = vec1.astype(dt)
v2 = vec2.astype(dt)
res = self.matmul(v1, v2)
assert_equal(res, tgt1)
# no broadcast, we must make v1 into a 2d ndarray
res = self.matmul(v2, v1.reshape(1, -1))
assert_equal(res, tgt2)
# boolean type
vec = np.array([True, True], dtype="?")
res = self.matmul(vec, vec)
assert_equal(res, True)
def test_vector_matrix_values(self):
vec = np.array([1, 2])
mat1 = np.array([[1, 2], [3, 4]])
mat2 = np.stack([mat1] * 2, axis=0)
tgt1 = np.array([7, 10])
tgt2 = np.stack([tgt1] * 2, axis=0)
for dt in self.types[1:]:
v = vec.astype(dt)
m1 = mat1.astype(dt)
m2 = mat2.astype(dt)
res = self.matmul(v, m1)
assert_equal(res, tgt1)
res = self.matmul(v, m2)
assert_equal(res, tgt2)
# boolean type
vec = np.array([True, False])
mat1 = np.array([[True, False], [False, True]])
mat2 = np.stack([mat1] * 2, axis=0)
tgt1 = np.array([True, False])
tgt2 = np.stack([tgt1] * 2, axis=0)
res = self.matmul(vec, mat1)
assert_equal(res, tgt1)
res = self.matmul(vec, mat2)
assert_equal(res, tgt2)
def test_matrix_vector_values(self):
vec = np.array([1, 2])
mat1 = np.array([[1, 2], [3, 4]])
mat2 = np.stack([mat1] * 2, axis=0)
tgt1 = np.array([5, 11])
tgt2 = np.stack([tgt1] * 2, axis=0)
for dt in self.types[1:]:
v = vec.astype(dt)
m1 = mat1.astype(dt)
m2 = mat2.astype(dt)
res = self.matmul(m1, v)
assert_equal(res, tgt1)
res = self.matmul(m2, v)
assert_equal(res, tgt2)
# boolean type
vec = np.array([True, False])
mat1 = np.array([[True, False], [False, True]])
mat2 = np.stack([mat1] * 2, axis=0)
tgt1 = np.array([True, False])
tgt2 = np.stack([tgt1] * 2, axis=0)
res = self.matmul(vec, mat1)
assert_equal(res, tgt1)
res = self.matmul(vec, mat2)
assert_equal(res, tgt2)
def test_matrix_matrix_values(self):
mat1 = np.array([[1, 2], [3, 4]])
mat2 = np.array([[1, 0], [1, 1]])
mat12 = np.stack([mat1, mat2], axis=0)
mat21 = np.stack([mat2, mat1], axis=0)
tgt11 = np.array([[7, 10], [15, 22]])
tgt12 = np.array([[3, 2], [7, 4]])
tgt21 = np.array([[1, 2], [4, 6]])
tgt12_21 = np.stack([tgt12, tgt21], axis=0)
tgt11_12 = np.stack((tgt11, tgt12), axis=0)
tgt11_21 = np.stack((tgt11, tgt21), axis=0)
for dt in self.types[1:]:
m1 = mat1.astype(dt)
m2 = mat2.astype(dt)
m12 = mat12.astype(dt)
m21 = mat21.astype(dt)
# matrix @ matrix
res = self.matmul(m1, m2)
assert_equal(res, tgt12)
res = self.matmul(m2, m1)
assert_equal(res, tgt21)
# stacked @ matrix
res = self.matmul(m12, m1)
assert_equal(res, tgt11_21)
# matrix @ stacked
res = self.matmul(m1, m12)
assert_equal(res, tgt11_12)
# stacked @ stacked
res = self.matmul(m12, m21)
assert_equal(res, tgt12_21)
# boolean type
m1 = np.array([[1, 1], [0, 0]], dtype=np.bool_)
m2 = np.array([[1, 0], [1, 1]], dtype=np.bool_)
m12 = np.stack([m1, m2], axis=0)
m21 = np.stack([m2, m1], axis=0)
tgt11 = m1
tgt12 = m1
tgt21 = np.array([[1, 1], [1, 1]], dtype=np.bool_)
tgt12_21 = np.stack([tgt12, tgt21], axis=0)
tgt11_12 = np.stack((tgt11, tgt12), axis=0)
tgt11_21 = np.stack((tgt11, tgt21), axis=0)
# matrix @ matrix
res = self.matmul(m1, m2)
assert_equal(res, tgt12)
res = self.matmul(m2, m1)
assert_equal(res, tgt21)
# stacked @ matrix
res = self.matmul(m12, m1)
assert_equal(res, tgt11_21)
# matrix @ stacked
res = self.matmul(m1, m12)
assert_equal(res, tgt11_12)
# stacked @ stacked
res = self.matmul(m12, m21)
assert_equal(res, tgt12_21)
@instantiate_parametrized_tests
class TestMatmul(MatmulCommon, TestCase):
def setUp(self):
self.matmul = np.matmul
def test_out_arg(self):
a = np.ones((5, 2), dtype=float)
b = np.array([[1, 3], [5, 7]], dtype=float)
tgt = np.dot(a, b)
# test as positional argument
msg = "out positional argument"
out = np.zeros((5, 2), dtype=float)
self.matmul(a, b, out)
assert_array_equal(out, tgt, err_msg=msg)
# test as keyword argument
msg = "out keyword argument"
out = np.zeros((5, 2), dtype=float)
self.matmul(a, b, out=out)
assert_array_equal(out, tgt, err_msg=msg)
# test out with not allowed type cast (safe casting)
msg = "Cannot cast"
out = np.zeros((5, 2), dtype=np.int32)
assert_raises_regex(TypeError, msg, self.matmul, a, b, out=out)
# test out with type upcast to complex
out = np.zeros((5, 2), dtype=np.complex128)
c = self.matmul(a, b, out=out)
assert_(c is out)
# with suppress_warnings() as sup:
# sup.filter(np.ComplexWarning, '')
c = c.astype(tgt.dtype)
assert_array_equal(c, tgt)
def test_empty_out(self):
# Check that the output cannot be broadcast, so that it cannot be
# size zero when the outer dimensions (iterator size) has size zero.
arr = np.ones((0, 1, 1))
out = np.ones((1, 1, 1))
assert self.matmul(arr, arr).shape == (0, 1, 1)
with pytest.raises((RuntimeError, ValueError)):
self.matmul(arr, arr, out=out)
def test_out_contiguous(self):
a = np.ones((5, 2), dtype=float)
b = np.array([[1, 3], [5, 7]], dtype=float)
v = np.array([1, 3], dtype=float)
tgt = np.dot(a, b)
tgt_mv = np.dot(a, v)
# test out non-contiguous
out = np.ones((5, 2, 2), dtype=float)
c = self.matmul(a, b, out=out[..., 0])
assert_array_equal(c, tgt)
c = self.matmul(a, v, out=out[:, 0, 0])
assert_array_equal(c, tgt_mv)
c = self.matmul(v, a.T, out=out[:, 0, 0])
assert_array_equal(c, tgt_mv)
# test out contiguous in only last dim
out = np.ones((10, 2), dtype=float)
c = self.matmul(a, b, out=out[::2, :])
assert_array_equal(c, tgt)
# test transposes of out, args
out = np.ones((5, 2), dtype=float)
c = self.matmul(b.T, a.T, out=out.T)
assert_array_equal(out, tgt)
@xfailIfTorchDynamo
def test_out_contiguous_2(self):
a = np.ones((5, 2), dtype=float)
b = np.array([[1, 3], [5, 7]], dtype=float)
# test out non-contiguous
out = np.ones((5, 2, 2), dtype=float)
c = self.matmul(a, b, out=out[..., 0])
assert c.tensor._base is out.tensor
m1 = np.arange(15.0).reshape(5, 3)
m2 = np.arange(21.0).reshape(3, 7)
m3 = np.arange(30.0).reshape(5, 6)[:, ::2] # non-contiguous
vc = np.arange(10.0)
vr = np.arange(6.0)
m0 = np.zeros((3, 0))
@parametrize(
"args",
(
# matrix-matrix
subtest((m1, m2), name="mm1"),
subtest((m2.T, m1.T), name="mm2"),
subtest((m2.T.copy(), m1.T), name="mm3"),
subtest((m2.T, m1.T.copy()), name="mm4"),
# matrix-matrix-transpose, contiguous and non
subtest((m1, m1.T), name="mmT1"),
subtest((m1.T, m1), name="mmT2"),
subtest((m1, m3.T), name="mmT3"),
subtest((m3, m1.T), name="mmT4"),
subtest((m3, m3.T), name="mmT5"),
subtest((m3.T, m3), name="mmT6"),
# matrix-matrix non-contiguous
subtest((m3, m2), name="mmN1"),
subtest((m2.T, m3.T), name="mmN2"),
subtest((m2.T.copy(), m3.T), name="mmN3"),
# vector-matrix, matrix-vector, contiguous
subtest((m1, vr[:3]), name="vm1"),
subtest((vc[:5], m1), name="vm2"),
subtest((m1.T, vc[:5]), name="vm3"),
subtest((vr[:3], m1.T), name="vm4"),
# vector-matrix, matrix-vector, vector non-contiguous
subtest((m1, vr[::2]), name="mvN1"),
subtest((vc[::2], m1), name="mvN2"),
subtest((m1.T, vc[::2]), name="mvN3"),
subtest((vr[::2], m1.T), name="mvN4"),
# vector-matrix, matrix-vector, matrix non-contiguous
subtest((m3, vr[:3]), name="mvN5"),
subtest((vc[:5], m3), name="mvN6"),
subtest((m3.T, vc[:5]), name="mvN7"),
subtest((vr[:3], m3.T), name="mvN8"),
# vector-matrix, matrix-vector, both non-contiguous
subtest((m3, vr[::2]), name="mvN9"),
subtest((vc[::2], m3), name="mvn10"),
subtest((m3.T, vc[::2]), name="mv11"),
subtest((vr[::2], m3.T), name="mv12"),
# size == 0
subtest((m0, m0.T), name="s0_1"),
subtest((m0.T, m0), name="s0_2"),
subtest((m1, m0), name="s0_3"),
subtest((m0.T, m1.T), name="s0_4"),
),
)
def test_dot_equivalent(self, args):
r1 = np.matmul(*args)
r2 = np.dot(*args)
assert_equal(r1, r2)
r3 = np.matmul(args[0].copy(), args[1].copy())
assert_equal(r1, r3)
@skip(reason="object arrays")
def test_matmul_exception_multiply(self):
# test that matmul fails if `__mul__` is missing
class add_not_multiply:
def __add__(self, other):
return self
a = np.full((3, 3), add_not_multiply())
with assert_raises(TypeError):
b = np.matmul(a, a)
@skip(reason="object arrays")
def test_matmul_exception_add(self):
# test that matmul fails if `__add__` is missing
class multiply_not_add:
def __mul__(self, other):
return self
a = np.full((3, 3), multiply_not_add())
with assert_raises(TypeError):
b = np.matmul(a, a)
def test_matmul_bool(self):
# gh-14439
a = np.array([[1, 0], [1, 1]], dtype=bool)
assert np.max(a.view(np.uint8)) == 1
b = np.matmul(a, a)
# matmul with boolean output should always be 0, 1
assert np.max(b.view(np.uint8)) == 1
# rg = np.random.default_rng(np.random.PCG64(43))
# d = rg.integers(2, size=4*5, dtype=np.int8)
# d = d.reshape(4, 5) > 0
np.random.seed(1234)
d = np.random.randint(2, size=(4, 5)) > 0
out1 = np.matmul(d, d.reshape(5, 4))
out2 = np.dot(d, d.reshape(5, 4))
assert_equal(out1, out2)
c = np.matmul(np.zeros((2, 0), dtype=bool), np.zeros(0, dtype=bool))
assert not np.any(c)
class TestMatmulOperator(MatmulCommon, TestCase):
import operator
matmul = operator.matmul
@skip(reason="no __array_priority__")
def test_array_priority_override(self):
class A:
__array_priority__ = 1000
def __matmul__(self, other):
return "A"
def __rmatmul__(self, other):
return "A"
a = A()
b = np.ones(2)
assert_equal(self.matmul(a, b), "A")
assert_equal(self.matmul(b, a), "A")
def test_matmul_raises(self):
assert_raises(
(RuntimeError, TypeError, ValueError), self.matmul, np.int8(5), np.int8(5)
)
@xpassIfTorchDynamo # (reason="torch supports inplace matmul, and so do we")
@skipif(numpy.__version__ >= "1.26", reason="This is fixed in numpy 1.26")
def test_matmul_inplace(self):
# It would be nice to support in-place matmul eventually, but for now
# we don't have a working implementation, so better just to error out
# and nudge people to writing "a = a @ b".
a = np.eye(3)
b = np.eye(3)
assert_raises(TypeError, a.__imatmul__, b)
@xfail # XXX: what's up with exec under Dynamo
def test_matmul_inplace_2(self):
a = np.eye(3)
b = np.eye(3)
assert_raises(TypeError, operator.imatmul, a, b)
assert_raises(TypeError, exec, "a @= b", globals(), locals())
@xpassIfTorchDynamo # (reason="matmul_axes")
def test_matmul_axes(self):
a = np.arange(3 * 4 * 5).reshape(3, 4, 5)
c = np.matmul(a, a, axes=[(-2, -1), (-1, -2), (1, 2)])
assert c.shape == (3, 4, 4)
d = np.matmul(a, a, axes=[(-2, -1), (-1, -2), (0, 1)])
assert d.shape == (4, 4, 3)
e = np.swapaxes(d, 0, 2)
assert_array_equal(e, c)
f = np.matmul(a, np.arange(3), axes=[(1, 0), (0), (0)])
assert f.shape == (4, 5)
class TestInner(TestCase):
def test_inner_scalar_and_vector(self):
for dt in np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "?":
sca = np.array(3, dtype=dt)[()]
vec = np.array([1, 2], dtype=dt)
desired = np.array([3, 6], dtype=dt)
assert_equal(np.inner(vec, sca), desired)
assert_equal(np.inner(sca, vec), desired)
def test_vecself(self):
# Ticket 844.
# Inner product of a vector with itself segfaults or give
# meaningless result
a = np.zeros(shape=(1, 80), dtype=np.float64)
p = np.inner(a, a)
assert_almost_equal(p, 0, decimal=14)
def test_inner_product_with_various_contiguities(self):
# github issue 6532
for dt in np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "?":
# check an inner product involving a matrix transpose
A = np.array([[1, 2], [3, 4]], dtype=dt)
B = np.array([[1, 3], [2, 4]], dtype=dt)
C = np.array([1, 1], dtype=dt)
desired = np.array([4, 6], dtype=dt)
assert_equal(np.inner(A.T, C), desired)
assert_equal(np.inner(C, A.T), desired)
assert_equal(np.inner(B, C), desired)
assert_equal(np.inner(C, B), desired)
# check a matrix product
desired = np.array([[7, 10], [15, 22]], dtype=dt)
assert_equal(np.inner(A, B), desired)
# check the syrk vs. gemm paths
desired = np.array([[5, 11], [11, 25]], dtype=dt)
assert_equal(np.inner(A, A), desired)
assert_equal(np.inner(A, A.copy()), desired)
@skip(reason="[::-1] not supported")
def test_inner_product_reversed_view(self):
for dt in np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "?":
# check an inner product involving an aliased and reversed view
a = np.arange(5).astype(dt)
b = a[::-1]
desired = np.array(10, dtype=dt).item()
assert_equal(np.inner(b, a), desired)
def test_3d_tensor(self):
for dt in np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "?":
a = np.arange(24).reshape(2, 3, 4).astype(dt)
b = np.arange(24, 48).reshape(2, 3, 4).astype(dt)
desired = np.array(
[
[
[[158, 182, 206], [230, 254, 278]],
[[566, 654, 742], [830, 918, 1006]],
[[974, 1126, 1278], [1430, 1582, 1734]],
],
[
[[1382, 1598, 1814], [2030, 2246, 2462]],
[[1790, 2070, 2350], [2630, 2910, 3190]],
[[2198, 2542, 2886], [3230, 3574, 3918]],
],
]
).astype(dt)
assert_equal(np.inner(a, b), desired)
assert_equal(np.inner(b, a).transpose(2, 3, 0, 1), desired)
@instantiate_parametrized_tests
class TestChoose(TestCase):
def setUp(self):
self.x = 2 * np.ones((3,), dtype=int)
self.y = 3 * np.ones((3,), dtype=int)
self.x2 = 2 * np.ones((2, 3), dtype=int)
self.y2 = 3 * np.ones((2, 3), dtype=int)
self.ind = [0, 0, 1]
def test_basic(self):
A = np.choose(self.ind, (self.x, self.y))
assert_equal(A, [2, 2, 3])
def test_broadcast1(self):
A = np.choose(self.ind, (self.x2, self.y2))
assert_equal(A, [[2, 2, 3], [2, 2, 3]])
def test_broadcast2(self):
A = np.choose(self.ind, (self.x, self.y2))
assert_equal(A, [[2, 2, 3], [2, 2, 3]])
# XXX: revisit xfails when NEP 50 lands in numpy
@skip(reason="XXX: revisit xfails when NEP 50 lands in numpy")
@parametrize(
"ops",
[
(1000, np.array([1], dtype=np.uint8)),
(-1, np.array([1], dtype=np.uint8)),
(1.0, np.float32(3)),
(1.0, np.array([3], dtype=np.float32)),
],
)
def test_output_dtype(self, ops):
expected_dt = np.result_type(*ops)
assert np.choose([0], ops).dtype == expected_dt
def test_docstring_1(self):
# examples from the docstring,
# https://numpy.org/doc/1.23/reference/generated/numpy.choose.html
choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]]
A = np.choose([2, 3, 1, 0], choices)
assert_equal(A, [20, 31, 12, 3])
def test_docstring_2(self):
a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
choices = [-10, 10]
A = np.choose(a, choices)
assert_equal(A, [[10, -10, 10], [-10, 10, -10], [10, -10, 10]])
def test_docstring_3(self):
a = np.array([0, 1]).reshape((2, 1, 1))
c1 = np.array([1, 2, 3]).reshape((1, 3, 1))
c2 = np.array([-1, -2, -3, -4, -5]).reshape((1, 1, 5))
A = np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2
expected = np.array(
[
[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3]],
[[-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5]],
]
)
assert_equal(A, expected)
class TestRepeat(TestCase):
def setUp(self):
self.m = np.array([1, 2, 3, 4, 5, 6])
self.m_rect = self.m.reshape((2, 3))
def test_basic(self):
A = np.repeat(self.m, [1, 3, 2, 1, 1, 2])
assert_equal(A, [1, 2, 2, 2, 3, 3, 4, 5, 6, 6])
def test_broadcast1(self):
A = np.repeat(self.m, 2)
assert_equal(A, [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
def test_axis_spec(self):
A = np.repeat(self.m_rect, [2, 1], axis=0)
assert_equal(A, [[1, 2, 3], [1, 2, 3], [4, 5, 6]])
A = np.repeat(self.m_rect, [1, 3, 2], axis=1)
assert_equal(A, [[1, 2, 2, 2, 3, 3], [4, 5, 5, 5, 6, 6]])
def test_broadcast2(self):
A = np.repeat(self.m_rect, 2, axis=0)
assert_equal(A, [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]])
A = np.repeat(self.m_rect, 2, axis=1)
assert_equal(A, [[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6]])
# TODO: test for multidimensional
NEIGH_MODE = {"zero": 0, "one": 1, "constant": 2, "circular": 3, "mirror": 4}
@xpassIfTorchDynamo # (reason="TODO")
class TestWarnings(TestCase):
def test_complex_warning(self):
x = np.array([1, 2])
y = np.array([1 - 2j, 1 + 2j])
with warnings.catch_warnings():
warnings.simplefilter("error", np.ComplexWarning)
assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y)
assert_equal(x, [1, 2])
class TestMinScalarType(TestCase):
def test_usigned_shortshort(self):
dt = np.min_scalar_type(2**8 - 1)
wanted = np.dtype("uint8")
assert_equal(wanted, dt)
# three tests below are added based on what numpy does
def test_complex(self):
dt = np.min_scalar_type(0 + 0j)
assert dt == np.dtype("complex64")
def test_float(self):
dt = np.min_scalar_type(0.1)
assert dt == np.dtype("float16")
def test_nonscalar(self):
dt = np.min_scalar_type([0, 1, 2])
assert dt == np.dtype("int64")
from numpy.core._internal import _dtype_from_pep3118
@skip(reason="dont worry about buffer protocol")
class TestPEP3118Dtype(TestCase):
def _check(self, spec, wanted):
dt = np.dtype(wanted)
actual = _dtype_from_pep3118(spec)
assert_equal(actual, dt, err_msg=f"spec {spec!r} != dtype {wanted!r}")
def test_native_padding(self):
align = np.dtype("i").alignment
for j in range(8):
if j == 0:
s = "bi"
else:
s = "b%dxi" % j
self._check(
"@" + s, {"f0": ("i1", 0), "f1": ("i", align * (1 + j // align))}
)
self._check("=" + s, {"f0": ("i1", 0), "f1": ("i", 1 + j)})
def test_native_padding_2(self):
# Native padding should work also for structs and sub-arrays
self._check("x3T{xi}", {"f0": (({"f0": ("i", 4)}, (3,)), 4)})
self._check("^x3T{xi}", {"f0": (({"f0": ("i", 1)}, (3,)), 1)})
def test_trailing_padding(self):
# Trailing padding should be included, *and*, the item size
# should match the alignment if in aligned mode
align = np.dtype("i").alignment
size = np.dtype("i").itemsize
def aligned(n):
return align * (1 + (n - 1) // align)
base = dict(formats=["i"], names=["f0"])
self._check("ix", dict(itemsize=aligned(size + 1), **base))
self._check("ixx", dict(itemsize=aligned(size + 2), **base))
self._check("ixxx", dict(itemsize=aligned(size + 3), **base))
self._check("ixxxx", dict(itemsize=aligned(size + 4), **base))
self._check("i7x", dict(itemsize=aligned(size + 7), **base))
self._check("^ix", dict(itemsize=size + 1, **base))
self._check("^ixx", dict(itemsize=size + 2, **base))
self._check("^ixxx", dict(itemsize=size + 3, **base))
self._check("^ixxxx", dict(itemsize=size + 4, **base))
self._check("^i7x", dict(itemsize=size + 7, **base))
def test_native_padding_3(self):
dt = np.dtype(
[("a", "b"), ("b", "i"), ("sub", np.dtype("b,i")), ("c", "i")], align=True
)
self._check("T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxxi:c:}", dt)
dt = np.dtype(
[
("a", "b"),
("b", "i"),
("c", "b"),
("d", "b"),
("e", "b"),
("sub", np.dtype("b,i", align=True)),
]
)
self._check("T{b:a:=i:b:b:c:b:d:b:e:T{b:f0:xxxi:f1:}:sub:}", dt)
def test_padding_with_array_inside_struct(self):
dt = np.dtype(
[("a", "b"), ("b", "i"), ("c", "b", (3,)), ("d", "i")], align=True
)
self._check("T{b:a:xxxi:b:3b:c:xi:d:}", dt)
def test_byteorder_inside_struct(self):
# The byte order after @T{=i} should be '=', not '@'.
# Check this by noting the absence of native alignment.
self._check("@T{^i}xi", {"f0": ({"f0": ("i", 0)}, 0), "f1": ("i", 5)})
def test_intra_padding(self):
# Natively aligned sub-arrays may require some internal padding
align = np.dtype("i").alignment
size = np.dtype("i").itemsize
def aligned(n):
return align * (1 + (n - 1) // align)
self._check(
"(3)T{ix}",
(
dict(
names=["f0"], formats=["i"], offsets=[0], itemsize=aligned(size + 1)
),
(3,),
),
)
def test_char_vs_string(self):
dt = np.dtype("c")
self._check("c", dt)
dt = np.dtype([("f0", "S1", (4,)), ("f1", "S4")])
self._check("4c4s", dt)
def test_field_order(self):
# gh-9053 - previously, we relied on dictionary key order
self._check("(0)I:a:f:b:", [("a", "I", (0,)), ("b", "f")])
self._check("(0)I:b:f:a:", [("b", "I", (0,)), ("a", "f")])
def test_unnamed_fields(self):
self._check("ii", [("f0", "i"), ("f1", "i")])
self._check("ii:f0:", [("f1", "i"), ("f0", "i")])
self._check("i", "i")
self._check("i:f0:", [("f0", "i")])
# NOTE: xpassIfTorchDynamo below
# 1. TODO: torch._numpy does not handle/model _CopyMode
# 2. order= keyword not supported (probably won't be)
# 3. Under TEST_WITH_TORCHDYNAMO many of these make it through due
# to a graph break leaving the _CopyMode to only be handled by numpy.
@skipif(numpy.__version__ < "1.23", reason="CopyMode is new in NumPy 1.22")
@xpassIfTorchDynamo
@instantiate_parametrized_tests
class TestArrayCreationCopyArgument(TestCase):
class RaiseOnBool:
def __bool__(self):
raise ValueError
# true_vals = [True, np._CopyMode.ALWAYS, np.True_]
# false_vals = [False, np._CopyMode.IF_NEEDED, np.False_]
true_vals = [True, 1, np.True_]
false_vals = [False, 0, np.False_]
def test_scalars(self):
# Test both numpy and python scalars
for dtype in np.typecodes["All"]:
arr = np.zeros((), dtype=dtype)
scalar = arr[()]
pyscalar = arr.item(0)
# Test never-copy raises error:
assert_raises(ValueError, np.array, scalar, copy=np._CopyMode.NEVER)
assert_raises(ValueError, np.array, pyscalar, copy=np._CopyMode.NEVER)
assert_raises(ValueError, np.array, pyscalar, copy=self.RaiseOnBool())
# Casting with a dtype (to unsigned integers) can be special:
with pytest.raises(ValueError):
np.array(pyscalar, dtype=np.int64, copy=np._CopyMode.NEVER)
@xfail # TODO: handle `_CopyMode` properly in torch._numpy
def test_compatible_cast(self):
# Some types are compatible even though they are different, no
# copy is necessary for them. This is mostly true for some integers
def int_types(byteswap=False):
int_types = np.typecodes["Integer"] + np.typecodes["UnsignedInteger"]
for int_type in int_types:
yield np.dtype(int_type)
if byteswap:
yield np.dtype(int_type).newbyteorder()
for int1 in int_types():
for int2 in int_types(True):
arr = np.arange(10, dtype=int1)
for copy in self.true_vals:
res = np.array(arr, copy=copy, dtype=int2)
assert res is not arr and res.flags.owndata
assert_array_equal(res, arr)
if int1 == int2:
# Casting is not necessary, base check is sufficient here
for copy in self.false_vals:
res = np.array(arr, copy=copy, dtype=int2)
assert res is arr or res.base is arr
res = np.array(arr, copy=np._CopyMode.NEVER, dtype=int2)
assert res is arr or res.base is arr
else:
# Casting is necessary, assert copy works:
for copy in self.false_vals:
res = np.array(arr, copy=copy, dtype=int2)
assert res is not arr and res.flags.owndata
assert_array_equal(res, arr)
assert_raises(
ValueError, np.array, arr, copy=np._CopyMode.NEVER, dtype=int2
)
assert_raises(ValueError, np.array, arr, copy=None, dtype=int2)
def test_buffer_interface(self):
# Buffer interface gives direct memory access (no copy)
arr = np.arange(10)
view = memoryview(arr)
# Checking bases is a bit tricky since numpy creates another
# memoryview, so use may_share_memory.
for copy in self.true_vals:
res = np.array(view, copy=copy)
assert not np.may_share_memory(arr, res)
for copy in self.false_vals:
res = np.array(view, copy=copy)
assert np.may_share_memory(arr, res)
res = np.array(view, copy=np._CopyMode.NEVER)
assert np.may_share_memory(arr, res)
def test_array_interfaces(self):
# Array interface gives direct memory access (much like a memoryview)
base_arr = np.arange(10)
class ArrayLike:
__array_interface__ = base_arr.__array_interface__
arr = ArrayLike()
for copy, val in [
(True, None),
(np._CopyMode.ALWAYS, None),
(False, arr),
(np._CopyMode.IF_NEEDED, arr),
(np._CopyMode.NEVER, arr),
]:
res = np.array(arr, copy=copy)
assert res.base is val
def test___array__(self):
base_arr = np.arange(10)
class ArrayLike:
def __array__(self):
# __array__ should return a copy, numpy cannot know this
# however.
return base_arr
arr = ArrayLike()
for copy in self.true_vals:
res = np.array(arr, copy=copy)
assert_array_equal(res, base_arr)
# An additional copy is currently forced by numpy in this case,
# you could argue, numpy does not trust the ArrayLike. This
# may be open for change:
assert res is not base_arr
for copy in self.false_vals:
res = np.array(arr, copy=False)
assert_array_equal(res, base_arr)
assert res is base_arr # numpy trusts the ArrayLike
with pytest.raises(ValueError):
np.array(arr, copy=np._CopyMode.NEVER)
@parametrize("arr", [np.ones(()), np.arange(81).reshape((9, 9))])
@parametrize("order1", ["C", "F", None])
@parametrize("order2", ["C", "F", "A", "K"])
def test_order_mismatch(self, arr, order1, order2):
# The order is the main (python side) reason that can cause
# a never-copy to fail.
# Prepare C-order, F-order and non-contiguous arrays:
arr = arr.copy(order1)
if order1 == "C":
assert arr.flags.c_contiguous
elif order1 == "F":
assert arr.flags.f_contiguous
elif arr.ndim != 0:
# Make array non-contiguous
arr = arr[::2, ::2]
assert not arr.flags.forc
# Whether a copy is necessary depends on the order of arr:
if order2 == "C":
no_copy_necessary = arr.flags.c_contiguous
elif order2 == "F":
no_copy_necessary = arr.flags.f_contiguous
else:
# Keeporder and Anyorder are OK with non-contiguous output.
# This is not consistent with the `astype` behaviour which
# enforces contiguity for "A". It is probably historic from when
# "K" did not exist.
no_copy_necessary = True
# Test it for both the array and a memoryview
for view in [arr, memoryview(arr)]:
for copy in self.true_vals:
res = np.array(view, copy=copy, order=order2)
assert res is not arr and res.flags.owndata
assert_array_equal(arr, res)
if no_copy_necessary:
for copy in self.false_vals:
res = np.array(view, copy=copy, order=order2)
# res.base.obj refers to the memoryview
if not IS_PYPY:
assert res is arr or res.base.obj is arr
res = np.array(view, copy=np._CopyMode.NEVER, order=order2)
if not IS_PYPY:
assert res is arr or res.base.obj is arr
else:
for copy in self.false_vals:
res = np.array(arr, copy=copy, order=order2)
assert_array_equal(arr, res)
assert_raises(
ValueError, np.array, view, copy=np._CopyMode.NEVER, order=order2
)
assert_raises(ValueError, np.array, view, copy=None, order=order2)
def test_striding_not_ok(self):
arr = np.array([[1, 2, 4], [3, 4, 5]])
assert_raises(ValueError, np.array, arr.T, copy=np._CopyMode.NEVER, order="C")
assert_raises(
ValueError,
np.array,
arr.T,
copy=np._CopyMode.NEVER,
order="C",
dtype=np.int64,
)
assert_raises(ValueError, np.array, arr, copy=np._CopyMode.NEVER, order="F")
assert_raises(
ValueError,
np.array,
arr,
copy=np._CopyMode.NEVER,
order="F",
dtype=np.int64,
)
class TestArrayAttributeDeletion(TestCase):
def test_multiarray_writable_attributes_deletion(self):
# ticket #2046, should not seqfault, raise AttributeError
a = np.ones(2)
attr = ["shape", "strides", "data", "dtype", "real", "imag", "flat"]
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "Assigning the 'data' attribute")
for s in attr:
assert_raises(AttributeError, delattr, a, s)
def test_multiarray_not_writable_attributes_deletion(self):
a = np.ones(2)
attr = [
"ndim",
"flags",
"itemsize",
"size",
"nbytes",
"base",
"ctypes",
"T",
"__array_interface__",
"__array_struct__",
"__array_priority__",
"__array_finalize__",
]
for s in attr:
assert_raises(AttributeError, delattr, a, s)
def test_multiarray_flags_writable_attribute_deletion(self):
a = np.ones(2).flags
attr = ["writebackifcopy", "updateifcopy", "aligned", "writeable"]
for s in attr:
assert_raises(AttributeError, delattr, a, s)
def test_multiarray_flags_not_writable_attribute_deletion(self):
a = np.ones(2).flags
attr = [
"contiguous",
"c_contiguous",
"f_contiguous",
"fortran",
"owndata",
"fnc",
"forc",
"behaved",
"carray",
"farray",
"num",
]
for s in attr:
assert_raises(AttributeError, delattr, a, s)
@skip # not supported, too brittle, too annoying
@instantiate_parametrized_tests
class TestArrayInterface(TestCase):
class Foo:
def __init__(self, value):
self.value = value
self.iface = {"typestr": "f8"}
def __float__(self):
return float(self.value)
@property
def __array_interface__(self):
return self.iface
f = Foo(0.5)
@parametrize(
"val, iface, expected",
[
(f, {}, 0.5),
([f], {}, [0.5]),
([f, f], {}, [0.5, 0.5]),
(f, {"shape": ()}, 0.5),
(f, {"shape": None}, TypeError),
(f, {"shape": (1, 1)}, [[0.5]]),
(f, {"shape": (2,)}, ValueError),
(f, {"strides": ()}, 0.5),
(f, {"strides": (2,)}, ValueError),
(f, {"strides": 16}, TypeError),
],
)
def test_scalar_interface(self, val, iface, expected):
# Test scalar coercion within the array interface
self.f.iface = {"typestr": "f8"}
self.f.iface.update(iface)
if HAS_REFCOUNT:
pre_cnt = sys.getrefcount(np.dtype("f8"))
if isinstance(expected, type):
assert_raises(expected, np.array, val)
else:
result = np.array(val)
assert_equal(np.array(val), expected)
assert result.dtype == "f8"
del result
if HAS_REFCOUNT:
post_cnt = sys.getrefcount(np.dtype("f8"))
assert_equal(pre_cnt, post_cnt)
class TestDelMisc(TestCase):
@xpassIfTorchDynamo # (reason="TODO")
def test_flat_element_deletion(self):
it = np.ones(3).flat
try:
del it[1]
del it[1:2]
except TypeError:
pass
except Exception:
raise AssertionError from None
class TestConversion(TestCase):
def test_array_scalar_relational_operation(self):
# All integer
for dt1 in np.typecodes["AllInteger"]:
assert_(1 > np.array(0, dtype=dt1), f"type {dt1} failed")
assert_(not 1 < np.array(0, dtype=dt1), f"type {dt1} failed")
for dt2 in np.typecodes["AllInteger"]:
assert_(
np.array(1, dtype=dt1) > np.array(0, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
assert_(
not np.array(1, dtype=dt1) < np.array(0, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
# Unsigned integers
for dt1 in "B":
assert_(-1 < np.array(1, dtype=dt1), f"type {dt1} failed")
assert_(not -1 > np.array(1, dtype=dt1), f"type {dt1} failed")
assert_(-1 != np.array(1, dtype=dt1), f"type {dt1} failed")
# Unsigned vs signed
for dt2 in "bhil":
assert_(
np.array(1, dtype=dt1) > np.array(-1, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
assert_(
not np.array(1, dtype=dt1) < np.array(-1, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
assert_(
np.array(1, dtype=dt1) != np.array(-1, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
# Signed integers and floats
for dt1 in "bhl" + np.typecodes["Float"]:
assert_(1 > np.array(-1, dtype=dt1), f"type {dt1} failed")
assert_(not 1 < np.array(-1, dtype=dt1), f"type {dt1} failed")
assert_(-1 == np.array(-1, dtype=dt1), f"type {dt1} failed")
for dt2 in "bhl" + np.typecodes["Float"]:
assert_(
np.array(1, dtype=dt1) > np.array(-1, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
assert_(
not np.array(1, dtype=dt1) < np.array(-1, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
assert_(
np.array(-1, dtype=dt1) == np.array(-1, dtype=dt2),
f"type {dt1} and {dt2} failed",
)
@skip(reason="object arrays")
def test_to_bool_scalar(self):
assert_equal(bool(np.array([False])), False)
assert_equal(bool(np.array([True])), True)
assert_equal(bool(np.array([[42]])), True)
assert_raises(ValueError, bool, np.array([1, 2]))
class NotConvertible:
def __bool__(self):
raise NotImplementedError
assert_raises(NotImplementedError, bool, np.array(NotConvertible()))
assert_raises(NotImplementedError, bool, np.array([NotConvertible()]))
if IS_PYSTON:
raise SkipTest("Pyston disables recursion checking")
self_containing = np.array([None])
self_containing[0] = self_containing
Error = RecursionError
assert_raises(Error, bool, self_containing) # previously stack overflow
self_containing[0] = None # resolve circular reference
def test_to_int_scalar(self):
# gh-9972 means that these aren't always the same
int_funcs = (int, lambda x: x.__int__())
for int_func in int_funcs:
assert_equal(int_func(np.array(0)), 0)
assert_equal(int_func(np.array([1])), 1)
assert_equal(int_func(np.array([[42]])), 42)
assert_raises((ValueError, TypeError), int_func, np.array([1, 2]))
@skip(reason="object arrays")
def test_to_int_scalar_2(self):
int_funcs = (int, lambda x: x.__int__())
for int_func in int_funcs:
# gh-9972
assert_equal(4, int_func(np.array("4")))
assert_equal(5, int_func(np.bytes_(b"5")))
assert_equal(6, int_func(np.str_("6")))
# The delegation of int() to __trunc__ was deprecated in
# Python 3.11.
if sys.version_info < (3, 11):
class HasTrunc:
def __trunc__(self):
return 3
assert_equal(3, int_func(np.array(HasTrunc())))
assert_equal(3, int_func(np.array([HasTrunc()])))
else:
pass
class NotConvertible:
def __int__(self):
raise NotImplementedError
assert_raises(NotImplementedError, int_func, np.array(NotConvertible()))
assert_raises(NotImplementedError, int_func, np.array([NotConvertible()]))
class TestWhere(TestCase):
def test_basic(self):
dts = [bool, np.int16, np.int32, np.int64, np.double, np.complex128]
for dt in dts:
c = np.ones(53, dtype=bool)
assert_equal(np.where(c, dt(0), dt(1)), dt(0))
assert_equal(np.where(~c, dt(0), dt(1)), dt(1))
assert_equal(np.where(True, dt(0), dt(1)), dt(0))
assert_equal(np.where(False, dt(0), dt(1)), dt(1))
d = np.ones_like(c).astype(dt)
e = np.zeros_like(d)
r = d.astype(dt)
c[7] = False
r[7] = e[7]
assert_equal(np.where(c, e, e), e)
assert_equal(np.where(c, d, e), r)
assert_equal(np.where(c, d, e[0]), r)
assert_equal(np.where(c, d[0], e), r)
assert_equal(np.where(c[::2], d[::2], e[::2]), r[::2])
assert_equal(np.where(c[1::2], d[1::2], e[1::2]), r[1::2])
assert_equal(np.where(c[::3], d[::3], e[::3]), r[::3])
assert_equal(np.where(c[1::3], d[1::3], e[1::3]), r[1::3])
# assert_equal(np.where(c[::-2], d[::-2], e[::-2]), r[::-2])
# assert_equal(np.where(c[::-3], d[::-3], e[::-3]), r[::-3])
# assert_equal(np.where(c[1::-3], d[1::-3], e[1::-3]), r[1::-3])
def test_exotic(self):
# zero sized
m = np.array([], dtype=bool).reshape(0, 3)
b = np.array([], dtype=np.float64).reshape(0, 3)
assert_array_equal(np.where(m, 0, b), np.array([]).reshape(0, 3))
@skip(reason="object arrays")
def test_exotic_2(self):
# object cast
d = np.array(
[
-1.34,
-0.16,
-0.54,
-0.31,
-0.08,
-0.95,
0.000,
0.313,
0.547,
-0.18,
0.876,
0.236,
1.969,
0.310,
0.699,
1.013,
1.267,
0.229,
-1.39,
0.487,
]
)
nan = float("NaN")
e = np.array(
[
"5z",
"0l",
nan,
"Wz",
nan,
nan,
"Xq",
"cs",
nan,
nan,
"QN",
nan,
nan,
"Fd",
nan,
nan,
"kp",
nan,
"36",
"i1",
],
dtype=object,
)
m = np.array(
[0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0], dtype=bool
)
r = e[:]
r[np.where(m)] = d[np.where(m)]
assert_array_equal(np.where(m, d, e), r)
r = e[:]
r[np.where(~m)] = d[np.where(~m)]
assert_array_equal(np.where(m, e, d), r)
assert_array_equal(np.where(m, e, e), e)
# minimal dtype result with NaN scalar (e.g required by pandas)
d = np.array([1.0, 2.0], dtype=np.float32)
e = float("NaN")
assert_equal(np.where(True, d, e).dtype, np.float32)
e = float("Infinity")
assert_equal(np.where(True, d, e).dtype, np.float32)
e = float("-Infinity")
assert_equal(np.where(True, d, e).dtype, np.float32)
# also check upcast
e = 1e150
assert_equal(np.where(True, d, e).dtype, np.float64)
def test_ndim(self):
c = [True, False]
a = np.zeros((2, 25))
b = np.ones((2, 25))
r = np.where(np.array(c)[:, np.newaxis], a, b)
assert_array_equal(r[0], a[0])
assert_array_equal(r[1], b[0])
a = a.T
b = b.T
r = np.where(c, a, b)
assert_array_equal(r[:, 0], a[:, 0])
assert_array_equal(r[:, 1], b[:, 0])
def test_dtype_mix(self):
c = np.array(
[
False,
True,
False,
False,
False,
False,
True,
False,
False,
False,
True,
False,
]
)
a = np.uint8(1)
b = np.array(
[5.0, 0.0, 3.0, 2.0, -1.0, -4.0, 0.0, -10.0, 10.0, 1.0, 0.0, 3.0],
dtype=np.float64,
)
r = np.array(
[5.0, 1.0, 3.0, 2.0, -1.0, -4.0, 1.0, -10.0, 10.0, 1.0, 1.0, 3.0],
dtype=np.float64,
)
assert_equal(np.where(c, a, b), r)
a = a.astype(np.float32)
b = b.astype(np.int64)
assert_equal(np.where(c, a, b), r)
# non bool mask
c = c.astype(int)
c[c != 0] = 34242324
assert_equal(np.where(c, a, b), r)
# invert
tmpmask = c != 0
c[c == 0] = 41247212
c[tmpmask] = 0
assert_equal(np.where(c, b, a), r)
@skip(reason="endianness")
def test_foreign(self):
c = np.array(
[
False,
True,
False,
False,
False,
False,
True,
False,
False,
False,
True,
False,
]
)
r = np.array(
[5.0, 1.0, 3.0, 2.0, -1.0, -4.0, 1.0, -10.0, 10.0, 1.0, 1.0, 3.0],
dtype=np.float64,
)
a = np.ones(1, dtype=">i4")
b = np.array(
[5.0, 0.0, 3.0, 2.0, -1.0, -4.0, 0.0, -10.0, 10.0, 1.0, 0.0, 3.0],
dtype=np.float64,
)
assert_equal(np.where(c, a, b), r)
b = b.astype(">f8")
assert_equal(np.where(c, a, b), r)
a = a.astype("<i4")
assert_equal(np.where(c, a, b), r)
c = c.astype(">i4")
assert_equal(np.where(c, a, b), r)
def test_error(self):
c = [True, True]
a = np.ones((4, 5))
b = np.ones((5, 5))
assert_raises((RuntimeError, ValueError), np.where, c, a, a)
assert_raises((RuntimeError, ValueError), np.where, c[0], a, b)
def test_empty_result(self):
# pass empty where result through an assignment which reads the data of
# empty arrays, error detectable with valgrind, see gh-8922
x = np.zeros((1, 1))
ibad = np.vstack(np.where(x == 99.0))
assert_array_equal(ibad, np.atleast_2d(np.array([[], []], dtype=np.intp)))
def test_largedim(self):
# invalid read regression gh-9304
shape = [10, 2, 3, 4, 5, 6]
np.random.seed(2)
array = np.random.rand(*shape)
for i in range(10):
benchmark = array.nonzero()
result = array.nonzero()
assert_array_equal(benchmark, result)
def test_kwargs(self):
a = np.zeros(1)
with assert_raises(TypeError):
np.where(a, x=a, y=a)
class TestHashing(TestCase):
def test_arrays_not_hashable(self):
x = np.ones(3)
assert_raises(TypeError, hash, x)
def test_collections_hashable(self):
x = np.array([])
assert_(not isinstance(x, collections.abc.Hashable))
class TestFormat(TestCase):
@xpassIfTorchDynamo # (reason="TODO")
def test_0d(self):
a = np.array(np.pi)
assert_equal(f"{a:0.3g}", "3.14")
assert_equal(f"{a[()]:0.3g}", "3.14")
def test_1d_no_format(self):
a = np.array([np.pi])
assert_equal(f"{a}", str(a))
def test_1d_format(self):
# until gh-5543, ensure that the behaviour matches what it used to be
a = np.array([np.pi])
assert_raises(TypeError, "{:30}".format, a)
from numpy.testing import IS_PYPY
class TestWritebackIfCopy(TestCase):
# all these tests use the WRITEBACKIFCOPY mechanism
def test_argmax_with_out(self):
mat = np.eye(5)
out = np.empty(5, dtype="i2")
res = np.argmax(mat, 0, out=out)
assert_equal(res, range(5))
def test_argmin_with_out(self):
mat = -np.eye(5)
out = np.empty(5, dtype="i2")
res = np.argmin(mat, 0, out=out)
assert_equal(res, range(5))
@xpassIfTorchDynamo # (reason="XXX: place()")
def test_insert_noncontiguous(self):
a = np.arange(6).reshape(2, 3).T # force non-c-contiguous
# uses arr_insert
np.place(a, a > 2, [44, 55])
assert_equal(a, np.array([[0, 44], [1, 55], [2, 44]]))
# hit one of the failing paths
assert_raises(ValueError, np.place, a, a > 20, [])
def test_put_noncontiguous(self):
a = np.arange(6).reshape(2, 3).T # force non-c-contiguous
assert not a.flags["C_CONTIGUOUS"] # sanity check
np.put(a, [0, 2], [44, 55])
assert_equal(a, np.array([[44, 3], [55, 4], [2, 5]]))
@xpassIfTorchDynamo # (reason="XXX: putmask()")
def test_putmask_noncontiguous(self):
a = np.arange(6).reshape(2, 3).T # force non-c-contiguous
# uses arr_putmask
np.putmask(a, a > 2, a**2)
assert_equal(a, np.array([[0, 9], [1, 16], [2, 25]]))
def test_take_mode_raise(self):
a = np.arange(6, dtype="int")
out = np.empty(2, dtype="int")
np.take(a, [0, 2], out=out, mode="raise")
assert_equal(out, np.array([0, 2]))
def test_choose_mod_raise(self):
a = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
out = np.empty((3, 3), dtype="int")
choices = [-10, 10]
np.choose(a, choices, out=out, mode="raise")
assert_equal(out, np.array([[10, -10, 10], [-10, 10, -10], [10, -10, 10]]))
@xpassIfTorchDynamo # (reason="XXX: ndarray.flat")
def test_flatiter__array__(self):
a = np.arange(9).reshape(3, 3)
b = a.T.flat
c = b.__array__()
# triggers the WRITEBACKIFCOPY resolution, assuming refcount semantics
del c
def test_dot_out(self):
# if HAVE_CBLAS, will use WRITEBACKIFCOPY
a = np.arange(9, dtype=float).reshape(3, 3)
b = np.dot(a, a, out=a)
assert_equal(b, np.array([[15, 18, 21], [42, 54, 66], [69, 90, 111]]))
@instantiate_parametrized_tests
class TestArange(TestCase):
def test_infinite(self):
assert_raises(
(RuntimeError, ValueError), np.arange, 0, np.inf # "unsupported range",
)
def test_nan_step(self):
assert_raises(
(RuntimeError, ValueError), # "cannot compute length",
np.arange,
0,
1,
np.nan,
)
def test_zero_step(self):
assert_raises(ZeroDivisionError, np.arange, 0, 10, 0)
assert_raises(ZeroDivisionError, np.arange, 0.0, 10.0, 0.0)
# empty range
assert_raises(ZeroDivisionError, np.arange, 0, 0, 0)
assert_raises(ZeroDivisionError, np.arange, 0.0, 0.0, 0.0)
def test_require_range(self):
assert_raises(TypeError, np.arange)
assert_raises(TypeError, np.arange, step=3)
assert_raises(TypeError, np.arange, dtype="int64")
@xpassIfTorchDynamo # (reason="weird arange signature (optionals before required args)")
def test_require_range_2(self):
assert_raises(TypeError, np.arange, start=4)
def test_start_stop_kwarg(self):
keyword_stop = np.arange(stop=3)
keyword_zerotostop = np.arange(start=0, stop=3)
keyword_start_stop = np.arange(start=3, stop=9)
assert len(keyword_stop) == 3
assert len(keyword_zerotostop) == 3
assert len(keyword_start_stop) == 6
assert_array_equal(keyword_stop, keyword_zerotostop)
@skip(reason="arange for booleans: numpy maybe deprecates?")
def test_arange_booleans(self):
# Arange makes some sense for booleans and works up to length 2.
# But it is weird since `arange(2, 4, dtype=bool)` works.
# Arguably, much or all of this could be deprecated/removed.
res = np.arange(False, dtype=bool)
assert_array_equal(res, np.array([], dtype="bool"))
res = np.arange(True, dtype="bool")
assert_array_equal(res, [False])
res = np.arange(2, dtype="bool")
assert_array_equal(res, [False, True])
# This case is especially weird, but drops out without special case:
res = np.arange(6, 8, dtype="bool")
assert_array_equal(res, [True, True])
with pytest.raises(TypeError):
np.arange(3, dtype="bool")
@parametrize("which", [0, 1, 2])
def test_error_paths_and_promotion(self, which):
args = [0, 1, 2] # start, stop, and step
args[which] = np.float64(2.0) # should ensure float64 output
assert np.arange(*args).dtype == np.float64
# repeat with non-empty ranges
args = [0, 8, 2]
args[which] = np.float64(2.0)
assert np.arange(*args).dtype == np.float64
@parametrize("dt", [np.float32, np.uint8, complex])
def test_explicit_dtype(self, dt):
assert np.arange(5.0, dtype=dt).dtype == dt
class TestRichcompareScalar(TestCase):
@skip # XXX: brittle, fails or passes under dynamo depending on the NumPy version
def test_richcompare_scalar_boolean_singleton_return(self):
# These are currently guaranteed to be the boolean singletons, but maybe
# returning NumPy booleans would also be OK:
assert (np.array(0) == "a") is False
assert (np.array(0) != "a") is True
assert (np.int16(0) == "a") is False
assert (np.int16(0) != "a") is True
@skip # (reason="implement views/dtypes")
class TestViewDtype(TestCase):
"""
Verify that making a view of a non-contiguous array works as expected.
"""
def test_smaller_dtype_multiple(self):
# x is non-contiguous
x = np.arange(10, dtype="<i4")[::2]
with pytest.raises(ValueError, match="the last axis must be contiguous"):
x.view("<i2")
expected = [[0, 0], [2, 0], [4, 0], [6, 0], [8, 0]]
assert_array_equal(x[:, np.newaxis].view("<i2"), expected)
def test_smaller_dtype_not_multiple(self):
# x is non-contiguous
x = np.arange(5, dtype="<i4")[::2]
with pytest.raises(ValueError, match="the last axis must be contiguous"):
x.view("S3")
with pytest.raises(ValueError, match="When changing to a smaller dtype"):
x[:, np.newaxis].view("S3")
# Make sure the problem is because of the dtype size
expected = [[b""], [b"\x02"], [b"\x04"]]
assert_array_equal(x[:, np.newaxis].view("S4"), expected)
def test_larger_dtype_multiple(self):
# x is non-contiguous in the first dimension, contiguous in the last
x = np.arange(20, dtype="<i2").reshape(10, 2)[::2, :]
expected = np.array(
[[65536], [327684], [589832], [851980], [1114128]], dtype="<i4"
)
assert_array_equal(x.view("<i4"), expected)
def test_larger_dtype_not_multiple(self):
# x is non-contiguous in the first dimension, contiguous in the last
x = np.arange(20, dtype="<i2").reshape(10, 2)[::2, :]
with pytest.raises(ValueError, match="When changing to a larger dtype"):
x.view("S3")
# Make sure the problem is because of the dtype size
expected = [
[b"\x00\x00\x01"],
[b"\x04\x00\x05"],
[b"\x08\x00\t"],
[b"\x0c\x00\r"],
[b"\x10\x00\x11"],
]
assert_array_equal(x.view("S4"), expected)
def test_f_contiguous(self):
# x is F-contiguous
x = np.arange(4 * 3, dtype="<i4").reshape(4, 3).T
with pytest.raises(ValueError, match="the last axis must be contiguous"):
x.view("<i2")
def test_non_c_contiguous(self):
# x is contiguous in axis=-1, but not C-contiguous in other axes
x = np.arange(2 * 3 * 4, dtype="i1").reshape(2, 3, 4).transpose(1, 0, 2)
expected = [
[[256, 770], [3340, 3854]],
[[1284, 1798], [4368, 4882]],
[[2312, 2826], [5396, 5910]],
]
assert_array_equal(x.view("<i2"), expected)
@instantiate_parametrized_tests
class TestSortFloatMisc(TestCase):
# Test various array sizes that hit different code paths in quicksort-avx512
@parametrize(
"N", [8, 16, 24, 32, 48, 64, 96, 128, 151, 191, 256, 383, 512, 1023, 2047]
)
def test_sort_float(self, N):
# Regular data with nan sprinkled
np.random.seed(42)
arr = -0.5 + np.random.sample(N).astype("f")
arr[np.random.choice(arr.shape[0], 3)] = np.nan
assert_equal(np.sort(arr, kind="quick"), np.sort(arr, kind="heap"))
# (2) with +INF
infarr = np.inf * np.ones(N, dtype="f")
infarr[np.random.choice(infarr.shape[0], 5)] = -1.0
assert_equal(np.sort(infarr, kind="quick"), np.sort(infarr, kind="heap"))
# (3) with -INF
neginfarr = -np.inf * np.ones(N, dtype="f")
neginfarr[np.random.choice(neginfarr.shape[0], 5)] = 1.0
assert_equal(np.sort(neginfarr, kind="quick"), np.sort(neginfarr, kind="heap"))
# (4) with +/-INF
infarr = np.inf * np.ones(N, dtype="f")
infarr[np.random.choice(infarr.shape[0], (int)(N / 2))] = -np.inf
assert_equal(np.sort(infarr, kind="quick"), np.sort(infarr, kind="heap"))
def test_sort_int(self):
# Random data with NPY_MAX_INT32 and NPY_MIN_INT32 sprinkled
# rng = np.random.default_rng(42)
np.random.seed(1234)
N = 2047
minv = np.iinfo(np.int32).min
maxv = np.iinfo(np.int32).max
arr = np.random.randint(low=minv, high=maxv, size=N).astype("int32")
arr[np.random.choice(arr.shape[0], 10)] = minv
arr[np.random.choice(arr.shape[0], 10)] = maxv
assert_equal(np.sort(arr, kind="quick"), np.sort(arr, kind="heap"))
if __name__ == "__main__":
run_tests()
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_python_type
|
def test_python_type(self):
for x in (np.float16(1.0), 1, 1.0, 1 + 0j):
assert_equal(np.mean([x]), 1.0)
assert_equal(np.std([x]), 0.0)
assert_equal(np.var([x]), 0.0)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_keepdims
|
def test_keepdims(self):
mat = np.eye(3)
for f in self.funcs:
for axis in [0, 1]:
res = f(mat, axis=axis, keepdims=True)
assert_(res.ndim == mat.ndim)
assert_(res.shape[axis] == 1)
for axis in [None]:
res = f(mat, axis=axis, keepdims=True)
assert_(res.shape == (1, 1))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_out
|
def test_out(self):
mat = np.eye(3)
for f in self.funcs:
out = np.zeros(3)
tgt = f(mat, axis=1)
res = f(mat, axis=1, out=out)
assert_almost_equal(res, out)
assert_almost_equal(res, tgt)
out = np.empty(2)
assert_raises(ValueError, f, mat, axis=1, out=out)
out = np.empty((2, 2))
assert_raises(ValueError, f, mat, axis=1, out=out)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dtype_from_input
|
def test_dtype_from_input(self):
icodes = np.typecodes["AllInteger"]
fcodes = np.typecodes["AllFloat"]
# integer types
for f in self.funcs:
for c in icodes:
mat = np.eye(3, dtype=c)
tgt = np.float64
res = f(mat, axis=1).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None).dtype.type
assert_(res is tgt)
# mean for float types
for f in [_mean]:
for c in fcodes:
mat = np.eye(3, dtype=c)
tgt = mat.dtype.type
res = f(mat, axis=1).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None).dtype.type
assert_(res is tgt)
# var, std for float types
for f in [_var, _std]:
for c in fcodes:
mat = np.eye(3, dtype=c)
# deal with complex types
tgt = mat.real.dtype.type
res = f(mat, axis=1).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None).dtype.type
assert_(res is tgt)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dtype_from_dtype
|
def test_dtype_from_dtype(self):
mat = np.eye(3)
# stats for integer types
# FIXME:
# this needs definition as there are lots places along the line
# where type casting may take place.
# for f in self.funcs:
# for c in np.typecodes['AllInteger']:
# tgt = np.dtype(c).type
# res = f(mat, axis=1, dtype=c).dtype.type
# assert_(res is tgt)
# # scalar case
# res = f(mat, axis=None, dtype=c).dtype.type
# assert_(res is tgt)
# stats for float types
for f in self.funcs:
for c in np.typecodes["AllFloat"]:
tgt = np.dtype(c).type
res = f(mat, axis=1, dtype=c).dtype.type
assert_(res is tgt)
# scalar case
res = f(mat, axis=None, dtype=c).dtype.type
assert_(res is tgt)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ddof
|
def test_ddof(self):
for f in [_var]:
for ddof in range(3):
dim = self.rmat.shape[1]
tgt = f(self.rmat, axis=1) * dim
res = f(self.rmat, axis=1, ddof=ddof) * (dim - ddof)
for f in [_std]:
for ddof in range(3):
dim = self.rmat.shape[1]
tgt = f(self.rmat, axis=1) * np.sqrt(dim)
res = f(self.rmat, axis=1, ddof=ddof) * np.sqrt(dim - ddof)
assert_almost_equal(res, tgt)
assert_almost_equal(res, tgt)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ddof_too_big
|
def test_ddof_too_big(self):
dim = self.rmat.shape[1]
for f in [_var, _std]:
for ddof in range(dim, dim + 2):
# with warnings.catch_warnings(record=True) as w:
# warnings.simplefilter('always')
res = f(self.rmat, axis=1, ddof=ddof)
assert_(not (res < 0).any())
# assert_(len(w) > 0)
# assert_(issubclass(w[0].category, RuntimeWarning))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_mean_values
|
def test_mean_values(self):
for mat in [self.rmat, self.cmat]:
for axis in [0, 1]:
tgt = mat.sum(axis=axis)
res = _mean(mat, axis=axis) * mat.shape[axis]
assert_almost_equal(res, tgt)
for axis in [None]:
tgt = mat.sum(axis=axis)
res = _mean(mat, axis=axis) * np.prod(mat.shape)
assert_almost_equal(res, tgt)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_mean_float16
|
def test_mean_float16(self):
# This fail if the sum inside mean is done in float16 instead
# of float32.
assert_(_mean(np.ones(100000, dtype="float16")) == 1)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.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)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_var_complex_values
|
def test_var_complex_values(self, complex_dtype, ndec):
# Test fast-paths for every builtin complex type
for axis in [0, 1, None]:
mat = self.cmat.copy().astype(complex_dtype)
msqr = _mean(mat * mat.conj(), axis=axis)
mean = _mean(mat, axis=axis)
tgt = msqr - mean * mean.conjugate()
res = _var(mat, axis=axis)
assert_almost_equal(res, tgt, decimal=ndec)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_var_complex_byteorder
|
def test_var_complex_byteorder(self):
# Test that var fast-path does not cause failures for complex arrays
# with non-native byteorder
cmat = self.cmat.copy().astype("complex128")
cmat_swapped = cmat.astype(cmat.dtype.newbyteorder())
assert_almost_equal(cmat.var(), cmat_swapped.var())
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestStats(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dotvecscalar2
|
def test_dotvecscalar2(self):
np.random.seed(100)
# b1 = np.random.rand(4, 1)
b1 = np.array([[0.54340494], [0.27836939], [0.42451759], [0.84477613]])
# b2 = np.random.rand(1, 1)
b2 = np.array([[0.00471886]])
res = np.dot(b1, b2)
tgt = np.array([[0.00256425], [0.00131359], [0.00200324], [0.00398638]])
assert_almost_equal(res, tgt, decimal=self.N)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
@instantiate_parametrized_tests
class TestDot(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
aligned_array
|
def aligned_array(shape, align, dtype, order="C"):
d = dtype(0)
N = np.prod(shape)
tmp = np.zeros(N * d.nbytes + align, dtype=np.uint8)
address = tmp.__array_interface__["data"][0]
for offset in range(align):
if (address + offset) % align == 0:
break
tmp = tmp[offset : offset + N * d.nbytes].view(dtype=dtype)
return tmp.reshape(shape, order=order)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
as_aligned
|
def as_aligned(arr, align, dtype, order="C"):
aligned = aligned_array(arr.shape, align, dtype, order)
aligned[:] = arr[:]
return aligned
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
assert_dot_close
|
def assert_dot_close(A, X, desired):
assert_allclose(np.dot(A, X), desired, rtol=1e-5, atol=1e-7)
m = aligned_array(100, 15, np.float32)
s = aligned_array((100, 100), 15, np.float32)
np.dot(s, m) # this will always segfault if the bug is present
testdata = itertools.product((15, 32), (10000,), (200, 89), ("C", "F"))
for align, m, n, a_order in testdata:
# Calculation in double precision
A_d = np.random.rand(m, n)
X_d = np.random.rand(n)
desired = np.dot(A_d, X_d)
# Calculation with aligned single precision
A_f = as_aligned(A_d, align, np.float32, order=a_order)
X_f = as_aligned(X_d, align, np.float32)
assert_dot_close(A_f, X_f, desired)
# Strided A rows
A_d_2 = A_d[::2]
desired = np.dot(A_d_2, X_d)
A_f_2 = A_f[::2]
assert_dot_close(A_f_2, X_f, desired)
# Strided A columns, strided X vector
A_d_22 = A_d_2[:, ::2]
X_d_2 = X_d[::2]
desired = np.dot(A_d_22, X_d_2)
A_f_22 = A_f_2[:, ::2]
X_f_2 = X_f[::2]
assert_dot_close(A_f_22, X_f_2, desired)
# Check the strides are as expected
if a_order == "F":
assert_equal(A_f_22.strides, (8, 8 * m))
else:
assert_equal(A_f_22.strides, (8 * n, 8))
assert_equal(X_f_2.strides, (8,))
# Strides in A rows + cols only
X_f_2c = as_aligned(X_f_2, align, np.float32)
assert_dot_close(A_f_22, X_f_2c, desired)
# Strides just in A cols
A_d_12 = A_d[:, ::2]
desired = np.dot(A_d_12, X_d_2)
A_f_12 = A_f[:, ::2]
assert_dot_close(A_f_12, X_f_2c, desired)
# Strides in A cols and X
assert_dot_close(A_f_12, X_f_2, desired)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.