Skip to content

Commit d376369

Browse files
krshrimalifacebook-github-bot
authored andcommitted
[Doc] make_tensor to torch.testing module (pytorch#63925)
Summary: This PR aims to add `make_tensor` to the `torch.testing` module in PyTorch docs. TODOs: * [x] Add examples cc: pmeier mruberry brianjo Pull Request resolved: pytorch#63925 Reviewed By: ngimel Differential Revision: D30633487 Pulled By: mruberry fbshipit-source-id: 8e5a1f880c6ece5925b4039fee8122bd739538af
1 parent 5b0dfd0 commit d376369

24 files changed

+213
-140
lines changed

docs/source/testing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ torch.testing
99
.. automodule:: torch.testing
1010

1111
.. autofunction:: assert_close
12+
.. autofunction:: make_tensor

test/test_autograd.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
from torch.autograd.profiler_util import (_format_time, EventList, FunctionEvent, FunctionEventAvg)
2525
import torch.autograd.functional as autogradF
2626
from torch.utils.checkpoint import checkpoint
27+
from torch.testing import make_tensor
2728
from torch.testing._internal.common_cuda import TEST_CUDA
2829
from torch.testing._internal.common_utils import (TestCase, run_tests, skipIfNoLapack,
2930
suppress_warnings, slowTest,
3031
load_tests,
3132
IS_WINDOWS, IS_MACOS, CudaMemoryLeakCheck,
3233
TEST_WITH_ROCM, disable_gc,
33-
gradcheck, gradgradcheck, make_tensor)
34+
gradcheck, gradgradcheck)
3435
from torch.autograd import Variable, Function, detect_anomaly, kineto_available
3536
from torch.autograd.function import InplaceFunction
3637
import torch.autograd.forward_ad as fwAD

test/test_binary_ufuncs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
from torch._six import inf, nan
1414
from torch.testing._internal.common_utils import (
1515
TestCase, iter_indices, TEST_WITH_ASAN, run_tests,
16-
torch_to_numpy_dtype_dict, make_tensor, TEST_SCIPY, set_default_dtype)
16+
torch_to_numpy_dtype_dict, TEST_SCIPY, set_default_dtype)
1717
from torch.testing._internal.common_device_type import (
1818
instantiate_device_type_tests, onlyCUDA, onlyCPU, dtypes, dtypesIfCUDA,
1919
dtypesIfCPU, deviceCountAtLeast, precisionOverride, onlyOnCPUAndCUDA,
2020
skipCUDAIfRocm, skipIf, ops)
21-
from torch.testing import all_types_and_complex_and, integral_types_and
21+
from torch.testing import all_types_and_complex_and, integral_types_and, make_tensor
2222
from torch.testing._internal.common_methods_invocations import binary_ufuncs
2323

2424
if TEST_SCIPY:

