]> khamidou — GitHub mirrors - safetensors.git/commitdiff
load_file: load tensors ordered by their offsets (#571)
authorYousong Zhou <yszhou4tech@gmail.com>
Wed, 5 Mar 2025 16:23:12 +0000 (00:23 +0800)
committerGitHub <noreply@github.com>
Wed, 5 Mar 2025 16:23:12 +0000 (17:23 +0100)
bindings/python/py_src/safetensors/flax.py
bindings/python/py_src/safetensors/mlx.py
bindings/python/py_src/safetensors/numpy.py
bindings/python/py_src/safetensors/tensorflow.py
bindings/python/py_src/safetensors/torch.py
bindings/python/src/lib.rs
safetensors/src/tensor.rs

index d0b8375e038eff487af33fcfaa4a597aacb5743f..a6471507dc44bff6c4a75e5b75a24241a693dad5 100644 (file)
@@ -121,7 +121,7 @@ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, Array]:
     """
     result = {}
     with safe_open(filename, framework="flax") as f:
-        for k in f.keys():
+        for k in f.offset_keys():
             result[k] = f.get_tensor(k)
     return result
 
index cf9fe37519c817e4d9db87e8ce53c2dc8b85254f..df04e710179e53a568b00104ad9e130490d441cd 100644 (file)
@@ -120,7 +120,7 @@ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, mx.array]:
     """
     result = {}
     with safe_open(filename, framework="mlx") as f:
-        for k in f.keys():
+        for k in f.offset_keys():
             result[k] = f.get_tensor(k)
     return result
 
index 0b245f12c1c949456c9b2edb45a11343e6a8099a..89e00e118cfcea1114364e95535cf350b0a0718f 100644 (file)
@@ -126,7 +126,7 @@ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, np.ndarray]:
     """
     result = {}
     with safe_open(filename, framework="np") as f:
-        for k in f.keys():
+        for k in f.offset_keys():
             result[k] = f.get_tensor(k)
     return result
 
index e2d74b0522698b3748a7da93753e065f4053beea..8513f803f60a58df1d8e67ad8aec594b3c395ff0 100644 (file)
@@ -120,7 +120,7 @@ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, tf.Tensor]:
     """
     result = {}
     with safe_open(filename, framework="tf") as f:
-        for k in f.keys():
+        for k in f.offset_keys():
             result[k] = f.get_tensor(k)
     return result
 
index 4476e754de612751c3cbb3250adac0907e22ea53..f3591adee420401fb35fd4b054df5c707f7a9ddc 100644 (file)
@@ -317,7 +317,7 @@ def load_file(filename: Union[str, os.PathLike], device: Union[str, int] = "cpu"
     """
     result = {}
     with safe_open(filename, framework="pt", device=device) as f:
-        for k in f.keys():
+        for k in f.offset_keys():
             result[k] = f.get_tensor(k)
     return result
 
index 1e6ba8531057296b94fcca14f8d985ed1ed1935f..4d1d5f82f29004819d186934600b7a90912c769e 100644 (file)
@@ -443,6 +443,16 @@ impl Open {
         Ok(keys)
     }
 
+    /// Returns the names of the tensors in the file, ordered by offset.
+    ///
+    /// Returns:
+    ///     (`List[str]`):
+    ///         The name of the tensors contained in that file
+    pub fn offset_keys(&self) -> PyResult<Vec<String>> {
+        let keys: Vec<String> = self.metadata.offset_keys();
+        Ok(keys)
+    }
+
     /// Returns a full tensor
     ///
     /// Args:
@@ -651,6 +661,15 @@ impl safe_open {
         self.inner()?.keys()
     }
 
+    /// Returns the names of the tensors in the file, ordered by offset.
+    ///
+    /// Returns:
+    ///     (`List[str]`):
+    ///         The name of the tensors contained in that file
+    pub fn offset_keys(&self) -> PyResult<Vec<String>> {
+        self.inner()?.offset_keys()
+    }
+
     /// Returns a full tensor
     ///
     /// Args:
index 42d59e040132ef52caedbb1921d0b84f1d3c0b46..1da1278ce3d9bf3a847b59d9caa60709a727b493 100644 (file)
@@ -554,6 +554,13 @@ impl Metadata {
             .collect()
     }
 
+    /// Gives back the tensor names ordered by offset
+    pub fn offset_keys(&self) -> Vec<String> {
+        let mut index_vec: Vec<_> = self.index_map.iter().collect();
+        index_vec.sort_by_key(|a| a.1);
+        index_vec.into_iter().map(|a| a.0.clone()).collect()
+    }
+
     /// Gives back the tensor metadata
     pub fn metadata(&self) -> &Option<HashMap<String, String>> {
         &self.metadata