colab-remote

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Colab Remote — SSH-Operated GPU Training

Colab Remote — 通过 SSH 操作的 GPU 训练

Operate Google Colab Pro/Pro+ instances as headless GPU backends from the local terminal.
从本地终端将 Google Colab Pro/Pro+ 实例作为无头 GPU 后端进行操作。

Architecture

架构

Local Mac (Claude Code)
  ├── agent-browser → Chrome → colab.research.google.com
  │   └── Opens notebook, runs colab-ssh setup cell
  ├── SSH tunnel → Colab runtime (via ngrok or cloudflared)
  │   └── Run training, monitor GPU, transfer files
  └── /autoany EGRI loop (local)
      └── Proposes mutations → SSH executes on Colab → evaluates results
Local Mac (Claude Code)
  ├── agent-browser → Chrome → colab.research.google.com
  │   └── Opens notebook, runs colab-ssh setup cell
  ├── SSH tunnel → Colab runtime (via ngrok or cloudflared)
  │   └── Run training, monitor GPU, transfer files
  └── /autoany EGRI loop (local)
      └── Proposes mutations → SSH executes on Colab → evaluates results

Phase 1: Launch Colab Session (Browser Automation)

阶段 1:启动 Colab 会话(浏览器自动化)

Use
/agent-browser
to open Colab and set up SSH access.
使用
/agent-browser
打开 Colab 并设置 SSH 访问。

Step 1: Open Colab and create notebook

步骤 1:打开 Colab 并创建 notebook

bash
agent-browser open "https://colab.research.google.com/#create=true"
agent-browser wait --load networkidle
agent-browser snapshot -i
If login is required, prompt the user to authenticate manually, then re-snapshot.
bash
agent-browser open "https://colab.research.google.com/#create=true"
agent-browser wait --load networkidle
agent-browser snapshot -i
如果需要登录,提示用户手动完成身份验证,然后重新执行 snapshot。

Step 2: Select GPU runtime

步骤 2:选择 GPU 运行时

Navigate Runtime > Change runtime type, select GPU (T4/V100/A100 depending on plan), and save.
前往 Runtime > Change runtime type,选择 GPU(根据套餐选择 T4/V100/A100),然后保存。

Step 3: Install colab-ssh and get connection details

步骤 3:安装 colab-ssh 并获取连接信息

Type the SSH setup code into a cell. Two methods supported:
Method A: ngrok (recommended)
python
!pip install colab-ssh --upgrade
from colab_ssh import launch_ssh
launch_ssh("YOUR_NGROK_TOKEN")
User must provide ngrok authtoken from https://ngrok.com.
Method B: cloudflared (no account needed)
python
!pip install colab-ssh --upgrade
from colab_ssh import launch_ssh_cloudflared
launch_ssh_cloudflared(password="your-password-here")
在 cell 中输入 SSH 设置代码。支持以下两种方式:
方式 A:ngrok(推荐)
python
!pip install colab-ssh --upgrade
from colab_ssh import launch_ssh
launch_ssh("YOUR_NGROK_TOKEN")
用户必须提供来自 https://ngrok.com 的 ngrok authtoken。
方式 B:cloudflared(无需账户)
python
!pip install colab-ssh --upgrade
from colab_ssh import launch_ssh_cloudflared
launch_ssh_cloudflared(password="your-password-here")

Step 4: Extract and save connection details

步骤 4:提取并保存连接信息

After the cell runs, snapshot output to extract hostname/port. Save for reuse:
bash
mkdir -p ~/.colab-remote
cat > ~/.colab-remote/session.env << 'EOF'
COLAB_HOST=0.tcp.ngrok.io
COLAB_PORT=12345
COLAB_USER=root
COLAB_METHOD=ngrok
EOF
Load in subsequent commands:
source ~/.colab-remote/session.env
cell 运行后,对输出执行 snapshot,以提取 hostname/port。保存以供后续使用:
bash
mkdir -p ~/.colab-remote
cat > ~/.colab-remote/session.env << 'EOF'
COLAB_HOST=0.tcp.ngrok.io
COLAB_PORT=12345
COLAB_USER=root
COLAB_METHOD=ngrok
EOF
在后续命令中加载:
source ~/.colab-remote/session.env

