Opt in to a lossy codec and read the recorded error¶
Jabara is lossless unless you opt in. A lossy codec (quant8-linear/1 or quant8-log/1) stores a float tensor as 8-bit codes, so the restored tensor is close to the saved one but not equal to it. A run resumed from lossy optimizer state is not a bit-exact continuation of the run that saved it. This guide assumes that you can pick codecs per role (see Choose codecs per role).
Before you start¶
Start in an empty directory. Save make_checkpoint.py there and run it. Like the Tutorial: compress and restore a checkpoint, it needs the torch extra and safetensors. It writes checkpoint-1000/, which the examples use.
$ python make_checkpoint.py
Opt in with allow_lossy¶
An assigner that picks a lossy codec makes the write raise LossyNotAllowedError, and no archive appears under the final name:
import os
import torch
from jabara.assign import CodecChoice, RoleAssigner
from jabara.errors import LossyNotAllowedError
from jabara.torch import save_state
state = torch.load("checkpoint-1000/optimizer.pt", weights_only=True)
assigner = RoleAssigner(
{"adam_m": CodecChoice(codec_id="quant8-linear/1"), "adam_v": CodecChoice(codec_id="quant8-log/1")}
)
try:
save_state(state, "lossy.jbr", assign=assigner)
except LossyNotAllowedError as error:
print(error)
print(os.path.exists("lossy.jbr"))
tensor 'state/0/exp_avg': codec quant8-linear/1 is lossy
False
Pass allow_lossy=True to confirm the choice. Tensors that the assigner gives a lossless codec stay lossless. Here the step tensors keep bytegroup-zstd/1:
save_state(state, "lossy.jbr", assign=assigner, allow_lossy=True)
The other entry points handle the opt-in as follows:
compress_file,compress_dir, andwrite_archivetakeallow_lossyandassign.jabara.hf.attach(trainer, assign=..., allow_lossy=True)applies to every checkpoint. Withoutallow_lossy, the error propagates out ofTrainer.train.JabaraCheckpointIOhas noallow_lossyand never writes a lossy tensor.jabara compresscannot pick a lossy codec.jabara benchruns lossy candidates without an opt-in, because it writes no archive. A lossy row does not mean thatcompressaccepts the codec.
What the two codecs bound¶
Both codecs quantize each chunk on its own and need a float dtype (float16, bfloat16, float32, or float64). Decoding is deterministic: a payload decodes to the same bytes on every machine. Encoding with quant8-log/1 can differ between platforms, because it builds its table with the platform's log and exp. The same input can then give a different payload, and a different file. A chunk with NaN, an infinity, or a value range that overflows float64 raises NonFiniteError. Nothing is clamped.
quant8-linear/1places up to 256 levels evenly between the chunk's minimum and maximum. The error bound depends on the chunk's range, so one large value in a chunk raises the error of all its small values.quant8-log/1places levels evenly on the logarithm of the magnitude, and stores the sign and exact zeros separately. The error bound grows with each value's magnitude, so a small value's error stays small next to the value.
Built-in codecs gives the exact bounds and the payload layouts.
Read the recorded error¶
The writer decodes each lossy chunk once, compares it with the input, and stores the largest absolute difference over the tensor as max_abs_err. A lossless tensor stores None. The value is in the tensor's own units, so compare it with the tensor's scale, not with the value of another tensor:
from jabara.container import Archive
from jabara.torch import load_state
restored = load_state("lossy.jbr")
with Archive.open("lossy.jbr") as archive:
for stat in archive.stats()[:3]:
print(stat.meta.name, stat.codec, stat.lossless, stat.max_abs_err is None)
stat = archive.stats()[1]
difference = (restored["state"][0]["exp_avg"].double() - state["state"][0]["exp_avg"].double()).abs().max()
print(float(difference) == stat.max_abs_err)
state/0/step bytegroup-zstd/1 True True
state/0/exp_avg quant8-linear/1 False False
state/0/exp_avg_sq quant8-log/1 False False
True
The last line shows that max_abs_err equals the measured difference between the saved and the restored tensor. jabara ls --json gives the same fields for each tensor, lossless and max_abs_err, but the text output of jabara ls does not. jabara bench prints max_abs_err=<value> on a lossy row, and marks the row NOT EXACT when its tensors do not decode bit for bit.
What verify checks¶
For a lossy tensor, the checksum of each chunk is the CRC-32 of the decoded bytes, not of the original bytes. Archive.verify() and jabara verify decode every chunk and check that checksum. A pass means that the archive is intact and decodes to the values that the writer measured. It cannot tell how close those values are to the original, because the archive does not hold the original. The recorded max_abs_err tells you that.
with Archive.open("lossy.jbr") as archive:
archive.verify()