# PPO example cannot use GPU

**URL:** <https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155>\
**Category:** RLlib\
**Created:** [August 6, 2021, 6:58pm UTC](https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155 "2021-08-06T18:58:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://discuss.ray.io/u/daniel)\
**Post date:** [August 6, 2021, 6:58pm UTC](https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155/1 "2021-08-06T18:58:53Z")

</div>

I want to run PPO example on GPU. Here is my code for training.

```auto
from ray.rllib.agents.ppo import PPOTrainer
import ray

ray.init()

trainer = PPOTrainer(config={"env": "CartPole-v0", 
                            "framework": "torch",
                            "num_gpus": 1,
                            "num_workers": 4})   
trainer.train()

```

However, It returns errors like

```auto
/ray/rllib/policy/torch_policy.py", line 155, in __init__
    self.device = self.devices[0]
IndexError: list index out of range

```

I install Pytorch with GPU support. and `torach.cuda_is_available() == True`.  
The error seems like that ray could not find the GPU devices even though Pytorch can.

Is there any solution to this?

Thanks!

---

<div class="post-metadata">

**Author:** ![michaelzhiluo](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@michaelzhiluo](https://discuss.ray.io/u/michaelzhiluo)\
**Post date:** [August 7, 2021, 4:00am UTC](https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155/2 "2021-08-07T04:00:31Z")

</div>

@daniel What does `ray.get_gpu_ids()` return?

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://discuss.ray.io/u/daniel)\
**Post date:** [August 7, 2021, 4:15am UTC](https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155/3 "2021-08-07T04:15:53Z")

</div>

when I put this line after the `ray.init()` it returns me an empty list.  
Is there any way to let the ray find the GPU?

---

<div class="post-metadata">

**Author:** ![michaelzhiluo](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@michaelzhiluo](https://discuss.ray.io/u/michaelzhiluo)\
**Post date:** [August 7, 2021, 8:07am UTC](https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155/4 "2021-08-07T08:07:28Z")

</div>

This is probably a Ray core issue in that case. For now, I think manually setting GPU ids here: [ray/torch\_policy.py at master · ray-project/ray · GitHub](https://github.com/ray-project/ray/blob/master/rllib/policy/torch_policy.py#L181) should work for the time being.

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://discuss.ray.io/u/daniel)\
**Post date:** [August 7, 2021, 2:58pm UTC](https://discuss.ray.io/t/ppo-example-cannot-use-gpu/3155/5 "2021-08-07T14:58:35Z")

</div>

is there any other solution to solve this via some function/API parameter when I am building my own application?
