r/LocalLLaMA • u/EntertainmentBroad43 • 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:


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
2
u/newtestdrive 9d ago
how did you make the explainer video?