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 bincode/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.0.5.tar.gz (59.2 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.0.5-cp38-abi3-win_amd64.whl (261.8 kB view details)

Uploaded CPython 3.8+Windows x86-64

bintensors-0.0.5-cp38-abi3-win32.whl (253.0 kB view details)

Uploaded CPython 3.8+Windows x86

bintensors-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl (586.7 kB view details)

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

bintensors-0.0.5-cp38-abi3-musllinux_1_2_i686.whl (620.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl (685.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

bintensors-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl (589.1 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (415.0 kB view details)

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

bintensors-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (463.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

bintensors-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (484.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (423.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (410.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.0.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (448.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.0.5-cp38-abi3-macosx_11_0_arm64.whl (369.7 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl (388.6 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for bintensors-0.0.5.tar.gz
Algorithm Hash digest
SHA256 b7ca746daf03d311a93fb9ebb9dc1c2c8fe62d5d1eed3e84ef5dbf1336e31ec5
MD5 3dbb7ddff37852c0da0e165a6db1baba
BLAKE2b-256 f6182c70ecd806faa03fb661fd1419f2a0797c2cd541fef3e40657ad82e78c42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 268eadb59cf746fecf3b472da8b64dcbc686c40861d683fc9ed8d6753541694b
MD5 dd10b6827ff8508f16954d44d302163c
BLAKE2b-256 c8bc21476dc18843dd483b18e70d13f9cc9e080aa67a08be47dc835de9484083

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 5bb7459a05b8db2c51354fa50e70f00fdc84f01797cbe2046b700085ed7cd554
MD5 eade638f9a6ce2bde619d5fd02412354
BLAKE2b-256 4211ef5aecdd41a21e32301501c503bfa0133772552260d1fd4c6185430030ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 09b817b02569000ac29c1d67609408c0e7c463962e881d4138e3d02fe980ff2c
MD5 92575fadc4e3f12004d700376397b20d
BLAKE2b-256 0a26e9f65f8bca39b23fd177d3b9c55ec384f60fe86b6d96e2b305b99f01f30b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 15497e639f900bce7cedcc257069e57b816a27531b13eabf6a9ba982e1fe8b32
MD5 7b00fa654e02432394d243f267b79969
BLAKE2b-256 d278df177b2b533b27e2b6b57188f1d145ccbb7294b41e80d456d69ea31f04c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 ea136f6e73973f1ebd9fc4e81528bb0efd10e3d1dccf1cd3e40919433989cf26
MD5 86dfdc25e1cde597cb019ff118e0d7d5
BLAKE2b-256 b32766eeb3f6a67f0867cba43c53551682e528b07370b6c2d2498c7b26e0f3e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 e68c76d7765c112aed2c4eeefa43e4f725cc124ab2d50b6ee759ed409f7af7f4
MD5 7928acbe1ac69d3fae2292294e24c327
BLAKE2b-256 14e9a2b3e0f2eb6e7768c41ce2cb7712db6482056bff0f0aedf660f65de670ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aa1e23855b5940cbfc20b41d2d5dc8e8edd0065474350e3d81a6d077c51143b5
MD5 6f7772c9b572c9387623a43568eed4b7
BLAKE2b-256 5c45c1aa897d9182ec01e642a7dec1023f281d2fdfd73a3e7dc0372d3b46223a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 964710b85d3e0a33b92015742fc1ecc55eb22b0c5337b34bfbb2f2ecb52f4fb4
MD5 d24b8f15dad461ad197ec3152a101c29
BLAKE2b-256 28994da5fc57fec67e1e6e74df0fcfd491acb8f4173da11d61d87ae26d17dc5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 3916795f50709ea60a94422489ae6b5064612b3d105700844d17626416a030bb
MD5 33e393d2ce228ada5dbe4e82acab1c06
BLAKE2b-256 96e323becab5e77d10c2c3e7917af9f8236ed22fa57f7d9be69f122a2fca8206

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 397765e9c21b291dac1c1d792fe7f6f909a535e060c554c5fbd2ea5ad3768937
MD5 b4a1bf2f99aa4277a6870bd330815d57
BLAKE2b-256 70d6fd90b88076dec8b09758f057f756e68b92a376704680ebf450ee1793b700

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 948b3b471880306f3949829d390bcea4532dc6fa78a0c2d30d042e9f7652a17e
MD5 21ce886bcd431815509c1a2d0c72cc5f
BLAKE2b-256 bc7a9b9f2c10eb8af7098d750bfa022126783c15466ca2fe37bdff3c0701246d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 e484338ba4ed7f97f7344fe383f96a2ce0c374f3e830fa8907eb2280e16c9969
MD5 a475b8dcb3925af2e30919272fb205e9
BLAKE2b-256 9de5a8ac8529f1c77eb35f4520c6f6440d1346d6133f850a1c6789a421747239

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4b72fb540059a86f49b642e5047bfc763cb6ff92b170007bf55c967be71fee34
MD5 4db4bd07c26a126f0be1124d2d241a98
BLAKE2b-256 487d8775fceb87c6390c2763df421cc098615806879894885f8e0a8503ce5e2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1938693c78ae37d139f27635780ff1266ac74122577fe7ce9fea2472ccbea9de
MD5 85619a3b0bdc24d5390780d722437c62
BLAKE2b-256 963155761b83ca95b84fc9fd514304161da2c5a634dd8d676e9f65e96b3c5168

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