Sub topic 1
Why CI for ML Is Different: Code, Data, and Models Can All Break
Prerequisites
This chapter assumes that you know Python and basic pytest, use Git and pull requests, and have trained and evaluated a model with scikit-learn or a similar library. You should also understand the basic structure of a YAML-based CI service such as GitHub Actions.
The three sources of failure
Continuous integration (CI) is an automated process that builds and tests a project whenever someone changes it. In a conventional software project, most failures come from source code: a function returns the wrong value, an interface changes, or a dependency breaks an import.
A machine learning (ML) project has at least three additional failure surfaces:
- Code can be wrong. A feature transformation might use the wrong unit or mishandle missing values.
- Data can be wrong. A column might disappear, a value might exceed its allowed range, or the class distribution might change.
- A model can be wrong. Training may complete successfully while producing invalid probabilities, poor predictions, or a large regression from the current model.
The important difference is that a successful program run does not prove that an ML system is correct. A training script can finish with exit code zero while learning from an empty class, an accidentally duplicated feature, or a corrupted label column.
| Layer | Example failure | Useful check |
|---|---|---|
| Code | A scaler divides by zero | Unit test for valid and invalid inputs |
| Data | `age` contains `-4` | Schema and range validation |
| Model | Accuracy drops from 0.91 to 0.80 | Quality gate against a baseline |
A layered CI strategy
Treat the test suite as layers. Each layer answers a different question and should fail with a message that helps the developer locate the problem.
- Code tests check small, deterministic functions such as feature transforms, parsers, and preprocessing steps.
- Data tests check the structure and contents of representative data. They verify required columns, data types, allowed ranges, missing-value limits, and sometimes distributions.
- Model behavior tests check properties that should remain true across model implementations. For example, a classifier should return one probability row per input row, and each row should sum to one.
- Quality gates compare candidate metrics with explicit minimums and a trusted baseline.
- Pipeline smoke tests run the complete training or inference path on a small committed sample. They detect wiring errors that isolated tests cannot see.
A useful CI order is fastest first. Run unit and data checks before expensive model training. If a feature function already fails, there is no value in spending several minutes training a model.
Why reproducibility matters
A CI result is useful only if another run can reasonably reproduce it. Seed random number generators used for data splitting, sampling, and model training. Pin dependency versions so that a package upgrade does not silently change preprocessing or model behavior. Use tolerances for floating-point comparisons instead of requiring exact equality.
Reproducibility does not mean that every system will produce bit-for-bit identical results. Hardware, libraries, and parallel algorithms can still introduce small differences. Define acceptable tolerances and compare metrics using those limits.
What belongs in a pull request
Every pull request should run checks that are fast, stable, and relevant to the changed code. A small committed sample dataset makes data validation and smoke tests predictable. Larger training runs, distribution comparisons on production-scale data, and expensive fairness or robustness evaluations can run on a schedule or when explicitly requested.
Keep test data and generated artifacts separate. Version the identity of the data, model, code revision, and evaluation metrics together. Otherwise, a passing CI result may be impossible to explain later.
Exercise
A pull request changes only a feature transformation. Which checks should run immediately, and why?
Show answer
Run the unit tests for the transformation, representative data validation, and a small pipeline smoke test. These checks are fast and can catch both the local logic error and any wiring problem caused by the change.