Choosing Between CPU and GPU Runtime in Kaggle Notebooks for Deep Learning Training
Learn how to decide between CPU and GPU runtime in Kaggle Notebooks, compare the options, see a ready‑to‑use implementation with device detection and OOM fallback, and verify your choice with simple checks.
01 Aug 2026, 11:06 UTC

Decision and Constraints
When you start a Kaggle Notebook you must decide whether to enable GPU acceleration. The choice affects training speed, weekly resource limits, and the likelihood of hitting notebook timeouts. The decision should be based on the expected training time per epoch, the size of your model, and the 30‑hour weekly GPU quota that applies to free Kaggle accounts.
Constraints
- GPU quota: Free Kernels provide up to 30 hours of GPU execution per week. Exceeding this limit triggers an automatic fallback to CPU and may terminate the session.
- CPU runtime: Unlimited wall‑clock time, but training is considerably slower.
- Internet access: Disabled by default; must be turned on in Settings → Internet if you need to download data or install packages from PyPI.
- Disk space: Notebook workspace limited to 20 GB. Large checkpoints should be written to Kaggle Datasets or external cloud storage.
Options Comparison
| Option | Typical Speedup (vs CPU) | Pros | Cons / Risks |
|---|---|---|---|
| CPU‑only runtime | 1× (baseline) | Unlimited time, no quota concerns, works with any framework. | Training can be slow; large models may exceed the 12‑hour notebook timeout. |
| GPU‑enabled runtime (Tesla P100) | 10‑20× for compatible frameworks (TensorFlow, PyTorch) | Much shorter epoch time, enables larger batch sizes. | Subject to 30‑hour weekly GPU quota; OOM errors can abort the session; requires framework‑specific device placement. |
Trade‑offs
GPU acceleration reduces the wall‑clock time needed for each epoch, which can keep a long‑running experiment within the notebook’s timeout. However, the GPU quota is a hard limit: if you exceed 30 hours in a week, Kaggle will silently switch the runtime to CPU and may disconnect the kernel, causing loss of unsaved state. CPU runtime avoids quota issues but may make it impractical to train very deep networks or large datasets within the allowed session length.
Both options require your code to be device‑agnostic. If a GPU is not available, the framework should fall back to the CPU without raising an uncaught exception.
Implementation and Validation
Detecting GPU availability at runtime
Place the following snippet in the first code cell of your notebook. It works for both PyTorch and TensorFlow and will not raise an error if the respective library is not installed.
import sys
try:
import torch
GPU_AVAILABLE = torch.cuda.is_available()
if GPU_AVAILABLE:
DEVICE = torch.device('cuda')
print(f'PyTorch GPU detected: {torch.cuda.get_device_name(0)}')
else:
DEVICE = torch.device('cpu')
print('PyTorch: No GPU found, using CPU.')
except Exception:
torch = None
GPU_AVAILABLE = False
DEVICE = None
try:
import tensorflow as tf
if tf.test.is_gpu_available(cuda_only=False):
GPU_AVAILABLE = True
DEVICE = tf.device('/GPU:0')
print(f'TensorFlow GPU detected: {tf.test.gpu_device_name()}')
else:
DEVICE = tf.device('/CPU:0')
print('TensorFlow: No GPU found, using CPU.')
except Exception:
tf = None
if DEVICE is None:
DEVICE = 'cpu'
print('No TF/Torch available, defaulting to CPU.')
# Use DEVICE in model construction
# Example for PyTorch:
# model = MyModel().to(DEVICE)
# Example for TensorFlow:
# with DEVICE:
# model = build_model()
Where to run: In any notebook cell. No special permissions are required; the cell executes with the notebook’s runtime.
Expected check: The printed line should show the GPU name when a GPU runtime is selected, or indicate CPU fallback otherwise.
Risk: If the notebook is launched with GPU disabled but you still attempt to move tensors to cuda, you will get a RuntimeError: No CUDA devices are available. The try/except blocks above prevent the notebook from crashing.
Training loop with automatic CPU fallback on OOM
Wrap the training step in a try/except that catches out‑of‑memory errors and retries on the CPU. This protects the session from being terminated when a GPU batch is too large.
import traceback
def train_epoch(model, loader, optimizer, loss_fn):
model.train()
total_loss = 0.0
for batch_idx, (data, target) in enumerate(loader):
try:
data, target = data.to(DEVICE), target.to(DEVICE)
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
total_loss += loss.item()
except RuntimeError as e:
if 'out of memory' in str(e).lower():
print(f'OOM on batch {batch_idx}, switching to CPU for this batch')
# move model and data to CPU temporarily
model_cpu = model.to('cpu')
data_cpu, target_cpu = data.to('cpu'), target.to('cpu')
optimizer.zero_grad()
output = model_cpu(data_cpu)
loss = loss_fn(output, target_cpu)
loss.backward()
optimizer.step()
total_loss += loss.item()
# move model back to original device for next iteration
model.to(DEVICE)
else:
raise # re‑raise unexpected errors
return total_loss / len(loader)
Where to run: After defining your model, loss function, and optimizer.
Permissions: None beyond the notebook’s default execution rights.
Expected check: If a batch exceeds GPU memory, you should see the OOM warning printed and training continue on the CPU for that batch.
Risk: Frequent OOM falls back to CPU will slow overall epoch time; consider reducing batch size or using gradient accumulation if this happens often.
Verification steps
- Confirm GPU presence: In a notebook cell run
!nvidia-smi. You should see a table with the Tesla P100 driver version, memory usage, and GPU utilization. If the command returns an error or shows no output, the GPU runtime is not active. - Framework‑specific device count:
- PyTorch:
print(torch.cuda.device_count())should return1when GPU is enabled. - TensorFlow:
print(len(tf.config.list_physical_devices('GPU')))should return1.
- PyTorch:
- Measure epoch speed: Train a small model (e.g., a single‑layer CNN on MNIST) for one epoch with GPU enabled, record the time (
%timemagic ortime.time()), then repeat with GPU disabled (change notebook settings → Accelerator → None). You should observe roughly a 10‑20× reduction in epoch time when the GPU is present. - Check quota usage: Open the notebook’s Settings → Account → GPU usage meter. It shows hours used this week; ensure you stay below the 30‑hour limit to avoid automatic fallback.
Limitations and Practical Check
The GPU provided by Kaggle is a Tesla P100 with 16 GB VRAM. Models that require more memory than this will trigger OOM errors even with a GPU runtime. Disk space is capped at 20 GB, so large datasets or numerous checkpoints should be off‑loaded to Kaggle Datasets (kaggle datasets version -p /path/to/data -u) or external storage.
Practical way to verify you are within limits: After each training session, run !df -h to inspect workspace usage and !nvidia-smi --query-gpu=utilization.gpu --format=csv to see recent GPU utilization. If utilization consistently shows 0 % while the runtime is set to GPU, you may have been silently switched to CPU due to quota exhaustion.
By detecting the device at runtime, guarding against OOM, and periodically checking the quota and disk usage, you can make an informed, reproducible choice between CPU and GPU execution that matches your model’s training time constraints.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.