Hello, I'm Arthur. When working on AI projects, I often find myself checking GPU usage, memory, and temperature to understand how well a workload is performing.
Instead of checking everything manually, you can use Python to collect GPU information and display it in a simple report.
In this article, we'll build a small GPU monitoring script using Python and NVIDIA's monitoring tools.
1. Check Your GPU Before Writing the Script
First, make sure your computer or server has an NVIDIA GPU and the NVIDIA drivers are installed.
Open your terminal and run:
nvidia-smi
This command displays information about your GPU, including its name, memory usage, temperature, and utilization.
If the command works, you're ready to use it in a Python script.
2. Create a Python Script
Create a new file called gpu_monitor.py.
We'll use Python's subprocess module to run the NVIDIA command and collect its output.
import subprocess
def get_gpu_info():
result = subprocess.run(
["nvidia-smi"],
capture_output=True,
text=True
)
if result.returncode != 0:
print("Unable to read GPU information.")
return
print(result.stdout)
get_gpu_info()
Save the file and run it:
python gpu_monitor.py
If everything is configured correctly, the script will display the same GPU information you would normally see in the terminal.
3. Get Specific GPU Metrics
The complete nvidia-smi output can be difficult to read.
You can request only the metrics you need.
For example, this command displays GPU utilization, memory usage, and temperature:
nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu --format=csv
You can run the same command from Python:
import subprocess
command = [
"nvidia-smi",
"--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu",
"--format=csv"
]
result = subprocess.run(
command,
capture_output=True,
text=True
)
print(result.stdout)
This produces a shorter report that is easier to understand and process.
4. Monitor GPU Usage Automatically
You can extend the script to collect GPU information every five seconds.
import subprocess
import time
command = [
"nvidia-smi",
"--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu",
"--format=csv"
]
while True:
result = subprocess.run(
command,
capture_output=True,
text=True
)
print(result.stdout)
time.sleep(5)
Now the script will keep displaying updated GPU information.
Press Ctrl+C when you want to stop it.
This is a simple way to observe GPU behavior while running an AI application or machine learning experiment.
5. Understand What the Numbers Mean
GPU monitoring becomes more useful when you know what to look for.
For example:
- High GPU utilization can indicate that the GPU is actively processing your workload.
- High memory usage means a large portion of the available VRAM is occupied.
- Increasing temperature can indicate that the GPU is producing more heat during a workload.
These numbers should be interpreted together. High utilization is not necessarily a problem, and low utilization does not always mean the GPU is faulty.
If you're planning to run larger AI workloads, monitoring these metrics can also help you understand which hardware configuration is suitable. You can compare available options through HelloServer's dedicated GPU servers.
6. Make the Script More Useful
Once the basic script works, you can improve it by:
- Saving GPU metrics to a CSV file.
- Recording the date and time of each reading.
- Creating graphs to visualize performance.
- Sending alerts when memory usage reaches a certain level.
For example, saving the readings to a CSV file can help you compare GPU performance across different training sessions.
You can also combine GPU monitoring with CPU and RAM monitoring to get a better understanding of your entire system.
Final Thoughts
Building a simple GPU monitoring script is a useful Python exercise for anyone working with AI or machine learning.
It combines Python programming, command-line tools, and basic hardware monitoring in one practical project.
You don't need a complicated monitoring system to get started. A few lines of Python can already help you understand what your GPU is doing.
If you're exploring GPU resources for your next AI project, you can also visit HelloServer to learn more about its hosting services.
Top comments (0)