Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Versioning Code, Data and Models  ›  Capstone Project

DVC Pipelines and Data Lineage

Data Lineage 15 minA DVC pipeline declares each stage's command, inputs and outputs, so DVC can rerun only what changed and record exactly which data and code produced every model.

DVC Pipelines and Data Lineage

Training the churn model takes two steps: turn the raw CSV into features, then fit the model. Run them by hand and you will one day forget to rerun the first after changing it — and train on stale features. A DVC pipeline writes the steps down with their inputs and outputs, so the tool decides what needs to run.

The rule

Declare the inputs; rerun only what they touched.

A pipeline lives in dvc.yaml. Each stage has a command, its deps (inputs), its params (settings read from params.yaml) and its outs (outputs):

stages:
  prepare:
    cmd: python prepare.py
    deps:
      - data/subscriptions.csv
      - prepare.py
    outs:
      - data/features.parquet
  train:
    cmd: python train.py
    deps:
      - data/features.parquet
      - train.py
    params:
      - max_depth
    outs:
      - models/model.pkl
    metrics:
      - metrics.json:
          cache: false

Because train depends on prepare's output, DVC knows the order. Running dvc repro does three things:

  1. Hashes every stage's deps and reads its params.
  2. Compares them with what was saved in dvc.lock from the last run.
  3. Reruns only stages whose inputs changed. A rerun that produces a new output changes the inputs of the stages downstream, so they rerun too. Then it updates dvc.lock.

dvc.lock is committed to Git. That makes it a lineage record: for any commit, it lists the exact hashes of the data, code and model involved, plus the parameter values. Asked "what made this model?", you read the lock file of the commit that produced it.

The shortcut

To predict what dvc repro will do, find the changed input, then walk downstream. Everything upstream of the change is skipped.

Changing max_depth from 6 to 8Step 1 of 4
  1. Find the change

    Only params.yaml changed, and only max_depth.

A trap: a script that reads a file you did not list in deps. DVC cannot see that file change, so it will wrongly skip the stage. Declare every input.

Drills

Answer fast — trust the rule.

quick drill

You change how prepare.py computes a feature, so features.parquet comes out different. Which stages does dvc repro run?

quick drill

What does dvc.lock record?

quick drill

train.py also reads config/thresholds.json, which is not listed in deps. You change that file. What does dvc repro do?

quick drill

Six months later, how do you find which data produced the model in commit a1b2c3?

Remember

  • Declare the inputs; rerun only what they touched.
  • Predict dvc repro by finding the changed input and walking downstream.
  • Trap: undeclared inputs are invisible to DVC, so stages get skipped wrongly.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPredict what reruns

The churn pipeline has stages prepare (deps: raw CSV, prepare.py) and train (deps: features, train.py, params: max_depth). You change only max_depth in params.yaml and run dvc repro. Explain what reruns and how this gives you data lineage.