|
| 1 | +import expecttest |
| 2 | +import os |
| 3 | +import torch |
| 4 | +import torch_xla |
| 5 | +import unittest |
| 6 | + |
| 7 | + |
| 8 | +def onlyOnCPU(fn): |
| 9 | + accelerator = os.environ.get("PJRT_DEVICE").lower() |
| 10 | + return unittest.skipIf(accelerator != "cpu", "PJRT_DEVICE=CPU required")(fn) |
| 11 | + |
| 12 | + |
| 13 | +class TestOpsErrorMessage(expecttest.TestCase): |
| 14 | + |
| 15 | + def test_add_broadcast_error(self): |
| 16 | + a = torch.rand(2, 2, 4, 4, device="xla") |
| 17 | + b = torch.rand(2, 2, device="xla") |
| 18 | + |
| 19 | + def test(): |
| 20 | + return torch.add(a, b) |
| 21 | + |
| 22 | + self.assertExpectedRaisesInline( |
| 23 | + exc_type=RuntimeError, |
| 24 | + callable=test, |
| 25 | + expect="""Shapes are not compatible for broadcasting: f32[2,2,4,4] vs. f32[2,2]. Expected dimension 2 of shape f32[2,2,4,4] (4) to match dimension 0 of shape f32[2,2] (2). Either that or that any of them is either 1 or unbounded. Try reshaping one of the tensors to match the other.""" |
| 26 | + ) |
| 27 | + |
| 28 | + @onlyOnCPU |
| 29 | + def test_construct_large_tensor_raises_error(self): |
| 30 | + |
| 31 | + def test(): |
| 32 | + # When eager-mode is enabled, OOM is triggered here. |
| 33 | + a = torch.rand(1024, 1024, 1024, 1024, 1024, device=torch_xla.device()) |
| 34 | + b = a.sum() |
| 35 | + # OOM is raised when we try to bring data from the device. |
| 36 | + return b.cpu() |
| 37 | + |
| 38 | + self.assertExpectedRaisesInline( |
| 39 | + exc_type=RuntimeError, |
| 40 | + callable=test, |
| 41 | + expect="""Error preparing computation: Out of memory allocating 4503599761588224 bytes.""" |
| 42 | + ) |
| 43 | + |
| 44 | + def test_cat_raises_error_on_incompatible_shapes(self): |
| 45 | + a = torch.rand(2, 2, device=torch_xla.device()) |
| 46 | + b = torch.rand(5, 1, device=torch_xla.device()) |
| 47 | + |
| 48 | + def test(): |
| 49 | + return torch.cat([a, b]) |
| 50 | + |
| 51 | + self.assertExpectedRaisesInline( |
| 52 | + exc_type=RuntimeError, |
| 53 | + callable=test, |
| 54 | + expect="""cat(): cannot concatenate tensors of shape f32[2,2] with f32[5,1] at dimension 0. Expected shapes to be equal (except at dimension 0) or that either of them was a 1D empty tensor of size (0,).""" |
| 55 | + ) |
| 56 | + |
| 57 | + def test_div_raises_error_on_invalid_rounding_mode(self): |
| 58 | + a = torch.rand(2, 2, device=torch_xla.device()) |
| 59 | + |
| 60 | + def test(): |
| 61 | + return torch.div(a, 2, rounding_mode="bad") |
| 62 | + |
| 63 | + self.assertExpectedRaisesInline( |
| 64 | + exc_type=RuntimeError, |
| 65 | + callable=test, |
| 66 | + expect="""div(): invalid rounding mode `bad`. Expected it to be either 'trunc', 'floor', or be left unspecified.""" |
| 67 | + ) |
| 68 | + |
| 69 | + def test_flip_raises_error_on_duplicated_dims(self): |
| 70 | + a = torch.rand(2, 2, 2, 2, device=torch_xla.device()) |
| 71 | + dims = [0, 0, 0, 1, 2, 3, -1] |
| 72 | + |
| 73 | + def test(): |
| 74 | + return torch.flip(a, dims=dims) |
| 75 | + |
| 76 | + self.assertExpectedRaisesInline( |
| 77 | + exc_type=RuntimeError, |
| 78 | + callable=test, |
| 79 | + expect="""flip(): expected each dimension to appear at most once. Found dimensions: 0 (3 times), 3 (2 times). Consider changing dims from [0, 0, 0, 1, 2, 3, -1] to [0, 1, 2, 3].""" |
| 80 | + ) |
| 81 | + |
| 82 | + def test_full_raises_error_on_negative_size(self): |
| 83 | + shape = [2, -2, 2] |
| 84 | + |
| 85 | + def test(): |
| 86 | + return torch.full(shape, 1.5, device="xla") |
| 87 | + |
| 88 | + self.assertExpectedRaisesInline( |
| 89 | + exc_type=RuntimeError, |
| 90 | + callable=test, |
| 91 | + expect="""full(): expected concrete sizes (i.e. non-symbolic) to be positive values. However found negative ones: [2, -2, 2].""" |
| 92 | + ) |
| 93 | + |
| 94 | + def test_gather_raises_error_on_rank_mismatch(self): |
| 95 | + S = 2 |
| 96 | + |
| 97 | + input = torch.arange(4, device=torch_xla.device()).view(S, S) |
| 98 | + index = torch.randint(0, S, (S, S, S), device=torch_xla.device()) |
| 99 | + dim = 1 |
| 100 | + |
| 101 | + def test(): |
| 102 | + return torch.gather(input, dim, index) |
| 103 | + |
| 104 | + self.assertExpectedRaisesInline( |
| 105 | + exc_type=RuntimeError, |
| 106 | + callable=test, |
| 107 | + expect="""gather(): expected rank of input (2) and index (3) tensors to be the same.""" |
| 108 | + ) |
| 109 | + |
| 110 | + def test_gather_raises_error_on_invalid_index_size(self): |
| 111 | + S = 2 |
| 112 | + X = S + 2 |
| 113 | + |
| 114 | + input = torch.arange(16, device=torch_xla.device()).view(S, S, S, S) |
| 115 | + index = torch.randint(0, S, (X, S, X, S), device=torch_xla.device()) |
| 116 | + dim = 1 |
| 117 | + |
| 118 | + def test(): |
| 119 | + return torch.gather(input, dim, index) |
| 120 | + |
| 121 | + self.assertExpectedRaisesInline( |
| 122 | + exc_type=RuntimeError, |
| 123 | + callable=test, |
| 124 | + expect="""gather(): expected sizes of index [4, 2, 4, 2] to be smaller or equal those of input [2, 2, 2, 2] on all dimensions, except on dimension 1. However, that's not true on dimensions [0, 2].""" |
| 125 | + ) |
| 126 | + |
| 127 | + def test_random__raises_error_on_empty_interval(self): |
| 128 | + a = torch.empty(10, device=torch_xla.device()) |
| 129 | + from_ = 3 |
| 130 | + to_ = 1 |
| 131 | + |
| 132 | + def test(): |
| 133 | + return a.random_(from_, to_) |
| 134 | + |
| 135 | + self.assertExpectedRaisesInline( |
| 136 | + exc_type=RuntimeError, |
| 137 | + callable=test, |
| 138 | + expect="""random_(): expected `from` (3) to be smaller than `to` (1).""" |
| 139 | + ) |
| 140 | + |
| 141 | + def test_random__raises_error_on_value_out_of_type_value_range(self): |
| 142 | + a = torch.empty(10, device=torch_xla.device(), dtype=torch.float16) |
| 143 | + from_ = 3 |
| 144 | + to_ = 65_504 + 2 |
| 145 | + |
| 146 | + def test(): |
| 147 | + return a.random_(from_, to_) |
| 148 | + |
| 149 | + self.assertExpectedRaisesInline( |
| 150 | + exc_type=RuntimeError, |
| 151 | + callable=test, |
| 152 | + expect="""random_(): expected `to` to be within the range [-65504, 65504]. However got value 65505, which is greater than the upper bound.""" |
| 153 | + ) |
| 154 | + |
| 155 | + def test_mm_raises_error_on_non_matrix_input(self): |
| 156 | + device = torch_xla.device() |
| 157 | + a = torch.rand(2, 2, 2, device=device) |
| 158 | + b = torch.rand(2, 2, device=device) |
| 159 | + |
| 160 | + def test(): |
| 161 | + torch.mm(a, b) |
| 162 | + |
| 163 | + self.assertExpectedRaisesInline( |
| 164 | + exc_type=RuntimeError, |
| 165 | + callable=test, |
| 166 | + expect="""mm(): expected the first input tensor f32[2,2,2] to be a matrix (i.e. a 2D tensor).""" |
| 167 | + ) |
| 168 | + |
| 169 | + def test_mm_raises_error_on_incompatible_shapes(self): |
| 170 | + device = torch_xla.device() |
| 171 | + a = torch.rand(2, 5, device=device) |
| 172 | + b = torch.rand(8, 2, device=device) |
| 173 | + |
| 174 | + def test(): |
| 175 | + torch.mm(a, b) |
| 176 | + |
| 177 | + self.assertExpectedRaisesInline( |
| 178 | + exc_type=RuntimeError, |
| 179 | + callable=test, |
| 180 | + expect="""mm(): cannot matrix-multiply tensors f32[2,5] and f32[8,2]. Expected the size of dimension 1 of the first input tensor (5) to be equal the size of dimension 0 of the second input tensor (8).""" |
| 181 | + ) |
| 182 | + |
| 183 | + def test_clamp_raises_error_on_no_min_and_max(self): |
| 184 | + device = torch_xla.device() |
| 185 | + a = torch.rand(2, 5, device=device) |
| 186 | + |
| 187 | + def test(): |
| 188 | + return torch.ops.aten.clamp.default(a) |
| 189 | + |
| 190 | + self.assertExpectedRaisesInline( |
| 191 | + exc_type=RuntimeError, |
| 192 | + callable=test, |
| 193 | + expect="""mm(): cannot matrix-multiply tensors f32[2,5] and f32[8,2]. Expected the size of dimension 1 of the first input tensor (5) to be equal the size of dimension 0 of the second input tensor (8).""" |
| 194 | + ) |
0 commit comments