forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_te_fuser_pass.cpp
165 lines (140 loc) · 4.82 KB
/
test_te_fuser_pass.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <test/cpp/tensorexpr/test_base.h>
#include <torch/csrc/jit/codegen/fuser/interface.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/irparser.h>
#include <torch/csrc/jit/passes/tensorexpr_fuser.h>
#include <torch/csrc/jit/tensorexpr/mem_arena.h>
#include <torch/csrc/jit/testing/file_check.h>
#include <sstream>
namespace torch {
namespace jit {
using namespace torch::jit::tensorexpr;
struct WithCPUFuser {
WithCPUFuser(bool val = true) : cpuFuserEnabled(canFuseOnCPU()) {
overrideCanFuseOnCPU(val);
}
~WithCPUFuser() {
overrideCanFuseOnCPU(cpuFuserEnabled);
}
bool cpuFuserEnabled;
};
void testFuserPass_1() {
WithCPUFuser cf;
KernelScope kernel_scope;
const auto graph_string = R"IR(
graph(%0 : Float(128:1, device=cpu),
%1 : Float(128:1, device=cpu)):
%12 : int = prim::Constant[value=1]()
%2.1 : Float(128:1, device=cpu) = aten::mul(%0, %1)
%2 : Float(128:1, device=cpu) = aten::mul(%2.1, %1)
%3 : Float(128:1, device=cpu) = aten::add_(%2, %1, %12)
%4 : Float(128:1, device=cpu) = aten::mul(%2, %1)
%5 : Float(128:1, device=cpu) = aten::add(%2, %4, %12)
return (%5))IR";
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g);
// We should not be able to fuse across the in-place operation here.
testing::FileCheck()
.check("prim::TensorExprGroup_")
->check("aten::add_")
->check("prim::TensorExprGroup_")
->run(*g);
}
void testFuserPass_2() {
WithCPUFuser cf;
KernelScope kernel_scope;
const auto graph_string = R"IR(
graph(%0 : Float(128:1, device=cpu),
%1 : Float(128:1, device=cpu)):
%12 : int = prim::Constant[value=1]()
%a : Float(128:1, device=cpu) = aten::mul(%0, %1)
%b : Float(128:1, device=cpu) = aten::add(%0, %1, %12)
%c : Float(128:1, device=cpu) = aten::add_(%b, %1, %12)
%d : Float(128:1, device=cpu) = aten::mul(%c, %a)
return (%d))IR";
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g);
// We should not be able to fuse across the in-place operation here.
testing::FileCheck()
.check("aten::add_")
->check("prim::TensorExprGroup_0")
->run(*g);
}
void testFuserPass_3() {
WithCPUFuser cf;
KernelScope kernel_scope;
const auto graph_string = R"IR(
graph(%x : Float(128:1, device=cpu),
%y : Float(128:1, device=cpu)):
%r : Float(128:1, device=cpu) = aten::mul(%x, %y)
return (%r))IR";
{
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g, /* min_group_size= */ 2);
// We should not create a fusion group since its size would be too small
testing::FileCheck().check_not("prim::TensorExprGroup")->run(*g);
}
{
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g, /* min_group_size= */ 1);
// We should create a fusion group since its size is above the threshold
testing::FileCheck().check("prim::TensorExprGroup")->run(*g);
}
}
void testFuserPass_0DimInput() {
KernelScope kernel_scope;
const auto graph_string = R"IR(
graph(%x : Float(device=cuda),
%y : Float(device=cuda)):
%one : int = prim::Constant[value=1]()
%a : Float(device=cuda) = aten::mul(%x, %y)
%b : Float(device=cuda) = aten::add(%x, %a, %one)
return (%b))IR";
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g);
// We should not fuse 0-dim tensors
testing::FileCheck().check_not("prim::TensorExprGroup")->run(*g);
}
void testFuserPass_UnfusibleDevice() {
WithCPUFuser cf(false);
KernelScope kernel_scope;
const auto graph_string = R"IR(
graph(%x : Float(10:1, device=cpu),
%y : Float(10:1, device=cpu)):
%a : Float(10:1, device=cpu) = aten::mul(%x, %y)
return (%a))IR";
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g, /* min_group_size= */ 1);
// Test that we're not starting fusion groups from nodes with unfusible device
testing::FileCheck().check_not("prim::TensorExprGroup")->run(*g);
}
void testFuserPass_UnknownShapes() {
WithCPUFuser cf;
KernelScope kernel_scope;
const auto graph_string = R"IR(
graph(%x : Tensor,
%y : Tensor):
%a : Tensor = aten::mul(%x, %y)
%b : Tensor = aten::mul(%x, %a)
return (%a))IR";
auto g = std::make_shared<Graph>();
torch::jit::parseIR(graph_string, g.get());
g->lint();
FuseTensorExprs(g);
// Test that we're not generating fusion groups when shapes are not known
testing::FileCheck().check_not("prim::TensorExprGroup")->run(*g);
}
} // namespace jit
} // namespace torch