सामग्री पर जाएँ

लेसन 9 · Pipelines

Build Your First DVC Pipeline

Turn scripts into a DVC pipeline with dvc stage add and dvc.yaml: define commands, dependencies and outputs, link stages into a graph and view it with dvc dag.

Versioning data is only half of reproducibility. You also need to know how a result was made. A DVC pipeline is a list of stages. Each stage records a command, what it reads and what it writes. From that information DVC works out the order, and later which steps need to run again.

A three-step project

Suppose you have these scripts:

  1. src/prepare.py cleans raw reviews.
  2. src/train.py trains a model from the cleaned data.
  3. src/evaluate.py scores the model.

Add stages with the command line

Terminal
# Read-only: describe the stages
dvc stage add -n prepare \
  -d src/prepare.py -d data/raw.csv \
  -o data/clean.csv \
  python src/prepare.py

dvc stage add -n train \
  -d src/train.py -d data/clean.csv \
  -o models/model.pkl \
  python src/train.py

dvc stage add -n evaluate \
  -d src/evaluate.py -d models/model.pkl -d data/clean.csv \
  -M reports/metrics.json \
  python src/evaluate.py

The flags mean:

Flag Meaning
-n Stage name (letters, numbers, dashes, underscores)
-d Dependency: a file or folder the stage reads
-o Output that DVC caches
-O Output that DVC does not cache
-m / -M Metrics file, cached / not cached
-f Overwrite an existing stage
--run Run right after adding

dvc stage add only writes the definition. It does not run anything unless you add --run.

The dvc.yaml file

The commands above create dvc.yaml. You can also write it by hand, and many people prefer that. For the first two stages it looks like this:

YAML
stages:
  prepare:
    cmd: python src/prepare.py
    deps:
      - src/prepare.py
      - data/raw.csv
    outs:
      - data/clean.csv
  train:
    cmd: python src/train.py
    deps:
      - src/train.py
      - data/clean.csv
    outs:
      - models/model.pkl
  evaluate:
    cmd: python src/evaluate.py
    deps:
      - src/evaluate.py
      - models/model.pkl
      - data/clean.csv
    outs:
      - reports/metrics.json:
          cache: false

The metrics lesson shows how to also mark that last file as a metrics file. A cmd can be one string or a list of commands that run in order; if one fails, the stage stops.

Dependencies create the graph

You never write "run prepare before train". DVC sees that train depends on data/clean.csv, which prepare outputs. So prepare comes first. The stages form a directed acyclic graph (DAG): arrows point forward and never form a loop.

flowchart LR
  raw["data/raw.csv"] --> prepare
  prepare --> train
  prepare --> evaluate
  train --> evaluate

Arrows follow files: one stage's output is another stage's dependency.

Terminal
# Read-only: show the graph and the stage list
dvc dag
dvc stage list

List dependencies honestly

DVC only knows what you tell it. If train.py imports src/features.py and you forget to list it, editing that file will not trigger a rerun. Include:

  • the script and any local modules it imports;
  • input data and configuration files;
  • parameters (a later chapter);
  • environment descriptions, such as a lock file of library versions, if changes should trigger reruns.

Each output can belong to only one stage, and outputs of different stages must not overlap. A file made by a pipeline stage should not also be tracked with dvc add.

Try the idea

This stand-in works out the run order from dependencies and outputs and rejects cycles. DVC does this for you; the code shows the principle.

Python Stand-in: order stages from their files

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Make scripts pipeline-friendly

  • Read inputs from the declared paths and write only declared outputs.
  • Create output folders inside the script if needed.
  • Exit with a non-zero code when something fails, so DVC stops.
  • Fix random seeds when you can.

DVC removes a stage's outputs before running it, so a script that appends to an old output file will not find it. The persist option, covered later, changes this.

Common mistake: a script that reads an undeclared file, such as a lookup table in your home folder. The pipeline works on your machine and fails, or silently differs, on another one.

Design stages well

Stage boundaries decide what DVC can skip. A single stage that cleans, trains and evaluates reruns all three whenever any line changes. Many tiny stages create clutter and extra files. A useful rule is to put a boundary where one of these is true:

  • the step is expensive, so you want to avoid repeating it;
  • its output is useful to several later steps;
  • it changes for different reasons than its neighbors;
  • you want to inspect its result on its own.

For this project, data preparation, training and evaluation each pass the test. Writing a plot from the metrics might not need its own stage until it grows.

Check the result of your design

After adding stages, run dvc dag and read the graph aloud: "prepare feeds train, and both feed evaluate". If an arrow is missing, a dependency is probably missing. If an arrow surprises you, a stage reads something you did not intend. A quick look now is cheaper than debugging stale results later. Finally, delete the outputs in a scratch copy and run the pipeline from nothing. If it succeeds, you have not relied on leftover files.

Moving from a script collection

You may already have a folder of scripts that you run by hand in a remembered order. Convert it gradually. First, write down the order and what each script reads and writes. Then add one stage at a time, running dvc repro after each addition. If a script reads files by an unexpected path, fix that before adding the next stage.

When all stages are in place, ask a colleague to run the pipeline from a fresh clone. Their problems reveal the hidden assumptions: a missing library, a local file or an environment variable. Document or declare each one.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does dvc stage add do by default?

    It records the stage in dvc.yaml. Add --run to execute it at once.
  2. 2. How does DVC know that prepare must run before train?

    The graph comes from matching outputs to dependencies.
  3. 3. What happens if train.py imports a module you did not list as a dependency?

    DVC only watches declared dependencies.
  4. 4. Which flag adds an output that DVC does not cache?

    Uppercase -O declares a no-cache output.

Summary

  • A stage records a command, its dependencies and its outputs in dvc.yaml.
  • Outputs feeding later dependencies form a DAG, shown by dvc dag.
  • dvc stage add defines stages; it does not run them without --run.
  • Declare every input, so reruns happen when they should.
  • Write scripts that use declared paths and fail loudly.

Next: Reproduce Pipelines with dvc repro

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।