forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_decompose_mem_bound_mm.py
260 lines (206 loc) · 7.92 KB
/
test_decompose_mem_bound_mm.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Owner(s): ["module: inductor"]
import logging
import unittest
import torch
import torch._inductor
from torch._dynamo.utils import counters
from torch._inductor.test_case import run_tests, TestCase
from torch._inductor.utils import run_and_get_code
from torch.testing import FileCheck
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
)
from torch.testing._internal.inductor_utils import HAS_CUDA
requires_cuda = unittest.skipUnless(HAS_CUDA, "requires cuda")
class MyModule(torch.nn.Module):
def __init__(
self, n_input: int, n_output: int, has_bias: bool, device="cuda"
) -> None:
super().__init__()
self.linear = torch.nn.Linear(n_input, n_output, bias=has_bias)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.linear(x)
class MyModule2(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, input1, input2):
output = torch.bmm(input1, input2)
return output
class MyModule3(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, input1, input2):
output = torch.mm(input1, input2)
return output
@requires_cuda
@torch._inductor.config.patch(
post_grad_fusion_options={
"decompose_mm_pass": {},
}
)
@instantiate_parametrized_tests
class TestDecomposeMemMM(TestCase):
def compare_dict_tensors(self, ref_dict, res_dict, rtol=1e-3, atol=1e-3):
if len(set(ref_dict.keys())) != len(set(res_dict.keys())):
return False
for key1 in ref_dict.keys():
key2 = "_orig_mod." + key1
assert key2 in res_dict, f"{key1} does not exist in traced module"
if not torch.allclose(ref_dict[key1], res_dict[key2], rtol=rtol, atol=atol):
return False
return True
def compare_pred(self, module, traced, input, rtol=1e-3, atol=1e-3):
ref = module(*input)
res = traced(*input)
self.assertEqual(ref, res, rtol=rtol, atol=atol)
def compare_parameters(self, module, traced, rtol=1e-3, atol=1e-3):
ref_params = dict(module.named_parameters())
res_params = dict(traced.named_parameters())
self.assertTrue(self.compare_dict_tensors(ref_params, res_params, rtol, atol))
def compare_gradients(self, module, traced, rtol=1e-3, atol=1e-3):
ref_grad = {key: param.grad for key, param in module.named_parameters()}
res_grad = {key: param.grad for key, param in traced.named_parameters()}
self.assertTrue(
self.compare_dict_tensors(ref_grad, res_grad, rtol=rtol, atol=atol)
)
@parametrize(
"b,m,k,n,should_decompose",
[(10240, 2, 2, 2, True), (10240, 2, 32, 32, False), (2000, 2, 2, 2, False)],
)
def test_decompose_bmm(self, b, m, n, k, should_decompose):
torch._logging.set_logs(inductor=logging.DEBUG)
mat1 = torch.randn(b, m, k, device="cuda").requires_grad_(True)
mat2 = torch.randn(b, k, n, device="cuda").requires_grad_(True)
counters.clear()
module = MyModule2().to("cuda")
traced = torch.compile(module)
input = [mat1, mat2]
ref = module(*input)
res = traced(*input)
self.compare_pred(module, traced, input)
expected_val = 1 if should_decompose else 0
self.assertEqual(
counters["inductor"]["decompose_bmm"],
expected_val,
)
ref.sum().backward()
res.sum().backward()
self.compare_parameters(module, traced)
self.compare_gradients(module, traced)
expected_val = 3 if should_decompose else 0
self.assertEqual(
counters["inductor"]["decompose_bmm"],
expected_val,
)
counters.clear()
@parametrize(
"m,k,n, should_decompose",
[(20480, 5, 2, True), (20480, 32, 2, False), (2048, 2, 2, False)],
)
@parametrize("has_bias", [True, False])
def test_decompose_linear(self, m, n, k, has_bias, should_decompose):
torch._logging.set_logs(inductor=logging.DEBUG)
input = torch.randn(m, k, device="cuda").requires_grad_(True)
counters.clear()
module = MyModule(k, n, has_bias).to("cuda")
traced = torch.compile(module)
input = [input]
ref = module(*input)
res = traced(*input)
self.compare_pred(module, traced, input)
expected_val = 1 if should_decompose else 0
if has_bias:
self.assertEqual(
counters["inductor"]["decompose_addmm"],
expected_val,
)
else:
self.assertEqual(
counters["inductor"]["decompose_mm"],
expected_val,
)
decompose_mm_fwd = counters["inductor"]["decompose_mm"]
ref.sum().backward()
res.sum().backward()
self.compare_parameters(module, traced)
self.compare_gradients(module, traced)
self.assertEqual(
counters["inductor"]["decompose_mm"] - decompose_mm_fwd,
expected_val,
)
counters.clear()
@parametrize(
"m,k,n, should_decompose",
[(20480, 5, 2, True), (20480, 32, 2, False), (2048, 2, 2, False)],
)
@parametrize("has_bias", [True, False])
def test_decompose_mm(self, m, n, k, has_bias, should_decompose):
torch._logging.set_logs(inductor=logging.DEBUG)
mat1 = torch.randn(m, k, device="cuda").requires_grad_(True)
mat2 = torch.randn(k, n, device="cuda").requires_grad_(True)
counters.clear()
module = MyModule3().to("cuda")
traced = torch.compile(module)
input = [mat1, mat2]
ref = module(*input)
res = traced(*input)
self.compare_pred(module, traced, input)
expected_val = 1 if should_decompose else 0
self.assertEqual(
counters["inductor"]["decompose_mm"],
expected_val,
)
decompose_mm_fwd = counters["inductor"]["decompose_mm"]
ref.sum().backward()
res.sum().backward()
self.compare_parameters(module, traced)
self.compare_gradients(module, traced)
expected_val = 1 if should_decompose else 0
self.assertEqual(
counters["inductor"]["decompose_mm"] - decompose_mm_fwd,
expected_val,
)
counters.clear()
@parametrize("m,k,n, should_decompose", [(20480, 5, 2, True)])
@parametrize("has_bias", [True, False])
def test_dynamic_shape(self, m, n, k, has_bias, should_decompose):
torch._logging.set_logs(inductor=logging.DEBUG)
input = torch.randn(m, k, device="cuda").requires_grad_(True)
counters.clear()
module = MyModule(k, n, has_bias).to("cuda")
traced = torch.compile(module, dynamic=True)
input = [input]
ref = module(*input)
res = traced(*input)
self.compare_pred(module, traced, input)
expected_val = 1 if should_decompose else 0
if has_bias:
self.assertEqual(
counters["inductor"]["decompose_addmm"],
expected_val,
)
ref.sum().backward()
res.sum().backward()
self.compare_parameters(module, traced)
self.compare_gradients(module, traced)
self.assertEqual(
counters["inductor"]["decompose_mm"],
1 if has_bias else 2,
)
counters.clear()
def test_realize_input(self):
m = 20480
k = 5
n = 2
torch._logging.set_logs(inductor=logging.DEBUG)
input1 = torch.randn(m, k, device="cuda").T.contiguous()
input2 = torch.randn(k, n, device="cuda")
@torch.compile()
def foo(x, y):
return x.T.contiguous() @ y
out, code = run_and_get_code(foo, input1, input2)
# two kernels generated
FileCheck().check_count(".run(", 2, exactly=True).run(code[0])
if __name__ == "__main__":
run_tests()