Skip to main content

bintensors


GitHub Crates.io Version docs.rs PyPI Python Version

Another file format for storing your models and "tensors", in a binary encoded format, designed for speed with zero-copy access.

Installation

Cargo

You can add bintensors to your cargo by using cargo add:

cargo add bintensors

Pip

You can install bintensors via the pip manager:

pip install bintensors

From source

For the sources, you need Rust

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Make sure it's up to date and using stable channel
rustup update
git clone https://github.com/GnosisFoundation/bintensors
cd bintensors/bindings/python
pip install setuptools_rust
# install
pip install -e .

Getting Started

import torch
from bintensors import safe_open
from bintensors.torch import save_file

tensors = {
   "weight1": torch.zeros((1024, 1024)),
   "weight2": torch.zeros((1024, 1024))
}
save_file(tensors, "model.bt")

tensors = {}
with safe_open("model.bt", framework="pt", device="cpu") as f:
   for key in f.keys():
       tensors[key] = f.get_tensor(key)

Overview

This project initially started as an exploration of the safetensors file format, primarily to gain a deeper understanding of an ongoing parent project of mine, on distributing models over a subnet. While the format itself is relatively intuitive and well-implemented, it leads to some consideration regarding the use of serde_json for storing metadata.

Although the decision by the Hugging Face safetensors development team to utilize serde_json is understandable, such as for file readability, I questioned the necessity of this approach. Given the complexity of modern models, which can contain thousands of layers, it seems inefficient to store metadata in a human-readable format. In many instances, such metadata might be more appropriately stored in a more compact, optimized format.

TDLR why not just use a more otimized serde such as bincode.

Observable Changes

serde safetensors Serde figure from safetensors generated by cargo bench serde bintensors Serde figure from bintensors generated by cargo bench

Incorporating the bincode library led to a significant performance boost in deserialization, nearly tripling its speed—an improvement that was somewhat expected. Benchmarking code can be found in bintensors/bench/benchmark.rs, where we conducted two separate tests per repository, comparing the serialization performance of model tests in safesensors and bintensors within the Rust-only implementation. The results, as shown in the figure above, highlight the substantial gains achieved.

To better understand the factors behind this improvement, we analyzed the call stack, comparing the performance characteristics of serde_json and bincode. To facilitate this, we generated a flame graph to visualize execution paths and identify potential bottlenecks in the serde_json deserializer. The findings are illustrated in the figures below.

This experiment was conducted on macOS, and while the results are likely consistent across platforms, I plan to extend the analysis to other operating systems for further validation.

serde bintensors
Serde figure from bintensors generated by flamepgraph & inferno

serde safetensors
Serde figure from safetensors generated by flamepgraph & inferno

Format

bintensors
Visual representation of bintensors (bt) file format

Notes

  • Duplicate keys are disallowed. Not all parsers may respect this.
  • Tensor values are not checked against, in particular NaN and +/-Inf could be in the file
  • Empty tensors (tensors with 1 dimension being 0) are allowed. They are not storing any data in the databuffer, yet retaining size in the header. They don’t really bring a lot of values but are accepted since they are valid tensors from traditional tensor libraries perspective (torch, tensorflow, numpy, ..).
  • The byte buffer needs to be entirely indexed, and cannot contain holes. This prevents the creation of polyglot files.
  • Endianness: Little-endian. moment.
  • Order: ‘C’ or row-major.
  • Checksum over the bytes, giving the file a unique identiy.
    • Allows distributive networks to validate distributed layers checksums.

Benefits

