S30 Logo
S30 AI Labwww.thes30.com
Back
#56

MLOps — Pipelines & CI/CD

Expert⚙️ MLOps & SystemsW12 D1

Before You Start

Required knowledge
Model training, validation splits, Python functions, JSON, and file handling.
Libraries used
NumPy
Dataset and source
Classification data generated by make_synthetic; model/run artifacts are written locally.
Expected output
A reproducible training pipeline with configuration, schema checks, quality gates, and saved artifacts.

MLOps Pipelines & CI/CD — Theory & Concepts

Read this before attempting the exercises. The central idea is that training is only one stage in an MLOps workflow. You also care about validation, reproducibility, quality gates, artifacts, and CI checks.


Concise Project Structure

mlops_project/
│
├── pipeline.py
├── train.py
├── validate.py
├── evaluate.py
├── ci_checks.py
├── config.py
├── requirements.txt
├── tests/
│   └── test_pipeline.py
├── .github/
│   └── workflows/
│       └── ml_ci.yml
└── README.md

This lab compresses that structure into a notebook, but the responsibilities are the same:

  • ●config.py: stores configuration only
  • ●validate.py: checks shapes, NaNs, and label validity before training
  • ●train.py: contains the model training logic
  • ●evaluate.py: computes metrics such as accuracy and log loss
  • ●pipeline.py: orchestrates the full workflow end to end
  • ●ci_checks.py: verifies artifacts, determinism, and regression gates after training
  • ●tests/test_pipeline.py: unit-tests individual functions
  • ●requirements.txt: lists Python dependencies
  • ●ml_ci.yml: tells GitHub Actions what to run after git push
  • ●README.md: explains how another developer runs the project

The key architectural idea is:

  • ●pipeline.py builds the model
  • ●ml_ci.yml and ci_checks.py ensure the pipeline keeps working correctly on every new push

Core Flow

git push
    ↓
CI starts
    ↓
Run run_pipeline(cfg)
        │
        ├── Config
        ├── Generate Data
        ├── Validate Data
        ├── Train
        ├── Evaluate
        ├── Quality Gate
        └── Save candidate artifacts
    ↓
CI Checks
        ├── Check determinism
        ├── Regression checks
        ├── Unit tests
        └── Validate artifacts
    ↓
PASS
    ↓
Register Model Version
        ↓
registry/models/toy_logreg/versions/004/
    ↓
Move to Staging
        ↓
Test / approve
        ↓
Promote to Production

The important shift from a normal ML notebook is that training the model is only one stage. In MLOps, you also care whether the run is reproducible, whether bad data is rejected, whether model quality is acceptable, and whether you can trace exactly what produced a given model.


1. Config Is the Source of Truth

Instead of having values scattered through notebook cells:

seed = 42
lr = 0.1
epochs = 500
test_size = 0.2

you package them together:

cfg = {
    "seed": 42,
    "lr": 0.1,
    "epochs": 500,
    "test_size": 0.2,
    "acc_gate": 0.85,
}

Then:

run_pipeline(cfg)

means:

Run the entire ML workflow according to this exact configuration.

That makes experiments much easier to reproduce.


2. Data Validation Happens Before Training

For example:

check("X_2d", X.ndim == 2)

means:

I expect X to be a matrix of shape (n_samples, n_features).

Likewise:

check("n_match", X.shape[0] == y.shape[0])

makes sure every training example has a corresponding target.

And:

check(
    "no_nan",
    np.isfinite(X).all() and np.isfinite(y).all(),
)

prevents things like:

X =
[[1.2, 3.1],
 [NaN, 5.2]]

from silently reaching your optimizer.

This reflects an important production principle:

Fail early rather than train incorrectly.


3. Determinism Is a First-Class Requirement

Suppose you run:

run_pipeline(cfg)

twice.

With the same data, seed, initialization, splitting logic, and training algorithm, you want something like:

run1 = {
    "acc": 0.91,
    "log_loss": 0.27,
}

run2 = {
    "acc": 0.91,
    "log_loss": 0.27,
}

If instead you get:

Run 1 accuracy = 0.91
Run 2 accuracy = 0.78

something nondeterministic may have entered the system.

The fixed seed therefore is not just convenient for teaching. It is part of reproducibility.


4. Evaluation Is Different from a Quality Gate

