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

fix coca training #710

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
10 changes: 7 additions & 3 deletions src/open_clip/coca_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def forward(
text: Optional[torch.Tensor] = None,
image_latent: Optional[torch.Tensor] = None,
image_embs: Optional[torch.Tensor] = None,
is_training=True
):
if image_latent is None or image_embs is None:
image_latent, image_embs = self._encode_image(image)
Expand All @@ -164,7 +165,9 @@ def forward(
text_latent, token_embs = self._encode_text(text)

# TODO: add assertion to avoid bugs?
labels = text[:, -token_embs.shape[1]:]
labels = text[:, 1:]
if is_training:
token_embs = token_embs[:, :-1]

logits = self.text_decoder(image_embs, token_embs)
return {
Expand Down Expand Up @@ -267,7 +270,7 @@ def generate(
while True:
x = out[:, -max_seq_len:]
cur_len = x.shape[1]
logits = self(image, x, image_latent=image_latent, image_embs=image_embs)["logits"][:, -1]
logits = self(image, x, image_latent=image_latent, image_embs=image_embs, is_training=False)["logits"][:, -1]
mask = (out[:, -1] == eos_token_id) | (out[:, -1] == pad_token_id)
sample = torch.ones((out.shape[0], 1), device=device, dtype=torch.long) * pad_token_id

Expand Down Expand Up @@ -363,7 +366,8 @@ def _generate_beamsearch(
model_inputs['images'],
model_inputs['text'],
image_latent=image_latent,
image_embs=image_embs
image_embs=image_embs,
is_training=False
)

for beam_group_idx in range(num_beam_groups):
Expand Down