Split an archive into parts and join it back¶
A .jbr archive is one file by default. You can lay it out as several files instead: parts of at most a given size, or a given number of parts. Split archives fit a size limit of a storage service, a transfer tool, or a file system, and they let you copy a large checkpoint in pieces. Cutting does not encode anything again, and joining gives back the archive byte for byte. jabara compress --part-size, jabara compress --parts, jabara split, jabara join, and the functions jabara.files.split_archive and jabara.files.join_archive do it.
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
Cut while compressing¶
Give compress a number of parts, or the most bytes a part may hold. Every archive of the call is laid out that way. With --parts 2, each archive is two files:
$ jabara compress checkpoint-1000 --parts 2
checkpoint-1000/model.safetensors.jbr: ... -> ... bytes (...% of original)
checkpoint-1000/optimizer.pt.jbr: ... -> ... bytes (...% of original)
$ python -c "import os; print(*sorted(f for f in os.listdir('checkpoint-1000') if '.jbr' in f), sep='\n')"
model.safetensors.jbr
model.safetensors.jbr.002
optimizer.pt.jbr
optimizer.pt.jbr.002
The first part keeps the name of the archive, model.safetensors.jbr. The others are beside it, with .002, .003, and so on added. Every command that takes an archive takes the first part, or the directory, and reads the parts as one archive:
$ jabara verify checkpoint-1000
ok checkpoint-1000/model.safetensors.jbr
ok checkpoint-1000/optimizer.pt.jbr
ls, verify, decompress, --base, and the Python Archive.open all work this way. decompress deletes every part of an archive unless you pass --keep.
Join and cut an archive that exists¶
jabara join writes the parts back as the one file they were cut from, and deletes the other parts. jabara split cuts an archive that is one file, without encoding it again. Both take an archive or a directory. On a directory, they skip the archives that are already in the state they would make (skipped, already split, skipped, not split):
$ jabara join checkpoint-1000
checkpoint-1000/model.safetensors.jbr: joined 2 parts
checkpoint-1000/optimizer.pt.jbr: joined 2 parts
$ jabara split checkpoint-1000/model.safetensors.jbr --part-size 300KB
checkpoint-1000/model.safetensors.jbr: 2 parts
$ jabara join checkpoint-1000/model.safetensors.jbr
checkpoint-1000/model.safetensors.jbr: joined 2 parts
A split of a file that is already split, and a join of a file that is not, are errors when you name the file, so that a typo does not pass quietly. To change the size of the parts, join first and split again.
How the size is written¶
--part-size takes a whole number and a unit, as Hugging Face's max_shard_size does. The unit decides the base:
KB,MB,GB, andTBcount thousands:2GBis 2,000,000,000 bytes.KiB,MiB,GiB, andTiBcount powers of 1024:500MiBis 524,288,000 bytes. The chunk size, 16 MiB, is in this unit.No unit, or
B, counts bytes. Units are not case-sensitive.2G,1.5GB, and0are errors.
The size is the size of a file, so a part of --part-size 2GB is at most 2,000,000,000 bytes with its 52-byte header. --parts N makes exactly N parts of about the same size, and N is 2 or more.
Where the cuts are¶
A cut falls between two chunks, never inside one, so every chunk is in one part and its CRC-32 still protects it. The index and the trailer at the end of the archive are in the last part. Two things follow:
A part cannot be smaller than the chunk it holds. A size below the largest chunk, or below the index, is refused with a message that names the size that does not fit:
jabara: a chunk (or the index) of 7103 bytes cannot be cut, and it does not fit in a part of 3000 bytes .... With the default chunk size, 16 MiB before coding, a size of 64 MiB or more always fits.--parts Nneeds N-1 places to cut. A tensor is one chunk when it is smaller than 16 MiB, so an archive of a few small tensors has few places, and a request for more parts than it has is refused.
An archive that fits --part-size stays one file, and compress and split do not make a split archive of one part.
Damaged and missing parts¶
Opening a split archive checks every part against the others before it reads a chunk. A part that is missing, in the wrong place, shorter or longer than its header says, or cut from another archive raises ArchiveFormatError, and the command prints one jabara: line that names the file:
jabara: checkpoint-1000/model.safetensors.jbr.002: part 2 of 3 is missing
A damaged byte inside a part is found as it is for a single file: the part header, the index, and every chunk have a CRC-32, and jabara verify decodes every chunk. join checks each part's data against the CRC-32 in its header while it copies, without decoding, so it also stops on a changed byte inside a part, delta archives included, and it changes nothing when a part is wrong. It writes the joined archive under a temporary name, renames it over the first part, and only then deletes the other parts, so a crash leaves either the parts or the whole archive.
Time, errors, and disk space¶
compress codes the whole archive before it cuts it, because the cut points are known only then. A size that is too small, or a --parts that is larger than the archive allows, is reported after the encoding, and the source stays. At its peak, compress with parts and split hold the archive and its parts at the same time, and join holds the parts and the joined file, so plan for about twice the size of the archive on disk.
When compress replaces an archive that is already split, it moves the old parts aside, puts the new ones in place with the first part last, and puts the old parts back if a rename fails. Only a kill between those renames can leave the old archive without its parts, which are then the files that end in .tmp beside it.
Delta archives¶
A split archive can be a base, and a delta archive can be split. A delta archive names its base by file name and the CRC-32 of the base's index, and cutting changes neither, so --base takes the first part or a directory as before. The protections for bases also hold: compress refuses to replace an archive that another one is written against, and decompress refuses to delete one (see Store a checkpoint as a difference against a base).
Use it from Python¶
compress_file and compress_dir take part_size (an integer, or a string such as "2GiB") and parts. split_archive and join_archive work on an existing archive:
from pathlib import Path
from jabara.files import compress_file, join_archive, split_archive
compress_file("checkpoint-1000/model.safetensors", keep=True)
archive = Path("checkpoint-1000/model.safetensors.jbr")
whole = archive.read_bytes()
made = split_archive(archive, parts=2)
print([p.name for p in made])
print(join_archive(archive) == archive, archive.read_bytes() == whole)
['model.safetensors.jbr', 'model.safetensors.jbr.002']
True True
split_archive returns the paths of the parts, the first being the archive itself, and [archive] when it already fits. Give it part_size or parts, not both. The size strings come from jabara.parts.parse_size.
Set it in a config file¶
part_size and parts are keys of the compress and split options like any other (see Run from a config file). The size is a string: part_size = "2GiB".