Phase 2: SSH Operations

阶段 2:SSH 操作

Connect

连接

bash
undefined
bash
undefined

ngrok

ngrok

ssh -o StrictHostKeyChecking=no -p $COLAB_PORT root@$COLAB_HOST
ssh -o StrictHostKeyChecking=no -p $COLAB_PORT root@$COLAB_HOST

cloudflared

cloudflared

ssh -o StrictHostKeyChecking=no -o ProxyCommand="cloudflared access ssh --hostname %h" root@$COLAB_HOST
undefined
ssh -o StrictHostKeyChecking=no -o ProxyCommand="cloudflared access ssh --hostname %h" root@$COLAB_HOST
undefined

Verify GPU

验证 GPU

bash
ssh -p $COLAB_PORT root@$COLAB_HOST "nvidia-smi"
bash
ssh -p $COLAB_PORT root@$COLAB_HOST "nvidia-smi"

Transfer files

传输文件

bash
undefined
bash
undefined

Upload

Upload

scp -P $COLAB_PORT -r ./data root@$COLAB_HOST:/content/data
scp -P $COLAB_PORT -r ./data root@$COLAB_HOST:/content/data

Download

Download

scp -P $COLAB_PORT -r root@$COLAB_HOST:/content/checkpoints ./checkpoints
undefined
scp -P $COLAB_PORT -r root@$COLAB_HOST:/content/checkpoints ./checkpoints
undefined

Run training

运行训练

bash
undefined
bash
undefined

Foreground

Foreground

ssh -p $COLAB_PORT root@$COLAB_HOST "cd /content && python train.py --epochs 10"
ssh -p $COLAB_PORT root@$COLAB_HOST "cd /content && python train.py --epochs 10"

Background (survives SSH disconnect)

Background (survives SSH disconnect)

ssh -p $COLAB_PORT root@$COLAB_HOST "cd /content && nohup python train.py > train.log 2>&1 &"
ssh -p $COLAB_PORT root@$COLAB_HOST "cd /content && nohup python train.py > train.log 2>&1 &"

Monitor

Monitor

ssh -p $COLAB_PORT root@$COLAB_HOST "tail -f /content/train.log"
undefined
ssh -p $COLAB_PORT root@$COLAB_HOST "tail -f /content/train.log"
undefined

Monitor GPU

监控 GPU

bash
ssh -p $COLAB_PORT root@$COLAB_HOST "nvidia-smi --query-gpu=utilization.gpu,utilization.memory,memory.used,memory.total,temperature.gpu --format=csv"
bash
ssh -p $COLAB_PORT root@$COLAB_HOST "nvidia-smi --query-gpu=utilization.gpu,utilization.memory,memory.used,memory.total,temperature.gpu --format=csv"

Install dependencies

安装依赖

bash
ssh -p $COLAB_PORT root@$COLAB_HOST "pip install torch transformers peft bitsandbytes accelerate datasets"
bash
ssh -p $COLAB_PORT root@$COLAB_HOST "pip install torch transformers peft bitsandbytes accelerate datasets"

Phase 3: EGRI Integration (/autoany)

阶段 3:EGRI 集成(/autoany)

Wire Colab as the execution backend for an EGRI optimization loop. See
references/egri-colab.md
for the full problem-spec template and harness patterns.
将 Colab 接入 EGRI 优化循环,作为执行后端。完整的 problem-spec 模板和 harness 模式请参阅
references/egri-colab.md
。

Execution loop (summary)

执行循环(摘要)

for each trial:
  1. Upload mutated artifact → scp to Colab
  2. Execute on Colab GPU → ssh python train.py
  3. Evaluate results → ssh python evaluate.py
  4. Download metrics → scp results.json
  5. Score locally (immutable evaluator)
  6. Promote or discard based on policy
for each trial:
  1. Upload mutated artifact → scp to Colab
  2. Execute on Colab GPU → ssh python train.py
  3. Evaluate results → ssh python evaluate.py
  4. Download metrics → scp results.json
  5. Score locally (immutable evaluator)
  6. Promote or discard based on policy

Phase 4: Session Lifecycle

阶段 4:会话生命周期

