实战场景

更新时间:
复制 MD 格式

展示 PAI CLI 在 DLC 分布式训练、DSW 交互式建模和资源管理等典型场景中的完整命令流程,按产品模块分类,可直接复制修改使用。

DLC 分布式训练实战

场景 1:PyTorch DDP 分布式训练全流程

# 1. 提交 PyTorchJob 分布式训练作业
JOB=$(pai dlc job submit \
  --name resnet50-ddp \
  --job-type PyTorchJob \
  --image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
  --worker-spec ecs.gn6v-c8g1.8xlarge \
  --worker-count 4 \
  --command "torchrun --nproc_per_node=8 train.py --batch-size 256" \
  --data-source d-xxxxxxxxxxxxx:/mnt/data \
  --code-source code-xxxxxxxxxxxxx \
  --code-branch main \
  --code-mount-path /workspace \
  --max-running-time-minutes 1440 \
  -o json | jq -r '.job_id')

echo "Job ID: $JOB"

# 2. 等待作业进入运行状态
pai dlc job wait $JOB --for Running --timeout 600

# 3. 查看作业详情和 Pod 列表
pai dlc job get $JOB

# 4. 查看 Pod 日志(先获取 Pod ID)
pai dlc job logs $JOB --pod <pod-id> --follow

# 5. 等待作业完成
pai dlc job wait $JOB --for Succeeded --timeout 86400 \
  && echo "训练完成!" || echo "训练失败,请检查日志"

场景 2:TFJob Parameter Server 架构训练

适用于推荐系统、大规模稀疏特征场景,使用 Chief + Worker + PS 三角色拓扑。

# 提交 TFJob,使用 --role 定义多角色拓扑
pai dlc job submit \
  --name deepfm-ps-training \
  --job-type TFJob \
  --image tensorflow:2.13.0-gpu \
  --command "python train.py --ps_hosts=$TF_PS_HOSTS --worker_hosts=$TF_WORKER_HOSTS" \
  --role chief:count=1,spec=ecs.gn6v-c8g1.8xlarge \
  --role worker:count=4,spec=ecs.gn6v-c8g1.8xlarge \
  --role ps:count=2,spec=ecs.g7.16xlarge \
  --data-source d-xxxxxxxxxxxxx:/mnt/data \
  --code-source code-xxxxxxxxxxxxx \
  --code-mount-path /workspace \
  --success-policy ChiefWorker \
  --max-running-time-minutes 2880

# PS 架构建议设置 success-policy 为 ChiefWorker(Chief 成功即判定作业成功)

场景 3:MPIJob Horovod 多机训练

# 提交 MPIJob,使用 Horovod 进行分布式训练
pai dlc job submit \
  --name horovod-bert \
  --job-type MPIJob \
  --image horovod:0.28.1-tf2.11.0-torch1.13.1-gpu \
  --worker-spec ecs.gn7i-c8g1.2xlarge \
  --worker-count 4 \
  --command "mpirun -np 32 -H localhost:8,worker-1:8,worker-2:8,worker-3:8 \
    -bind-to none -map-by slot \
    -x NCCL_DEBUG=INFO \
    python train_horovod.py --batch-size 64" \
  --data-source d-xxxxxxxxxxxxx:/mnt/data \
  --code-source code-xxxxxxxxxxxxx \
  --code-mount-path /workspace

# MPIJob 会在 Worker 之间自动建立 SSH 互信,无需手动配置

场景 4:包年包月资源提交与优先级调度

说明

使用包年包月资源前,需先通过 pai resource quota list 获取可用的 Quota ID。

# 1. 查看可用配额
pai resource quota list --status Ready --verbose

# 2. 使用包年包月配额提交作业(高优先级)
pai dlc job submit \
  --name llama2-finetune-priority \
  --job-type PyTorchJob \
  --image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
  --resource-id quota-a1b2c3d4e5f6 \
  --cpu 8 \
  --memory 64Gi \
  --gpu 1 \
  --gpu-type A100 \
  --shared-memory 16Gi \
  --priority 9 \
  --command "torchrun --nproc_per_node=1 finetune.py --model llama2-7b" \
  --data-source d-xxxxxxxxxxxxx:/mnt/data \
  --code-source code-xxxxxxxxxxxxx \
  --code-mount-path /workspace \
  --max-running-time-minutes 7200

