Pure-Python safetensors
Project description
pure_safetensors
Safetensors library but in pure clean Python. Run it on PyPy or IronPython or wherever.
Dependencies
We try to keep dependencies light:
- attrs dataclass library (2881 LoC)
- marshmallow serialization and validation library (2647 LoC)
- sortedcollections tiny sorted collections library (339 LoC) built on top of sortedcontainers (1493 LoC)
- (optional) sparsefile sparse file library (191 LoC)
- (optional) fickle whitelist-based firewall for safe pickle loading, used for PyTorch model conversion (926 LoC)
Optionally, this library integrates with NumPy (if available). PyTorch integration is planned, someday.
To run the tests, you'll need pytest
, numpy
, and optionally hypothesis
.
Examples
from pure_safetensors import SafeTensors
with SafeTensors("/path/to/example.safetensors", "r+") as sf:
arrays = sf.as_numpy()
arrays["hello"][3, :] += 420.69
arrays["world"] = arrays["hello"][0:2] * 10
# assign multiple arrays! much faster!
arrays.update(
{
"q": my_array_1,
"k": my_array_2,
"v": my_array_3,
}
)
# delete arrays! such wonders!
del arrays["v"]
Do you have an existing PyTorch checkpoint model that you would like to safetensors? Then try running:
python3 -m pure_safetensors import-pytorch /path/to/model.ckpt /path/to/model.safetensors
Bugs
The space allocator is a greedy algorithm based on first-fit-decreasing bin packing. So if you add/remove tensors to an existing file, it may leave too much empty space behind.
PyTorch support isn't implemented yet.
Alternatives
pure_safetensors | safetensors | pure_torch.py | safetensors.cpp | |
---|---|---|---|---|
Written in pure Python? | ✅ | ❌ | ✅ | — |
Supports NumPy (without PyTorch)? | ✅ | ✅ | ❌ | — |
Supports PyTorch? | ❌ | ✅ | ✅ | — |
Can work without numpy or pytorch? | ✅ | ✅ | ❌ | — |
Can write safetensors files? | ✅ | ✅ | ❌ | ❌ |
Can modify file in-place to add/remove tensors? | ✅ | ❌ | ❌ | ❌ |
Has test suite? | ✅ | ✅ | ❌ | ❌ |
Stable API? | 🤷 | ✅ | ❔ | ❔ |
Automatically makes files sparse to save space? | ✅ | ❌ | ❌ | ❌ |
Works on platforms without mmap? | ✅ | ❌ | ❌ | ❌ |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pure_safetensors-0.3.0.tar.gz
.
File metadata
- Download URL: pure_safetensors-0.3.0.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64bb9fd4eccdd2a6f3e2c0afbe3998d31e6dc6e77ff3cba982ed09007401257b |
|
MD5 | 3c3bed7dcc15ffc87a08fc0e5f830cc1 |
|
BLAKE2b-256 | 9d8a3def7260009e164dfb005cd990b1b26a9fb7ca4f4e163c548ffbd432cfd8 |
File details
Details for the file pure_safetensors-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: pure_safetensors-0.3.0-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ab347c878c588924caf6cf5001c36336948654482ddd64aca64726dbcc21ab2 |
|
MD5 | 26081c70c6ed9bf6075c8b36d4e1a8f4 |
|
BLAKE2b-256 | a84a0a16d1d8f2c768fd6537415cbc839b9e3d3a27d63d1ab30ffd1f6ad63fdb |