TierMax runtimeIdle timeoutGPU
Free12h90minT4, limited
Pro24h90minT4, V100, priority
Pro+24h90minT4, V100, A100
套餐最长运行时长空闲超时GPU
Free12h90minT4,有限
Pro24h90minT4, V100,优先
Pro+24h90minT4, V100, A100

Keep-alive

保活

bash
ssh -p $COLAB_PORT root@$COLAB_HOST "while true; do sleep 300; echo keepalive; done &"
bash
ssh -p $COLAB_PORT root@$COLAB_HOST "while true; do sleep 300; echo keepalive; done &"

Reconnect after timeout

超时后重新连接

  1. Check:
    ssh -p $COLAB_PORT root@$COLAB_HOST "echo ok" 2>/dev/null && echo "UP" || echo "DOWN"
  2. If dead, re-launch via Phase 1 (browser automation)
  3. Resume from last checkpoint
  1. 检查:
    ssh -p $COLAB_PORT root@$COLAB_HOST "echo ok" 2>/dev/null && echo "UP" || echo "DOWN"
  2. 如果连接已断开,则通过阶段 1(浏览器自动化)重新启动
  3. 从最后一个 checkpoint 恢复

Google Drive persistence

Google Drive 持久化

Mount Drive to persist across sessions:
bash
ssh -p $COLAB_PORT root@$COLAB_HOST "python -c 'from google.colab import drive; drive.mount(\"/content/drive\")'"
挂载 Drive,以跨会话持久化数据:
bash
ssh -p $COLAB_PORT root@$COLAB_HOST "python -c 'from google.colab import drive; drive.mount(\"/content/drive\")'"

Checkpoints survive in /content/drive/MyDrive/

Checkpoints survive in /content/drive/MyDrive/

undefined

Checkpoint 将保存在 /content/drive/MyDrive/ 中。

Quick Reference

快速参考

TaskCommand
Check GPU
ssh -p $COLAB_PORT root@$COLAB_HOST "nvidia-smi"
Upload
scp -P $COLAB_PORT ./file root@$COLAB_HOST:/content/
Download
scp -P $COLAB_PORT root@$COLAB_HOST:/content/file ./
Run script
ssh -p $COLAB_PORT root@$COLAB_HOST "python /content/script.py"
Background job
ssh -p $COLAB_PORT root@$COLAB_HOST "nohup python train.py > log 2>&1 &"
Tail log
ssh -p $COLAB_PORT root@$COLAB_HOST "tail -20 /content/log"
Disk space
ssh -p $COLAB_PORT root@$COLAB_HOST "df -h /content"
Kill job
ssh -p $COLAB_PORT root@$COLAB_HOST "pkill -f train.py"
Session alive?
ssh -p $COLAB_PORT root@$COLAB_HOST "echo ok" 2>/dev/null
任务命令
检查 GPU
ssh -p $COLAB_PORT root@$COLAB_HOST "nvidia-smi"
上传
scp -P $COLAB_PORT ./file root@$COLAB_HOST:/content/
下载
scp -P $COLAB_PORT root@$COLAB_HOST:/content/file ./
运行脚本
ssh -p $COLAB_PORT root@$COLAB_HOST "python /content/script.py"
后台任务
ssh -p $COLAB_PORT root@$COLAB_HOST "nohup python train.py > log 2>&1 &"
查看日志末尾
ssh -p $COLAB_PORT root@$COLAB_HOST "tail -20 /content/log"
磁盘空间
ssh -p $COLAB_PORT root@$COLAB_HOST "df -h /content"
终止任务
ssh -p $COLAB_PORT root@$COLAB_HOST "pkill -f train.py"
会话是否存活?
ssh -p $COLAB_PORT root@$COLAB_HOST "echo ok" 2>/dev/null

Prerequisites

前置条件

  • ngrok account (free): https://ngrok.com — or
    cloudflared
    :
    brew install cloudflared
  • Colab Pro/Pro+ for GPU priority and longer runtimes
  • agent-browser installed and working
  • Google account signed into Chrome
  • ngrok 账户(免费):https://ngrok.com — 或使用
    cloudflared
    :
    brew install cloudflared
  • Colab Pro/Pro+,用于 GPU 优先调度和更长的运行时长
  • 已安装且可正常运行的 agent-browser
  • Chrome 中已登录 Google 账户