跳转到内容

Compute

PyTorch + CUDA

这是不加预设的那个选项:官方 PyTorch 镜像、和构建版本匹配好的 CUDA、一个通过 SSH 使用的 root shell,除此之外没有任何东西挡路。

模板列表里有四个上游 tag。它们就是官方的 pytorch/pytorch 镜像,除了平台的 SSH 层之外没有改动。

模板镜像PyTorchCUDA
PyTorch 2.8 - Cuda 12.9pytorch/pytorch:2.8.0-cuda12.9-cudnn9-runtime2.8.012.9
PyTorchpytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime2.5.112.4
PyTorch 2.2 - Cuda 12.1pytorch/pytorch:2.2.0-cuda12.1-cudnn8-runtime2.2.012.1
PyTorch 2.1 - Cuda 11.8pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime2.1.011.8

选最新的那个,除非有东西把你钉在旧版本上 —— 比如某个依赖是针对旧 CUDA 构建的,或者某个仓库还没跟上 PyTorch 的大版本。

开始之前要知道两件事:

  • 没有 Web UI。 这些模板不在 80 端口上跑任何东西,所以 https://<your-slug>.instance.grafilab.ai 不会有响应。你通过 SSH 工作,想在浏览器里看的东西由你自己启动,再用 Web Access 暴露。
  • 这些是 runtime 镜像,不是 devel。 它们带 CUDA 运行时库但不带工具链,所以没有 nvcc。安装时需要编译 CUDA kernel 的包会失败 —— 见故障排查。

跑脚本时选这个模板:一个启动后就让它跑着的训练循环、一轮评测扫描、一个自带 CLI 的仓库。你得到一个干净的环境和对它的完全控制。

如果你更想在浏览器里逐个 cell 地做,JupyterLab 是同样的东西加一层 notebook 服务。如果你想在浏览器 IDE 里改文件,用 code-server。

部署实例,在第 2 步 Template Selection 里选其中一张 PyTorch 卡片。把卡的 VRAM 和你打算训练或部署的模型匹配起来。完整流程见部署 GPU 实例。

  1. In the console open My Instance (https://app.grafilab.ai/orders) and find the card for your instance. Wait until its status reads Active.

  2. Click Connect. The Connect with SSH dialog shows an SSH Connect command and a Password.

  3. Copy the command into a terminal, press Enter, and paste the password when prompted. You land in a root shell inside the container.

先确认驱动看得到卡:

终端窗口
nvidia-smi

再确认 PyTorch 也看得到:

终端窗口
python -c "import torch; print(torch.__version__, torch.cuda.is_available(), torch.cuda.get_device_name(0))"

你要看到 True 和你租的那块卡的名字。如果得到 False,见故障排查。

终端窗口
git clone https://github.com/<owner>/<repo>.git
cd <repo>
pip install -r requirements.txt

比一杯咖啡时间更长的任务都放进 tmux 里跑,这样 SSH 断了任务也还活着:

终端窗口
tmux new -s train
python train.py

按 Ctrl-b 再按 d 分离,用 tmux attach -t train 回来。如果你只需要把输出写进文件,nohup python train.py > train.log 2>&1 & 也行。

一开始 80 端口上没有服务,所以你想从浏览器访问的东西都得自己启动并通过代理映射。绑定到所有网卡,不要绑 localhost:

终端窗口
pip install jupyterlab
jupyter lab --ip 0.0.0.0 --port 8888 --allow-root

然后打开实例卡片上的 Web Access,添加一条映射,填 Service Name 和 Port 8888,使用它生成的代理 URL。Gradio 应用或你跑的任何其他服务同理。步骤见管理实例。

Nothing persists past the rental

Instances have no persistent volume. Anything you write at runtime — downloaded models, installed packages, notebooks, outputs — lives in the container for the lifetime of the rental and is gone when it expires. Before Expire at, back up the folders you care about to Grafilab storage and download the archive.

checkpoint 是要保护的东西。把它们写到你有意选定的位置,随着训练产出把它们推离这台机器 —— 推到 git 远端、对象存储或 Hugging Face 仓库 —— 并在 Expire at 之前用实例卡片上的 Backup 处理仍然只存在于实例上的东西。

  • 在 GPU 节点上 torch.cuda.is_available() 返回 False。 该节点的 NVIDIA 驱动比镜像构建时用的 CUDA 版本旧。换一个 CUDA tag 更旧的模板重新启动 —— 2.5.1-cuda12.4 或 2.2.0-cuda12.1 —— 或者换一个节点。
  • nvcc: command not found,或某个包编译 CUDA 扩展失败。 这些是不带 CUDA 工具链的 runtime 镜像。如果该包有预编译 wheel 就装 wheel,或者用对应的 -devel 镜像 tag 创建一个自定义模板 —— 见自定义模板。
  • 刚启动后 Connection refused。 容器还在启动中。等实例卡片显示 Active。
  • DataLoader 因共享内存报错崩溃。 容器只有 2 GB 的 /dev/shm。调低 num_workers,或者设置 torch.multiprocessing.set_sharing_strategy("file_system")。