# Limit GPU cores for training

**URL:** <https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729>\
**Category:** 🤗Transformers\
**Created:** [August 17, 2020, 4:14am UTC](https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729 "2020-08-17T04:14:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![proxyht](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@proxyht](https://discuss.huggingface.co/u/proxyht)\
**Post date:** [August 17, 2020, 4:14am UTC](https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729/1 "2020-08-17T04:14:41Z")

</div>

My company has a 6-gpu server. However, I only want to train the model on a specific number of gpus (maybe 1 or 2).  
How can I achieve that?

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](https://discuss.huggingface.co/u/valhalla)\
**Post date:** [August 17, 2020, 4:57am UTC](https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729/2 "2020-08-17T04:57:09Z")

</div>

What are you using for training ? `Trainer` or the `pytorch-lightning` ?

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](https://discuss.huggingface.co/u/valhalla)\
**Post date:** [August 17, 2020, 5:13am UTC](https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729/3 "2020-08-17T05:13:50Z")

</div>

You can set this env variable

```bash
CUDA_VISIBLE_DEVICES=0,1

```

specify the devices that you want to use and the `Trainer` will only use those cuda devices.

For `pytorch-lightninig` here’s [official doc](https://pytorch-lightning.readthedocs.io/en/latest/multi_gpu.html#select-gpu-devices)

---

<div class="post-metadata">

**Author:** ![proxyht](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@proxyht](https://discuss.huggingface.co/u/proxyht)\
**Post date:** [August 22, 2020, 1:25am UTC](https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729/4 "2020-08-22T01:25:10Z")

</div>

Thank you. This is exactly what I need

---

<div class="post-metadata">

**Author:** ![mollahsabbir](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mollahsabbir/32/19492_2.png) [@mollahsabbir](https://discuss.huggingface.co/u/mollahsabbir)\
**Post date:** [September 14, 2023, 9:29am UTC](https://discuss.huggingface.co/t/limit-gpu-cores-for-training/729/5 "2023-09-14T09:29:41Z")

</div>

Adding these two lines at the top of a Notebook (or Python script) can limit the devices to use for that script.

` ` `import os ` ` `  
` ` `os.environ["CUDA_VISIBLE_DEVICES"]="0,2" ` ` `
