]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Adding _safe_open_handle. (#608)
authorNicolas Patry <patry.nicolas@protonmail.com>
Fri, 1 Aug 2025 15:25:35 +0000 (17:25 +0200)
committerGitHub <noreply@github.com>
Fri, 1 Aug 2025 15:25:35 +0000 (17:25 +0200)
* [WIP]. Adding safe_handle.

* Adding S3.

* File handle becomes private for merge.

.pre-commit-config.yaml
bindings/python/py_src/safetensors/__init__.py
bindings/python/src/lib.rs
bindings/python/tests/test_handle.py [new file with mode: 0644]

index 70e9ba35ca1c0507d484ab4cdd65f72a58aa7c69..27dc28c05172d21dd92c6ef1d266fd2214a7fab9 100644 (file)
@@ -38,12 +38,11 @@ repos:
         name: "Python (black)"
         args: ["--line-length", "119", "--target-version", "py35"]
         types: ["python"]
-  - repo: https://github.com/pycqa/flake8
-    rev: 7.2.0
+  - repo: https://github.com/astral-sh/ruff-pre-commit
+    # Ruff version.
+    rev: v0.11.11
     hooks:
-      - id: flake8
-        args: ["--config", "bindings/python/setup.cfg"]
-  - repo: https://github.com/pre-commit/mirrors-isort
-    rev: v5.7.0 # Use the revision sha / tag you want to point at
-    hooks:
-      - id: isort
+      # Run the linter.
+      - id: ruff-check
+      # Run the formatter.
+      - id: ruff-format
index c9a5d2ca92b5248ce798a19f8e14c3492992cae1..0aea2154eb6b993974d3e2874f2d3ad09e21d9c0 100644 (file)
@@ -4,6 +4,7 @@ from ._safetensors_rust import (  # noqa: F401
     __version__,
     deserialize,
     safe_open,
+    _safe_open_handle,
     serialize,
     serialize_file,
 )
index 527767f4e12a783226ea728ffe3bc5c5b9e2458a..36f784b4f76a49cea4c94df78f762fcc8b8b331b 100644 (file)
@@ -1258,6 +1258,118 @@ pyo3::create_exception!(
     "Custom Python Exception for Safetensor errors."
 );
 
+#[pyclass]
+#[allow(non_camel_case_types)]
+struct _safe_open_handle {
+    inner: Option<Open>,
+}
+
+impl _safe_open_handle {
+    fn inner(&self) -> PyResult<&Open> {
+        let inner = self
+            .inner
+            .as_ref()
+            .ok_or_else(|| SafetensorError::new_err("File is closed".to_string()))?;
+        Ok(inner)
+    }
+}
+
+#[pymethods]
+impl _safe_open_handle {
+    #[new]
+    #[pyo3(signature = (f, framework, device=Some(Device::Cpu)))]
+    fn new(f: PyObject, framework: Framework, device: Option<Device>) -> PyResult<Self> {
+        let filename = Python::with_gil(|py| -> PyResult<PathBuf> {
+            let _ = f.getattr(py, "fileno")?;
+            let filename = f.getattr(py, "name")?;
+            let filename: PathBuf = filename.extract(py)?;
+            Ok(filename)
+        })?;
+        let inner = Some(Open::new(filename, framework, device)?);
+        Ok(Self { inner })
+    }
+
+    /// Return the special non tensor information in the header
+    ///
+    /// Returns:
+    ///     (`Dict[str, str]`):
+    ///         The freeform metadata.
+    pub fn metadata(&self) -> PyResult<Option<HashMap<String, String>>> {
+        Ok(self.inner()?.metadata())
+    }
+
+    /// Returns the names of the tensors in the file.
+    ///
+    /// Returns:
+    ///     (`List[str]`):
+    ///         The name of the tensors contained in that file
+    pub fn keys(&self) -> PyResult<Vec<String>> {
+        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:
+    ///     name (`str`):
+    ///         The name of the tensor you want
+    ///
+    /// Returns:
+    ///     (`Tensor`):
+    ///         The tensor in the framework you opened the file for.
+    ///
+    /// Example:
+    /// ```python
+    /// from safetensors import safe_open
+    ///
+    /// with safe_open("model.safetensors", framework="pt", device=0) as f:
+    ///     tensor = f.get_tensor("embedding")
+    ///
+    /// ```
+    pub fn get_tensor(&self, name: &str) -> PyResult<PyObject> {
+        self.inner()?.get_tensor(name)
+    }
+
+    /// Returns a full slice view object
+    ///
+    /// Args:
+    ///     name (`str`):
+    ///         The name of the tensor you want
+    ///
+    /// Returns:
+    ///     (`PySafeSlice`):
+    ///         A dummy object you can slice into to get a real tensor
+    /// Example:
+    /// ```python
+    /// from safetensors import safe_open
+    ///
+    /// with safe_open("model.safetensors", framework="pt", device=0) as f:
+    ///     tensor_part = f.get_slice("embedding")[:, ::8]
+    ///
+    /// ```
+    pub fn get_slice(&self, name: &str) -> PyResult<PySafeSlice> {
+        self.inner()?.get_slice(name)
+    }
+
+    /// Start the context manager
+    pub fn __enter__(slf: Py<Self>) -> Py<Self> {
+        slf
+    }
+
+    /// Exits the context manager
+    pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) {
+        self.inner = None;
+    }
+}
+
 /// A Python module implemented in Rust.
 #[pymodule(gil_used = false)]
 fn _safetensors_rust(m: &PyBound<'_, PyModule>) -> PyResult<()> {
@@ -1265,6 +1377,7 @@ fn _safetensors_rust(m: &PyBound<'_, PyModule>) -> PyResult<()> {
     m.add_function(wrap_pyfunction!(serialize_file, m)?)?;
     m.add_function(wrap_pyfunction!(deserialize, m)?)?;
     m.add_class::<safe_open>()?;
+    m.add_class::<_safe_open_handle>()?;
     m.add("SafetensorError", m.py().get_type::<SafetensorError>())?;
     m.add("__version__", env!("CARGO_PKG_VERSION"))?;
     Ok(())
diff --git a/bindings/python/tests/test_handle.py b/bindings/python/tests/test_handle.py
new file mode 100644 (file)
index 0000000..aaf0717
--- /dev/null
@@ -0,0 +1,65 @@
+import unittest
+
+import numpy as np
+
+from safetensors import _safe_open_handle
+from safetensors.numpy import save_file, save
+
+
+class ReadmeTestCase(unittest.TestCase):
+    def assertTensorEqual(self, tensors1, tensors2, equality_fn):
+        self.assertEqual(tensors1.keys(), tensors2.keys(), "tensor keys don't match")
+
+        for k, v1 in tensors1.items():
+            v2 = tensors2[k]
+
+            self.assertTrue(equality_fn(v1, v2), f"{k} tensors are different")
+
+    def test_numpy_example(self):
+        tensors = {"a": np.zeros((2, 2)), "b": np.zeros((2, 3), dtype=np.uint8)}
+
+        save_file(tensors, "./out_np.safetensors")
+
+        # Now loading
+        loaded = {}
+        with open("./out_np.safetensors", "r") as f:
+            with safe_open_handle(f, framework="np", device="cpu") as g:
+                for key in g.keys():
+                    loaded[key] = g.get_tensor(key)
+        self.assertTensorEqual(tensors, loaded, np.allclose)
+
+    def test_fsspec(self):
+        import fsspec
+
+        tensors = {"a": np.zeros((2, 2)), "b": np.zeros((2, 3), dtype=np.uint8)}
+
+        fs = fsspec.filesystem("file")
+        byts = save(tensors)
+        with fs.open("fs.safetensors", "wb") as f:
+            f.write(byts)
+        # Now loading
+        loaded = {}
+        with fs.open("fs.safetensors", "rb") as f:
+            with safe_open_handle(f, framework="np", device="cpu") as g:
+                for key in g.keys():
+                    loaded[key] = g.get_tensor(key)
+        self.assertTensorEqual(tensors, loaded, np.allclose)
+
+    @unittest.skip("Will not work without s3 access")
+    def test_fsspec_s3(self):
+        import s3fs
+
+        tensors = {"a": np.zeros((2, 2)), "b": np.zeros((2, 3), dtype=np.uint8)}
+
+        s3 = s3fs.S3FileSystem(anon=True)
+        byts = save(tensors)
+        print(s3.ls("my-bucket"))
+        with s3.open("out/fs.safetensors", "wb") as f:
+            f.write(byts)
+        # Now loading
+        loaded = {}
+        with s3.open("out/fs.safetensors", "rb") as f:
+            with safe_open_handle(f, framework="np", device="cpu") as g:
+                for key in g.keys():
+                    loaded[key] = g.get_tensor(key)
+        self.assertTensorEqual(tensors, loaded, np.allclose)