This distinction is especially important.

Evaluation merely calculates:

metrics = {
    "acc": 0.87,
    "log_loss": 0.31,
}

A quality gate makes a decision:

check("acc_gate", metrics["acc"] >= 0.85)

So if:

accuracy = 0.87
gate = 0.85

the model passes.

But:

accuracy = 0.81
gate = 0.85

means the pipeline fails.

Conceptually:

  • ●Metric: "How good is the model?"
  • ●Quality gate: "Is the model good enough to proceed?"

That second question is what connects ML training to CI/CD.


5. The Artifact Directory Is Effectively the Output of the Build

You might end with:

artifacts/
├── weights.npy
├── bias.txt
├── metrics.json
└── config.json

These files answer different questions.

weights.npy and bias.txt tell you what model was trained.

metrics.json tells you how well it performed.

config.json tells you how it was produced.

So months later, rather than merely having model_v17, you have enough information to understand the training run.


6. CI Now Tests More Than Code Correctness

Normal software CI might ask:

  • ●Did unit tests pass?
  • ●Did the program crash?

ML CI additionally asks:

  • ●Did training finish?
  • ●Did data validation pass?
  • ●Did accuracy remain acceptable?
  • ●Did results unexpectedly change?
  • ●Were artifacts produced?

That is why CI needs to validate the full ML workflow, not just whether Python executed without crashing.

For example:

Previous accuracy = 0.91
Current accuracy  = 0.90
Threshold         = 0.85

PASS

versus:

Previous accuracy = 0.91
Current accuracy  = 0.73

FAIL

Even if the Python code executes perfectly, the ML build can still be considered bad.


7. CI and CD Are Related but Different

Your current exercise is mainly about CI.

CI — Continuous Integration

code change
   ↓
train/test pipeline
   ↓
validate
   ↓
quality gate

CD — Continuous Delivery / Deployment would extend that:

CI passes
   ↓
register model
   ↓
deploy candidate
   ↓
canary / A-B test
   ↓
production

So your current lab intentionally stops around:

quality gate
→ saved artifacts

rather than automatically deploying anything.


8. Where Tools Like MLflow, Airflow, and Kubernetes Eventually Fit

Once the simple pipeline makes sense, the larger MLOps ecosystem becomes much easier to understand:

Your current concept          Later production tool
----------------------------------------------------
run_pipeline()                Airflow / Dagster
metrics.json                  MLflow
artifacts/                    S3 / artifact store
config.json                   experiment tracking
quality gate                  CI pipeline
saved model                   model registry
manual model serving          Kubernetes / serving infra
manual inspection             monitoring system

The tools automate or scale concepts you already understand. They do not replace the concepts.

So the strongest interview explanation is usually:

MLOps is about making the ML lifecycle reproducible, testable, deployable, and observable. We validate data, automate training pipelines, track artifacts and experiments, enforce model-quality gates in CI, version models, deploy them safely, monitor production behavior, and potentially retrain when needed.

Then you can introduce MLflow, Airflow, Kubernetes, feature stores, model registries, and monitoring systems.

For this lab specifically, the most important mental model is:

MLOps is not just deploying models.

It starts much earlier:

reproducible training → validation → evaluation → gates → artifacts → CI → deployment → monitoring.

Ready to practice?

Python 3 — Notebook
0/8 solved
1
Dataset & Setup

MLOps — Pipelines & CI/CD — Student Lab

Offline lab: build a tiny reproducible ML pipeline and add CI-style checks.

0 — Config + pipeline entrypoint

Loading editor...
2
0 — Config + pipeline entrypoint

0 — Config + pipeline entrypoint

Loading editor...
3
1 — Data generation + schema checks

1 — Data generation + schema checks

Loading editor...
4
2 — Logistic regression (NumPy)

2 — Logistic regression (NumPy)

Loading editor...
5
3 — Evaluation + quality gate

3 — Evaluation + quality gate

Loading editor...
6
4 — Artifacts (model + metrics)

4 — Artifacts (model + metrics)

Loading editor...
7
5 — Implement `run_pipeline` + CI-style checks
1

5 — Implement run_pipeline + CI-style checks

Loading editor...
8
6 — CI simulation: determinism + regression gate
2

6 — CI simulation: determinism + regression gate

Loading editor...

Need help? Share feedback