# Gradient memory Issue

**URL:** <https://discuss.pytorch.org/t/gradient-memory-issue/184335>\
**Category:** Captum\
**Created:** [July 16, 2023, 2:23pm UTC](https://discuss.pytorch.org/t/gradient-memory-issue/184335 "2023-07-16T14:23:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESLAM\_ZAHER](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eslam_zaher/32/62290_2.png) [@ESLAM\_ZAHER](https://discuss.pytorch.org/u/ESLAM_ZAHER)\
**Post date:** [July 16, 2023, 2:23pm UTC](https://discuss.pytorch.org/t/gradient-memory-issue/184335/1 "2023-07-16T14:23:43Z")

</div>

Hi,

When I am calculating the integrated gradients, I ran out of GPU memory. It has to do with autograd or something, but I am not sure how to caculate a couple of integrated gradients with different steps and don’t run out of memory. I do the IG calculation within with torch.no\_grad(): with no different behaviour  
I am using A100 16G GPU

---

<div class="post-metadata">

**Author:** ![Z4K](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/z4k/32/64046_2.png) [@Z4K](https://discuss.pytorch.org/u/Z4K)\
**Post date:** [July 16, 2023, 2:56pm UTC](https://discuss.pytorch.org/t/gradient-memory-issue/184335/2 "2023-07-16T14:56:43Z")

</div>

Hi !  
You can consider using the **internal\_batch\_size** parameter when you call the attribute method.

---

<div class="post-metadata">

**Author:** ![ESLAM\_ZAHER](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eslam_zaher/32/62290_2.png) [@ESLAM\_ZAHER](https://discuss.pytorch.org/u/ESLAM_ZAHER)\
**Post date:** [July 16, 2023, 3:07pm UTC](https://discuss.pytorch.org/t/gradient-memory-issue/184335/3 "2023-07-16T15:07:30Z")

</div>

Would you care to elaborate more? The attribution method works on the model, input test example, and target. How batch size come into play? and why I am running out of memory on a low level?

---

<div class="post-metadata">

**Author:** ![Z4K](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/z4k/32/64046_2.png) [@Z4K](https://discuss.pytorch.org/u/Z4K)\
**Post date:** [July 16, 2023, 3:15pm UTC](https://discuss.pytorch.org/t/gradient-memory-issue/184335/4 "2023-07-16T15:15:00Z")

</div>

Hi,  
Integrated gradient computes gradients of multiple points between the baseline and input (the number of points corresponds to the **n\_steps** parameter). The default is 50 steps.  
You might run out memory because a batch of 50 points (if you have not changes the n\_steps parameter) is too much for your GPU memory.  
If so, you can either reduce the number of steps or use the parameter **internal\_batch\_size**.
