MLOps Studio Trainer overview
MLOps Studio Trainer provides APIs for defining training jobs and reusable training runtimes.
From a Jupyter Notebook in MLOps Studio, you can create, monitor, and remove these resources.
TrainJob
TrainJob is a simplified way to run training jobs in MLOps Studio Training Operator v2.
It replaces traditional PyTorch Job, TFJob, and XGBoostJob with one universal API.
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: training
spec:
# Runtime reference (contains configuration)
runtimeRef:
name: pytorch-cpu-standard
# Training code only!
trainer:
image: my-registry.com/my-training-code:v1
command: ["python", "train.py"]
The manifest above is a trainer.kubeflow.org/v1alpha1 TrainJob.
For a complete notebook-based validation workflow using a kubeflow.org/v1 PyTorchJob, see Scenario - launching Trainer.
Training Runtimes:
In MLOps Studio, there are several mechanisms for creating custom runtimes. Each has its own advantages and specific use cases:
Mechanism |
Scope |
Use Case |
|---|---|---|
PodDefault |
Namespace Scope |
Jupyter + Training |
ClusterTrainingRuntime |
Cluster Scope |
Training Jobs |
TrainingRuntime |
Namespace Scope |
Training Jobs |
PodTemplateOverrides |
Per-Job |
Single job |
1. PodDefault
PodDefault is a MLOps Studio mechanism, not limited to Training Operator, that allows for the automatic injection of configuration into pods.
apiVersion: kubeflow.org/v1alpha1
kind: PodDefault
metadata:
name: pytorch-cpu-small
namespace: kubeflow-user-example-com
spec:
desc: "PyTorch CPU Small (0.5 CPU, 1Gi RAM)"
selector:
matchLabels:
runtime: pytorch-cpu-small
env:
- name: OMP_NUM_THREADS
value: "1"
- name: MKL_NUM_THREADS
value: "1"
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
2. ClusterTrainingRuntime
ClusterTrainingRuntime is a cluster-scoped API that allows for the management of templates for TrainJobs. ClusterTrainingRuntime can be deployed across the entire Kubernetes cluster and reused in TrainJobs. It simplifies the process of running training jobs by providing standardized designs and ready-to-use environments.
apiVersion: trainer.kubeflow.org/v1alpha1
kind: ClusterTrainingRuntime
metadata:
name: torch-distributed
labels:
trainer.kubeflow.org/framework: torch
spec:
mlPolicy:
numNodes: 1
torch:
numProcPerNode: auto
template:
spec:
replicatedJobs:
- name: node
template:
metadata:
labels:
trainer.kubeflow.org/trainjob-ancestor-step: trainer
spec:
template:
spec:
containers:
- name: node
image: pytorch/pytorch:2.7.1-cuda12.8-cudnn9-runtime
3. TrainingRuntime
TrainingRuntime is a namespace-scoped API that enables the management of templates for TrainJobs within individual namespaces. It is an ideal solution for teams or projects that require their own customized training configurations for each namespace, providing the flexibility of decentralized control.
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainingRuntime
metadata:
name: pytorch-team-runtime
namespace: team-a
labels:
trainer.kubeflow.org/framework: torch
spec:
mlPolicy:
numNodes: 1
torch:
numProcPerNode: auto
template:
spec:
replicatedJobs:
- name: node
template:
metadata:
labels:
trainer.kubeflow.org/trainjob-ancestor-step: trainer
spec:
template:
spec:
containers:
- name: node
image: pytorch/pytorch:2.7.1-cuda12.8-cudnn9-runtime
4. PodTemplateOverrides
The PodTemplateOverrides API allows for the customization of pod templates for specific tasks within a TrainJob without modifying the TrainingRuntime.
This is useful when it is necessary to apply task-specific configurations, such as custom service accounts, node selectors, tolerations, or additional volumes.
Custom mutating admission webhooks can also be utilized to configure TrainJob overrides using this API.
podTemplateOverrides:
- targetJobs:
- name: node
spec:
serviceAccountName: custom-sa
nodeSelector:
accelerator: nvidia-tesla-v100
Custom PyTorch CPU runtime scenario
The complete notebook procedure creates a PodDefault, submits a PyTorchJob, verifies the injected environment, and removes both resources.
Scheduling
Job Scheduling in MLOps Studio Trainer is a mechanism for managing the scheduling and queuing of training jobs, which is particularly important for distributed training.
MLOps Studio Trainer supports 3 main scheduling mechanisms:
Coscheduling (Kubernetes SchedulerPlugins)
Lightweight - a plugin for the default K8s scheduler.
Gang scheduling - all-or-nothing.
Simple to configure.
Use case: Basic gang scheduling for distributed training
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: distributed-training
spec:
runtimeRef:
name: pytorch-distributed
# Gang scheduling configuration
scheduler:
gang:
enabled: true
minMembers: 4 # Minimum members to start
trainer:
command: ["python", "train.py"]
2. Volcano
Advanced - dedicated batch scheduler.
Gang scheduling + more.
Fairness policies - equitable resource distribution.
Priority queues - priority-based queuing.
Preemption - interrupting lower-priority tasks.
Use case: Large ML clusters, multi-tenant environments, advanced policies.
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: distributed-training
spec:
runtimeRef:
name: pytorch-distributed
# Volcano scheduler
scheduler:
volcano:
enabled: true
minAvailable: 4
queue: ml-training-queue
priorityClassName: high-priority
trainer:
command: ["python", "train.py"]
3. Kueue
Job queueing – task queuing
Resource management – quota management
Multi-tenant – isolation between teams
Fairness – equitable access to resources
Integration – integration with multiple job controllers
Use case: Job queue management, resource limits per team
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: distributed-training
labels:
kueue.x-k8s.io/queue-name: team-a-queue
spec:
runtimeRef:
name: pytorch-distributed
# Kueue automatically handles queueing
trainer:
command: ["python", "train.py"]
Distributed PyTorchJob scenario
For a complete CPU-based distributed training workflow using the Gloo backend, see Gloo on CPU.
Running the Trainer via kubectl
1. Basic TrainJob
TrainJob Structure:
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: mnist-training
namespace: kubeflow-user-example-com
spec:
# Runtime reference (contains configuration)
runtimeRef:
name: pytorch-cpu-standard
# Training code only!
trainer:
image: my-registry.com/my-training-code:v1
command: ["python", "train.py"]
Creating a TrainJob
# Create trainjob.yaml file
kubectl apply -f trainjob.yaml
# Check status
kubectl get trainjobs -n kubeflow-user-example-com
# Details
kubectl describe trainjob mnist-training -n kubeflow-user-example-com
2. Training Runtimes
2.1 ClusterTrainingRuntime (Cluster Scope)
ClusterTrainingRuntime is a cluster-scoped runtime – available to all namespaces
apiVersion: trainer.kubeflow.org/v1alpha1
kind: ClusterTrainingRuntime
metadata:
name: torch-distributed
labels:
trainer.kubeflow.org/framework: torch
spec:
mlPolicy:
numNodes: 1
torch:
numProcPerNode: auto
template:
spec:
replicatedJobs:
- name: node
template:
metadata:
labels:
trainer.kubeflow.org/trainjob-ancestor-step: trainer
spec:
template:
spec:
containers:
- name: node
image: pytorch/pytorch:2.7.1-cuda12.8-cudnn9-runtime
resources:
requests:
cpu: "1"
memory: "2Gi"
Management:
# Create ClusterTrainingRuntime
kubectl apply -f cluster-runtime.yaml
# List all ClusterTrainingRuntime
kubectl get clustertrainingruntimes
# Delete
kubectl delete clustertrainingruntime torch-distributed
2.2 TrainingRuntime (Namespace Scope)
TrainingRuntime is a namespace-specific runtime.
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainingRuntime
metadata:
name: pytorch-team-runtime
namespace: team-a
labels:
trainer.kubeflow.org/framework: torch
spec:
mlPolicy:
numNodes: 1
torch:
numProcPerNode: auto
template:
spec:
replicatedJobs:
- name: node
template:
spec:
template:
spec:
containers:
- name: node
image: pytorch/pytorch:latest
env:
- name: OMP_NUM_THREADS
value: "1"
resources:
requests:
cpu: "0.5"
memory: "1Gi"
Management
# Create TrainingRuntime in namespace
kubectl apply -f training-runtime.yaml -n team-a
# List runtimes in namespace
kubectl get trainingruntimes -n team-a
# Delete
kubectl delete trainingruntime pytorch-team-runtime -n team-a
2.3 PodDefault (Legacy)
PodDefault
apiVersion: kubeflow.org/v1alpha1
kind: PodDefault
metadata:
name: pytorch-cpu-small
namespace: kubeflow-user-example-com
spec:
desc: "PyTorch CPU Small (0.5 CPU, 1Gi RAM)"
selector:
matchLabels:
runtime: pytorch-cpu-small
env:
- name: OMP_NUM_THREADS
value: "1"
- name: MKL_NUM_THREADS
value: "1"
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
Management:
# Create PodDefault
kubectl apply -f poddefault.yaml
# List
kubectl get poddefaults -n kubeflow-user-example-com
# Delete
kubectl delete poddefault pytorch-cpu-small -n kubeflow-user-example-com
3. Monitoring and Logs
Checking a TrainJob status
# Status of all TrainJobs
kubectl get trainjobs -n kubeflow-user-example-com
# Details of a specific TrainJob
kubectl describe trainjob mnist-training -n kubeflow-user-example-com
# Check pods created by TrainJob
kubectl get pods -n kubeflow-user-example-com \
-l training.kubeflow.org/job-name=mnist-training
# Pod status
kubectl get pods -n kubeflow-user-example-com \
-l training.kubeflow.org/job-name=mnist-training \
-o wide
Fetching logs
# Logs from a specific pod
kubectl logs -n kubeflow-user-example-com \
mnist-training-node-0
# Last 50 lines
kubectl logs -n kubeflow-user-example-com \
mnist-training-node-0 --tail=50
# Follow (stream)
kubectl logs -n kubeflow-user-example-com \
mnist-training-node-0 -f
# Logs from all TrainJob pods
kubectl logs -n kubeflow-user-example-com \
-l training.kubeflow.org/job-name=mnist-training \
--all-containers
Events
kubectl get events -n kubeflow-user-example-com \
--field-selector involvedObject.name=mnist-training
# Events for pod
kubectl describe pod mnist-training-node-0 \
-n kubeflow-user-example-com | grep -A 20 "Events:"
4. Complete PyTorchJob workflow
The complete kubectl-based validation procedure uses a kubeflow.org/v1 PyTorchJob and covers submission, monitoring, logs, expected results, and cleanup.
5. Distributed Training from TrainJob
Multi-Node Training
apiVersion: trainer.kubeflow.org/v1alpha1
kind: TrainJob
metadata:
name: distributed-training
namespace: kubeflow-user-example-com
spec:
runtimeRef:
name: torch-distributed
trainer:
image: pytorch/pytorch:latest
command: ["python", "train_distributed.py"]
numNodes: 2 # 2 nodes for distributed training
# Create distributed training job
kubectl apply -f distributed-trainjob.yaml
# Check all pods (there should be 2)
kubectl get pods -n kubeflow-user-example-com \
-l training.kubeflow.org/job-name=distributed-training
# Logs from each node
kubectl logs distributed-training-node-0 -n kubeflow-user-example-com
kubectl logs distributed-training-node-1 -n kubeflow-user-example-com
6. Useful commands
Debugging
# Check why pod is Pending
kubectl describe pod <pod-name> -n kubeflow-user-example-com
# Shell into running pod
kubectl exec -it <pod-name> -n kubeflow-user-example-com -- /bin/bash
# Check cluster resources
kubectl top nodes
kubectl top pods -n kubeflow-user-example-com
# List all Training CRDs
kubectl get trainjobs -A
kubectl get trainingruntimes -A
kubectl get clustertrainingruntimes
Batch operations
# Delete all TrainJobs in namespace
kubectl delete trainjobs --all -n kubeflow-user-example-com
# Delete all completed pods
kubectl delete pods --field-selector=status.phase==Succeeded \
-n kubeflow-user-example-com
# Export TrainJob to YAML
kubectl get trainjob pytorch-training -n kubeflow-user-example-com \
-o yaml > trainjob-backup.yaml