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.1.0.tar.gz (60.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.1.0-cp38-abi3-win_amd64.whl (269.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

bintensors-0.1.0-cp38-abi3-win32.whl (260.4 kB view details)

Uploaded CPython 3.8+Windows x86

bintensors-0.1.0-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.0-cp38-abi3-musllinux_1_2_i686.whl (628.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.9 kB view details)

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

bintensors-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (473.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

bintensors-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (491.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (430.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (419.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (456.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.1.0-cp38-abi3-macosx_11_0_arm64.whl (376.5 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl (396.6 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for bintensors-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9cc43584ae56399796ceb5b78c7b64371bd83af03f1e89acacf8a49ba3cb95d2
MD5 07e0a63fcc7fa5c03a2de40b9a73f746
BLAKE2b-256 5c7c9d6cbdddc2c25d66f3c3d4e47613276dd8c0b3515a3d8413af80a6eaa137

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 61ad401f0bddbdebc267a361d1a7debd7512b41f0c07f52f83bba4a490023ad0
MD5 6e08aa0fbeb6cfa4679e2ba3b915f2ff
BLAKE2b-256 96b09852f7a3cff33e4118c08892ca7d4bd4efb09cd151a46192074935a14ee8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bintensors-0.1.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 260.4 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.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 3da7b7e1eab080aeffd3b842323bf33f67ebcd4530ecd85779b7a8959b30f76e
MD5 2ebb0a694af577073ab5ce42c75fb50e
BLAKE2b-256 91ef06f54c1a5ebaec9333aff2beea6d4860c07f1aaedb93dad65d3413921677

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b75cd28f6847e7905e762b2906f35a4a9a74d10013a670eec3bacd0993d07d29
MD5 06ed6b00ad4dda11017444093d65e7c4
BLAKE2b-256 e43781e1d9f6b0e036a6a3ddd04ac110367ba7d5f64412fd8b16e4268db92678

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 5abcb742778c48be644af72cdc85762592f9751af7610a037f76eeafc87b552d
MD5 5da4a2adfe95f1847e67c47bfe2bcbdf
BLAKE2b-256 eaa936566ab09c1bf7969147ac7423f1980614786fe9f46aaae46bda53cd3ef7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 ea0f357ceff170813c1520b165ebc33b213c8047e4bd50033a6e1cea9c401aad
MD5 eeeb74d0579ecafe4a1dcace66935382
BLAKE2b-256 a70ddde2f06f7a65f4a59ca49571655ea825b4ee725d701fa0d8647e86776214

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 6e10a63babb23383b3d184417abb0341f6dc2b387e147e5dc2757e3072d6d830
MD5 8f0bf615b3f8024380bf551977566e3e
BLAKE2b-256 cb5e343b118c4469f2bbc3309217daa35d0f47c7357b3c6cca74dabb975680f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 098af5cca072714d823031c65a01ad957487cfb150ac927a1b8cd7cbf58a1a88
MD5 eb7a7406dbb5ea8b7351ead98164cf83
BLAKE2b-256 cf5af75bbc2ee01ded275be58526dd0b3d79bde09ea80501fbee6636c7ca4bf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 525c5bc251beba43a8cbf0ca9fef9b87d46958edea9337722c6dc9f9dd184ea0
MD5 5ecb50d3ded9281153d8b7a8f83dab4d
BLAKE2b-256 0fdce87b7f3a74909b876e519dddadb33330e4200dce1109fbcb501ef808198f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 2e92f5a36bce9e7753ec34ac42a5f8c5285b5d9d74f08912b0186a4587d22104
MD5 ada3baa23b95375e91f41ca0c80e2b6b
BLAKE2b-256 e5ccb98d08b6f27e37fd02e80ca26ec003e48682edae2c64ba403a8cba87adf6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 bee3cd791345d6104811bcb85d3c9966db26c4e6d3ca6c4746f119ce36610265
MD5 0b80e03f750a3020f9232d03c353f4e9
BLAKE2b-256 98d617756ee6ca9f14b6a1ce370144728770a080f7e26bb495153a8dbca917b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f3988a36167e2b4c80aa8755937a65d8d56200fc7d779d604b845c208f86e01e
MD5 0f91b2ca67781854926fcffc47fb7f43
BLAKE2b-256 9c337471927cb25dbf3c6188148ed3eb717331e377c86cc0b08f6df045bd9b40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 4cbcb8ba7a4604052ac42e77f9fa609ab4a9e912a26fd996db8d6d73d1173276
MD5 4a216a02c84781834250c76a0b8215f7
BLAKE2b-256 c9b7233f76fe052d508adaaea10ce715ca1dc460b779e24e16bcdc1679aa273b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 45b9ebc56925273f8f8ad127b47d71cb54a5dd11fa2b3ba91abe4692dc148f80
MD5 69e10e2a5dc8113ea4b1813db0fc53bf
BLAKE2b-256 8e086399d86cd4164b94b81580307b537ef6da4ce75667445c45ab7b7b39a011

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b7e4f692a6fa63013758cc673af6cfe4b47d868aec940a8b29196f8d911c972a
MD5 fcbbe600e950b22f04963d8a553a475c
BLAKE2b-256 6d68d73861c6a293a0f661856818108e247b9a801d37d763eac1a2c7477ff6be

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