PyTorch Versions CUDA Forward Time

antoalli: <p>Hi!<br>
I’m working on the official DGCNN (Wang et Al.) PyTorch implementation but I’m encountering strange behaviours among different PyTorch versions (always using Cuda compilation tools V10.1.243).<br>
For some reason the CUDA forward time of a batch seems to be higher (ms --&gt; s) when moving to newer pytorch versions (from 1.4 going on). The tested code is exactly the same, the cuda toolkit also. Any idea why this happens? Thanks!</p>
<p>PyTorch 1.3.1 Forward pass Profiling:<br>
Self CPU time total: 6.875ms<br>
CUDA time total: 264.906ms</p>
<p>PyTorch 1.4 Forward pass Profiling:<br>
Self CPU time total: 27.976ms                                                                                                                        CUDA time total: 1.498s</p>
<p>PyTorch 1.5 Forward pass Profiling:<br>
Self CPU time total: 12.93ms                                                                                                                        CUDA time total: 1.434s</p>
<p>(Those are just time measurement for forwarding a batch through the model, the trend is that for newer version of pytorch (&gt;1.4) CUDA time total is in seconds while for older (&lt;1.3.1) is in ms )</p>

albanD: <p>This is quite surprising indeed.<br>
Do you have a small code sample (30 lines) that we could use to reproduce this?</p>

antoalli: <p>Hi, reproducing it into 30 lines of code is quite challenging, I’m trying to isolate the specific part causing this ‘slow forward’ but at the moment I didn’t had success.<br>
Here you can see a detailed profiling of all ops done during forward (<a href="https://pastebin.com/RYRFFruE" rel="nofollow noopener">https://pastebin.com/RYRFFruE</a>).<br>
The model I’m using is the ‘DGCNN’ from <a href="https://github.com/WangYueFt/dgcnn/blob/master/pytorch/model.py" rel="nofollow noopener">https://github.com/WangYueFt/dgcnn/blob/master/pytorch/model.py</a> (Wang et Al.), in the forward a nearest neighboors graph is constructed at each layer. Still trying to understand if the problem is in layers forward or in some of the ops used for graph construction.<br>
Thank you</p>
<p>Edit:<br>
If I exclude the graph construction part (using random tensor) forward performances seems to be the comparable between 1.2 and 1.5. At the same time, by profiling the function used for graph construction between the two versions I’m not experiencing very different behaviours… I’m confused</p>

albanD: <p>Interesting. Do both of them build the same graph? Or could there be some discrepancies here by any chance?</p>

antoalli: <p>Nope, the graph is constructed in the same way. Of course the graph depends on the input. I also tried to substitute my input data with torch.rand() tensor and then measure the forward time (results in the pastebin) and also in this case seems that pytorch 1.2 provides way better performances than 1.5.<br>
Any idea on how to further debug this? Thank you! <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=12" title=":blush:" class="emoji" alt=":blush:" loading="lazy" width="20" height="20"></p>
<p>EDIT 1:<br>
Investigating a little bit I’ve produced some tracing plot for PyT 1.2 and 1.5… The problem seems to be in batch normalization layer… am I wrong?</p>
<p>PyTorch 1.2 cudnn enabled:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/6/a6377521a10a61b10533fc7fe45511f319e913eb.png" data-download-href="https://discuss.pytorch.org/uploads/default/a6377521a10a61b10533fc7fe45511f319e913eb" title="cuda12trace"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6377521a10a61b10533fc7fe45511f319e913eb_2_690x145.png" alt="cuda12trace" data-base62-sha1="nIq4Nhc6bddh2rxQca8IpeZqrJp" width="690" height="145" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6377521a10a61b10533fc7fe45511f319e913eb_2_690x145.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6377521a10a61b10533fc7fe45511f319e913eb_2_1035x217.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a6377521a10a61b10533fc7fe45511f319e913eb_2_1380x290.png 2x" data-dominant-color="E2E0E3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">cuda12trace</span><span class="informations">2760×582 86.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
PyTorch 1.5 cudnn enabled:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02e61362e18dc2ee54735a59edb35cf087c0e437.png" data-download-href="https://discuss.pytorch.org/uploads/default/02e61362e18dc2ee54735a59edb35cf087c0e437" title="cuda15trace"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02e61362e18dc2ee54735a59edb35cf087c0e437_2_690x105.png" alt="cuda15trace" data-base62-sha1="pDTaPne9NYC1eI8HGCVZ0vcTNJ" width="690" height="105" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02e61362e18dc2ee54735a59edb35cf087c0e437_2_690x105.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02e61362e18dc2ee54735a59edb35cf087c0e437_2_1035x157.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02e61362e18dc2ee54735a59edb35cf087c0e437_2_1380x210.png 2x" data-dominant-color="E0DEDF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">cuda15trace</span><span class="informations">2954×452 69.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Edit 2:<br>
Interestingly… by setting ‘torch.backends.cudnn.enabled = False’ performance gets better (half the CUDA time) with PyTorch 1.5, still more time than with PyTorch 1.2 and cudnn enabled.</p>

albanD: <p>Do you use the same cudnn version in both cases?<br>
Also make sure to run your code a few times before doing the timing. cudnn benchmarking will run the forward multiple times to find the best kernel if it is enabled.</p>
<p>What is the input size for the batchnorm? Does running just the batchnorm with this input size gives similar behavior?</p>

antoalli: <p>PyTorch 1.2 CUDNN 7602, cudnn.enabled=True ==&gt; OK<br>
PyTorch 1.3 CUDNN 7603, cudnn.enabled=True ==&gt; OK<br>
PyTorch 1.5 CUDNN 7603, cudnn.enabled=True ==&gt; NOT OK (slow forward)</p>
<p>I’ve tried those configurations… seems that the problem is not in CUDNN but somewhere in torch 1.5. Actually I perform BatchNormalization after each layer so there are more than one BN layer, here is the model:<br>
DGCNN(<br>
(conv1): Sequential(<br>
(0): Conv2d(6, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): LeakyReLU(negative_slope=0.2)<br>
)<br>
(conv2): Sequential(<br>
(0): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): LeakyReLU(negative_slope=0.2)<br>
)<br>
(conv3): Sequential(<br>
(0): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): LeakyReLU(negative_slope=0.2)<br>
)<br>
(conv4): Sequential(<br>
(0): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): LeakyReLU(negative_slope=0.2)<br>
)<br>
(conv5): Sequential(<br>
(0): Conv1d(512, 1024, kernel_size=(1,), stride=(1,), bias=False)<br>
(1): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): LeakyReLU(negative_slope=0.2)<br>
)<br>
(cls): Sequential(<br>
(0): Linear(in_features=2048, out_features=512, bias=False)<br>
(1): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): LeakyReLU(negative_slope=0.2)<br>
(3): Dropout(p=0.5, inplace=False)<br>
(4): Linear(in_features=512, out_features=256, bias=True)<br>
(5): BatchNorm1d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(6): LeakyReLU(negative_slope=0.2)<br>
(7): Dropout(p=0.5, inplace=False)<br>
(8): Linear(in_features=256, out_features=40, bias=True)<br>
)<br>
)</p>
<p>I’ll keep investigating in profiling the batchnorm layers forward. The problem is not in cudnn benchmark, this is not the first forward and I also tried to disable it.<br>
Actually I’m stuck with PyTorch 1.3 and some libraries working only with torch &gt; 1.4…</p>

