In Ray Train 2.59, how do ScalingConfig and FailureConfig decide how many workers a TorchTrainer runs and how many failures it survives?
answer
- worker count, GPUs, retry budget
- num_workers as int or (min, max)
- max_failures defaults to 0
- ray.train.get_checkpoint after a restart
- preemptions charged to a separate budget
basics
~20 sScalingConfig sets the worker group: num_workers (an int, or a (min, max) tuple for elastic runs), use_gpu and resources_per_worker. FailureConfig(max_failures), default 0, sets how many failures Ray Train recovers from by restarting every worker from the latest reported checkpoint.
solid answer
~40 sIn Ray 2.59, where Ray Train V2 is the default, `TorchTrainer(train_func, scaling_config=..., run_config=...)` starts one Ray actor per worker, sets up the torch process group and runs `train_func` in each. `ScalingConfig(num_workers=16, use_gpu=True)` reserves one GPU per worker (`resources_per_worker={"GPU": 2}` overrides that); `num_workers=(8, 16)` makes the run elastic — it starts once 8 workers fit, keeps asking for 16 and considers resizing every `elastic_resize_monitor_interval_s` (60 s). `RunConfig(failure_config=FailureConfig(max_failures=3))` lets the controller recover three times: it shuts down all workers, waits for replacement nodes and restarts them, and `ray.train.get_checkpoint()` hands each one the latest checkpoint reported through `ray.train.report`. `max_failures` defaults to 0 and -1 means unlimited; detected preemptions go to `max_preemption_failures`, unlimited by default. Checkpoints land under `RunConfig(storage_path)`, which must be shared storage for multi-node runs.
code
python · 24 linesimport ray.train
from ray.train import FailureConfig, RunConfig, ScalingConfig
from ray.train.torch import TorchTrainer
def train_func(config):
start_step = 0
ckpt = ray.train.get_checkpoint()
if ckpt:
with ckpt.as_directory() as d:
start_step = load_state(d) # your loader; returns the next step
for step in range(start_step, config["steps"]):
... # forward, backward, optimizer step
if step % 500 == 0:
save_and_report(step) # writes a dir, then ray.train.report(..., checkpoint=...)
trainer = TorchTrainer(
train_func,
train_loop_config={"steps": 20_000},
scaling_config=ScalingConfig(num_workers=(8, 16), use_gpu=True),
run_config=RunConfig(name="ft-summarizer-r7",
storage_path="/mnt/shared/ray-runs",
failure_config=FailureConfig(max_failures=3)),
)
result = trainer.fit()go deeper
Know that TorchTrainer runs your training function on several workers, that ScalingConfig sets how many, and that failures are not retried unless you configure it.
Explain num_workers as an int or a (min, max) tuple, use_gpu against resources_per_worker, and how FailureConfig, report and get_checkpoint fit together on a restart.
Compare Ray Train's restart path with torchrun's, account for the separate preemption budget, and make the training function resume correctly at a different worker count.
Choose between running jobs on a Ray cluster or under a batch scheduler with torchrun by who operates the cluster, how nodes fail and how replacement capacity arrives.
## The pieces **Ray Train** runs a PyTorch training function on a Ray cluster. In Ray 2.59 the rewritten **Ray Train V2** is the default (`RAY_TRAIN_V2_ENABLED` defaults to on). The objects: - `TorchTrainer(train_func, train_loop_config=..., scaling_config=..., run_config=...)` — starts one Ray **actor** per worker, initialises the torch process group across them and calls `train_func(config)` in each. The training function does not call `init_process_group` itself. - `ScalingConfig` — how many workers and what each reserves. - `RunConfig` — the run's `name`, its `storage_path`, and its `failure_config` and `checkpoint_config`. - `FailureConfig` — the retry budget. ## Sizing the worker group - `num_workers=16` with `use_gpu=True` reserves one GPU (and by default one CPU) per worker, so 16 GPUs in all. - `resources_per_worker={"CPU": 8, "GPU": 1}` overrides the per-worker reservation; the keys are case-sensitive. - `num_workers=(8, 16)` makes the run **elastic**: Ray Train asks for 16, starts as soon as 8 can be placed, and while the group is healthy considers growing it every `elastic_resize_monitor_interval_s` (60 s by default). A resize restarts the workers with the new size. - `placement_strategy` (default `"PACK"`) controls how the workers' placement group is spread over nodes. ## What a failure does 1. A worker raises (a CUDA out-of-memory, a storage error) or its node dies. 2. The controller shuts down **all** workers, as torchrun does. 3. If the failure counts against a budget that is not used up, it requests replacement nodes when needed and starts a new worker group — with an elastic config, possibly smaller than before, growing back toward the maximum later. 4. Each new worker calls `ray.train.get_checkpoint()`, which returns the latest checkpoint reported through `ray.train.report(metrics, checkpoint=...)`, and resumes from it. A training function that never loads it starts from scratch. The budgets in Ray 2.59's `FailureConfig`: - `max_failures` — worker and node errors; **default 0**, so out of the box the first failure ends the run; `-1` retries without limit. - `max_preemption_failures` — failures Ray identifies as node preemptions, counted separately; default `-1` (unlimited). - `fail_fast` is deprecated and raises if you pass it. A crashed **driver** (the process that called `trainer.fit()`) is not covered by `max_failures`. Relaunching the same script with the **same** `RunConfig(name, storage_path)` makes Ray Train find the run's state and resume; a different name starts a new run, and reusing a name by accident resumes an old one. ## Ray Train against torchrun | | Ray Train 2.59 | torchrun (PyTorch 2.14) | |---|---|---| | who restarts | one controller for the run | one agent per node | | what restarts | all workers | all workers | | default retries | `max_failures=0` | `--max-restarts=0` | | elastic range | `num_workers=(min, max)` | `--nnodes=MIN:MAX` (in nodes) | | replacement nodes | requested from the Ray cluster's autoscaler | brought by the outer scheduler | | where the checkpoint comes from | `ray.train.get_checkpoint()` | a path your script reads | Both stop every worker and both rely on the training function to resume. The deciding conditions are operational: a team already running a Ray cluster with an autoscaler gets node replacement and checkpoint bookkeeping from Ray; a team on Slurm or bare nodes gets the same restart semantics from torchrun with less machinery. ## Inside the training function `ray.train.torch.prepare_model(model)` moves the model to the worker's device and wraps it in `DistributedDataParallel` by default (`parallel_strategy="ddp"`; `"fsdp"` or `None` are the alternatives), and `prepare_data_loader(loader)` adds a `DistributedSampler` when there is more than one worker. The world size and the worker's rank come from `ray.train.get_context().get_world_size()` and `get_world_rank()`, never from constants, because an elastic run can come back at a different size: with a per-worker batch of 8, 16 workers average 128 samples per step and 10 workers only 80, unless the function raises gradient accumulation to compensate. ## What the training function owes Ray - Report checkpoints with `ray.train.report(metrics, checkpoint=Checkpoint.from_directory(d))`; every worker must call `report` the same number of times, because it acts as a barrier. - Load at the start with `ray.train.get_checkpoint()` and `checkpoint.as_directory()`. - Set `storage_path` to storage every node can reach; the default `~/ray_results` is local and does not survive losing the node. - Give every new run a unique `name`.
- With num_workers=(8, 16), what happens to the global batch when the run restarts with 10 workers?Ray Train changes the number of workers, not your batch arithmetic. If each worker keeps its per-worker batch, the global batch drops from 16 to 10 workers' worth; the training function should read `ray.train.get_context().get_world_size()` and adjust accumulation if the global batch must stay fixed.
- A spot node is reclaimed three times in a run with FailureConfig(max_failures=1). Does the run survive?In Ray 2.59 it can: a failure Ray identifies as a preemption is charged to `max_preemption_failures`, which is unlimited by default, not to `max_failures`. A genuine error that recurs on the next attempt is charged to `max_failures` and ends the run after one retry.
- Ray Train retries failed workers automatically with no FailureConfig set.
- Ray Train restarts only the worker that failed and keeps the rest running.
- num_workers=(8, 16) waits until all 16 workers can be placed before starting.
- The default storage_path is fine for a multi-node run.
- A driver crash is covered by max_failures like a worker crash.
Assumes Ray 2.59 (Ray Train V2 by default; FailureConfig.max_failures defaults to 0, max_preemption_failures to -1, fail_fast deprecated) and, for the torchrun comparison, PyTorch 2.14.