# Fine-tuning Gemma 4 12B on GPU produces nan grads

> Source: <https://discuss.huggingface.co/t/fine-tuning-gemma-4-12b-on-gpu-produces-nan-grads/193226#post_1>
> Published: 2026-12-06 17:57:50+00:00

**Recommendation:** Before launching a Gemma 4 12B fine-tune, run one example's backward pass on GPU and check torch.isnan on lm_head.weight.grad; the reported setup produced NaNs on CUDA but not CPU.
More in the playbook "Fine-tuning and data": <https://wpnews.pro/playbooks/training-data>

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.
