Values with the same index in a sparse tensor, how to only remain the max value when convert the sparse tensor to an dense one

Values with the same index in a sparse tensor are accumulated when use torch.cuda.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense().

How to only remain the max value when convert the sparse tensor to an dense one.
Any tricks to implement this?

Specifically,
indices = torch.cuda.LongTensor([[0, 1, 0], [2, 0, 2]])
values = torch.cuda.FloatTensor([3, 4, 5])
t = torch.cuda.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense()
then
t’s values are [[ 0 0 5],[ 4 0 0]] rather than [[ 0 0 8],[ 4 0 0]].

This is NOT what sparse tensors want to do! The sparse tensor represents [[ 0 0 8],[ 4 0 0]].

The operation you want is requested in #22378, which also links to a great third party implementation of scatter_max.

Best regards

Thomas

1 Like