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

[DINOv2] Update pooler output #25392

Merged
merged 1 commit into from
Aug 10, 2023
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
27 changes: 5 additions & 22 deletions src/transformers/models/dinov2/modeling_dinov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,14 @@ def _set_gradient_checkpointing(self, module: Dinov2Encoder, value: bool = False
DINOV2_START_DOCSTRING,
)
class Dinov2Model(Dinov2PreTrainedModel):
def __init__(self, config: Dinov2Config, add_pooling_layer: bool = True):
def __init__(self, config: Dinov2Config):
super().__init__(config)
self.config = config

self.embeddings = Dinov2Embeddings(config)
self.encoder = Dinov2Encoder(config)

self.layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
self.pooler = Dinov2Pooler(config) if add_pooling_layer else None

# Initialize weights and apply final processing
self.post_init()
Expand Down Expand Up @@ -651,10 +650,10 @@ def forward(
)
sequence_output = encoder_outputs[0]
sequence_output = self.layernorm(sequence_output)
pooled_output = self.pooler(sequence_output) if self.pooler is not None else None
pooled_output = sequence_output[:, 0, :]

if not return_dict:
head_outputs = (sequence_output, pooled_output) if pooled_output is not None else (sequence_output,)
head_outputs = (sequence_output, pooled_output)
return head_outputs + encoder_outputs[1:]

return BaseModelOutputWithPooling(
Expand All @@ -665,22 +664,6 @@ def forward(
)


# Copied from transformers.models.vit.modeling_vit.ViTPooler with ViT->Dinov2
class Dinov2Pooler(nn.Module):
def __init__(self, config: Dinov2Config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = nn.Tanh()

def forward(self, hidden_states):
# We "pool" the model by simply taking the hidden state corresponding
# to the first token.
first_token_tensor = hidden_states[:, 0]
pooled_output = self.dense(first_token_tensor)
pooled_output = self.activation(pooled_output)
return pooled_output


@add_start_docstrings(
"""
Dinov2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state
Expand All @@ -693,7 +676,7 @@ def __init__(self, config: Dinov2Config) -> None:
super().__init__(config)

self.num_labels = config.num_labels
self.dinov2 = Dinov2Model(config, add_pooling_layer=False)
self.dinov2 = Dinov2Model(config)

# Classifier head
self.classifier = (
Expand Down Expand Up @@ -770,7 +753,7 @@ def forward(
loss = loss_fct(logits, labels)

if not return_dict:
output = (logits,) + outputs[1:]
output = (logits,) + outputs[2:]
return ((loss,) + output) if loss is not None else output

return ImageClassifierOutput(
Expand Down
Loading