Skip to main content

No project description provided

Reason this release was yanked:

broken numpy module no such thing as np.bfloat

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.2.tar.gz (55.4 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.2-cp38-abi3-win_amd64.whl (260.3 kB view details)

Uploaded CPython 3.8+Windows x86-64

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

Uploaded CPython 3.8+Windows x86

bintensors-0.0.2-cp38-abi3-musllinux_1_2_x86_64.whl (583.5 kB view details)

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

bintensors-0.0.2-cp38-abi3-musllinux_1_2_i686.whl (616.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

bintensors-0.0.2-cp38-abi3-musllinux_1_2_armv7l.whl (679.7 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

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

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

bintensors-0.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (412.7 kB view details)

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

bintensors-0.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (461.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

bintensors-0.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (478.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

bintensors-0.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (417.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

bintensors-0.0.2-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.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (444.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

bintensors-0.0.2-cp38-abi3-macosx_11_0_arm64.whl (369.7 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

bintensors-0.0.2-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.2.tar.gz.

File metadata

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

File hashes

Hashes for bintensors-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6d4c3ebb186a9e94dc4dee11e1d759eca4af31645c3250b889cd7a0c3fc47c0b
MD5 91203fd5823b3900bb7af81d3b616173
BLAKE2b-256 59695eb08f50f92cf882d65957de7c743473813cad54bad8fd622341362f200f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 db8883f010431780d748793924bf4b5fd547ab1decdfbca188c27c8664961fe3
MD5 59504a266163c0ea157e0cec8c342469
BLAKE2b-256 21c311795e9ce5a81d5c798b4f3f0d123e2f227141593760252f3457bdd811d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bintensors-0.0.2-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.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 06961333ca1fd1c84a04452475176c387a898a8ebac23beb69d98aa7d99a1ed3
MD5 7c73c34c0c0f28a9eba9e45bfb78b868
BLAKE2b-256 72be1b738cb1de02596727f49b53bf0c87e9bcc885c158ba8d88a64606a25db7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 46f14d7699217aa8679e77023ffb6c934127d7e2430636bc95f28254f6c4e7a3
MD5 03b631246dcdde829390600e8b3971df
BLAKE2b-256 3227605d510ffef92d1d2cb6adb7538952c22665bd1d4cfb1a499d360384c807

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 39d6f035be6e95f4b2fe00dad439b4c8bd9c5b1dbed6b3175c50d0f19183e80e
MD5 2af0d4e0ab3c76dacdb86560eccac589
BLAKE2b-256 88fbaf0ee80903a28ae759f2c65faa5e9720e5bacde7d15adb0f26722a3e85ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 dab204bbdd51599d5bcfe11c997ce9757ff0a20f948bdc8b6c901c6bc1491c22
MD5 55af63c013f748cb1c6eaac688bd806a
BLAKE2b-256 c443691d7feddae79b3958543f2097de4d4b3c44bd87f9f4abbbf10d2d150fde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 6bb4746937a831d34ea6abf63d2abfdebbaf66bc7ed6376c018385dee057e429
MD5 433669bd05ec5da13e3e36bfe60b30cc
BLAKE2b-256 b690e87bdb7042e4f670c37cf50d329dd16118ec321b8136af818bc39b4f1e49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bc9e6e95b96fbf0e54a2e5b182c20145605c9540d144ccbbb4e1f8be5ed69ff2
MD5 4a17b9a736d96a5b20f4d1190ac8fa38
BLAKE2b-256 700724c1e5633f9eebaa367cba224bea6a1ea4aedc5ab05a09c005cd5b1bfaee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 229050ed39ef982d8694d9ef6ee534c5c91ecb0a5130fc9f1b35d696518ad91e
MD5 581ce57f3a72e34c922d1059a13d27de
BLAKE2b-256 8b0fbf3cf6c4f25b0d88d2028a6c670a523b82bb8375a487df74f406b02ebc73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 8fe095eea6265517e48bec232932acdec3ae34e61bf4f35e9a7ce567a560a975
MD5 20c00241e8084254b135f10eae0fcab8
BLAKE2b-256 66a5c08654d3ad90772186662fd8d027bbd87a9c44e855f6f637a6baf86a02e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 0e425e58843fac8bccda3ebaa205ce6897ea79c0df4e41f8f542cab72150496d
MD5 32c12bdcc20ccb138e2799f705bce5ed
BLAKE2b-256 353334c7e5528c2f6a8946f3cf2417d6580622e3c0719250b7dc0784b5d67eec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 409ee7370c2a04a32922404fd48b95720e8e52962cc7a00ad763e28f2647ae90
MD5 1cd64c4923577c7f00e86d884a87236d
BLAKE2b-256 3a362ccb6faf17b707a7efad06492617e434daea8cf96024e064a8641f3a15af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 cb7ca59711eb989101e23e7a95f207bf22cfd516c3b95c1d95dd4f74378c8f8e
MD5 8535867121e5da361da6a4e2bc52b431
BLAKE2b-256 faf6cbc4ec7a3144cac908808bf86618055c0acaab31e4899a23f258e0f63403

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eaac424f7517d1c66951da1dc6f431afb6b9224412393adf6b68b8ff7713d5b0
MD5 8191a2139c0b2ba122542636cb72afa4
BLAKE2b-256 8687d6af79bff08b65e203ded03610af3715e0c0f0dc7d74e51d3c6005fd4bde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bintensors-0.0.2-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5387b725439d2618c2dc08c68948c146ed2cd5f7a534da91d9567dc1ed917404
MD5 d93c38b46359160e218dab80c725f4c4
BLAKE2b-256 4202dcd1ad7181f5220b1b4da4a0c9aab17314c31ad9980bb1b14892aa186df5

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