Pytree2Safetensors
Pytree2Safetensors is a simple package to save and load JAX PyTrees to and from Safetensors, a popular file format for saving neural network weights.
To install, run
pip install --upgrade pytree2safetensors
Pytree2Safetensors depends on jax, safetensors, and jaxtyping. You also need
to have at least Python 3.10
Specification
Serialising/Deserialising
keypath2string(path: KeyPath) -> str
Serializes a JAX key path (i.e., a path to a leaf in a pytree) to a string by joining together a string representation of each key in the path. Prefixes of these representation tell what type of key it is. A GetAttryKey is prefixed with ".", a DictKey is prefixed with "@", and a SequenceKey is prefixed with "#". If the initial key is a GetAttryKey, the initial "." is left off.
Examples:
keypath2string((GetAttrKey("layers"), SequenceKey(10), DictKey("query"),))
# => "layers#10@query
keypath2string((SequenceKey(2), GetAttrKey("layers"), SequenceKey(10), DictKey("query"),))
# => "#2.layers#10@query
string2keypath(string: str) -> KeyPath
Inverse of keypath2string
pytree2dict(tree: PyTree) -> dict
Returns a dictionary of serialized key paths mapping to leaves of the tree.
dict2pytree(dictionary: dict) -> tree
Inverse of pytree2dict, except that it wraps attributes in PyTreeContainers instead of using the original object. This is because there is no way for the deserialiser to know what the original object was. You can use load_into_pytree to load weights into an initialized
pytree.
PyTreeContainer
A class which implements the bare minimum to be a node in a pytree according to JAX.
Saving
save_pytree(tree: PyTree, path: str) -> None
Saves the pytree as a safetensors at the given path. Equivalent to
safetensors.flax.save_file(pytree2dict(tree), path)
Loading
load_file
Alias of safetensors.flax.load_file
load_pytree(path: str) -> PyTree
Loads a file and uses dict2pytree to convert the safetensors dict to a pytree.
set_weights(module: PyTree, dictionary: dict) -> PyTree
Given a pytree module and a safetensors dict, load the weights in the safetensors dict into the module using string2keypath to determine their paths. Returns a new pytree.
load_into_pytree(module: PyTree, path: str) -> PyTree
Equivalent to set_weights(module, load_file(path)).
Metadata
Release files for pytree2safetensors 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pytree2safetensors-0.1.4.tar.gz | 4.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytree2safetensors-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.6 kB
Release files / pytree2safetensors-0.1.4.tar.gz
| Download URL | pytree2safetensors-0.1.4.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d7c9bed9ed5b4bf6cc5dc6615b87c8bc96ed4ede82b7e9c1c22531e47babc65
|
|
BLAKE2b-256 checksum How to use checksums |
057df4953f902c9291bbdc9bbc2a3f41962338385524144d86322e0af0a205a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Release files / pytree2safetensors-0.1.4-py3-none-any.whl
| Download URL | pytree2safetensors-0.1.4-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5713fcb9b5f1fab0717c88bcfc3d7fe72b2dc39d865288f26e66fe5fad391657
|
|
BLAKE2b-256 checksum How to use checksums |
a9b1a6ca7d1339a88126199b07fb02863832d96b2a08af6870e58a798cf1695a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Security for pytree2safetensors 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.