What is an efficient way to average over subtensors of a torch.nn.Parameter?

PersonalFinance004: <p>I have a torch.nn.Parameter <code>P</code> of shape (10, 50). (50000 replaced by 10 for the sake of easier explainability).<br>
Conceptually, each index of the first dimension corresponds to an ‘embedding’ of size (1, 50).<br>
My model has two ‘paths’ to the output, path one of which uses all of the 10 embeddings individually, whereas the second path is supposed to use averages over certain neighboring embeddings. My problem is with the second path, I only mentioned the first so it is clear why I need to ‘average’ at all.<br>
I have a dictionary</p>
<pre><code class="lang-auto">d = dict({0:[0], 1:[2,3,4], 2:[5,6,7,8,9]})
</code></pre>
<p>in which each key serves as an index i of a group of embeddings belonging together (i.e. which should be averaged), and the values are the indices of the embeddings which I would like to take the average of.</p>
<p>My forward function gets passed a <code>batch</code> of indices, each of which corresponds to a key of <code>d</code>. Now I want to get the averaged embedding for each index in the batch, to further process it in my forward function.</p>
<p>I tried the following:</p>
<pre><code class="lang-auto">averaged_embeddings = []
for id in batch:
    emb_idxs = d[id]
    emb_count = len(emb_idxs)
    embeddings = torch.narrow(P, 0, emb_idxs[0], emb_count)
    average = torch.sum(embeddings, dim=0) / emb_count
    average = average.view(1,5)
    averaged_embeddings.append(average)
batch_of_averaged_embeddings = torch.cat(averaged_embeddings,0)

</code></pre>
<p>The problem is that this seems to be incredibly inefficient. My batch sizes are in the 10000s, and in one epoch I process more than 1000 batches. A single epoch would now take ~2.5 hours instead of 30 seconds, which was the time without using these averages.</p>
<p>Is there a better way to accomplish what I want to do? For a given index from the batch, it’s always the same embeddings which are averaged. So maybe I could first check which groups appear in the batch, then do the averaging once, and fill the tensor with copies of the result. But is there more I can do to make this faster, maybe something completely different?<br>
Any help would be appreciated.</p>

googlebot: <p>Seems like you only need matrix multiplication here, Weights @ P = averages. Weights of shape (batch, num_embeddings) should replace your d dict and contain “contributions”, that are either zero or 1/emb_count, to get masked averages.</p>

PersonalFinance004: <p>Thank you for the suggestion. I have now tried to do the following:</p>
<ol>
<li>Initialize a weight tensor and</li>
<li>adjust the forward function accordingly. But now I run into memory problems (see end of post):</li>
</ol>
<p>Initializing weights:</p>
<pre><code class="lang-auto">group_ids = d.keys()
weights_shape = (len(group_ids), P.shape[0])
weights = torch.zeros(size=weights_shape, dtype=torch.double, requires_grad=False, device='cuda')

for group_id in group_ids:
    number_of_relevant_embs = len(d[group_id])
    id_of_first_relevant_emb = d[group_id][0]
    id_of_last_relevant_emb = id_of_first_relevant_emb+number_of_relevant_embs
    weights[group_id][id_of_first_relevant_emb:id_of_last_relevant_emb] = 1/number_of_relevant_embs
</code></pre>
<p>Adjusted part of forward function (given <code>batch</code> of <code>group_id</code>s):</p>
<pre><code class="lang-auto">weight_batch = weights[batch]
batch_of_averaged_embeddings = torch.matmul(weight_batch, embeddings)
</code></pre>
<p>Unfortunately, since weights_shape is around (30000,40000), this approach already fails when trying to create the <code>weights</code> torch.zeros tensor, with a CUDA out of memory RuntimeError (wants to allocate around 10GB for this, which is almost the entire memory of my GPU).</p>
<p>Is what I did what you meant in the first place?</p>

googlebot: <aside class="quote no-group" data-username="PersonalFinance004" data-post="3" data-topic="79902">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/d6d6ee/48.png" class="avatar"> PersonalFinance004:</div>
<blockquote>
<p>Is what I did what you meant in the first place?</p>
</blockquote>
</aside>
<p>Yes. With that shape, maybe sparse weights matrix will work. Another option is doing matrix multiplication block by block, but that may not help if you’re recording P gradients for this. Your initial code can also be reformulated as doing matmul row by row (dot product) - if you use JIT, remove dict lookup and list append (without gradients - you can write results to preallocated tensor, with gradients - torch.cat may be unavoidable), it should be reasonably fast…</p>

PersonalFinance004: <p>I tried the sparse matrix approach today, but I couldn’t get it to work because apparently there is no way to extract a batch from it like so:<br>
<code>weight_batch = sparse_weight_matrix[batch]</code><br>
which I would need to get one set of weights per <code>group_id</code> in the <code>batch</code>.</p>

googlebot: <p>I have rather ment weights matrix created per batch. Then won’t <code>sparse_weights.mm(P)[batch]</code> work? It is kinda weird, as one mm() is really needed per epoch, but training with batches complicates things…</p>

googlebot: <p>Also, you may try <code>batch_map.smm(sparse_weights).mm(P)</code>, where batch_map - sparse binary tensor of shape (batch_size,len(group_ids)). That’s just a clunky way to index sparse_weights, not sure if there are better ways…</p>

PersonalFinance004: <p>I tried three more things.</p>
<pre><code class="lang-auto">sparse_weights.mm(P)[batch]
</code></pre>
<p>as you suggested does indeed work. One epoch now takes ~3min20secs instead of the original 30 seconds, which is still not great, but better than the other options I tried. I have to determine whether this is really worth it.</p>
<p>The approach with</p>
<pre><code class="lang-auto">batch_map.smm(sparse_weights).mm(P)
</code></pre>
<p>did not work, because apparently <code>.smm</code> / <code>.spmm</code> are no longer available. There exists some external library for handling sparse matrix multiplication, but apparently there is a problem with it properly handling gradients.</p>
<p>One other thing I tried is the following: I realized that of the 30000 groups, only approximately 2000 are occurring in one <code>batch</code> of size ~35000.<br>
So given a batch, I extracted these unique indices into a list, converted this list to a dense one-hot matrix, and multiplied it with the sparse <code>group_weights</code> matrix to get only the relevant weights. The resulting dense matrix multiplied with <code>P</code> gave me the 2000 relevant averaged embeddings, from which I could extract the batch of averaged embeddings corresponding to <code>batch</code>. Basically I though that by not calculating all averaged embeddings I could save time.<br>
Unfortunately, contrary to what I had hoped, this approach took much longer again, with 30minutes per epoch.</p>

googlebot: <p>Your problem is probably elementwise assignments to cuda tensor, like you do with dense weights above. You should do these on cpu tensor, for one-hot you can even use bool dtype, then do .to(device=“cuda”, dtype=XXX). Actually, I don’t know if such sparse/dense matmul is faster on cuda, especially with float64.</p>

PersonalFinance004: <p>Sorry, where do I do these elementwise assignments you’re speaking of?</p>

googlebot: <p>assignments to slices, to be exact; anything like:</p>
<pre><code class="lang-auto">`weights[group_id][id_of_first_relevant_emb:id_of_last_relevant_emb] = 1/number_of_relevant_embs`
</code></pre>
<p>in a loop, such statements produce a lot of cuda commands</p>