Ways to handle collisions when assigning multiple entries to the same location in a torch tensor

jtl: <p>Hi,</p>
<p>I have a question regarding vectorized indexing to assign entries from the source tensor to the target when there are collisions in the target indexes.</p>
<p>Specifically, considering the following example:</p>
<pre><code class="lang-python">import torch

device = torch.device("cpu")

source_tensor = torch.tensor(
    [1.23, 2.68, 9.37, 5.84, 6.87, 5.21, 7.46, 2.33, 8.95],
    dtype=torch.float32,
    device=device,
)

indexes = torch.tensor(
    [0, 0, 1, 0, 0, 1, 0, 1, 1],
    dtype=torch.long,
    device=device,
)

target_tensor = torch.zeros(2, dtype=torch.float32, device=device)

target_tensor[indexes] = source_tensor
print(target_tensor)
</code></pre>
<p>We have multiple entries from the source tensor got assigned to the same location (0 and 1) in the target tensor. In this example, the number of points are still small. But in my practical case, the number of points can be 10K+.</p>
<ul>
<li>
<p>The first issue I encountered is that I got different results if I change device to <code>torch.device("cuda")</code>. It means that the ways it handles collisions are different between GPU and CPU. How do I make it consistent?</p>
</li>
<li>
<p>Same as the issue mentioned above, in this example, the CPU one seems to be assigning the entries sequentially (at least on my desktop), so one  would get <code>tensor([7.4600, 8.9500])</code> as the result (they are the last ones got assigned to location 0 and 1 separately). But when I did it in my practical case (&gt; 10K points), what I encountered was that it’s also not done sequentially even using CPU. Is there a way to enforce the sequential behavior on both GPU and CPU? Because then we can potentially pre-sort the tensor if we want to always fetch the minimum when collisions happen? One might suggest that I can simply remove the collisions, but I don’t know a way to do that through vectorized operations, and that makes the process super slow.</p>
</li>
</ul>
<p>Another potential solution is the <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.index_reduce_.html#torch.Tensor.index_reduce_" rel="noopener nofollow ugc"><code>Tensor.index_reduce_()</code></a> . By using this function, we can specify the rules like <code>amin</code>, etc. However, this function won’t give me the inverse index. i.e. I can’t infer which entry from the <code>source_tensor</code> got selected. Is there any suggestion how do I get the inverse indexes?</p>
<p>Thanks</p>

ptrblck: <p>I don’t think there is a way to make the result consistent between GPU and CPU as both are using undefined behavior and the result depends on the order of operations.<br>
The right approach would be to use e.g. <code>index_reduce_</code> or to remove the duplicates as you’ve already mentioned.<br>
Could you describe your last comment in more details and the concern regarding the “inverse indices” and how these should be created?</p>

jtl: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> ,</p>
<p>Thank you for the reply! So the reason why I need the inverse indices is that actually the <code>source_tensor</code> and the <code>target_tensor</code> have multiple channels. For example, <code>source_tensor</code> can have shape <code>(N, 10)</code>, N indicates number of points and 10 is the feature dimension. However, within these 10 channels, we want to use one of them to handle the collisions.</p>
<p>For instance, one of them can be the distances, and when collision happens, we always want to fetch the one with the smallest distance. With <code>Tensor.index_reduce_()</code>, we can fetch that specific channel out through <code>source_tensor[:, 0]</code> (imagine that it’s in the first channel), and perform the reduction. We will have the correct results. However, we don’t know which entry got fetched from the original tensor. Thus, we do not know how to assign the rest 9 channels correctly.</p>
<p>So in my example in the post, the inverse indices would be <code>[0, 7]</code> if the reduction rule is <code>amin</code>. Among all entries that are assigned to target index <code>0</code>, the index <code>0</code> from the <code>source_tensor</code> is the smallest one. Similarly, among all entries that are assigned to target index <code>1</code>, the index <code>7</code> from the <code>source_tensor</code> is the smallest one.</p>

ptrblck: <p>Thanks for the explanation.<br>
Indeed the indices won’t be returned and it seems you might need to use another operation to return them as seen here:</p>
<pre><code class="lang-python">device = torch.device("cpu")

source_tensor = torch.tensor(
    [1.23, 2.68, 9.37, 5.84, 6.87, 5.21, 7.46, 2.33, 8.95],
    dtype=torch.float32,
    device=device,
)

indexes = torch.tensor(
    [0, 0, 1, 0, 0, 1, 0, 1, 1],
    dtype=torch.long,
    device=device,
)

target_tensor = torch.zeros(2, dtype=torch.float32, device=device)

target_tensor = torch.zeros(2, dtype=torch.float32, device=device)
target_tensor.index_reduce_(0, indexes, source_tensor, reduce="amin", include_self=False)
print((target_tensor.unsqueeze(1) == source_tensor.unsqueeze(0)).nonzero()[:, 1])
# tensor([0, 7])
</code></pre>
<p>I would also assume you would use a similar approach to get the indices or how are you returning the indices right now?</p>

jtl: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="174246">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>(target_tensor.unsqueeze(1) == source_tensor.unsqueeze(0)).nonzero()[:, 1]</code></p>
</blockquote>
</aside>
<p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> ,</p>
<p>Thank you for the prompt reply. Unfortunately, the source tensor and the target tensor in our case have more than 100K entries. Therefore, the suggested solution is not feasible in this case. Even it’s vectorized, but the memory and time complexities are O(N^2).</p>
<p>The current solution I’m using is a bit hacky. If one wants to keep the <code>min</code> or <code>max</code> using a specific channel, he/she can achieve that by hiding the indices in the lower digit and shift the value parts to the higher digits.</p>
<p>Of course the amount of left shifting depends on the precision you want to keep and the number of points you have. After the reduction, just remove the value parts and you get the inverse indices.</p>
<p>Not a good solution though, would be nice if they support inverse indices in the future.</p>

ptrblck: <p>Yes, the memory requirement could be large due to the broadcasting depending on the input tensor shapes.</p>
<p>Could you describe your “hacky” approach a bit more and maybe post a code snippet, please?</p>