Computing backward() in parallel

rangwani_harsh: <p>Hi,<br>
I have a use case in which I have multiple loss functions on which I have to call backward without using any reduction like mean or sum. I want to calculate gradients for different losses parallely.</p>
<pre><code class="lang-auto">losses = [loss1, loss2, loss3]
losses.backward()

print(param.grad)
## It should contain the jacobian of the gradients

</code></pre>
<p>In particular I am trying to implement this functionality in pytorch <a href="https://github.com/tianheyu927/PCGrad/blob/c5fbd7c856526373828074f06875230f7f3ee79e/PCGrad_tf.py#L39" rel="nofollow noopener">https://github.com/tianheyu927/PCGrad/blob/c5fbd7c856526373828074f06875230f7f3ee79e/PCGrad_tf.py#L39</a></p>
<p>Is it possible to do it parallely without a for loop?</p>

albanD: <p>Hi</p>
<p>What do you expect the grad to contain exactly? The sum of the gradient for the 3 losses?</p>

rangwani_harsh: <p>I want grad to exactly contain the stack of gradients wrt to three losses. How I am doing it at present is this way:</p>
<pre><code class="lang-auto">    flattened_grads = [torch.Tensor([]).to(device) for i in range(len(losses))]
    
 

    # Accumulate the gradient vectors
    for i, loss in enumerate(losses):
        for optim in optimizers:
            optim.zero_grad()
        loss.backward(retain_graph = True)
        for net in nets:
            for name, param in net.named_parameters():
                flattened_grads[i] = torch.cat([flattened_grads[i],param.grad.view(-1)])
</code></pre>
<p>I want to optimize this code to run faster and possibly in parallel. As the computation graphs are similar I should ideally backprop each node once.</p>

albanD: <p>If you want three different gradients computed, you will need to call backward 3 times I’m afraid <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>
<p>Since they are dependent, each Node needs to be recomputed. So “running in parallel” would only run once a Node that is 3 times bigger. So not leading to any improvement.</p>

rangwani_harsh: <p>There is a tf way of doing this used here <a href="https://github.com/tianheyu927/PCGrad/blob/c5fbd7c856526373828074f06875230f7f3ee79e/PCGrad_tf.py#L39" rel="nofollow noopener">https://github.com/tianheyu927/PCGrad/blob/c5fbd7c856526373828074f06875230f7f3ee79e/PCGrad_tf.py#L39</a>. Isn’t something like this possible in Pytorch?</p>
<p>If not ,then are there anyother ways to optimize the above code my gpu still runs at around 50% usage and increasing the batch size is deterimental to my results?</p>

albanD: <p>In general in pytorch, multiple backward pass cannot run concurrently in different threads so there is no benefit in adding such functionality <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"><br>
We are starting working on this but this is quite tricky as it have very deep implications on how the low level engine behaves.</p>
<p>More generally, pytorch is not great for small workloads at the moment as the overhead of the framework is non negligible in eager mode. But we are working on it <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

rangwani_harsh: <p>They seem to be doing this <a href="https://github.com/f-dangel/backpack" rel="nofollow noopener">https://github.com/f-dangel/backpack</a> and they too <a href="https://github.com/ChenAo-Phys/pytorch-Jacobian" rel="nofollow noopener">https://github.com/ChenAo-Phys/pytorch-Jacobian</a>.<br>
But they don’t support my use case directly. Any method based on this suitable here ?</p>

albanD: <p>You can also use <code>torch.autograd.functional.jacobian()</code> if you use pytorch &gt;1.5.<br>
But it will still be done using a for-loop under the hood I’m afraid.</p>

rangwani_harsh: <p>They claim to be doing better then  <a href="https://github.com/f-dangel/backpack" rel="nofollow noopener">https://github.com/f-dangel/backpack</a>  for loop’s. Hope that pytorch adds this feature soon.</p>

albanD: <p>I don’t remember backpack being able to compute multiple losses at the same time in the general case.<br>
In special cases they are able to compute what would require using a naive approach multiple backward passes in a single one. But these are only special cases.</p>

kfxw: <p>Hi! Greetings from more than one year later!</p>
<p>The functionality you talked about is exactly something what I need now. Do you have any progress on this functionality?</p>
<p>Thanks!</p>

kfxw: <p>I’ve just found a torch.vmap() in a newer version of pytorch. I’ll try that first.</p>

kkontras: <p>Any luck with this? It would be quite beneficial to access the gradients of the three losses separately and using the for loop just pile up the computational time of each backward</p>

albanD: <p>Hey!<br>
If the losses are fully independent, there isn’t much better that can be done than running each of them I’m afraid.<br>
If they’re related, I would suggest using vmap or tools like backpack or element-wise gradients from core.</p>

kkontras: <p>A work-around I found these days was replicating some parts of the network and load_state from the main branch, then assigning the combination of gradiets (possibly as just a summation or whatever) to the main branch by hand. This way you trade-off speed with memory. It works well but its still a trade-off. It would be interesting to see such implementation on pytorch directly.</p>