|
| 1 | +from copy import deepcopy |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +import torch |
| 6 | +import torch.nn.functional as F |
| 7 | +import torch_tensorrt |
| 8 | +from parameterized import parameterized |
| 9 | +from torch.testing._internal.common_utils import TestCase, run_tests |
| 10 | +from torch_tensorrt.dynamo import partitioning |
| 11 | + |
| 12 | +from ..testing_utilities import lower_graph_testing |
| 13 | + |
| 14 | +# Note: the following tests were a part of test_global_partitioning.py and were flaky when |
| 15 | +# we ran all the tests. So, the following test cases were separated out in this test_flaky_global_partitioning.py |
| 16 | +# The partitioned graphs were different when you ran the graph as a part of test_global_partitioning.py vs when you |
| 17 | +# run these tests independently. pytest by default doesn't use parallel execution, so we are not sure why this behavior occurs |
| 18 | +# currently. When you run these tests independently, the partitioned graph is structurally correct and is similar to fast partitioning. |
| 19 | + |
| 20 | + |
| 21 | +class TestGlobalPartitioning(TestCase): |
| 22 | + def test_partition_partially_supported_multi_op(self): |
| 23 | + class PartiallySupportedMultiOp(torch.nn.Module): |
| 24 | + def __init__(self, *args, **kwargs) -> None: |
| 25 | + super().__init__(*args, **kwargs) |
| 26 | + |
| 27 | + def forward(self, x, y): |
| 28 | + sum_1 = torch.ops.aten.add.Tensor(x, y) |
| 29 | + sum_2 = torch.ops.aten.add.Tensor(x, sum_1) |
| 30 | + sum_ = np.sum(sum_1) + np.sum(sum_2) |
| 31 | + relu_ = torch.ops.aten.relu.default(sum_) |
| 32 | + pow_ = torch.ops.aten.pow.Tensor_Scalar(relu_, 2) |
| 33 | + return pow_ |
| 34 | + |
| 35 | + fx_graph = torch.fx.symbolic_trace(PartiallySupportedMultiOp()) |
| 36 | + partitioned_graph, _ = partitioning.global_partition( |
| 37 | + deepcopy(fx_graph), min_block_size=2 |
| 38 | + ) |
| 39 | + # breakpoint() |
| 40 | + self.assertEqual( |
| 41 | + len(list(partitioned_graph.named_children())), |
| 42 | + 2, |
| 43 | + "Unsupported operators interleave supported ones, expected 2 segments", |
| 44 | + ) |
| 45 | + |
| 46 | + def test_partition_partially_supported_with_torch_executed_ops(self): |
| 47 | + class PartiallySupportedMultiOp(torch.nn.Module): |
| 48 | + def __init__(self, *args, **kwargs) -> None: |
| 49 | + super().__init__(*args, **kwargs) |
| 50 | + |
| 51 | + def forward(self, x, y): |
| 52 | + sum_1 = torch.ops.aten.add.Tensor(x, y) |
| 53 | + sum_2 = torch.ops.aten.add.Tensor(x, sum_1) |
| 54 | + sum_ = torch.ops.aten.add.Tensor(sum_1, sum_2) |
| 55 | + relu_ = torch.ops.aten.relu.default(sum_) |
| 56 | + pow_ = torch.ops.aten.pow.Tensor_Scalar(relu_, 2) |
| 57 | + return pow_ |
| 58 | + |
| 59 | + unexpected_ops = {torch.ops.aten.add.Tensor} |
| 60 | + |
| 61 | + inputs = [ |
| 62 | + torch.randint( |
| 63 | + 1, |
| 64 | + 10, |
| 65 | + (5,), |
| 66 | + ), |
| 67 | + torch.randint( |
| 68 | + 1, |
| 69 | + 10, |
| 70 | + (5,), |
| 71 | + ), |
| 72 | + ] |
| 73 | + |
| 74 | + fx_graph = torch.fx.symbolic_trace(PartiallySupportedMultiOp()) |
| 75 | + ( |
| 76 | + unexpected_ops_seen, |
| 77 | + _, |
| 78 | + partitioned_graphs, |
| 79 | + ) = lower_graph_testing( |
| 80 | + fx_graph, |
| 81 | + inputs, |
| 82 | + unexpected_ops=unexpected_ops, |
| 83 | + min_block_size=2, |
| 84 | + torch_executed_ops={"torch.ops.aten.add.Tensor"}, |
| 85 | + testing_partitioning=True, |
| 86 | + use_fast_partitioner=False, |
| 87 | + ) |
| 88 | + |
| 89 | + self.assertEqual( |
| 90 | + len(unexpected_ops_seen), |
| 91 | + 0, |
| 92 | + f"The following unexpected ops were encountered: {unexpected_ops_seen}", |
| 93 | + ) |
| 94 | + |
| 95 | + self.assertEqual( |
| 96 | + len(partitioned_graphs), |
| 97 | + 1, |
| 98 | + "Without control flow breaks, there should only be a single graph", |
| 99 | + ) |
| 100 | + self.assertEqual( |
| 101 | + len(list(partitioned_graphs[0].named_children())), |
| 102 | + 1, |
| 103 | + "Certain operators are set to run in Torch, expected 1 segment", |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + run_tests() |
0 commit comments