Skip to main content

No project description provided

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.4.tar.gz (55.5 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.4-cp38-abi3-win_amd64.whl (260.4 kB view details)

Uploaded CPython 3.8+Windows x86-64

bintensors-0.0.4-cp38-abi3-win32.whl (250.8 kB view details)

Uploaded CPython 3.8+Windows x86

bintensors-0.0.4-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.4-cp38-abi3-musllinux_1_2_i686.whl (620.6 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.0.4-cp38-abi3-musllinux_1_2_armv7l.whl (685.1 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.0.4-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.4-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.4-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (484.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.0.4-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (423.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.0.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (410.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.0.4-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (448.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.0.4-cp38-abi3-macosx_11_0_arm64.whl (369.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.0.4-cp38-abi3-macosx_10_12_x86_64.whl (386.1 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for bintensors-0.0.4.tar.gz
Algorithm Hash digest
SHA256 3ab0d5c71d44fb5e265eb1180265df09b92757b1312315db64e0e4c5a5e44299
MD5 dc338e8e92343182e2ee9d935ea76c69
BLAKE2b-256 21c0ec3c620c73a0926f1bccb3fc3ff7ee09ca5338df47b12e4c0683d55057dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 21a2b0cf472da7bb8d3eda26323d89c0937afe30729eb3c4f70a02aed5410ea8
MD5 d5f0659bd5d79079d2e566aba8079b45
BLAKE2b-256 7c1f2322233afcb7111a3161d84873a35a6eec5165c7fcebc40277f7bc1e2a79

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bintensors-0.0.4-cp38-abi3-win32.whl
  • Upload date:
  • Size: 250.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.0.4-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 a5d483851a750695741af1b6d408095808f26f99d8d4fa48472756dfc6e3bf83
MD5 e19d120707a162a2f660bc37f33423fc
BLAKE2b-256 9f3c0a2430748c094ac8f42fc74ab2cbb20cc1f241568a21dafcaf4d8ae62994

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 800e887a1f65625d1f21db66e896c711410ee2166f503bb4e3d1c8fc71126721
MD5 12d28505363526beae2072bf0ddcd5ba
BLAKE2b-256 19bcbd78c816f1046f4aae3827cd3bb53864061dd173c827b4f63c086f76a72d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 967e7245a3db24d462c6fac3fc3b2368faf82e53af939200ac63ce7edfc8303f
MD5 c6dd5541203d3a7a11a38b9ff4ddc011
BLAKE2b-256 605497088c52f0a10a4b38628605b063a400deffac238ecce6acb6accb265da3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 206451e12a41fd1eb3e1bd1c2af34002de3fb2a05a1056e6bc923111e47099af
MD5 2f3f0425e03fadc7c4e69fae76864c21
BLAKE2b-256 3f2cbfc6d078129e417c7c24f1fe92eb1853605024e62fcb500c893612f85b82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 ef31f1b88e28798feea2eb49ac968d4ad9676e3e2fa2a2465d20e307d53c7003
MD5 c9cb5e16ddd23ada192c25ebad7fcb9d
BLAKE2b-256 5926013221d873e4f33d8057be45ed6013dd7310888f88a0e00af2ae7f10dcb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 687cf247f3adc9a05f3698df9504f35df023667f42ae7064d5e765e1d07372d9
MD5 3a0aa39b2ea6c2e50ffdb8c22f92ae6d
BLAKE2b-256 740d3ab584751c1bb6eeb293f79b059340f29a01901a9d51a76a5566534a12a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 6dd2d67853ec7579548ec4d10660082241d604d347bf3a0095ce387fabc8868d
MD5 151485dee8d8b218cb29bcf8405247d6
BLAKE2b-256 790371292e701967c93412b721b1cf914f42bbf6d93fd92b3313117ebd8785e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 450e2310a2f928c78d0678d948332d9f0110b98fe4ae46e46d132b559cce59ec
MD5 359d5f6325769c7ce536bbc1a57fcc65
BLAKE2b-256 4f8c6b07d3523c39dec470cb5dae93fd758433376452bdfa4fb4b70e18404d98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 4339c7ee12b4eceb63b3072f806ab6fad77a2b0eb423800dfec1681b0d8eeacf
MD5 b9d89146e3e1f28fc6d74c2df066bb76
BLAKE2b-256 3b893c1ccf19f8e98175bea53b3a54c7123a9211fe3e86de677f4dee30b4daa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8d6b6ee1a363c76aaa2d274ff584b6c90051e997db10e77b768875483a7a4283
MD5 549db22f107eac871f6fc9494507b69b
BLAKE2b-256 e8f791ce4e9b4929095f396460d68742a3d0dfa088c26808626103c0b863349c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 43b41f7ec1be2d3f0a6e537b4bf951fba37f2e4b95d54933812a152f76cc284d
MD5 c5997bad6d742af032ebf11c6dcbed7a
BLAKE2b-256 4f6b291a4cb3f45dc44dc52f373d2273d3a3147499368fd1df35e072ecda491c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b71c47431b9a19f9c791e7494e0887a9b743164e35a2f7cda97ea1f14827401
MD5 3f7f6fb7f99491632cf27b9e99142e57
BLAKE2b-256 6c3ec3885a50635283a67c19aa802ec613c43feac8cd0b244d2ed75c7e9948f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.4-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 13aeb4e1b2a34f572926e3289290df38884898c4bd0c1fd88e480bf23653c394
MD5 dc344f170dd6e53d6b70dba36c96110b
BLAKE2b-256 87daee11cf9e5ae351082665e31297272577e0d6c8550e0dd109fae0cf13bfc0

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