Sub topic 18
Testing and Observing the Retraining Pipeline
Assume you know the basic stages of machine learning: data preparation, training, evaluation, and deployment. This chapter focuses on testing the pipeline as software and observing it while it runs.
Test the pipeline as a system
A retraining pipeline can fail even when the training code works. New data might have missing columns, an evaluation step might compare the wrong model version, or a failed job might restart and publish the same model twice.
Test each layer:
- Unit tests check small functions, such as schema checks and trigger decisions.
- Integration tests run several stages together with a small fixture dataset.
- Contract tests verify that one stage produces the fields and files expected by the next stage.
- End-to-end tests run the complete pipeline in a safe environment.
- Failure tests deliberately remove data, make validation fail, or interrupt a job.
- Idempotency tests run the same job twice and confirm that it does not create conflicting artifacts.
A useful test checks both the result and the reason. For example, a drift trigger should return drift_with_enough_data, not only True.
import unittest
def should_promote(challenger_auc, champion_auc, data_ok, minimum_gain=0.002):
if not data_ok:
return False, "data_validation_failed"
if challenger_auc - champion_auc < minimum_gain:
return False, "gain_below_threshold"
return True, "promote"
class PromotionTests(unittest.TestCase):
def test_rejects_bad_data(self):
self.assertEqual(
should_promote(0.95, 0.90, False),
(False, "data_validation_failed"),
)
def test_accepts_sufficient_gain(self):
self.assertEqual(
should_promote(0.905, 0.900, True),
(True, "promote"),
)
if __name__ == "__main__":
unittest.main()Observe every run
Record structured metadata for every pipeline run:
- run ID and trigger reason
- input data version and row count
- code, dependency, and configuration versions
- training duration and resource use
- validation results and evaluation metrics
- candidate model ID and promotion decision
- error type, retry count, and final status
Track these metrics over time. Useful signals include success rate, end-to-end duration, queue time, cost per run, rejected-candidate rate, and time from trigger to deployment. Logs explain one run. Metrics reveal trends. Traces show where a multi-stage run spends time.
Move toward continuous training
Maturity should increase control and feedback, not merely automation.
| Stage | Typical practice | Main risk |
|---|---|---|
| Manual | An engineer starts and reviews each run | Slow response |
| Scheduled | A fixed workflow runs periodically | Unnecessary runs |
| Triggered | Drift, performance, or volume starts a run | Noisy triggers |
| Guarded | Validation, approval, and rollback protect release | More operational work |
| Continuous | Frequent automated runs use full monitoring | Runaway cost or bad data |
Start with reproducible manual runs. Add a schedule after the steps are reliable. Add drift and performance triggers only after measuring false alarms. Automate promotion last, with clear thresholds, cooldowns, budgets, and rollback.
Exercise
A pipeline succeeds technically, but its runtime doubles and candidate rejection rises for three weeks. What should you inspect first?
Show answer
Inspect input volume, validation warnings, resource use, data quality, and evaluation metrics by run. Compare them with the recorded data and code versions before changing thresholds.