# 3. 查看作业在队列中的位置
pai resource quota workloads quota-a1b2c3d4e5f6 --workload-type dlc --status Enqueued

场景 5:多 Role 拓扑配置(Chief + Worker + Evaluator)

不同 Role 可使用不同镜像和资源规格,优化成本。

# Chief 和 Worker 使用 GPU 镜像,Evaluator 使用 CPU 镜像降低成本
pai dlc job submit \
  --name multi-role-training \
  --job-type TFJob \
  --command "python train.py" \
  --role chief:count=1,spec=ecs.gn6v-c8g1.8xlarge,image=tensorflow:2.13.0-gpu \
  --role worker:count=4,spec=ecs.gn6v-c8g1.8xlarge,image=tensorflow:2.13.0-gpu \
  --role evaluator:count=1,spec=ecs.g7.xlarge,image=tensorflow:2.13.0-cpu \
  --data-source d-xxxxxxxxxxxxx:/mnt/data \
  --code-source code-xxxxxxxxxxxxx \
  --code-mount-path /workspace \
  --success-policy ChiefWorker

场景 6:抢占式实例低成本训练

警告

抢占式实例可能被回收,建议配合 checkpoint 机制使用,确保训练可恢复。

# 使用抢占式实例提交训练作业(通过 --role 的 spot 参数)
pai dlc job submit \
  --name spot-training \
  --job-type PyTorchJob \
  --image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
  --command "torchrun --nproc_per_node=8 train.py --batch-size 256 --checkpoint-interval 100" \
  --role worker:count=4,spec=ecs.gn6v-c8g1.8xlarge,spot=true,spot-strategy=SpotAsPriceGo \
  --data-source d-xxxxxxxxxxxxx:/mnt/data \
  --code-source code-xxxxxxxxxxxxx \
  --code-mount-path /workspace \
  --max-running-time-minutes 1440

# spot-strategy 可选:
#   SpotAsPriceGo       — 按量付费价格出价(推荐)
#   SpotWithPriceLimit  — 设置最高出价(需配合 spot-price-limit)

场景 7:作业失败排查与日志分析

# 1. 列出失败的作业
pai dlc job list --status Failed -o json | jq '.[] | {job_id, display_name, status, gmt_create_time}'

# 2. 查看失败作业详情(含 Pod 状态和错误信息)
pai dlc job get <job-id>

# 3. 查看作业/Pod 的系统事件(Pending 原因、OOMKilled、重启记录)
pai dlc job events <job-id>
pai dlc job events <job-id> --pod <pod-id>

# 4. 查看失败 Pod 的日志(省略 --pod 时自动读主 Pod)
pai dlc job logs <job-id> --pod <pod-id> --tail 500

# 5. 查看最近 30 分钟的日志
pai dlc job logs <job-id> --pod <pod-id> --since 30m

# 6. 常见失败原因速查:
#    OOM                → 减少 batch-size 或增加 memory
#    ImagePullBackOff   → 检查镜像地址和认证配置
#    ResourceInsufficient → 更换规格或使用包年包月配额
#    CodeCloneFailed    → 检查代码源配置和分支名

场景 8:CI/CD 自动化训练流水线

# 1. 使用 --dry-run 验证作业配置
pai dlc job submit \
  --name ci-training-${CI_COMMIT_SHA::8} \
  --job-type PyTorchJob \
  --image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
  --worker-spec ecs.gn6v-c8g1.8xlarge \
  --worker-count 2 \
  --command "torchrun --nproc_per_node=8 train.py --epochs 5" \
  --code-source code-xxxxxxxxxxxxx \
  --code-branch $CI_COMMIT_REF_NAME \
  --code-mount-path /workspace \
  --dry-run

# 2. 正式提交并阻塞等待结果(适合 CI/CD 流水线)
pai dlc job submit \
  --name ci-training-${CI_COMMIT_SHA::8} \
  --job-type PyTorchJob \
  --image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
  --worker-spec ecs.gn6v-c8g1.8xlarge \
  --worker-count 2 \
  --command "torchrun --nproc_per_node=8 train.py --epochs 5" \
  --code-source code-xxxxxxxxxxxxx \
  --code-branch $CI_COMMIT_REF_NAME \
  --code-mount-path /workspace \
  --wait --wait-for Succeeded --wait-timeout 7200

