Understanding Training Jobs
This lesson explains the parts of a command job, how jobs are submitted and monitored, how scripts should handle inputs and outputs, and which job types exist.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the parts of a command job.
- Explain common job states such as Queued.
- Describe how a script reads inputs and writes outputs.
- Identify why files outside outputs are lost.
The parts of a command job
A command job runs a command in a container on compute. Its definition names everything the run needs, so it can be repeated exactly.
| Part | Example | Purpose |
|---|---|---|
code | ./src | Local folder uploaded as a snapshot with the job |
command | python train.py --data ${{inputs.data}} | What runs, with placeholders for inputs and outputs |
environment | azureml:train-env:2 | The software the command runs with |
compute | azureml:cpu-cluster, or omitted for serverless | Where it runs |
inputs | Data assets, URIs, numbers or strings | What the command reads |
outputs | uri_folder or mlflow_model | What the command produces |
experiment_name | churn | Groups related runs |
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
experiment_name: churn
display_name: train-gbm
code: ./src
command: >-
python train.py --data ${{inputs.data}} --n-estimators ${{inputs.n_estimators}}
--model-output ${{outputs.model}}
environment: azureml:train-env:2
compute: azureml:cpu-cluster
inputs:
data:
type: uri_file
path: azureml:churn-data:3
n_estimators: 200
outputs:
model:
type: mlflow_modelSubmitting and monitoring
A job is submitted with az ml job create --file job.yml or ml_client.jobs.create_or_update(job). The command returns the job's name and a studio link; --stream, or az ml job stream --name, shows the logs as the job runs, and az ml job show reports its status: NotStarted, Queued, Preparing, Running, Finalizing, then Completed, Failed or Canceled.
Each job keeps its logs, metrics, code snapshot, environment and outputs in the workspace. az ml job download retrieves outputs and logs. A job in Queued waits for compute, often because a cluster is scaling up or quota is exhausted; a job in Preparing is building or pulling its environment image, which can take several minutes for a new custom environment.
Writing scripts for jobs
A script that runs well as a job takes everything it needs as arguments, reads inputs from the paths it receives, writes results to its output paths, and logs metrics with MLflow. It does not hard-code storage paths, credentials or workspace names, so the same script runs locally, in a job and in a pipeline step.
import argparse, mlflow, pandas as pd
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
parser = argparse.ArgumentParser()
parser.add_argument("--data"); parser.add_argument("--n-estimators", type=int, default=100)
parser.add_argument("--model-output")
args = parser.parse_args()
df = pd.read_csv(args.data)
X, y = df.drop(columns="churned"), df["churned"]
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42)
mlflow.autolog(log_models=False)
model = GradientBoostingClassifier(n_estimators=args.n_estimators).fit(X_tr, y_tr)
mlflow.log_metric("test_auc", roc_auc_score(y_te, model.predict_proba(X_te)[:, 1]))
mlflow.sklearn.save_model(model, args.model_output)Writing the model to the output path makes it part of the job's outputs, from which it can be registered. Files written anywhere else on the node disappear when the node is released.
Job types
The command job is the foundation of several other job types, which add behaviour around it. The following list shows how they relate, and later topics explain each in depth.
- Sweep job: runs a command job many times over a hyperparameter search space.
- Automated ML job: generates and runs many training trials automatically from a task definition.
- Pipeline job: runs several steps, each a component or command, as a graph.
- Distributed command job: a command job with a
distributionsetting and several instances.
A model that vanishes
A training job completes successfully and logs good metrics, but when the data scientist tries to register the model from the job, there is no model file in the job's outputs. The script saves the model with joblib.dump(model, "/tmp/model.pkl").
The script wrote the model to a temporary path on the compute node, which is not uploaded and is lost when the node is released. The job should declare an output, such as model of type mlflow_model or uri_folder, pass its path to the script with ${{outputs.model}}, and save the model there, preferably with mlflow.sklearn.save_model so that it is an MLflow model.
Rerunning the job would fail the same way. Logging the file with mlflow.log_artifact would also preserve it, but declaring an output is the standard way to pass a model to registration and later pipeline steps. Using a larger compute disk would not change what is uploaded.
A job stays in the Queued state for a long time. What are the usual causes?
Show answer
The job is waiting for compute: the cluster is scaling up from zero, it has reached its maximum nodes, or the subscription lacks quota for the VM family.
Exam Tips
Remember for the exam
- A command job names code, command, environment, compute, inputs and outputs.
- Use --stream to follow logs; Queued usually means waiting for compute or quota.
- Scripts take arguments, read input paths and write to output paths.
- Files outside declared outputs are lost when the node is released.