From 0b21039218bd869969838d60286fe6786a87c9c3 Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Thu, 6 Apr 2023 13:08:14 +0200 Subject: [PATCH] Minor checks + proptest in python. (#228) * Minor checks + proptest in python. * Fix readme. * Tf version. * Fix. * Jax version. * Jaxlib. * Jax. * Cannot have jax version - Python 3.7 is minimal version for which they don't have release jax>0.4 - Our doc builder relies on jax>0.4 * wtf. * . * ... * Wahtever. --- README.md | 1 + bindings/python/py_src/safetensors/numpy.py | 2 +- bindings/python/setup.py | 8 +++-- bindings/python/src/lib.rs | 2 +- safetensors/README.md | 1 + safetensors/src/lib.rs | 1 + safetensors/src/slice.rs | 34 ++++++++++----------- safetensors/src/tensor.rs | 5 ++- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index a32c7fa..bd2b4d7 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ pip install -e . ### Getting started ```python +import torch from safetensors import safe_open from safetensors.torch import save_file diff --git a/bindings/python/py_src/safetensors/numpy.py b/bindings/python/py_src/safetensors/numpy.py index bae82b8..d0dd151 100644 --- a/bindings/python/py_src/safetensors/numpy.py +++ b/bindings/python/py_src/safetensors/numpy.py @@ -163,7 +163,7 @@ def _view2np(safeview) -> Dict[str, np.ndarray]: return result -def _is_little_endian(tensor: np.ndarray) -> str: +def _is_little_endian(tensor: np.ndarray) -> bool: byteorder = tensor.dtype.byteorder if byteorder == "=": if sys.byteorder == "little": diff --git a/bindings/python/setup.py b/bindings/python/setup.py index cd3e060..a5e8cc7 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -14,14 +14,16 @@ _deps = [ "h5py>=3.7.0", "huggingface_hub>=0.12.1", "isort>=5.5.4", - "jax>=0.4.1", - "numpy>=1.24.2", + "jax>=0.3.25", + "numpy>=1.21.6", "setuptools_rust>=1.5.2", "pytest>=7.2.0", "pytest-benchmark>=4.0.0", - "tensorflow>=2.12.0", + "tensorflow>=2.11.0", "torch>=1.10", "paddlepaddle>=2.4.1", + "python-afl>=0.7.3", + "hypothesis>=6.70.2", ] diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 2f6d999..9215383 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -713,7 +713,7 @@ impl PySafeSlice { .map(slice_to_indexer) .collect::>()?; - let iterator = tensor.sliced_data(slices.clone()).map_err(|e| { + let iterator = tensor.sliced_data(&slices).map_err(|e| { SafetensorError::new_err(format!( "Error during slicing {slices:?} vs {:?}: {:?}", self.info.shape, e diff --git a/safetensors/README.md b/safetensors/README.md index a32c7fa..bd2b4d7 100644 --- a/safetensors/README.md +++ b/safetensors/README.md @@ -44,6 +44,7 @@ pip install -e . ### Getting started ```python +import torch from safetensors import safe_open from safetensors.torch import save_file diff --git a/safetensors/src/lib.rs b/safetensors/src/lib.rs index d80475b..cbbaf2c 100644 --- a/safetensors/src/lib.rs +++ b/safetensors/src/lib.rs @@ -31,6 +31,7 @@ //!## Getting started //! //!```python +//!import torch //!from safetensors import safe_open //!from safetensors.torch import save_file //! diff --git a/safetensors/src/slice.rs b/safetensors/src/slice.rs index 03c23ef..255a5f4 100644 --- a/safetensors/src/slice.rs +++ b/safetensors/src/slice.rs @@ -84,7 +84,7 @@ where A: Into, { fn slice(&'data self, index: A) -> Result, InvalidSlice> { - self.sliced_data(vec![index.into()]) + self.sliced_data(&[index.into()]) } } @@ -94,7 +94,7 @@ where { fn slice(&'data self, index: (A,)) -> Result, InvalidSlice> { let idx_a = index.0.into(); - self.sliced_data(vec![idx_a]) + self.sliced_data(&[idx_a]) } } @@ -106,7 +106,7 @@ where fn slice(&'data self, index: (A, B)) -> Result, InvalidSlice> { let idx_a = index.0.into(); let idx_b = index.1.into(); - self.sliced_data(vec![idx_a, idx_b]) + self.sliced_data(&[idx_a, idx_b]) } } @@ -120,7 +120,7 @@ where let idx_a = index.0.into(); let idx_b = index.1.into(); let idx_c = index.2.into(); - self.sliced_data(vec![idx_a, idx_b, idx_c]) + self.sliced_data(&[idx_a, idx_b, idx_c]) } } @@ -211,7 +211,7 @@ pub struct SliceIterator<'data> { impl<'data> SliceIterator<'data> { pub(crate) fn new( view: &'data TensorView<'data>, - slices: Vec, + slices: &[TensorIndexer], ) -> Result { // Make sure n. axis does not exceed n. of dimensions let n_slice = slices.len(); @@ -327,7 +327,7 @@ mod tests { let iterator = SliceIterator::new( &attn_0, - vec![TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded)], + &[TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded)], ) .unwrap(); assert_eq!(iterator.remaining_byte_len(), 24); @@ -335,7 +335,7 @@ mod tests { let iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Included(0), Bound::Excluded(1)), ], @@ -356,7 +356,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded)], + &[TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded)], ) .unwrap(); assert_eq!(iterator.next(), Some(&data[0..24])); @@ -364,7 +364,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), ], @@ -375,7 +375,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), ], @@ -386,7 +386,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), @@ -398,7 +398,7 @@ mod tests { assert!(SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), @@ -419,7 +419,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![TensorIndexer::Narrow( + &[TensorIndexer::Narrow( Bound::Included(0), Bound::Excluded(1), )], @@ -430,7 +430,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Included(0), Bound::Excluded(1)), ], @@ -441,7 +441,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Included(0), Bound::Excluded(1)), @@ -454,7 +454,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Included(1), Bound::Excluded(2)), TensorIndexer::Narrow(Bound::Included(0), Bound::Excluded(1)), @@ -476,7 +476,7 @@ mod tests { let mut iterator = SliceIterator::new( &attn_0, - vec![ + &[ TensorIndexer::Narrow(Bound::Unbounded, Bound::Unbounded), TensorIndexer::Narrow(Bound::Included(1), Bound::Excluded(3)), ], diff --git a/safetensors/src/tensor.rs b/safetensors/src/tensor.rs index c89d640..3505edd 100644 --- a/safetensors/src/tensor.rs +++ b/safetensors/src/tensor.rs @@ -473,8 +473,7 @@ impl Metadata { let tensor_name = self .index_map .iter() - .filter_map(|(name, &index)| if index == i { Some(&name[..]) } else { None }) - .next() + .find_map(|(name, &index)| if index == i { Some(&name[..]) } else { None }) .unwrap_or("no_tensor"); return Err(SafeTensorError::InvalidOffset(tensor_name.to_string())); } @@ -570,7 +569,7 @@ impl<'data> TensorView<'data> { /// The various pieces of the data buffer according to the asked slice pub fn sliced_data( &'data self, - slices: Vec, + slices: &[TensorIndexer], ) -> Result, InvalidSlice> { SliceIterator::new(self, slices) } -- 2.43.0