|
| 1 | +import torch |
| 2 | +from parameterized import param, parameterized |
| 3 | +from torch.testing._internal.common_utils import run_tests |
| 4 | + |
| 5 | +from .harness import DispatchTestCase |
| 6 | + |
| 7 | + |
| 8 | +class TestIndexPutConverter(DispatchTestCase): |
| 9 | + @parameterized.expand( |
| 10 | + [ |
| 11 | + param( |
| 12 | + test_name="1d_indices_single", |
| 13 | + source_tensor=torch.zeros([5], dtype=torch.int32), |
| 14 | + indices_tensor=(torch.tensor([0], dtype=torch.int32),), |
| 15 | + value_tensor=torch.tensor([1], dtype=torch.int32), |
| 16 | + ), |
| 17 | + param( |
| 18 | + test_name="1d_indices_multiple", |
| 19 | + source_tensor=torch.zeros([5], dtype=torch.int32), |
| 20 | + indices_tensor=(torch.tensor([0, 3], dtype=torch.int32),), |
| 21 | + value_tensor=torch.tensor([1, 3], dtype=torch.int32), |
| 22 | + ), |
| 23 | + param( |
| 24 | + test_name="2d_indices_single", |
| 25 | + source_tensor=torch.zeros([5, 5], dtype=torch.int32), |
| 26 | + indices_tensor=( |
| 27 | + torch.tensor([2], dtype=torch.int32), |
| 28 | + torch.tensor([0], dtype=torch.int32), |
| 29 | + ), |
| 30 | + value_tensor=torch.tensor([3], dtype=torch.int32), |
| 31 | + ), |
| 32 | + param( |
| 33 | + test_name="2d_indices_multiple", |
| 34 | + source_tensor=torch.zeros([5, 5], dtype=torch.int32), |
| 35 | + indices_tensor=( |
| 36 | + torch.tensor([0, 2, 2], dtype=torch.int32), |
| 37 | + torch.tensor([2, 0, 2], dtype=torch.int32), |
| 38 | + ), |
| 39 | + value_tensor=torch.tensor([1, 3, 4], dtype=torch.int32), |
| 40 | + ), |
| 41 | + param( |
| 42 | + test_name="3d_indices_single", |
| 43 | + source_tensor=torch.zeros([3, 3, 3], dtype=torch.int32), |
| 44 | + indices_tensor=( |
| 45 | + torch.tensor([1], dtype=torch.int32), |
| 46 | + torch.tensor([2], dtype=torch.int32), |
| 47 | + torch.tensor([2], dtype=torch.int32), |
| 48 | + ), |
| 49 | + value_tensor=torch.tensor([7], dtype=torch.int32), |
| 50 | + ), |
| 51 | + param( |
| 52 | + test_name="3d_indices_multiple", |
| 53 | + source_tensor=torch.zeros([3, 3, 3], dtype=torch.int32), |
| 54 | + indices_tensor=( |
| 55 | + torch.tensor([0, 1, 1], dtype=torch.int32), |
| 56 | + torch.tensor([1, 2, 1], dtype=torch.int32), |
| 57 | + torch.tensor([2, 0, 2], dtype=torch.int32), |
| 58 | + ), |
| 59 | + value_tensor=torch.tensor([5, 7, 2], dtype=torch.int32), |
| 60 | + ), |
| 61 | + param( |
| 62 | + test_name="4d_indices_single", |
| 63 | + source_tensor=torch.zeros([2, 2, 2, 2], dtype=torch.int32), |
| 64 | + indices_tensor=( |
| 65 | + torch.tensor([1], dtype=torch.int32), |
| 66 | + torch.tensor([1], dtype=torch.int32), |
| 67 | + torch.tensor([0], dtype=torch.int32), |
| 68 | + torch.tensor([1], dtype=torch.int32), |
| 69 | + ), |
| 70 | + value_tensor=torch.tensor([5], dtype=torch.int32), |
| 71 | + ), |
| 72 | + param( |
| 73 | + test_name="4d_indices_multiple", |
| 74 | + source_tensor=torch.zeros([2, 2, 2, 2], dtype=torch.int32), |
| 75 | + indices_tensor=( |
| 76 | + torch.tensor([0, 1], dtype=torch.int32), |
| 77 | + torch.tensor([1, 1], dtype=torch.int32), |
| 78 | + torch.tensor([1, 0], dtype=torch.int32), |
| 79 | + torch.tensor([1, 0], dtype=torch.int32), |
| 80 | + ), |
| 81 | + value_tensor=torch.tensor([5, 7], dtype=torch.int32), |
| 82 | + ), |
| 83 | + param( |
| 84 | + test_name="negative_indices", |
| 85 | + source_tensor=torch.zeros([5, 5], dtype=torch.int32), |
| 86 | + indices_tensor=( |
| 87 | + torch.tensor([-1, -2], dtype=torch.int32), |
| 88 | + torch.tensor([2, 0], dtype=torch.int32), |
| 89 | + ), |
| 90 | + value_tensor=torch.tensor([1, 3], dtype=torch.int32), |
| 91 | + ), |
| 92 | + param( |
| 93 | + test_name="mixed_indices", |
| 94 | + source_tensor=torch.zeros([4, 4], dtype=torch.int32), |
| 95 | + indices_tensor=( |
| 96 | + torch.tensor([0, 1, -1, -2], dtype=torch.int32), |
| 97 | + torch.tensor([0, -1, 2, 1], dtype=torch.int32), |
| 98 | + ), |
| 99 | + value_tensor=torch.tensor([2, 4, 6, 8], dtype=torch.int32), |
| 100 | + ), |
| 101 | + param( |
| 102 | + test_name="1d_indices_float", |
| 103 | + source_tensor=torch.zeros([5], dtype=torch.float32), |
| 104 | + indices_tensor=(torch.tensor([0, 3], dtype=torch.int32),), |
| 105 | + value_tensor=torch.tensor([1.5, 3.5], dtype=torch.float32), |
| 106 | + ), |
| 107 | + param( |
| 108 | + test_name="2d_indices_float", |
| 109 | + source_tensor=torch.zeros([5, 5], dtype=torch.float32), |
| 110 | + indices_tensor=( |
| 111 | + torch.tensor([0, 2], dtype=torch.int32), |
| 112 | + torch.tensor([2, 0], dtype=torch.int32), |
| 113 | + ), |
| 114 | + value_tensor=torch.tensor([1.5, 3.5], dtype=torch.float32), |
| 115 | + ), |
| 116 | + param( |
| 117 | + test_name="3d_indices_float", |
| 118 | + source_tensor=torch.zeros([3, 3, 3], dtype=torch.float32), |
| 119 | + indices_tensor=( |
| 120 | + torch.tensor([0, 1], dtype=torch.int32), |
| 121 | + torch.tensor([1, 2], dtype=torch.int32), |
| 122 | + torch.tensor([2, 0], dtype=torch.int32), |
| 123 | + ), |
| 124 | + value_tensor=torch.tensor([5.5, 7.5], dtype=torch.float32), |
| 125 | + ), |
| 126 | + param( |
| 127 | + test_name="4d_indices_float", |
| 128 | + source_tensor=torch.zeros([2, 2, 2, 2], dtype=torch.float32), |
| 129 | + indices_tensor=( |
| 130 | + torch.tensor([0, 1], dtype=torch.int32), |
| 131 | + torch.tensor([1, 0], dtype=torch.int32), |
| 132 | + torch.tensor([0, 1], dtype=torch.int32), |
| 133 | + torch.tensor([1, 0], dtype=torch.int32), |
| 134 | + ), |
| 135 | + value_tensor=torch.tensor([5.5, 7.5], dtype=torch.float32), |
| 136 | + ), |
| 137 | + # param( |
| 138 | + # test_name="3d_indices_float_broadcase_index", |
| 139 | + # source_tensor=torch.zeros([3, 3, 3], dtype = torch.int32), |
| 140 | + # indices_tensor=( |
| 141 | + # torch.tensor([0,1], dtype=torch.int32), |
| 142 | + # torch.tensor([0,1], dtype=torch.int32), |
| 143 | + # ), |
| 144 | + # value_tensor=torch.tensor([10], dtype = torch.int32), |
| 145 | + # ), |
| 146 | + # param( |
| 147 | + # test_name="2d_indices_accumulate_True", |
| 148 | + # source_tensor=torch.zeros([5, 5], dtype=torch.int32), |
| 149 | + # indices_tensor=(torch.tensor([0, 0], dtype=torch.int32), torch.tensor([1, 1], dtype=torch.int32)), |
| 150 | + # value_tensor=torch.tensor([1, 2], dtype=torch.int32), |
| 151 | + # accumulate=True, |
| 152 | + # ), |
| 153 | + # param( |
| 154 | + # test_name="3d_indices_accumulate_True", |
| 155 | + # source_tensor=torch.zeros([3, 3, 3], dtype=torch.int32), |
| 156 | + # indices_tensor=(torch.tensor([0, 0], dtype=torch.int32), torch.tensor([1, 1], dtype=torch.int32), torch.tensor([2, 2], dtype=torch.int32)), |
| 157 | + # value_tensor=torch.tensor([1, 2], dtype=torch.int32), |
| 158 | + # accumulate=True, |
| 159 | + # ), |
| 160 | + # param( |
| 161 | + # test_name="4d_indices_accumulate_True", |
| 162 | + # source_tensor=torch.zeros([2, 2, 2, 2], dtype=torch.int32), |
| 163 | + # indices_tensor=(torch.tensor([0, 0], dtype=torch.int32), torch.tensor([1, 1], dtype=torch.int32), torch.tensor([0, 0], dtype=torch.int32), torch.tensor([1, 1], dtype=torch.int32)), |
| 164 | + # value_tensor=torch.tensor([1, 2], dtype=torch.int32), |
| 165 | + # accumulate=True, |
| 166 | + # ), |
| 167 | + ] |
| 168 | + ) |
| 169 | + def test_index_put( |
| 170 | + self, test_name, source_tensor, indices_tensor, value_tensor, accumulate=False |
| 171 | + ): |
| 172 | + @torch._dynamo.assume_constant_result |
| 173 | + def get_indices_tensor(): |
| 174 | + return indices_tensor |
| 175 | + |
| 176 | + class TestIndexPut(torch.nn.Module): |
| 177 | + def forward(self, source_tensor, value_tensor): |
| 178 | + indices_tensor_const = get_indices_tensor() |
| 179 | + return torch.ops.aten.index_put.default( |
| 180 | + source_tensor, indices_tensor_const, value_tensor, accumulate |
| 181 | + ) |
| 182 | + |
| 183 | + self.run_test( |
| 184 | + TestIndexPut(), |
| 185 | + inputs=[source_tensor, value_tensor], |
| 186 | + enable_passes=True, |
| 187 | + use_dynamo_tracer=True, |
| 188 | + ) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + run_tests() |
0 commit comments