[GH-ISSUE #452] Issue with Text Encoder Output Dimensions in Fine-Tuned CLIP Model When Using with Stable Diffusion #387

Open
opened 2026-10-31 21:14:34 +00:00 by bsakaguchi · 2 comments
Owner

Originally created by @QXGeraldMo on GitHub (Jul 5, 2024).
Original GitHub issue: https://github.com/openai/CLIP/issues/452

I'm encountering an issue with the dimensions of the text encoder output in a fine-tuned CLIP model. The fine-tuning output of my CLIP model based on RN50 is (1, 1024), whereas the output from CLIPTextModel in transformers is (1, 77, 768). This causes difficulties when integrating with Stable Diffusion models, as the encoder_hidden_states parameter in U-Net requires an embedding of shape (x, x, 768).

Can someone help me deal with this problem? Would truncating the output of encode_text to (1, 1, 768) potentially resolve this issue?

Originally created by @QXGeraldMo on GitHub (Jul 5, 2024). Original GitHub issue: https://github.com/openai/CLIP/issues/452 I'm encountering an issue with the dimensions of the text encoder output in a fine-tuned CLIP model. The fine-tuning output of my CLIP model based on RN50 is (1, 1024), whereas the output from CLIPTextModel in transformers is (1, 77, 768). This causes difficulties when integrating with Stable Diffusion models, as the encoder_hidden_states parameter in U-Net requires an embedding of shape (x, x, 768). Can someone help me deal with this problem? Would truncating the output of encode_text to (1, 1, 768) potentially resolve this issue?
Author
Owner

@supersonicMaclaurin commented on GitHub (Jul 23, 2024):

same question

<!-- gh-comment-id:2244463685 --> @supersonicMaclaurin commented on GitHub (Jul 23, 2024): same question
Author
Owner

@99991 commented on GitHub (Jan 23, 2025):

CLIP has many models with different sizes. See also here for HuggingFace CLIP models.

That aside, if you finetune a vision encoder like ResNet50 without also finetuning the text encoder at the same time, their embeddings might become desynchronized and you get bad results. But truncating would definitely not work at all unless the model is specifically trained for this task (see e.g. Matryoshka embeddings).

<!-- gh-comment-id:2609972995 --> @99991 commented on GitHub (Jan 23, 2025): CLIP has [many models](https://github.com/openai/CLIP/blob/dcba3cb2e2827b402d2701e7e1c7d9fed8a20ef1/clip/clip.py#L30) with different sizes. See also [here](https://huggingface.co/models?search=openai%2Fclip) for HuggingFace CLIP models. That aside, if you finetune a vision encoder like ResNet50 without also finetuning the text encoder at the same time, their embeddings might become desynchronized and you get bad results. But truncating would definitely not work at all unless the model is specifically trained for this task (see e.g. Matryoshka embeddings).
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#387
No description provided.