]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Small helpers
authorNicolas Patry <patry.nicolas@protonmail.com>
Wed, 6 Jul 2022 20:26:56 +0000 (22:26 +0200)
committerNicolas Patry <patry.nicolas@protonmail.com>
Wed, 6 Jul 2022 20:26:56 +0000 (22:26 +0200)
safetensors/src/lib.rs

index 97c65ec52893a3fd414a47044536064bbb0a8416..e0c6067b22327bbeb9d7f6500e3294b36852aa72 100644 (file)
@@ -13,6 +13,8 @@ pub enum SafeTensorError {
     InvalidHeader,
     /// The header does contain a valid string, but it is not valid JSON.
     InvalidHeaderDeserialization,
+    /// The tensor name was not found in the archive
+    TensorNotFound,
 }
 
 fn prepare<'hash, 'data>(
@@ -119,6 +121,30 @@ impl<'data> SafeTensors<'data> {
         }
         tensors
     }
+
+    /// Allow the user to get a specific tensor within the SafeTensors.
+    /// The tensor returned is merely a view and the data is not owned by this
+    /// structure.
+    pub fn tensor(&self, tensor_name: &str) -> Result<TensorView<'_>, SafeTensorError> {
+        for (name, info) in &self.metadata.0 {
+            if name == tensor_name {
+                return Ok(TensorView {
+                    dtype: &info.dtype,
+                    shape: &info.shape,
+                    data: &self.data
+                        [info.data_offsets.0 + self.offset..info.data_offsets.1 + self.offset],
+                });
+            }
+        }
+        Err(SafeTensorError::TensorNotFound)
+    }
+
+    /// Allow the user to get a specific tensor within the SafeTensors.
+    /// The tensor returned is merely a view and the data is not owned by this
+    /// structure.
+    pub fn names(&self) -> Vec<&'_ String> {
+        self.metadata.0.iter().map(|(name, _)| name).collect()
+    }
 }
 
 /// The stuct representing the header of safetensor files which allow
@@ -290,7 +316,6 @@ mod tests {
         std::fs::write(&filename, out).unwrap();
 
         let raw = std::fs::read(&filename).unwrap();
-
         let _deserialized = SafeTensors::deserialize(&raw).unwrap();
     }
 }