From e57ca430268a9638c011cdd329f62a7e8fecadab Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Mon, 31 Oct 2022 10:13:32 +0100 Subject: [PATCH] Supporting torch==1.13.0 (#45) --- bindings/python/py_src/safetensors/torch.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/bindings/python/py_src/safetensors/torch.py b/bindings/python/py_src/safetensors/torch.py index 32ab804..54636ca 100644 --- a/bindings/python/py_src/safetensors/torch.py +++ b/bindings/python/py_src/safetensors/torch.py @@ -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") -- 2.43.0