Compress the checkpoints of many runs

A seed sweep or a hyperparameter search leaves one folder per run, each with several checkpoint directories. This guide compresses all of them with --recursive, checks the archives, and measures whether a delta against another run would help. It assumes that you know jabara compress and jabara verify (see the Tutorial: compress and restore a checkpoint).

To compress the checkpoints while the runs train, attach Jabara to each run instead (see Use Jabara with the Hugging Face Trainer and Use Jabara with PyTorch Lightning).

Before you start

Start in an empty directory. Save make_runs.py there. Like the Tutorial: compress and restore a checkpoint, it needs the torch extra and safetensors. It trains two small runs with the seeds 0 and 1, and each run keeps two checkpoints in the layout of a Hugging Face Trainer:

"""Write runs/seed-0/ and runs/seed-1/, two small runs that each keep checkpoint-2/ and checkpoint-4/."""

import json
from pathlib import Path

import torch
from safetensors.torch import save_file

for seed in (0, 1):
    torch.manual_seed(seed)
    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 step in range(1, 5):
        batch = torch.randn(32, 256)
        optimizer.zero_grad()
        torch.nn.functional.mse_loss(model(batch), batch).backward()
        optimizer.step()
        if step % 2 == 0:
            checkpoint = Path(f"runs/seed-{seed}/checkpoint-{step}")
            checkpoint.mkdir(parents=True, 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": step}))
$ python make_runs.py

You now have runs/seed-0/checkpoint-2, runs/seed-0/checkpoint-4, runs/seed-1/checkpoint-2, and runs/seed-1/checkpoint-4.

Compress one checkpoint directory

$ jabara compress runs/seed-0/checkpoint-2
runs/seed-0/checkpoint-2/model.safetensors.jbr: ... -> ... bytes (...% of original)
runs/seed-0/checkpoint-2/optimizer.pt.jbr: ... -> ... bytes (...% of original)

jabara compress takes one checkpoint directory. Given runs/seed-0 it prints jabara: runs/seed-0: no *.safetensors files or optimizer.pt to compress; it holds 2 checkpoint directories (checkpoint-2, checkpoint-4), pass --recursive to take them all and exits with status 1.

Compress every checkpoint of a run

--recursive takes the checkpoint directories one level below its PATH. The layout here has two levels, so run the command once per run. runs alone holds no checkpoint directory of its own, and jabara compress runs --recursive finds nothing.

$ jabara compress runs/seed-0 --recursive
runs/seed-0/checkpoint-2: skipped, already compressed
runs/seed-0/checkpoint-4/model.safetensors.jbr: ... -> ... bytes (...% of original)
runs/seed-0/checkpoint-4/optimizer.pt.jbr: ... -> ... bytes (...% of original)
runs/seed-0/checkpoint-4: 2 archives, ... -> ... bytes (...% of original)
total: 2 checkpoints, 1 skipped, ... -> ... bytes (...% of original)
$ jabara compress runs/seed-1 --recursive
runs/seed-1/checkpoint-2/model.safetensors.jbr: ... -> ... bytes (...% of original)
runs/seed-1/checkpoint-2/optimizer.pt.jbr: ... -> ... bytes (...% of original)
runs/seed-1/checkpoint-2: 2 archives, ... -> ... bytes (...% of original)
runs/seed-1/checkpoint-4/model.safetensors.jbr: ... -> ... bytes (...% of original)
runs/seed-1/checkpoint-4/optimizer.pt.jbr: ... -> ... bytes (...% of original)
runs/seed-1/checkpoint-4: 2 archives, ... -> ... bytes (...% of original)
total: 2 checkpoints, 0 skipped, ... -> ... bytes (...% of original)

runs/seed-0/checkpoint-2 was compressed in the previous section, so it is skipped. If the command stops, run it again: it skips the checkpoints that are done. A failure stops the run at that checkpoint. The checkpoints before it are done, and the rest stay as they were. Each checkpoint deletes its originals only after it has written and verified every archive in it. To keep the originals, for example until you have copied the archives to other storage, pass --keep. --dry-run lists what would be compressed and changes nothing.

Check the archives

$ jabara verify runs/seed-0 --recursive
ok runs/seed-0/checkpoint-2/model.safetensors.jbr
ok runs/seed-0/checkpoint-2/optimizer.pt.jbr
ok runs/seed-0/checkpoint-4/model.safetensors.jbr
ok runs/seed-0/checkpoint-4/optimizer.pt.jbr
$ jabara verify runs/seed-1 --recursive
ok runs/seed-1/checkpoint-2/model.safetensors.jbr
ok runs/seed-1/checkpoint-2/optimizer.pt.jbr
ok runs/seed-1/checkpoint-4/model.safetensors.jbr
ok runs/seed-1/checkpoint-4/optimizer.pt.jbr

verify --recursive checks every archive and does not stop at the first failure. It prints FAILED <path>: <reason> for each archive that fails, and exits with status 1.

Measure a delta against another run

A delta archive stores a checkpoint as its difference from a base (see Store a checkpoint as a difference against a base). Jabara has not measured how well a checkpoint of one run compresses against a checkpoint of another run. For runs that start from different random weights, there is no reason to expect a gain. Measure it on your own checkpoints before you store anything that way. jabara bench reads both directories, compressed or not, and writes no file:

$ jabara bench runs/seed-1/checkpoint-4 --base runs/seed-0/checkpoint-4 --candidates bytegroup-zstd/1,xor-delta/1 --repeats 1
... threads, best of 1, 1 warm-up, chunk ... bytes
bytegroup-zstd/1 weight: ... 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
xor-delta/1 weight: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
xor-delta/1 step: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
xor-delta/1 adam_m: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact
xor-delta/1 adam_v: ... bytes (...% of original), encode ... MB/s, decode ... MB/s, exact

Store a delta only for the roles where the xor-delta/1 row is smaller than the bytegroup-zstd/1 row. For runs that start from the same pretrained weights, use those weights, compressed, as the base. jabara compress DIR --base BASE codes a whole checkpoint directory against an archive or a compressed directory, and every archive written against a base needs that base to be read.

Resume one of the runs

Run jabara decompress on a checkpoint directory, or jabara decompress runs/seed-0 --recursive on a whole run, to write the original files back, then resume with your framework as usual. With the Trainer adapter attached, the Trainer resumes from the archives directly (see Use Jabara with the Hugging Face Trainer). The adapter reads a delta archive when its base checkpoint is a sibling directory; otherwise restore it first with jabara decompress FILE --base ARCHIVE.