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 Text Encoder #1969

Merged
merged 21 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions torchtune/models/clip/_text_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CLIPTextEncoder(nn.Module):
max_seq_len (int): context size, default 77
embed_dim (int): embedding/model dimension size, default 768
num_layers (int): number of transformer layers, default 12
eot_token (int): the id of the end-of-text token (for selecting the final output)
"""

def __init__(
Expand All @@ -35,11 +36,13 @@ def __init__(
max_seq_len: int = 77,
embed_dim: int = 768,
num_layers: int = 12,
eot_token: int = 49407,
):
super().__init__()
self.layers = nn.ModuleList([copy.deepcopy(layer) for i in range(num_layers)])
self.final_norm = final_norm
self.max_seq_len = max_seq_len
self.eot_token = eot_token

self.token_embedding = nn.Embedding(vocab_size, embed_dim)
self.position_embedding = nn.Parameter(torch.empty(max_seq_len, embed_dim))
Expand Down Expand Up @@ -87,10 +90,9 @@ def forward(
)
x = self.final_norm(x)

# Select the output of the EOS token for each encoding in the batch
# Select the output of the EOT token for each encoding in the batch
# [b, s, d] -> [b, d]
# TODO: handle the case when the EOS token is not the highest token ID
eos_token_positions = tokens.argmax(dim=-1)
eos_token_positions = (tokens == self.eot_token).int().argmax(dim=-1)
Copy link
Contributor

Choose a reason for hiding this comment

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

can do (tokens == self.eot_token).nonzero()

Copy link
Contributor Author

@calvinpelletier calvinpelletier Nov 20, 2024

Choose a reason for hiding this comment

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

we don't want all positions of the eot token, just the first one (argmax gives the first position where they match)

x = x.take_along_dim(eos_token_positions.view(-1, 1, 1), dim=1).squeeze(dim=1)

return x
Expand Down
Loading