]> khamidou — GitHub mirrors - safetensors.git/commitdiff
impl `View` for `TensorView` (#329)
authorCorey Lowman <clowman1993@gmail.com>
Mon, 21 Aug 2023 07:14:25 +0000 (03:14 -0400)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 07:14:25 +0000 (09:14 +0200)
The existing impl only exists for `&TensorView`, which means you cannot pass a `Vec<(String, TensorView)>` into `serialize_to_file`. With this new impl, you can.

safetensors/src/tensor.rs

index a05f49a0a022ae53179fb1d5f8089576cbe42910..e740bcbb67248141f8a62f8da449b000c133c5c8 100644 (file)
@@ -554,6 +554,24 @@ impl<'data> View for &TensorView<'data> {
     }
 }
 
+impl<'data> View for TensorView<'data> {
+    fn dtype(&self) -> Dtype {
+        self.dtype
+    }
+
+    fn shape(&self) -> &[usize] {
+        &self.shape
+    }
+
+    fn data(&self) -> Cow<[u8]> {
+        self.data.into()
+    }
+
+    fn data_len(&self) -> usize {
+        self.data.len()
+    }
+}
+
 impl<'data> TensorView<'data> {
     /// Create new tensor view
     pub fn new(