]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Updating rust ergonomics. (#144)
authorNicolas Patry <patry.nicolas@protonmail.com>
Fri, 30 Dec 2022 13:28:44 +0000 (14:28 +0100)
committerGitHub <noreply@github.com>
Fri, 30 Dec 2022 13:28:44 +0000 (14:28 +0100)
- `get_` prefix removed in a few functions.
- Make error implement Error.

bindings/python/src/lib.rs
safetensors/src/slice.rs
safetensors/src/tensor.rs

index 6d0e85eefdea04e5ca8137521e159f95445f5398..c47859a2ab471f8ea9ee7558a7ca26c8f2e9da9f 100644 (file)
@@ -160,10 +160,10 @@ fn deserialize(py: Python, bytes: &[u8]) -> PyResult<Vec<(String, HashMap<String
     for (tensor_name, tensor) in safetensor.tensors() {
         let mut map = HashMap::new();
 
-        let pyshape: PyObject = PyList::new(py, tensor.get_shape().iter()).into();
-        let pydtype: PyObject = format!("{:?}", tensor.get_dtype()).into_py(py);
+        let pyshape: PyObject = PyList::new(py, tensor.shape().iter()).into();
+        let pydtype: PyObject = format!("{:?}", tensor.dtype()).into_py(py);
 
-        let pydata: PyObject = PyByteArray::new(py, tensor.get_data()).into();
+        let pydata: PyObject = PyByteArray::new(py, tensor.data()).into();
 
         map.insert("shape".to_string(), pyshape);
         map.insert("dtype".to_string(), pydtype);
@@ -823,7 +823,7 @@ impl PySafeSlice {
                     .map(slice_to_indexer)
                     .collect::<Result<_, _>>()?;
 
-                let iterator = tensor.get_sliced_data(slices.clone()).map_err(|e| {
+                let iterator = tensor.sliced_data(slices.clone()).map_err(|e| {
                     exceptions::PyException::new_err(format!(
                         "Error during slicing {slices:?} vs {:?}:  {:?}",
                         self.info.shape, e
index bd75fdd8040635a8bf1ca579a78cb0368e17faec..8631ed7990aed34bb31dd93108e317b3f8126ff0 100644 (file)
@@ -84,7 +84,7 @@ where
     A: Into<TensorIndexer>,
 {
     fn slice(&'data self, index: A) -> Result<SliceIterator<'data>, InvalidSlice> {
-        self.get_sliced_data(vec![index.into()])
+        self.sliced_data(vec![index.into()])
     }
 }
 
@@ -94,7 +94,7 @@ where
 {
     fn slice(&'data self, index: (A,)) -> Result<SliceIterator<'data>, InvalidSlice> {
         let idx_a = index.0.into();
-        self.get_sliced_data(vec![idx_a])
+        self.sliced_data(vec![idx_a])
     }
 }
 
@@ -106,7 +106,7 @@ where
     fn slice(&'data self, index: (A, B)) -> Result<SliceIterator<'data>, InvalidSlice> {
         let idx_a = index.0.into();
         let idx_b = index.1.into();
-        self.get_sliced_data(vec![idx_a, idx_b])
+        self.sliced_data(vec![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.get_sliced_data(vec![idx_a, idx_b, idx_c])
+        self.sliced_data(vec![idx_a, idx_b, idx_c])
     }
 }
 
@@ -136,7 +136,7 @@ where
 //         let idx_b = index.1.into();
 //         let idx_c = index.2.into();
 //         let idx_d = index.3.into();
-//         self.get_sliced_data(&[idx_a, idx_b, idx_c, idx_d])
+//         self.sliced_data(&[idx_a, idx_b, idx_c, idx_d])
 //     }
 // }
 //
@@ -154,7 +154,7 @@ where
 //         let idx_c = index.2.into();
 //         let idx_d = index.3.into();
 //         let idx_e = index.4.into();
-//         self.get_sliced_data(&[idx_a, idx_b, idx_c, idx_d, idx_e])
+//         self.sliced_data(&[idx_a, idx_b, idx_c, idx_d, idx_e])
 //     }
 // }
 //
@@ -174,7 +174,7 @@ where
 //         let idx_d = index.3.into();
 //         let idx_e = index.4.into();
 //         let idx_f = index.5.into();
-//         self.get_sliced_data(&[idx_a, idx_b, idx_c, idx_d, idx_e, idx_f])
+//         self.sliced_data(&[idx_a, idx_b, idx_c, idx_d, idx_e, idx_f])
 //     }
 // }
 //
@@ -196,7 +196,7 @@ where
 //         let idx_e = index.4.into();
 //         let idx_f = index.5.into();
 //         let idx_g = index.6.into();
-//         self.get_sliced_data(&[idx_a, idx_b, idx_c, idx_d, idx_e, idx_f, idx_g])
+//         self.sliced_data(&[idx_a, idx_b, idx_c, idx_d, idx_e, idx_f, idx_g])
 //     }
 // }
 
index 7f46913d0bfc0b7759a77d4d4fc542ce9d22d360..654f6769c087b5112ea30858228278f2389a8fc6 100644 (file)
@@ -38,6 +38,14 @@ impl From<std::io::Error> for SafeTensorError {
     }
 }
 
+impl std::fmt::Display for SafeTensorError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{:?}", self)
+    }
+}
+
+impl std::error::Error for SafeTensorError {}
+
 fn prepare<'hash, 'data>(
     data: &'hash BTreeMap<String, TensorView<'data>>,
     data_info: &'hash Option<BTreeMap<String, String>>,
@@ -262,22 +270,22 @@ impl<'data> TensorView<'data> {
         Self { dtype, shape, data }
     }
     /// The current tensor dtype
-    pub fn get_dtype(&self) -> Dtype {
+    pub fn dtype(&self) -> Dtype {
         self.dtype
     }
 
     /// The current tensor shape
-    pub fn get_shape(&'data self) -> &'data [usize] {
+    pub fn shape(&'data self) -> &'data [usize] {
         &self.shape
     }
 
     /// The current tensor byte-buffer
-    pub fn get_data(&self) -> &'data [u8] {
+    pub fn data(&self) -> &'data [u8] {
         self.data
     }
 
     /// The various pieces of the data buffer according to the asked slice
-    pub fn get_sliced_data(
+    pub fn sliced_data(
         &'data self,
         slices: Vec<TensorIndexer>,
     ) -> Result<SliceIterator<'data>, InvalidSlice> {
@@ -503,10 +511,10 @@ mod tests {
 
         assert_eq!(loaded.names(), vec!["test"]);
         let tensor = loaded.tensor("test").unwrap();
-        assert_eq!(tensor.get_shape(), vec![2, 2]);
-        assert_eq!(tensor.get_dtype(), Dtype::I32);
+        assert_eq!(tensor.shape(), vec![2, 2]);
+        assert_eq!(tensor.dtype(), Dtype::I32);
         // 16 bytes
-        assert_eq!(tensor.get_data(), b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
+        assert_eq!(tensor.data(), b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
     }
 
     #[test]