-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Expand file tree
/
Copy pathefficient_conv_bn_eval.py
More file actions
445 lines (388 loc) · 15.5 KB
/
Copy pathefficient_conv_bn_eval.py
File metadata and controls
445 lines (388 loc) · 15.5 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# mypy: allow-untyped-defs
import inspect
import torch
import torch.nn as nn
from torch._dynamo.utils import counters
from torch._inductor import config as inductor_config
from torch.func import functional_call
from ..pattern_matcher import (
CallFunctionVarArgs,
CallModuleVarArgs,
Match,
register_graph_pattern,
)
from .pre_grad import efficient_conv_bn_eval_pass
# Cache the signature of F.batch_norm at module load time to avoid repeated
# introspection during graph transformation (fixes performance regression).
_BATCH_NORM_SIGNATURE = inspect.signature(torch.nn.functional.batch_norm)
def efficient_conv_bn_eval(
bn: nn.modules.batchnorm._BatchNorm, conv: nn.modules.conv._ConvNd, x: torch.Tensor
):
"""
Implementation based on https://arxiv.org/abs/2305.11624
"Efficient ConvBN Blocks for Transfer Learning and Beyond"
It leverages the associative law between convolution and affine transform,
i.e., normalize (weight conv feature) = (normalize weight) conv feature.
It works for Eval mode of ConvBN blocks during validation, and can be used
for **training** as well, but only if one sets `bn.training=False`. It
reduces memory footprint and computation cost, at the cost of slightly
reduced numerical stability.
Args:
bn (nn.modules.batchnorm._BatchNorm): a BatchNorm module.
conv (nn.modules.conv._ConvNd): a conv module
x (torch.Tensor): Input feature map.
"""
if bn.running_var is None:
raise AssertionError("expected bn.running_var to not be None")
if bn.running_mean is None:
raise AssertionError("expected bn.running_mean to not be None")
# These lines of code are designed to deal with various cases
# like bn without affine transform, and conv without bias
weight_on_the_fly = conv.weight
if conv.bias is not None:
bias_on_the_fly = conv.bias
else:
bias_on_the_fly = torch.zeros_like(bn.running_var)
if bn.weight is not None:
bn_weight = bn.weight
else:
bn_weight = torch.ones_like(bn.running_var)
if bn.bias is not None:
bn_bias = bn.bias
else:
bn_bias = torch.zeros_like(bn.running_var)
# shape of [C_out, 1, 1, 1] in Conv2d
target_shape = [-1] + [1] * (conv.weight.ndim - 1)
if isinstance(conv, nn.modules.conv._ConvTransposeNd):
# for transposed conv, the C_out dimension should be at index 1.
target_shape[:2] = [target_shape[1], target_shape[0]]
weight_coeff = torch.rsqrt(bn.running_var + bn.eps).reshape(target_shape)
# shape of [C_out, 1, 1, 1] in Conv2d
coefff_on_the_fly = bn_weight.view_as(weight_coeff) * weight_coeff
# shape of [C_out, C_in, k, k] in Conv2d
weight_on_the_fly = weight_on_the_fly * coefff_on_the_fly
# shape of [C_out] in Conv2d
bias_on_the_fly = bn_bias + coefff_on_the_fly.flatten() * (
bias_on_the_fly - bn.running_mean
)
input = x
params = {"weight": weight_on_the_fly, "bias": bias_on_the_fly}
output = functional_call(conv, params, input)
return output
def efficient_conv_bn_eval_decomposed(
bn_weight,
bn_bias,
bn_running_mean,
bn_running_var,
bn_eps,
conv: torch._ops.OpOverload,
conv_weight,
conv_bias,
x,
conv_remaining_args,
):
"""
Implementation based on https://arxiv.org/abs/2305.11624
"Efficient ConvBN Blocks for Transfer Learning and Beyond"
It leverages the associative law between convolution and affine transform,
i.e., normalize (weight conv feature) = (normalize weight) conv feature.
It works for Eval mode of ConvBN blocks during validation, and can be used
for **training** as well, but only if one sets `bn.training=False`. It
reduces memory footprint and computation cost, at the cost of slightly
reduced numerical stability.
Args:
"""
if bn_running_var is None:
raise AssertionError("expected bn_running_var to not be None")
# These lines of code are designed to deal with various cases
# like bn without affine transform, and conv without bias
weight_on_the_fly = conv_weight
if conv_bias is not None:
bias_on_the_fly = conv_bias
else:
bias_on_the_fly = torch.zeros_like(bn_running_var)
if bn_weight is None:
bn_weight = torch.ones_like(bn_running_var)
if bn_bias is None:
bn_bias = torch.zeros_like(bn_running_var)
# shape of [C_out, 1, 1, 1] in Conv2d
target_shape = [-1] + [1] * (conv_weight.ndim - 1)
if "conv_transpose" in conv.__str__():
# for transposed conv, the C_out dimension should be at index 1.
target_shape[:2] = [target_shape[1], target_shape[0]]
weight_coeff = torch.rsqrt(bn_running_var + bn_eps).reshape(target_shape)
# shape of [C_out, 1, 1, 1] in Conv2d
coefff_on_the_fly = bn_weight.view_as(weight_coeff) * weight_coeff
# shape of [C_out, C_in, k, k] in Conv2d
weight_on_the_fly = weight_on_the_fly * coefff_on_the_fly
# shape of [C_out] in Conv2d
bias_on_the_fly = bn_bias + coefff_on_the_fly.flatten() * (
bias_on_the_fly - bn_running_mean
)
input = x
return conv(*((input, weight_on_the_fly, bias_on_the_fly) + conv_remaining_args))
@register_graph_pattern(
CallFunctionVarArgs(
[
torch.nn.functional.batch_norm,
]
),
# pyrefly: ignore [bad-argument-type]
pass_dict=efficient_conv_bn_eval_pass,
extra_check=lambda match: not inductor_config.freezing
and inductor_config.efficient_conv_bn_eval_fx_passes,
)
def efficient_conv_bn_eval_graph_transform_inlined(match: Match, *args, **kwargs):
"""
Graph transformation pass for fusing F.batch_norm with preceding conv operations.
This pass handles F.batch_norm calls with default arguments by normalizing
the args tuple using inspect.signature. It fuses batch normalization with
the preceding convolution for more efficient evaluation.
"""
bn_node = match.nodes[0]
graph = match.graph
# Normalize arguments by binding to cached signature and applying defaults.
# This handles cases where F.batch_norm is called with fewer than 8 args.
bound_args = _BATCH_NORM_SIGNATURE.bind(*bn_node.args, **bn_node.kwargs)
bound_args.apply_defaults()
# Use bound_args.args instead of mutating bn_node.args
normalized_args = bound_args.args
# We can only use efficient conv-bn for eval mode with track_running_stats
# normalized_args[5] is the "training" argument
training_arg = normalized_args[5]
# Safety check: if 'training' is a symbolic Node (from tracing/export),
# we cannot optimize since we don't know the value at compile time.
if isinstance(training_arg, torch.fx.Node):
return
if training_arg:
return
# Check if the input is Conv
input_node = normalized_args[0]
if input_node.op != "call_function": # type: ignore[union-attr]
return
input_fn = input_node.target # type: ignore[arg-type, union-attr]
supported_convs = [
torch._C._nn.linear,
torch.conv1d,
torch.conv2d,
torch.conv3d,
torch.conv_transpose1d,
torch.conv_transpose2d,
torch.conv_transpose3d,
]
if not any(input_fn is cls for cls in supported_convs):
return
conv_node = input_node
# Output of conv is used by other nodes, cannot optimize
if len(conv_node.users) > 1: # type: ignore[union-attr]
return
counters["inductor"]["efficient_conv_bn_eval"] += 1
with graph.inserting_before(bn_node):
# prepare args for the fused function
bn_running_mean = normalized_args[1]
bn_running_var = normalized_args[2]
bn_weight = normalized_args[3]
bn_bias = normalized_args[4]
bn_eps = normalized_args[7]
if len(conv_node.args) < 2: # type: ignore[union-attr]
raise AssertionError(
f"expected at least 2 conv_node args, got {len(conv_node.args)}" # type: ignore[union-attr]
)
conv_input = conv_node.args[0] # type: ignore[union-attr]
conv_weight = conv_node.args[1] # type: ignore[union-attr]
conv_bias = conv_node.args[2] if len(conv_node.args) >= 3 else None # type: ignore[union-attr]
conv_remaining_args = conv_node.args[3:] # type: ignore[union-attr]
args = (
bn_weight,
bn_bias,
bn_running_mean,
bn_running_var,
bn_eps,
conv_node.target, # type: ignore[union-attr]
conv_weight,
conv_bias,
conv_input,
conv_remaining_args,
)
# create a new node
new_node = graph.create_node(
op="call_function",
target=efficient_conv_bn_eval_decomposed,
args=args, # type: ignore[arg-type]
name="efficient_conv_bn_eval",
)
# this node replaces the original conv + bn, and therefore
# should replace the uses of bn_node
bn_node.replace_all_uses_with(new_node)
# take care of the deletion order:
# delete bn_node first, and then conv_node
graph.erase_node(bn_node)
graph.erase_node(conv_node) # type: ignore[arg-type]
return
@register_graph_pattern(
CallFunctionVarArgs(
[
torch.ops.aten.batch_norm.default,
]
),
# pyrefly: ignore [bad-argument-type]
pass_dict=efficient_conv_bn_eval_pass,
extra_check=lambda match: not inductor_config.freezing
and inductor_config.efficient_conv_bn_eval_fx_passes,
)
def efficient_conv_bn_eval_graph_transform_decomposed(match: Match, *args, **kwargs):
bn_node = match.nodes[0]
graph = match.graph
if len(bn_node.args) != 9:
raise AssertionError(f"expected 9 bn_node args, got {len(bn_node.args)}")
# We can only use efficient conv-bn for eval mode with track_running_stats
# bn_node.args is `training`
if bn_node.args[-4]:
return
# Check if the input is Conv
input_node = bn_node.args[0]
if input_node.op != "call_function": # type: ignore[union-attr]
return
input_fn = input_node.target # type: ignore[arg-type, union-attr]
supported_convs = [
torch.ops.aten.linear.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv_transpose1d.default,
torch.ops.aten.conv_transpose2d.input,
torch.ops.aten.conv_transpose3d.input,
]
if not any(input_fn is cls for cls in supported_convs):
return
conv_node = input_node
# Output of conv is used by other nodes, cannot optimize
if len(conv_node.users) > 1: # type: ignore[union-attr]
return
counters["inductor"]["efficient_conv_bn_eval"] += 1
with graph.inserting_before(bn_node):
# prepare args for the fused function
bn_weight = bn_node.args[1]
bn_bias = bn_node.args[2]
bn_running_mean = bn_node.args[3]
bn_running_var = bn_node.args[4]
bn_eps = bn_node.args[7]
if len(conv_node.args) < 2: # type: ignore[union-attr]
raise AssertionError(
f"expected at least 2 conv_node args, got {len(conv_node.args)}" # type: ignore[union-attr]
)
conv_input = conv_node.args[0] # type: ignore[union-attr]
conv_weight = conv_node.args[1] # type: ignore[union-attr]
conv_bias = conv_node.args[2] if len(conv_node.args) >= 3 else None # type: ignore[union-attr]
conv_remaining_args = conv_node.args[3:] # type: ignore[union-attr]
args = (
bn_weight,
bn_bias,
bn_running_mean,
bn_running_var,
bn_eps,
conv_node.target, # type: ignore[union-attr]
conv_weight,
conv_bias,
conv_input,
conv_remaining_args,
)
# create a new node
new_node = graph.create_node(
op="call_function",
target=efficient_conv_bn_eval_decomposed,
args=args, # type: ignore[arg-type]
name="efficient_conv_bn_eval",
)
# this node replaces the original conv + bn, and therefore
# should replace the uses of bn_node
bn_node.replace_all_uses_with(new_node)
# take care of the deletion order:
# delete bn_node first, and then conv_node
graph.erase_node(bn_node)
graph.erase_node(conv_node) # type: ignore[arg-type]
return
@register_graph_pattern(
CallModuleVarArgs(
[
nn.modules.batchnorm._BatchNorm,
nn.BatchNorm1d,
nn.BatchNorm2d,
nn.BatchNorm3d,
nn.SyncBatchNorm,
],
),
# pyrefly: ignore [bad-argument-type]
pass_dict=efficient_conv_bn_eval_pass,
extra_check=lambda match: not inductor_config.freezing
and inductor_config.efficient_conv_bn_eval_fx_passes,
)
def efficient_conv_bn_eval_graph_transform(match: Match, *args, **kwargs):
# We matched a BN node
bn_node = match.nodes[0]
graph = match.graph
gm = graph.owning_module
bn_mod = getattr(gm, bn_node.target) # type: ignore[arg-type]
# We can only use efficient conv-bn for eval mode with track_running_stats
if not bn_mod.track_running_stats or bn_mod.training:
return
# Check if the input is Conv
if bn_node.args:
input_node = bn_node.args[0]
else:
input_node = bn_node.kwargs["input"]
if input_node.op != "call_module": # type: ignore[union-attr]
return
if not hasattr(gm, input_node.target): # type: ignore[arg-type, union-attr]
return
input_mod = getattr(gm, input_node.target) # type: ignore[arg-type, union-attr]
supported_convs = [
nn.Linear,
nn.Conv1d,
nn.Conv2d,
nn.Conv3d,
nn.ConvTranspose1d,
nn.ConvTranspose2d,
nn.ConvTranspose3d,
]
if not any(isinstance(input_mod, cls) for cls in supported_convs):
return
conv_node = input_node
# Output of conv is used by other nodes, cannot optimize
if len(conv_node.users) > 1: # type: ignore[union-attr]
return
# Find a pair of conv and bn computation nodes to optimize.
counters["inductor"]["efficient_conv_bn_eval"] += 1
with graph.inserting_before(conv_node): # type: ignore[arg-type]
# create `get_attr` node to access modules
# note that we directly call `create_node` to fill the `name`
# argument. `graph.get_attr` and
# `graph.call_function` does not allow the `name` argument.
conv_get_node = graph.create_node(
op="get_attr",
target=conv_node.target, # type: ignore[union-attr]
name="get_conv",
)
bn_get_node = graph.create_node(
op="get_attr", target=bn_node.target, name="get_bn"
)
if conv_node.args: # type: ignore[union-attr]
conv_input = conv_node.args[0] # type: ignore[union-attr]
else:
conv_input = conv_node.kwargs["input"] # type: ignore[union-attr]
# prepare args for the fused function
args = (bn_get_node, conv_get_node, conv_input)
# create a new node
new_node = graph.create_node(
op="call_function",
target=efficient_conv_bn_eval,
args=args,
name="efficient_conv_bn_eval",
)
# this node replaces the original conv + bn, and therefore
# should replace the uses of bn_node
bn_node.replace_all_uses_with(new_node)
# take care of the deletion order:
# delete bn_node first, and then conv_node
graph.erase_node(bn_node)
graph.erase_node(conv_node) # type: ignore[arg-type]