test/test_buffer_protocol.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import torch.testing._internal.common_utils as common
2+
from torch.testing import make_tensor
23
from torch.testing._internal.common_device_type import (
34
instantiate_device_type_tests,
45
dtypes
@@ -23,7 +24,7 @@ def _run_test(self, shape, dtype, count=-1, first=0, offset=None, **kwargs):
2324
if offset is None:
2425
offset = first * get_dtype_size(dtype)
2526

26-
numpy_original = common.make_tensor(shape, torch.device("cpu"), dtype).numpy()
27+
numpy_original = make_tensor(shape, torch.device("cpu"), dtype).numpy()
2728
original = memoryview(numpy_original)
2829
# First call PyTorch's version in case of errors.
2930
# If this call exits successfully, the NumPy version must also do so.
@@ -125,7 +126,7 @@ def test_invalid_positional_args(self, device, dtype):
125126

126127
@dtypes(*common.torch_to_numpy_dtype_dict.keys())
127128
def test_shared_buffer(self, device, dtype):
128-
x = common.make_tensor((1,), device, dtype)
129+
x = make_tensor((1,), device, dtype)
129130
# Modify the whole tensor
130131
arr, tensor = self._run_test(SHAPE, dtype)
131132
tensor[:] = x
@@ -158,7 +159,7 @@ def test_not_a_buffer(self, device, dtype):
158159

159160
@dtypes(*common.torch_to_numpy_dtype_dict.keys())
160161
def test_non_writable_buffer(self, device, dtype):
161-
numpy_arr = common.make_tensor((1,), device, dtype).numpy()
162+
numpy_arr = make_tensor((1,), device, dtype).numpy()
162163
byte_arr = numpy_arr.tobytes()
163164
with self.assertWarnsOnceRegex(UserWarning,
164165
r"The given buffer is not writable."):

test/test_foreach.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import re
55
import torch
66
import unittest
7+
8+
from torch.testing import make_tensor
79
from torch.testing._internal.common_utils import TestCase, run_tests, TEST_WITH_ROCM, TEST_WITH_SLOW
810
from torch.testing._internal.common_device_type import \
911
(instantiate_device_type_tests, dtypes, onlyCUDA, skipCUDAIfRocm, skipMeta, ops)
1012
from torch.testing._internal.common_methods_invocations import \
11-
(foreach_unary_op_db, foreach_binary_op_db, foreach_pointwise_op_db, foreach_minmax_op_db, make_tensor)
13+
(foreach_unary_op_db, foreach_binary_op_db, foreach_pointwise_op_db, foreach_minmax_op_db)
1214

1315
# Includes some values such that N * N won't be a multiple of 4,
1416
# which should ensure we test the vectorized and non-vectorized

test/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import numpy as np
1010

11-
from torch.testing._internal.common_utils import TestCase, run_tests, make_tensor
11+
from torch.testing import make_tensor
12+
from torch.testing._internal.common_utils import TestCase, run_tests
1213
from torch.testing._internal.common_device_type import (
1314
instantiate_device_type_tests, onlyCUDA, dtypes, dtypesIfCPU, dtypesIfCUDA,
1415
onlyOnCPUAndCUDA)

test/test_jit.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@
6969
from torch.autograd import Variable
7070
from torch.jit.annotations import BroadcastingList2, BroadcastingList3, Any # noqa: F401
7171
from torch.nn.utils.rnn import PackedSequence
72-
from torch.testing import FileCheck
73-
from torch.testing._internal.common_utils import make_tensor
72+
from torch.testing import FileCheck, make_tensor
7473
import torch.autograd.profiler
7574
import torch.cuda
7675
import torch.jit

test/test_linalg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
from torch.testing._internal.common_utils import \
1616
(TestCase, run_tests, TEST_SCIPY, IS_MACOS, IS_WINDOWS, slowTest,
17-
TEST_WITH_ASAN, make_tensor, TEST_WITH_ROCM, IS_FBCODE, IS_REMOTE_GPU,
17+
TEST_WITH_ASAN, TEST_WITH_ROCM, IS_FBCODE, IS_REMOTE_GPU,
1818
iter_indices, gradcheck, gradgradcheck)
1919
from torch.testing._internal.common_device_type import \
2020
(instantiate_device_type_tests, dtypes,
2121
onlyCPU, skipCUDAIf, skipCUDAIfNoMagma, skipCPUIfNoLapack, precisionOverride,
2222
skipCUDAIfNoMagmaAndNoCusolver, skipCUDAIfRocm, onlyOnCPUAndCUDA, dtypesIfCUDA,
2323
onlyCUDA, skipCUDAVersionIn, skipMeta, skipCUDAIfNoCusolver)
24-
from torch.testing import floating_and_complex_types, floating_types, all_types
24+
from torch.testing import floating_and_complex_types, floating_types, all_types, make_tensor
2525
from torch.testing._internal.common_cuda import SM53OrLater, tf32_on_and_off, CUDA11OrLater, CUDA9
2626
from torch.distributions.binomial import Binomial
2727

test/test_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import torch
66

77
from torch.testing import \
8-
(FileCheck, floating_and_complex_types_and, get_all_dtypes)
8+
(FileCheck, floating_and_complex_types_and, get_all_dtypes, make_tensor)
99
from torch.testing._internal.common_utils import \
10-
(TestCase, is_iterable_of_tensors, run_tests, IS_SANDCASTLE, clone_input_helper, make_tensor,
10+
(TestCase, is_iterable_of_tensors, run_tests, IS_SANDCASTLE, clone_input_helper,
1111
gradcheck, gradgradcheck, IS_IN_CI, suppress_warnings)
1212
from torch.testing._internal.common_methods_invocations import \
1313
(op_db, _NOTHING, UnaryUfuncInfo, ReductionOpInfo, SpectralFuncInfo)

test/test_reductions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
from torch._six import inf, nan
1212
from torch.testing import (
13-
integral_types_and, floating_and_complex_types_and, get_all_dtypes)
13+
integral_types_and, floating_and_complex_types_and, get_all_dtypes, make_tensor)
1414
from torch.testing._internal.common_utils import (
1515
TestCase, run_tests, skipIfNoSciPy, slowTest, torch_to_numpy_dtype_dict,
16-
IS_WINDOWS, make_tensor)
16+
IS_WINDOWS)
1717
from torch.testing._internal.common_device_type import (
1818
OpDTypes, instantiate_device_type_tests, onlyCPU, dtypes, dtypesIfCUDA, dtypesIfCPU,
1919
onlyOnCPUAndCUDA, onlyCUDA, largeTensorTest, ops, precisionOverride)

