# Gradient Overflow issue while using deepspeed

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgradient-overflow-issue-while-using-deepspeed%2F167833%3E%5C
**Category:** Beginners\
**Created:** [August 28, 2025, 12:39am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgradient-overflow-issue-while-using-deepspeed%2F167833 "2025-08-28T00:39:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaydeepb](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jaydeepb/32/14906_2.png) [@jaydeepb](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjaydeepb)%5C
**Post date:** [August 28, 2025, 12:39am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgradient-overflow-issue-while-using-deepspeed%2F167833%2F1 "2025-08-28T00:39:29Z")

</div>

Hi. I’m trying to fine-tune `mistralai/Mistral-Small-24B-Base-2501` using deepspeed and consistently getting the overflow error. When I use `bf16` and `fp32,`I don’t see the overflow issue but the training loss is Nan. When I switch to `fp16` the training loss is correct but it throws the overflow error. How can I fix this? This works fine with smaller models. Using `lr=1e-7`.

My `df_config.json`:

```auto
{
    "train_micro_batch_size_per_gpu": 1,
    "gradient_accumulation_steps": 8,
    "zero_optimization": {
        "stage": 2
    },
    "zero_allow_untested_optimizer": true,
    "fp16": {
        "enabled": true,
        "loss_scale": 0,
        "initial_scale_power": 32,
        "loss_scale_window": 1000,
        "hysteresis": 2,
        "min_loss_scale": 1
    },
    "gradient_clipping": 1.0,
    "wall_clock_breakdown": false
}

```

Using `deepspeed 0.17.2` and `transformers 4.42.4`.

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [August 28, 2025, 1:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgradient-overflow-issue-while-using-deepspeed%2F167833%2F2 "2025-08-28T01:04:31Z")

</div>

If the GPU supports bfloat16, it’s probably better to use bfloat16. Regarding `NaN` issues, SDPA seems to be the culprit in many cases. Try `attn_implementation="eager"`.

- [CUDNN sdp attention causes loss explosion #139298](https://github.com/pytorch/pytorch/issues/139298)
- [SDPA produces NaN with padding mask #103749](https://github.com/pytorch/pytorch/issues/103749)
- [Gemma 2 returns NaN when using default attn (sdpa) with padding #32390](https://github.com/huggingface/transformers/issues/32390)

---

<div class="post-metadata">

**Author:** ![jaydeepb](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jaydeepb/32/14906_2.png) [@jaydeepb](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjaydeepb)%5C
**Post date:** [August 28, 2025, 4:50am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgradient-overflow-issue-while-using-deepspeed%2F167833%2F3 "2025-08-28T04:50:31Z")

</div>

@John6666 loading the model in `bfloat16` and then using `bf16=true` in deepspeed seems to solve this issue for now!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [August 28, 2025, 4:51pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgradient-overflow-issue-while-using-deepspeed%2F167833%2F4 "2025-08-28T16:51:04Z")

</div>

Closed on August 28, 2025, 4:51pm UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
