model_memo
Reuse of model code and weights downloaded from the Hub.
Every background run is a process of its own, and a waved run re-enters the
whole DAG once per wave, so a model inference step loads its model once per
wave. ModelMemo keeps each model's class and the location of its weights for
the life of the process, so later waves load from local disk. It also keeps the
weights in a store shared by every background run of one host, so a later run
reads them from disk rather than from the Hub. A lookup that read the weights
from the shared store or downloaded them hands those bytes to its caller, so
the first load in a process decrypts them once at most.
The shared store is host-<scope>-<key id>/ under the model weights root. Its
key is derived from the host's cache key, which exists only in memory: the host
holds it for as long as it runs, and each background run asks its pod for it
once, at the memo's first lookup. Each entry is sealed with AES-GCM under a key
of its own and named by a keyed hash of the model, its version, the project, the
Hub's code hash and the weights object's validator. Re-uploaded weights
therefore miss rather than serve stale bytes, and a file that is altered,
truncated, renamed or copied from another store fails its tag, is deleted and is
downloaded again. Once the host has gone nothing in its store can be decrypted,
and the sweep removes it.
A run with no key to use, because there is no pod to ask, the pod gave none, or
settings.model_weights_shared_cache_enabled is false, keeps its weights in a
proc-* store of its own instead, under a random key held only in its memory
and removed when it exits. So does a run whose weights object has no validator
or changed during the download, and a run whose weights the shared store
refused (its size cap, its free-space floor, a locked file) or failed to write.
When neither store can be used the caller is handed the weights themselves, and
the next wave downloads again. The Hub is still asked for the model's code once
per process and project, since that call is its access check.
A failure to use the disk is not a failure to load a model. Nothing here raises on the caller's behalf: a store problem costs a download, and only the Hub's own errors propagate.
Module
Functions
get_model_memo
def get_model_memo() ‑> ModelMemo:Return the process-wide model memo, creating it on first use.
Classes
FetchedModel
class FetchedModel(model_cls: type[ModelProtocol], weights: HubWeights | None):What the Hub gave for a model: its class and, if it has any, its weights.
Arguments
model_cls: The model class downloaded from the Hub.weights: The model's weights, orNonewhen the Hub gave no weights URL, either because there are none or because the metadata call failed.
HubWeights
class HubWeights( model_hash: str, probe: Callable[[], WeightsValidator | None], download: Callable[[], DownloadedWeights],):A model's weights object on the Hub, not yet downloaded.
Arguments
model_hash: The Hub's hash of the model's code.probe: Reads the weights object's validator without downloading it, returningNonewhen it cannot.download: Downloads the weights, with the validator of the object they came from. Raises the Hub's errors.
Variables
-
download : Callable[[], DownloadedWeights]
-
model_hash : str
-
probe : Callable[[], WeightsValidator | None]
MemoLookup
class MemoLookup( entry: MemoisedModel, record: ModelLoadRecord | None, weights: bytes | None = None,):The result of one ModelMemo.get_or_load.
Arguments
entry: The model class and its weights.record: How the weights were obtained, orNonewhen they were already memoised in this process.weights: The weights this lookup already holds in memory: those it read from the shared store, which passed their tag in that read, or those it downloaded.Nonewhen it read nothing, on a hit in this process, or when the model has no weights. A caller applies these rather than readingentryback, and holds them no longer than that.
MemoisedModel
class MemoisedModel( model_cls: type[ModelProtocol], weights_path: Path | None = None, weights_bytes: bytes | None = None, weights_store: WeightsStore | None = None,):A model class and the weights to load into an instance of it.
Exactly one of weights_path and weights_bytes is set when the model has
weights at all; both are None when it has none.
Arguments
model_cls: The model class downloaded from the Hub.weights_path: Path to the sealed weights inweights_store.weights_bytes: The weights themselves, set only when they could not be stored. Such a result is not memoised.weights_store: The store holdingweights_path.
Variables
-
model_cls : type[ModelProtocol]
-
weights_bytes : bytes | None
-
weights_path : Path | None
-
weights_store : WeightsStore | None
ModelMemo
class ModelMemo(root: Path | None = None, key_source: ModelMemoKeySource | None = None):Remembers model classes and weights for the life of the process.
Weights go to the host's shared store when this process has the host's key and the weights object has a validator, and otherwise, or when the shared store does not take them, to a store private to this process. Which stores exist is settled once, at the first lookup that has weights to store, and is fixed from then on.
Arguments
root: Directory holding the stores. Defaults toresolve_model_weights_root(), read at the first lookup.key_source: Where to ask for the host's key. Defaults to the source the background run installed, read at the first lookup.
Methods
get_or_load
def get_or_load( self, key: ModelMemoKey, load: Callable[[], FetchedModel],) ‑> MemoLookup:Return the memoised entry for key, calling load on a miss.
load runs while this key's lock is held, so concurrent waves wanting
the same model fetch it once. The lock is per key: the inference steps
of one DAG run in parallel against different models and must not wait
on each other.
On a miss the weights are read from the host's shared store when it holds them, and downloaded otherwise. A download goes to the shared store when the weights object kept its validator throughout, and to this process's store when there is no shared store to use, the validator changed, or the shared store refused or failed the write.
Arguments
key: Identifies the model.load: Fetches the model class and its weights' metadata from the Hub. Called at most once per key per process, unless the result carried no weights, could not be stored, or was invalidated.
Returns The entry, how its weights were obtained and, when this lookup read them from the shared store or downloaded them, the weights.
Raises
Exception: Whateverloador the weights download raises. Nothing to do with the stores is raised.
has_had_weights
def has_had_weights(self, key: ModelMemoKey) ‑> bool:Return whether key's model has been loaded with weights in this process.
Arguments
key: Identifies the model.
Returns True when it has, so an answer without weights is a failed fetch.
invalidate
def invalidate(self, key: ModelMemoKey) ‑> None:Forget key, deleting its weights from this process's store.
For a caller whose weights would not load: the next get_or_load reads
the shared entry again or downloads. A shared entry's file is left
alone, because another run may have written good weights at that path
since this run read it; the next read of it deletes it if it still
fails.
The delete happens under the key's lock, with the entry, because an
entry is named after the model's identity alone. Deleting outside the
lock would let a concurrent get_or_load install a fresh file at that
same path first, and this delete would then remove the new file rather
than the stale one - leaving a memoised entry pointing at nothing.
Arguments
key: Identifies the model to forget.
note_has_weights
def note_has_weights(self, key: ModelMemoKey) ‑> None:Record that key's model was loaded with weights in this process.
Arguments
key: Identifies the model.
read_weights
def read_weights(self, entry: MemoisedModel) ‑> bytes | None:Return entry's weights, decrypting them if they are on disk.
A read from a store marks the entry as just used, so a shared entry every wave reads is not the first one evicted. That mark is best effort and never fails the read.
Arguments
entry: The memoised model whose weights to read.
Returns
The weights, or None if the model has none.
Raises
DecryptError: If the file was altered, truncated or sealed under another key.OSError: If the file could not be read, including when it has been evicted or deleted since it was memoised.
replace_with_download
def replace_with_download( self, key: ModelMemoKey, fetched: FetchedModel, content: bytes | None,) ‑> None:Replace key's entry with weights the caller downloaded past the memo.
For a caller whose memoised weights would not read even after a fresh
get_or_load, and which then downloaded them straight from the Hub.
The old entry is forgotten, so later waves no longer read a file that
is gone or bad. Its file is left alone: it is named by the model's
identity, so another run may already have written good weights there.
The download is kept in this process's own store, as get_or_load
keeps weights the shared store would not take, and later lookups are
hits that read it from there. With no process store, or no weights,
the key is left unmemoised and the next lookup starts afresh.
Arguments
key: Identifies the model.fetched: What the Hub gave for the model when it was downloaded.content: The downloaded weights, orNonewhen there were none.
ModelMemoKey
class ModelMemoKey( hub_host: str, username: str, model_name: str, model_version: int, project_id: str | None,):Identifies one model as the Hub resolves it.
project_id is part of the identity, not just of the request. The Hub gates
model access by project, and one pod process serves several projects, so a
memo shared between them would serve one project bytes that were authorised
for another without the Hub ever being asked. Two projects entitled to the
same model therefore download it once each, which costs one extra download
and keeps the access check where it belongs.
Arguments
hub_host: Host of the Hub the model came from. Distinguishes the same model name on staging from production.username: The model's owner on the Hub.model_name: The model's name on the Hub.model_version: The model's version. Pinned by the task, so new weights normally arrive as a new version and therefore a new key.project_id: The project the model was fetched for, orNonefor a publicly accessible model.
Variables
-
hub_host : str
-
model_name : str
-
model_version : int
-
project_id : str | None
-
username : str