管理 DSW(Data Science Workshop)交互式建模实例的完整生命周期。
instance create
pai dsw instance create --name NAME [flags]参数 | 类型 | 默认值 | 说明 |
| string | — | 实例名称 必填 |
| string | — | 镜像 ID(与 --image-url 二选一) |
| string | — | 镜像仓库地址(与 --image 二选一) |
| string | — | 私有镜像 Base64 认证信息 |
| string | — | 按量付费:ECS 实例规格,如 ecs.gn7i-c8g1.2xlarge |
| string | — | 包年包月:资源配额 ID |
| string | — | 包年包月:CPU 核数 |
| string | — | 包年包月:内存大小(如 8GB) |
| int | — | 包年包月:GPU 卡数 |
| string | — | 包年包月:GPU 卡型(如 V100、A100、T4) |
| string | — | 包年包月:共享内存大小(如 8GB) |
| int | — | 包年包月:调度优先级,1(最低)至 9(最高) |
| string | — | 超卖策略 |
| string | config | 工作空间 ID |
| stringArray | — | 数据集挂载,格式 id:mount_path[:version],可重复指定 |
| string | — | 数据集 JSON 数组(API 字段格式) |
| string | — | 云盘容量(如 100Gi),与 --cloud-disks 互斥 |
| string | — | 云盘配置 JSON 数组 |
| stringArray | — | 环境变量 KEY=VALUE,可重复指定 |
| stringArray | — | 实例标签 KEY=VALUE,可重复指定 |
| stringArray | — | 实例标记 KEY=VALUE,可重复指定 |
| string | — | 每次启动时执行的脚本命令 |
| string | — | UserCommand JSON(与 --startup-script 互斥) |
| int | — | 空闲自动关机时间(分钟) |
| string | — | NVIDIA 驱动版本(如 535.230.02) |
| string | — | 可见性 |
| string | — | SSH 公钥,设置后开启 SSH 访问 |
| string | — | SSH 访问方式,逗号分隔:intranet, internet |
| string | 22 | SSH 监听端口 |
| string | 自动 | SSH 公网映射端口 |
| string | — | 弹性公网 IP 分配 ID(配合 NAT 网关使用) |
| string | — | NAT 网关 ID |
| string | — | NLB 网关 ID(与 NAT/EIP 互斥) |
| string | — | VPC 网络配置 JSON |
| string | — | 按量付费:抢占式实例配置 JSON |
| stringArray | — | 自定义服务/端口转发配置,可重复指定 |
| string | — | 调度亲和性配置 JSON |
| string | — | 节点调度配置 JSON |
| string | — | 实例凭证配置 JSON |
| string | — | Docker 配置 JSON |
| string | — | 动态挂载配置 JSON |
| string | — | 实例迁移选项(灵骏 + 持久化存储场景) |
| string | — | 工作目录存储来源 |
| bool | false | 打印组装好的请求体(PascalCase,与 API 拼写一致)而不创建,可检查后配合 --body-json 复用 |
| string | — | 完整请求体 JSON(覆盖以上所有选项) |
| bool | false | 输出 JSON 请求模板后退出,用于生成 --body-json 模板 |
| stringArray | — | 提交前修改请求体字段,详见--override 请求体编辑 |
常用示例
# 1. 按量付费:最简创建(ECS 规格 + 镜像)
pai dsw instance create \
--name my-instance \
--ecs-spec ecs.gn7i-c8g1.2xlarge \
--image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04
# 2. 包年包月:使用资源配额(用 cpu/gpu/memory,不能用 ecs-spec)
pai dsw instance create \
--name my-prepaid \
--resource-id quota-a1b2c3d4e5f6 \
--cpu 8 --memory 16GB --gpu 1 --gpu-type A100 \
--image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04
# 3. 挂载数据集 + 空闲自动关机(60 分钟无操作后关机)
pai dsw instance create \
--name my-instance \
--ecs-spec ecs.gn7i-c8g1.2xlarge \
--image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
--dataset d-abc123:/mnt/data \
--idle-timeout 60
# 4. 开启 SSH 远程访问
pai dsw instance create \
--name my-instance \
--ecs-spec ecs.gn7i-c8g1.2xlarge \
--image pytorch:2.0.1-gpu-py310-cu118-ubuntu20.04 \
--ssh-public-key "ssh-rsa AAAA..." \
--ssh-access-type internet
# 5. --generate-skeleton 生成请求模板(用于 --body-json 高级配置)
pai dsw instance create --generate-skeleton > instance.json
# 编辑 instance.json 后提交
pai dsw instance create --body-json "$(cat instance.json)"按量付费(--ecs-spec)和包年包月(--resource-id + cpu/gpu/memory)二选一,不能同时使用。
instance list
pai dsw instance list [flags]参数 | 类型 | 默认值 | 说明 |
| string | config | 工作空间 ID |
| string | — | 按状态过滤(如 Running、Stopped、Creating) |
| string | — | 按实例名称过滤 |
| string | ALL | 资源组 ID。ALL 表示全部,空值表示仅按量付费实例 |
| string | — | 排序字段 |
| string | — | 排序方向 |
| int | 1 | 页码 |
| int | 20 | 每页条数 |
| string | — | 自定义输出列,逗号分隔。默认:instance_id, instance_name, status, payment_type, ecs_spec, image_id, workspace_id, gmt_create_time, gmt_modified_time |
instance get
pai dsw instance get <instance-id> [--all] [--raw] [--show-secrets]参数 | 类型 | 默认值 | 说明 |
| bool | false | table 视图显示全部字段(json/yaml 输出本就包含全部字段) |
| bool | false | 显示服务密码与镜像仓库认证明文。默认只显示 |
| bool | false | 输出未加工的原始 API 响应:凭证原样输出、含 HTTP 信封、API 未设置的字段直接省略(而非显示 null) |
instance update
pai dsw instance update <instance-id> [flags]更新实例属性。只发送你传入的字段(patch 语义),未传入的字段保持原值;因此删除某项配置需用对应的 --remove-* 参数(会要求确认)。
参数 | 类型 | 默认值 | 说明 |
| — | 不修改 | 替换对应字段(含义同 create) |
| stringArray | 不修改 | 替换整组配置(非增量合并) |
| bool | false | 删除对应配置,需确认(或加 |
| string | — | 完整 UpdateInstance 请求体(PascalCase),支持 |
| stringArray | — | 提交前修改请求体字段,详见 --override 请求体编辑 |
| bool | false | 仅打印请求体,不发送 |
| bool | false | 跳过 --remove-* 的确认提示 |
instance apply / save / render — 声明式管理
用配置文件管理实例(类 kubectl 工作流):save 导出在线实例的配置 → 编辑 → render 离线校验 → apply 创建或更新。
pai dsw instance save <instance-id> [--out FILE] [--format yaml|json] [--force]
pai dsw instance render -f <file>
pai dsw instance apply -f <file> [--dry-run] [--prune] [-y]参数 | 类型 | 默认值 | 说明 |
| string | stdout | 写入文件;已存在时拒绝覆盖,需配合 |
| string | yaml | 导出格式 |
| string | — | 实例配置文件(JSON/YAML), |
| string | 按名称查找 | 直接指定目标实例,跳过查找;覆盖文件中的 InstanceId |
| bool | false | 对比在线实例,显示将创建或变更的内容;无差异时输出 |
| bool | false | 删除文件中未声明且可删除的配置(数据集、环境变量、VPC、驱动等),需确认 |
| stringArray | — | 应用前修改配置字段,详见--override 请求体编辑 |
# 1. 导出在线实例配置(仅含创建请求接受的字段,凭证永不写入)
pai dsw instance save dsw-xxxx --out instance.yaml
# 2. 编辑后离线校验(不调 API、不需凭证,可在 CI 中运行)
pai dsw instance render -f instance.yaml
# 3. 预览差异后应用
pai dsw instance apply -f instance.yaml --dry-run
pai dsw instance apply -f instance.yaml
# 管道用法:导出后直接校验
pai dsw instance save dsw-xxxx | pai dsw instance render -f -apply 按配置中的实例名查找目标:不存在则创建,存在则更新,无差异则不发送请求(幂等)。适合把实例配置纳入 Git 管理。
instance delete
pai dsw instance delete <instance-id> [-y] [--dry-run]参数 | 类型 | 默认值 | 说明 |
| bool | false | 跳过确认提示(脚本/自动化场景必须指定) |
| bool | false | 仅预览操作,不实际执行 |
若被删除的实例正是 instance use 设置的当前上下文,删除后 CLI 会自动清除该 context,避免后续命令指向已不存在的实例。
instance start / stop
pai dsw instance start <instance-id> [--dry-run]
pai dsw instance stop <instance-id> [--dry-run]参数 | 类型 | 默认值 | 说明 |
| bool | false | 仅预览操作,不实际执行 |
instance wait
pai dsw instance wait <instance-id> --for <status>参数 | 类型 | 默认值 | 说明 |
| string | — | 目标状态 必填 |
| int | 5 | 轮询间隔(秒) |
| int | 300 | 最大等待时间(秒) |
instance exec
pai dsw instance exec [instance-id] [-- command...]参数 | 类型 | 默认值 | 说明 |
| string | — | 执行 Python 代码片段 |
| string | — | 上传本地文件到实例并执行 |
| string | /tmp/dsw_exec_<name> | 远程上传目标路径(以 / 结尾表示目录) |
| string | — | 远程工作目录 |
| int | 120 | 命令执行超时时间(秒) |
| string | gateway | 数据面通道 |
使用引号包裹含管道/重定向的命令:pai dsw instance exec -- "ls -la | grep model"
常用示例
# 1. 执行 Shell 命令(含管道,需用引号包裹)
pai dsw instance exec -- "ls -la /workspace | grep .pt"
# 2. 执行 Python 代码片段
pai dsw instance exec --code "import torch; print(torch.cuda.is_available())"
# 3. 上传本地脚本到实例并执行
pai dsw instance exec --file ./train.py
# 4. 指定远程工作目录和超时时间
pai dsw instance exec --cwd /workspace --timeout 600 -- "python eval.py"
# 5. 上传脚本到指定路径并执行
pai dsw instance exec --file ./test.py --remote-path /workspace/test.py
# 6. 通过 VPC 通道执行(实例未开启公网时)
pai dsw instance exec --via vpc -- "nvidia-smi"instance cp
pai dsw instance cp <src> <dst> [flags]上传:pai dsw instance cp ./local.txt dsw:/workspace/file.txt
下载:pai dsw instance cp dsw:/workspace/out.csv ./
参数 | 类型 | 默认值 | 说明 |
| string | 当前上下文 | 目标实例 ID(未设置上下文时需指定) |
| string | gateway | 数据面通道 |
| bool | false | 仅预览操作,不实际执行 |
常用示例
# 1. 上传本地文件到实例
pai dsw instance cp ./train.py dsw:/workspace/train.py
# 2. 下载实例文件到本地
pai dsw instance cp dsw:/workspace/model.pt ./
# 3. 上传整个目录
pai dsw instance cp ./data/ dsw:/workspace/data/
# 4. 未设置上下文时指定实例
pai dsw instance cp ./config.yaml dsw:/workspace/ --instance i-xxxxxxxx
# 5. 通过 VPC 通道传输
pai dsw instance cp ./large-file.bin dsw:/workspace/ --via vpc
# 6. 预览操作(不实际传输)
pai dsw instance cp ./train.py dsw:/workspace/train.py --dry-runinstance logs
pai dsw instance logs [instance-id] [flags]参数 | 类型 | 默认值 | 说明 |
| bool | false | 持续跟踪事件流(类似 tail -f) |
| string | — | 仅显示指定时间之后的事件(如 30m、2h、3d) |
| int | 2000 | 最大返回事件数 |
instance metrics
pai dsw instance metrics <instance-id> --metric <NAME> [flags]读取实例的云监控(CloudMonitor)指标,如 CPU/GPU 利用率、内存、磁盘。先用 --list-metrics 查看可用指标目录。
指标仅对包年包月(Subscription)实例返回数据。按量付费实例的 API 会成功返回空序列,CLI 会检查付费类型并明确提示——否则一张空表看起来会像实例空闲。
参数 | 类型 | 默认值 | 说明 |
| string | — | CloudMonitor 指标名 必填(或用 --list-metrics 查看目录) |
| bool | false | 打印指标目录后退出 |
| string | -1h / now | 时间窗口:RFC3339 或相对偏移(如 -90m、-6h、-2d) |
| string | 60 | 采样间隔(秒),仅支持 |
| int | 1440 | 每页最大数据点数(API 上限 1440) |
| string | — | 翻页令牌(从 |
| string | 自动构建 | 覆盖 CloudMonitor 维度 JSON,如加 "gpu" 维度限定 CARD_* 指标的单张卡;常规使用无需传 |
| string | acs_pai_dsw | CloudMonitor 命名空间,一般无需修改 |
# 查看可用指标
pai dsw instance metrics dsw-xxxx --list-metrics
# 最近 1 小时的 GPU 显存利用率
pai dsw instance metrics dsw-xxxx --metric INSTANCE_GPU_ACCELERATOR_MEMORY_UTIL
# 指定时间窗口与采样间隔(5 分钟精度)
pai dsw instance metrics dsw-xxxx --metric INSTANCE_CPU_UTIL \
--start 2026-08-16T00:00:00Z --end 2026-08-16T06:00:00Z --period 300CARD_* 指标每张 GPU 一条序列(以 gpu 列区分),16 卡实例每个时间点产生 16 行。API 按卡分页:长窗口触顶时保留全部时间戳但丢掉整卡,结果看似完整实则不然,CLI 会报告 truncated——缩短窗口或调大 --period 即可看全。
instance sanity-check
pai dsw instance sanity-check <instance-id> [--type ssh] [flags]对实例发起健康检查并等待结果。检查是异步任务:默认创建后轮询直到终态;用 --async 只拿任务 ID 立即返回,后续用 --task-id 回读结果。
参数 | 类型 | 默认值 | 说明 |
| string | ssh | 检查类型,已知: |
| bool | false | 创建任务后立即返回任务 ID,不等待结果 |
| string | — | 读取已有任务的结果(与 --async 互斥) |
| bool | false | 返回逐检查点详情(不传时仅返回问题列表) |
| int | 5 / 300 | 轮询间隔与最大等待时间(秒) |
instance use / current
pai dsw instance use <instance-id> [--via gateway|vpc|internet] [--force]
pai dsw instance current参数 | 类型 | 默认值 | 说明 |
| string | gateway | 设置默认数据面通道 |
| bool | false | 使用 vpc/internet 通道时,即使不可达也设置上下文 |
设置上下文后,后续 exec / cp / rootfs-usage 等命令可省略 instance-id。
instance list-ecs-specs
pai dsw instance list-ecs-specs --accelerator-type <CPU|GPU>参数 | 类型 | 默认值 | 说明 |
| string | — | 加速器类型 必填 |
| string | — | 排序字段 |
| string | — | 排序方向 |
| int | 1 | 页码 |
| int | 100 | 每页条数 |
| string | — | 自定义输出列。默认:instance_type, accelerator_type, cpu, memory, gpu, gpu_type, gpu_memory_size, is_available |
instance rootfs-usage
pai dsw instance rootfs-usage [instance-id] [flags]分析实例根文件系统磁盘占用,自动排除已挂载的数据集、云盘和 /mnt/workspace 等非系统盘路径。
参数 | 类型 | 默认值 | 说明 |
| string | / | 分析的起始路径 |
| int | 2 | 目录展示深度 |
| stringArray | — | 额外排除的路径,可重复指定 |
| bool | true | 在输出中显示排除路径列表 |
| int | 5 | 扫描轮询间隔(秒) |
| int | 300 | 最大等待时间(秒) |
| string | gateway | 数据面通道 |
instance mcp-config
pai dsw instance mcp-config [instance-id] [--via internet|vpc]输出标准 mcpServers JSON 配置,可直接粘贴到支持 MCP 协议的客户端(如 Cursor、Claude Code)中使用。
参数 | 类型 | 默认值 | 说明 |
| string | internet | 使用的服务地址类型 |
instance proxy
pai dsw instance proxy start [instance-id] [--port 9806] [-d]
pai dsw instance proxy stop [instance-id] [--all]
pai dsw instance proxy list启动本地 HTTP 代理,将 MCP 流量转发到 DSW 实例。MCP 客户端连接 http://localhost:<port>/mcp 即可,无需配置阿里云凭证。
参数 | 类型 | 默认值 | 说明 |
| int | 9806 | 本地监听端口 |
| bool | false | 后台运行代理进程 |
| bool | false | 停止所有运行中的代理 |