-
Notifications
You must be signed in to change notification settings - Fork 395
/
batch_norm.py
229 lines (182 loc) · 8.45 KB
/
batch_norm.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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import pytorchvideo.layers.distributed as du
import torch
import torch.distributed as dist
from fvcore.nn.distributed import differentiable_all_reduce
from torch import nn
class NaiveSyncBatchNorm1d(nn.BatchNorm1d):
"""
An implementation of 1D naive sync batch normalization. See details in
NaiveSyncBatchNorm2d below.
Args:
num_sync_devices (int): number of (local) devices to sync.
global_sync (bool): sync across all devices (on all machines).
args (list): other arguments.
"""
def __init__(self, num_sync_devices=None, global_sync=True, **args):
self.global_sync = global_sync
if self.global_sync and num_sync_devices is not None:
raise ValueError(
f"Cannot set num_sync_devices separately when global_sync = {self.global_sync}"
)
if not self.global_sync and num_sync_devices is None:
raise ValueError(
f"num_sync_devices cannot be None when global_sync = {self.global_sync}"
)
if not self.global_sync:
self.num_sync_devices = num_sync_devices
if self.num_sync_devices > 0:
assert du.get_local_size() % self.num_sync_devices == 0, (
du.get_local_size(),
self.num_sync_devices,
)
self.num_groups = du.get_local_size() // self.num_sync_devices
else:
self.num_sync_devices = du.get_local_size()
self.num_groups = 1
super(NaiveSyncBatchNorm1d, self).__init__(**args)
def forward(self, input):
if du.get_world_size() == 1 or not self.training:
return super().forward(input)
B, C = input.shape[0], input.shape[1]
assert B > 0, "SyncBatchNorm does not support zero batch size."
mean = torch.mean(input, dim=[0])
meansqr = torch.mean(input * input, dim=[0])
vec = torch.cat([mean, meansqr], dim=0)
# sync stats globally or locally
if self.global_sync:
vec = differentiable_all_reduce(vec) * (1.0 / dist.get_world_size())
else:
vec = du.GroupGather.apply(vec, self.num_sync_devices, self.num_groups) * (
1.0 / self.num_sync_devices
)
mean, meansqr = torch.split(vec, C)
var = meansqr - mean * mean
invstd = torch.rsqrt(var + self.eps)
scale = self.weight * invstd
bias = self.bias - mean * scale
scale = scale.reshape(1, -1)
bias = bias.reshape(1, -1)
self.running_mean += self.momentum * (mean.detach() - self.running_mean)
self.running_var += self.momentum * (var.detach() - self.running_var)
return input * scale + bias
class NaiveSyncBatchNorm2d(nn.BatchNorm2d):
"""
An implementation of 2D naive sync batch normalization.
In PyTorch<=1.5, ``nn.SyncBatchNorm`` has incorrect gradient
when the batch size on each worker is different.
(e.g., when scale augmentation is used, or when it is applied to mask head).
This is a slower but correct alternative to `nn.SyncBatchNorm`.
Args:
num_sync_devices (int): number of (local) devices to sync.
global_sync (bool): sync across all devices (on all machines).
args (list): other arguments.
Note:
This module computes overall statistics by using
statistics of each worker with equal weight. The result is true statistics
of all samples (as if they are all on one worker) only when all workers
have the same (N, H, W). This mode does not support inputs with zero batch size.
"""
def __init__(self, num_sync_devices=None, global_sync=True, **args):
self.global_sync = global_sync
if self.global_sync and num_sync_devices is not None:
raise ValueError(
f"Cannot set num_sync_devices separately when global_sync = {self.global_sync}"
)
if not self.global_sync and num_sync_devices is None:
raise ValueError(
f"num_sync_devices cannot be None when global_sync = {self.global_sync}"
)
if not self.global_sync:
self.num_sync_devices = num_sync_devices
if self.num_sync_devices > 0:
assert du.get_local_size() % self.num_sync_devices == 0, (
du.get_local_size(),
self.num_sync_devices,
)
self.num_groups = du.get_local_size() // self.num_sync_devices
else:
self.num_sync_devices = du.get_local_size()
self.num_groups = 1
super(NaiveSyncBatchNorm2d, self).__init__(**args)
def forward(self, input):
if du.get_world_size() == 1 or not self.training:
return super().forward(input)
B, C = input.shape[0], input.shape[1]
assert B > 0, "SyncBatchNorm does not support zero batch size."
mean = torch.mean(input, dim=[0, 2, 3])
meansqr = torch.mean(input * input, dim=[0, 2, 3])
vec = torch.cat([mean, meansqr], dim=0)
# sync stats globally or locally
if self.global_sync:
vec = differentiable_all_reduce(vec) * (1.0 / dist.get_world_size())
else:
vec = du.GroupGather.apply(vec, self.num_sync_devices, self.num_groups) * (
1.0 / self.num_sync_devices
)
mean, meansqr = torch.split(vec, C)
var = meansqr - mean * mean
invstd = torch.rsqrt(var + self.eps)
scale = self.weight * invstd
bias = self.bias - mean * scale
scale = scale.reshape(1, -1, 1, 1)
bias = bias.reshape(1, -1, 1, 1)
self.running_mean += self.momentum * (mean.detach() - self.running_mean)
self.running_var += self.momentum * (var.detach() - self.running_var)
return input * scale + bias
class NaiveSyncBatchNorm3d(nn.BatchNorm3d):
"""
Naive version of Synchronized 3D BatchNorm. See details in
NaiveSyncBatchNorm2d above.
Args:
num_sync_devices (int): number of (local) devices to sync.
global_sync (bool): sync across all devices (on all machines).
args (list): other arguments.
"""
def __init__(self, num_sync_devices=None, global_sync=True, **args):
self.global_sync = global_sync
if self.global_sync and num_sync_devices is not None:
raise ValueError(
f"Cannot set num_sync_devices separately when global_sync = {self.global_sync}"
)
if not self.global_sync and num_sync_devices is None:
raise ValueError(
f"num_sync_devices cannot be None when global_sync = {self.global_sync}"
)
if not self.global_sync:
self.num_sync_devices = num_sync_devices
if self.num_sync_devices > 0:
assert du.get_local_size() % self.num_sync_devices == 0, (
du.get_local_size(),
self.num_sync_devices,
)
self.num_groups = du.get_local_size() // self.num_sync_devices
else:
self.num_sync_devices = du.get_local_size()
self.num_groups = 1
super(NaiveSyncBatchNorm3d, self).__init__(**args)
def forward(self, input):
if du.get_world_size() == 1 or not self.training:
return super().forward(input)
B, C = input.shape[0], input.shape[1]
assert B > 0, "SyncBatchNorm does not support zero batch size."
mean = torch.mean(input, dim=[0, 2, 3, 4])
meansqr = torch.mean(input * input, dim=[0, 2, 3, 4])
vec = torch.cat([mean, meansqr], dim=0)
# sync stats globally or locally
if self.global_sync:
vec = differentiable_all_reduce(vec) * (1.0 / dist.get_world_size())
else:
vec = du.GroupGather.apply(vec, self.num_sync_devices, self.num_groups) * (
1.0 / self.num_sync_devices
)
mean, meansqr = torch.split(vec, C)
var = meansqr - mean * mean
invstd = torch.rsqrt(var + self.eps)
scale = self.weight * invstd
bias = self.bias - mean * scale
scale = scale.reshape(1, -1, 1, 1, 1)
bias = bias.reshape(1, -1, 1, 1, 1)
self.running_mean += self.momentum * (mean.detach() - self.running_mean)
self.running_var += self.momentum * (var.detach() - self.running_var)
return input * scale + bias