# How to restrict Trainer to use certain GPUs?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-restrict-trainer-to-use-certain-gpus%2F17185%3E%5C
**Category:** Beginners\
**Created:** [April 26, 2022, 4:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-restrict-trainer-to-use-certain-gpus%2F17185 "2022-04-26T04:05:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beyond](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/beyond/32/28447_2.png) [@beyond](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbeyond)%5C
**Post date:** [April 26, 2022, 4:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-restrict-trainer-to-use-certain-gpus%2F17185%2F1 "2022-04-26T04:05:37Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/1/188ca39e87d518d8e2d75fc5b562f3d1768e0e0d.png)

I have 8 gpus on my machine, I only want to train the model with for first 4 gpus. How can I achieve that?

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBramVanroy)%5C
**Post date:** [April 26, 2022, 6:56am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-restrict-trainer-to-use-certain-gpus%2F17185%2F2 "2022-04-26T06:56:39Z")

</div>

Try again, but add the os.environ call before you import anything else. So

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

from transformers import Trainer
...

```

If you’re using Jupyter, you can also use magic commands (again, at the top, before importing anything else):

```auto
%env CUDA_VISIBLE_DEVICES=0,1,2,3

from transformers import Trainer
...

```

---

<div class="post-metadata">

**Author:** ![Santosh-Gupta](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Santosh-Gupta](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSantosh-Gupta)%5C
**Post date:** [February 25, 2024, 8:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-restrict-trainer-to-use-certain-gpus%2F17185%2F3 "2024-02-25T08:48:48Z")

</div>

How about in cases where you would want to have additional training steps, and increase the number of devices each time? It looks like this method would need a new instance of each script