Since this is a simple fork of safetensors it holds similar propeties that safetensors holds.

  • Preformance boost: Bintensors provides a nice preformace boost to the growning ecosystem, of model stroage.
  • Prevent DOS attacks: To ensure robust security in our file format, we've implemented anti-DOS protections while maintaining compatibility with the original format's approach. The header buffer is strictly limited to 100MB, preventing resource exhaustion attacks via oversized metadata. Additionally, we enforce strict address boundary validation to guarantee non-overlapping tensor allocations, ensuring memory consumption never exceeds the file's actual size during loading operations. This two-pronged approach effectively mitigates both memory exhaustion vectors and buffer overflow opportunities.
  • Faster load: PyTorch seems to be the fastest file to load out in the major ML formats. However, it does seem to have an extra copy on CPU, which we can bypass in this lib by using torch.UntypedStorage.from_file. Currently, CPU loading times are extremely fast with this lib compared to pickle. GPU loading times are as fast or faster than PyTorch equivalent. Loading first on CPU with memmapping with torch, and then moving all tensors to GPU seems to be faster too somehow (similar behavior in torch pickle)
  • Lazy loading: in distributed (multi-node or multi-gpu) settings, it’s nice to be able to load only part of the tensors on the various models. For BLOOM using this format enabled to load the model on 8 GPUs from 10mn with regular PyTorch weights down to 45s. This really speeds up feedbacks loops when developing on the model. For instance you don’t have to have separate copies of the weights when changing the distribution strategy (for instance Pipeline Parallelism vs Tensor Parallelism).

Licence: MIT

Metadata

Release files for bintensors 0.2.0

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

Source distribution (sdist)

Source distribution for bintensors 0.2.0
File Size Uploaded
bintensors-0.2.0.tar.gz 65.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bintensors 0.2.0
File
bintensors-0.2.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
bintensors-0.2.0-cp38-abi3-win32.whl CPython 3.8 abi3 Windows x86-32 Details
bintensors-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl CPython 3.8 abi3 Linux musl 1.2+ x86-64 Details
bintensors-0.2.0-cp38-abi3-musllinux_1_2_i686.whl CPython 3.8 abi3 Linux musl 1.2+ x86-32 Details
bintensors-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl CPython 3.8 abi3 Linux musl 1.2+ ARMv7l Details
bintensors-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl CPython 3.8 abi3 Linux musl 1.2+ ARM64 Details
bintensors-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details
bintensors-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl CPython 3.8 abi3 Linux glibc 2.17+ IBM System/390x Details
bintensors-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl CPython 3.8 abi3 Linux glibc 2.17+ PowerPC 64-le Details
bintensors-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl CPython 3.8 abi3 Linux glibc 2.17+ ARMv7l Details
bintensors-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details
bintensors-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl CPython 3.8 abi3 Linux glibc 2.5+ x86-32 Details
bintensors-0.2.0-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details
bintensors-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details

Total release size: 6.6 MB

Release files / bintensors-0.2.0.tar.gz

Download URL bintensors-0.2.0.tar.gz
Size 65.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2168d48ed2a1242be5aa62165c7cd864f6fb03b93e8ffc795f02daeb9c9988ac
BLAKE2b-256 checksum
How to use checksums
786956231adcbf55af8074c0d842c3adb84807f776377773c2a315e790213aa9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-win_amd64.whl

Download URL bintensors-0.2.0-cp38-abi3-win_amd64.whl
Size 274.4 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
eaecdf8d8d9a2d4a55376c224f55fc4014099f8bac30889c91aaff50e023fac5
BLAKE2b-256 checksum
How to use checksums
1478a96d1fcd133e8a22512a6ec746c91e0ce097c8fa49d138508bd1c43cdccf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-win32.whl

Download URL bintensors-0.2.0-cp38-abi3-win32.whl
Size 264.3 kB
Tags CPython 3.8 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
eb387e85b4744b4c4449b693d1070f95ec93c83f2feab3344cf05c23132250ad
BLAKE2b-256 checksum
How to use checksums
2be77b5a0ab8b698bc7b82db9201e9e728669191ba4ec6e4452e4a1508d1db09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl

Download URL bintensors-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
Size 599.8 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
0d255403cfbb2d2ac0c7f99a62f19709a74661a9f1ceb27ede20f2bc26434894
BLAKE2b-256 checksum
How to use checksums
f4155aeddc607179ac77367c63d618189c1f4353046889d9d2c0aa087cf56b9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-musllinux_1_2_i686.whl

Download URL bintensors-0.2.0-cp38-abi3-musllinux_1_2_i686.whl
Size 633.5 kB
Tags CPython 3.8 Linux musl 1.2+ x86-32 abi3
SHA-256 checksum
How to use checksums
7bea2372a6ea15aee011eb684ad2e5ee4a6068bf282d6fdd7075f554c15219aa
BLAKE2b-256 checksum
How to use checksums
001cbc6a31fb2deed350f4b2435f0b623a284da175dd9d95f1d44a0a816aa202
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl

