Hi all, I get NaNs and Infs after the first convolution of my unet for a diffusion model. All parameters are randomly initialized at first, and the checkpoint I use to generate images have been trained for 30k iterations. Below is part of my unet code:
While initializing my unet:
self.init_conv = nn.Conv2d(channel, init_channel, 3, padding = 1)
Define the forward pass:
def forward(self, x, t, c):
if torch.sum(torch.isnan(x))>0: #Check if input x has NaNs
print(f'Input x has NaNs at t: {t}')
raise Exception()
if torch.sum(torch.isinf(x))>0: #Check if input x has Infs
print(f'Input x has Infs at t: {t}')
raise Exception()
h = self.init_conv(x)
for params in self.init_conv.parameters(): #Check the weights of the convolution
if (torch.sum(torch.isnan(params.data))+torch.sum(torch.isinf(params.data))) > 0:
print(f"params have nan or inf")
raise Exception()
if torch.sum(torch.isnan(h))>0: #Check if output h has NaNs
print(f'After init_conv h has NaNs at t: {t}')
raise Exception()
if torch.sum(torch.isinf(h))>0: #Check if output h has Infs
print(f'After init_conv h has Infs at t: {t}')
raise Exception()
...
I add two if blocks to check if input x has NaNs or Infs and another two if blocks to do the same thing for convolution output h. A for loop is added to check if the weights of the convolution have NaNs and Infs. Below is the result.
shows that input x is okay, so are the weights of convolution layers. But after convolution, the output has Infs. Is it possible for getting Infs even the input and weights have no NaN or Inf?