How to get current available GPUs in tensorflow?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If TensorFlow is configured correctly, you can ask it which GPU devices are currently visible from Python. The key detail is that TensorFlow distinguishes between physical devices, logical devices, and devices that may exist on the machine but are hidden from the current process.
List Physical GPUs
The most common answer is tf.config.list_physical_devices("GPU"). This returns the GPU devices TensorFlow can see before model execution starts.
If the list is empty, TensorFlow does not currently see any usable GPU devices. That does not always mean the machine has no GPU. It can also mean:
- the TensorFlow install does not support the current platform
- the NVIDIA driver or runtime is missing
- the process is running in an environment where the GPU is hidden
- the Python interpreter is not the one where TensorFlow GPU support was installed
For quick troubleshooting, compare the TensorFlow result with nvidia-smi at the shell level. If the operating system cannot see the device either, TensorFlow is not the first place to debug.
Physical Devices vs Logical Devices
TensorFlow can expose more than one logical GPU even when the machine has only one physical device. That is why it is useful to understand both views.
Physical devices are the actual hardware TensorFlow can access. Logical devices are the runtime-level devices TensorFlow has created for execution. If you partition one GPU into multiple logical devices, the two lists will differ.
In most day-to-day training setups, you care first about physical visibility. Logical devices matter more when you are configuring virtual GPUs, distribution strategies, or memory partitions.
Check Which Devices Are Visible to the Process
TensorFlow also lets you inspect visible devices through the runtime config API. This can help if the environment intentionally limits device exposure.
This is helpful in containerized or shared environments where an orchestration layer decides which GPUs a process may use. A machine could have four GPUs installed while the current process sees only one of them.
Configure Memory Growth Before First Use
When GPUs are visible, a useful next step is enabling memory growth so TensorFlow does not immediately reserve most of GPU memory.
This does not affect whether devices are detected, but it often makes local development easier, especially when multiple processes share one machine.
The important rule is to configure memory settings before TensorFlow initializes the GPU runtime for real work. Once the runtime is active, some config changes are no longer allowed.
A Complete Diagnostic Snippet
The following script gives a compact view of what TensorFlow currently sees:
That is usually enough to answer the practical question, “Which GPUs can this TensorFlow process use right now?”
What to Do If No GPUs Appear
An empty list is common on first setup. Work through the stack in order:
- confirm the host can see the GPU with
nvidia-smi - confirm your TensorFlow install matches the supported platform
- verify you are using the intended virtual environment
- check whether the environment hides devices with container or scheduler settings
- rerun the small TensorFlow listing script before testing a full model
Avoid jumping straight into model debugging. If list_physical_devices("GPU") returns nothing, the problem is almost always environment configuration rather than training code.
Common Pitfalls
The most common pitfall is assuming that a GPU existing on the machine means TensorFlow can use it. TensorFlow only reports devices that are visible and compatible with the current runtime.
Another mistake is mixing up physical and logical devices. They answer different questions and can legitimately have different counts.
A third issue is trying to change GPU memory config after TensorFlow has already initialized the runtime. Those changes need to happen early.
Finally, many people run the check in one Python environment and train in another. Always confirm that the same interpreter is used for both inspection and execution.
Summary
- Use
tf.config.list_physical_devices("GPU")to see which physical GPUs TensorFlow currently detects. - Use
tf.config.list_logical_devices("GPU")when you need to inspect runtime-level device partitioning. - Visible devices can differ from installed hardware because containers or schedulers may limit access.
- Configure memory growth before GPU initialization if you want less aggressive memory reservation.
- If no GPUs are listed, debug the environment first and the model code second.

