|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +from torch.testing._internal.common_utils import run_tests |
| 4 | +from torch_tensorrt.fx.tools.common_fx2trt import DispatchTestCase, InputTensorSpec |
| 5 | + |
| 6 | + |
| 7 | +class TestMeanDimConverter(DispatchTestCase): |
| 8 | + def test_mean_dim_keepdims(self): |
| 9 | + class TestModule(nn.Module): |
| 10 | + def forward(self, x): |
| 11 | + return torch.mean(x, dim=[0, 1], keepdim=True) |
| 12 | + |
| 13 | + inputs = [torch.randn(1, 10)] |
| 14 | + self.run_test(TestModule(), inputs, expected_ops={torch.ops.aten.mean.dim}) |
| 15 | + |
| 16 | + def test_mean_dim_keepdims_with_dynamic_shape(self): |
| 17 | + class TestModule(nn.Module): |
| 18 | + def forward(self, x): |
| 19 | + return torch.mean(x, dim=[0, 1, 2], keepdim=True) |
| 20 | + |
| 21 | + input_specs = [ |
| 22 | + InputTensorSpec( |
| 23 | + shape=(-1, -1, -1), |
| 24 | + dtype=torch.float32, |
| 25 | + shape_ranges=[((1, 1, 1), (1, 2, 3), (3, 3, 3))], |
| 26 | + ), |
| 27 | + ] |
| 28 | + self.run_test_with_dynamic_shape( |
| 29 | + TestModule(), input_specs, expected_ops={torch.ops.aten.mean.dim} |
| 30 | + ) |
| 31 | + |
| 32 | + def test_mean_dim_keepdims_false(self): |
| 33 | + class TestModule(nn.Module): |
| 34 | + def forward(self, x): |
| 35 | + return torch.mean(x, dim=0, keepdim=False) |
| 36 | + |
| 37 | + inputs = [torch.randn(3, 5, 7)] |
| 38 | + self.run_test(TestModule(), inputs, expected_ops={torch.ops.aten.mean.dim}) |
| 39 | + |
| 40 | + def test_mean_dim_keepdims_false_with_dynamic_shape(self): |
| 41 | + class TestModule(nn.Module): |
| 42 | + def forward(self, x): |
| 43 | + return torch.mean(x, dim=-1, keepdim=False) |
| 44 | + |
| 45 | + input_specs = [ |
| 46 | + InputTensorSpec( |
| 47 | + shape=(-1, -1, -1), |
| 48 | + dtype=torch.float32, |
| 49 | + shape_ranges=[((1, 1, 1), (1, 2, 3), (3, 3, 3))], |
| 50 | + ), |
| 51 | + ] |
| 52 | + self.run_test_with_dynamic_shape( |
| 53 | + TestModule(), input_specs, expected_ops={torch.ops.aten.mean.dim} |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class TestMeanConverter(DispatchTestCase): |
| 58 | + def test_mean(self): |
| 59 | + class TestModule(nn.Module): |
| 60 | + def forward(self, x): |
| 61 | + return torch.mean(x) |
| 62 | + |
| 63 | + inputs = [torch.randn(3, 8, 5, 7, 1)] |
| 64 | + self.run_test(TestModule(), inputs, expected_ops={torch.ops.aten.mean.default}) |
| 65 | + |
| 66 | + def test_mean_with_dynamic_shape(self): |
| 67 | + class TestModule(nn.Module): |
| 68 | + def forward(self, x): |
| 69 | + return torch.mean(x) |
| 70 | + |
| 71 | + input_specs = [ |
| 72 | + InputTensorSpec( |
| 73 | + shape=(-1, -1, -1), |
| 74 | + dtype=torch.float32, |
| 75 | + shape_ranges=[((1, 1, 1), (1, 5, 8), (3, 10, 10))], |
| 76 | + ), |
| 77 | + ] |
| 78 | + self.run_test_with_dynamic_shape( |
| 79 | + TestModule(), input_specs, expected_ops={torch.ops.aten.mean.default} |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + run_tests() |
0 commit comments