|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +from parameterized import parameterized |
| 4 | +from torch.testing._internal.common_utils import run_tests |
| 5 | + |
| 6 | +from .harness import DispatchTestCase |
| 7 | + |
| 8 | + |
| 9 | +class TestSumConverter(DispatchTestCase): |
| 10 | + @parameterized.expand( |
| 11 | + [ |
| 12 | + ((3, 2, 4),), |
| 13 | + ((2, 3, 4, 5),), |
| 14 | + ((2, 3, 4, 5),), |
| 15 | + ((6, 7, 5, 4, 5),), |
| 16 | + ] |
| 17 | + ) |
| 18 | + def test_sum_dim_int_default(self, input_shape): |
| 19 | + class Sum(nn.Module): |
| 20 | + def forward(self, x): |
| 21 | + return torch.sum(x) |
| 22 | + |
| 23 | + inputs = [torch.randn(*input_shape)] |
| 24 | + self.run_test( |
| 25 | + Sum(), |
| 26 | + inputs, |
| 27 | + expected_ops={torch.ops.aten.sum.default}, |
| 28 | + ) |
| 29 | + |
| 30 | + @parameterized.expand( |
| 31 | + [ |
| 32 | + ((3, 2, 4), 1, True), |
| 33 | + ((2, 3, 4, 5), 3, True), |
| 34 | + ((2, 3, 4, 5), None, False), |
| 35 | + ((6, 7, 5, 4, 5), 4, False), |
| 36 | + ] |
| 37 | + ) |
| 38 | + def test_sum_dim_int(self, input_shape, dim, keep_dims): |
| 39 | + class Sum(nn.Module): |
| 40 | + def forward(self, x): |
| 41 | + return torch.sum(x, dim=dim, keepdim=keep_dims) |
| 42 | + |
| 43 | + inputs = [torch.randn(*input_shape)] |
| 44 | + self.run_test( |
| 45 | + Sum(), |
| 46 | + inputs, |
| 47 | + expected_ops={torch.ops.aten.sum.dim_IntList}, |
| 48 | + ) |
| 49 | + |
| 50 | + @parameterized.expand( |
| 51 | + [ |
| 52 | + ((3, 2, 4), [1], True), |
| 53 | + ((2, 1, 4, 5), None, True), |
| 54 | + ((2, 3, 4, 5), [0, 1, 2, 3], False), |
| 55 | + ((6, 7, 5, 4, 5), [1, 3, 4], False), |
| 56 | + ] |
| 57 | + ) |
| 58 | + def test_sum_dim_tuple(self, input_shape, dim, keep_dims): |
| 59 | + class Sum(nn.Module): |
| 60 | + def forward(self, x): |
| 61 | + return torch.sum(x, dim=dim, keepdim=keep_dims) |
| 62 | + |
| 63 | + inputs = [torch.randn(*input_shape)] |
| 64 | + self.run_test( |
| 65 | + Sum(), |
| 66 | + inputs, |
| 67 | + expected_ops={torch.ops.aten.sum.dim_IntList}, |
| 68 | + ) |
| 69 | + |
| 70 | + @parameterized.expand( |
| 71 | + [ |
| 72 | + ((3, 2, 4), 1, True, torch.int, 0, 5), |
| 73 | + ((2, 3, 4, 5), None, True, torch.int, -10, 10), |
| 74 | + ((2, 3, 4, 5), 2, False, torch.int32, -5, 0), |
| 75 | + ((6, 7, 5, 4, 5), 4, False, torch.int32, -5, 5), |
| 76 | + ] |
| 77 | + ) |
| 78 | + def test_sum_dim_int_int(self, input_shape, dim, keep_dims, dtype, low, high): |
| 79 | + class Sum(nn.Module): |
| 80 | + def forward(self, x): |
| 81 | + return torch.sum(x, dim=dim, keepdim=keep_dims) |
| 82 | + |
| 83 | + inputs = [torch.randint(low, high, input_shape, dtype=dtype)] |
| 84 | + self.run_test( |
| 85 | + Sum(), |
| 86 | + inputs, |
| 87 | + expected_ops={torch.ops.aten.sum.dim_IntList}, |
| 88 | + check_dtype=False, |
| 89 | + ) |
| 90 | + |
| 91 | + @parameterized.expand( |
| 92 | + [ |
| 93 | + ((3, 2, 4), [1], True, torch.int, 0, 5), |
| 94 | + ((2, 1, 4, 5), [0, 3], True, torch.int, -10, 10), |
| 95 | + ((2, 3, 4, 5), None, False, torch.int32, -5, 0), |
| 96 | + ((6, 7, 5, 4, 5), [1, 3, 4], False, torch.int32, -5, 5), |
| 97 | + ] |
| 98 | + ) |
| 99 | + def test_sum_dim_tuple_int(self, input_shape, dim, keep_dims, dtype, low, high): |
| 100 | + class Sum(nn.Module): |
| 101 | + def forward(self, x): |
| 102 | + return torch.sum(x, dim=dim, keepdim=keep_dims) |
| 103 | + |
| 104 | + inputs = [torch.randint(low, high, input_shape, dtype=dtype)] |
| 105 | + self.run_test( |
| 106 | + Sum(), |
| 107 | + inputs, |
| 108 | + expected_ops={torch.ops.aten.sum.dim_IntList}, |
| 109 | + check_dtype=False, |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + run_tests() |
0 commit comments