# How do I run my experiment on a single GPU?

**URL:** https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689
**Category:** Uncategorized
**Created:** [March 8, 2023, 2:54pm UTC](https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689 "2023-03-08T14:54:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mtt](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mtt/32/3992_2.png) [@mtt](https://discuss.ray.io/u/mtt)
#### Post date: [March 8, 2023, 2:54pm UTC](https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689/1 "2023-03-08T14:54:30Z")

</div>

I’m currently relying on `tune.Tuner` to run my experiment on a machine that has 28 CPUs and 2 GPUs. Being that I’m not the only one who has access to this machine, I’d like to restrict my experiment to a single GPU.

Despite specifying `with_resources(Trainer, {"cpu": 1, "gpu": 1})` both GPUs are used. The only way to avoid this is by setting `os.environ["CUDA_VISIBLE_DEVICES"] = "1"`.

Is there a way to achieve my goal without explicitly setting an environment variable myself? If I understand correctly, according to the [documentation](https://docs.ray.io/en/latest/tune/tutorials/tune-resources.html#how-to-leverage-gpus-in-tune) this should be taken care of by `tune.with_resources`:

> To leverage GPUs, you must set `gpu` in `tune.with_resources(trainable, resources_per_trial)`. This will automatically set `CUDA_VISIBLE_DEVICES` for each trial.

```auto
run_config = RunConfig(
    stop={"training_iteration": epochs},
    checkpoint_config=ck_config,
    name=f"{model_name}_{exp_details}",
    local_dir=str(Path( __file__ ).parent / "ray_checkpoints")
)

tuner = Tuner(
    trainable= with_resources(Trainer, {"cpu": 1, "gpu": 1}),
    run_config=run_config,
    tune_config=TuneConfig(mode="min", metric="val_loss", num_samples=5),
    param_space=configuration,
)

```

---

<div class="post-metadata">

### Author: ![justinvyu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/justinvyu/32/4309_2.png) [@justinvyu](https://discuss.ray.io/u/justinvyu)
#### Post date: [March 8, 2023, 5:32pm UTC](https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689/2 "2023-03-08T17:32:00Z")

</div>

Hi @mtt,

`tune.with_resources` sets the _resources per trial_, and since you have 5 trials with 2 GPUs, Tune will schedule 2 trials at a time, each taking one of the GPUs.

You can specify fractional GPUs per trial, as well as limit concurrency so you never go above some GPU usage. For example, this will run 2 trials concurrently on 1 GPU.

```diff
tuner = Tuner(
+ trainable= with_resources(Trainer, {"cpu": 1, "gpu": 0.5}),
    run_config=run_config,
    tune_config=TuneConfig(
        mode="min",
        metric="val_loss",
        num_samples=5,
+ max_concurrent_trials=2,
    ),
    param_space=configuration,
)

```

See [Ray Tune FAQ — Ray 2.3.0](https://docs.ray.io/en/latest/tune/faq.html#how-do-i-set-resources) for more info.

---

<div class="post-metadata">

### Author: ![Xinchengzelin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xinchengzelin/32/3352_2.png) [@Xinchengzelin](https://discuss.ray.io/u/Xinchengzelin)
#### Post date: [August 18, 2023, 12:35pm UTC](https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689/3 "2023-08-18T12:35:56Z")

</div>

Hi, @justinvyu  
Could I ask a more question?  
if the trainable is a TorchTrainer, I can’t use `tune.with_resources`, How could I do to use 0.5GPU for one trail?  
I test to set in the TorchTrainer：

```auto
scaling_config=ScalingConfig(
            num_workers=2s,  
            use_gpu=True,
            resources_per_worker={"GPU":0.5}
        )

```

However, It didn’t work as expected. How could I do? Thanks in advance

---

<div class="post-metadata">

### Author: ![matthewdeng](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/matthewdeng/32/1446_2.png) [@matthewdeng](https://discuss.ray.io/u/matthewdeng)
#### Post date: [August 19, 2023, 4:21pm UTC](https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689/4 "2023-08-19T16:21:11Z")

</div>

@Xinchengzelin can you create a new topic for this and elaborate a more about what the expected (resource) end state is?

---

<div class="post-metadata">

### Author: ![Xinchengzelin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xinchengzelin/32/3352_2.png) [@Xinchengzelin](https://discuss.ray.io/u/Xinchengzelin)
#### Post date: [August 20, 2023, 12:53am UTC](https://discuss.ray.io/t/how-do-i-run-my-experiment-on-a-single-gpu/9689/5 "2023-08-20T00:53:08Z")

</div>

Thanks @matthewdeng, I create a new topic: [How to use fraction GPU in `ray.tune.Tuner`?](https://discuss.ray.io/t/how-to-use-fraction-gpu-in-ray-tune-tuner/11863)
