-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathfuse_parallel_branches_test.py
82 lines (63 loc) · 2.55 KB
/
fuse_parallel_branches_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# isort:skip_file
# pyre-ignore-all-errors
from __future__ import absolute_import, division, print_function, unicode_literals
import unittest
import torch
import torch_glow
from glow.glow.torch_glow.tests.tests import utils
class TestFuseParallelBranches(utils.TorchGlowTestCase):
def test_fuse_parallel_branches_with_fusible_root(self):
r"""Test GlowFuser fusing parallel branches with a common fusible root
a = add(x, y)
/ \
b1 = add(a, x) b2 = add(a, y)
\ /
res = TupleConstruct(b1, b2)
This should be fused as
glow::FusionGroup_0
|
TupleConstruct
"""
def test_fuser(x, y):
a = x + y
branch1 = a + x
branch2 = a + y
res = (branch1, branch2)
return res
inputs = (torch.randn(2, 4), torch.randn(2, 4))
traced = torch.jit.trace(test_fuser, inputs)
torch_glow.glowCustomFuseDebug_(traced.graph)
count = 0
for node in traced.graph.nodes():
if node.kind() == "glow::FusionGroup":
count += 1
assert count == 1, f"Expect 1 glow::FusionGroup, found {count}."
# TODO: support fusing parallel branches without a common fusible root correctly
@unittest.skip("Not supported yet")
def test_fuse_parallel_branches_without_fusible_root(self):
r"""Test GlowFuser fusing parallel branches without a common fusible root
x = add(x, x) y = add(y, y)
| |
b1 = add(x, x) b2 = add(y, y)
\ /
res = TupleConstruct(b1, b2)
This should be fused as
glow::FusionGroup_0
|
TupleConstruct
"""
def test_fuser(x, y):
x = x + x
y = y + y
branch1 = x + x
branch2 = y + y
res = (branch1, branch2)
return res
inputs = (torch.randn(2, 4), torch.randn(2, 4))
traced = torch.jit.trace(test_fuser, inputs)
torch_glow.glowCustomFuseDebug_(traced.graph)
count = 0
for node in traced.graph.nodes():
if node.kind() == "glow::FusionGroup":
count += 1
assert count == 1, f"Expect 1 glow::FusionGroup, found {count}."