Bird
Raised Fist0
MLOpsdevops~10 mins

Data pipelines with DVC in MLOps - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Data pipelines with DVC
Define stages in dvc.yaml
Run dvc repro to execute pipeline
DVC checks dependencies and outputs
Execute commands for each stage
Save outputs and update dvc.lock
Track data and pipeline with git and dvc
Repeat: modify data/code -> dvc repro -> track changes
This flow shows how DVC runs a data pipeline by defining stages, executing them, tracking outputs, and updating pipeline state.
Execution Sample
MLOps
stages:
  preprocess:
    cmd: python preprocess.py data/raw data/preprocessed
    deps:
      - data/raw
      - preprocess.py
    outs:
      - data/preprocessed
This dvc.yaml snippet defines a pipeline stage 'preprocess' that runs a Python script with input dependencies and output data.
Process Table
StepActionStageDependencies CheckedCommand ExecutedOutputs Saveddvc.lock Updated
1Start pipeline run-----
2Check 'preprocess' stage dependenciespreprocessdata/raw, preprocess.py---
3Run commandpreprocess-python preprocess.py data/raw data/preprocessed--
4Save output datapreprocess--data/preprocessed-
5Update dvc.lock with new hashespreprocess---Updated
6Pipeline run complete-----
💡 All stages executed successfully, outputs saved, and dvc.lock updated to reflect current pipeline state.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
dependencies_checkedNone['data/raw', 'preprocess.py']['data/raw', 'preprocess.py']['data/raw', 'preprocess.py']['data/raw', 'preprocess.py']['data/raw', 'preprocess.py']
command_statusNot runNot runRunningCompletedCompletedCompleted
outputs_savedNoneNoneNonedata/preprocesseddata/preprocesseddata/preprocessed
dvc_lock_statusOldOldOldOldUpdatedUpdated
Key Moments - 3 Insights
Why does DVC check dependencies before running a stage?
DVC checks dependencies (see Step 2 in execution_table) to decide if the stage needs to run. If dependencies haven't changed, DVC can skip running the stage to save time.
What happens if the output data already exists and dependencies are unchanged?
DVC will skip running the command and keep the existing outputs and dvc.lock unchanged, avoiding unnecessary work. This is shown by the dependency check and output save steps.
Why is dvc.lock updated after running a stage?
dvc.lock records exact hashes of dependencies and outputs after execution (Step 5). This helps DVC track changes and decide if future runs need to re-execute stages.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does DVC run the actual command for the 'preprocess' stage?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Command Executed' column in execution_table rows.
According to variable_tracker, what is the status of 'dvc_lock_status' after Step 4?
AUpdated
BOld
CNot created
DDeleted
💡 Hint
Look at the 'dvc_lock_status' row and the column 'After Step 4'.
If the dependencies change, what will DVC do differently in the execution flow?
ARun the stage command again
BSkip running the stage
CDelete outputs without running
DIgnore changes and keep old outputs
💡 Hint
Refer to the key moment about dependency checking and stage execution.
Concept Snapshot
DVC pipelines are defined in dvc.yaml with stages specifying commands, dependencies, and outputs.
Run 'dvc repro' to execute stages if dependencies changed.
DVC tracks outputs and updates dvc.lock with hashes.
This avoids rerunning unchanged stages, saving time.
Use git to version control pipeline files and data pointers.
Full Transcript
This visual execution shows how DVC manages data pipelines. First, you define stages in dvc.yaml with commands, dependencies, and outputs. When you run 'dvc repro', DVC checks if dependencies changed. If yes, it runs the stage command, saves outputs, and updates dvc.lock with new hashes. If no changes, it skips running to save time. This process helps track data and code changes efficiently in machine learning projects.

Practice

(1/5)
1. What is the main purpose of using dvc repro in a DVC pipeline?
easy
A. To delete all pipeline data and cache
B. To initialize a new DVC repository
C. To reproduce pipeline stages and update outputs if inputs changed
D. To manually edit pipeline stage commands

Solution

  1. Step 1: Understand the role of dvc repro

    This command checks if any inputs or dependencies of pipeline stages have changed.
  2. Step 2: Effect of running dvc repro

    If changes are detected, it reruns the affected stages to update outputs accordingly.
  3. Final Answer:

    To reproduce pipeline stages and update outputs if inputs changed -> Option C
  4. Quick Check:

    dvc repro updates pipeline outputs [OK]
Hint: Remember: repro means rerun changed pipeline parts [OK]
Common Mistakes:
  • Confusing repro with initialization commands
  • Thinking repro deletes data
  • Assuming repro edits pipeline commands
