Skip to content

Commit 7c1b6ca

Browse files
committed
feat(slice): add feature flag and guards for std/alloc
1 parent 298195a commit 7c1b6ca

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

binding/python/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ memmap2 = "0.9"
1515
bincode = "2.0.1"
1616

1717
[dependencies.bintensors]
18-
path = "../../bintensors"
18+
path = "../../bintensors"
19+
default-features = false
20+
features = ["std", "slice"]

bintensors/Cargo.toml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@ readme = "README.md"
99
keywords = ["bintensors", "serde", "tensors", "huggingface", "pytorch"]
1010
repository = "https://github.com/GnosisFoundation/bintensors"
1111
description = """
12-
bintensors is a high-performance binary tensor serialization
13-
format designed to be faster than safetensors by eliminating
14-
JSON metadata overhead. Instead of using JSON for metadata storage,
15-
bintensors employs a compact binary header, reducing parsing time and improving I/O efficiency.
16-
The format begins with an 8-byte unsigned integer (u64), indicating the size of the binary header.
17-
This header contains essential tensor metadata, including the data type (dtype),
18-
the shape of the tensor stored as a length-prefixed list,
19-
and data offsets specifying where each tensor's raw values are stored within the file.
20-
An index map is also included for named tensor lookups. With its efficient structure,
21-
bintensors provides a fast and lightweight alternative for serializing and deserializing large models,
22-
making it ideal for machine learning, scientific computing, and other high-performance applications.
12+
Bintensors is a high-performance binary tensor serialization format designed to be faster eliminating use of JSON serialization metadata.
2313
"""
2414
exclude = ["target/*", "Cargo.lock"]
2515

@@ -43,6 +33,7 @@ sha1 = "0.10.6"
4333
default = ["std"]
4434
std = ["bincode/std", "serde/std"]
4535
alloc = ["bincode/alloc", "bincode/serde", "serde/alloc", "hashbrown"]
36+
slice = []
4637

4738
[[bench]]
4839
name = "benchmark"

bintensors/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
)]
55
#![doc = include_str!("../DOC_README.md")]
66
#![cfg_attr(not(feature = "std"), no_std)]
7+
8+
#[cfg(any(feature = "std", feature = "alloc"))]
9+
#[cfg(feature = "slice")]
710
pub mod slice;
811
pub mod tensor;
912
/// serialize_to_file only valid in std
@@ -20,6 +23,8 @@ extern crate alloc;
2023
compile_error!("must choose either the `std` or `alloc` feature, but not both.");
2124
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
2225
compile_error!("must choose either the `std` or `alloc` feature");
26+
#[cfg(all(not(feature = "std"), not(feature = "alloc"), feature = "slice"))]
27+
compile_error!("must choose either the `std` or `alloc` feature to use the slice feature.");
2328

2429
/// A facade around all the types we need from the `std`, `core`, and `alloc`
2530
/// crates. This avoids elaborate import wrangling having to happen in every

bintensors/src/tensor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Module Containing the most important structures
22
use crate::lib::{BTreeMap, Cow, HashMap, String, ToString, Vec};
3+
#[cfg(feature = "slice")]
34
use crate::slice::{InvalidSlice, SliceIterator, TensorIndexer};
45
use bincode::{Decode, Encode};
56
use digest::Digest;
@@ -731,6 +732,7 @@ impl<'data> TensorView<'data> {
731732
}
732733

733734
/// The various pieces of the data buffer according to the asked slice
735+
#[cfg(feature = "slice")]
734736
pub fn sliced_data(
735737
&'data self,
736738
slices: &[TensorIndexer],
@@ -818,7 +820,7 @@ impl Dtype {
818820
mod tests {
819821

820822
use super::*;
821-
use crate::slice::IndexOp;
823+
822824
use proptest::prelude::*;
823825
#[cfg(not(debug_assertions))]
824826
use sha1::Sha1;
@@ -829,6 +831,9 @@ mod tests {
829831
#[cfg(not(feature = "std"))]
830832
extern crate std;
831833

834+
#[cfg(feature = "slice")]
835+
use crate::slice::IndexOp;
836+
832837
const MAX_DIMENSION: usize = 8;
833838
const MAX_SIZE: usize = 8;
834839
const MAX_TENSORS: usize = 8;
@@ -995,6 +1000,7 @@ mod tests {
9951000
assert_eq!(tensor.data().as_ptr() as usize % tensor.dtype().size(), 0);
9961001
}
9971002

1003+
#[cfg(feature = "slice")]
9981004
#[test]
9991005
fn test_slicing() {
10001006
let data: Vec<u8> = vec![0.0f32, 1.0, 2.0, 3.0, 4.0, 5.0]

0 commit comments

Comments
 (0)