From 467a605bcb0f8ce3a324b9e1dd93c309bd6661aa Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 6 Mar 2025 00:23:12 +0800 Subject: [PATCH] load_file: load tensors ordered by their offsets (#571) --- bindings/python/py_src/safetensors/flax.py | 2 +- bindings/python/py_src/safetensors/mlx.py | 2 +- bindings/python/py_src/safetensors/numpy.py | 2 +- .../python/py_src/safetensors/tensorflow.py | 2 +- bindings/python/py_src/safetensors/torch.py | 2 +- bindings/python/src/lib.rs | 19 +++++++++++++++++++ safetensors/src/tensor.rs | 7 +++++++ 7 files changed, 31 insertions(+), 5 deletions(-) diff --git a/bindings/python/py_src/safetensors/flax.py b/bindings/python/py_src/safetensors/flax.py index d0b8375..a647150 100644 --- a/bindings/python/py_src/safetensors/flax.py +++ b/bindings/python/py_src/safetensors/flax.py @@ -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 diff --git a/bindings/python/py_src/safetensors/mlx.py b/bindings/python/py_src/safetensors/mlx.py index cf9fe37..df04e71 100644 --- a/bindings/python/py_src/safetensors/mlx.py +++ b/bindings/python/py_src/safetensors/mlx.py @@ -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 diff --git a/bindings/python/py_src/safetensors/numpy.py b/bindings/python/py_src/safetensors/numpy.py index 0b245f1..89e00e1 100644 --- a/bindings/python/py_src/safetensors/numpy.py +++ b/bindings/python/py_src/safetensors/numpy.py @@ -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 diff --git a/bindings/python/py_src/safetensors/tensorflow.py b/bindings/python/py_src/safetensors/tensorflow.py index e2d74b0..8513f80 100644 --- a/bindings/python/py_src/safetensors/tensorflow.py +++ b/bindings/python/py_src/safetensors/tensorflow.py @@ -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 diff --git a/bindings/python/py_src/safetensors/torch.py b/bindings/python/py_src/safetensors/torch.py index 4476e75..f3591ad 100644 --- a/bindings/python/py_src/safetensors/torch.py +++ b/bindings/python/py_src/safetensors/torch.py @@ -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 diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 1e6ba85..4d1d5f8 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -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> { + let keys: Vec = 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> { + self.inner()?.offset_keys() + } + /// Returns a full tensor /// /// Args: diff --git a/safetensors/src/tensor.rs b/safetensors/src/tensor.rs index 42d59e0..1da1278 100644 --- a/safetensors/src/tensor.rs +++ b/safetensors/src/tensor.rs @@ -554,6 +554,13 @@ impl Metadata { .collect() } + /// Gives back the tensor names ordered by offset + pub fn offset_keys(&self) -> Vec { + 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> { &self.metadata -- 2.43.0