# 3. 根据退出码判断结果(0=成功,非 0=失败)
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
  echo "✅ 训练成功,开始部署模型..."
else
  echo "❌ 训练失败(退出码: $EXIT_CODE),请检查日志"
  pai dlc job logs $JOB --pod <pod-id>
  exit 1
fi

DSW 交互式建模实战

场景 9:创建 GPU 实例 → 训练 → 下载结果

# 1. 查看可用 GPU 规格
pai dsw instance list-ecs-specs --accelerator-type GPU

# 2. 创建实例并等待就绪
INSTANCE=$(pai dsw instance create \
  --name train-bert-$(date +%Y%m%d) \
  --ecs-spec ecs.gn7i-c8g1.2xlarge \
  --image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
  --idle-timeout 60 \
  -o json | jq -r '.instance_id')

pai dsw instance wait $INSTANCE --for Running

# 3. 上传训练脚本和数据
pai dsw instance cp ./train.py dsw:/workspace/train.py
pai dsw instance cp ./data/ dsw:/workspace/data/

# 4. 执行训练
pai dsw instance exec $INSTANCE -- \
  "cd /workspace && python train.py --epochs 10 --batch-size 32"

# 5. 下载模型和日志
pai dsw instance cp dsw:/workspace/checkpoints/ ./checkpoints/
pai dsw instance cp dsw:/workspace/logs/ ./logs/

# 6. 停止实例(不再计费)
pai dsw instance stop $INSTANCE

场景 10:使用 MCP 连接 AI 编程助手

# 1. 设置当前实例上下文
pai dsw instance use <instance-id>

# 2. 生成 MCP 配置
pai dsw instance mcp-config

# 3. 启动本地代理(后台运行)
pai dsw instance proxy start -d

# 4. 在 Cursor / Claude Code 等客户端中添加 MCP Server:
#    URL: http://localhost:9806/mcp

# 5. 查看代理状态
pai dsw instance proxy list

# 6. 使用完毕后停止代理
pai dsw instance proxy stop --all

场景 11:批量管理实例

# 停止所有 Running 状态的实例
pai dsw instance list --status Running -o json | jq -r '.[].instance_id' | \
  while read id; do pai dsw instance stop "$id"; done

# 删除所有已停止超过 7 天的实例
pai dsw instance list --status Stopped -o json | \
  jq -r '.[] | select(.gmt_modified_time < "'$(date -d '7 days ago' -Iseconds)'") | .instance_id' | \
  while read id; do pai dsw instance delete "$id" -y; done

场景 12:声明式管理实例配置(save → render → apply)

把实例配置纳入文件/Git 管理,团队内复用同一套环境定义。

# 1. 将现有实例导出为配置文件(凭证永不写入)
pai dsw instance save dsw-xxxx --out team-env.yaml

# 2. 编辑配置(换名字、改规格…)后离线校验,不需凭证可在 CI 运行
pai dsw instance render -f team-env.yaml

# 3. 预览将创建/变更的内容
pai dsw instance apply -f team-env.yaml --dry-run

# 4. 应用:不存在则创建,存在则更新,无差异则不发请求(幂等)
pai dsw instance apply -f team-env.yaml

# 5. 临时改一个字段再应用(不动文件)
pai dsw instance apply -f team-env.yaml --override EcsSpec=ecs.gn7i-c16g1.4xlarge

资源管理实战

场景 13:检查资源配额 → 排队状态

# 1. 查看所有配额
pai resource quota list

# 2. 查看特定配额的详细信息(含用量)
pai resource quota get <quota-id>

# 3. 查看配额中的排队情况(所有人的)
pai resource quota workloads <quota-id> --all-users --status Enqueued

# 4. 查看谁在占用资源
pai resource quota user-usage <quota-id> --sort-by UsedGPU --order desc

# 5. 查看我的工作负载状态
pai resource quota workloads <quota-id>