Backward computation 10x slower than Forward

3yanlis1bos: <p>Hi,</p>
<p>I have been trying to debug to find out why it takes too long to train a CNN regressor with the following training code:</p>
<pre><code class="lang-auto">img = img.cuda()
img = Variable(img)

output = model(img)
target = target.type(torch.FloatTensor).unsqueeze(0).cuda()
target = Variable(target)

loss = criterion(output, target)

losses.update(loss.item(), img.size(0))

optimizer.zero_grad()
loss.backward()
optimizer.step()
</code></pre>
<p><strong>The problem</strong></p>
<p>This is a blocking issue for my research since increases the training time in unacceptable amounts.<br>
(former training time : 15h-18h, current: 45h-52h)</p>
<p>I have used <code>perf_counter</code> to measure the time needed for each line of above code and realized that backpropagation takes ~10x of the time needed for forward pass. I know that backprop is expected to take longer than forward pass, but I believe this difference is a bit too much.</p>
<p><strong>The tests</strong></p>
<p>Thinking that it might be an infrastructural problem, I also have ran my code at colab environment. The result was more or less the same. Here are the timings of training steps for first 7 images from the first epoch of both trainings.</p>
<pre><code class="lang-auto">colab              local                   
forward  backward  forward  backward                                      
0.077s   0.818s    0.071s   0.699s         
0.025s   0.238s    0.095s   1.043s         
0.071s   0.751s    0.066s   0.751s         
0.046s   0.486s    0.063s   0.685s         
0.073s   0.778s    0.060s   0.619s         
0.039s   0.385s    0.015s   0.139s         
0.077s   0.818s    0.019s   0.176s         
</code></pre>
<p>I appreaciate any help on finding or rectifying the flaw.</p>

albanD: <p>Hi,</p>
<p>It is hard to say without any more information.<br>
Can you share a small script that reproduces this on colab so that we can inspect it in more details?</p>

3yanlis1bos: <p>Hi Alban,</p>
<p>Thank you very much for reaching out.</p>
<p>I would love to, but currently I do have a repro on colab but I can not get that without my training set, which I access over my own gdrive. I will soon try to find a way. I am open to advices on that for sure.</p>
<p>Here is some good news though: I was able to eliminate the decrease in speed. Now my training is as fast as before. I have managed to fix the problem using one of my old environment.yml files. I had already checked the hardware, data, ubuntu and conda itself (by reinstalling all of them) but it was my environment after all.</p>
<p>What it all means to me is that there is a library (or are libraries) that causes the training to be 5x slower. I might be able to find that certain library/ies by gradually upgrading my environment. I will investigate further once I am done with my current project.</p>
<p>Here are links to my <a href="https://gist.github.com/BedirYilmaz/c603094de13bbe56cf0beccd1d51be5b" rel="nofollow noopener">old</a> and <a href="https://gist.github.com/BedirYilmaz/80dfbcf46d7daa95d697fbf3f0298521" rel="nofollow noopener">new</a> environments.</p>
<p>Btw, I want to emphasize that the problem persisted when I tried to get a repro in colab. This might mean that something interesting going on here like, something in one of the up-to-date libraries might be slowing down the training of CSRNet.</p>

ptrblck: <p>By skimming through both environments I think the main difference is:</p>
<p>Old environment:</p>
<ul>
<li><code>pytorch=0.4.0=py36hdf912b8_0</code></li>
<li>
<code>cuda90=1.0=h6433d27_0</code> and <code>cudatoolkit=9.0=h13b8566_0</code>
</li>
<li><code>cudnn=7.1.2=cuda9.0_0</code></li>
</ul>
<p>New environment:</p>
<ul>
<li><code>pytorch=1.4.0=py3.8_cuda10.1.243_cudnn7.6.3_0</code></li>
<li><code>cudatoolkit=10.1.243=h6bb024c_0</code></li>
</ul>
<p>Besides an older PyTorch, CUDA, and cudnn version, there were also two <code>cuda</code> packages installed and I’m not sure how they interact with each other.</p>
<p>Also, did you add synchronizations to your code when profiling manually?<br>
Since CUDA operations are asynchronous, you would have to synchronize via <code>torch.cuda.synchronize()</code> before starting and stopping the timer.</p>

3yanlis1bos: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>thank you for your attention. I agree with you on main differences. I am also surprised about how old version of those core libraries makes my code run faster.</p>
<p>Here is my measurement code. I have summarized the result by discarding insignificant timings (&gt;0.01) when I was reporting them, for the sake of simplicity.</p>
<pre><code class="lang-auto">a = time.perf_counter()
img = img.cuda()
img = Variable(img)
target = target.type(torch.FloatTensor).unsqueeze(0).cuda()
target = Variable(target)
torch.cuda.synchronize()
b = time.perf_counter()

print('data conversion {:.3f}s '.format(b - a), end='')

a = time.perf_counter()
output = model(img)
torch.cuda.synchronize()
b = time.perf_counter()

print('model forward pass {:.3f}s '.format(b - a), end='')

a = time.perf_counter()
loss = criterion(output, target)
losses.update(loss.item(), img.size(0))
torch.cuda.synchronize()
b = time.perf_counter()

print('loss {:.3f}s '.format(b - a), end='')

a = time.perf_counter()
optimizer.zero_grad()
torch.cuda.synchronize() 
b = time.perf_counter()

print('zerograd {:.3f}s '.format(b - a), end='')

a = time.perf_counter()
loss.backward()
optimizer.step()
torch.cuda.synchronize() 

b = time.perf_counter()
print('backprop {:.3f}s '.format(b - a), end='')
a = time.perf_counter()
optimizer.step()
torch.cuda.synchronize() 
b = time.perf_counter()

print('step {:.3f}s'.format(b - a))
</code></pre>
<p><strong>Here is how the timing looked like originally</strong></p>
<pre><code class="lang-auto">local

data conversion 0.001s model forward pass 0.045s loss 0.001s zerograd 0.000s backprop 0.496s step 0.002s
data conversion 0.002s model forward pass 0.067s loss 0.000s zerograd 0.000s backprop 0.762s step 0.003s
data conversion 0.000s model forward pass 0.015s loss 0.000s zerograd 0.000s backprop 0.115s step 0.002s
data conversion 0.001s model forward pass 0.071s loss 0.000s zerograd 0.000s backprop 0.759s step 0.003s
data conversion 0.000s model forward pass 0.015s loss 0.000s zerograd 0.000s backprop 0.133s step 0.003s
data conversion 0.001s model forward pass 0.058s loss 0.000s zerograd 0.000s backprop 0.589s step 0.003s
</code></pre>