Using Google Colab

Several assignments and in-class demos in this course are Jupyter notebooks that need a GPU. You do not need to own one, and you should not spend an afternoon installing CUDA on your laptop. Google Colab gives you a free GPU in the browser, and every notebook in this course is built to run there without any setup beyond what is on this page.

All you need is a Google account and Chrome, Firefox, or Safari.

What Colab is

Colab runs a Python notebook on a Google machine and shows it to you in a browser tab. A notebook is a document of cells: text cells (like this page) and code cells you can run, in order, with the output appearing underneath.

The machine your notebook runs on is called a session. It is temporary. Anything you install or download lives only as long as that session, and Google will shut it down if you close the tab or leave it idle for a while. Your notebook file is saved; the machine it ran on is not.

Opening a course notebook

Every notebook link on the schedule says run in Colab. Click it and the notebook opens, ready to run. That is the whole procedure.

Two things to do immediately after it opens:

  1. Save your own copy. File → Save a copy in Drive. The link opens a read-only view of the course copy; without your own copy, your work is not saved anywhere. The copy lands in your Google Drive under Colab Notebooks.
  2. Turn on the GPU. See the next section. Do this before running any cells.

Turning on the GPU

This is the step people forget, and it is the reason a notebook that should finish in three minutes appears to hang.

  1. Runtime → Change runtime type
  2. Under Hardware accelerator, choose T4 GPU
  3. Save

Colab will restart the session. That is expected — you have not lost anything, you just need to start running cells from the top again.

If you skip this step, Colab silently gives you a slower machine with no GPU and says nothing about it. The course notebooks check for you: near the top, each one runs nvidia-smi and prints the name of the graphics card it found. If it prints a card and its memory, you are set. If it warns that no GPU is attached, go back and do the three steps above.

Running the notebook

Runtime → Run all executes every cell from the top. The first run of a notebook that downloads a model takes a few minutes — it is fetching several gigabytes of weights — and afterwards the model is cached for the rest of the session, so later cells are fast.

You can also run cells one at a time with the play button on the left of each cell, or Shift+Enter. Do this in order the first time through. Cells are not independent: a cell that uses model will fail if you have not run the cell that defines it.

A short vertical progress bar to the left of a cell means it is still running. Wait for it to finish before running the next one.

When something goes wrong

“NameError: name ‘model’ is not defined.” You ran cells out of order, or the session restarted underneath you. Runtime → Run all and let it go from the top.

“CUDA out of memory.” The GPU ran out of room. Each notebook says which setting to lower — usually a batch size. Lower it, then Runtime → Restart session and run again from the top. Restarting is required; the memory is not released otherwise.

Everything is extremely slow. You almost certainly do not have a GPU attached. Check the nvidia-smi output near the top of the notebook, and see the GPU section above.

The session disconnected. Colab reclaims idle machines, and free sessions have a time limit. Reconnect and Runtime → Run all. Anything downloaded during the old session is gone and will download again.

A cell has been running for a very long time. Compare against the runtime the notebook states. If it is far over, interrupt it (Runtime → Interrupt execution) rather than waiting indefinitely, and check the GPU first.

Free-tier limits, and how not to hit them

Free Colab is generous but rationed. You get a GPU when one is available, for a few hours at a time, with a daily cap. Heavy use in the afternoon can mean no GPU that evening.

Three habits keep you out of trouble:

Colab Pro exists and is not required for anything in this course. Do not buy it on my account.

Submitting work

Unless an assignment says otherwise, submit the notebook itself, with the output still in it: File → Download → Download .ipynb. A notebook whose cells have been cleared is not evidence that anything ran.

If you were asked for a write-up as well, put it in text cells inside the same notebook rather than in a separate document.

Working locally instead

You are welcome to run these notebooks on your own machine if you have an NVIDIA GPU and want to. Each example directory has a requirements.txt, and the notebooks are ordinary .ipynb files. Nothing in the course depends on Colab specifically — it is there so that no one is blocked by hardware.

If you go this route, do it in a virtual environment, and do not expect help debugging a local CUDA install during office hours. Colab is the supported path.