jabara.formats.base のソースコード
"""The interface of a checkpoint file format. See DESIGN.md, "Checkpoint file formats"."""
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Iterable
from pathlib import Path
from typing import ClassVar, NamedTuple
from pydantic import JsonValue
from jabara.types import TensorRecord
[ドキュメント]
class Split(NamedTuple):
"""A checkpoint file taken apart: its tensors, and what restores the file around them."""
#: The tensors, in the order `join` takes their bytes back.
records: list[TensorRecord]
#: What `join` needs besides the bytes, as JSON. The archive index stores it with `kind`.
source: dict[str, JsonValue]
#: The bytes of the file that are not tensor data, if the format keeps any. The archive stores them as one more
#: tensor of dtype `uint8` and role `other`, so a codec compresses them too.
remainder: bytes = b""
[ドキュメント]
class CheckpointFormat(ABC):
"""Base class of every checkpoint file format. A subclass sets `kind`, `names`, `in_directory`, and
`frameworks`, implements `split` and `join`, and is registered with `@register_format`.
`join(split(path))` must rebuild the file byte for byte when the tensors come back bit for bit. `split` only
reads: it never runs code that the file holds.
"""
#: Recorded in the archive index as `source["kind"]`; a reader finds the format by it, so it never changes.
kind: ClassVar[str]
#: `fnmatch` patterns of the file names this format takes when a file is named on its own.
names: ClassVar[tuple[str, ...]]
#: `fnmatch` patterns of the file names `compress_dir` takes from a checkpoint directory.
in_directory: ClassVar[tuple[str, ...]]
#: (framework, file names) pairs for the formats reference page.
frameworks: ClassVar[tuple[tuple[str, str], ...]]
[ドキュメント]
@abstractmethod
def split(self, path: Path) -> Split:
"""Take the file at `path` apart. Raises `ArchiveFormatError` for a file this format cannot take."""
[ドキュメント]
@abstractmethod
def join(
self, source: dict[str, JsonValue], remainder: bytes, tensors: Iterable[bytes], out: Path, *, exact: bool
) -> None:
"""Write the file at `out` from `source`, `remainder`, and the bytes of each record in the order `split`
returned them. `exact` is true when every tensor came back bit for bit, so the file must come back byte for
byte; a format that can check that does. Raises `ArchiveFormatError` for a `source` it cannot use."""