A Kubeflow Trainer TrainJob on the torch-distributed runtime sets trainer.numNodes: 4 — how does that become pods, and how does each torchrun learn its place?
answer
- the TrainJob value wins
- one JobSet, one node replicated job
- the Torch policy launches through torchrun
- PET_ variables carry the topology
- spec.trainer cannot be edited later
basics
~10 strainer.numNodes overrides the runtime's mlPolicy.numNodes, so the JobSet's node job runs four pods. The Torch policy launches training through torchrun and injects PET_NNODES, PET_NPROC_PER_NODE, PET_NODE_RANK, PET_MASTER_ADDR and PET_MASTER_PORT into each node container.
solid answer
~40 sIn Kubeflow Trainer v2.3.0 the TrainJob's `trainer.numNodes` takes precedence over the runtime's `mlPolicy.numNodes`, which is 1 in the shipped `torch-distributed` runtime and defaults to 1 when unset. The controller renders a JobSet named after the TrainJob, and its `node` replicated job (the one labelled `trainer.kubeflow.org/trainjob-ancestor-step: trainer`) runs four pods, each with `trainer.image`, `trainer.command` and the requests in `trainer.resourcesPerNode`. Because the runtime's `mlPolicy` has a `torch` source, the job is launched via `torchrun`, and the Torch plugin injects the `PET_*` topology variables into the main `node` container: node count, processes per node, this pod's node rank, and the rendezvous host's address and port. `trainer.numProcPerNode` sets processes per node; the API documents `auto` as the Torch default. Every field under `trainer` is immutable, so resizing means a new TrainJob.
code
yaml · 16 linesapiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: layout-detect-ft
namespace: doc-ai
spec:
runtimeRef:
name: torch-distributed
trainer:
numNodes: 4
numProcPerNode: 8
image: registry.internal.example/doc-ai/layout-train:1.4
command: ["torchrun", "/workspace/train.py", "--config=/workspace/layout.yaml"]
resourcesPerNode:
limits:
nvidia.com/gpu: 8go deeper
Know that trainer.numNodes on the TrainJob sets the number of node pods and overrides the runtime's default.
Walk through the chain: TrainJob, JobSet named after it, the node replicated job, and the PET_ variables the Torch plugin injects for torchrun.
Size a multi-node GPU run correctly with resourcesPerNode and numProcPerNode, and explain why a resize is a new TrainJob because spec.trainer is immutable.
Decide which topology knobs a platform exposes to job authors and which stay fixed in runtimes, trading flexibility against keeping GPU shapes predictable for capacity planning.
## From one integer to a JobSet A **TrainJob** in Kubeflow Trainer v2.3.0 never creates pods directly. The controller turns it into a **JobSet**, a Kubernetes object that groups several Jobs (called *replicated jobs*) and is named after the TrainJob. The shipped `torch-distributed` ClusterTrainingRuntime defines a single replicated job called `node`, and that job's template carries the label `trainer.kubeflow.org/trainjob-ancestor-step: trainer`. That label marks it as the place where the TrainJob's `trainer` block is injected. The number of node pods comes from two places, and the TrainJob wins: - The runtime's `mlPolicy.numNodes` is the default. In `torch-distributed` it is `1`, and the API defaults it to `1` when it is missing. - The TrainJob's `trainer.numNodes` overrides it. With `numNodes: 4`, the `node` replicated job runs four pods. ## The fields that shape the topology | Field | Where it lives | Effect | |---|---|---| | `trainer.numNodes` | TrainJob | Number of node pods for this run | | `mlPolicy.numNodes` | Runtime | Default node count when the TrainJob omits it | | `trainer.numProcPerNode` | TrainJob | Processes per node; for Torch the API documents `auto` as the default, overridable with an integer | | `trainer.resourcesPerNode` | TrainJob | Resource requests and limits for each node pod, for example `nvidia.com/gpu: 8` | | `trainer.image`, `command`, `args`, `env` | TrainJob | Overrides for the `node` container | | `mlPolicy.torch` | Runtime | Selects the Torch launch path; its `envInjection.targets` can extend the variables to init or sidecar containers | Only one launch source may be set in an `mlPolicy`: `torch`, `mpi`, `jax`, `xgboost` or `flux`. A runtime with none of them runs plain pods with the node count and environment applied. ## What the Torch policy adds TrainJobs on a runtime with a `torch` source are launched via the `torchrun` CLI, and the Torch plugin injects these variables into the main `node` container of every pod: - `PET_NNODES`: how many nodes take part (4 here). - `PET_NPROC_PER_NODE`: how many processes `torchrun` starts on each node. - `PET_NODE_RANK`: this pod's position among the nodes, different on every pod. - `PET_MASTER_ADDR` and `PET_MASTER_PORT`: where all nodes meet to rendezvous. `torchrun` reads these in place of its own command-line options, then starts its local worker processes and hands each one its rank. What the training script does with those ranks, such as setting up a process group and wrapping the model, is ordinary PyTorch distributed code and not something the TrainJob controls. ## Sizing a 4 x 8 GPU run For a layout-detection transformer on four nodes with eight GPUs each: 1. Set `trainer.numNodes: 4`. 2. Request the GPUs per node in `trainer.resourcesPerNode`, for example a limit of `nvidia.com/gpu: 8`. 3. Leave `numProcPerNode` at the documented `auto`, or set `8` to pin one process per GPU. 4. Point `trainer.command` at `torchrun` and the training script. The runtime's image is used if `trainer.image` is omitted. 5. Apply it and watch `kubectl get trainjob`, whose `State` column shows the latest condition (`Complete`, `Failed`, `Suspended`). From Python, `CustomTrainer(num_nodes=4, resources_per_node={"gpu": 8})` passed to `TrainerClient().train()` sets the same fields. `TrainerClient().get_job_logs(name)` reads the `node-0` step by default. ## Reading the status back The TrainJob reports on its child Jobs as well as its own conditions. `status.jobsStatus` has one entry per replicated job, here `node`, with counts of child Jobs that are `ready`, `active`, `succeeded`, `failed` and `suspended`. A run stuck with fewer ready than expected often means the pods are still waiting for GPUs rather than failing. `status.conditions` ends in `Complete` or `Failed`; with `activeDeadlineSeconds` set, an overrun fails with reason `DeadlineExceeded`. From Python, `TrainerClient().get_job(name)` returns the job's steps, such as `node-0`, each with its pod name and status, and `wait_for_job_status(name)` polls every 2 seconds for up to 600 seconds by default. ## What the TrainJob does not do - **It cannot be resized in place.** `spec.trainer` is immutable, so going from four nodes to two means deleting the TrainJob and creating a new one. - **It takes a single node count.** `numNodes` is one integer in v1alpha1; there is no min-max range on the TrainJob. Restarting and resuming a long run belongs with the fault-tolerance material. - **It does not decide when all four pods start together.** That is the runtime's `podGroupPolicy` or a cluster queue, which the GPU-scheduling material covers. - **It does not edit the runtime.** The `torch-distributed` object still says `numNodes: 1` for the next TrainJob that omits the field.
- A preflight init container must also know the node rank. How do you give it the PET_ variables?By default the Torch plugin injects `PET_*` only into the main `node` container. The runtime's `mlPolicy.torch.envInjection.targets` lists a replicated job (`jobName: node`) and the extra `containerNames` that should receive them, such as an init container running a preflight check. Targets always get the full set: `PET_NNODES`, `PET_NPROC_PER_NODE`, `PET_NODE_RANK`, `PET_MASTER_ADDR` and `PET_MASTER_PORT`.
- What happens to a TrainJob that exceeds activeDeadlineSeconds?All running pods are terminated, the underlying JobSet is deleted, and the TrainJob gets a `Failed` condition with reason `DeadlineExceeded`. The timer starts at creation and resets when a suspended TrainJob is resumed. The field is immutable and its minimum is 1.
- The runtime's mlPolicy.numNodes always wins over the TrainJob
- numNodes counts workers, and a separate master pod is added on top
- The training script must compute its node rank from the pod name
- You can kubectl patch trainer.numNodes on a running TrainJob
- PET_ variables are injected into every container in the pod by default
Assumes Kubeflow Trainer v2.3.0 (trainer.kubeflow.org/v1alpha1), where mlPolicy.torch carries only envInjection and per-node process count is set on the TrainJob.