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

format

1, or 2 when base is a list. Jabara 0.1.0 reads only 1.

writer

jabara <version> of the writer.

tensors

One entry per tensor, in the order that the writer received them.

skeleton

The nested structure of a saved state, as jabara.skeleton.flatten returns it, with each tensor replaced by a reference to its name. save_state writes one. null when the writer was given none.

source

What the archive restores to. Jabara writes {"kind": "tensors"} (plain tensors, no file to restore, and the value when the writer was given none), {"kind": "safetensors", "header": "<the file's JSON header>"}, and {"kind": "torch"} (a state written by save_state).

base

null, unless a tensor uses a delta codec. Then it is {"name": "<file name of the base archive>", "index_crc": <CRC-32 of the base index>}, or, when the delta tensors refer to several base archives, a list of two or more such objects. A tensor's reference is in the first archive of the list that holds a tensor of the same name, dtype, and shape.

A tensor entry has the fields of TensorMeta (name, dtype, shape, role) and these:

Field

Meaning

codec

The codec id.

params

The full parameters of the codec, defaults included.

lossless

The lossless flag of the codec.

max_abs_err

For a lossy codec, the largest absolute difference between the input and the decoded values over the tensor, measured by the writer. null for a lossless codec.

chunks

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_size bytes, 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 threads workers, 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.open raises ArchiveFormatError for a wrong magic number at either end, an index whose CRC-32 does not match, an unknown format, 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 ChecksumError on 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 raises BaseMismatchError. Opening a delta archive with base archives that lack one with a recorded index_crc also raises BaseMismatchError, 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 raises MissingBaseError when it reads a delta tensor.

How the writer protects the file

  • write_archive writes to a new temporary file next to the target, named after it with .<random hex>.tmp added. 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, fsync does not flush the write cache of the drive.

  • write_archive accepts a lazy iterable of records, such as a generator. It reads the bytes of each record in place, without a copy. With threads above 1, it takes the next record while chunks of earlier records are still being coded. The bytes of a record must not change until write_archive returns, 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_archive refuses, before it creates the file, a skeleton or source that 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 raises ValueError if the error is not finite.