Skip to content

feature: impl no_std feature build called alloc #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions bintensors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ license = "MIT"
rust-version = "1.85"
authors = ["Luca Vivona"]
readme = "README.md"
keywords = [
"bintensors",
"Tensors",
"Pytorch",
"Numpy",
"bincode",
]
keywords = ["bintensors", "Tensors", "Pytorch", "Numpy", "bincode"]
repository = "https://github.com/GnosisFoundation/bintensors"
description = """
bintensors is a high-performance binary tensor serialization
Expand All @@ -30,9 +24,12 @@ making it ideal for machine learning, scientific computing, and other high-perfo
exclude = ["target/*", "Cargo.lock"]

[dependencies]
bincode = { version = "2.0.1", default-features = false, features = ["derive"] }
bincode = { version = "2.0.1", default-features = false, features = ["derive", "serde"] }
hashbrown = { version = "0.15.2", features = ["serde"], optional = true}
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
digest = "0.10.7"


[dev-dependencies]
criterion = "0.5"
memmap2 = "0.9"
Expand All @@ -44,11 +41,12 @@ sha1 = "0.10.6"

[features]
default = ["std"]
std = ["bincode/default"]
std = ["bincode/std", "serde/std"]
alloc = ["bincode/alloc", "serde/alloc", "hashbrown"]

[[bench]]
name = "benchmark"
harness = false

[package.metadata.docs.rs]
rustdoc-args = ["--html-in-header", "./docs/katex.html"]
rustdoc-args = ["--html-in-header", "./docs/katex.html"]
32 changes: 17 additions & 15 deletions bintensors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@ pub use tensor::serialize_to_file;
pub use tensor::{serialize, serialize_with_checksum, BinTensorError, BinTensors, Dtype, View};

// TODO: uncomment when all of no_std is ready
// #[cfg(feature = "alloc")]
// #[macro_use]
// extern crate alloc;
#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;

#[cfg(not(feature = "std"))]
compile_error!("must have the `std` feature");
#[cfg(all(feature = "std", feature = "alloc"))]
compile_error!("must choose either the `std` or `alloc` feature, but not both.");
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
compile_error!("must choose either the `std` or `alloc` feature");

/// A facade around all the types we need from the `std`, `core`, and `alloc`
/// crates. This avoids elaborate import wrangling having to happen in every
/// module.
mod lib {
// TODO: uncomment when add non-std
// #[cfg(not(feature = "std"))]
// mod no_stds {
// pub use alloc::borrow::Cow;
// pub use alloc::string::{String, ToString};
// pub use alloc::vec::Vec;
// pub use hashbrown::HashMap;
// }
#[cfg(not(feature = "std"))]
mod no_stds {
pub use alloc::borrow::Cow;
pub use alloc::string::{String, ToString};
pub use alloc::vec::Vec;
pub use hashbrown::HashMap;
}

#[cfg(feature = "std")]
mod stds {
pub use std::borrow::Cow;
Expand All @@ -40,9 +43,8 @@ mod lib {
}

/// choose std or no_std to export by feature flag
// TODO: uncomment when add non-std
// #[cfg(not(feature = "std"))]
// pub use no_stds::*;
#[cfg(not(feature = "std"))]
pub use no_stds::*;
#[cfg(feature = "std")]
pub use stds::*;
}
13 changes: 9 additions & 4 deletions bintensors/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,13 @@ impl<'data> BinTensors<'data> {

/// The stuct representing the header of bintensor files which allow
/// indexing into the raw byte-buffer array and how to interpret it.
#[derive(Debug, Clone, Encode, Decode)]
#[derive(Debug, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Clone))]
pub struct Metadata {
#[bincode(with_serde)]
metadata: Option<HashMap<String, String>>,
tensors: Vec<TensorInfo>,
#[bincode(with_serde)]
index_map: HashMap<String, usize>,
}

Expand Down Expand Up @@ -717,7 +720,8 @@ impl<'data> TensorView<'data> {
/// A single tensor information.
/// Endianness is assumed to be little endian
/// Ordering is assumed to be 'C'.
#[derive(Debug, Clone, Encode, Decode)]
#[derive(Debug, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Clone))]
pub struct TensorInfo {
/// The type of each element of the tensor
pub dtype: Dtype,
Expand All @@ -728,7 +732,8 @@ pub struct TensorInfo {
}

/// The various available dtypes. They MUST be in increasing alignment order
#[derive(Debug, Encode, Decode, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]

#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Encode, Decode)]
#[non_exhaustive]
pub enum Dtype {
/// Boolan type
Expand Down Expand Up @@ -1151,7 +1156,7 @@ mod tests {
match BinTensors::deserialize(&reloaded) {
Err(BinTensorError::InvalidOffset(_)) => {
// Yes we have the correct error
// std::fs::remove_file(filename).unwrap();
std::fs::remove_file(filename).unwrap();
}
Err(err) => panic!("Unexpected error {err:?}"),
Ok(_) => panic!("This should not be able to be deserialized"),
Expand Down
Loading