test_control_flow.py 1.87 KB
Newer Older
1
2
3
import torch
import torch.nn as nn
from torch.fx import GraphModule
4

5
from colossalai.fx import ColoTracer as Tracer
6
from colossalai.testing import clear_cache_before_run
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


class ControlFlowModel(nn.Module):

    def __init__(self):
        super().__init__()
        self.linear1 = nn.Linear(10, 10)
        self.linear2 = nn.Linear(10, 10)

    def forward(self, x, y):
        x1 = self.linear1(x)
        y1 = self.linear2(y)

        if x1.dim() == 2:
            return x1 + y1
        else:
            return x1 - y1


26
@clear_cache_before_run()
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def test_control_flow():
    model = ControlFlowModel()
    tracer = Tracer()
    graph_branch_true = tracer.trace(model,
                                     meta_args={
                                         'x': torch.rand(4, 10, device='meta'),
                                         'y': torch.rand(4, 10, device='meta')
                                     })
    graph_branch_false = tracer.trace(model,
                                      meta_args={
                                          'x': torch.rand(10, device='meta'),
                                          'y': torch.rand(4, 10, device='meta')
                                      })

    gm_branch_true = GraphModule(model, graph_branch_true, model.__class__.__name__)
    gm_branch_false = GraphModule(model, graph_branch_false, model.__class__.__name__)
    gm_branch_true.recompile()
    gm_branch_false.recompile()

    # test the true branch
    x = torch.rand(4, 10)
    y = torch.rand(4, 10)
    assert torch.all(model(x, y) == gm_branch_true(x, y))
    assert torch.all(gm_branch_false(x, y) != gm_branch_true(x, y))

    # test the true branch
    x = torch.rand(10)
    y = torch.rand(4, 10)
    assert torch.all(model(x, y) == gm_branch_false(x, y))
    assert torch.all(gm_branch_false(x, y) != gm_branch_true(x, y))


if __name__ == '__main__':
    test_control_flow()