Verifying GPU Acceleration in Google Colab Before Training
Colab notebooks default to CPU. Select GPU in Runtime settings and verify with nvidia-smi or framework device queries before running compute-heavy code.
25 Nov 2025, 09:55 UTC

Slow Training on a CPU Runtime
The immediate problem in Google Colab is that a notebook defaults to a CPU backend. If you run a deep-learning training cell expecting speed, it may execute slowly, and Colab does not always make the active hardware obvious inside the notebook itself. The useful takeaway: explicitly change the runtime type before compute-heavy work, then verify the accelerator with a diagnostic cell rather than assuming it is active.
Changing the Runtime Type
Colab lets you attach a GPU or TPU to a notebook through the runtime settings. This is an environment-level decision, not a code setting.
- Open the notebook in Google Colab.
- Select Runtime > Change runtime type from the menu.
- Under Hardware accelerator, choose GPU or TPU, then click Save.
- Reconnect the runtime if prompted.
Execution Location: Colab UI menu, not a code cell.
Permissions: Requires edit access to the notebook and an active Google account.
Risk: GPU and TPU availability is not guaranteed on the free tier; the selector may show the option but the runtime can still be assigned a CPU if capacity is unavailable.
Verifying the Accelerator in Code
After changing the runtime type, run a diagnostic cell to confirm the hardware is visible to your framework. Do not rely on the menu selection alone.
For NVIDIA GPU status, run this shell command in a code cell:
!nvidia-smi
Execution Location: Any notebook code cell.
Permissions: None beyond standard Colab runtime execution.
Expected Check: A table listing GPU name, driver version, and memory usage. If the cell returns command not found, no GPU is attached.
Risk: A successful nvidia-smi only proves the driver sees the card; it does not prove TensorFlow or PyTorch can allocate tensors on it.
To verify framework-level access, use the library's own device query. Example for PyTorch:
import torch
print("CUDA available:", torch.cuda.is_available())
print("Device count:", torch.cuda.device_count())
if torch.cuda.is_available():
print("Current device:", torch.cuda.get_device_name(0))
Example for TensorFlow:
import tensorflow as tf
print("GPUs:", tf.config.list_physical_devices('GPU'))
Expected Check: True or a non-empty device list means the framework can see the accelerator. False or an empty list means code will run on CPU even if the runtime menu says GPU.
Trade-Off: Free-Tier Limits and Ephemeral State
GPU access on Colab is subject to usage limits and can be revoked mid-session. The runtime filesystem is ephemeral, so model checkpoints saved only to /content vanish when the runtime disconnects. Pair accelerator selection with persistent storage if the run exceeds one session.
Practical Check: After verifying the GPU, save a small checkpoint file and run !ls -lh /content/ to confirm where it lands. Plan to copy artifacts to Google Drive or another external store before the session ends.
Actionable Next Step
Before writing training code, open Runtime > Change runtime type, select GPU, then run !nvidia-smi and your framework's CUDA check in the first cell. Treat that verification as a gate: if it fails, stop and switch runtimes rather than waiting for a slow CPU training job to finish.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.