[GH-ISSUE #183] Attention is not masked on padding tokens #157

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

Originally created by @mesnico on GitHub (Nov 17, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/183

I noticed that no padding masks are used in the attention computation of the input sentence.
The transformer in the encode_text function should be masked with the actual sentence length. However, in this implementation, it appears not to be the case:
github.com/openai/CLIP@573315e83f/clip/model.py (L344)

All the 77 tokens (padding tokens comprised) seem to participate in the attention computation.
Am I missing something?

Thank you in advance!

Originally created by @mesnico on GitHub (Nov 17, 2021). Original GitHub issue: https://github.com/openai/CLIP/issues/183 I noticed that no padding masks are used in the attention computation of the input sentence. The transformer in the `encode_text` function should be masked with the actual sentence length. However, in this implementation, it appears not to be the case: https://github.com/openai/CLIP/blob/573315e83f07b53a61ff5098757e8fc885f1703e/clip/model.py#L344 All the 77 tokens (padding tokens comprised) seem to participate in the attention computation. Am I missing something? Thank you in advance!
Author
Owner

@jongwook commented on GitHub (Nov 17, 2021):

We use a causal attention mask (code, cf. #179) and it basically blocks any information from "future" tokens from flowing back in. So we can take the embedding at the EOT token (code), while safely ignoring any attention calculation of the padding tokens.

<!-- gh-comment-id:971752528 --> @jongwook commented on GitHub (Nov 17, 2021): We use a causal attention mask ([code](https://github.com/openai/CLIP/blob/573315e83f07b53a61ff5098757e8fc885f1703e/clip/model.py#L324-L330), cf. #179) and it basically blocks any information from "future" tokens from flowing back in. So we can take the embedding at the EOT token ([code](https://github.com/openai/CLIP/blob/573315e83f07b53a61ff5098757e8fc885f1703e/clip/model.py#L349-L350)), while safely ignoring any attention calculation of the padding tokens.
Author
Owner

@mesnico commented on GitHub (Nov 17, 2021):

I was totally ignoring the pad masking "side effect" of causal attention.
Thank you!

<!-- gh-comment-id:971789130 --> @mesnico commented on GitHub (Nov 17, 2021): I was totally ignoring the pad masking "side effect" of causal attention. Thank you!
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#157
No description provided.