Thank you, this works. But from what I understand, this should require a lot of memory, since the source data array will be replicated x times if I x different selected sums are taken? For example, the original array is replicated thrice here:
index = torch.tensor([[0, 0, 1, 1, 0, 1],
                      [1, 1, 0, 0, 1, 0],
                      [0, 0, 1, 1, 1, 1],])

data = torch.tensor([[5., 1., 7., 2., 3., 4.],
                     [5., 1., 7., 2., 3., 4.]])
                     [5., 1., 7., 2., 3., 4.]])

torch.zeros(3, 3).scatter_add(1, index, data)
> tensor([[ 9., 13.,  0.],
          [13.,  9.,  0.],
          [ 6., 16.,  0.]])