Skip to content
fn

save_sharded

→None
save_sharded(obj: object, path: str | bytes | os.PathLike[str], shard_size_mb: float = 1024.0, pickle_protocol: int = 4)

Save obj as a sharded, self-describing checkpoint directory.

Splits a state dict across multiple shard files capped at shard_size_mb MiB each and emits a JSON index listing which keys live in which shard. Useful for very large models where a single monolithic file is impractical to host, partial-load, or download-resume.

Parameters

objobject
Object to save. dict / OrderedDict state dicts are sharded; other types are written as a single shard for compatibility with load_sharded.
path(str, bytes or os.PathLike)
Destination directory. Created if it does not exist.
shard_size_mbfloat= 1024.0
Soft cap on per-shard size in MiB. A tensor exceeding the cap gets its own shard. Default 1024.0.
pickle_protocolint= 4
Pickle protocol forwarded to every per-shard save. Default 4.

Notes

The packing algorithm is a first-fit pass that never splits a single tensor across shards — preserving the property that any given key resolves to exactly one file. Keys that share a storage (a tied weight, a view and its base) are packed together as one unit, so the storage is written once and every alias shares it again after load_sharded; such a group larger than the cap gets a shard of its own. When that moves a key, the index records the dict's key order (key_order) and load_sharded restores it. The index records the optional _metadata attribute when present on a state dict.

Examples

>>> import lucid
>>> sd = {"w": lucid.randn(4096, 4096)}
>>> lucid.serialization.save_sharded(sd, "/tmp/ckpt_dir", shard_size_mb=64)