# How to use specific gpu in accelerate?

**URL:** <https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810>\
**Category:** 🤗Accelerate\
**Created:** [November 20, 2023, 5:42am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810 "2023-11-20T05:42:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nastyhilda](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nastyhilda/32/21332_2.png) [@nastyhilda](https://discuss.huggingface.co/u/nastyhilda)\
**Post date:** [November 20, 2023, 5:42am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/1 "2023-11-20T05:42:51Z")

</div>

I want to use GPUs with different conditions.  
But I think the accelerator.device() is always cuda:0. even I wanted to rewrite it like cuda:1 or cuda:2 but it couldn’t be modified. How to fix this problem?

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](https://discuss.huggingface.co/u/muellerzr)\
**Post date:** [November 20, 2023, 3:02pm UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/2 "2023-11-20T15:02:24Z")

</div>

You can specify either `CUDA_VISIBLE_DEVICES` or use the `--gpu_ids` param to either your config or `accelerate launch` 🙂

---

<div class="post-metadata">

**Author:** ![hanjohn](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@hanjohn](https://discuss.huggingface.co/u/hanjohn)\
**Post date:** [November 21, 2023, 5:27am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/3 "2023-11-21T05:27:13Z")

</div>

I’m a bit confused by your response. In fact, I already added the following code at the beginning of my file, specifying GPU number 6:

```auto
os.environ['CUDA_VISIBLE_DEVICES'] = str(6)
device = torch.device(f"cuda:{args.gpu}" if torch.cuda.is_available() else "cpu")

```

However, after using `accelerator = Accelerator()` , when I check `accelerator.device` , it still shows `device(type='cuda')` and remains on device 0. If I manually set `accelerator.device = self.device` , it throws an error: `AttributeError: can't set attribute` . Could you please provide more detailed instructions on how to specify the GPU using the Accelerator? I would greatly appreciate it.

---

<div class="post-metadata">

**Author:** ![nastyhilda](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nastyhilda/32/21332_2.png) [@nastyhilda](https://discuss.huggingface.co/u/nastyhilda)\
**Post date:** [November 21, 2023, 5:57am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/4 "2023-11-21T05:57:38Z")

</div>

exactly the same situation!!

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](https://discuss.huggingface.co/u/muellerzr)\
**Post date:** [November 21, 2023, 1:56pm UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/5 "2023-11-21T13:56:26Z")

</div>

> [@hanjohn](#):
>
> `os.environ['CUDA_VISIBLE_DEVICES'] = str(6)`

You cannot do this in your python file like that, this has to be done _before_ your python file has been called, or before torch/accelerate/anything that init’s the GPU has been imported (possibly).

So solutions:

```bash
accelerate launch --gpu_ids 6 myscript.py

```

```bash
CUDA_VISIBLE_DEVICES=6 python myscript.py

```

(Do not know if this last solution will actually work, I haven’t tried it before)

```python
import os
os.environ["CUDA_VISIBLE_DEVICES"] = str(6)
import torch
...

```

---

<div class="post-metadata">

**Author:** ![hanjohn](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@hanjohn](https://discuss.huggingface.co/u/hanjohn)\
**Post date:** [November 21, 2023, 2:59pm UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/6 "2023-11-21T14:59:58Z")

</div>

Thank you for your prompt reply. However, when following your guidance I encountered the following error:

RuntimeError: CUDA error: invalid device ordinal  
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.

---

<div class="post-metadata">

**Author:** ![nastyhilda](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nastyhilda/32/21332_2.png) [@nastyhilda](https://discuss.huggingface.co/u/nastyhilda)\
**Post date:** [November 22, 2023, 3:09am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/7 "2023-11-22T03:09:15Z")

</div>

Thank you I solved it

---

<div class="post-metadata">

**Author:** ![hanjohn](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@hanjohn](https://discuss.huggingface.co/u/hanjohn)\
**Post date:** [November 22, 2023, 4:57am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/8 "2023-11-22T04:57:16Z")

</div>

Hi, Could you please explain in more detail how you solved this problem? I’m interested to understand the steps you took to resolve it.

---

<div class="post-metadata">

**Author:** ![nastyhilda](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nastyhilda/32/21332_2.png) [@nastyhilda](https://discuss.huggingface.co/u/nastyhilda)\
**Post date:** [November 22, 2023, 5:14am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/9 "2023-11-22T05:14:16Z")

</div>

When changing your GPU through os.environ[‘CUDA\_VISIBLE\_DEVICES’], it is important that the env code should come first before all import sections. I think I failed when I tried to change GPU after all the other Accelerate and torch or tensorflow modules had been imported, suggesting that the problem of the ordinal environment codes order is the biggest.

---

<div class="post-metadata">

**Author:** ![hanjohn](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@hanjohn](https://discuss.huggingface.co/u/hanjohn)\
**Post date:** [November 23, 2023, 12:49am UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/10 "2023-11-23T00:49:05Z")

</div>

Thank you I solved it

---

<div class="post-metadata">

**Author:** ![cs-mshah](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@cs-mshah](https://discuss.huggingface.co/u/cs-mshah)\
**Post date:** [April 25, 2024, 7:33pm UTC](https://discuss.huggingface.co/t/how-to-use-specific-gpu-in-accelerate/62810/11 "2024-04-25T19:33:41Z")

</div>

None of this worked for me. Everywhere I added CUDA\_VISIBLE\_DEVICES=4 but somehow it is taking 0. Nowhere in the code have I specified 0. This is the crappiest thing I’ve ever experienced. Wasted 1 hour and still not resolved. Kindly change the api to something simpler and respect the environment variables. Here is the code I’m using: [GitHub - thuanz123/realfill](https://github.com/thuanz123/realfill)