test/test_shape_ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import warnings
88

99
from torch._six import nan
10+
from torch.testing import make_tensor
1011
from torch.testing._internal.common_utils import (
11-
TestCase, run_tests, make_tensor, torch_to_numpy_dtype_dict)
12+
TestCase, run_tests, torch_to_numpy_dtype_dict)
1213
from torch.testing._internal.common_device_type import (
1314
instantiate_device_type_tests, onlyCPU, onlyCUDA, dtypes, onlyOnCPUAndCUDA,
1415
dtypesIfCPU, dtypesIfCUDA, largeTensorTest)

test/test_sort_and_select.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from torch._six import nan
66
from itertools import permutations, product
77

8-
from torch.testing import all_types, all_types_and
8+
from torch.testing import all_types, all_types_and, make_tensor
99
from torch.testing._internal.common_utils import \
10-
(TEST_WITH_ROCM, TestCase, run_tests, make_tensor, slowTest)
10+
(TEST_WITH_ROCM, TestCase, run_tests, slowTest)
1111
from torch.testing._internal.common_device_type import \
1212
(instantiate_device_type_tests, dtypes, onlyOnCPUAndCUDA,
1313
skipCUDAIfRocm, onlyCUDA, dtypesIfCUDA, dtypesIfCPU, onlyCPU, largeTensorTest)

test/test_sparse.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import random
66
from collections import defaultdict
77
import unittest
8+
from torch.testing import make_tensor
89
from torch.testing._internal.common_utils import TestCase, run_tests, skipIfRocm, do_test_dtypes, \
9-
do_test_empty_full, load_tests, TEST_NUMPY, TEST_SCIPY, IS_WINDOWS, gradcheck, coalescedonoff, make_tensor, \
10+
do_test_empty_full, load_tests, TEST_NUMPY, TEST_SCIPY, IS_WINDOWS, gradcheck, coalescedonoff, \
1011
DeterministicGuard
1112
from torch.testing._internal.common_cuda import TEST_CUDA, _get_torch_cuda_version
1213
from numbers import Number

test/test_sparse_csr.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import unittest
44
import random
55
import itertools
6+
7+
from torch.testing import make_tensor
68
from torch.testing._internal.common_utils import \
7-
(IS_MACOS, IS_WINDOWS, TestCase, run_tests, load_tests, coalescedonoff, make_tensor)
9+
(IS_MACOS, IS_WINDOWS, TestCase, run_tests, load_tests, coalescedonoff)
810
from torch.testing._internal.common_device_type import \
911
(instantiate_device_type_tests, dtypes, onlyCPU, onlyCUDA)
1012

test/test_tensor_creation_ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from itertools import product, combinations, combinations_with_replacement, permutations
99
import random
1010

