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")