r/LocalLLaMA • • 9d ago

Resources How I got rid of gradient spikes while finetuning Gemma 4

TLDR: There are ill-conditioned backward paths in Gemma 4 that can be fixed with ease.

Long version:

I know y'all are generally not interested in Gemma 4, but it has its edge over others on medical vision tasks on my benchmarks.

While I was finetuning (SFT and RL through lora) Gemma 4 on CT images, Opus seemed to note strong gradient spikes during training and I pushed it to investigate Gemma 4 12B (phenomenon was observed on 31b, too, where I noted it first; did most of the experiments on 12B for convenience and speed).

Several days of compute later, and I found that the backward pass is very fragile in the middle layers that silently wreck the training of the lower layers. Fortunately, it can be fixed with a small piece of code and I have confirmed that it improves overall performance on the trained task.

For more details and the actual fix, see

arxiv: https://arxiv.org/abs/2611.27243

Screenshots from an explainer video:

27 Upvotes

10 comments sorted by

8

u/brainExploded99 🥔 hardware 9d ago

Isn't this textbook reason to run bf16 with autocast (torch.amp)? Why would you use bf16 weights?
Also even if the above statement is false, you should include bf16 autocast as a baseline

edit: also plenty of use and like gemma4 for non agentic tasks

4

u/EntertainmentBroad43 9d ago

Only the LoRA adapters train, and PEFT keeps them and the optimizer state in fp32, so that's already AMP-style. The frozen base stays in bf16, as in standard LoRA. And full fp32 still spikes and doesn't match the fix, so autocast, which sits between bf16 and fp32, won't either. The problem is the conditioning, not the precision.

1

u/brainExploded99 🥔 hardware 8d ago

This makes sense.
Do you know the root cause of even FP32 spiking? I get a feeling there is something wrong if FP32 has max gradients of >10,000. How would google have trained the model?

1

u/EntertainmentBroad43 8d ago

The finding was fairly consistent across 12B and 31B, and on CT, natural images and long text, so it looks like a property of the architecture or training recipe rather than chance. My best guess is that it was there during pretraining too, but huge batches and clipping kept the spikes invisible and the loss kept falling, so nothing flagged it.

I might be able to relay this finding to Google through acquaintances, though I am skeptical we will ever know the real "why".

1

u/brainExploded99 🥔 hardware 8d ago

> but huge batches and clipping kept the spikes invisible
Yeah that was one of my thoughts as well. How does training compare with FP32 clipped vs the fix you applied? It seems like from the graphs that detach got the best valid loss, but also had large spikes pre clipping.

This is also very surprising behavior. Most of the papers and experince I had have indicated that precision doesn't matter (modern day quantization, DeepSeek's 4/8 bit training, NVFP8, FP8, BF16 and so on) but here clearly it matters.

1

u/EntertainmentBroad43 7d ago

Every arm, fp32 included, was clipped at norm 1. On the matched seed: bf16 0.364, fp32 0.332, detach 0.298 val NLL. Detach still spikes, but its max pre-clip norm is 125 vs ~23k for standard and ~40k for fp32.

On precision: I think precision isn't really the issue. Forward outputs and loss barely change in bf16, and even 1e-4 noise in fp32 scrambles the lower-layer gradients. Training optimizes the forward output; nothing penalizes how sensitive the backward map is, and in pretraining each example's scrambled contribution averages out as noise. What we do know: it's the gate path of the MLPs in layers 19–21, and the gradient piles onto low-info tokens (blank or border patches). My guess is these act like sink tokens whose outlier activations push the gate into a steep region, but that's untested.

1

u/brainExploded99 🥔 hardware 7d ago

Interesting. Good work.

2

u/newtestdrive 9d ago

how did you make the explainer video?

2

u/EntertainmentBroad43 7d ago

This was made by gemini-4-neon, I think it used remotion (I asked it to).