How to save sparse_coo_tensor?

Hi,

I’m trying to save a sparse_coo_tensor, here’s an exmaple (using the torch docs for sparse_coo_tensor):

nnz = 3
dims = [5,5,2,3]
I = torch.cat([torch.randint(0, dims[0], size=(nnz,)),
torch.randint(0, dims[1], size=(nnz,))], 0).reshape(2, nnz)
V = torch.randn(nnz, dims[2], dims[3])
size = torch.Size(dims)
S = torch.sparse_coo_tensor(I, V, size)
torch.save(S, '/path/to/file.pth')

What I get is RuntimeError: sparse tensors do not have storage.
Any idea how to save it? thanks

We have an open feature request for this here: https://github.com/pytorch/pytorch/issues/16667

For now as a workaround, you can save tensor.indices(), tensor.values() and tensor.size() separately and reconstruct a sparse tensor from these three things.

1 Like