Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def get_image_features(

return vision_outputs

@can_return_tuple
@auto_docstring
def forward(
self,
Expand All @@ -177,11 +178,8 @@ def forward(
position_ids: torch.LongTensor | None = None,
return_loss: bool | None = None,
token_type_ids: torch.LongTensor | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
**kwargs,
) -> tuple[torch.Tensor] | CLIPOutput:
**kwargs: Unpack[TransformersKwargs],
) -> CLIPOutput:
r"""
return_loss (`bool`, *optional*):
Whether or not to return the contrastive loss.
Expand Down Expand Up @@ -239,29 +237,23 @@ def forward(
>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score
>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
```"""
return_dict = return_dict if return_dict is not None else self.config.return_dict

vision_outputs = self.vision_model(
pixel_values=pixel_values,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
**kwargs,
)

text_outputs = self.text_model(
input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
**kwargs,
)

image_embeds = vision_outputs[1] # pooler_output
image_embeds = vision_outputs.pooler_output
image_embeds = self.visual_projection(image_embeds)

text_embeds = text_outputs[1] # pooler_output
text_embeds = text_outputs.pooler_output
text_embeds = self.text_projection(text_embeds)

# normalized features
Expand All @@ -277,10 +269,6 @@ def forward(
if return_loss:
loss = clip_loss(logits_per_text)

if not return_dict:
output = (logits_per_image, logits_per_text, text_embeds, image_embeds, text_outputs, vision_outputs)
return ((loss,) + output) if loss is not None else output

return CLIPOutput(
loss=loss,
logits_per_image=logits_per_image,
Expand Down Expand Up @@ -323,8 +311,7 @@ def from_vision_text_pretrained(
All remaining positional arguments will be passed to the underlying model's `__init__` method.

kwargs (remaining dictionary of keyword arguments, *optional*):
Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
`output_attentions=True`).
Can be used to update the configuration object (after it being loaded) and initiate the model.

- To update the text configuration, use the prefix *text_* for each configuration parameter.
- To update the vision configuration, use the prefix *vision_* for each configuration parameter.
Expand Down