Unverified Commit 17b06e2c authored by Miguel Almeida's avatar Miguel Almeida Committed by GitHub
Browse files

Fix Swinv2ForImageClassification NaN output (#29981)

To address the issue of NaN logit outputs for certain combinations
of the `image_size`, `patch_size` and `depths` configuration
parameters, an assertion was made to ensure that the resulting
`window_size` field in the model's Self Attention class is greater
than 1, preventing divisions by zero in the normalization of
`relative_coords_table`.

Fix: #28675
parent 81642d2b
...@@ -298,7 +298,7 @@ class Swin2SRSelfAttention(nn.Module): ...@@ -298,7 +298,7 @@ class Swin2SRSelfAttention(nn.Module):
if pretrained_window_size[0] > 0: if pretrained_window_size[0] > 0:
relative_coords_table[:, :, :, 0] /= pretrained_window_size[0] - 1 relative_coords_table[:, :, :, 0] /= pretrained_window_size[0] - 1
relative_coords_table[:, :, :, 1] /= pretrained_window_size[1] - 1 relative_coords_table[:, :, :, 1] /= pretrained_window_size[1] - 1
else: elif window_size > 1:
relative_coords_table[:, :, :, 0] /= self.window_size[0] - 1 relative_coords_table[:, :, :, 0] /= self.window_size[0] - 1
relative_coords_table[:, :, :, 1] /= self.window_size[1] - 1 relative_coords_table[:, :, :, 1] /= self.window_size[1] - 1
relative_coords_table *= 8 # normalize to -8, 8 relative_coords_table *= 8 # normalize to -8, 8
......
...@@ -454,7 +454,7 @@ class Swinv2SelfAttention(nn.Module): ...@@ -454,7 +454,7 @@ class Swinv2SelfAttention(nn.Module):
if pretrained_window_size[0] > 0: if pretrained_window_size[0] > 0:
relative_coords_table[:, :, :, 0] /= pretrained_window_size[0] - 1 relative_coords_table[:, :, :, 0] /= pretrained_window_size[0] - 1
relative_coords_table[:, :, :, 1] /= pretrained_window_size[1] - 1 relative_coords_table[:, :, :, 1] /= pretrained_window_size[1] - 1
else: elif window_size > 1:
relative_coords_table[:, :, :, 0] /= self.window_size[0] - 1 relative_coords_table[:, :, :, 0] /= self.window_size[0] - 1
relative_coords_table[:, :, :, 1] /= self.window_size[1] - 1 relative_coords_table[:, :, :, 1] /= self.window_size[1] - 1
relative_coords_table *= 8 # normalize to -8, 8 relative_coords_table *= 8 # normalize to -8, 8
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment