Skip to content

crp.ml

Auto-generated reference for the crp.ml subpackage.

ml

crp.ml

Managed ML model registry for optional-but-default-on CRP components.

ModelLoadError

Bases: Exception

Raised when a model cannot be loaded within budget.

ModelManager

Central registry for ML models used by CRP subsystems.

register(key, loader, *, budget_ms=50.0, load_timeout_ms=30000.0, device=None, cache_dir=None)

Register a model loader without loading it.

unregister(key)

Drop a model from the registry and release its instance.

is_available(key)

True if the model is currently loaded and usable.

load(key)

Load (or return cached) the model for key.

Returns None if the model is not registered, already failed, or cannot be loaded within its load budget. Never raises.

run(key, fn, *, budget_ms=None, fallback=None)

Run fn(model) under a latency budget.

Parameters:

Name Type Description Default
key str

Registered model key.

required
fn Callable[[Any], Any]

Callable receiving the loaded model and returning a result.

required
budget_ms float | None

Per-call budget. Defaults to the registered spec budget.

None
fallback Any

Value returned if model is unavailable or fn exceeds budget.

None

Returns:

Type Description
Any

fn(model) result, or fallback on any budget/availability issue.

clear()

Unload all models and reset error state.

shutdown()

Release resources.

managed_call(key, fn, *, budget_ms=None, fallback=None)

Convenience wrapper around the global manager.

register_model(key, loader, *, budget_ms=50.0, load_timeout_ms=30000.0)

Convenience wrapper to register a model on the global manager.

timed_fallback(budget_ms, fallback)

Decorator that runs a function under budget_ms and returns fallback on timeout.

The wrapped function receives the model as its first argument; the model is fetched from the registry using the argument name model_key if present.

ml.registry

crp.ml.registry

Managed ML model registry - lazy, budgeted, with deterministic fallbacks.

CRP's ML-driven components are default-on when their optional dependencies are installed, but they are never allowed to block the governance loop. The registry loads models on first use, caches them, enforces a per-call latency budget, and degrades to a rule-based fallback if the model is missing, slow, or raises. This makes ML enhancement mandatory-in-presence without storming resources.

ModelSpec dataclass

Specification for a lazily-loaded model.

ModelLoadError

Bases: Exception

Raised when a model cannot be loaded within budget.

ModelManager

Central registry for ML models used by CRP subsystems.

register(key, loader, *, budget_ms=50.0, load_timeout_ms=30000.0, device=None, cache_dir=None)

Register a model loader without loading it.

unregister(key)

Drop a model from the registry and release its instance.

is_available(key)

True if the model is currently loaded and usable.

load(key)

Load (or return cached) the model for key.

Returns None if the model is not registered, already failed, or cannot be loaded within its load budget. Never raises.

run(key, fn, *, budget_ms=None, fallback=None)

Run fn(model) under a latency budget.

Parameters:

Name Type Description Default
key str

Registered model key.

required
fn Callable[[Any], Any]

Callable receiving the loaded model and returning a result.

required
budget_ms float | None

Per-call budget. Defaults to the registered spec budget.

None
fallback Any

Value returned if model is unavailable or fn exceeds budget.

None

Returns:

Type Description
Any

fn(model) result, or fallback on any budget/availability issue.

clear()

Unload all models and reset error state.

shutdown()

Release resources.

managed_call(key, fn, *, budget_ms=None, fallback=None)

Convenience wrapper around the global manager.

register_model(key, loader, *, budget_ms=50.0, load_timeout_ms=30000.0)

Convenience wrapper to register a model on the global manager.

timed_fallback(budget_ms, fallback)

Decorator that runs a function under budget_ms and returns fallback on timeout.

The wrapped function receives the model as its first argument; the model is fetched from the registry using the argument name model_key if present.