Choose codecs per role¶
An assigner can give the weights and each kind of optimizer state their own codec. It picks a CodecChoice for each tensor from its TensorMeta, which includes the tensor's role (see Tensor roles). Pass the assigner as assign= to save_state, write_archive, compress_file, compress_dir, jabara.hf.attach, or JabaraCheckpointIO. With no assigner, Jabara uses DefaultAssigner: bytegroup-zstd/1 for float dtypes and zstd/1 for the rest.
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
Pick codecs with RoleAssigner¶
RoleAssigner maps each role name to a CodecChoice. A role that the mapping does not list goes to default, which is DefaultAssigner() when you leave it out.
import torch
from jabara.assign import CodecChoice, RoleAssigner
from jabara.container import Archive
from jabara.torch import save_state
state = torch.load("checkpoint-1000/optimizer.pt", weights_only=True)
assigner = RoleAssigner(
{
"adam_m": CodecChoice(codec_id="zstd/1", params={"level": 9}),
"adam_v": CodecChoice(codec_id="zstd/1", params={"level": 9}),
}
)
save_state(state, "optimizer.jbr", assign=assigner)
with Archive.open("optimizer.jbr") as archive:
for role, codec in sorted({(s.meta.role, s.codec) for s in archive.stats()}):
print(role, codec)
adam_m zstd/1
adam_v zstd/1
step bytegroup-zstd/1
The first and second moments use zstd/1 at level 9. The mapping does not list step, so the default assigner gives those tensors bytegroup-zstd/1. params holds the codec's parameters. The codec's Params model checks them, and its defaults fill in what you leave out. The archive records the codec id and the full parameters of each tensor, so a reader builds the same codec. Built-in codecs lists the codecs and their parameters.
A choice that names an unknown codec raises UnknownCodecError, and parameters that the codec does not accept raise pydantic.ValidationError. Both errors are raised when the archive is written, not when you build the assigner.
The other built-in assigners¶
UniformAssigner(choice)applies one choice to every float tensor andDefaultAssignerto the rest.RoleAssigner(by_role, default=...)takes another assigner asdefault, soRoleAssigner({"weight": ...}, default=UniformAssigner(...))sends every other role to one codec.
A role is any string. The built-in schemas produce weight, adam_m, adam_v, adam_vmax, momentum, muon_momentum, step, and other. A schema that you write can add more (see Add a codec, an assigner, a state schema, or a file format).
Choose from the command line¶
jabara compress has no option to pick a codec. It uses the default assigner, or xor-delta/1 against a base when you pass --base (see Store a checkpoint as a difference against a base). To use a RoleAssigner, call compress_file or save_state from Python.
To decide, run jabara bench. It runs each candidate codec on every role of a checkpoint and prints one row per candidate and role, so you can compare codecs for the first and the second moments separately:
$ jabara bench checkpoint-1000/optimizer.pt --candidates zstd/1,bytegroup-zstd/1 --repeats 1
... threads, best of 1, 1 warm-up, chunk ... bytes
zstd/1 step: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
zstd/1 adam_m: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
zstd/1 adam_v: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
bytegroup-zstd/1 step: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
bytegroup-zstd/1 adam_m: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
bytegroup-zstd/1 adam_v: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
Compare codecs on your own checkpoint explains the rows. jabara ls on an archive shows the codec of every tensor.