The DeepSeek-V4 report attributes loss spikes to outlier activations in the MoE layers and lists SwiGLU clamping as one of two fixes: throughout training, the linear component of SwiGLU is clamped to the range and the gate component is capped from above at 10 (no lower cap on the gate).
Implement swiglu_clamped(x, W_gate, W_up, limit=10.0, clamp=True):
x has shape (n, d), W_gate and W_up have shape (d, h).clamp=True, replace by and by ; with clamp=False leave both untouched.(out, clipped_fraction): out has shape (n, h); clipped_fraction is a Python float, the number of entries of and whose value was changed by the clamp divided by the total number of entries of both (). It is 0.0 when clamp=False.Also implement clamp_report(x, W_gate, W_up, limit=10.0) returning a dict with max_unclamped (largest absolute activation without clamping), max_clamped (with clamping) and clipped_fraction, each rounded to 4 decimals.
0.5 * (1 + tanh(z / 2)) never overflows.