Skip to content
Advertisement

Training a simple model in Tensorflow GPU slower than CPU

I have set up a simple linear regression problem in Tensorflow, and have created simple conda environments using Tensorflow CPU and GPU both in 1.13.1 (using CUDA 10.0 in the backend on an NVIDIA Quadro P600).

However, it looks like the GPU environment always takes longer time than the CPU environment. The code I’m running is below.

JavaScript

Here are some outputs printed if they’re any indicative of what’s happening:

For the CPU run:

JavaScript

For the GPU run:

JavaScript

I am about to post another question about implementing CUBLAS in R as well because that was giving me slow speed times compared to Intel MKL, but I’m hoping that maybe there’s a clear cut reason why even something as well built as TF (compared to hacky R and CUBLAS patching) is being slow with GPU.


EDIT: Following Vlad’s suggestion, I wrote up the following script to try and throw some large sized objects and training it, but I think I might not be setting it up correctly because the CPU one in this case even as the size of the matrices are increasing. Any suggestions perhaps?

JavaScript

Advertisement

Answer

As I said in a comment, the overhead of invoking GPU kernels, and copying data to and from GPU, is very high. For operations on models with very little parameters it is not worth of using GPU since frequency of CPU cores is much higher. If you compare matrix multiplication (this is what DL mostly does), you will see that for large matrices GPU outperforms CPU significantly.

Take a look at this plot. X-axis are the sizes of two square matrices and y-axis is time took to multiply those matrices on GPU and on CPU. As you can see at the beginning, for small matrices the blue line is higher, meaning that it was faster on CPU. But as we increase the size of the matrices the benefit from using GPU increases significantly.

enter image description here

The code to reproduce:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement