Scheduling Forward and Backward in separate GPU cores

Vibhatha_Abeykoon: <p>Hi,</p>
<p>Is there a specific scheduling capability where I can define forward pass and backward pass to run on specific GPU devices? With model parallelism, it is clear that we can schedule the layers to be in specific GPU devices. But could we go further into the details? Without overriding, the backward function is it possible to give a config and train a neural network? This could involve a weight copy or activation copy to a separate GPU device to do the backward. Is this something possible with Autograd definitions and GPU scheduling in Pytorch?</p>

albanD: <p>Hi,</p>
<p>No this is not possible at the moment I’m afraid.<br>
What the engine does is to run the backward of a function on the same device (and cuda stream if it was on GPU) as the forward pass. This is a good heuristic as the backward pass is usually as expensive as the forward. So the splitting used in the forward is a good indication of how the backward should be split.</p>

Vibhatha_Abeykoon: <p><a class="mention" href="/u/alband">@albanD</a></p>
<p>Exactly. But if we not use backward and do all in manual passion, this is possible with<br>
multiple cuda streams? Is this possible?</p>

albanD: <blockquote>
<p>But if we not use backward and do all in manual passion,</p>
</blockquote>
<p>Not sure what you mean by this.<br>
If you don’t use the autograd engine and write everything by hand, you can do whatever you want as you write it by hand.</p>

Vibhatha_Abeykoon: <p>Yes that is true.</p>
<p>What I meant was using autograd function, on a given tensor and get it’s gradient update value.<br>
Generally what we do is, we have this leaf node or loss value and call backward().</p>
<p>So, it traverse back and calculate all gradients for us. Is this right?</p>
<p>If so, I can access w.grad and get the weight updates.</p>
<p>What if I manually go back and just go layer by layer by calling corresponding grad_function of each tensor (contributing to weights) by passing shape-matching inputs and get the grad value.</p>
<p>This way, I get more control over it. I don’t want to entirely loose the autograd functionality, but use it at node level to just get the corresponding weight vector update. Can this be possible?</p>

albanD: <p>I guess if you save every input/output pair for every function, you can do this by doing. If you have b = foo(a) and c = bar(b)<br>
<code>grad_b = autograd.grad(c, b, grad_c)</code><br>
<code>grad_a = autograd.grad(b, a, grad_b)</code><br>
And do that for every op if you have more. That will work, but might not be super efficient.</p>

Vibhatha_Abeykoon: <p>Your function representation is exactly what I explained.</p>
<p>One point to understand, why this could be very inefficient?</p>
<p>Inefficient in a sense of more time to write code or execution time will be slow because of not using<br>
some of the internal optimizations in Autograd?</p>

albanD: <p>Inefficient at execution time because the autograd engine has some overhead (to discover the work that needs to be done) that is going to be repeated many times here. Also usually, the backward pass runs only in cpp and without holding the python GIL, here, you will come back to python after each operation.</p>

Vibhatha_Abeykoon: <p>Great!. So if I look into a neat CPP implementation, I could not worry on the Python overhead, but still I have to deal with the overhead of autograd engine.</p>
<p>Could you please elaborate a bit on the overhead?</p>

albanD: <p>This overhead is mainly the discovery of what needs to be done to compute gradients. So it needs to traverse all the graph of computation, which takes a bit of time.</p>
<p>Note that if you’re simply experimenting, this overhead won’t kill you. But it won’t be 0.</p>

nirandaperera: <p><a class="mention" href="/u/alband">@albanD</a> I have a follow up question to this. So, AFAIU forward and backward pass happens inside the GPU device without holding the python GIL.</p>
<p>But how about the weight update operation done by an optimizer? Would optimizer access grads and params of each layer in the python environment, update the weights and push them back to the GPU? or would you translate the python logic to a cuda kernel and update the weights inside the GPU itself?</p>

albanD: <p>These are quite orthogonal things.<br>
If you’re using python, the Tensor is a python object. But if the Tensor is a GPU tensor, then the memory it works with is on the GPU. So when you perform operations on it, they will run on the GPU.</p>
<p>This discussion here about the GIL is unrelated. You can use the underlying cpp Tensor object and use it without touching the python object as well.</p>

Vibhatha_Abeykoon: <p><a class="mention" href="/u/alband">@albanD</a> I think this clears things. <img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"></p>

nirandaperera: <p>Hi <a class="mention" href="/u/alband">@albanD</a>,<br>
How much of work would it be to separate out forward and backward passes? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

albanD: <p>Like run the forward on one GPU and the backward on another?</p>

nirandaperera: <p>Yes, on separate GPUs.</p>

albanD: <p>That would be a LOT of work <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>What would be the benefit of doing this?</p>

Vibhatha_Abeykoon: <p><a class="mention" href="/u/alband">@albanD</a></p>
<p>For the sake of the argument of reducing the computation overhead in the backward,</p>
<p>If we can divide this into parts, the forward and the backward, can’t we get better performance.</p>
<aside class="quote no-group" data-username="albanD" data-post="17" data-topic="70922">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>That would be a LOT of work <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>Which APIs would be involved in such a task? How complex could this task be?<br>
Could you give insight with respect to the existing APIs?</p>

albanD: <p>I don’t see how you can get better performances.<br>
The forward and backward are sequential.<br>
And if you say you have 2 device and you want to overlap multiple forward backward. Then you can overlap them while keeping each fw/bw pair on the same device as well.</p>
<blockquote>
<p>Which APIs would be involved in such a task? How complex could this task be?<br>
Could you give insight with respect to the existing APIs?</p>
</blockquote>
<p>The whole backward pass assumes that intermediary gradients match the intermediary results in the forward pass. Also if some buffers were saved, they would need to be moved from different device.<br>
This will for sure not be accepted as a PR (unless you can show a use case where it brings massive speed improvement). And hacking it might still be quite hard.</p>

Vibhatha_Abeykoon: <p>What if we use pipeline mechanism to load micro-batches instead of mini-batches?<br>
And do the final weight vector update at the end of the mini-batch.</p>
<p>Then that sequential relationship can still be maintained (as it cannot be broken because of weight vector update requirement). But when backward takes more time, more GPUs’ can do backward task and reduce backward timing. Isn’t this a practical use-case where we could improve performance?</p>