Gradient overflows when self-attention module added
When I add self-attention module to the network, gradient overflows and loss becomes Nan. (opt_level=O2)
Following is code for the self-attention module (got from link) I used,
class CGDBlock3D(nn.Module):
def __init__(self, in_channels):
super(CGDBlock3D, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.max_pool = nn.AdaptiveMaxPool3d(1)
self.softmax = nn.Softmax(dim=1)
self.w0 = nn.Parameter(torch.ones(in_channels, 1))
self.w1 = nn.Parameter(torch.ones(in_channels, 1))
self.w2 = nn.Parameter(torch.ones(in_channels, 1))
self.bias0 = nn.Parameter(torch.zeros(1, in_channels, 1, 1, 1))
self.bias1 = nn.Parameter(torch.zeros(1, in_channels, 1, 1, 1))
self.bias2 = nn.Parameter(torch.zeros(1, in_channels, 1, 1, 1))
nn.init.xavier_uniform_(self.w0)
nn.init.xavier_uniform_(self.w1)
nn.init.xavier_uniform_(self.w2)
def cgd(self, x, N, C):
g = self.avg_pool(x).view(N, C, 1, 1, 1)
f = self.max_pool(x).view(N, C, 1, 1, 1)
g_s = self.softmax(g) # b ,c ,1 ,1, 1
psi = torch.matmul(g.view(N, C), self.w0).view(N, 1, 1, 1, 1)
phi = torch.matmul(f.view(N, C), self.w1).view(N, 1, 1, 1, 1)
psi_s = torch.tanh(psi * g_s + self.bias0) # b ,c ,1 ,1, 1
phi_s = torch.tanh(phi * g_s + self.bias1) # b ,c ,1 ,1, 1
gf = torch.matmul(phi_s.view(N, C), self.w2).view(N, 1, 1, 1, 1)
gf_tanh = torch.tanh(gf * psi_s + self.bias2).view(N, C, 1, 1, 1)
z = x * (1 + gf_tanh)
return z
def forward(self, x):
N, C, T, _, _ = x.size()
out = self.cgd(x, N, C)
return out
I'm not sure which part of the module makes the problem. I assumed softmax but it happened even I remove the softmax from the code.
Hi @kkjh0723,
thanks for the code snippet. Could you post some dummy inputs so that we can reproduce this behavior?
The shapes and value ranges for all inputs to the forward method should be sufficient.
Also, how did you define in_channels?
@ptrblck thanks for the reply. The module is used in a kind of 3d ResNet, so the dummy input might be,
img = Variable(torch.randn(1, 256, 8, 14, 14)) #batch, C, T, H, W
in_channels is a number of input channels, so 256 in case of the above dummy input.
Hi, the same problem also happens to me. Although I am not using a self-attention module, I have some convolution operations sharing the same weight. To implement this, I define the weight using nn.Parameter and load it to the operation, just like the self-attention module doing here. In my experiment, I used opt_level='O1' and I could observe a lot of "Gradient overflows. Skipping step, loss scaler 0 reducing loss scale to M" warnings, where M started from 32768.0 to 0.0 and the whole program stopped because of ZeroDivisionError. This warning didn't take place when training with full precision (in ordinary pytorch training configuration). I am wondering if @kkjh0723 or @ptrblck has solved the problem or not. It has been two months since this issue was opened.
Found similar problems some days before. I believe you should replace element-wise multiplication (psi * g_s) with torch.mul. This might be related to the fp16 computation when you use 'O1'.