Skip to main content

Another file format for storing your models in a binary encoded format.

Project description

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

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

bintensors-0.2.0.tar.gz (65.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bintensors-0.2.0-cp38-abi3-win_amd64.whl (274.4 kB view details)

Uploaded CPython 3.8+Windows x86-64

bintensors-0.2.0-cp38-abi3-win32.whl (264.3 kB view details)

Uploaded CPython 3.8+Windows x86

bintensors-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl (599.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

bintensors-0.2.0-cp38-abi3-musllinux_1_2_i686.whl (633.5 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl (696.4 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

bintensors-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl (601.4 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (429.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

bintensors-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (479.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

bintensors-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (496.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (434.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (424.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (461.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.2.0-cp38-abi3-macosx_11_0_arm64.whl (384.0 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl (401.3 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file bintensors-0.2.0.tar.gz.

File metadata

  • Download URL: bintensors-0.2.0.tar.gz
  • Upload date:
  • Size: 65.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.3

File hashes

Hashes for bintensors-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2168d48ed2a1242be5aa62165c7cd864f6fb03b93e8ffc795f02daeb9c9988ac
MD5 e327715dace84ec83ced682d01f0cdd1
BLAKE2b-256 786956231adcbf55af8074c0d842c3adb84807f776377773c2a315e790213aa9

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 eaecdf8d8d9a2d4a55376c224f55fc4014099f8bac30889c91aaff50e023fac5
MD5 653c5d7536319949ad406eb1c43b2772
BLAKE2b-256 1478a96d1fcd133e8a22512a6ec746c91e0ce097c8fa49d138508bd1c43cdccf

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-win32.whl.

File metadata

  • Download URL: bintensors-0.2.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 264.3 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.3

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 eb387e85b4744b4c4449b693d1070f95ec93c83f2feab3344cf05c23132250ad
MD5 1bcc964a17725cda15fd2da2f6aab69d
BLAKE2b-256 2be77b5a0ab8b698bc7b82db9201e9e728669191ba4ec6e4452e4a1508d1db09

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0d255403cfbb2d2ac0c7f99a62f19709a74661a9f1ceb27ede20f2bc26434894
MD5 fbc79b8c8765c444d79b809a341774ce
BLAKE2b-256 f4155aeddc607179ac77367c63d618189c1f4353046889d9d2c0aa087cf56b9f

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 7bea2372a6ea15aee011eb684ad2e5ee4a6068bf282d6fdd7075f554c15219aa
MD5 a30e5edb99aadbc245fb063e5533db88
BLAKE2b-256 001cbc6a31fb2deed350f4b2435f0b623a284da175dd9d95f1d44a0a816aa202

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 4d32f14d1f23dce9bf2e6f8f4a9b9ef91a51c9d3c5e72eff04ff6fb08d481ba2
MD5 4f84cb28196a08fc34971bca3892beae
BLAKE2b-256 2f1fc2ee8cb4e48e315db5a0314189e2ff6b457ebc3b6025208a723f0c56daef

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8c7c1c4a813017e7bc9e32ce3d7de6a59b45dacf1dc9c87a33fb9a89d3e66955
MD5 e9233618797d9569ae5982abb02bdd40
BLAKE2b-256 34277b1b9c9e005f2111ef373d07a07b7a9386a9ed6aa18f9cfe2028856e7c11

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b73314c815c03378c373e0606868f8853e59ae782b822f493b63ce6d1cbca3a9
MD5 4a8305e13a9d1f3b74e22ba1d3260d2e
BLAKE2b-256 4aea03ac75760c56ad72061e1addef93ebf5bcdda2ef43b3ca3f572368ffc4c8

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 ad8314210da0e00e28a31e864d977e529c7702e3bbb792a0cbc9106e6fb095ec
MD5 be06614a49ad77084ffd1afb10d51570
BLAKE2b-256 d5f150156b001ee5fc0189dd9d45a1d12fa2770b5bfe2e95ba388f9a8c7c07d7

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 90bc087e1960c562be155dbe32a3b3eef3100564ba6196091600de1076304b2d
MD5 1f8107c6d81127b41511885c239b0e7b
BLAKE2b-256 cbf77f2ccf1a69c61bbea779f4e12bc210ae4a12f1a1c4e61aff2110d5098ccb

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 d83734c4155f2e338cfa5dc5e6b47d6f502f5b4dad55a43586466e6688eb019a
MD5 39729cab4b6f221877fb70baddf43b54
BLAKE2b-256 7894b406d9762e9f8e2138eb91e34b9e4d75cebd90dd9f9432aeb153d10b2a98

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 004d26702cfa5db0de70791bb9f6e50f2cbf2159c511258e9b69afe959ad3355
MD5 d7e4c57449bdd1a4b0d83a74cf7846c6
BLAKE2b-256 ec4b879c6528dfa680035611dc2f945f6e453feb510ae61719000fb50188d886

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 16478928bb89bda075140f7892507515286c9e7bb7fa97c16b0b85d7f81cc8a0
MD5 a19b3c47a47705cdde595b0cdb74df6f
BLAKE2b-256 50bdd19d1aae0c0608406377e208cc7316bb9a4e8ac39e56c7f23b01dc11aeca

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f3cf86dd5cc19e97c25561923e70c76cc61578ae6b2aae082309e5067cd0e9d7
MD5 a719ed36e62c4c23e596238d9be30c10
BLAKE2b-256 4f347b68a7b8aaae72a5a625ac5602d944bdbd3e066c0e9fb83d809ef4cb0bd6

See more details on using hashes here.

File details

Details for the file bintensors-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for bintensors-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 25e1f0a7f06443d3cffdc22870c98385586efe010e8e65500e2985675aa2feba
MD5 3ffa968a524f6a72b65253168948d013
BLAKE2b-256 d2d664fd4673f84c9dcc9a0c3beb434d15ed4856edd3a111bb8ad0816c3d40b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page