Archive format¶
An archive is one file, conventionally named <original name>.jbr. The writer produces it front to back in one pass, and a reader starts from the end. This page describes version 1, the only version.
Layout¶
offset 0 MAGIC 8 bytes b"JABARA\x00\x01"
chunks ... encoded chunks, back to back
index N bytes UTF-8 JSON
N 8 bytes unsigned little-endian
crc 4 bytes CRC-32 of the index bytes, unsigned little-endian
end MAGIC_END 8 bytes b"\x01\x00ARABAJ"
Index¶
The index is the JSON form of the pydantic model jabara.container.ArchiveIndex, whose JSON schema is in JSON schemas. This archive holds one tensor stored with raw/1, so the chunk length equals the raw length:
import numpy as np
from jabara.assign import CodecChoice, UniformAssigner
from jabara.container import Archive, write_archive
from jabara.types import TensorMeta, TensorRecord
meta = TensorMeta(name="w", dtype="float32", shape=(2, 2), role="weight")
record = TensorRecord(meta, np.arange(4, dtype="<f4"))
write_archive("w.jbr", [record], assign=UniformAssigner(CodecChoice(codec_id="raw/1")))
with Archive.open("w.jbr") as archive:
print(archive.index.model_dump_json(indent=2))
{
"format": 1,
"writer": "jabara 0.1.0",
"tensors": [
{
"name": "w",
"dtype": "float32",
"shape": [
2,
2
],
"role": "weight",
"codec": "raw/1",
"params": {},
"lossless": true,
"max_abs_err": null,
"chunks": [
{
"offset": 8,
"length": 16,
"raw_length": 16,
"crc32": 627055499
}
]
}
],
"skeleton": null,
"source": {
"kind": "tensors"
},
"base": null
}
Field |
Meaning |
|---|---|
|
|
|
|
|
One entry per tensor, in the order that the writer received them. |
|
The nested structure of a saved state, as |
|
What the archive restores to. Jabara writes |
|
|
A tensor entry has the fields of TensorMeta (name, dtype, shape, role) and these:
Field |
Meaning |
|---|---|
|
The codec id. |
|
The full parameters of the codec, defaults included. |
|
The |
|
For a lossy codec, the largest absolute difference between the input and the decoded values over the tensor, measured by the writer. |
|
The chunks of the tensor, in order. A zero-byte tensor has none. |
A chunk entry has offset and length, the position and size of the encoded chunk in the file, raw_length, the size of the decoded chunk, and crc32, the CRC-32 (zlib.crc32) of the bytes that a reader must produce. Those are the original bytes for a lossless codec and the decoded bytes for a lossy one.
Chunks¶
The writer splits the raw bytes of each tensor into chunks of
chunk_sizebytes, rounded down to a whole number of items and never less than one item. The default is 16 MiB (CHUNK_SIZE). The largest allowed value is 256 MiB (MAX_CHUNK_SIZE).Each chunk is coded on its own, so chunks can be coded in parallel on a thread pool of
threadsworkers, the chunks of different tensors at the same time. The writer writes the chunks in tensor and chunk order, whatever order they finish in.A delta tensor has the same chunk boundaries as its reference in the base, so the reference of chunk k is chunk k of the base tensor.
What a reader checks¶
Before it reads any chunk,
Archive.openraisesArchiveFormatErrorfor a wrong magic number at either end, an index whose CRC-32 does not match, an unknownformat, or an index that contradicts itself or the file. The index must have unique tensor names, chunk sizes that are whole numbers of items and add up to the size of each tensor, and chunks that lie between the magic number and the index without overlapping.After decoding a chunk, a reader compares its length and CRC-32 with the index and raises
ChecksumErroron a mismatch.A delta archive needs its base. Reading a delta tensor without it raises
MissingBaseError, and reading it with a base that has no tensor of the same name, dtype, and shape raisesBaseMismatchError. Opening a delta archive with base archives that lack one with a recordedindex_crcalso raisesBaseMismatchError, naming it. A delta archive may refer to another delta archive (jabara compress --recursive --delta previous), so restoring one needs the archive and every archive behind it, down to a full one. The index has no field for a chain: each archive names only its own base, by file name and index CRC-32, and the format version is unchanged. Jabara 0.1.0 never writes a chain, and given the middle archive of one as the base it raisesMissingBaseErrorwhen it reads a delta tensor.
How the writer protects the file¶
write_archivewrites to a new temporary file next to the target, named after it with.<random hex>.tmpadded. The writer creates it so that it cannot replace an existing file. It syncs the file to disk, renames it to the target, and on POSIX syncs the directory. A crash never leaves a partly written archive under the final name. On macOS,fsyncdoes not flush the write cache of the drive.write_archiveaccepts a lazy iterable of records, such as a generator. It reads the bytes of each record in place, without a copy. Withthreadsabove 1, it takes the next record while chunks of earlier records are still being coded. The bytes of a record must not change untilwrite_archivereturns, so a generator must make a new buffer for each record. A generator that refills one buffer goes undetected: the archive then holds wrong bytes, and no error is raised.write_archiverefuses, before it creates the file, askeletonorsourcethat would not read back, such as JSON nested deeper than the reader's parser accepts.For a lossy tensor, the writer decodes every chunk once, compares it with the input, and records the result in
max_abs_err. It raisesValueErrorif the error is not finite.