# ScalingConfig() num\_workers not corresponding to training runs?

**URL:** <https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744>\
**Category:** Ray Train\
**Created:** [November 7, 2023, 5:51pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744 "2023-11-07T17:51:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ray\_user\_11](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@ray\_user\_11](https://discuss.ray.io/u/ray_user_11)\
**Post date:** [November 7, 2023, 5:51pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/1 "2023-11-07T17:51:34Z")

</div>

I am running a `ray.tune.Tuner()` that depends on a `ScalingConfig()`. My understanding from the documentation is that, when using only CPUs, the number of simultaneous training runs should be equal to `num_workers`.

However, I am observing the following: With `num_workers=1` and 1 CPU per worker, 7 training runs launch simultaneously, and 8/8 of my CPUs are used (presumably 7 CPUs for the training runs and 1 CPU for `ray.tune.Tuner()`. With any other combination, only 1 training run launches. E.g., with `num_workers=7` and 1 CPU per worker, or with `num_workers=10` and 0.5 CPU per worker.

I do not think the problem is due to lack of CPUs, as e.g. 10 workers \* 0.5 CPUs per worker = 5 CPUs, which is less than the 7 I have available. I also get no message about lacking resources.

I am doing all of this with a fresh install of Ray, so I believe my version is up to date.

My code:

```auto
scaling_config = ScalingConfig(num_workers=1, 
                               use_gpu=False,
                               resources_per_worker={"CPU": 1})

trainer = TorchTrainer(train_func, 
                       scaling_config=scaling_config)

tuner = ray.tune.Tuner(
    trainer,
    tune_config=tune.TuneConfig(
        num_samples=num_samples
    ),
    run_config=RunConfig(
        storage_path=storage_path,
        checkpoint_config=CheckpointConfig(
            checkpoint_score_attribute="val_loss",
            checkpoint_score_order="min",
            num_to_keep=1
            )   
        ),
    param_space=config
)

results = tuner.fit()

```

---

<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:** [November 7, 2023, 5:57pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/2 "2023-11-07T17:57:01Z")

</div>

`ScalingConfig.num_workers` corresponds to the number of distributed workers for a single `TorchTrainer`. See [here](https://docs.ray.io/en/releases-2.8.0/train/overview.html) for an overview.

For Tune, the number of simultaneous training runs (aka trials) can be configured by `TuneConfig.max_concurrent_trials`.

---

<div class="post-metadata">

**Author:** ![ray\_user\_11](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@ray\_user\_11](https://discuss.ray.io/u/ray_user_11)\
**Post date:** [November 7, 2023, 8:34pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/3 "2023-11-07T20:34:45Z")

</div>

I have increased `max_concurrent_trials` but I do not get any increase in my number of simultaneously running trials.

I have 7 available CPUs. I set `max_concurrent_trials=n` in TuneConfig(). I want to run _n_ concurrent trials. How can I do this? It is not clear to me from the documentation.

---

<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:** [November 9, 2023, 9:21pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/4 "2023-11-09T21:21:40Z")

</div>

@ray_user_11 The Ray Train “coordinator” actor actually takes up 1 CPU by default as well. So even if you have 1 worker, you’ll have 2 actors:

```auto
Coordinator (1 CPU [default])
  -> Worker (1 CPU [default, and from your setting]

```

The coordinator is not really doing any work (just lightweight event processing and making remote function calls), so you can set that to 0 CPUs.

```diff
scaling_config = ScalingConfig(num_workers=1, 
                               use_gpu=False,
+ trainer_resources={"CPU": 0},
                               resources_per_worker={"CPU": 1})

```

Now, if your cluster has N CPUs, then each “TorchTrainer” instance will take up just a single CPU. You should be able to get 7 concurrent trials running at a time.

---

<div class="post-metadata">

**Author:** ![ray\_user\_11](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@ray\_user\_11](https://discuss.ray.io/u/ray_user_11)\
**Post date:** [November 10, 2023, 6:58pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/5 "2023-11-10T18:58:12Z")

</div>

Is possible concurrent trials = number of cpus? Or can I use a fractional `resources_per_worker`, setting `num_workers=1`, `trainer_resources={"CPU": 0}`, `resources_per_worker={"CPU": number_of_cpus / possible_concurrent_trials}` to get whatever number of possible concurrent trials I want? How do I calculate the ceiling for possible concurrent trials given my hardware?

Setting fractional `resources_per_worker` has not led to any increase in number of concurrent trials for me in the past. Instead, it has decreased the number of concurrent trials.

---

<div class="post-metadata">

**Author:** ![EthanMarx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ethanmarx/32/5357_2.png) [@EthanMarx](https://discuss.ray.io/u/EthanMarx)\
**Post date:** [January 12, 2024, 5:27pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/6 "2024-01-12T17:27:45Z")

</div>

I am encountering the same confusion. Not clear how to configure the number of trials properly via ScalingConfig when running with RayTrain

---

<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:** [January 17, 2024, 11:17pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/7 "2024-01-17T23:17:56Z")

</div>

@ray_user_11 @EthanMarx

Ray will not stop you from scheduling as many concurrent trials as you want.

For example, let’s say I have a cluster with 16 CPUs.

This setup would give 16 CPUs / (2 CPUs per trial) = 8 concurrent trials:

```python
num_workers = 2
cpus_per_worker = 1

trainer = TorchTrainer(
    ...,
    scaling_config=ScalingConfig(
        num_workers=2,
        trainer_resources={"CPU": 0},
        resources_per_worker={"CPU": cpus_per_worker},
    )
)
tune.Tuner(trainer, tune_config=tune.TuneConfig(num_samples=100), ...)

```

This setup would give 16 CPUs / (0.5 CPUs per trial) = 32 concurrent trials:

```python
num_workers = 2
cpus_per_worker = 1

trainer = TorchTrainer(
    ...,
    scaling_config=ScalingConfig(
        num_workers=2,
        trainer_resources={"CPU": 0},
        resources_per_worker={"CPU": cpus_per_worker},
    )
)
tune.Tuner(trainer, tune_config=tune.TuneConfig(num_samples=100), ...)

```

In practice, you’ll be scheduling based on GPU availability, which is the limiting factor for your max concurrency. (each trial needs X whole GPUs)

---

<div class="post-metadata">

**Author:** ![EthanMarx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ethanmarx/32/5357_2.png) [@EthanMarx](https://discuss.ray.io/u/EthanMarx)\
**Post date:** [February 4, 2024, 6:31pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/8 "2024-02-04T18:31:43Z")

</div>

What is the motivation for scaling up the number of workers in the `ScalingConfig` as opposed to adding more `resources_per_worker`?

How do these workers interact with the pytorch lightning workers that get launched during distributed training?

For example, what’s the difference between specifying

```auto

trainer = TorchTrainer(
    ...,
    scaling_config=ScalingConfig(
        num_workers=1
        trainer_resources={"CPU": 0},
        resources_per_worker={"GPU": 4},
    )
)

```

and

```auto
trainer = TorchTrainer(
    ...,
    scaling_config=ScalingConfig(
        num_workers=4
        trainer_resources={"CPU": 0},
        resources_per_worker={"GPU": 1},
    )
)

```

Am I correct in thinking that, in both of these cases, a cluster with 4 GPUs will launch only one trial?

Also, for my use case I am launching ray workers via kubernetes. So it’s possible that the GPUs are spread across multiple nodes

---

<div class="post-metadata">

**Author:** ![EthanMarx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ethanmarx/32/5357_2.png) [@EthanMarx](https://discuss.ray.io/u/EthanMarx)\
**Post date:** [February 5, 2024, 9:25pm UTC](https://discuss.ray.io/t/scalingconfig-num-workers-not-corresponding-to-training-runs/12744/9 "2024-02-05T21:25:05Z")

</div>

@justinvyu

So I’ve configured a static ray kubernetes cluster with 1 node that contains 8 gpus and 68 cpus.  
I launched a Tune job requesting 20 trials with the following `ScalingConfig`

```python

scaling_config = ScalingConfig(
    trainer_resources={"CPU": 0},
    resources_per_worker={"CPU": 8, "GPU": 1},
    num_workers=1,
    use_gpu=True,
)

```

The tune job starts fine and initally schedules 8 jobs. However, eventually some of the jobs Error out and I see the following

```auto
Ignore this message if the cluster is autoscaling. No trial is running and no new trial has been started
 within the last 60 seconds. This could be due to the cluster not having enough resources available. You asked for 8.0 CPUs and
1.0 GPUs per trial, but the cluster only has 4.0 CPUs and 0 GPUs available. Stop the tuning and adjust the required resources (e
.g. via the `ScalingConfig` or `resources_per_trial`, or `num_workers` for rllib), or add more resources to your cluster.

```

I am confused why the tuner is trying to schedule another trial when all of my resources are already taken. How can I fix this? Thank you!
