Skip to main content

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"]

Conversion

Do you have an existing PyTorch checkpoint model that you would like to convert to safetensors? Then try running:

python3 -m pure_safetensors import-pytorch /path/to/model.ckpt /path/to/model.safetensors

You may also be able to convert the model in-place without making an additional copy! Use this facility at your own risk (or make a zero-cost copy-on-write backup copy of your checkpoint file using cp --reflink=always ... if your filesystem supports it).

# convert it
python3 -m pure_safetensors import-pytorch-inplace /data/model.ckpt

# rename
mv /data/model.ckpt /data/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? ✅ ❌ ❌ ❌

Metadata

Release files for pure-safetensors 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pure-safetensors 0.3.2
File Size Uploaded
pure_safetensors-0.3.2.tar.gz 19.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pure-safetensors 0.3.2
File Interpreter ABI Platform
pure_safetensors-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 37.4 kB

Release files / pure_safetensors-0.3.2.tar.gz

Download URL pure_safetensors-0.3.2.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e8b748158428643f9f7c6dddff5784d3051b429e40a0075b63c6662e2c102b13
BLAKE2b-256 checksum
How to use checksums
c093bd80899faff14079c56c09ebd882fb46f484a68522592cf79ab12b404ccf
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pure_safetensors-0.3.2-py3-none-any.whl

Download URL pure_safetensors-0.3.2-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c1af9d49da1addf3716e91634da33c3ee9d3c37a5a498e9af33fcf9a956e661
BLAKE2b-256 checksum
How to use checksums
1945675273f16792d7b139bbd970525713b70fae65962de9d61b96b39e1726fb
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page