Display GPU Usage While Code is Running in Colab

16,781

Solution 1

Used wandb to log system metrics:

!pip install wandb
import wandb
wandb.init()

Which outputs a URL in which you can view various graphs of different system metrics.

Solution 2

If you have Colab Pro, can open Terminal, located on the left side, indicated as '>_' with a black background.

You can run commands from there even when some cell is running

Write command to see GPU usage in real-time:

watch nvidia-smi

Solution 3

There is another way to see gpu usage but this method only works for seeing the memory usage. Go to click runtime -> manage sessions. This allows you to see how much memory it takes so that you can increase your batch size.

Solution 4

A little more clear explaination.

  1. Go to weights and bias and create your account.
  2. Run the following commands.
!pip install wandb
import wandb
wandb.init()
  1. Go to the link in your notebook for authorization - copy the API key.
  2. Paste the key in notebook input field.
  3. After Authorization you will find another link in notebook - see your Model + System matrices there.

Solution 5

You can run a script in background to track GPU usage.

Step 1: Create a file to monitor GPU usage in a jupyter cell.

%%writefile gpu_usage.sh
#! /bin/bash
#comment: run for 10 seconds, change it as per your use
end=$((SECONDS+10))

while [ $SECONDS -lt $end ]; do
    nvidia-smi --format=csv --query-gpu=power.draw,utilization.gpu,memory.used,memory.free,fan.speed,temperature.gpu >> gpu.log
    #comment: or use below command and comment above using #
    #nvidia-smi dmon -i 0 -s mu -d 1 -o TD >> gpu.log
done

Step 2: Execute the above script in the background in another cell.

%%bash --bg

bash gpu_usage.sh

Step 3: Run the inference.

Note that the script will record GPU usage for first 10 seconds, change it as per your model running time.

The GPU utilization results will be saved in gpu.log file.

Share:
16,781

Related videos on Youtube

Matthew So
Author by

Matthew So

Updated on July 05, 2022

Comments

  • Matthew So
    Matthew So almost 2 years

    I have a program running on Google Colab in which I need to monitor GPU usage while it is running. I am aware that usually you would use nvidia-smi in a command line to display GPU usage, but since Colab only allows one cell to run at once at any one time, this isn't an option. Currently, I am using GPUtil and monitoring GPU and VRAM usage with GPUtil.getGPUs()[0].load and GPUtil.getGPUs()[0].memoryUsed but I can't find a way for those pieces of code to execute at the same time as the rest of my code, thus the usage numbers are much lower than they actually should be. Is there any way to print the GPU usage while other code is running?

  • Vinay Verma
    Vinay Verma over 3 years
    A little more clear explaination. Go to weights and bias and create your account Run the above commands Go to the link in your notebook for authorization - copy the API key Paste the key in notebook input field Then you will find a link go to that - and see your Model + System matrices.
  • stason
    stason about 3 years
    It isn't normally available, says: Sign up for Colab Pro to gain access to terminal functionality
  • climbest
    climbest about 3 years
    Yes, @stason , u r right, and sorry I didn't clarify this condition bc I haven't tested it on Colab Free