Skip to main content

No project description provided

Reason this release was yanked:

Readme.md not rendering properly on pypi

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

Uploaded CPython 3.8+Windows x86-64

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

Uploaded CPython 3.8+Windows x86

bintensors-0.0.1-cp38-abi3-musllinux_1_2_x86_64.whl (583.7 kB view details)

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

bintensors-0.0.1-cp38-abi3-musllinux_1_2_i686.whl (616.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.0.1-cp38-abi3-musllinux_1_2_armv7l.whl (679.9 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

bintensors-0.0.1-cp38-abi3-musllinux_1_2_aarch64.whl (582.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (412.4 kB view details)

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

bintensors-0.0.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (460.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

bintensors-0.0.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (478.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (417.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (403.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.0.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (444.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.0.1-cp38-abi3-macosx_11_0_arm64.whl (369.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.0.1-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.1.tar.gz.

File metadata

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

File hashes

Hashes for bintensors-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d2078d69dbe44cc44545360e6256a8000e2fdd50cd6f6434a3fd28edba22285c
MD5 e2a6a0fe2f667e76f5b94a03b929b900
BLAKE2b-256 4c272c5f735228ec1ee5e2da74b5257251ca66fdac238ae4e23f72c8e6d77056

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 32a51e9a5e05eaa637a3c41214699be9c113e71b1602f9dea5fefb845ac773f9
MD5 7dcd2f101f723d4576d6e174d5fe7d74
BLAKE2b-256 26014562cb02bd345e39cc5983e6ffb76677368bd27696b0e5581df301b92b7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bintensors-0.0.1-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.1-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 e04a3a114b270d36109ec126a9dc275bd9f545e8d3117596b1705a8d97c5c4b6
MD5 8f2ba6d4f8b69a33f644023dadd93812
BLAKE2b-256 b7fd928c0eda8755cea68a3b106bc1655954aad653624d74034e6d92634ce550

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ba255fe5ca3651e05429d65d15fe68d862f5c36b61d5b64c0e78e9b37130b454
MD5 afc88f093b5daf151cedb8f359dbec8c
BLAKE2b-256 7ee9896a9eb138618d40aa62eaec8f0dccb5162cbf323a250f85485098523886

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 bec75004ca3320357a67bc86e567538124a530e0bbda5922af01435f753a675a
MD5 963d47c839b6bab5d4d84bf717655757
BLAKE2b-256 f0a440164a297bc382cfa68fb2aa524474d7a3f933e0af1763b82de2205a1cf2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 2ec36bf0e674b3797a9abb78977ee3f0726a2f9d80f5c6fc7d56139aaa426e16
MD5 3561c8411a1465897a0666db9b01f87d
BLAKE2b-256 c2ca9e6c2bd0b6623a8a88e02760eb6146d94040f407ffbb993787ece174aa01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c490b097f72589cb411fe61671e050b99738cc2d1248066f49fe0883ec773c72
MD5 c1bc766ebe845a1283b32f6eff4a1567
BLAKE2b-256 bc53ea52259deceeb0e8a4da74140e4c2c0a5ecee8a350b1b2c1e062ef69ef47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3699485c5602fca2e7888ef6bc4ceb738860299da385c8060805fbefd3f3229e
MD5 eb9b465511e2208d56e4aaeed4d9701d
BLAKE2b-256 c02a05f90e7cba2a6c3dbb87233e40595cdad737aee67e7e88a10f226fa22433

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 45885c7a7036e1f6044ab43f7f55ba055b9392d2fd365e21f10be660a01e2658
MD5 5edbd17c6e6fc60bdc4989d67322236d
BLAKE2b-256 a56c2431dd295ff48f94b545a6d9e69f49d5c0f461de4c3bae666b4a51733647

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 895926435ed9b855a8178a30c2225af4ada7d8ac3dca7b3458479a95fc6b204c
MD5 87ec99864e9032777e237b1dff7a34ce
BLAKE2b-256 f176bba41f5163d2362a5718abed699eb5628f50094c4fe4ff22400f6b49e1d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 52d8c5b6585a18a07fe30c210a0bf9262c8e72ee5888759a185b24a4a350924b
MD5 28c438d7b30589687a4d5377201cebd7
BLAKE2b-256 b8ee8a9eba861630d7c0dfc8d8ecf1c1f96903514d51c46374fa4cb831bbd83e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 86852ef42e0db93081b647b5c5d52e6552bd4d1f756044b86bb3769b9321ff2c
MD5 adaf0a9718c2b15bf1b91e854e7a8042
BLAKE2b-256 ca08e8f3fa8a854099b5c21a9d25f29247ee50592085bfe340889d6839fc7279

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 b92522436c1757b54346bf19774af6561bf06a50c58e5f8f9f5158e7494babe8
MD5 4a689ce83639c7ccde31dc6b306a9b6e
BLAKE2b-256 c69f04ca534c3555cb6721675411b7d62c5363bde954b3beccce0dd1e435a02d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a295990264d887703f2cbff218c7d9bc3f068e4dd9f9308e15f829e2b3177a07
MD5 92229a1297dc9d8c3f3b0173d74e306b
BLAKE2b-256 fc3244b124e9cf27154f103d800e32d0d47e81ff672c065492310bdda4ddf75c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.1-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 593a7cf5e9ea84b8dfea6e858436fa5bb34f602323c29b1f884d32bfb3a7cc38
MD5 de9b06f5fb1f0a5d5aedc803201e3d67
BLAKE2b-256 d19302fe237c30e3aeb31e12c34474dbadaa1dd3575ea2b1965858ac9a229f14

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