Colab runtime type switch without losing in-memory training state
0 reputation · 02 May 2020, 20:47 UTC
0 reputation · 02 May 2020, 20:47 UTC
I have a small training workload running in a Google Colab notebook on a CPU runtime. Midway through, I want to switch to a GPU runtime via Runtime → Change runtime type without restarting the training from scratch. The model weights and optimizer state currently live only in memory.
Changing the runtime type appears to trigger a full VM restart, which clears all variables, loaded models, and intermediate data. Colab does not seem to offer a built-in way to snapshot and restore the workspace across this switch, so the burden falls on manual checkpointing (for example, writing to Google Drive). For larger models this adds I/O overhead and may run into Drive quota limits. I am assuming current Colab behavior on the free tier, though Colab Pro/Enterprise appear to behave the same way here.
• Colab has no API to carry in‑memory training state across a runtime type change. The VM is recreated, and all variables are lost.
• The only reliable method is to checkpoint the model weights and the optimizer state to persistent storage (e.g., Google Drive) before changing the runtime.
• To resume training exactly, checkpoint both weights and optimizer state; checkpoints of weights alone will miss learning‑rate schedules, momentum buffers, and other optimizer internals.
• Switching between GPU types (T4 → L4, etc.) triggers the same VM restart and does not preserve state; the behavior is identical to CPU → GPU transitions.
When a user selects Runtime → Change runtime type, Colab terminates the current VM and spins up a fresh one. The new VM starts with a clean environment; no hidden persistence mechanism is exposed to notebooks. This is confirmed by assigning a Python variable, restarting the runtime, and observing that the variable is undefined afterward.
Mount Google Drive (or another persistent bucket):
from google.colab import drive
drive.mount('/content/drive')Save model and optimizer:
# PyTorch example
import torch
checkpoint = {
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss_value,
}
torch.save(checkpoint, '/content/drive/MyDrive/checkpoint.pt')Change the runtime type via the UI.
After the new VM starts, reload the checkpoint:
checkpoint = torch.load('/content/drive/MyDrive/checkpoint.pt')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
start_epoch = checkpoint['epoch'] + 1
Resume the training loop from start_epoch.
Switching from one GPU type to another (e.g., T4 → L4) behaves exactly like a CPU → GPU transition: the VM is recreated, and all in‑memory state is lost. The same checkpointing strategy applies.
To fine‑tune the checkpoint frequency, it would help to know the size of your model and the memory capacity of the target GPU. If the model comfortably fits on the new GPU, less frequent checkpoints may be acceptable; otherwise, more frequent checkpoints or a smaller batch size may be required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.