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.1.1.dev0.tar.gz (59.8 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.1.1.dev0-cp38-abi3-win_amd64.whl (270.0 kB view details)

Uploaded CPython 3.8+Windows x86-64

bintensors-0.1.1.dev0-cp38-abi3-win32.whl (260.0 kB view details)

Uploaded CPython 3.8+Windows x86

bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_x86_64.whl (595.7 kB view details)

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

bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_i686.whl (629.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_armv7l.whl (692.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_aarch64.whl (597.1 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (425.1 kB view details)

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

bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (475.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (491.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (430.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (419.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (457.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.1.1.dev0-cp38-abi3-macosx_11_0_arm64.whl (377.1 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.1.1.dev0-cp38-abi3-macosx_10_12_x86_64.whl (396.9 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file bintensors-0.1.1.dev0.tar.gz.

File metadata

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

File hashes

Hashes for bintensors-0.1.1.dev0.tar.gz
Algorithm Hash digest
SHA256 03e91d40b2b397202007a5bfb7b5aaca7d3b2b47289d05b9d37e69da3684bc1a
MD5 17c61ca9ac37d6d421177f4aa9773d9c
BLAKE2b-256 e8da5fbf7d8720c316c0388ac0133e0a5a93f12ef46ea66672e399ac1405f465

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4ec7f8dcd3c43554dc1c4422b38dc2cf07487fa350e5b0cba38df0fbef5404bb
MD5 380bc178f781186e08785c2dc18611bf
BLAKE2b-256 0ba322d08309c01b2f877c3a2218fa8c7d8dc1d0156318b94f9d84938ba8e1cc

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-win32.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 43942c8b6059f67a1fdff70c251edf23101880ed1053f76e8259107d0bf0a32d
MD5 4f33dff9347d46885357ff2792f1180b
BLAKE2b-256 67203da290cb95081a78094fcbd03ee478c6e959123e2451c949af17d4bffc9a

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 419bc69e668f64b50fd7360576951fda0cbe5db14e0c7df447e3fed2480805b6
MD5 3b46020fd60c98a86ac6bd6a069150ea
BLAKE2b-256 25e4b57d1a931f3adaaba4f131e8cf32490dc758fcf58bc82f7a82fdcd207fcf

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 9d9fc40af595f90de0b81e0ec474abe8ec06864aa691a4f0d01663586cb945fa
MD5 db5ac8eb906a718440f0338ceb161f3d
BLAKE2b-256 5a3fba828a80cd423ca62393c4e4ddc9dc0162c8c11b2563614e509b1a7fd131

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 7cf4b105d609c019a89a63c54b7158607a348c202bb8bcc35fc84e0754607c3a
MD5 abe2a7f27f9801814649dce4ca9200bc
BLAKE2b-256 221491a158c2ed78318a24d085ad6d0bf2f4e7d9e993a997dcd77f5bb01352f5

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 769efcec8a117943a1146f712c4673e63f5f3d84f795cfa2367c8cccd1b89c6b
MD5 ffd67548d6ad50e846d1da1272841362
BLAKE2b-256 a3754d99de54755d2472898563bc271dc136a27c374347ba4e9eace3c97ba046

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 28925491f0d2b21ad98387d14145622a4458068852024b0bab1bcc8529189e24
MD5 a1a1cfee20de969c31f974c5df44c5a4
BLAKE2b-256 76b4b8048dcfbb47eadd29b974bed5208fcfda1bb6882747331776c7910830e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 8de8a7cc4e596b01f55ada6eae2cf135a5563c47654f82ab04036cc2398fa8fa
MD5 7dbf97240510c8c9ad961024381314f3
BLAKE2b-256 c2219737fd31f672a2ca361f4ca5b02e5a0785a51672f1e07c1e7e45f7d551d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 de8a9457df83c4b7a8e40af812342d8b354e56f80f8d04d7b66b9cc2991ec39e
MD5 388c2802547b37072c3d2406d54d92b5
BLAKE2b-256 d92286450ea98fad3e853e80a1df56f06b00a2b6b5756bc95e69490c68f33255

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 68c4ab974ad813fc011caad389c7d6f8674cddbcc44b78b6662ff25476fa2086
MD5 beff996bc8adebc96b3deb0c13bc28c0
BLAKE2b-256 2a1c7c15b17912019dd6d104cb9e62e7c8def867ee2fc6212bf0ab714e9345fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 899a32cd2c056492a90ef09e72e21432d270fb0eaf49621e9e6f7f99c11a9d27
MD5 51c42512571230afa8fda602022ce0ac
BLAKE2b-256 bdb3cbca2f016a942691d3a8eefbee836aac776a1703dd1c060fed9e039493d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 71f8f1bb6dd29ecb25ac555ee81ab4f10e5015c9099cc1306b007b7affe9593e
MD5 fb278ab619af23365bcec313ca2cb05d
BLAKE2b-256 be30aa20d7ec4934262b2ce8943604e216d9d0c45215fbb0f8c7ecd757f4f16c

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1a0a85eba1ac09a10f73dd0eb707e2391faa6d70d25c216867a5af5044a5cb9b
MD5 19c692ba9b3a9b70860ce5b76f322a9e
BLAKE2b-256 e693809c08be8d2cff959e86b3cd77f4abfae9f53365135716e6bf25b05f808e

See more details on using hashes here.

File details

Details for the file bintensors-0.1.1.dev0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for bintensors-0.1.1.dev0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f32efa52932e125b55488db0190fa7b3a43bdc7b6b198fdf79b8d905e0e6b5c7
MD5 fdc2f2f8b8b10e38a85e5fcf554bd3a1
BLAKE2b-256 c585fb88a95ff5410fcda6664c5c8442e986b373c2c6426106bbbe23ca6a87d9

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