Learned Temperature Collapse in Contrastive Loss
In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass.
In contrastive learning (like CLIP), logits are scaled by `1 / temperature`.
- Root cause
- In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity.
- Recommended fix
- Clamp the Temperature Parameter self.temperature.data = torch.clamp(self.temperature.data, min=1e-4, max=100.0) Clamping the temperature strictly prevents it from approaching zero, ensuring logits remain safely bounded.
- How Denpex helps
- Denpex matches Learned Temperature Collapse in Contrastive Loss across every rank in a distributed run and reports which rank failed first, so you act on the initiating node instead of the loudest one.
What this failure is
Learned Temperature Collapse in Contrastive Loss is a Model failure seen during ML training runs. In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass. Common tags: Loss Divergence.
Is this what broke your run? Paste your log.
You're reading about Learned Temperature Collapse in Contrastive Loss. Paste your own crash log or traceback below and get the real root cause for YOUR run, not this generic entry. No account, no card. Logs are masked at ingress and never saved to account history.
Want 14 days on the Scale plan?
Request an evaluation code. A verified workplace organization activates up to 50 diagnoses a day, alerts, history, and follow-up questions. No credit card or automatic subscription.
Why it happens (the mechanism)
The loss seems to be improving wonderfully right before it blows up, tricking researchers into thinking the model was learning exceptionally well before a 'random' numerical glitch.
What you'll observe
- Loss drops suddenly, then goes to NaN
- Gradient norm explodes
Common symptoms and what they mean
| Symptom | Why it happens |
|---|---|
| The contrastive loss initially decreases rapidly but suddenly explodes to NaN. | In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass. |
| The learned temperature parameter converges towards zero. | In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass. |
Which systems are affected
- PyTorch
- CLIP
- Contrastive Learning
How to confirm this is the problem
Use this checklist to test the hypothesis against a small reproduction. No single line proves the root cause, so preserve the preceding events and compare one variable at a time.
- ✓Track the value of the temperature parameter on Weights & Biases or TensorBoard.
- ✓Log the max logit value prior to CrossEntropyLoss.
The fix and the prevention pattern
The root cause is on this page and stays free. A free account adds the exact remediation steps, saved history, and the fix on every entry in the encyclopedia.
Sign up free. Unlock the full analysisNo credit card. Daily allowance follows verified trust tier. Instant access.
Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to see the initiating rank, collateral failures, exact fix, and verification command without uploading the log.
Install the free VS Code extensionRoot cause
- In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass.
The fix and how to prevent it
Evaluate Denpex on your own logs
Request a Scale evaluation code. A verified workplace organization activates 14 days with up to 50 diagnoses a day. Every account keeps its current diagnosis allowance and gets a verification path. No card or automatic subscription.
References
Don't just read the fix, diagnose your run
The encyclopedia tells you what went wrong. Denpex tells you what went wrong in YOUR training run. With your logs, your config, and your stack.