Does TensorFlow by default use all available GPUs in the machine?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Not in the way many people expect. In current TensorFlow, all discovered GPUs are visible by default unless you restrict them, and TensorFlow may reserve memory on all visible GPUs. But that does not mean your model is automatically training across all GPUs.
Visibility Is Not the Same as Multi-GPU Training
TensorFlow makes a distinction between devices being visible to the runtime and a model actually being distributed across those devices.
If you simply write ordinary TensorFlow or Keras code on a machine with several GPUs, TensorFlow can place operations on a GPU automatically. In many straightforward cases, work ends up on GPU:0 unless you explicitly configure a strategy or manual device placement.
So the short answer is:
- all GPUs are usually visible by default
- memory may be reserved on all visible GPUs
- computation is not automatically spread across all GPUs for training
What Happens by Default
This small example shows the available physical GPUs.
If TensorFlow can see four GPUs, that does not mean a normal model.fit(...) call will use four replicas automatically. For true single-machine multi-GPU training, you typically use tf.distribute.MirroredStrategy.
That strategy creates one replica per visible GPU and synchronizes updates across them.
Why People Think TensorFlow Uses Everything Automatically
The confusion comes from memory behavior. TensorFlow documentation explains that visible GPUs are visible to the runtime by default, and the runtime may map nearly all memory on each visible GPU unless you enable memory growth or restrict visibility.
That can make it look like TensorFlow is fully using every GPU, when in reality it may only be reserving memory while your actual model runs on one device or under a specific distribution policy.
How to Restrict TensorFlow to Specific GPUs
If you want TensorFlow to use only one GPU, set visible devices before the runtime initializes them.
This is useful on shared machines or when you want reproducible experiments on a fixed device.
Manual Device Placement Is Different Again
You can also place specific operations on a specific GPU.
Manual placement tells TensorFlow where to run an operation, but it still does not create data-parallel multi-GPU training by itself.
Recommended Mental Model
Think of TensorFlow GPU behavior in three layers:
- device discovery
- device visibility and memory policy
- actual distribution of computation
Beginners often look only at the first layer and assume the other two are automatic. They are not.
Common Pitfalls
A common mistake is reading GPU memory usage in nvidia-smi and assuming all GPUs are actively training the model. Memory reservation does not prove distributed execution.
Another mistake is calling tf.config.set_visible_devices after TensorFlow has already initialized GPUs. That raises a runtime error because visibility must be configured early.
Finally, if you want multi-GPU training, do not rely on default placement heuristics. Use tf.distribute.MirroredStrategy or another explicit strategy.
Summary
- TensorFlow usually makes all discovered GPUs visible by default.
- Visible does not mean your model automatically trains on all GPUs.
- TensorFlow may reserve memory on all visible GPUs, which can be misleading.
- Use
tf.distribute.MirroredStrategyfor normal single-host multi-GPU training. - Restrict visibility and memory policy explicitly when you need predictable GPU usage.
Related reading
- Does TensorFlow have cross validation implemented?
- Does tensorflow map_fn support taking more than one tensor?
- Does TensorFlow plan to support OpenCL?
- Does Tensorflow simplify a computational graph?
- Does TensorFlow by default use all available GPUs in the machine?
- Does TensorFlow have cross validation implemented?
- Does TensorFlow job use multiple cores by default?
- Does Tensorflow normalize input data by default?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.