albanD: <p>Well the benchmark you show seems to indicate that the batchnorm function is the problem. If you have cudnn enabled, then pytorch does nothing and just forwards to it.</p>
<p>What is the input size for the batchnorm? Does running just the batchnorm with this input size gives similar behavior?</p>

ptrblck: <p>Could you also post the model code, please?<br>
I cannot infer the connection between <code>conv4</code> and <code>conv5</code>, as you are switching from 2D layers to 1D ones and also the input channels do not match the output channels between the <code>convX</code> blocks.</p>

antoalli: <p>You’re right! There are some max ops. in between, then the max-feat from the 4 conv. layers are concatenated and fed to the Conv1d.<br>
Input is a batch of PointClouds so a tensor with dim. [numShapesInBatch, 3, numPointsPerShape]</p>
<p>This is the forward code:</p>
<pre><code class="lang-python">def forward(self, x):
        batch_size = x.size(0)
        x = get_graph_feature(x, k=self.k)  # (batch_size, 3, num_points) =&gt; (batch_size, 3*2, num_points, k)
        x = self.conv1(x)  # (batch_size, 3*2, num_points, k) =&gt; (batch_size, 64, num_points, k)
        x1 = x.max(dim=-1, keepdim=False)[0]  # (batch_size, 64, num_points, k) =&gt; (batch_size, 64, num_points)

        x = get_graph_feature(x1, k=self.k)
        x = self.conv2(x)
        x2 = x.max(dim=-1, keepdim=False)[0]  # =&gt; (batch_size, 64, num_points)

        x = get_graph_feature(x2, k=self.k)
        x = self.conv3(x)
        x3 = x.max(dim=-1, keepdim=False)[0]  # =&gt; (batch_size, 128, num_points)

        x = get_graph_feature(x3, k=self.k)
        x = self.conv4(x)
        x4 = x.max(dim=-1, keepdim=False)[0]  # =&gt; (batch_size, 256, num_points)

        x = torch.cat((x1, x2, x3, x4), dim=1)  # =&gt; (batch_size, 64+64+128+256, num_points)

        x = self.conv5(x)  #(batch_size, 64+64+128+256, num_points) =&gt; (batch_size, emb_dims, num_points)
        x1 = F.adaptive_max_pool1d(x, 1).view(batch_size, -1)
        x2 = F.adaptive_avg_pool1d(x, 1).view(batch_size, -1)
        x = torch.cat((x1, x2), 1)

        # Classification Forward
        x = self.cls(x)
        return x
</code></pre>
<p>(P.S. the code is from <a href="https://github.com/WangYueFt/dgcnn/blob/20fdb459ca5d10fe8aba1d296e66340f65990b85/pytorch/model.py#L88" rel="nofollow noopener">https://github.com/WangYueFt/dgcnn/blob/20fdb459ca5d10fe8aba1d296e66340f65990b85/pytorch/model.py#L88</a> )</p>