Skip to main content

How to Check Whether TensorFlow Can Use a GPU

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Training a neural network on a GPU can be many times faster than on a CPU, but only if TensorFlow can actually see the GPU. Before you wait for a long training run, ask TensorFlow which devices it has. This guide does that with TensorFlow 2.22.0 on a Mac, where the honest answer is "the CPU only", and shows what tells you why.

Syntax​

Syntax
tf.config.list_physical_devices(device_type=None)
PartWhat it means
tf.config.list_physical_devicesLists the hardware TensorFlow can use on this computer
device_typeOptional. "GPU" lists only GPUs, "CPU" only CPUs. Left out, it lists every device

The API documentation says it returns a "List of discovered tf.config.PhysicalDevice objects", and that "By default all discovered CPU and GPU devices are considered visible."

The Example​

A uv project, tf-check, with TensorFlow added by uv add tensorflow. A script asks for the GPUs, then for every device, then whether this TensorFlow package was built for NVIDIA GPUs at all.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

gpu.py
import tensorflow as tf

print("GPUs:", tf.config.list_physical_devices("GPU"))
print("All devices:", tf.config.list_physical_devices())
print("Built with CUDA:", tf.sysconfig.get_build_info()["is_cuda_build"])

The gpu.py script in a macOS-style code window: it prints tf.config.list_physical_devices("GPU"), tf.config.list_physical_devices() and tf.sysconfig.get_build_info()["is_cuda_build"], each with a label.

tf.sysconfig.get_build_info() returns a dictionary about how the installed TensorFlow package was built, and its is_cuda_build entry says whether it was built with CUDA, NVIDIA's GPU toolkit.

Step 2: Run It​

Terminal
uv run python gpu.py
Output
GPUs: []
All devices: [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
Built with CUDA: False

The terminal after uv run python gpu.py: GPUs: [], All devices: one CPU, /physical_device:CPU:0, and Built with CUDA: False.

Line by line:

  • GPUs: []: an empty list. TensorFlow found no GPU it can use.
  • All devices: one device, the CPU, named /physical_device:CPU:0. Everything runs there.
  • Built with CUDA: False: the macOS package has no NVIDIA GPU support in it at all. The installation guide says it plainly: "There is currently no official GPU support for MacOS."

On Linux with an NVIDIA GPU, the installation guide installs tensorflow[and-cuda] and then verifies it with this same check, list_physical_devices('GPU'). I couldn't run that here: this Mac has no NVIDIA GPU. The command works unchanged in PowerShell.

Step 3: Read the Whole Build Information​

When GPUs is empty, the build information shows whether the package could use a GPU in the first place:

Terminal
uv run python -c "import tensorflow as tf; print(tf.sysconfig.get_build_info())"
Output
OrderedDict({'is_cuda_build': False, 'is_rocm_build': False, 'is_tensorrt_build': False})

The terminal after printing tf.sysconfig.get_build_info(): is_cuda_build False, is_rocm_build False, is_tensorrt_build False.

All three are False: no CUDA (NVIDIA), no ROCm (AMD) and no TensorRT. The get_build_info documentation notes that "the actual keys and values returned by this function is subject to change across different versions of TensorFlow or across platforms". The documentation also lists keys such as cuda_version and cudnn_version, which this Mac's package doesn't have.

Common Mistakes​

MistakeWhat happensDo this instead
Writing the device type in lowercasetf.config.list_physical_devices("cpu") returned [], while "CPU" listed the CPU. The documentation: "any string other than supported device type such as 'CPU' it returns an empty list instead of raising error"Write "GPU" and "CPU" in capitals
Using tf.test.is_built_with_cuda() in a programIts documentation says it "should only be used in tests written with tf.test.TestCase"Read is_cuda_build from tf.sysconfig.get_build_info()
Expecting the Mac's GPU to show upThe macOS package is CPU-only: GPUs: []Plan for CPU speed on a Mac, or train on a Linux computer with an NVIDIA GPU

FAQ​

What about Windows?​

The installation guide says that "TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows", and points to WSL2, Linux inside Windows, for GPU training.

Conclusion​

tf.config.list_physical_devices("GPU") returned [] on this Mac, the full list showed only the CPU, and is_cuda_build explained why: this TensorFlow package has no GPU support built in. On a computer where TensorFlow can use a GPU, the same script lists it.

Additional Resources​