GPU + CPU computation sharing (CNN)

tiramisuNcustard: <p>Can I carry out multiplication of the fully connected layer in CPU? For example, I am assuming that if there are 4 GPUs available - matrix multiplications in the convolution layer might be distributed among 3 GPUs and the matrix multiplications of the fully connected layer might be allocated to the 4th GPU.</p>
<p>What if there are only 3 GPUs available - can the matrix multiplication of the fully connected layer be done using the RAM+CPU (assuming enough memory/processing power is available) while the GPUs are used for convolution layer matrix multiplications? Does PyTorch does this automatically? If not, can I instruct the machine to use the RAM+CPU for the fully connected layer multiplications? Can this help speed up runtime?</p>
<p>Thank you.</p>

ptrblck: <aside class="quote no-group" data-username="tiramisuNcustard" data-post="1" data-topic="163059">
<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/t/6a8cbe/48.png" class="avatar"> tiramisuNcustard:</div>
<blockquote>
<p>Does PyTorch does this automatically?</p>
</blockquote>
</aside>
<p>No, PyTorch does not apply model sharding automatically and you would need to apply it.<br>
Based on your description it seems you are looking for something like pipeline parallelism?<br>
If so, then you could certainly use the CPU for specific layers too, but would need to check if you could get the expected performance as the data transfer between the host and device could be expensive compared to the actual operations.</p>
<p>Maybe something like a data parallel approach would be a good starter?</p>

tiramisuNcustard: <p>Thank you.</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="163059">
<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>and you would need to apply it</p>
</blockquote>
</aside>
<p>So, will something like the following work if I wanted to carry out the FC (fully connected layer) operations on the CPU?</p>
<pre><code class="lang-auto">cpu = torch.device("cpu")
...
...
x = self.fc1(x)
...
...
self.fc1.to(cpu)
</code></pre>

agu: <p>Your code snippet looks like it will run the FC layer’s forward pass before moving the FC layer to CPU, which means that if the FC layer is already on GPU, then the <code>x = self.fc1(x)</code> computation will happen on GPU. If you want the computation to happen on CPU, then you should call <code>self.fc1.to(cpu)</code> before <code>x = self.fc1(x)</code>.</p>
<p>However, as <a class="mention" href="/u/ptrblck">@ptrblck</a> mentioned, the host-to-device (H2D) / device-to-host (D2H) transfers may become a bottleneck. More specifically, if there are data dependencies, e.g. the output of a FC layer is the input to a convolutional layer or vice versa, then upcoming computations must wait for either a H2D or D2H transfer for their inputs. Without careful pipelining, this may lead to an overall slowdown compared to simply running all computations on GPU.</p>
<p>Instead, you may want to consider data parallelism to help leverage your available hardware. For example, if you currently have batch size <code>N</code> and have 3 GPUs available, you may wrap your model with <a href="https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html" rel="noopener nofollow ugc"><code>DistributedDataParallel</code></a> (DDP) and use a <a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.distributed.DistributedSampler" rel="noopener nofollow ugc"><code>DistributedSampler</code></a> to distribute the dataset across the 3 GPUs, where you now specify the batch size of your dataset to be <code>N / 3</code>. This should allow you to run through your dataset up to 3x as fast. The <a href="https://pytorch.org/tutorials/intermediate/ddp_tutorial.html" rel="noopener nofollow ugc">DDP tutorial</a> may be a helpful starting point if you are not already familiar.</p>