Hi Lapertor!



 lapertor:

Grouping by index and summing as you say would also answer my needs
…
but I am convinced that there is a way to do it the way I said.


If by “the way I said” means to perform your computation with a single
pytorch tensor operation in the presence of duplicate indices, then I
don’t think it’s possible.
It’s difficult to prove a negative, but I think any kind of pytorch "advanced
indexing* or index_add-style approach will give the “wrong” answer
(and potentially become non-deterministic) when confronted with
duplicate indices.
My assumption is that pytorch, for efficiency reasons, does the indexed
operation, figuratively speaking, “in parallel,” breaking the semantics
you would expect if you had coded the operation with sequential loops.
In my experience, as with Vainaijr’s observation, “the last addition for
each index, and give 2, not 2.4,” the behavior is as if the result for just
one of a set duplicate indices overwrites the others.  Furthermore, I
believe that pytorch makes no guarantee as to which index “wins” so
that your result may change if you move your computation to the gpu
or change the size of your tensors, etc.
I would be very interested to hear if you do find a way to perform such
indexed operations in the presence of duplicate indices.  (It would be
great if an expert would weigh in on this.)
Best.
K. Frank