Aller au contenu

Leçon 12 · Pipelines

Advanced DVC Stage Options and External Data

Control DVC stages with cache, persist, push and remote output settings, wdir, frozen and always_changed, and depend on data that lives outside your project.

Most stages need only cmd, deps and outs. Some situations need more control: a report you want in Git rather than the cache, a stage that should not run for a while, or input that lives in a bucket. This lesson covers those options and when to avoid them.

Output settings

Each output can carry settings:

YAML
stages:
  featurize:
    cmd: python src/featurize.py
    deps:
      - data/clean.csv
    outs:
      - data/features/:
          desc: One file per month
      - reports/summary.md:
          cache: false
      - data/index.db:
          persist: true
      - data/debug_dump/:
          push: false
Setting Default Effect
cache true Store the output in the DVC cache
persist false Keep the output when the stage reruns
push true Include it in dvc push
remote default remote Use a specific remote for this output
desc none A note for people

cache: false

Use it for small text files you prefer to keep in Git, such as metrics or a short report. DVC still records information about it in the lock file but does not store the content, so you must commit it with Git. Setting it on any output disables the run cache for that stage.

persist: true

Normally DVC deletes outputs before running the stage, so each run starts clean. persist: true keeps the old output, which helps with incremental work such as updating a search index. The price is that the result now depends on what was there before. That weakens reproducibility, so prefer clean builds when you can.

push: false and remote

push: false keeps an output out of dvc push, for example a temporary debug file or data that must not leave a machine. A remote setting sends selected outputs to a separate location, which helps when sensitive data needs tighter access.

Stage settings

  • wdir sets the folder the command runs in. Paths in deps and outs are relative to it. It helps when a script expects to run from its own folder.
  • desc and meta hold notes. DVC ignores meta, and desc does not affect operations.
  • frozen: true stops dvc repro from running the stage. dvc freeze <stage> and dvc unfreeze <stage> set and clear it. Use it to hold an expensive or unavailable step still, and remember to unfreeze it.
  • always_changed: true makes DVC treat the stage as changed every time. It fits steps that read something DVC cannot watch, such as the current content of a web service. Overusing it defeats the purpose of skipping.

A stage with no dependencies and no outputs is also always considered changed.

External dependencies

A dependency can live outside the project: in S3, Google Cloud Storage, Azure, over SSH, HDFS or HTTP, or at an absolute local path.

YAML
stages:
  prepare:
    cmd: python src/download_and_prepare.py
    deps:
      - s3://partner-bucket/exports/latest.csv
      - src/download_and_prepare.py
    outs:
      - data/clean.csv

DVC notices when the external object changes and reruns the stage. For private storage, define a remote and use the remote://name/path form so credentials stay in your DVC configuration.

External outputs are possible too: DVC detects changes, but it does not store them in the cache for versioning. Saving external outputs to an external cache was deprecated as of DVC 3.0. If you need history for external data, look at dvc import-url or the cloud versioning features.

Try the idea

This stand-in shows why persist: true can make a result depend on history. Each run "appends" batches to an index.

Python Stand-in: persist true versus a clean build

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.

Idempotent means running the step twice gives the same result as running it once. If you use persist, make the script idempotent.

Which option for which need?

Pick the smallest tool

Small report cache false, commit in Git
Incremental build persist true, idempotent script
1 / 2

Common mistake: leaving a stage frozen and forgetting about it. The pipeline then looks fine while that stage's output is stale. Review frozen entries in pull requests.

Editing stages later

You can edit dvc.yaml directly, or use dvc stage add -f to rewrite one stage. To delete a stage, remove it from the file or use dvc remove <stage>; check the command reference for the option that also deletes outputs. After editing, run dvc repro --dry to see what DVC plans to do, and commit dvc.yaml and dvc.lock together.

Reviewing option changes

Because these settings change how results are produced, treat edits to them like code changes. In a pull request, ask three questions. Does a new cache: false file get committed to Git, and is it small enough? Does persist make the output depend on history, and does the script cope with that? Is a new always_changed stage really watching something DVC cannot see? A short comment in desc explaining why an option is there will save a teammate from removing it by mistake.

Start simple

When a pipeline misbehaves, remove special options first and see whether the problem remains. A clean build with plain dependencies and outputs is easier to reason about. Add an option back only when you can name the need it serves.

Also test the change from a clean state. Delete the affected outputs in a scratch clone, run the pipeline again, and compare the result with what you had before. If the two differ, an option is probably hiding a dependency on old files or on something outside the project that you have not declared.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. You set cache: false on a metrics file. Who keeps its history?

    DVC does not cache it, so commit small files like this with Git.
  2. 2. What is the main risk of persist: true?

    Keeping old output can make results depend on history. Make the script idempotent.
  3. 3. What does dvc freeze do?

    A frozen stage is skipped until you unfreeze it.
  4. 4. When might always_changed be justified?

    It forces reruns for inputs DVC cannot detect. Use it sparingly.

Summary

  • Output settings control caching, persistence, pushing and the target remote.
  • cache: false files belong in Git and disable the run cache for the stage.
  • frozen holds a stage; always_changed forces it.
  • External dependencies trigger reruns; external outputs are not cached.
  • Prefer clean, reproducible builds over persisted state.

Next: Parameters and params.yaml

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.