Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix drop path being ignored in DINOv2 #29147

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/transformers/models/dinov2/modeling_dinov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def __init__(self, config: Dinov2Config) -> None:
self.norm1 = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
self.attention = Dinov2Attention(config)
self.layer_scale1 = Dinov2LayerScale(config)
self.drop_path1 = Dinov2DropPath(config.drop_path_rate) if config.drop_path_rate > 0.0 else nn.Identity()
self.drop_path = Dinov2DropPath(config.drop_path_rate) if config.drop_path_rate > 0.0 else nn.Identity()

self.norm2 = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)

Expand All @@ -389,7 +389,6 @@ def __init__(self, config: Dinov2Config) -> None:
else:
self.mlp = Dinov2MLP(config)
self.layer_scale2 = Dinov2LayerScale(config)
self.drop_path2 = Dinov2DropPath(config.drop_path_rate) if config.drop_path_rate > 0.0 else nn.Identity()

def forward(
self,
Expand All @@ -408,15 +407,15 @@ def forward(
outputs = self_attention_outputs[1:] # add self attentions if we output attention weights

# first residual connection
hidden_states = attention_output + hidden_states
hidden_states = self.drop_path(attention_output) + hidden_states

# in Dinov2, layernorm is also applied after self-attention
layer_output = self.norm2(hidden_states)
layer_output = self.mlp(layer_output)
layer_output = self.layer_scale2(layer_output)

# second residual connection
layer_output = layer_output + hidden_states
layer_output = self.drop_path(layer_output) + hidden_states

outputs = (layer_output,) + outputs

Expand Down