Tutorial: compress and restore a checkpoint¶
In this tutorial you compress a small checkpoint directory with the jabara command, inspect it, verify it, and restore it. Then you do the same from Python with save_state and load_state.
You need Jabara with the torch extra in an active virtual environment (see Installation). The checkpoint script also needs the safetensors package. Install it into the same environment:
$ pip install safetensors
In an environment from uv sync, run uv pip install safetensors instead, because uv environments have no pip.
Make a checkpoint directory¶
In an empty directory, save this script as make_checkpoint.py (docs/_snippets/make_checkpoint.py in the clone, or on GitHub). It trains a model of two 256 by 256 linear layers for three AdamW steps with a fixed seed. Then it writes the weights, the optimizer state, and the trainer state under the file names of a Hugging Face Trainer checkpoint.
"""Write checkpoint-1000/, a small checkpoint directory with the files a Hugging Face Trainer saves."""
import json
from pathlib import Path
import torch
from safetensors.torch import save_file
torch.manual_seed(0)
model = torch.nn.Sequential(torch.nn.Linear(256, 256), torch.nn.ReLU(), torch.nn.Linear(256, 256))
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
for _ in range(3):
batch = torch.randn(32, 256)
optimizer.zero_grad()
torch.nn.functional.mse_loss(model(batch), batch).backward()
optimizer.step()
checkpoint = Path("checkpoint-1000")
checkpoint.mkdir(exist_ok=True)
save_file(model.state_dict(), checkpoint / "model.safetensors")
torch.save(optimizer.state_dict(), checkpoint / "optimizer.pt")
(checkpoint / "trainer_state.json").write_text(json.dumps({"global_step": 1000}))
Run it:
$ python make_checkpoint.py
You now have checkpoint-1000, a directory with model.safetensors, optimizer.pt, and trainer_state.json.
Compress the directory¶
$ jabara compress checkpoint-1000
checkpoint-1000/model.safetensors.jbr: ... -> ... bytes (...% of original)
checkpoint-1000/optimizer.pt.jbr: ... -> ... bytes (...% of original)
Each line names a new archive and gives the tensor bytes before and after compression, and the stored size as a percentage of the original.
Jabara wrote and verified both archives, then deleted model.safetensors and optimizer.pt. In a directory, jabara compress takes only the *.safetensors files and optimizer.pt, so trainer_state.json is unchanged.
Inspect the archives¶
$ jabara ls checkpoint-1000
checkpoint-1000/model.safetensors.jbr: 4 tensors, ... -> ... bytes (...% of original)
0.bias float32 [256] weight bytegroup-zstd/1 ... -> ... bytes (...% of original)
0.weight float32 [256, 256] weight bytegroup-zstd/1 ... -> ... bytes (...% of original)
2.bias float32 [256] weight bytegroup-zstd/1 ... -> ... bytes (...% of original)
2.weight float32 [256, 256] weight bytegroup-zstd/1 ... -> ... bytes (...% of original)
checkpoint-1000/optimizer.pt.jbr: 13 tensors, ... -> ... bytes (...% of original)
state/0/step float32 [] step bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/0/exp_avg float32 [256, 256] adam_m bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/0/exp_avg_sq float32 [256, 256] adam_v bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/1/step float32 [] step bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/1/exp_avg float32 [256] adam_m bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/1/exp_avg_sq float32 [256] adam_v bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/2/step float32 [] step bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/2/exp_avg float32 [256, 256] adam_m bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/2/exp_avg_sq float32 [256, 256] adam_v bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/3/step float32 [] step bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/3/exp_avg float32 [256] adam_m bytegroup-zstd/1 ... -> ... bytes (...% of original)
state/3/exp_avg_sq float32 [256] adam_v bytegroup-zstd/1 ... -> ... bytes (...% of original)
(remainder) uint8 [...] other bytegroup-zstd/1 ... -> ... bytes (...% of original)
Each tensor line gives the name, dtype, shape, role, and codec. Jabara gives each tensor a role from its place in the state. Every tensor of a safetensors file has the role weight. In the optimizer state, exp_avg has the role adam_m and exp_avg_sq has the role adam_v.
An assigner picks the codec of each tensor. With no assigner, Jabara uses DefaultAssigner, which gives these float32 tensors bytegroup-zstd/1.
Verify the archives¶
$ jabara verify checkpoint-1000
ok checkpoint-1000/model.safetensors.jbr
ok checkpoint-1000/optimizer.pt.jbr
verify decodes every chunk and checks its CRC-32. A damaged archive makes it print jabara: <message> and exit with status 1.
Restore the directory¶
$ jabara decompress checkpoint-1000
checkpoint-1000/model.safetensors
checkpoint-1000/optimizer.pt
model.safetensors and optimizer.pt match the files that the script wrote byte for byte. The archives are deleted. Add --keep to compress or decompress to keep the input files. With --recursive, these commands take a directory that holds several checkpoints, such as checkpoint-1000 and checkpoint-2000 side by side.
Do the same from Python¶
save_state writes a nested state of tensors and plain values to an archive. Load the optimizer state from optimizer.pt and save it:
import torch
from jabara.torch import save_state
state = torch.load("checkpoint-1000/optimizer.pt", weights_only=True)
info = save_state(state, "optimizer.jbr")
print(info.path)
optimizer.jbr
Open the archive to inspect and verify it. stats() gives one entry per tensor, and verify() raises an error if a chunk does not match its checksum:
from jabara.container import Archive
with Archive.open("optimizer.jbr") as archive:
archive.verify()
for stat in archive.stats():
print(stat.meta.name, stat.meta.role, stat.codec)
state/0/step step bytegroup-zstd/1
state/0/exp_avg adam_m bytegroup-zstd/1
state/0/exp_avg_sq adam_v bytegroup-zstd/1
state/1/step step bytegroup-zstd/1
state/1/exp_avg adam_m bytegroup-zstd/1
state/1/exp_avg_sq adam_v bytegroup-zstd/1
state/2/step step bytegroup-zstd/1
state/2/exp_avg adam_m bytegroup-zstd/1
state/2/exp_avg_sq adam_v bytegroup-zstd/1
state/3/step step bytegroup-zstd/1
state/3/exp_avg adam_m bytegroup-zstd/1
state/3/exp_avg_sq adam_v bytegroup-zstd/1
load_state returns the same nested state. Compare it with the state that you saved:
from jabara.torch import load_state
restored = load_state("optimizer.jbr")
print(restored["param_groups"] == state["param_groups"])
print(all(
torch.equal(restored["state"][i][key], state["state"][i][key])
for i in state["state"]
for key in state["state"][i]
))
True
True
The restored state loads into an optimizer like the output of torch.load:
model = torch.nn.Sequential(torch.nn.Linear(256, 256), torch.nn.ReLU(), torch.nn.Linear(256, 256))
optimizer = torch.optim.AdamW(model.parameters())
optimizer.load_state_dict(restored)
Next steps¶
Command line lists every command and its options.
Built-in codecs lists the built-in codecs.
Python API documents
save_state,load_state,Archive, and the rest of the Python API.