Download URL bintensors-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl
Size 696.4 kB
Tags CPython 3.8 Linux musl 1.2+ ARMv7l abi3
SHA-256 checksum
How to use checksums
4d32f14d1f23dce9bf2e6f8f4a9b9ef91a51c9d3c5e72eff04ff6fb08d481ba2
BLAKE2b-256 checksum
How to use checksums
2f1fc2ee8cb4e48e315db5a0314189e2ff6b457ebc3b6025208a723f0c56daef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl

Download URL bintensors-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
Size 601.4 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
8c7c1c4a813017e7bc9e32ce3d7de6a59b45dacf1dc9c87a33fb9a89d3e66955
BLAKE2b-256 checksum
How to use checksums
34277b1b9c9e005f2111ef373d07a07b7a9386a9ed6aa18f9cfe2028856e7c11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL bintensors-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 429.5 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
b73314c815c03378c373e0606868f8853e59ae782b822f493b63ce6d1cbca3a9
BLAKE2b-256 checksum
How to use checksums
4aea03ac75760c56ad72061e1addef93ebf5bcdda2ef43b3ca3f572368ffc4c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl

Download URL bintensors-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Size 479.2 kB
Tags CPython 3.8 Linux glibc 2.17+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
ad8314210da0e00e28a31e864d977e529c7702e3bbb792a0cbc9106e6fb095ec
BLAKE2b-256 checksum
How to use checksums
d5f150156b001ee5fc0189dd9d45a1d12fa2770b5bfe2e95ba388f9a8c7c07d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl

Download URL bintensors-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Size 496.4 kB
Tags CPython 3.8 Linux glibc 2.17+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
90bc087e1960c562be155dbe32a3b3eef3100564ba6196091600de1076304b2d
BLAKE2b-256 checksum
How to use checksums
cbf77f2ccf1a69c61bbea779f4e12bc210ae4a12f1a1c4e61aff2110d5098ccb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl

Download URL bintensors-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Size 434.3 kB
Tags CPython 3.8 Linux glibc 2.17+ ARMv7l abi3
SHA-256 checksum
How to use checksums
d83734c4155f2e338cfa5dc5e6b47d6f502f5b4dad55a43586466e6688eb019a
BLAKE2b-256 checksum
How to use checksums
7894b406d9762e9f8e2138eb91e34b9e4d75cebd90dd9f9432aeb153d10b2a98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL bintensors-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 424.0 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
004d26702cfa5db0de70791bb9f6e50f2cbf2159c511258e9b69afe959ad3355
BLAKE2b-256 checksum
How to use checksums
ec4b879c6528dfa680035611dc2f945f6e453feb510ae61719000fb50188d886
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl

Download URL bintensors-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Size 461.7 kB
Tags CPython 3.8 Linux glibc 2.5+ x86-32 abi3
SHA-256 checksum
How to use checksums
16478928bb89bda075140f7892507515286c9e7bb7fa97c16b0b85d7f81cc8a0
BLAKE2b-256 checksum
How to use checksums
50bdd19d1aae0c0608406377e208cc7316bb9a4e8ac39e56c7f23b01dc11aeca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-macosx_11_0_arm64.whl

Download URL bintensors-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
Size 384.0 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f3cf86dd5cc19e97c25561923e70c76cc61578ae6b2aae082309e5067cd0e9d7
BLAKE2b-256 checksum
How to use checksums
4f347b68a7b8aaae72a5a625ac5602d944bdbd3e066c0e9fb83d809ef4cb0bd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release files / bintensors-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl

Download URL bintensors-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Size 401.3 kB
Tags CPython 3.8 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
25e1f0a7f06443d3cffdc22870c98385586efe010e8e65500e2985675aa2feba
BLAKE2b-256 checksum
How to use checksums
d2d664fd4673f84c9dcc9a0c3beb434d15ed4856edd3a111bb8ad0816c3d40b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.3

Release history Release notifications | RSS feed

This release

0.2.0 This release

15 release files

0.1.1

15 release files

0.1.0

15 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