In Kubeflow Trainer v2, what does a TrainJob's runtimeRef point at, and which part of the training job does that runtime own?
answer
- two objects, two owners
- a blueprint the platform team keeps
- kind defaults to the cluster-scoped one
- namespaced runtime: same namespace only
- ancestor-step label marks where values land
basics
~20 sA TrainJob's runtimeRef names a ClusterTrainingRuntime (the default kind) or a same-namespace TrainingRuntime. That platform-owned runtime holds the JobSet template and mlPolicy; the TrainJob adds only run values such as image, command, node count and resources.
solid answer
~40 sIn Kubeflow Trainer v2.3.0 (`trainer.kubeflow.org/v1alpha1`), `spec.runtimeRef` is the required link from a `TrainJob` to a runtime: a `name`, a `kind` that defaults to `ClusterTrainingRuntime` and an `apiGroup` that defaults to `trainer.kubeflow.org`. A cluster-scoped `ClusterTrainingRuntime` can be used from any namespace; a namespaced `TrainingRuntime` only by TrainJobs in its own namespace. The runtime is the platform team's blueprint: the JobSet `template` with its replicated jobs and pod settings, the `mlPolicy` (a default `numNodes` plus at most one launch source such as `torch` or `mpi`) and an optional `podGroupPolicy`. The data scientist's TrainJob fills in `trainer` (`image`, `command`, `args`, `env`, `numNodes`, `resourcesPerNode`, `numProcPerNode`) and optional `initializer` URIs, and the controller merges both into one JobSet named after the TrainJob.
go deeper
Recall the pairing: a TrainJob names a runtime through runtimeRef, the kind defaults to ClusterTrainingRuntime, and the TrainJob supplies image, command and node count.
Explain how the controller merges the halves: a JobSet named after the TrainJob, built from the runtime template, with trainer values landing in the job labelled trainjob-ancestor-step: trainer.
Show the ownership boundary in practice: team TrainingRuntimes for local blueprints, runtimePatches for one-off pod settings, and why editing a shared runtime for one run is a defect.
Weigh how many runtimes a platform should publish against letting teams patch: fewer blueprints are easier to secure and upgrade, more give teams autonomy but multiply what you must keep validated.
## Why Trainer v2 splits a job in two The legacy **Kubeflow Training Operator** (v1) gave every framework its own kind under `kubeflow.org/v1`: `PyTorchJob`, `TFJob`, `MPIJob`. Each manifest carried complete pod templates, so whoever submitted a run also owned the base image, volumes, security context and every scheduling detail. **Kubeflow Trainer v2** replaced those kinds with one `TrainJob` kind plus two runtime kinds, `TrainingRuntime` and `ClusterTrainingRuntime`, all under `trainer.kubeflow.org/v1alpha1` in release v2.3.0. The Trainer documentation names the two personas this serves: **platform administrators**, who deploy and maintain runtimes, and **AI practitioners**, who submit TrainJobs against them. ## What runtimeRef resolves to `spec.runtimeRef` is the only required part of a TrainJob spec. It has three fields: | Field | Default | Meaning | |---|---|---| | `name` | none (required) | The runtime to use, for example `torch-distributed` | | `kind` | `ClusterTrainingRuntime` | Set `TrainingRuntime` to use a namespaced runtime | | `apiGroup` | `trainer.kubeflow.org` | The runtime's API group | The scoping rules follow from the kinds: - A **`ClusterTrainingRuntime`** is cluster-scoped, so a TrainJob in any namespace can reference it. - A **`TrainingRuntime`** is namespaced, and the TrainJob must live in the same namespace. This is how a team gets its own blueprint without touching the shared ones. - Because `kind` defaults to `ClusterTrainingRuntime`, a reference to a team runtime that leaves `kind` out resolves to a cluster runtime of that name, which is either missing or a different blueprint. - `runtimeRef` is **immutable** once the TrainJob exists; switching runtimes means creating a new TrainJob. ## Who owns which fields | Kept in the runtime (platform team) | Set in the TrainJob (person running the job) | |---|---| | JobSet `template`: replicated jobs, containers, default image, volumes, security context | `trainer.image`, `trainer.command`, `trainer.args`, `trainer.env` | | `mlPolicy`: default `numNodes` and the launch source (`torch`, `mpi`, `jax`, `xgboost`, `flux`) | `trainer.numNodes`, `trainer.numProcPerNode`, `trainer.resourcesPerNode` | | `podGroupPolicy` for gang admission | `suspend`, `activeDeadlineSeconds` | | Initializer jobs, when the runtime has them | `initializer.dataset` and `initializer.model` (`storageUri`, `env`, `secretRef`) | When a run needs something only the template can express, such as a `nodeSelector` or an extra volume, the TrainJob adds a **`runtimePatches`** entry keyed by a `manager` name instead of the person editing the shared runtime. ## How the two halves are merged 1. The controller reads the runtime named by `runtimeRef`. 2. It builds a **JobSet** whose name and namespace equal the TrainJob's, starting from the runtime's `template`. 3. Replicated jobs labelled `trainer.kubeflow.org/trainjob-ancestor-step: trainer` receive the TrainJob's `trainer` values; jobs labelled `dataset-initializer` or `model-initializer` receive the matching `initializer` values. 4. The `mlPolicy` plugin adds its launch configuration, and any `runtimePatches` entries are merged in. Anything the TrainJob leaves unset falls back to the runtime, so a TrainJob that names only `runtimeRef` runs the runtime's own image and node count. ## The shipped runtimes and the SDK path Trainer v2.3.0 ships `ClusterTrainingRuntime` manifests named `torch-distributed`, `deepspeed-distributed`, `jax-distributed`, `mlx-distributed` and `xgboost-distributed`, plus TorchTune runtimes for LLM fine-tuning. Every runtime carries the label `trainer.kubeflow.org/framework` so the Kubeflow SDK can recognise it. A minimal TrainJob against the PyTorch runtime looks like this: ```yaml apiVersion: trainer.kubeflow.org/v1alpha1 kind: TrainJob metadata: name: layout-detect-ft namespace: doc-ai spec: runtimeRef: name: torch-distributed # kind defaults to ClusterTrainingRuntime trainer: image: registry.internal.example/doc-ai/layout-train:1.4 command: ["torchrun", "/workspace/train.py"] numNodes: 4 ``` From Python, the Kubeflow SDK 0.5 builds the same object: `TrainerClient().train(runtime="torch-distributed", trainer=CustomTrainer(func=..., num_nodes=4))` returns the generated TrainJob name, and the runtime defaults to `torch-distributed` when omitted. `TrainerClient().list_runtimes()` shows which runtimes the cluster offers. ## Checking what a TrainJob resolved to When a run behaves unexpectedly, confirm the binding before debugging the training code: 1. List the blueprints: `kubectl get clustertrainingruntimes` for the shared ones and `kubectl get trainingruntimes -n doc-ai` for the team's own. 2. Read the TrainJob's `spec.runtimeRef` and compare its `kind` with the object you meant. 3. Look at the JobSet that carries the TrainJob's name; its replicated jobs show what the runtime contributed. 4. Check the TrainJob's latest condition, which `kubectl get trainjob` prints in its `State` column. Two runtime labels matter here. Runtimes shipped by the Kubeflow community carry `trainer.kubeflow.org/webhook-validation: disabled` because they were validated ahead of time; runtimes a platform team writes should omit it, so the Trainer validation webhook checks them on every create and update. A runtime marked `trainer.kubeflow.org/support: "deprecated"` makes the webhook warn when a TrainJob references it, and it may be removed two minor releases later. ## Mistakes to avoid - Treating the runtime as an image name. It is a full JobSet blueprint plus an ML policy. - Copying a shared `ClusterTrainingRuntime` into every TrainJob by hand. That rebuilds the v1 problem the split removed. - Editing a shared runtime to satisfy one team's run. A `runtimePatches` entry or a team `TrainingRuntime` keeps the change local. - Expecting a framework-specific kind. Trainer v2 has one TrainJob kind; the framework comes from the runtime's `mlPolicy`.
- A team's TrainJob in namespace doc-ai must use a runtime only that team may change. How do you set it up?Deploy a namespaced `TrainingRuntime` in `doc-ai` and reference it with `runtimeRef.kind: TrainingRuntime`. Only TrainJobs in that namespace can use it, so the team's blueprint cannot leak to others, and the shared `ClusterTrainingRuntime` objects stay untouched. Give it the `trainer.kubeflow.org/framework` label so the Kubeflow SDK recognises it.
- The run needs one node pool and an extra volume that the shared runtime lacks. Where does that go?Into a `runtimePatches` entry on the TrainJob, keyed by a `manager` name, which patches the runtime's JobSet template for this job only: pod `nodeSelector`, `tolerations`, `volumes`, container `volumeMounts` and similar allowed fields. `runtimePatches` replaced the older `podTemplateOverrides` field. If every run needs it, the platform team should put it in a runtime instead.
- Can you change runtimeRef on a TrainJob that has already been created?No. `runtimeRef`, `trainer` and `initializer` are immutable after creation, enforced by validation rules on the TrainJob schema. To switch runtimes you delete the TrainJob and create a new one. That keeps the JobSet that is running consistent with the spec that produced it.
A runtime is a printed form the platform team designs: layout, sections and fine print are fixed. A TrainJob is one filled-in copy. It writes in the blanks the form marks, such as image, command and node count, and may clip a labelled note to its own copy, but cannot redraw the form for everyone else.
- Each framework still needs its own job kind, so PyTorch work uses PyTorchJob
- runtimeRef.kind defaults to TrainingRuntime, the namespaced kind
- A TrainJob can reference a TrainingRuntime from any namespace
- The runtime is just the container image the job runs
- Data scientists should edit the shared ClusterTrainingRuntime for each run
Assumes Kubeflow Trainer v2.3.0 (TrainJob API trainer.kubeflow.org/v1alpha1) and the Kubeflow SDK 0.5.0; the Community Distribution 26.03.1 still bundles Trainer 2.2.0.