Sub topic 7
Reproducible pipelines with dvc.yaml
This chapter assumes that you know basic Git commands, can use a command line, and understand simple Python scripts and YAML. You should also know that a pipeline is a sequence of steps where one step produces an input for the next step.
A DVC pipeline records those steps in dvc.yaml. Each stage declares its command, input files, parameters, and outputs. DVC uses this information to decide whether a stage must run again.
Defining pipeline stages
Suppose a project prepares raw data and then trains a model. Create a dvc.yaml file like this:
stages:
prepare:
cmd: python src/prepare.py data/raw.csv data/prepared
deps:
- src/prepare.py
- data/raw.csv
outs:
- data/prepared
train:
cmd: python src/train.py data/prepared models/model.pkl
deps:
- src/train.py
- data/prepared
params:
- train.n_estimators
- train.max_depth
outs:
- models/model.pkl
metrics:
- metrics.json:
cache: falseA stage is one named pipeline step. The cmd field is the command DVC runs. A dependency, listed under deps, is an input that can make the stage stale. An output, listed under outs, is a file or directory produced by the command.
The params field tells DVC which settings affect the stage. In this example, DVC reads these keys from params.yaml:
train:
n_estimators: 100
max_depth: 5The metrics field identifies a metrics file. Setting cache: false keeps the file in the workspace while Git versions the file contents.
The stages form a directed acyclic graph, or DAG. A DAG is a dependency graph with no circular path. DVC follows this graph when it runs the pipeline.
Reproducing the pipeline
Run every required stage with:
dvc reproDVC compares the current dependencies, parameters, and outputs with the information in dvc.lock. It runs a stage when an input changed, a parameter changed, an output is missing, or the stage has not run before. If nothing changed, DVC can skip the stage.
After a successful run, commit the pipeline description and its lock file:
git add dvc.yaml dvc.lock params.yaml metrics.json
git commit -m "Train baseline model"dvc.yaml describes the intended pipeline. dvc.lock records the exact hashes of dependencies and outputs used by a particular run. Together, these files allow another checkout to reproduce the same result, provided the required data and code are available.
| Change | Typical result from `dvc repro` |
|---|---|
| Edit `src/prepare.py` | Rerun `prepare`, then rerun dependent stages |
| Change `train.max_depth` | Rerun `train` |
| Change only an unrelated file | Skip both stages |
| Delete `models/model.pkl` | Rerun `train` |
Common errors
A frequent mistake is leaving an input out of deps. If the training script reads a configuration file but that file is not declared, DVC cannot know that the stage needs to run again.
Another mistake is declaring an output that the command does not create. Make sure the script writes exactly the path listed under outs. A directory output must also exist after the command finishes.
Do not edit dvc.lock by hand. Change the source code, data, or parameters, then run dvc repro so DVC can update the lock file.
Use dvc dag to inspect the pipeline graph:
dvc dagThis is useful when a stage runs unexpectedly or when you need to confirm the order of dependencies.
Exercise
You change train.max_depth in params.yaml, but dvc repro does not rerun the training stage. What should you check?
Show answer
Check that train.max_depth appears under the train stage's params list in dvc.yaml, and confirm that the changed params.yaml file is the one used by the project.