import os
import sys
from pathlib import Path
resource = {
"cpu_count_visible": os.cpu_count(),
"colab": "google.colab" in sys.modules,
"kaggle": Path("/kaggle/working").exists(),
}
resourceDive into Deep Learning · §39.2
Colab and Kaggle: Ephemeral Hosted Runtimes, Quotas, and Portable Setup
ephemeral VMs · GitHub launch pipeline · Colab compute units · Kaggle versioned I/O · idempotent setup · Young’s checkpoint law
Motivation
/content), installed packages, and kernel RAM.Mental Model
A portable hosted workflow separates ephemeral execution state from durable storage at every stage of the session:
/content or scratch) holds temporary intermediate files during training.Book Infrastructure
Every code-bearing page in D2L provides a Run notebook menu (Colab, Kaggle, Download) synchronized with the active framework tab:
notebooks branch: The build publishes self-contained .ipynb files with a generated setup cell that verifies framework versions and downloads matching d2l helpers.Supply-Chain Safety
pip install git+https://... or curl ... | bash executes arbitrary remote code, and drive.mount grants full read/write access to your Google Drive.Colab Platform
Runtime Inspection
Because a notebook menu shows the accelerator you requested rather than what the scheduler allocated, always verify the environment programmatically:
"google.colab" in sys.modules at kernel startup./kaggle/working as the writable output directory.torch.cuda.get_device_properties(0), tf.config.list_physical_devices("GPU"), or jax.devices() to confirm device model and total VRAM before starting a run.Image Stability & Automation
colab new, colab exec, and colab stop let local scripts and coding agents drive hosted GPUs without leaving a browser tab open.Kaggle Platform
/kaggle/input/ mounts datasets/models instantly; /kaggle/working/ (20 GB) persists into saved versions; all other paths are ephemeral scratch.Save & Run All): Replays the entire notebook top-to-bottom in a fresh background container and snapshots code, logs, and /kaggle/working/ outputs.Quota Sizing
C \approx 6 N D \approx 2.8 \times 10^{19}\text{ FLOPs (BERT-base)}
Choosing a Provider
drive.mount, and sells compute units for L4/A100 access./kaggle/input, and captures immutable Save & Run All versions.Portable Notebooks
Because a hosted VM is replaceable, the notebook must carry its own setup without breaking the provider’s pretested environment:
importlib.util.find_spec (or importlib.metadata.version) to install only what is missing at a pinned version.pip install --upgrade: It overwrites tested provider wheels, slows startup, and causes Friday’s run to resolve different dependencies than Monday’s.sys.modules, pip install updates disk metadata (importlib.metadata.version) while the live kernel keeps running the old code in RAM (module.__version__) until a kernel restart.{'python': '3.12.3', 'missing': [], 'reconstructible': True}
Diagnosing Image Drift
When a notebook that worked last week suddenly fails on a hosted runtime, a logged environment fingerprint distinguishes code bugs from provider image updates:
numpy, framework metadata versions).nvidia-smi) plus provider image variables (COLAB_*, KAGGLE_*).importlib.metadata.version("torch") records installed frameworks without paying the multi-second overhead or GPU initialization side effects of importing unused backends.Durability & Security
/kaggle/working, /content/drive, or Path.cwd() automatically, and keep API tokens in the provider secret store.Check Your Understanding
Test your intuition on three practical hosted-notebook scenarios:
/content/checkpoints/ every 15 minutes without mounting Google Drive. The session hits the 12-hour limit. Which checkpoints survive?import transformers already ran, a cell executes !pip install transformers==4.57.0. Why does transformers.__version__ still print 4.56.1?Answers:
.ipynb before the session expires./content is ephemeral VM scratch disk that is deleted with the container. On Colab, a checkpoint writer should verify /content/drive is mounted (or sync to external storage) before starting a long run.sys.modules; modifying files in site-packages on disk does not reload objects already resident in kernel RAM. The kernel must be restarted after package changes.Key Takeaways
find_spec before installing pinned packages (restarting if already imported) and log a JSON environment fingerprint./kaggle/working at interval \tau^* = \sqrt{2\delta M}, and keep API keys in the provider secret store.