Why TensorFlow Is Slow on GPU: Fixes & Settings
In this article
TensorFlow is slow on your GPU because the GPU is not actually being used for most of the work. The usual culprits are a CPU-only build, an unsatisfied CUDA/cuDNN version, data loading that starves the GPU, or code that runs in eager mode without a compiled graph. The fix is to confirm GPU visibility first, then remove the bottlenecks one at a time. This guide walks through the checks in the order that finds the problem fastest.
What You'll Need
- A machine with an NVIDIA GPU (TensorFlow's official GPU support on Windows and Linux is CUDA-based; Apple silicon uses the separate tensorflow-metal plugin).
- A working Python environment where you can install packages, ideally a virtual environment.
- The NVIDIA driver installed and up to date for your GPU.
- Matching versions of CUDA and cuDNN for your TensorFlow release, or a TensorFlow build that bundles them (the pip
tensorflowpackage on Linux and Windows includes GPU support from 2.11 onward, but still needs a compatible driver). - A terminal or command prompt and permission to run
nvidia-smi.
Menu and dialog labels in NVIDIA and TensorFlow tools can differ slightly by version, operating system and region, so treat exact wording as a guide rather than a guarantee.
Step-by-Step
- Confirm the GPU is visible to the OS. Run
nvidia-smiin a terminal. You should see your GPU model, driver version and memory usage. If the command is not found, the driver is missing or not on your PATH, and TensorFlow cannot use the GPU either. - Check what TensorFlow sees. In Python run:
import tensorflow as tfprint(tf.config.list_physical_devices('GPU'))
An empty list means TensorFlow is running CPU-only. A populated list means the GPU is available but may still be underused. - Verify the build is a GPU build. Run
print(tf.test.is_built_with_cuda()). If it returns False, you installed the CPU-only package. Uninstall it and install the GPU-capable package for your platform, then restart the environment. - Match CUDA and cuDNN to your TensorFlow version. Each TensorFlow release is tested against specific CUDA and cuDNN versions. A mismatch usually shows up as a warning about missing libraries or a failed device query. Check the official TensorFlow install page for the version pair that matches your release, install exactly those, and reboot if the driver changed.
- Make sure operations run on the GPU. By default, TensorFlow places operations on the GPU when a compatible kernel exists, but some ops have no GPU implementation and silently fall back to the CPU. Wrap a small test in
tf.device('/GPU:0')and watchnvidia-smiwhile it runs. If GPU utilization stays near zero, the work is on the CPU. - Stop running in eager mode for training loops. Eager execution is convenient but adds per-operation overhead. Wrap your training step in
@tf.functionso TensorFlow traces it into a graph. This is often the single biggest speedup for small models and small batches. - Fix data loading. If the GPU sits idle between steps, the input pipeline is the bottleneck. Use
tf.datawith.cache(),.prefetch(tf.data.AUTOTUNE)and.batch()before the model, and avoid Python-side loops that read files one at a time. Keep image decoding and augmentation on the CPU in parallel workers rather than in the main thread. - Increase batch size where memory allows. Tiny batches leave the GPU underfed. Raise the batch size until memory use is close to the limit, then tune from there. Monitor with
nvidia-smito confirm utilization rises. - Check for accidental CPU copies. Moving tensors between CPU and GPU is expensive. Keep data on the GPU once it is there, avoid calling
.numpy()inside the training loop, and do not mix NumPy arrays with TensorFlow tensors in hot paths. - Rule out thermal throttling and power limits. Run
nvidia-smi -q -d PERFORMANCEduring a long job. If clocks drop or the GPU reports throttle reasons, improve cooling or check the power supply. A throttled GPU looks slow even when the code is fine. - Try mixed precision. On supported GPUs, enabling mixed precision lets TensorFlow use faster lower-precision math where it is safe, which can raise throughput and reduce memory use. Check the official guide for the correct setup for your hardware before relying on it.
- Profile instead of guessing. Use TensorBoard's profiler to see where time goes: input pipeline, kernel launches or host-device transfers. Fix the largest bar first, then re-profile.
Common Mistakes
- Installing
tensorflowand assuming GPU support is automatic on every platform. On macOS, official GPU acceleration needs the tensorflow-metal plugin, and only on Apple silicon. - Mixing CUDA versions from different installers, which produces confusing library errors.
- Measuring speed with the first run only. The first call includes graph tracing and memory allocation; time a few warm-up iterations before judging.
- Blaming the GPU for a slow disk. Reading thousands of small files per epoch will starve any GPU.
- Using very small batches on a large GPU and expecting high utilization.
- Leaving debug prints or
.numpy()calls inside the training loop, which force synchronization and slow everything down.
Tips
- Keep a short diagnostic script that prints device list, build flags and a one-line timing test, and run it after every environment change.
- Pin your TensorFlow, CUDA and cuDNN versions in a requirements file so a rebuild does not silently change the stack.
- Use
nvidia-smiin a second terminal during training; sustained low utilization points to the input pipeline, while high utilization with slow steps points to the model or memory transfers. - Prefer official TensorFlow installation instructions over random tutorials, because version pairings change between releases.
- If you only need inference, check whether a lighter runtime or a converted model format suits your deployment better than the full training stack.
Troubleshooting
If tf.config.list_physical_devices('GPU') returns an empty list, work through this order: confirm nvidia-smi works, confirm the GPU build is installed, confirm CUDA and cuDNN match the TensorFlow version, then reboot. On Windows, also check that the correct driver is installed and that no other process is holding the GPU.
If the GPU is detected but utilization stays near zero, the problem is almost always the input pipeline or eager execution. Add .prefetch and @tf.function first, then profile.
If you see out-of-memory errors after raising the batch size, lower it and enable memory growth so TensorFlow allocates GPU memory as needed instead of grabbing it all at startup. This is a configuration change, not a code rewrite.
If performance degrades over a long run, check temperatures and throttle reasons with nvidia-smi before changing code.
Frequently Asked Questions
Does TensorFlow use the GPU automatically?
It uses the GPU when a GPU build is installed, the driver and CUDA stack are compatible, and the operation has a GPU kernel. Otherwise it falls back to the CPU, often silently.
Why is my GPU at 0% during training?
Usually the data pipeline cannot feed it fast enough, or the model is running eagerly on tiny batches. Check input loading and wrap the training step in @tf.function.
Do I need to install CUDA separately?
It depends on your platform and TensorFlow version. Some pip packages bundle the GPU libraries; others require a matching CUDA and cuDNN install. Follow the official install page for your exact version.
Does TensorFlow support AMD or Intel GPUs?
Official GPU support is built around NVIDIA CUDA on Windows and Linux, with tensorflow-metal for Apple silicon. Other vendors are not officially supported by the standard TensorFlow package.
Why is the first epoch always slow?
The first run includes graph tracing, kernel compilation and memory allocation. Later epochs are the fair measure of steady-state speed.
Can mixed precision make training faster?
On supported GPUs it can increase throughput and reduce memory use. Follow the official mixed-precision guide for your hardware and check that your model still converges.