TensorFlow
GPUs
Machine Learning
Python
Hardware Detection

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.

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices("GPU")
4
5print("TensorFlow version:", tf.__version__)
6print("Visible physical GPUs:", len(gpus))
7for gpu in gpus:
8    print(gpu)

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.

python
1import tensorflow as tf
2
3physical_gpus = tf.config.list_physical_devices("GPU")
4logical_gpus = tf.config.list_logical_devices("GPU")
5
6print("Physical GPUs:", physical_gpus)
7print("Logical GPUs:", logical_gpus)

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.

python
1import tensorflow as tf
2
3visible = tf.config.get_visible_devices()
4gpu_visible = [device for device in visible if device.device_type == "GPU"]
5
6print("Visible devices:", visible)
7print("Visible GPU devices:", gpu_visible)

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.

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices("GPU")
4for gpu in gpus:
5    tf.config.experimental.set_memory_growth(gpu, True)
6
7print("Configured memory growth for", len(gpus), "GPU devices")

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:

python
1import tensorflow as tf
2
3print("TensorFlow:", tf.__version__)
4
5physical = tf.config.list_physical_devices("GPU")
6logical = tf.config.list_logical_devices("GPU")
7
8print("Physical GPU count:", len(physical))
9for device in physical:
10    print("  physical:", device)
11
12print("Logical GPU count:", len(logical))
13for device in logical:
14    print("  logical:", device)

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:

  1. confirm the host can see the GPU with nvidia-smi
  2. confirm your TensorFlow install matches the supported platform
  3. verify you are using the intended virtual environment
  4. check whether the environment hides devices with container or scheduler settings
  5. 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.

Course illustration
Course illustration

All Rights Reserved.