# Limit GPU cores for training

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729%3E%5C
**Category:** 🤗Transformers\
**Created:** [August 17, 2020, 4:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fproxyht)%5C
**Post date:** [August 17, 2020, 4:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729%2F1 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [August 17, 2020, 4:57am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729%2F2 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [August 17, 2020, 5:13am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729%2F3 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fproxyht)%5C
**Post date:** [August 22, 2020, 1:25am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729%2F4 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmollahsabbir)%5C
**Post date:** [September 14, 2023, 9:29am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flimit-gpu-cores-for-training%2F729%2F5 "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" ` ` `
