Saltar al contenido

Lección 10 · Pipelines

Reproduce Pipelines with dvc repro and dvc.lock

Learn how dvc repro decides which stages to run, what dvc.lock records, how the run cache restores old results, and which flags control a pipeline run.

Once a pipeline is described, one command runs it: dvc repro. It compares the current state with the recorded state and runs only the stages that need it. That saves time and removes the guesswork of "did I rerun the second script?"

Run the pipeline

Terminal
# Read-only: reproduce, then record the result
dvc repro
git add dvc.yaml dvc.lock .gitignore
git commit -m "Train first model"
dvc push

On the first run, every stage executes. If you run dvc repro again without changes, DVC reports that everything is up to date and runs nothing. DVC also adds the cached outputs to .gitignore files, so add any of those that changed.

What dvc.lock records

dvc.lock is the pipeline's snapshot. For each stage it stores the command, the hashes and sizes of dependencies and outputs, and the parameter values used. An illustrative fragment:

YAML
schema: '2.0'
stages:
  prepare:
    cmd: python src/prepare.py
    deps:
    - path: data/raw.csv
      hash: md5
      md5: 22a1a2931c8370d3aeedd7183606fd7f
      size: 14445097
    outs:
    - path: data/clean.csv
      hash: md5
      md5: 6f2a9c0b1d3e4f5a6b7c8d9e0f1a2b3c
      size: 11012233

Treat dvc.yaml as your recipe and dvc.lock as the receipt. Commit both. Do not edit dvc.lock by hand.

How DVC decides what to run

A stage runs when something it declares differs from the lock file: the command, a dependency, a parameter value, or a missing or changed output. Then downstream stages run if the files they read changed.

A smart detail: if prepare reruns but writes identical output, train sees the same hash and is skipped. Content, not timestamps, decides.

Preview before running

Terminal
# Read-only: look first
dvc status          # what changed?
dvc repro --dry     # which commands would run?

dvc status lists changed dependencies and outputs per stage.

The run cache

Suppose you change a parameter, run, then change it back. You do not want to train again. DVC keeps a run cache: records of past stage runs, keyed by their dependencies and commands. When the same inputs appear again, DVC can restore the saved outputs instead of executing the command. This assumes your command is deterministic: the same inputs give the same outputs. If a stage depends on the current time, random numbers or a web request, a restored result may not match a new run.

  • --no-run-cache forces real execution when inputs match a past run.
  • -f (--force) reruns stages even when nothing changed.
  • If any output of a stage uses cache: false, the run cache is not used for that stage.
  • dvc push --run-cache can share it with teammates; it is not pushed by default.

Useful flags

Command Effect
dvc repro train Run train and whatever it needs upstream
dvc repro -s train Run only train, without checking upstream
dvc repro --downstream train Run train and stages after it
dvc repro -f Rerun even if unchanged
dvc repro --pull Download missing data as needed
dvc repro --allow-missing Skip stages whose only change is missing data
dvc repro -k Keep going with independent stages after a failure
dvc repro --glob "train@*" Select stages by pattern
dvc repro --no-commit Do not cache outputs yet

By default dvc repro does not download data. Also remember that DVC removes a stage's outputs before running it, unless they are marked persist.

Try the idea

This stand-in decides what to run by comparing fingerprints and includes a tiny run cache. It is a simplification: real DVC also checks the output files and more.

Python Stand-in: skip, run or restore a stage

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.

Reproducibility has limits

A passing dvc repro means the recorded inputs produced the recorded outputs on some machine at some time. It does not promise identical results elsewhere if library versions, hardware or randomness differ. Pin dependencies, set seeds and record environment details. For more on this, see MLOps.

Common mistake: committing dvc.yaml without dvc.lock, or committing the lock without pushing data. Teammates then get a recipe without a matching receipt, or a receipt that points to data they cannot get.

Reading a run

When you run dvc repro, read its messages stage by stage. You will see which stages were skipped, which ran and which were restored from the run cache. If a stage that you expected to run was skipped, compare its declared inputs with what you changed. If a stage you expected to skip ran, find the input that differs with dvc status.

A useful routine for a pull request is:

  1. Run dvc status and note what DVC thinks changed.
  2. Run dvc repro --dry and check that the list of commands matches your expectation.
  3. Run dvc repro.
  4. Look at git diff dvc.lock to see which hashes changed. A surprising change in an output hash often means nondeterministic code, such as writing the current time into a file.
  5. Commit and push.

Step 4 is a cheap and powerful review habit, because unexpected changes show up in a plain text diff.

Failures

If a command fails, DVC stops that branch of the pipeline and does not record the failed stage as complete. Fix the cause and run dvc repro again. Stages that already succeeded and whose inputs did not change are not repeated.

Targets and teamwork

You do not have to run everything. While developing the evaluation script, run just that stage and what it needs. Before you commit, run the full pipeline once, so the lock file describes a consistent state.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is dvc.lock for?

    The lock file is a snapshot that DVC compares against. Commit it with dvc.yaml.
  2. 2. prepare reruns but produces identical output. What happens to train?

    DVC compares content hashes, so an unchanged file does not trigger downstream work.
  3. 3. Which assumption does the run cache make?

    Restoring old outputs is only correct if the same inputs would give the same outputs.
  4. 4. Which flag shows what would run without running it?

    A dry run prints the commands only.

Summary

  • dvc repro runs only stages whose declared inputs or outputs differ from dvc.lock.
  • Commit dvc.yaml and dvc.lock, then push the data.
  • The run cache restores previous results and assumes deterministic commands.
  • --dry, -f, --downstream and --pull control runs.
  • Reproducibility also depends on environments and randomness.

Next: Templating, foreach and matrix Stages

Tu progreso se guarda en este navegador. No necesitas una cuenta.