11+
from torch.testing import make_tensor
1112
from torch.testing._internal.common_utils import (
1213
TestCase, run_tests, do_test_empty_full, TEST_WITH_ROCM, suppress_warnings,
13-
torch_to_numpy_dtype_dict, slowTest, make_tensor, TEST_SCIPY, IS_MACOS, IS_PPC,
14+
torch_to_numpy_dtype_dict, slowTest, TEST_SCIPY, IS_MACOS, IS_PPC,
1415
IS_WINDOWS)
1516
from torch.testing._internal.common_device_type import (
1617
instantiate_device_type_tests, deviceCountAtLeast, onlyOnCPUAndCUDA,

test/test_testing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
import torch
1212

13+
from torch.testing import make_tensor
1314
from torch.testing._internal.common_utils import \
14-
(IS_FBCODE, IS_SANDCASTLE, IS_WINDOWS, TestCase, make_tensor, run_tests, skipIfRocm, slowTest)
15+
(IS_FBCODE, IS_SANDCASTLE, IS_WINDOWS, TestCase, run_tests, skipIfRocm, slowTest)
1516
from torch.testing._internal.common_device_type import \
1617
(PYTORCH_TESTING_DEVICE_EXCEPT_FOR_KEY, PYTORCH_TESTING_DEVICE_ONLY_FOR_KEY, dtypes,
1718
get_device_type_test_bases, instantiate_device_type_tests, onlyCUDA, onlyOnCPUAndCUDA,

test/test_torch.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
from itertools import product, combinations, permutations
2828
from functools import partial
2929
from torch import multiprocessing as mp
30+
from torch.testing import make_tensor
3031
from torch.testing._internal.common_utils import (
3132
TestCase, TEST_WITH_ROCM, run_tests,
3233
IS_WINDOWS, IS_FILESYSTEM_UTF8_ENCODING, NO_MULTIPROCESSING_SPAWN,
3334
do_test_dtypes, IS_SANDCASTLE, IS_FBCODE, IS_REMOTE_GPU, load_tests, slowTest,
3435
skipCUDAMemoryLeakCheckIf, BytesIOContext, noarchTest,
3536
skipIfRocm, skipIfNoSciPy, TemporaryFileName, TemporaryDirectoryName,
36-
wrapDeterministicFlagAPITest, DeterministicGuard, CudaSyncGuard, make_tensor)
37+
wrapDeterministicFlagAPITest, DeterministicGuard, CudaSyncGuard)
3738
from multiprocessing.reduction import ForkingPickler
3839
from torch.testing._internal.common_device_type import (
3940
instantiate_device_type_tests,

test/test_unary_ufuncs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
from torch._six import inf, nan
1212
from torch.testing._internal.common_utils import (
1313
TestCase, run_tests, torch_to_numpy_dtype_dict, numpy_to_torch_dtype_dict,
14-
suppress_warnings, make_tensor, TEST_SCIPY, slowTest, skipIfNoSciPy, IS_WINDOWS)
14+
suppress_warnings, TEST_SCIPY, slowTest, skipIfNoSciPy, IS_WINDOWS)
1515
from torch.testing._internal.common_methods_invocations import (
1616
unary_ufuncs, _NOTHING)
1717
from torch.testing._internal.common_device_type import (
1818
instantiate_device_type_tests, ops, dtypes, onlyCPU, onlyOnCPUAndCUDA,
1919
onlyCUDA, dtypesIfCUDA, precisionOverride, skipCUDAIfRocm, dtypesIfCPU,
2020
OpDTypes)
2121
from torch.testing import (
22-
floating_types_and, all_types_and_complex_and, floating_and_complex_types_and)
22+
floating_types_and, all_types_and_complex_and, floating_and_complex_types_and, make_tensor)
2323

2424
if TEST_SCIPY:
2525
import scipy

test/test_view_ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from functools import partial
77
import random
88

9+
from torch.testing import make_tensor
910
from torch.testing._internal.common_utils import \
10-
(TestCase, run_tests, suppress_warnings, make_tensor)
11+
(TestCase, run_tests, suppress_warnings)
1112
from torch.testing._internal.common_device_type import \
1213
(instantiate_device_type_tests, onlyCPU, dtypes, onlyOnCPUAndCUDA)
1314

torch/testing/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ._core import * # noqa: F403
22
from ._asserts import * # noqa: F403
3+
from ._creation import * # noqa: F403
34
from ._check_kernel_launches import * # noqa: F403
45
from ._deprecated import * # noqa: F403

0 commit comments

Comments
 (0)