Export weights to a standard safetensors file¶
You can write the weights in a Jabara archive to a safetensors file that any tool can load, without restoring the whole checkpoint. jabara export-weights ARCHIVE OUT and jabara.files.export_weights(archive_path, out) write the tensors with the role weight. The command refuses an OUT that exists: it prints jabara: OUT: already exists and exits with status 1. export_weights replaces an existing out file without an error.
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
Export an archive of a safetensors file¶
Every tensor of an archive made from a *.safetensors file has the role weight, and the archive keeps the file's header, so the output is the original file, byte for byte. The archive stays in place. If checkpoint-1000/model.safetensors.jbr already exists, skip the first command, because jabara compress refuses a directory that holds both a file and its archive:
$ jabara compress checkpoint-1000/model.safetensors --keep
checkpoint-1000/model.safetensors.jbr: ... -> ... bytes (...% of original)
$ jabara export-weights checkpoint-1000/model.safetensors.jbr exported.safetensors
export-weights prints nothing. To share the model while the checkpoint stays compressed, run it on the model.safetensors.jbr that the Hugging Face Trainer adapter writes.
In Python, make the same call and check that the file equals the original:
import filecmp
from jabara.files import compress_file, export_weights
compress_file("checkpoint-1000/model.safetensors", keep=True)
export_weights("checkpoint-1000/model.safetensors.jbr", "exported.safetensors")
print(filecmp.cmp("checkpoint-1000/model.safetensors", "exported.safetensors", shallow=False))
True
A second call with the same out replaces the file:
from pathlib import Path
Path("exported.safetensors").write_bytes(b"not a safetensors file")
export_weights("checkpoint-1000/model.safetensors.jbr", "exported.safetensors")
print(filecmp.cmp("checkpoint-1000/model.safetensors", "exported.safetensors", shallow=False))
True
Export from save_state¶
For any other archive, the output holds the tensors with the role weight, under their archive names and in archive order, and leaves out the rest. Save a model's state_dict with WeightsSchema so that every tensor has that role (see Save and load state in a plain PyTorch loop):
import torch
from safetensors.torch import load_file
from jabara.schemas import WeightsSchema
from jabara.torch import save_state
model = torch.nn.Linear(4, 2)
save_state(model.state_dict(), "model.jbr", schema=WeightsSchema())
export_weights("model.jbr", "linear.safetensors")
exported = load_file("linear.safetensors")
print(sorted(exported))
print(all(torch.equal(exported[name], tensor) for name, tensor in model.state_dict().items()))
['bias', 'weight']
True
The header is padded with spaces to a multiple of 8 bytes. A dtype that safetensors cannot hold raises UnsupportedDtypeError.
When there is nothing to export¶
An archive with no weight tensor raises ArchiveFormatError, and the command prints jabara: <archive>: no tensors with role 'weight' and exits with status 1. By default, two kinds of archive have none:
The archive of
optimizer.pt, whose tensors are optimizer state.The archive that
jabara compressmakes of a.ptor.pthfile.compressgives those files the optimizer roles, so the tensors of a modelstate_dictget the roleother.
For a .pt file, load it and call save_state with WeightsSchema as above. export-weights takes no base, so it cannot read a delta archive (see Store a checkpoint as a difference against a base).