HOWTO · TensorFlow
How to Check if TensorFlow Is Using GPU
Check whether TensorFlow detects a GPU in your Python process, interpret the device list, and identify what to inspect when no GPU appears.
On this page
To check whether TensorFlow can see a GPU, query its physical devices in the same Python process, interpreter, environment, and startup configuration as your application. A separate terminal or notebook may use a different TensorFlow installation, so its result may not describe your application.
Check Whether TensorFlow Is Using a GPU
Run this example in the application’s Python environment:
import importlib.util
if importlib.util.find_spec("tensorflow") is None:
print("TensorFlow is not installed in this Python environment.")
else:
import tensorflow as tf
devices = tf.config.list_physical_devices("GPU")
print("TensorFlow GPU devices:", devices)
TensorFlow is not installed in this Python environment.
When TensorFlow is installed, a non-empty list means this process detected one or more GPU devices; an empty list means it detected none. The list does not identify why a device is missing, and visibility alone does not prove that a particular model or operation ran on the GPU. The message shown above means the active Python environment has no TensorFlow package; install or select the environment your application uses, then run the check again.
Run the check from the same entry point your application uses. In a notebook, confirm the selected kernel; in a service or container, run it from that process’s environment and configuration. This helps distinguish a device-discovery problem from differences between Python environments.
If TensorFlow lists a GPU but you need to verify a particular operation’s placement, run a representative operation in the same application environment and inspect both its placement messages and result.device. This example requests /GPU:0 for a matrix multiplication; adapt the operation and inputs to match your workload.
import importlib.util
if importlib.util.find_spec("tensorflow") is None:
print("TensorFlow is not installed in this Python environment.")
else:
import tensorflow as tf
tf.debugging.set_log_device_placement(True)
with tf.device("/GPU:0"):
result = tf.matmul(tf.ones((2, 2)), tf.ones((2, 2)))
print("Result device:", result.device)
The supplied runtime environment does not have TensorFlow installed, so it cannot show placement output for this TensorFlow-dependent example. On a configured machine, placement messages and result.device report what happened for this operation; their exact device names vary by build and hardware. A device request alone does not prove that the operation, or every operation in a model, ran on the GPU.
GPU support depends on the TensorFlow distribution, operating system, hardware, driver, and installed dependencies. There is no single setup command that applies to every machine. Consult the official TensorFlow installation guide for the supported installation path for your platform and distribution. A backend name such as DirectML does not by itself show that this process detected a GPU; check the current documentation for the specific backend and package you chose before changing versions or drivers. This guide does not make compatibility claims for a particular backend or hardware combination.
If the device list is empty, check these items in order:
- Confirm that the active interpreter, virtual environment, or notebook kernel is the one used by the application.
- Confirm which TensorFlow distribution is installed there and follow its official installation guidance for GPU support on your platform. A distribution without GPU support will not expose a GPU device.
- Check that the operating system and, if applicable, the container can see the GPU. A container also needs host GPU access and compatible driver support.
- Check the vendor driver and dependencies required by the selected TensorFlow distribution or backend. Restart the Python process after changing its environment, then run the device query again.
If TensorFlow detects a GPU but your operation uses the CPU, enable placement logging around that operation and inspect its device. That separates device visibility from operation placement and gives you a concrete next point to investigate.