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.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-cp38-abi3-win_amd64.whl (269.9 kB view details)

Uploaded CPython 3.8+Windows x86-64

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

Uploaded CPython 3.8+Windows x86

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

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

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

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

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

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

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

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

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

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

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

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

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

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

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

Uploaded CPython 3.8+macOS 11.0+ ARM64

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

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: bintensors-0.1.1.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.tar.gz
Algorithm Hash digest
SHA256 e45f998eb9ffe570b170f266d6392b8f8cd8964a02651cfb9f6d19bf8a428af8
MD5 4ed0b54ee406ec2ac2534ead34066433
BLAKE2b-256 b1fd1028f75c256df6414db255e86125cdebb7c4d3298fe6b48f23b64148f498

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 edd4bff3bdb639d65ba1fd59e1f29ef3aa8e171451bc1823a072e887703d0eb0
MD5 5da3ae62fb128eac88b032693dc5d693
BLAKE2b-256 71e401bdcd910ba99dacd5e68974df7d99b8778dbc35a6ed35d2ccd6ac2f1702

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 1cb03c2161dacf13d1924368fca0578a3f8746b9bf8978fd29f5c533dbbff28f
MD5 6c28fc4f84673b6726088c0c3934f8a8
BLAKE2b-256 078a5662e1da9e0dcccbabb52aeba9a0b0a48732611ec8565ba439bb5373aadb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a81338bfb53a09778b1747bc06552a992c8ffd37d6064f01da3cb33d4018e1fe
MD5 79753f37d2b60421523f0da7b9cb6a80
BLAKE2b-256 07670295dbe1db111178d5b62db73083bbc4e1560344573f05b78531a42c2fd1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 addf00dd2b7d54711536e85630644f98fd26e29be041cd130970df6191a20d51
MD5 4a7917ae756855e85569e9bf2848e87d
BLAKE2b-256 847a1a3c8f18985190958b6b53d04da60823bb91376d09f7938fd556fdec85d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 b461a0c65f4a1cf35f88e87ad964048ff13ebe1fa8df2f2329c44b4b64bde12b
MD5 ea85fc330ac2a4cc9600c6b4630bf2ce
BLAKE2b-256 cc85841cce6eeeeb41cc7818fb5f23c7793f474d43aa4d94f6757b3e4aa62ac5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 9fa5384b3836ebfce9187c72d269c67af6e0e42f8fa76a85b2a317ec9141778c
MD5 2aabceedca8e083543dfa531ad849d15
BLAKE2b-256 21bfc46d84f08d18816d7154fc0990385a99598316a69b1e91d723401c7e7c80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dc42654a744658d3d149181d16767c8d3e5473748a82a4dce66be55b12608039
MD5 efb25f518e9e75fdd15b6139f57c99c4
BLAKE2b-256 811d979aa05f0e9aaea94476c5a12c8d26dd839bde82460bba69092b6e9000ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 ddb794c8639c2858e3fbdaee39f032b9aff79c170ce0a7814c8f51187aea1aea
MD5 f4ca083b317822ddabd60793431552e2
BLAKE2b-256 513b956d5a750ec241606cf836d11afece33fdd8fb1276e78d6671305b4a613d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 88f59122fb6915b631ca46074c8679fe2264d606c64fad0826a44cc3910b7b29
MD5 18c897146525feb87b184b54246693fc
BLAKE2b-256 a2e6979ab62ec24706a036d71371776c1cac726c382a30f56d212ed8799c0d8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 1a016adf2d3dd2f27c43f8cbbc760e0d7d1597d2cae7f6c2afac8623a287fc68
MD5 793a51ab7b95f9b730a1a0cfc26e8fa8
BLAKE2b-256 9d786ddeb25dff27e022386f693a4182d07b3ea5297fc168769750e35c2e53de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8db0aa8987478e273aba0007a0f375aab7840639d7a9e18f6cb3cedbdfeda476
MD5 1227959abcfc14fa21bd7e5044231f18
BLAKE2b-256 72fc256edc4d90d80bef79682ba279bd7114765dc6e4423b44490583c1cbf949

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 7654e57f5ded8c6208d7322e372602d9b2806c737216d277622e02e120713e76
MD5 49843f93111f967c04037e6a4c6dce0a
BLAKE2b-256 e211e38350613d6e7bb767bf78f15ab6fbd56cb1e4ff675e004e88907fedd958

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 da1f423c10b7979d4da6fab0b7e73d7436a99cba44359470c257b0f92d3a60b5
MD5 d627e621eb60034cb8fd975cfd3a4d50
BLAKE2b-256 642def7912db1d0f376f146b469a92efd44c210220e1e6ec2120098ae93f6588

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.1-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 39703b26ca180c80e2060e0cd7356a0c5b53725380bb3417512b8510a2890101
MD5 5ff7182aa916caf9d05c28b7e40eb16e
BLAKE2b-256 8919b67d0127b4a4eae2027205d17c596c99687fe51e1975da963da8413ab2ab

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