[PR #271] [MERGED] Remove inefficient computation from AttentionPool2d Module #480

Closed
opened 2026-10-31 21:15:24 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/openai/CLIP/pull/271
Author: @jenkspt
Created: 7/21/2022
Status: ✅ Merged
Merged: 7/21/2022
Merged by: @jongwook

Base: main ← Head: fix-attention-pool2d


📝 Commits (3)

  • 8c0b98a fix inefficient attention computation
  • 5eb1828 remove erroneous formatting
  • 2fc776c simplified flatten

📊 Changes

1 file changed (+3 additions, -4 deletions)

View changed files

📝 clip/model.py (+3 -4)

📄 Description

This is a simple fix that removes unnecessary attention computation from the AttentionPool2d Module.
In the existing version, self attention is calculated on the full spatial + average embedding sequence with shape [(HW+1), N, C]. In the proposed fix, attention is calculated with the average embedding [1, N, C] as the query and the spatial + average embedding sequence [(HW+1), N, C] as the key/value.

I created this gist: https://gist.github.com/jenkspt/3a09cc150ab531781c6084c166047639 to demonstrate the equivalence of the existing implementation and the proposed one. There is parity in both the computation and the parameter state -- so there shouldn't be any breaking changes introduced.

I realize that AttentionPool2d is only used once in the CLIP model, so this fix will not have a huge impact -- however I arrived here from https://github.com/openai/guided-diffusion/blob/main/guided_diffusion/unet.py#L22-L51, which is based on the clip version (and has the same problem) -- so I think there is the added benefit for posterity


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/openai/CLIP/pull/271 **Author:** [@jenkspt](https://github.com/jenkspt) **Created:** 7/21/2022 **Status:** ✅ Merged **Merged:** 7/21/2022 **Merged by:** [@jongwook](https://github.com/jongwook) **Base:** `main` ← **Head:** `fix-attention-pool2d` --- ### 📝 Commits (3) - [`8c0b98a`](https://github.com/openai/CLIP/commit/8c0b98adfca6604fdb61999fdebdf39141da2512) fix inefficient attention computation - [`5eb1828`](https://github.com/openai/CLIP/commit/5eb1828ead57f4dfa49e24eef48aa70bdc804423) remove erroneous formatting - [`2fc776c`](https://github.com/openai/CLIP/commit/2fc776c0f9e62a9f795ed1a0552c84fb1ac644db) simplified flatten ### 📊 Changes **1 file changed** (+3 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `clip/model.py` (+3 -4) </details> ### 📄 Description This is a simple fix that removes unnecessary attention computation from the `AttentionPool2d` Module. In the existing version, self attention is calculated on the full spatial + average embedding sequence with shape `[(HW+1), N, C]`. In the proposed fix, attention is calculated with the average embedding `[1, N, C]` as the query and the spatial + average embedding sequence `[(HW+1), N, C]` as the key/value. I created this gist: [https://gist.github.com/jenkspt/3a09cc150ab531781c6084c166047639](https://gist.github.com/jenkspt/3a09cc150ab531781c6084c166047639) to demonstrate the equivalence of the existing implementation and the proposed one. There is parity in both the computation and the parameter state -- so there shouldn't be any breaking changes introduced. I realize that `AttentionPool2d` is only used once in the CLIP model, so this fix will not have a huge impact -- however I arrived here from [https://github.com/openai/guided-diffusion/blob/main/guided_diffusion/unet.py#L22-L51](https://github.com/openai/guided-diffusion/blob/main/guided_diffusion/unet.py#L22-L51), which is based on the clip version (and has the same problem) -- so I think there is the added benefit for posterity --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 21:15:24 +00:00
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#480
No description provided.