colab-remote
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseColab 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 resultsLocal 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 resultsPhase 1: Launch Colab Session (Browser Automation)
阶段 1:启动 Colab 会话(浏览器自动化)
Use to open Colab and set up SSH access.
/agent-browser使用 打开 Colab 并设置 SSH 访问。
/agent-browserStep 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 -iIf 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
EOFLoad in subsequent commands:
source ~/.colab-remote/session.envcell 运行后,对输出执行 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.envPhase 2: SSH Operations
阶段 2:SSH 操作
Connect
连接
bash
undefinedbash
undefinedngrok
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
undefinedssh -o StrictHostKeyChecking=no -o ProxyCommand="cloudflared access ssh --hostname %h" root@$COLAB_HOST
undefinedVerify 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
undefinedbash
undefinedUpload
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
undefinedscp -P $COLAB_PORT -r root@$COLAB_HOST:/content/checkpoints ./checkpoints
undefinedRun training
运行训练
bash
undefinedbash
undefinedForeground
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"
undefinedssh -p $COLAB_PORT root@$COLAB_HOST "tail -f /content/train.log"
undefinedMonitor 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 for the full problem-spec template and harness patterns.
references/egri-colab.md将 Colab 接入 EGRI 优化循环,作为执行后端。完整的 problem-spec 模板和 harness 模式请参阅 。
references/egri-colab.mdExecution 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 policyfor 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 policyPhase 4: Session Lifecycle
阶段 4:会话生命周期
| Tier | Max runtime | Idle timeout | GPU |
|---|---|---|---|
| Free | 12h | 90min | T4, limited |
| Pro | 24h | 90min | T4, V100, priority |
| Pro+ | 24h | 90min | T4, V100, A100 |
| 套餐 | 最长运行时长 | 空闲超时 | GPU |
|---|---|---|---|
| Free | 12h | 90min | T4,有限 |
| Pro | 24h | 90min | T4, V100,优先 |
| Pro+ | 24h | 90min | T4, 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
超时后重新连接
- Check:
ssh -p $COLAB_PORT root@$COLAB_HOST "echo ok" 2>/dev/null && echo "UP" || echo "DOWN" - If dead, re-launch via Phase 1 (browser automation)
- Resume from last checkpoint
- 检查:
ssh -p $COLAB_PORT root@$COLAB_HOST "echo ok" 2>/dev/null && echo "UP" || echo "DOWN" - 如果连接已断开,则通过阶段 1(浏览器自动化)重新启动
- 从最后一个 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
快速参考
| Task | Command |
|---|---|
| Check GPU | |
| Upload | |
| Download | |
| Run script | |
| Background job | |
| Tail log | |
| Disk space | |
| Kill job | |
| Session alive? | |
| 任务 | 命令 |
|---|---|
| 检查 GPU | |
| 上传 | |
| 下载 | |
| 运行脚本 | |
| 后台任务 | |
| 查看日志末尾 | |
| 磁盘空间 | |
| 终止任务 | |
| 会话是否存活? | |
Prerequisites
前置条件
- ngrok account (free): https://ngrok.com — or :
cloudflaredbrew 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 — 或使用 :
cloudflaredbrew install cloudflared - Colab Pro/Pro+,用于 GPU 优先调度和更长的运行时长
- 已安装且可正常运行的 agent-browser
- Chrome 中已登录 Google 账户