Skip to content
Back to skills

Colab Gpu Runner

ASecurity

Run GPU jobs on Google Colab unattended through google-colab-cli (the `colab` command) — provisioning, background execution, port tunnels, a dead-man fuse, teardown and compute-unit spend control. Read this whenever work should run on Colab, a cloud GPU, T4/L4/A100/G4, or the user mentions colab cli, compute units, or grabbing a GPU. The colab-video-gen skill builds on it.

  • 435 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 29, 2026
developmentpythongoshellbash

Works with

  • terminal
  • cli

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned November 29, 2026

npx -y skills add aimpact-inc/repo-harness --skill colab-gpu-runner --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Colab Gpu Runner?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Colab Gpu Runner
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/aimpact-inc-colab-gpu-runner/badge)](https://www.skillsdirectory.com/skills/aimpact-inc-colab-gpu-runner)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: colab-gpu-runner
description: Run GPU jobs on Google Colab unattended through google-colab-cli (the `colab` command) — provisioning, background execution, port tunnels, a dead-man fuse, teardown and compute-unit spend control. Read this whenever work should run on Colab, a cloud GPU, T4/L4/A100/G4, or the user mentions colab cli, compute units, or grabbing a GPU. The colab-video-gen skill builds on it.
---

# Colab GPU Runner (shared base)

Worker entrypoint for unattended GPU jobs on Google Colab. Use the fuse and teardown to limit runtime charges.

For CLI syntax, use `colab skill` and `colab <cmd> --help`. Set `SKILL_FILE` to the absolute path of this `SKILL.md` as loaded by the host. The helper is `scripts/colab_job.sh`, relative to that file’s directory.

## 1. Requirements

- Requires `google-colab-cli` 0.8.0 or later on `PATH`, with `oauth2` sign-in configured in the OS keyring. Run `JOB preflight <config>` to verify sign-in without opening a browser.
- Run the helper in a Bash environment with Python 3 and absolute POSIX paths. Port forwarding also requires OpenSSH, netcat (`nc`), and an SSH key supported by the Colab CLI.
- If preflight prints `AUTH_MISSING` (exit 3), stop and tell the user: "please run `colab usage` once in a terminal to sign in". That is the only human step, and the agent cannot do it.
- Never run `colab auth`, `colab drivemount`, `colab repl`, or `colab ssh` without `-- CMD` from an agent. They need a TTY or a human and either hang or create an extra billable VM.

## 2. Spend rules (every task)

1. Run `JOB preflight <config>` before provisioning and record the balance. If the balance cannot be read, stop and ask.
2. CPU, T4 and L4 may be provisioned freely. A100, G4, H100, `--high-mem` and TPU need the user to have allowed them **in the current request** ("A100 is fine"); otherwise ask first. That keeps pre-authorized runs fully unattended.
3. One state file per task: `--config <scratch>/<task>.json`, session name `-s <task>`. Only stop sessions in your own config. Sessions shown as `[?]` in `colab sessions` belong to another agent or person; never touch them.
4. Trial at the smallest scale first (one image, one frame, two seconds, one question), check the output, then run the full job.
5. Right after provisioning, arm the fuse: `JOB fuse <config> <minutes>`. It is a local `nohup` process that stops the VMs at the deadline even if the agent dies. Minutes = estimate × 1.5, capped at 180.
6. Success or failure, finish with `JOB teardown <config>` (stops VMs, closes tunnels, disarms the fuse, verifies with `colab sessions`). Check `colab usage` again about 10 minutes later; billing display lags.
7. Never buy compute units and never run `colab pay`.

## 3. Standard flow

```bash
JOB="$(dirname -- "$SKILL_FILE")/scripts/colab_job.sh"
TASK=my-gpu-job
CFG="/path/to/scratch/$TASK.json"     # absolute path, one per task
"$JOB" preflight "$CFG"              # auth, balance and existing sessions
colab --config "$CFG" new -s "$TASK" --gpu L4 </dev/null  # or: "$JOB" grab "$CFG" "$TASK" L4 T4
"$JOB" fuse "$CFG" 90
colab --config "$CFG" ssh -s "$TASK" -- 'nvidia-smi --query-gpu=name,memory.total --format=csv' </dev/null
colab --config "$CFG" upload -s "$TASK" ./job.sh /content/job.sh </dev/null
"$JOB" spawn "$CFG" "$TASK" /content/job.sh /content/job.log KEY=VALUE
"$JOB" tail "$CFG" "$TASK" /content/job.log 40  # poll until JOB_EXITED
colab --config "$CFG" download -s "$TASK" /content/out/result.json ./result.json </dev/null
colab --config "$CFG" log -s "$TASK" -o "./$TASK.ipynb" </dev/null
"$JOB" teardown "$CFG"
```

### Long work always runs in the background

`colab exec` disconnects after 30 s without output while the remote code keeps running. Agents then misread it as a failure, provision again and pay twice. So:

- Anything over a minute becomes a `/content/*.sh` started with `JOB spawn` and polled with `JOB tail` (every 1–3 minutes, depending on job length). `spawn` forwards `KEY=VALUE` pairs into the script's environment.
- If a foreground `colab exec` is unavoidable, add `--timeout 900`.
- A single `JOB tail` can fail with `RuntimeError: Connection was lost` (seen while a large model was loading). That is the control connection, not the job: poll again on the next round, and only treat `JOB_EXITED` as the job ending.
- Call `colab ssh -s X -- CMD` with `</dev/null` and send its output to a file or pipe.

### Reaching services on the VM

vLLM, ComfyUI and similar servers bind to the VM's `127.0.0.1`. To reach one from this machine, run `JOB tunnel <config> <task> <local-port> <remote-port>` to open an SSH tunnel through `colab ssh --proxy-mode`. Teardown closes it. The runtime allows one SSH connection, so while a tunnel is open `colab ssh -- CMD` returns HTTP 429; use `JOB tail` or `colab exec` (with IPython `!` lines for shell) instead.

## 4. Grabbing a GPU and error codes

| Response | Meaning | Action |
|---|---|---|
| 503 | That GPU type is out of stock (common at peak) | `JOB grab` cycles through a list, or pick another type |
| 412 | You already hold a VM of that type | `colab sessions`; reuse it if it is yours, leave it if not |
| 400 / no quota or eligibility | The account tier cannot get that GPU (Pro gets no H100) | Switch type; do not retry |
| 401 / 403 | Auth problem | Back to §1; `colab auth` does not fix it |
| Session not found / 404 | VM reclaimed (idle ~30–90 min, max ~12 h per session) | Provision again and rebuild from local scripts |

- `JOB grab <config> <task> L4 A100+hm G4` tries each in order, 30 s between rounds, and gives up after `GRAB_MAX_MINUTES` (default 30). A `+hm` suffix adds `--high-mem` (the 80 GB A100 comes with the high-RAM shape). A list containing A100/G4 still needs rule 2 above. Arm the fuse after the grab succeeds, so the wait does not eat the fuse window.
- An unknown `--gpu` value silently becomes A100; spell types exactly: `T4 L4 G4 H100 A100`.
- Easier hours (Beijing time): roughly 06–11 and 14–20.
- Two VMs of the same type are not allowed, but L4 + T4 can run together (e.g. start on T4 while grabbing L4 to take over).

## 5. Reference numbers (measured Nov 2026; they change)

| GPU | VRAM | ~units/hour | Good for |
|---|---|---|---|
| T4 | 16 GB | ~1.2 | Light batches, faster-whisper; slow on video models, so often costlier |
| L4 | 24 GB | ~2.4 | Default: ≤14B quantized LLMs, ComfyUI video, Blender |
| A100 | 40/80 GB | ~8.2 | 27–32B FP8 LLMs, large video models; needs authorization |
| G4 | 96 GB | ~10 | 70B+ LLMs; hard to get; needs authorization |

AI Pro grants 200 units a month, each grant expiring after ~90 days. A faster GPU is often cheaper for heavy work (the same shot costs less on L4 than T4).

## 6. Environment and data

- VMs are disposable: packages and models vanish at stop. What survives is your local scripts, so put every install step in the uploaded `.sh` instead of relying on manual state.
- Download large weights straight to the VM disk (`aria2c -x16`, or `HF_HUB_ENABLE_HF_TRANSFER=1`), not to Google Drive. Pin file names (`aria2c --out=` or `hf download --local-dir`); cache names are hashes that tools such as ComfyUI cannot find.
- Uploading hands data to Google. Each domain skill lists what must never be uploaded; follow its list.
- `colab exec -f file.py` only sends the file's content: remote `__file__` and relative paths differ from local. For a whole tree, `tar` it, upload, and extract.

## 7. Report

Report the GPU used, duration, balance before and after (noting the billing lag), the teardown output, and the archived `.ipynb` and result paths. On any failure, still report whether teardown completed.

Files in this skill

  • SKILL.md7.5 KB
  • scripts/colab_job.sh6.1 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.