Logit Soft-Capping

easy
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 10. Everything before it counts as cleared.

Gemma 2 and Grok soft-cap logits so no value runs away: softcap(z)=c⋅tanh⁡(z/c)\text{softcap}(z) = c \cdot \tanh(z / c). Small logits pass through almost unchanged, while huge ones saturate smoothly at ±c (unlike hard clipping, the gradient never becomes exactly zero).

Implement:

  • softcap(logits, cap)
  • softcap_grad(logits, cap): the derivative 1−tanh⁡2(z/c)1 - \tanh^2(z / c)
  • max_prob(logits): the largest softmax probability (computed stably)
  • capped_ratio(logits, cap): max_prob(softcap(logits, cap)) / max_prob(logits), showing how capping tempers over-confident predictions
#transformers#stability#sampling2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Logit Soft-Capping · TechGradify