mirror of
https://github.com/openai/CLIP.git
synced 2026-11-05 01:37:26 +00:00
[PR #530] [CLOSED] Clamp logit_scale to prevent numerical instability #524
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openai/CLIP#524
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/openai/CLIP/pull/530
Author: @Mr-Neutr0n
Created: 2/11/2026
Status: ❌ Closed
Base:
main← Head:fix/clamp-logit-scale📝 Commits (1)
10e2590Clamp logit_scale to prevent numerical instability during training📊 Changes
1 file changed (+1 additions, -0 deletions)
View changed files
📝
clip/model.py(+1 -0)📄 Description
Summary
torch.clamp(logit_scale, max=100)after exponentiation inCLIP.forward()to prevent the learned temperature parameter from causing numerical overflow during training/fine-tuning.Problem
The
logit_scaleparameter is learned during training with no upper bound enforced. As training progresses,self.logit_scalecan grow large enough thatself.logit_scale.exp()overflows, producingInf/NaNin the cosine similarity logits. This causes the contrastive loss to becomeNaNand training to diverge entirely.This is a known failure mode when fine-tuning CLIP, and the fix is consistent with:
open_clip/model.py)Fix
One-line addition in
clip/model.py:The max value of 100 corresponds to a minimum temperature of 0.01, which produces an extremely sharp softmax distribution and is well beyond any practical operating point.
Test plan
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.