]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Improve type hinting (#159)
authorThomas Wang <24695242+thomasw21@users.noreply.github.com>
Mon, 16 Jan 2023 14:28:18 +0000 (15:28 +0100)
committerGitHub <noreply@github.com>
Mon, 16 Jan 2023 14:28:18 +0000 (15:28 +0100)
bindings/python/py_src/safetensors/flax.py
bindings/python/py_src/safetensors/numpy.py
bindings/python/py_src/safetensors/tensorflow.py
bindings/python/py_src/safetensors/torch.py
bindings/python/tests/test_pt_comparison.py

index 80f4f276580a68c9f72d3bd88a13edd0344611eb..4eb72d819004438f9c422718d24eae871d8fe843 100644 (file)
@@ -1,4 +1,5 @@
-from typing import Dict, Optional
+import os
+from typing import Dict, Optional, Union
 
 import numpy as np
 
@@ -37,7 +38,7 @@ def save(tensors: Dict[str, jnp.DeviceArray], metadata: Optional[Dict[str, str]]
 
 def save_file(
     tensors: Dict[str, jnp.DeviceArray],
-    filename: str,
+    filename: Union[str, os.PathLike],
     metadata: Optional[Dict[str, str]] = None,
 ) -> None:
     """
@@ -97,7 +98,7 @@ def load(data: bytes) -> Dict[str, jnp.DeviceArray]:
     return _np2jnp(flat)
 
 
-def load_file(filename: str) -> Dict[str, jnp.DeviceArray]:
+def load_file(filename: Union[str, os.PathLike]) -> Dict[str, jnp.DeviceArray]:
     """
     Loads a safetensors file into flax format.
 
index 377513c392da684751b79b7e67385f7148f9d086..8a4c661bdb7aa9e97edd3096ef159e87f0aba390 100644 (file)
@@ -1,5 +1,6 @@
+import os
 import sys
-from typing import Dict, Optional
+from typing import Dict, Optional, Union
 
 import numpy as np
 
@@ -40,7 +41,9 @@ def save(tensor_dict: Dict[str, np.ndarray], metadata: Optional[Dict[str, str]]
     return result
 
 
-def save_file(tensor_dict: Dict[str, np.ndarray], filename: str, metadata: Optional[Dict[str, str]] = None) -> None:
+def save_file(
+    tensor_dict: Dict[str, np.ndarray], filename: Union[str, os.PathLike], metadata: Optional[Dict[str, str]] = None
+) -> None:
     """
     Saves a dictionnary of tensors into raw bytes in safetensors format.
 
@@ -101,7 +104,7 @@ def load(data: bytes) -> Dict[str, np.ndarray]:
     return _view2np(flat)
 
 
-def load_file(filename: str) -> Dict[str, np.ndarray]:
+def load_file(filename: Union[str, os.PathLike]) -> Dict[str, np.ndarray]:
     """
     Loads a safetensors file into numpy format.
 
index 74e42042ba1635b5919529aa573a65d854c1195f..96b704f16c2b6e904ee1710e9c68e8940868ad5c 100644 (file)
@@ -1,4 +1,5 @@
-from typing import Dict, Optional
+import os
+from typing import Dict, Optional, Union
 
 import numpy as np
 import tensorflow as tf
@@ -37,7 +38,7 @@ def save(tensors: Dict[str, tf.Tensor], metadata: Optional[Dict[str, str]] = Non
 
 def save_file(
     tensors: Dict[str, tf.Tensor],
-    filename: str,
+    filename: Union[str, os.PathLike],
     metadata: Optional[Dict[str, str]] = None,
 ) -> None:
     """
@@ -97,7 +98,7 @@ def load(data: bytes) -> Dict[str, tf.Tensor]:
     return _np2tf(flat)
 
 
-def load_file(filename: str) -> Dict[str, tf.Tensor]:
+def load_file(filename: Union[str, os.PathLike]) -> Dict[str, tf.Tensor]:
     """
     Loads a safetensors file into tensorflow format.
 
index 1a59f47b0bf2900ca803256827daf7b5c4cf86ab..110f3261e48286652804bb0a6816e00eb42f506a 100644 (file)
@@ -1,6 +1,7 @@
+import os
 import sys
 from collections import defaultdict
-from typing import Any, Dict, Optional
+from typing import Any, Dict, Optional, Union
 
 import torch
 
@@ -39,7 +40,7 @@ def save(tensors: Dict[str, torch.Tensor], metadata: Optional[Dict[str, str]] =
 
 def save_file(
     tensors: Dict[str, torch.Tensor],
-    filename: str,
+    filename: Union[str, os.PathLike],
     metadata: Optional[Dict[str, str]] = None,
 ):
     """
@@ -71,7 +72,7 @@ def save_file(
     serialize_file(_flatten(tensors), filename, metadata=metadata)
 
 
-def load_file(filename: str, device="cpu") -> Dict[str, torch.Tensor]:
+def load_file(filename: Union[str, os.PathLike], device="cpu") -> Dict[str, torch.Tensor]:
     """
     Loads a safetensors file into torch format.
 
index bd640d0f1624273540a82a987553c9c6d82cb84a..8c5c3cec76537eae1c8f02c83a41f551e63a5297 100644 (file)
@@ -49,7 +49,11 @@ class TorchTestCase(unittest.TestCase):
             save_file(data, local)
         self.assertEqual(
             str(ctx.exception),
-            "You are trying to save a sparse tensor: `test` which this library does not support. You can make it a dense tensor before saving with `.to_dense()` but be aware this might make a much larger file than needed.",
+            (
+                "You are trying to save a sparse tensor: `test` which this library does not support. You can make it a"
+                " dense tensor before saving with `.to_dense()` but be aware this might make a much larger file than"
+                " needed."
+            ),
         )
 
     def test_bogus(self):