skip to content

In DVC, after `featurize.ngram_max` changes in `params.yaml`, why does `dvc repro` re-run featurize, train and evaluate but skip prepare?

level: middleimportance: should knowfreq 13%

answer

  1. granular, per-key dependency
  2. params lists keys, not whole files
  3. lock stores each key's value
  4. new output hash is a changed dep
  5. upstream hashes still match

basics

~20 s

DVC tracks params per key, so only featurize, which lists featurize.ngram_max, sees a value that differs from dvc.lock. Its new features change train's dependency hash, the new model changes evaluate's, and prepare's recorded hashes still match.

solid answer

~40 s

For each stage `dvc repro` checks four things against `dvc.lock`: the command text, the hash of every file in `deps`, the value of every key listed under `params`, and whether the recorded outputs still exist and are in the cache. Params are granular: unless a stage tracks the whole file, it is invalidated only when a key it lists changes, not when anything else in `params.yaml` changes, so prepare is skipped. Featurize runs and writes new `data/features`; that directory's new hash is now a modified dependency for train, and train's new model is a modified dependency for evaluate. The cascade travels through output hashes, not graph position: if featurize had produced byte-identical features, train would be skipped. `dvc status` shows `params.yaml: modified: featurize.ngram_max` under featurize before you run anything.

go deeper

for a junior

Recall that a stage lists the param keys it uses, and that dvc repro re-runs only stages whose listed keys, dependencies or command changed.

for a middle

Walk the cascade: a changed key invalidates one stage, and its new output hash is a modified dependency for the next. Name the four checks against dvc.lock.

for a senior

Explain when the cascade stops: byte-identical outputs, run-cache restores, undeclared keys. Know when to use force-downstream and how dvc status explains a rerun.

for a principal

Decide how granular params should be across a shared params.yaml so expensive stages are not rebuilt for keys that do not affect them, without leaving keys undeclared.

## What DVC compares for each stage When `dvc repro` (or `dvc exp run`, which reproduces the same way) reaches a stage, it compares the current state with that stage's entry in **`dvc.lock`**, the file DVC rewrote the last time the stage ran. A stage is **stale**, or "changed", when any of these differ: | Check | What DVC compares | How `dvc status` reports it | |---|---|---| | Command | The `cmd` text in `dvc.yaml` against the lock | `changed command` | | Dependencies | The content hash of each path in `deps` | `changed deps` with `modified`, `deleted` or `new` | | Parameters | The value of each key listed under `params` | under `changed deps`, the params file name and `modified: <key>` | | Outputs | Whether each recorded output exists, matches and is in the cache | `changed outs` with `deleted`, `modified` or `not in cache` | A stage with `always_changed: true`, or with neither dependencies nor outputs, is always considered changed. A `frozen` stage is never executed. ## Why params are granular A **parameter dependency** is a single key in a structured file such as `params.yaml`, written in `dvc.yaml` as a dotted path (`featurize.ngram_max`). DVC stores the key's **value**, not the file's hash, in the lock. Several stages can share one `params.yaml`, and each is invalidated only by the keys it lists. That is the point: editing `train.lr` must not rebuild the 40-language feature matrix. Two less common forms exist. A stage can list keys from another file (`- other.yaml: [epochs]`), or track a whole file with an empty entry (`- config.json:`), in which case any key in it counts. ## Walking the language-identification pipeline Take four stages: `prepare` (lists `prepare.split`), `featurize` (lists `featurize.ngram_min` and `featurize.ngram_max`), `train` (lists `train.lr`) and `evaluate`. A teammate raises `featurize.ngram_max` from 4 to 5 and runs `dvc repro`: 1. **prepare**: its command, its dependency hashes and `prepare.split` all match the lock. DVC prints `Stage 'prepare' didn't change, skipping`. 2. **featurize**: `featurize.ngram_max` is 5 in the file and 4 in the lock, so the stage is changed. DVC deletes `data/features`, runs the command, caches the new directory and updates its lock entry. 3. **train**: none of its own params changed, but `data/features` now hashes differently from the value in train's lock entry. That is a modified dependency, so train runs. 4. **evaluate**: `models/langid.pkl` has a new hash, so evaluate runs and rewrites the metrics. ## When the cascade stops - **Same bytes, no rerun.** DVC follows content, not graph position. If a change upstream produces byte-identical output, the downstream stages see no difference and are skipped. `--force-downstream` overrides this when the cache cannot be trusted, such as when one shared dependency is declared only on the first stage or outputs are non-deterministic. - **Run cache.** If `ngram_max` goes back to 4 later, DVC finds a past run of featurize with exactly that command, dependencies and params, and prints `Stage 'featurize' is cached - skipping run, checking out outputs`. The old features return without recomputation; they differ from the hash in train's lock entry, so train is changed too, and it can likewise be restored if it once ran on exactly those inputs. - **Undeclared keys.** If `src/featurize.py` reads a key from `params.yaml` that the stage does not list, changing it invalidates nothing. Granularity applies only to what you declare. ## Reading the plan before running it - `dvc status` lists each stale stage and why, without running anything. - `dvc repro --dry` prints the commands that would run. - `dvc dag` shows the graph, so you can predict the cascade from a stage. - `dvc params diff` shows which values differ from the last commit. ## Mistakes interviewers listen for - **"Any edit to params.yaml rebuilds everything."** Only stages listing the changed key, or tracking the whole file, are invalidated directly; the rest follow only if an output they depend on actually changes. - **"Downstream always reruns."** The cascade travels through hashes. A stage that reruns and writes identical bytes stops it. - **"The lock stores the params file's hash."** It stores each tracked key's value, which is why one shared `params.yaml` can serve every stage. - **"Reverting a value forces recomputation."** The run cache can restore a past result instantly, which is right as long as the stage is deterministic and every input it reads is declared.

  • The featurize script also reads `featurize.lowercase` from `params.yaml`, but the stage does not list it. What happens when it changes?
    Nothing: DVC only compares the keys a stage lists, so featurize is skipped and the features no longer match the file. Add the key under the stage's `params`, or track the whole file with an empty entry such as `- params.yaml:` if the stage really depends on all of it.
  • How do you make train run again even though `data/features` and `train.lr` are unchanged?
    `dvc repro -f -s train` forces that single stage, skipping the run cache as well. Without `-s`, `-f train` also forces every stage upstream of train. If the goal is to rerun everything after a changed stage, `--force-downstream` does that.

  • Changing any key in params.yaml re-runs every stage that uses the file
  • DVC hashes the whole params.yaml file as a single dependency
  • A downstream stage always re-runs once anything upstream re-ran
  • Setting a param back to an old value always forces a full recomputation
  • Stages without params can never be invalidated by a params change