Skip to main content

model_weights_store

An encrypted on-disk store of model weights, shareable across processes.

Weights downloaded from the Hub are sealed with AES-256-GCM and written to a store directory under one root:

  • host-<scope>-<key id>/: shared by every background run of one host. Its key is derived from the host's cache key, which lives only in memory, so the store is unreadable once its host is gone.
  • proc-<pid>-<create ms>-<random>/: private to one process, under a random key held only by that process, and removed when it exits.

An entry is <name>.weights.enc, where name is a keyed hash of the model's canonical identity. Each entry is sealed under its own key and bound, through the associated data, to the store's key id, its own name and its plaintext length, so a file that is moved, renamed, copied from another store or truncated fails the tag.

Writes to a shared store are capped, and refused when they would leave the volume below a free-space floor. Least recently used entries are evicted to make room only once the new entry is in place, so a write that fails or is refused evicts nothing.

Module​

Functions​

associated_data​

def associated_data(key_id: str, entry_name: str, plaintext_length: int) ‑> bytes:

Return the associated data an entry is sealed with.

Arguments

  • key_id: The store's public key id.
  • entry_name: The entry's name.
  • plaintext_length: The length of the entry's plaintext.

Returns b"bfmw/1", the ASCII key id, the ASCII entry name, then the length as a big-endian unsigned 64-bit integer.

canonical_identity​

def canonical_identity(    *,    hub_host: str,    username: str,    model_name: str,    model_version: int,    project_id: str | None,    model_hash: str,    weights_tag: str | None,    weights_size: int | None,) ‑> bytes:

Return the bytes that identify one model's weights in a store.

Arguments

  • hub_host: Host of the Hub the model came from.
  • username: The model's owner on the Hub.
  • model_name: The model's name on the Hub.
  • model_version: The model's version.
  • project_id: The project the model was fetched for, if any.
  • model_hash: The Hub's hash of the model's code.
  • weights_tag: The weights object's validator, if any.
  • weights_size: The weights object's size in bytes, if known.

Returns The eight fields as UTF-8, joined with NUL; None is empty.

open_host_store​

def open_host_store(    root: Path, memo_key: bytes, scope: str,) ‑> WeightsStore:

Open the store this host's background runs share.

The directory is host-<scope>-<key id> under root, created 0700 if it does not exist.

Arguments

  • root: The store root, already prepared by prepare_weights_root.
  • memo_key: The host's model-memo key.
  • scope: The host's <pid>-<create ms> scope.

Returns The shared store.

Raises

  • ValueError: If scope is malformed or memo_key is not 32 bytes.
  • OSError: If the directory cannot be created, or is a symlink, not a directory, or not private to this user.

open_process_store​

def open_process_store(    root: Path,) ‑> WeightsStore:

Open a store private to this process, removed when it exits.

The directory is proc-<pid>-<create ms>-<random> under root, created 0700, and its key is random and held only in this process's memory.

Arguments

  • root: The store root, already prepared by prepare_weights_root.

Returns The per-process store.

Raises

  • OSError: If the directory cannot be created or is not private.

prepare_weights_root​

def prepare_weights_root(root: Path) ‑> bool:

Create root private to this user, and check it can be trusted.

Arguments

  • root: The store root.

Returns True when root exists and is private to this user; False when it is a symlink, not a directory, shared with other users, or cannot be created.

resolve_model_weights_root​

def resolve_model_weights_root() ‑> pathlib.Path:

Return the directory that holds the model weights stores.

Read at call time, so a settings reload is honoured.

Returns settings.model_weights_cache_dir when set, otherwise bitfount-model-weights in the system temporary directory.

Classes​

WeightsStore​

class WeightsStore(directory: Path, memo_key: bytes, *, shared: bool):

One store directory of sealed model weights.

Built by open_host_store or open_process_store. Writes from this process are serialised by a lock; other processes writing the same store race only on whole-file renames, so a reader sees one complete file or another.

Arguments

  • directory: The store directory.
  • memo_key: The 32-byte key the store's entries are derived from. It is never in repr, and the store cannot be pickled.
  • shared: Whether other processes use the store. Only a shared store is held to the size cap and the free-space floor.

Attributes

  • directory: The store directory.
  • key_id: The store's public key id.
  • shared: Whether other processes use the store.

Raises

  • ValueError: If memo_key is not 32 bytes.

Methods​


discard​

def discard(self, path: Path) ‑> None:

Delete the entry at path. Best effort.

Arguments

  • path: The entry's path.

entry_name​

def entry_name(self, identity: bytes) ‑> str:

Return the name of the entry for identity.

Arguments

  • identity: The model's canonical identity (see canonical_identity).

Returns 32 lowercase hex characters, keyed by the store's key.

entry_path​

def entry_path(self, name: str) ‑> pathlib.Path:

Return the path of the entry called name.

Arguments

  • name: An entry name, as returned by entry_name.

Returns The entry's path in the store directory.

Raises

  • ValueError: If name is not an entry name, so it cannot shape a path.

read​

def read(self, path: Path) ‑> bytes:

Read and unseal the entry at path.

The file is held open only for one whole read, so a writer on Windows is blocked as briefly as possible.

Arguments

  • path: The entry's path, as returned by entry_path.

Returns The weights.

Raises

  • FileNotFoundError: If there is no such entry.
  • DecryptError: If the entry is too short, fails its tag, or was sealed for another name, store or length.
  • OSError: If the entry could not be read for any other reason.

touch​

def touch(self, path: Path) ‑> None:

Mark the entry at path as just used, for LRU eviction. Best effort.

Arguments

  • path: The entry's path.

write​

def write(self, name: str, plaintext: bytes) ‑> StoreWriteFailure | None:

Seal plaintext and store it as the entry called name.

A shared store refuses an entry larger than settings.model_weights_cache_max_bytes, and plans which least recently used entries to evict so the new one fits under it. It then refuses the write if writing it would leave less than settings.model_weights_cache_min_free_bytes free on the volume, not counting the planned entries, which are still on disk while it is written. The entry is written to a temporary file, flushed to disk and renamed into place. A rename refused by a Windows sharing lock is retried 3 times, 50 ms apart.

Planned entries are evicted only after the rename, so a write that is refused or fails leaves every existing entry in place. Writers in other processes are not coordinated, so the store can sit briefly over the cap.

Arguments

  • name: The entry's name, as returned by entry_name.
  • plaintext: The weights.

Returns None when stored; otherwise why not: "cap" (the entry alone is larger than the cap), "free_space" (the floor), "locked" (the rename stayed locked) or "os_error" (any other disk error).

Raises

  • ValueError: If name is not an entry name.