Add image-text contrastive loss to Blip2ForImageTextRetrieval - #48583
Open
xquantize wants to merge 1 commit into
Open
Add image-text contrastive loss to Blip2ForImageTextRetrieval#48583xquantize wants to merge 1 commit into
xquantize wants to merge 1 commit into
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: blip_2 |
Contributor
CI recapDashboard: View test results in Grafana |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Blip2ForImageTextRetrievalalready computes ITC and ITM logits, but stops there — there's no loss path, so the class can't be used for training or fine-tuning. This adds the contrastive (ITC) half of that, as discussed in #34019.Two things are needed for a usable ITC loss:
A learnable temperature. The original LAVIS checkpoints carry a
tempparameter, but the conversion script drops it.convert_blip_2_original_to_pytorch.pyL223 asserts it away as an unexpected key, so converted models have no temperature to scale similarities with. This PR addslogit_scaleas a learnable parameter on the class, constant-initialised from a newBlip2Config.logit_scale_init_value(2.6592, matching CLIP). It does not read the LAVIStempvalue; mapping that through conversion would be a reasonable follow-up.The loss itself. A new
return_lossargument onforwardcomputes symmetric cross entropy overlogits_per_imageandlogits_per_text, each scaled bylogit_scale.exp(). The two directions are averaged and returned inBlip2ImageTextMatchingModelOutput.loss(and in the tuple path whenreturn_dict=False).Backwards compatible.
return_lossdefaults toFalse, and the returned logits stay unscaled either way, so existing inference behaviour is unchanged. There's a test for this.Scope. ITC only.
return_loss=Truewithuse_image_text_matching_head=Trueraises aValueErrorrather than silently doing the wrong thing. This is deliberately not the full QFormer pretraining recipe from #34019: no hard-negative ITM sampling, no ITG. @qgallouedec noted that's a training recipe rather than aforward(labels=...)addition. Partial progress on #34019.Test plan
New tests in
tests/models/blip_2/test_modeling_blip_2.py:lossisNonewhenreturn_lossis not passedreturn_loss=Truegives a finite scalar losslogit_scale.gradreturn_loss=TrueraisesValueErrorBefore submitting
Who can review?
@qgallouedec — you offered to look at a scoped draft on this class in #34019.