Skip to main content

Module ml

Module ml 

Source
Expand description

On-device ML orchestration and the model inventory seam (SSoT: AI/ML Integrations).

This module owns the structure the AI design fixes — independent of which model actually runs:

  • the registry — the canonical model inventory (one v1-committed row per task) and the embedding-provenance invariant (every embedding carries (model_id, model_version); the vector index refuses inserts from unknown models, while a superseded-but-known version is admitted as a stale-flagged row and excluded from queries until regenerated). It owns the version-bump swap primitive;

  • the regen loop — the background per-asset regeneration orchestration that consumes the staleness a swap creates: it walks stale_embedding_assets and re-embeds each asset at the new canonical version through an injected Embedder seam, resumably and per-asset (never a global truncate). The production implementor is RunnerEmbedder, which rebuilds the derived index by re-reading each original through AssetSource and re-running the canonical model through ModelRunner — the same two seams first-time indexing uses, so a regenerated vector equals what a fresh import would produce.

  • the runner seam — the ModelRunner trait every consumer routes per-task inference over decoded pixels through, and the deterministic FixtureRunner double. Real per-platform runners (ONNX/CoreML/NNAPI, the CLIP runner) ride the default-off, weight-fetching inference feature — a follow-up, never part of the default gate;

  • the orchestrator — the deterministic execution path for the v1-committed slots: run the canonical model over an asset, land semantic + face vectors in the right vec0 partition (platform-partition fallback), and write zero-shot AI tags into tags_ai as a signed metadata update through the AiTagSink seam — implemented by Workspace, named as a trait so ml does not import lifecycle (which imports ml); plus the device-bound batching/thermal policy.

Real per-platform inference is deferred behind the Embedder / ModelRunner seams (the real runner is a later slice), exactly as live MLS group state is deferred behind AlbumAuthority with ReferenceAuthority standing in; the deterministic DeterministicEmbedder and FixtureRunner drive every path with no model weights. The local vector index is the VectorIndex port; its SQLite adapter is capsule-library’s catalog. No model weights are committed to this repository.

Re-exports§

pub use registry::DistanceMetric;
pub use registry::EmbeddingDim;
pub use registry::ModelId;
pub use registry::ModelVersion;
pub use registry::RegistryError;
pub use registry::TaskKind;

Structs§

BBox
A normalized bounding box (coordinates in [0, 1]).
Detection
A detection from a detection task (object box, face box + landmarks downstream).
DeterministicEmbedder
A deterministic reference embedder — no real inference, no model weights. It maps (asset_id, canonical_version) to a stable L2-normalized vector, so:
EmbedError
A failure producing an embedding for an asset.
EmbeddingInsert
A provenanced embedding to insert into the index: the subject asset, the producing model (task, model_id, model_version), the platform partition discriminator, and the vector.
EmbeddingRecord
A stored embedding’s provenance (from the companion table; the vector itself stays in vec0).
FixtureRunner
A deterministic, weightless runner for tests. Declares the canonical model versions from a Registry (so its outputs satisfy the embedding-provenance invariant) and embeds by hashing input bytes — content equality ⇒ vector equality, which makes semantic matches reproducible.
Frame
One image (or video keyframe) handed to a runner. The offline core passes the asset’s bytes; a real runner decodes + runs the model, the fixture hashes them.
KnnHit
A single nearest-neighbour hit.
KnownAnswer
A known-answer probe pinning the deterministic execution path for a task: a fixed input and the exact embedding a bit-exact device must reproduce. Comparing a runner’s output to expected bit-for-bit is the byte-identical known-answer check the contract requires.
ModelRow
One inventory row: the canonical model for a TaskKind — a v1-committed slot from AI/ML — Pipeline Slots, carrying the model, its function, and its named fallback.
RegenReport
The outcome of one regeneration invocation.
Registry
The model inventory seam and the embedding-provenance gate.
RunnerEmbedder
The production Embedder: regeneration re-runs inference over the originals.
StoreError
A failure from the asset store behind the AssetSource / AiTagSink seam, type-erased.
VectorTableSpec
The shape of one task’s vec0 partition table: which task it serves, and the (dim, metric) its vectors are sized and ranked by.

Enums§

BatchMode
Per-asset execution mode, chosen from available memory at task start.
OrchestratorError
Failures from orchestrating inference.
RegenError
Failures from the regeneration loop.
RunnerError
Errors a runner can return.
TaskOutput
What a task produces: a stored embedding vector, or detections that feed downstream stages.
VectorIndexError
Failures from the vector index.

Constants§

CANONICAL_PARTITION
The shared partition every device that reaches bit-exactness on the deterministic execution path writes into. Devices that cannot fall back to their own platform tag and are never merged into this partition.
MICRO_BATCH_SIZES
The micro-batch sizes that keep the NPU cache hot.

Traits§

AiTagSink
The write side: an asset store that accepts AI tags for an asset.
AssetSource
The store the orchestrator reads asset pixels from and indexes vectors into.
Embedder
The embedder seam the regeneration loop drives: “produce the canonical embedding for an asset under a task”. Abstracting it keeps the orchestration testable with a deterministic double and independent of any real model runtime.
EmbeddingProvenance
The embedding-provenance gate the vector index calls before it stores or ranks anything — the seam through which the model inventory reaches the index without the index depending on ml.
ModelRunner
The inference seam every consumer routes through. Implemented by real per-platform runners (behind the inference feature) and by FixtureRunner for tests.
VectorIndex
The store port the ML layer writes embeddings into and ranks them from.

Functions§

auto_tag
Zero-shot tag asset_id: embed the image and each candidate label with the semantic-search embedder, then add as AI tags every label whose cosine similarity to the image clears threshold. The tags land in the asset’s tags_ai OR-set as one signed metadata update (never in tags_user), each carrying the canonical (model_id, model_version). Returns the labels assigned. No separate classifier — the semantic embeddings are reused.
choose_batch_mode
Pick the execution mode: go vertical only when the estimated resident set fits the RAM budget; otherwise stay horizontal to bound peak memory.
embed_and_store
Embed asset_id under task’s canonical model over its decoded pixels and store the vector in the task’s vec0 partition under partition (resolve it with resolve_partition). Serves both embedding slots — SemanticSearch and FaceRecognition. Refuses a non-canonical runner before touching the index. The index is derived state, so no signed manifest is minted (recovery-first: it is rebuilt by re-running inference).
micro_batch_size
The largest micro-batch size (from MICRO_BATCH_SIZES) that fits both the pending count and the device ceiling. Always at least 1.
regenerate_stale
Regenerate stale embeddings for task in embedder’s platform partition, re-deriving the work-list from stale_embedding_assets on each call.
resolve_partition
Resolve the vec0 partition runner’s embeddings for ka.task must land in.
semantic_search
Natural-language search: embed query with the semantic-search embedder and return the k nearest assets in partition’s current canonical version (stale/other-partition vectors are excluded structurally).
should_pause_for_heat
Whether to pause the pipeline for heat: at or above threshold_c the OS may kill the app, so the pipeline pauses until cooldown.

Type Aliases§

Embedding
An embedding vector (L2-normalized by convention, so cosine ranks like the inner product).