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

[CLIP] allow loading projection layer in vision and text model #18962

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

patil-suraj
Copy link
Member

@patil-suraj patil-suraj commented Sep 9, 2022

This PR allows optionally loading projection layers in CLIPVisionModel and CLIPTextModel.
There are some tasks in diffusers that rely either on text or image projections but not both. This will allow using only the related modality model instead of loading the full model or having to write wrappers.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@patil-suraj patil-suraj changed the title [wip][CLIP] allow loading projection layer in vision and text model [CLIP] allow loading projection layer in vision and text model Sep 9, 2022
@dataclass
class CLIPVisionModelOutput(ModelOutput):
"""
Base class for model's outputs that also contains a pooling of the last hidden states.
Copy link
Member

@patrickvonplaten patrickvonplaten Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Base class for model's outputs that also contains a pooling of the last hidden states.
Base class for vision model's outputs that also contains a pooling of the last hidden states.

Copy link
Member

@patrickvonplaten patrickvonplaten Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also didn't we create this class for the projection output? The pooling output is also part of the previous output "BaseModelOutputWithPooling" -> should we maybe focus on the image_embeds here instead of the last_hidden_state?

@dataclass
class CLIPTextModelOutput(ModelOutput):
"""
Base class for model's outputs that also contains a pooling of the last hidden states.
Copy link
Member

@patrickvonplaten patrickvonplaten Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Base class for model's outputs that also contains a pooling of the last hidden states.
Base class for text model's outputs that also contains a pooling of the last hidden states.

@patrickvonplaten
Copy link
Member

patrickvonplaten commented Sep 11, 2022

Just to understand better - what is a checkpoint that uses such a projection layer? Is that really part of CLIP or rather of the model built on top of CLIP? Also if one uses the text_embeds or image_embeds output -> what is the purpose of also having the pooled_output?

Wondering if we should instead create a new head here instead of forcing it into the same class? Or is this an architecture that the official CLIP is using often?

@patil-suraj
Copy link
Member Author

patil-suraj commented Sep 12, 2022

what is a checkpoint that uses such a projection layer? Is that really part of CLIP or rather of the model built on top of CLIP?

The projection layers are already part of the CLIP model. Those are used to convert the final hidden states of vision and text model into clip embedding space.

self.visual_projection = nn.Linear(self.vision_embed_dim, self.projection_dim, bias=False)
self.text_projection = nn.Linear(self.text_embed_dim, self.projection_dim, bias=False)

The reason we added CLIPTextModel and CLIPVisionModel, is that users could load text and vision models separately as these individual models can be used in downstream task. But the current design is not optimal, as it does not return final clip embeddings. Those final embeddings are very useful for downstream tasks such retrieval, classification. And now these are also being used in text2image or image2image models.

So if a user needs either the text embeds or vision embeds they need to load the whole clip model, or write a custom wrapper module to include the projection layer (which is what we did for safety checker in diffusers https://github.com/huggingface/transformers/blob/main/src/transformers/models/clip/modeling_clip.py#L880)

Wondering if we should instead create a new head here instead of forcing it into the same class?

Good point! Then maybe we could add CLIPTextModelWithProjection and CLIPVisionModelWithProjection.

@sgugger
Copy link
Member

sgugger commented Sep 12, 2022

Good point! Then maybe we could add CLIPTextModelWithProjection and CLIPVisionModelWithProjection.

I'd prefer that solution too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants