[GH-ISSUE #84] Why calculate logits_per_image and logits_per_text separately. #77

Closed
opened 2026-10-31 21:10:51 +00:00 by bsakaguchi · 2 comments
Owner

Originally created by @vinson2233 on GitHub (Apr 8, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/84

Just a quick question on calculating the logits.

https://github.com/openai/CLIP/blob/main/clip/model.py line 363

logit_scale = self.logit_scale.exp()  
logits_per_image = logit_scale * image_features @ text_features.t()  
logits_per_text = logit_scale * text_features @ image_features.t()  

Let
BS = Batch_size
A = image_features is matrix with dimension BS x 512
B = text_features is matrix with dimension BS x 512
x' is transpose of x
Then AxB' = (BxA')'

Is there any reason to not writing logits_per_text simply as the transpose of logits_per_image?

logits_per_text = logits_per_image.t()

Also in the paper you only calculate the logits once
logits = np.dot(I_e, T_e.T) * np.exp(t)

Thanks

Originally created by @vinson2233 on GitHub (Apr 8, 2021). Original GitHub issue: https://github.com/openai/CLIP/issues/84 Just a quick question on calculating the logits. https://github.com/openai/CLIP/blob/main/clip/model.py line 363 ```python logit_scale = self.logit_scale.exp() logits_per_image = logit_scale * image_features @ text_features.t() logits_per_text = logit_scale * text_features @ image_features.t() ``` Let BS = Batch_size A = image_features is matrix with dimension BS x 512 B = text_features is matrix with dimension BS x 512 x' is transpose of x Then AxB' = (BxA')' Is there any reason to not writing logits_per_text simply as the transpose of logits_per_image? ```python logits_per_text = logits_per_image.t() ``` Also in the paper you only calculate the logits once logits = np.dot(I_e, T_e.T) * np.exp(t) Thanks
Author
Owner

@jongwook commented on GitHub (Apr 8, 2021):

That's correct, no need to do the same calculation twice. The two lines were remnant of distributed training where the transposed matrices were all-gathered tensors.

<!-- gh-comment-id:815557793 --> @jongwook commented on GitHub (Apr 8, 2021): That's correct, no need to do the same calculation twice. The two lines were remnant of distributed training where the transposed matrices were all-gathered tensors.
Author
Owner

@vinson2233 commented on GitHub (Apr 8, 2021):

I see, thanks :)

<!-- gh-comment-id:815562212 --> @vinson2233 commented on GitHub (Apr 8, 2021): I see, thanks :)
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#77
No description provided.