# Auto-finding batch size in Lightning not working with Tune

**URL:** <https://discuss.ray.io/t/auto-finding-batch-size-in-lightning-not-working-with-tune/9512>\
**Category:** Ray Tune\
**Created:** [February 25, 2023, 1:30pm UTC](https://discuss.ray.io/t/auto-finding-batch-size-in-lightning-not-working-with-tune/9512 "2023-02-25T13:30:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/doug/32/3965_2.png) [@Doug](https://discuss.ray.io/u/Doug)\
**Post date:** [February 25, 2023, 1:30pm UTC](https://discuss.ray.io/t/auto-finding-batch-size-in-lightning-not-working-with-tune/9512/1 "2023-02-25T13:30:35Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- Medium: It contributes to significant difficulty to complete my task, but I can work around it.

I have a small codebase that uses PyTorch Lightning. When not using Ray Tune, I can automatically find the best batch size by passing the `auto_scale_batch_size` argument to the Lightning Trainer and calling `trainer.tune`. However, when using Ray Tune with the same code, I always get a `torch.cuda.OutOfMemoryError` error in `trainer.fit`, after `trainer.tune`. If I remove the call to `trainer.tune` and select a fixed batch size, everything works.

- This problem happens both when I use a single node (the head node) or a cluster with more computers.
- I am not running distributed training, I train a single model in each GPU.
- I am using `ray.tune.integration.pytorch_lightning` and not `ray_lightning`.

Does anyone know what causes this? I am posting here because it seems related to Ray and how it allocates resources.

This feature would be useful for me because I have a cluster with GPUs with different amounts of memory and I also try different network topologies that work best with different batch sizes.

---

<div class="post-metadata">

**Author:** ![justinvyu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/justinvyu/32/4309_2.png) [@justinvyu](https://discuss.ray.io/u/justinvyu)\
**Post date:** [February 28, 2023, 7:24pm UTC](https://discuss.ray.io/t/auto-finding-batch-size-in-lightning-not-working-with-tune/9512/2 "2023-02-28T19:24:02Z")

</div>

Hi @Doug,

A few questions:

- Do you have a minimal reproduction script I could run?
- What’s your hardware (what kind of GPU, how much GRAM)?
- How large is your model?
- What version of Ray are you on?

---

<div class="post-metadata">

**Author:** ![Doug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/doug/32/3965_2.png) [@Doug](https://discuss.ray.io/u/Doug)\
**Post date:** [March 8, 2023, 5:39pm UTC](https://discuss.ray.io/t/auto-finding-batch-size-in-lightning-not-working-with-tune/9512/3 "2023-03-08T17:39:19Z")

</div>

I ended up refactoring my project and the issue disappeared, so I don’t believe it was a problem with Ray, but maybe my software design.

I’d consider this solved, if that’s OK with you @justinvyu.
