kornia/kornia
Auf GitHub ansehen[Bug]: Add numerical stability guard for SigLip2 logit_scale
Open
#3.481 geöffnet am 12. Jan. 2026
help wantedtriage
Repository-Metriken
- Stars
- (8.677 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 19T 9h) (7 gemergte PRs in 30 T)
Beschreibung
🐛 Describe the bug
When calling SigLip2Model.forward() during training with an unconstrained logit_scale parameter that has grown to 100.0, the model produces NaN values in logits_per_image and logits_per_text instead of the expected similarity matrix with finite values.
🔄 Steps to Reproduce
import torch
from kornia.models.siglip2 import SigLip2Model, SigLip2Config
config = SigLip2Config()
model = SigLip2Model(config)
# 1.Simulate large logit_scale (can occur during training)
model.logit_scale.data.fill_(100.0)
pixel_values = torch.randn(2, 3, 224, 224)
input_ids = torch.randint(0, 1000, (2, 10))
# 2.Forward pass produces NaN without clamping
output = model(pixel_values=pixel_values, input_ids=input_ids)
print(output.logits_per_image) # Contains NaN values
💻 Minimal Code Example
import torch
from kornia.models.siglip2 import SigLip2Model, SigLip2Config
# Create model
config = SigLip2Config()
model = SigLip2Model(config)
# Simulate extreme logit_scale value (can occur during training)
model.logit_scale.data.fill_(100.0) # Large positive value
# Prepare inputs
pixel_values = torch.randn(2, 3, 224, 224)
input_ids = torch.randint(0, 1000, (2, 10))
# Forward pass
output = model(pixel_values=pixel_values, input_ids=input_ids)
# Check for NaN (occurs without clamping)
print(f"logit_scale value: {model.logit_scale.item()}")
print(f"Effective scale (exp): {output.logit_scale.item()}") # Should overflow
print(f"Contains NaN: {torch.isnan(output.logits_per_image).any()}")
print(f"logits_per_image:\n{output.logits_per_image}")
✅ Expected behavior
The model should maintain numerical stability regardless of learned parameter values.
❌ Actual behavior
exp(100) overflows, producing inf/NaN in logits and loss.
🔧 Environment
- Kornia version: 0.8.2
- PyTorch version: 2.9.1
- Python version: 3.11.14
- OS: macOS (darwin)
- Installation method: pip / from source
- CUDA/cuDNN version: N/A (CPU testing)
- GPU model: N/A
📝 Additional context
The overflow can occur during:
- Extended training runs where
logit_scalelearns extreme values - Transfer learning scenarios with aggressive learning rates
- Any situation where the learnable temperature parameter grows beyond safe numerical bounds
🤝 Contribution Intent
- I plan to submit a PR to fix this bug
- I'm reporting this bug but not planning to fix it