Programmatically Switching Google Colab Runtime to GPU
Colab defaults to a CPU runtime. This guide explains how to toggle the runtime to GPU via code, verify acceleration, and avoid common pitfalls.
14 Feb 2026, 15:39 UTC

Runtime type determines acceleration
When you start a new Colab notebook, the runtime type decides whether your code runs on the CPU or on accelerated hardware such as a GPU or TPU. By default, Colab selects the CPU runtime, which means imported frameworks like PyTorch or TensorFlow will load CPU‑only builds. If you expect GPU acceleration without switching the runtime, your notebook will silently execute on the CPU—performance will be significantly slower, but no error is raised.
Toggle via the UI
- Open the Runtime menu at the top of the notebook.
- Select Change runtime type.
- Choose GPU or TPU from the dialog and confirm.
- Click the Restart button that appears to apply the change.
Toggle programmatically
from google.colab import runtime
runtime.set_runtime_type('GPU')
This command sets the desired runtime, but the change only takes effect after you restart the kernel. Without a restart, the notebook process continues on the CPU runtime, and calls such as torch.cuda.is_available() will return False even though the command ran without error.
Verify acceleration is active
Run the following cell to confirm GPU is selected:
import torch
print(torch.cuda.is_available()) # Expected: True (with GPU runtime selected)
For device‑specific details, execute:
import torch
print(torch.cuda.get_device_name(0)) # Expected: Tesla T4 or equivalent Colab GPU model
Why the restart matters
Colab initializes a fresh kernel environment each time you start or restart it. The runtime type is a property of that kernel, not a per‑cell setting. Changing it via code or the UI does not alter the running kernel’s configuration; the kernel must be re‑created for the new type to take effect. This is why many users report GPU not available after toggling the type—they forgot the restart step.
Limits and common mistakes
- Ephemeral runtime: Every Colab session reinitializes the environment. Setup cells, imported data, and framework versions must be re‑executed after a restart.
- GPU time sharing: Colab allocates GPU time across users. Idle or long‑running sessions may be disconnected after approximately 90 minutes to free resources.
- Mismatched device imports: If you install a CPU‑only version of PyTorch while the runtime is set to GPU, operations on GPU tensors will raise
RuntimeError. Align the framework build with the selected runtime before switching. - Silent performance degradation: The most frequent mistake is changing the runtime type without restarting. The notebook appears to run, but metrics show CPU speeds. Always verify with the torch checks after a restart.
Comparing UI and API paths
| Path | Steps | Restart required | Visibility |
|---|---|---|---|
| UI | Click Runtime then Change runtime type then select GPU then confirm then Restart | Yes – prompted after selection | Shown in UI menu |
| API | from google.colab import runtime; runtime.set_runtime_type('GPU') | Yes – kernel restart after the call | Hidden in code; must verify via framework checks |
Full workflow example
Below is a minimal cell sequence you can copy into a fresh Colab notebook to go from CPU‑only to GPU‑accelerated execution:
# Cell 1: Set runtime type (programmatic)
from google.colab import runtime
runtime.set_runtime_type('GPU')
# Cell 2: Restart kernel (prompt appears after Cell 1 runs)
# Wait for the Restart kernel dialog, then click Restart
# Cell 3: Verify acceleration
import torch
print(torch.cuda.is_available()) # Should output True after restart
# Cell 4: Use GPU tensor
x = torch.randn(1000, 1000, device='cuda')
print(x.sum())
Note: If you skip the restart in Cell 2, Cell 3 will return False and Cell 4 will either error or run on CPU, depending on your PyTorch build.
Quick checklist
- Identify the acceleration you need (GPU or TPU).
- Switch the runtime type via UI or the
google.colab.runtimeAPI. - Restart the kernel (the restart prompt appears automatically after a programmatic toggle).
- Re‑run your setup cells.
- Verify with
import torch; print(torch.cuda.is_available())– it should returnTrue.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.