]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Supporting torch==1.13.0 (#45)
authorNicolas Patry <patry.nicolas@protonmail.com>
Mon, 31 Oct 2022 09:13:32 +0000 (10:13 +0100)
committerGitHub <noreply@github.com>
Mon, 31 Oct 2022 09:13:32 +0000 (10:13 +0100)
bindings/python/py_src/safetensors/torch.py

index 32ab804c0b7f0da39bda3e061f281b3ebf9c493c..54636cac93283dfe27f557ec0ab7c038de1d7f0b 100644 (file)
@@ -84,21 +84,20 @@ def _view2torch(safeview) -> Dict[str, torch.Tensor]:
 
 
 def _tobytes(tensor: torch.Tensor, name: str) -> bytes:
-    try:
-        if not tensor.is_contiguous():
-            raise ValueError(
-                f"You are trying to save a non contiguous tensor: `{name}` which is not allowed. It either means you"
-                " are trying to save tensors which are reference of each other in which case it's recommended to save"
-                " only the full tensors, and reslice at load time, or simply call `.contiguous()` on your tensor to"
-                " pack it before saving."
-            )
-    except RuntimeError:
-        # This occurs with sparse tensors
+    if tensor.layout != torch.strided:
         raise ValueError(
             f"You are trying to save a sparse tensor: `{name}` 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."
         )
+
+    if not tensor.is_contiguous():
+        raise ValueError(
+            f"You are trying to save a non contiguous tensor: `{name}` which is not allowed. It either means you"
+            " are trying to save tensors which are reference of each other in which case it's recommended to save"
+            " only the full tensors, and reslice at load time, or simply call `.contiguous()` on your tensor to"
+            " pack it before saving."
+        )
     if tensor.device.type != "cpu":
         # Moving tensor to cpu before saving
         tensor = tensor.to("cpu")