Angad Sandhu
Search
Browse
On this page

4.0_pyNVML

What is my GPU doing?

Now that we are executing code on our GPUs we may want to understand more about how well we are utilising our hardware.

This tutorial is designed to be used with jupyterlab-nvdashboard which is a Jupyter Lab extension that shows graphs of your GPU utilization, memory and transfer speeds.

Image unavailable: (unlabeled image) — original archive file images/nvdashboard.gif is absent.

All of the APIs used to create this dashboard are available in Python, so let's dig into pyNVML ourselves.

NVML stands for the NVIDIA Markup Language and is a package which is included with NVIDIA drivers to report information about GPU status.

We can import and use the pyNVML package to explore this data.

import pynvml
pynvml.nvmlInit()

This package provices direct bindings to the C NVML library, so pretty much anything mentioned in their documentation is possible.

We can see how many GPUs we have.

pynvml.nvmlDeviceGetCount()

What our driver version is.

pynvml.nvmlSystemGetDriverVersion()

We can then grab a handle for each GPU to query specific metrics about it.

gpus = [pynvml.nvmlDeviceGetHandleByIndex(i) for i in range(pynvml.nvmlDeviceGetCount())]
gpus

We can get the model.

[pynvml.nvmlDeviceGetName(gpu) for gpu in gpus]

We can get memory info (let's convert it to GiB)

[pynvml.nvmlDeviceGetMemoryInfo(gpu).used / 1e9 for gpu in gpus]
[pynvml.nvmlDeviceGetMemoryInfo(gpu).free / 1e9 for gpu in gpus]

By accessing rich metrics from our GPUs we can make more informed decisions on how our code is performing and how we can divide up our work.

Browse the library