Unable to load sparse tensor

I’m trying to save sparse tensor and then load them back. The save step executes without any problem but when I try to load it back I get a weird error

"RuntimeError: size is inconsistent with indices: for dim 1, size is 10 but found index 4934256341737799680"

Code to reproduce this error is given below:

ind = torch.LongTensor([[0]*4, [2, 4, 7, 9]])
val = torch.FloatTensor([5, 6, 6, 6])
sp_tensor = torch.sparse_coo_tensor(ind, val, torch.Size([1, 10]))
torch.save(sp_tensor, "test.pt")
sp_tensor = torch.load("test.pt")
print(sp_tensor)

Thanks for reporting this issue! :slight_smile:
It seems to related to this issue with a potential fix in this PR, which is currently being reviewed.

1 Like

Got it. Thank you!

Is there a recommended workaround for the time being?

As a workaround you could save the tensor in its dense form via:

torch.save(sp_tensor.to_dense(), 'test.pt')
1 Like