save_sharded
→Nonesave_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
objobjectdict / OrderedDict state dicts are
sharded; other types are written as a single shard for
compatibility with load_sharded.path(str, bytes or os.PathLike)shard_ size_ mbfloat= 1024.01024.0.pickle_ protocolint= 4save.
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)