2. Which of the following is the correct syntax to add a pipeline stage with DVC that runs python train.py and outputs model.pkl?
easy
A. dvc stage add -n train -o model.pkl python train.py
B. dvc add stage train -o model.pkl python train.py
C. dvc run -n train -o model.pkl python train.py
D. dvc stage add -n train -d train.py -o model.pkl python train.py

Solution

  1. Step 1: Identify required flags for stage creation

    The dvc stage add command requires -n for name, -d for dependencies, and -o for outputs.
  2. Step 2: Check which option includes all required flags correctly

    dvc stage add -n train -d train.py -o model.pkl python train.py uses -n train, -d train.py (dependency), and -o model.pkl with the command python train.py.
  3. Final Answer:

    dvc stage add -n train -d train.py -o model.pkl python train.py -> Option D
  4. Quick Check:

    Stage add needs name, dependency, output flags [OK]
Hint: Stage add needs -n (name), -d (deps), -o (outputs) [OK]
Common Mistakes:
  • Omitting the dependency with -d
  • Using deprecated dvc run instead of stage add
  • Mixing order of flags incorrectly
3. Given this DVC pipeline stage definition in dvc.yaml:
stages:
  preprocess:
    cmd: python preprocess.py data/raw data/processed
    deps:
      - data/raw
      - preprocess.py
    outs:
      - data/processed
What happens when you run dvc repro after modifying data/raw?
medium
A. The preprocess stage reruns and updates data/processed
B. Nothing happens because only preprocess.py changes trigger rerun
C. The pipeline fails due to missing output specification
D. All pipeline stages rerun regardless of changes

Solution

  1. Step 1: Identify dependencies of the preprocess stage

    The stage depends on data/raw and preprocess.py.
  2. Step 2: Effect of changing data/raw on dvc repro

    Changing a dependency triggers rerun of that stage to update outputs.
  3. Final Answer:

    The preprocess stage reruns and updates data/processed -> Option A
  4. Quick Check:

    Changed input triggers stage rerun [OK]
Hint: Change in deps triggers rerun of that stage [OK]
Common Mistakes:
  • Assuming no rerun if only data changes
  • Thinking all stages rerun always
  • Confusing outputs with dependencies
4. You run dvc repro but get an error: ERROR: failed to reproduce stage 'train': missing dependency 'data/train.csv'. What is the most likely cause?
medium
A. The file data/train.csv was deleted or moved after pipeline creation
B. The dvc.yaml file is missing the train stage
C. The dvc.lock file is corrupted
D. You forgot to run dvc init before dvc repro

Solution

  1. Step 1: Understand the error message

    The error says a dependency file is missing, which means DVC cannot find data/train.csv.
  2. Step 2: Common causes of missing dependency errors

    Usually, the file was deleted, renamed, or moved after the pipeline stage was created.
  3. Final Answer:

    The file data/train.csv was deleted or moved after pipeline creation -> Option A
  4. Quick Check:

    Missing dependency file causes repro error [OK]
Hint: Check if all dependency files exist before repro [OK]
Common Mistakes:
  • Assuming dvc.yaml missing stage causes this error
  • Blaming dvc.lock corruption without evidence
  • Forgetting to initialize repo before repro
5. You want to create a DVC pipeline with two stages: extract that outputs data/raw.csv, and train that depends on data/raw.csv and outputs model.pkl. Which sequence of commands correctly sets up this pipeline?
hard
A. dvc stage add -n train -o model.pkl python train.py dvc stage add -n extract -d data/raw.csv -o data/raw.csv python extract.py
B. dvc stage add -n extract -o data/raw.csv python extract.py dvc stage add -n train -d data/raw.csv -o model.pkl python train.py
C. dvc run -n extract -o data/raw.csv python extract.py dvc run -n train -d data/raw.csv -o model.pkl python train.py
D. dvc add data/raw.csv dvc add model.pkl

Solution

  1. Step 1: Define extract stage with output only

    Extract stage produces data/raw.csv so it needs -n extract and -o data/raw.csv with the command.
  2. Step 2: Define train stage depending on extract output

    Train stage depends on data/raw.csv so it needs -d data/raw.csv, outputs model.pkl, and runs python train.py.
  3. Step 3: Confirm correct order and commands

    dvc stage add -n extract -o data/raw.csv python extract.py dvc stage add -n train -d data/raw.csv -o model.pkl python train.py correctly adds extract first, then train with proper dependencies and outputs.
  4. Final Answer:

    dvc stage add -n extract -o data/raw.csv python extract.py dvc stage add -n train -d data/raw.csv -o model.pkl python train.py -> Option B
  5. Quick Check:

    Define stages with correct deps and outputs [OK]
Hint: Add extract stage first, then train with dependency on extract output [OK]
Common Mistakes:
  • Adding train stage before extract output exists
  • Using dvc add instead of stage add for pipeline steps
  • Missing dependencies in train stage