Sub topic 3
Gating promotion on evaluation results
This chapter assumes you can write Python dataclasses, run commands in a shell, and read a basic accuracy metric. It builds on the stage functions and the configuration object from earlier chapters.
Gating promotion on evaluation results
A gate is a check that stops a model from moving forward when its metrics fall short. The gate compares evaluation accuracy to a threshold and raises an error on failure, so the promotion step never runs for a weak model.
def promote_if_passing(metrics: dict, min_accuracy: float) -> bool:
if metrics["accuracy"] < min_accuracy:
raise ValueError(
f"accuracy {metrics['accuracy']:.3f} is below the gate of {min_accuracy}"
)
return TrueStages as a dependency graph
A stage declares its inputs. Those declarations form a directed acyclic graph (DAG), a set of steps where arrows show which step must finish first. Stages with no path between them can run in any order.
Idempotent stages and caching
An idempotent stage produces the same output when it runs again with the same inputs. Caching relies on this property. The stage stores its result under a key built from its name and inputs, and skips the work when that key already exists.
import hashlib
import json
import pathlib
import pickle
def run_cached(name, inputs, compute, cache_dir=pathlib.Path("cache")):
cache_dir.mkdir(exist_ok=True)
key = hashlib.sha256(json.dumps([name, inputs], sort_keys=True).encode()).hexdigest()[:16]
path = cache_dir / f"{name}-{key}.pkl"
if path.exists():
return pickle.loads(path.read_bytes())
result = compute(**inputs)
path.write_bytes(pickle.dumps(result))
return resultRunning the pipeline from the CLI, CI, or an orchestrator
The same entry point serves all three environments. A command line call looks like this:
python -m pipeline run --config configs/prod.jsonA CI job runs the tests first, then the pipeline, so a failing test blocks training. An orchestrator such as Airflow or cron runs the same command on a schedule and records each run's status.
Handling failures and retries
Orchestrators can retry failed stages. Retries are safe only for idempotent stages, because a half-written file could corrupt the next attempt. Do not retry gate failures. The same inputs produce the same metric, so a retry wastes compute. Retry transient errors such as network timeouts.
Exercise
Why is retrying a gate failure usually pointless?
Show answer
With the same data, configuration, and seed, the stage produces the same metric, so the gate fails again. Only a change to the inputs can change the outcome.