PaiMode.md 7.35 KB
Newer Older
Chi Song's avatar
Chi Song committed
1
2
3
4
5
6
# **在 OpenPAI 上运行 Experiment**

NNI 支持在 [OpenPAI](https://github.com/Microsoft/pai) (简称 pai)上运行 Experiment,即 pai 模式。 在使用 NNI 的 pai 模式前, 需要有 [OpenPAI](https://github.com/Microsoft/pai) 群集的账户。 如果没有 OpenPAI 账户,参考[这里](https://github.com/Microsoft/pai#how-to-deploy)来进行部署。 在 pai 模式中,会在 Docker 创建的容器中运行 Trial 程序。

## 设置环境

Chi Song's avatar
Chi Song committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
步骤 1. 参考[指南](../Tutorial/QuickStart.md)安装 NNI。

步骤 2. 获取 OpenPAI 的令牌。  
点击 OpenPAI 界面右上方的 `My profile` 按钮。 ![](../../img/pai_token_button.jpg) 找到 token management,复制当前账号的令牌。 ![](../../img/pai_token_profile.jpg)

步骤 3. 将 NFS 存储挂载到本机。  
点击 OpenPAI 网站的 `Submit job` 按钮。 ![](../../img/pai_job_submission_page.jpg)  
在作业提交页面找到数据管理区域。 ![](../../img/pai_data_management_page.jpg)  
`DEFAULT_STORAGE` 字段是在作业运行起来后,OpenPAI 容器中挂载的路径。 `Preview container paths` 是 API 提供的 NFS 主机和路径,需要将对应的位置挂载到本机,然后 NNI 才能使用 NFS 存储。  
例如,使用下列命令:

    sudo mount nfs://gcr-openpai-infra02:/pai/data /local/mnt
    

然后容器中的 `/data` 路径会被挂载到本机的 `/local/mnt` 文件夹  
然后在 NNI 的配置文件中如下配置:

    nniManagerNFSMountPath: /local/mnt
    containerNFSMountPath: /data
    

步骤 4. 获取 OpenPAI 存储插件名称。 联系 OpenPAI 管理员,获得 NFS 存储插件的名称。 默认存储的名称是 `teamwise_storage`,NNI 配置文件中的配置如下:

    paiStoragePlugin: teamwise_storage
    
Chi Song's avatar
Chi Song committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

## 运行 Experiment

`examples/trials/mnist-annotation` 为例。 NNI 的 YAML 配置文件如下:

```yaml
authorName: your_name
experimentName: auto_mnist
# 并发运行的 Trial 数量
trialConcurrency: 2
# Experiment 的最长持续运行时间
maxExecDuration: 3h
# 空表示一直运行
maxTrialNum: 100
# 可选项: local, remote, pai
trainingServicePlatform: pai
LongzeSong's avatar
LongzeSong committed
48
49
# 搜索空间文件
searchSpacePath: search_space.json
Chi Song's avatar
Chi Song committed
50
# 可选项: true, false
Chi Song's avatar
Chi Song committed
51
52
53
54
55
56
57
58
59
60
61
useAnnotation: true
tuner:
  builtinTunerName: TPE
  classArgs:
    optimize_mode: maximize
trial:
  command: python3 mnist.py
  codeDir: ~/nni/examples/trials/mnist-annotation
  gpuNum: 0
  cpuNum: 1
  memoryMB: 8196
Chi Song's avatar
Chi Song committed
62
  image: msranni/nni:latest
Chi Song's avatar
Chi Song committed
63
64
65
  virtualCluster: default
  nniManagerNFSMountPath: /home/user/mnt
  containerNFSMountPath: /mnt/data/user
Chi Song's avatar
Chi Song committed
66
  paiStoragePlugin: team_wise
Chi Song's avatar
Chi Song committed
67
# 配置要访问的 OpenPAI 集群
Chi Song's avatar
Chi Song committed
68
69
paiConfig:
  userName: your_pai_nni_user
Chi Song's avatar
Chi Song committed
70
  token: your_pai_token
Chi Song's avatar
Chi Song committed
71
72
73
74
75
  host: 10.1.1.1
```

注意:如果用 pai 模式运行,需要在 YAML 文件中设置 `trainingServicePlatform: pai`

Chi Song's avatar
Chi Song committed
76
[本机模式](LocalMode.md),以及[远程计算机模式](RemoteMachineMode.md)相比,pai 模式的 Trial 需要额外的配置:
Chi Song's avatar
Chi Song committed
77
78

* cpuNum 
Chi Song's avatar
Chi Song committed
79
    * 可选。 Trial 程序的 CPU 需求,必须为正数。 如果没在 Trial 配置中设置,则需要在 `paiConfigPath` 指定的配置文件中设置。
Chi Song's avatar
Chi Song committed
80
* memoryMB 
Chi Song's avatar
Chi Song committed
81
    * 可选。 Trial 程序的内存需求,必须为正数。 如果没在 Trial 配置中设置,则需要在 `paiConfigPath` 指定的配置文件中设置。
Chi Song's avatar
Chi Song committed
82
* image 
Chi Song's avatar
Chi Song committed
83
84
    * 可选。 在 pai 模式中,Trial 程序由 OpenPAI 在 [Docker 容器](https://www.docker.com/)中安排运行。 此键用来指定 Trial 程序的容器使用的 Docker 映像。
    * [Docker Hub](https://hub.docker.com/) 上有预制的 NNI Docker 映像 [nnimsra/nni](https://hub.docker.com/r/msranni/nni/)。 它包含了用来启动 NNI Experiment 所依赖的所有 Python 包,Node 模块和 JavaScript。 生成此 Docker 映像的文件在[这里](https://github.com/Microsoft/nni/tree/master/deployment/docker/Dockerfile)。 可以直接使用此映像,或参考它来生成自己的映像。 如果没在 Trial 配置中设置,则需要在 `paiConfigPath` 指定的配置文件中设置。
Chi Song's avatar
Chi Song committed
85
* virtualCluster 
Chi Song's avatar
Chi Song committed
86
    * 可选。 设置 OpenPAI 的 virtualCluster,即虚拟集群。 如果未设置此参数,将使用默认(default)虚拟集群。
Chi Song's avatar
Chi Song committed
87
88
89
90
91
* nniManagerNFSMountPath 
    * 必填。 在 nniManager 计算机上设置挂载的路径。
* containerNFSMountPath 
    * 必填。 在 OpenPAI 的容器中设置挂载路径。
* paiStoragePlugin 
Chi Song's avatar
Chi Song committed
92
93
94
    * 可选。 设置 PAI 中使用的存储插件的名称。 如果没在 Trial 配置中设置,则需要在 `paiConfigPath` 指定的配置文件中设置。
* paiConfigPath 
    * 可选。 设置 OpenPAI 作业配置文件路径,文件为 YAML 格式。
Chi Song's avatar
Chi Song committed
95

Chi Song's avatar
Chi Song committed
96
97
98
99
100
完成并保存 NNI Experiment 配置文件后(例如可保存为:exp_pai.yml),运行以下命令:

    nnictl create --config exp_pai.yml
    

Chi Song's avatar
Chi Song committed
101
来在 pai 模式下启动 Experiment。 NNI 会为每个 Trial 创建 OpenPAI 作业,作业名称的格式为 `nni_exp_{experiment_id}_trial_{trial_id}`。 可以在 OpenPAI 集群的网站中看到 NNI 创建的作业,例如: ![](../../img/nni_pai_joblist.jpg)
Chi Song's avatar
Chi Song committed
102
103
104
105
106

注意:pai 模式下,NNIManager 会启动 RESTful 服务,监听端口为 NNI 网页服务器的端口加1。 例如,如果网页端口为`8080`,那么 RESTful 服务器会监听在 `8081`端口,来接收运行在 Kubernetes 中的 Trial 作业的指标。 因此,需要在防火墙中启用端口 `8081` 的 TCP 协议,以允许传入流量。

当一个 Trial 作业完成后,可以在 NNI 网页的概述页面(如:http://localhost:8080/oview)中查看 Trial 的信息。

Chi Song's avatar
Chi Song committed
107
在 Trial 列表页面中展开 Trial 信息,点击如下的 logPath: ![](../../img/nni_webui_joblist.jpg)
Chi Song's avatar
Chi Song committed
108

Chi Song's avatar
Chi Song committed
109
接着将会打开 HDFS 的 WEB 界面,并浏览到 Trial 的输出文件: ![](../../img/nni_trial_hdfs_output.jpg)
Chi Song's avatar
Chi Song committed
110
111
112

在输出目录中可以看到三个文件:stderr, stdout, 以及 trial.log

Chi Song's avatar
Chi Song committed
113
## 数据管理
Chi Song's avatar
Chi Song committed
114

Chi Song's avatar
Chi Song committed
115
使用 NNI 启动 Experiment 前,应在 nniManager 计算机中设置相应的挂载数据的路径。 OpenPAI 有自己的存储(NFS、AzureBlob ...),在 PAI 中使用的存储将在启动作业时挂载到容器中。 应通过 `paiStoragePlugin` 字段选择 OpenPAI 中的存储类型。 然后,应将存储挂载到 nniManager 计算机上,并在配置文件中设置 `nniManagerNFSMountPath`,NNI会生成 bash 文件并将 `codeDir` 中的数据拷贝到 `nniManagerNFSMountPath` 文件夹中,然后启动 Trial 任务。 `nniManagerNFSMountPath` 中的数据会同步到 OpenPAI 存储中,并挂载到 OpenPAI 的容器中。 容器中的数据路径在 `containerNFSMountPath` 设置,NNI 将进入该文件夹,运行脚本启动 Trial 任务。
Chi Song's avatar
Chi Song committed
116
117
118

## 版本校验

Chi Song's avatar
Chi Song committed
119
从 0.6 开始,NNI 支持版本校验。确保 NNIManager 与 trialKeeper 的版本一致,避免兼容性错误。 检查策略:
Chi Song's avatar
Chi Song committed
120
121

1. 0.6 以前的 NNIManager 可与任何版本的 trialKeeper 一起运行,trialKeeper 支持向后兼容。
Chi Song's avatar
Chi Song committed
122
123
2. 从 NNIManager 0.6 开始,与 triakKeeper 的版本必须一致。 例如,如果 NNIManager 是 0.6 版,则 trialKeeper 也必须是 0.6 版。
3. 注意,只有版本的前两位数字才会被检查。例如,NNIManager 0.6.1 可以和 trialKeeper 的 0.6 或 0.6.2 一起使用,但不能与 trialKeeper 的 0.5.1 或 0.7 版本一起使用。
Chi Song's avatar
Chi Song committed
124

Chi Song's avatar
Chi Song committed
125
如果 Experiment 无法运行,而且不能确认是否是因为版本不匹配造成的,可以在 Web 界面检查是否有相关的错误消息。 ![](../../img/version_check.png)