Fine-tuning Gemma 4 12B on GPU produces nan grads A developer reports that fine-tuning Google's Gemma 4 12B on GPU with a single example produces NaN gradients in the lm_head weights, while identical code on CPU returns finite gradients. The minimal reproduction, run with Python v3.14.10, Transformers v5.24.1 and PyTorch v2.15.0, calls backward() on a one-sentence loss in bfloat16 and shows torch.isnan on model.lm_head.weight.grad returning True on cuda:0 and False on CPU, contradicting the usual fp16 or oversized-gradient explanations. I’m trying to fine-tune Gemma 4 12B, but I’m running into an issue where fine-tuning it on sometimes even a single example produces nan grads on GPU. I’ve been able to find a very simple example that shows this and seems to be reproducible at least, for me : python import torch from transformers import AutoModelForCausalLM, AutoTokenizer model = AutoModelForCausalLM.from pretrained 'google/gemma-4-12B' tokenizer = AutoTokenizer.from pretrained 'google/gemma-4-12B' model = model.to 'cuda' inputs = tokenizer 'This is a test.', return tensors='pt' inputs = {k: v.to model.device for k, v in inputs.items } inputs 'labels' = inputs 'input ids' .detach .clone print model.lm head.weight.grad prints "None" print torch.any torch.isnan model.lm head.weight prints "tensor False " outputs = model inputs outputs.loss.backward print torch.any torch.isnan model.lm head.weight.grad prints "tensor True, device='cuda:0' " Most of the advice I’ve found about this elsewhere assumes that nans come in when updating the weights because the grads are too large/too small, or that it has to do with using fp16. But the source of the nans in my case in the grads themselves; and Gemma 4 uses bfloat16 by default, which is supposed to be a fix for nans though clearly it doesn’t seem to be working in this case . Oddly enough, running the same code as above with the model on the CPU works fine: python import torch from transformers import AutoModelForCausalLM, AutoTokenizer model = AutoModelForCausalLM.from pretrained 'google/gemma-4-12B' tokenizer = AutoTokenizer.from pretrained 'google/gemma-4-12B' inputs = tokenizer 'This is a test.', return tensors='pt' inputs 'labels' = inputs 'input ids' .detach .clone print model.lm head.weight.grad prints "None" print torch.any torch.isnan model.lm head.weight prints "tensor False " outputs = model inputs outputs.loss.backward print torch.any torch.isnan model.lm head.weight.grad prints "tensor False " Though of course, this isn’t feasible at scale since it’d take too long to fine-tune the model this way. Any idea how to fix this? It’s baffling me since it seems to be a problem that others attribute to using float16 instead of bfloat16, or which is an issue with gradients being too large or small, but that’s not what’s happening in my case since I’m already using bfloat16 and it’s the gradients themselves that start as nans rather than something that starts when the weights are updated. For reference, I’m running Python v3.14.10, Transformers v5.24.1, and PyTorch v2.15.0. Happy to provide more info if needed.