|
| 1 | +import torch |
| 2 | +from torch.testing._internal.common_utils import TEST_NUMPY |
| 3 | +if TEST_NUMPY: |
| 4 | + import numpy as np |
| 5 | + |
| 6 | +# From the docs, there are quite a few ways to create a tensor: |
| 7 | +# https://pytorch.org/docs/stable/tensors.html |
| 8 | + |
| 9 | +# torch.tensor() |
| 10 | +reveal_type(torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])) # E: torch.tensor.Tensor |
| 11 | +reveal_type(torch.tensor([0, 1])) # E: torch.tensor.Tensor |
| 12 | +reveal_type(torch.tensor([[0.11111, 0.222222, 0.3333333]], |
| 13 | + dtype=torch.float64, |
| 14 | + device=torch.device('cuda:0'))) # E: torch.tensor.Tensor |
| 15 | +reveal_type(torch.tensor(3.14159)) # E: torch.tensor.Tensor |
| 16 | + |
| 17 | +# torch.sparse_coo_tensor |
| 18 | +i = torch.tensor([[0, 1, 1], |
| 19 | + [2, 0, 2]]) # E: torch.tensor.Tensor |
| 20 | +v = torch.tensor([3, 4, 5], dtype=torch.float32) # E: torch.tensor.Tensor |
| 21 | +reveal_type(torch.sparse_coo_tensor(i, v, [2, 4])) # E: torch.tensor.Tensor |
| 22 | +reveal_type(torch.sparse_coo_tensor(i, v)) # E: torch.tensor.Tensor |
| 23 | +reveal_type(torch.sparse_coo_tensor(i, v, [2, 4], |
| 24 | + dtype=torch.float64, |
| 25 | + device=torch.device('cuda:0'))) # E: torch.tensor.Tensor |
| 26 | +reveal_type(torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1])) # E: torch.tensor.Tensor |
| 27 | +reveal_type(torch.sparse_coo_tensor(torch.empty([1, 0]), |
| 28 | + torch.empty([0, 2]), [1, 2])) # E: torch.tensor.Tensor |
| 29 | + |
| 30 | +# torch.as_tensor |
| 31 | +if TEST_NUMPY: |
| 32 | + a = np.array([1, 2, 3]) |
| 33 | + reveal_type(torch.as_tensor(a)) # E: torch.tensor.Tensor |
| 34 | + reveal_type(torch.as_tensor(a, device=torch.device('cuda'))) # E: torch.tensor.Tensor |
| 35 | + |
| 36 | +# torch.as_strided |
| 37 | +x = torch.randn(3, 3) |
| 38 | +reveal_type(torch.as_strided(x, (2, 2), (1, 2))) # E: torch.tensor.Tensor |
| 39 | +reveal_type(torch.as_strided(x, (2, 2), (1, 2), 1)) # E: torch.tensor.Tensor |
| 40 | + |
| 41 | +# torch.from_numpy |
| 42 | +if TEST_NUMPY: |
| 43 | + a = np.array([1, 2, 3]) |
| 44 | + reveal_type(torch.from_numpy(a)) # E: torch.tensor.Tensor |
| 45 | + |
| 46 | +# torch.zeros/zeros_like |
| 47 | +reveal_type(torch.zeros(2, 3)) # E: torch.tensor.Tensor |
| 48 | +reveal_type(torch.zeros(5)) # E: torch.tensor.Tensor |
| 49 | +reveal_type(torch.zeros_like(torch.empty(2, 3))) # E: torch.tensor.Tensor |
| 50 | + |
| 51 | +# torch.ones/ones_like |
| 52 | +reveal_type(torch.ones(2, 3)) # E: torch.tensor.Tensor |
| 53 | +reveal_type(torch.ones(5)) # E: torch.tensor.Tensor |
| 54 | +reveal_type(torch.ones_like(torch.empty(2, 3))) # E: torch.tensor.Tensor |
| 55 | + |
| 56 | +# torch.arange |
| 57 | +reveal_type(torch.arange(5)) # E: torch.tensor.Tensor |
| 58 | +reveal_type(torch.arange(1, 4)) # E: torch.tensor.Tensor |
| 59 | +reveal_type(torch.arange(1, 2.5, 0.5)) # E: torch.tensor.Tensor |
| 60 | + |
| 61 | +# torch.range |
| 62 | +reveal_type(torch.range(1, 4)) # E: torch.tensor.Tensor |
| 63 | +reveal_type(torch.range(1, 4, 0.5)) # E: torch.tensor.Tensor |
| 64 | + |
| 65 | +# torch.linspace |
| 66 | +reveal_type(torch.linspace(3, 10, steps=5)) # E: torch.tensor.Tensor |
| 67 | +reveal_type(torch.linspace(-10, 10, steps=5)) # E: torch.tensor.Tensor |
| 68 | +reveal_type(torch.linspace(start=-10, end=10, steps=5)) # E: torch.tensor.Tensor |
| 69 | +reveal_type(torch.linspace(start=-10, end=10, steps=1)) # E: torch.tensor.Tensor |
| 70 | + |
| 71 | +# torch.logspace |
| 72 | +reveal_type(torch.logspace(start=-10, end=10, steps=5)) # E: torch.tensor.Tensor |
| 73 | +reveal_type(torch.logspace(start=0.1, end=1.0, steps=5)) # E: torch.tensor.Tensor |
| 74 | +reveal_type(torch.logspace(start=0.1, end=1.0, steps=1)) # E: torch.tensor.Tensor |
| 75 | +reveal_type(torch.logspace(start=2, end=2, steps=1, base=2)) # E: torch.tensor.Tensor |
| 76 | + |
| 77 | +# torch.eye |
| 78 | +reveal_type(torch.eye(3)) # E: torch.tensor.Tensor |
| 79 | + |
| 80 | +# torch.empty/empty_like/empty_strided |
| 81 | +reveal_type(torch.empty(2, 3)) # E: torch.tensor.Tensor |
| 82 | +reveal_type(torch.empty_like(torch.empty(2, 3), dtype=torch.int64)) # E: torch.tensor.Tensor |
| 83 | +reveal_type(torch.empty_strided((2, 3), (1, 2))) # E: torch.tensor.Tensor |
| 84 | + |
| 85 | +# torch.full/full_like |
| 86 | +reveal_type(torch.full((2, 3), 3.141592)) # E: torch.tensor.Tensor |
| 87 | +reveal_type(torch.full_like(torch.full((2, 3), 3.141592), 2.71828)) # E: torch.tensor.Tensor |
| 88 | + |
| 89 | +# torch.quantize_per_tensor |
| 90 | +reveal_type(torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)) # E: torch.tensor.Tensor |
| 91 | + |
| 92 | +# torch.quantize_per_channel |
| 93 | +x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]]) |
| 94 | +quant = torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8) |
| 95 | +reveal_type(x) # E: torch.tensor.Tensor |
| 96 | + |
| 97 | +# torch.dequantize |
| 98 | +reveal_type(torch.dequantize(x)) # E: torch.tensor.Tensor |
| 99 | + |
| 100 | +# torch.complex |
| 101 | +real = torch.tensor([1, 2], dtype=torch.float32) |
| 102 | +imag = torch.tensor([3, 4], dtype=torch.float32) |
| 103 | +reveal_type(torch.complex(real, imag)) # E: torch.tensor.Tensor |
| 104 | + |
| 105 | +# torch.polar |
| 106 | +abs = torch.tensor([1, 2], dtype=torch.float64) |
| 107 | +pi = torch.acos(torch.zeros(1)).item() * 2 |
| 108 | +angle = torch.tensor([pi / 2, 5 * pi / 4], dtype=torch.float64) |
| 109 | +reveal_type(torch.polar(abs, angle)) # E: torch.tensor.Tensor |
| 110 | + |
| 111 | +# torch.heaviside |
| 112 | +inp = torch.tensor([-1.5, 0, 2.0]) |
| 113 | +values = torch.tensor([0.5]) |
| 114 | +reveal_type(torch.heaviside(inp, values)) # E: torch.tensor.Tensor |
0 commit comments