How to restrict Trainer to use certain GPUs?

image

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

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

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):

%env CUDA_VISIBLE_DEVICES=0,1,2,3

from transformers import Trainer
...
1 Like