A better Colab with persistent disk

Xianbo QIAN
4 min readJan 20, 2023

--

I love working with notebooks but it’s a bit tricky to run long duration work with Colab. As soon as the window is closed, all the connection is lost including the intermediate data on the VM disk as well as the in-memory state. Unforunately there is no way to get them back except running through the notebook again. There is a paid option to run the task in background for a longer time with Colab Pro+ but that’s crazily expensive.

An alternative approach that I found so far is to use HuggingFace Spaces.

You might feel surprised: what? HF Spaces? Isn’t that a place to host your demos?

Yes, and no.

HF Spaces is basically a more advanced version of no-disk Amazon EC2 instance, where the security, HTTPS, sub-domain name have been provided for free. They also offer free HF Spaces with 16GB ram and around 50GB disk (volatile after server restart). There are paid options from T4 up to A100 GPUs with very reasonable price.

HF Spaces can literally run any app, meaning that they can even run JupyterLab instance :-) And here is a public example: https://huggingface.co/spaces/camenduru/jupyter

This Space is a bit slow to load due to its popularity. what we need to do is to duplicate this Space and make it private so that it’s not shared with others (Sharing JupyterLab instance with people you don’t know is also very risky as your Github / HF token might get leaked).

Once the Space is forked, you need to assign it a GPU to do handle real ML tasks.

And once you’ve done with your work, just switch it back to the free tier to stop billing.

There is also a handy sleeping time button that you can optinoally turn on in case you forget to turn the GPU off, it will do it for you.

Now, let’s wait a few moment until the server is built. It may take up to a few minutes.

Now we should be able to log in with the default token hugginface

If you don’t like the embedded mode, you can also open it in “full-screen” model without HF banner, via the free subdomain name.

It will be {USER_NAME}-{REPO_NAME}.hf.space/login?token=huggingface

In my case it’s https://xianbao-jupyterlab.hf.space/login?token=huggingface

Once you managed to login, you’re able to do anything as if on your local machine. As long as the server kept open, your data is kept safe. You can run long duration work there and periodically open Jupyterlab to check the progress. You can find all the running task on the second left panel.

How about the persistent data? I still need them even after I’ve finished my current task. Isn’t changing hardware back to CPU causing a reboot which then flashes the disk?

That’s right. That’s why you might want to persist the disk content to HF datasets (or github if you don’t have large files).

Here are some code that uses to save the disk before turning it off.

! pip install huggingface_hub
%%capture
!sudo apt -qq install git-lfs
!git config --global credential.helper store
import huggingface_hub as hf
hf.login()
repo_id = "xianbao/jupyterlab-work"

try:
hf.hf_api.repo_info(repo_id)
print("Repo found")
except hf.hf_api.RepositoryNotFoundError:
hf.create_repo(repo_id, private=True)

repo = hf.Repository(local_dir="work", clone_from=repo_id)
repo.git_pull(rebase=True)

repo.git_add(auto_lfs_track=True)
repo.git_commit()
repo.git_push(blocking=True)

Enjoy!

--

--

Responses (2)