Tensorflow How do you monitor GPU performance during model training in real-time?
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
Monitoring GPU performance during TensorFlow model training is essential for identifying bottlenecks, preventing out-of-memory errors, and ensuring your hardware is fully utilized. Without monitoring, you might train for hours with the GPU sitting at 10% utilization because the data pipeline is the bottleneck. TensorFlow provides built-in profiling tools, and NVIDIA offers command-line and programmatic interfaces to track GPU metrics in real time.
Using nvidia-smi for Real-Time Monitoring
The simplest approach is NVIDIA's nvidia-smi command-line tool, which reports GPU utilization, memory usage, temperature, and power consumption.
A GPU utilization below 80% during training usually indicates a data pipeline bottleneck, not a GPU limitation.
TensorFlow Profiler (TensorBoard)
TensorFlow's built-in profiler provides detailed GPU traces showing kernel execution, memory allocation, and data transfer overhead.
The profiler shows a timeline of GPU operations, highlights idle periods, and provides recommendations for improving performance. The "Overview" page summarizes GPU utilization percentage and step time breakdown (input pipeline vs. computation).
Programmatic GPU Monitoring with GPUtil
For monitoring within your training script, the GPUtil library provides a Python interface to GPU metrics.
Custom Keras Callback for GPU Logging
Checking TensorFlow GPU Configuration
Before training, verify that TensorFlow detects your GPU and is configured correctly.
Common Pitfalls
- Low GPU utilization due to data pipeline bottleneck: If
nvidia-smishows low GPU utilization during training, the GPU is waiting for data. Usetf.datapipelines with.prefetch(tf.data.AUTOTUNE)and.map(..., num_parallel_calls=tf.data.AUTOTUNE)to overlap data loading with computation. - Profiling too many batches: Setting
profile_batchto a large range generates enormous trace files and slows training. Profile 5-20 batches to capture representative behavior without excessive overhead. - TensorFlow allocating all GPU memory upfront: By default, TensorFlow reserves all GPU memory at process start. This prevents other processes from using the GPU and can cause OOM errors on multi-tenant systems. Enable
set_memory_growth(True)to allocate memory incrementally. - Ignoring memory fragmentation: Even when total GPU memory is sufficient, fragmentation can cause allocation failures. Monitor
memory.usedvsmemory.totalinnvidia-smiand watch for spikes during large batch operations. - Not distinguishing GPU compute utilization from memory bandwidth utilization:
nvidia-smireports bothutilization.gpu(SM occupancy) andutilization.memory(memory controller activity). High memory utilization with low compute utilization indicates memory-bound operations that might benefit from mixed-precision training (tf.keras.mixed_precision).
Summary
- Use
nvidia-smi --loop=1for quick real-time GPU monitoring from the terminal - Use TensorFlow Profiler with TensorBoard for detailed operation-level traces and optimization recommendations
- Use
GPUtilor custom Keras callbacks for programmatic monitoring within training scripts - Enable
set_memory_growth(True)to prevent TensorFlow from reserving all GPU memory - Low GPU utilization usually indicates a data pipeline bottleneck — fix with
tf.dataprefetching and parallel mapping
Related reading
- Tensorflow How does tf.get_variable work?
- Tensorflow How to convert .meta, .data and .index model files into one graph.pb file
- Tensorflow How to get all variables from rnn_cell.BasicLSTM rnn_cell.MultiRNNCell
- TensorFlow How to handle void labeled data in image segmentation?
- TensorFlow how is dataset.train.next_batch defined?
- TensorFlow How to apply the same image distortion to multiple images
- Tensorflow how to close tensorboard server
- Tensorflow How to convert NaNs to a number?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the 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.