Slow backward() on better hardware

arrowtea: <p>I am experiencing a 3-4x slower speed when applying the following simple code on newer hardware:</p>
<pre><code class="lang-auto">import torch
import time

device = torch.device('cuda:0')

class NNClass(torch.nn.Module):
    def __init__(self):
        super(NNClass, self).__init__()
        self.fc1 = torch.nn.Linear(3, 128)
        self.fclast = torch.nn.Linear(128, 3)

    def forward(self, inp):
        out = self.fc1(inp)
        return self.fclast(out)

theNN = NNClass().to(device)
lossfunc = torch.nn.MSELoss()
inp = torch.zeros(size=(64, 3), device=device)
labels = torch.zeros(size=(64, 3), device=device)

tic = time.perf_counter()
for idx in range(10000000):
    Fout = theNN(inp)
    loss = lossfunc(Fout, labels)
    loss.backward()

    if idx % 1000 == 0:
        print(f'idx: {idx}')
    if idx == int(3e4):
        break

toc = time.perf_counter()
print(f'wallclock: {toc - tic}')
</code></pre>
<p>The inferior hardware is a laptop (Lenovo X1 Carbon) connected to an eGPU containing a 1080ti, and the superior hardware is a desktop with an i9-10940x processor (with an ASUS Pro WS X299 Sage II motherboard) with three GPUs – two 2080ti’s and one 1080ti. Both systems are running Ubuntu 18.04 with nvidia driver 460, and both have PyTorch installed using Anaconda with the instructed install command:</p>
<pre><code class="lang-auto">conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch
</code></pre>
<p>Basically, when I run the exact same code on both systems, the one with superior hardware is about 3-4x slower when training with the GPU (I trained with one GPU for the superior hardware). The inferior hardware runs in about 12 seconds, while the superior hardware finishes in 46 seconds. As a sanity check, when I train both systems with the CPU only, the superior hardware wins as expected.</p>
<p>Any help is much appreciated!</p>

albanD: <p>Hi,</p>
<p>Which version of pytorch are you using?<br>
Also which version of cudnn are you using on each machine (if you have a custom one)?</p>

arrowtea: <p>Thanks for the reply! It looks like PyTorch is 1.7.1 and cuDNN is 8.0.5, for both machines. When I run ‘conda list’ on both, I see version 1.7.1 with the build py3.8_cuda11.0.221_cudnn8.0.5_0 from the pytorch channel.</p>

albanD: <p>That looks good.</p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> any idea what could be causing this?</p>

ptrblck: <p>I would try to lower the number of variables a bit to further isolate the issue.<br>
If I understand the use case correctly, <a class="mention" href="/u/arrowtea">@arrowtea</a> has already used the same PyTorch, CUDA, cudnn versions on both machines. The only difference would be the hardware.</p>
<p>Could you run the test on the 1080 in your desktop and compare it to the 1080 in the laptop?</p>

arrowtea: <p>On the desktop I ran it with a 1080ti and got the same results.</p>
<p>I just tried comparing Tensorflow code, and I also got the same results – the desktop was slower than the laptop (checking nvidia-smi to make sure the appropriate GPUs were used). Sorry for the trouble, as this is a PyTorch forum. I’ll go ahead see if I can find my solution from perhaps Nvidia support.</p>
<p>But if anyone has suggestions for solutions or perhaps where I can start to look for solutions, please let me know! Thank you!</p>

ptrblck: <p>“The same results” means that your desktop is slower using the 1080 vs. your laptop with a 1080?<br>
If so, I would profile your code and check it for other bottlenecks besides the GPU. E.g. in case you are using a fast SSD in your laptop vs. an old HDD in your desktop, the performance of the data loading pipeline would tank.</p>
<p>EDIT: you could also profile the achieved memory bandwidth on both systems for the GPU and check, if the used hardware is a limiting factor (e.g. by sharing the PCIe connection etc.).</p>

arrowtea: <p>Yeah, using the desktop with the 1080 was slower than the laptop with the 1080. I’m also using an SSD for both the desktop and laptop.</p>
<p>In any case, I examined the CPU usage of both the laptop and the desktop and saw the laptop under load was working at about 4.4 GHz for all CPUs while the desktop under load was staying at the minimum clock, 1.2 GHz for all CPUs (all this is training the with GPUs). This prompted me to check the CPU frequency ratios in the bios.</p>
<p>So I went into the bios and instead of using the default CPU frequency ratios of Auto, I manually set them to the advertised setting of (4.8 GHz) which seemed to fix things in PyTorch. And actually, when I was stress testing the CPUs under the Auto setting, I saw that the maximum it would achieve for an individual CPU is about 4.2GHz. So I went back and manually set the CPU frequency ratios to 4.1 GHz just as a test, and even here I saw a x2 speedup over the Auto setting.</p>
<p>So definitely something fishy is going on with the motherboard.</p>
<p>Also, what’s even weirder is the PyTorch code did get sped up, but the Tensorflow code did not. Pretty weird!</p>
<p>Thanks for your the suggestions though!</p>