Skip to main content

No project description provided

Reason this release was yanked:

broken numpy module function save_file

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

Uploaded CPython 3.8+Windows x86-64

bintensors-0.0.3-cp38-abi3-win32.whl (250.9 kB view details)

Uploaded CPython 3.8+Windows x86

bintensors-0.0.3-cp38-abi3-musllinux_1_2_x86_64.whl (586.6 kB view details)

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

bintensors-0.0.3-cp38-abi3-musllinux_1_2_i686.whl (621.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.0.3-cp38-abi3-musllinux_1_2_armv7l.whl (685.3 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.0.3-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.3-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.3-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (484.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.0.3-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.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (410.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

bintensors-0.0.3-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (448.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

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

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl (386.0 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: bintensors-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 cdbb291cca57d953dad87058c562e10640c2031b11f5518f332546c2f99a0e3a
MD5 1332c53615df22d3547706db581ab4d2
BLAKE2b-256 813ba196cde9a799a3d1f45cde967e0b8fd6a40579a4c0e1278c8f72c6fd5c62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4034366a0917c25321d5b173273c07c6ce10679f7d8ffa8fbedd739844e2e16f
MD5 3ec656cbdb338c6042784f9aca019486
BLAKE2b-256 ec6152dcaf8d7c2abae8b0d0d8853d08de37155624481b1bfcdfc578499e95e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bintensors-0.0.3-cp38-abi3-win32.whl
  • Upload date:
  • Size: 250.9 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.3-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 819380a599d258fd9c12914d64f0b09d6be2262d4f3a2c7ec5ac80a4fa41b32e
MD5 a9fe43ad0656950cc2f765e13a8d9256
BLAKE2b-256 3ef246e20b4190593af587f8e9644fef4bbf378ffc16459a19288cd7b9ea6bda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 32a43e34ac3c027676d16238bf24a0de4365b418ba14f50ffb16801ac089b3cb
MD5 db18218e01a95a230f6b7799865b770a
BLAKE2b-256 f3a65c04a1ca522f698bcf40bb29709834c3a364927e1f2034df4faa4ea76ca2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 e93e8f844a3cd8be63f0fc1bcac4c4151cce0bc3f945e28f40610e725f0f7e1f
MD5 a98e774ba8121c43445ec2299d7d9a8d
BLAKE2b-256 1d7006efa715dc907743d329da805cedf11c69fa21cdab0a2abc9dcb4cff1701

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 0ab557ce0ce2376e93f5fb21eaa8cad66164c9d267bd760243649bd6a9705ba5
MD5 56281f1e3528ce7dc90a5dc2cd6f799d
BLAKE2b-256 4791eed6df0e1a5d4e8f1526ea55540f3c38212eab25c86a669c18770a564454

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 81f865c0840ac34403ab89b3cf44f0314d7113511467050e7e48a51b66146588
MD5 8cafc6d55c9c848720100e5fbe4661ce
BLAKE2b-256 d03fce1f85bf6f8bd4e09de7acb3bdf79e1d1895e627978151e161de7d3d2d39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bbe2572af46289d9d6fce5ab3d0b81690496917e0b2d1402554c4d26f0d019a8
MD5 8f03341927398dce3e642c0bf0770cc2
BLAKE2b-256 57ecb212b666dbfdc166e961e32d048281749c27bea61956c14af81067bde99c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 1fbce7bc64057bd579a34817d0f72228b96885752af51290f5170e9d95e15999
MD5 ce1ae7e7e54769293321d87e3ff317de
BLAKE2b-256 b598248c82ddcac3c03a8e2e5bfe4e33f92a48527de009c9b0d4e9ee48c18e51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 d2e5ba525111115060e37a3caf7126b53fa4f86984dba79211ae647448f70dba
MD5 8dec66b7e0f9b9fec832c41eeaf06c4c
BLAKE2b-256 9f3945215aa7f158afbb2c84b7af7c465ac783544bbc6da1f31dc72f7288455d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 db656f7dcd6c45dd19d8f444f796631e61f7100cd1677618f179f1d5098e6aef
MD5 2472c88e87dade30356307b715ffbe7b
BLAKE2b-256 a13751cab28acd5d4a0b92487f675902bc7a3087ad0b6f76b95f0654e0ab8a98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3440f09029d335e48c11f7a4433b592234d6cb52abe543cba09a71509005b922
MD5 a26fbb89c738e49060662608d0db278b
BLAKE2b-256 d08cefbec75dbe28ec369ea4438a692ad1e261826f08966f67fad03c58c10c94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 e4eaebc39ca122c2c0a821d7c779d750c788ab1c2316c6ffadd0b0ec847849e5
MD5 5bbd65b23f0a14cad216da9ca02207f9
BLAKE2b-256 9cfc700aa15a00a1187363a0300bf060b9f82dc21cd03fa36ad00c34ce47a5b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf72678af7274c7139b993c772fbe41007742cfc3536140497810b3f7e6d4359
MD5 6ebf92c07be63ef40ff5d800f48bb9c1
BLAKE2b-256 f6b2774d4b675b3bfa8c87556daa67139b8b5799ca7a64a85a268892a89c9ec1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9812d7c61e0ae8c4c2fe7b2620ba6d1c18ca21de90289815720079331b1ef128
MD5 4831971247b17b05bc6eae5431aaa665
BLAKE2b-256 1f52d03c5e7a5efe9442cf2019735e3cda3cc624ae0f157779fb9f98e8cbf258

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