Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 62 additions & 58 deletions docs/source/train.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## train_config

- log_step_count_steps: 200 # 每200步打印一行log
- log_step_count_steps: 200 # 每 200 步打印一行 log

- optimizer_config # 优化器相关的参数
- optimizer_config # 优化器相关的参数

```protobuf
{
Expand All @@ -21,9 +21,11 @@
```

- 多优化器支持:
- 可以配置两个optimizer, 分别对应embedding权重和dense权重;
- 实现参考EasyRecModel.get_grouped_vars和multi_optimizer.MultiOptimizer;

- 可以配置两个 optimizer, 分别对应 embedding 权重和 dense 权重;
- 实现参考 EasyRecModel.get_grouped_vars 和 multi_optimizer.MultiOptimizer;
- 示例(samples/model_config/deepfm_combo_on_avazu_embed_adagrad.config):

```protobuf
train_config {
...
Expand Down Expand Up @@ -51,49 +53,51 @@
}
}
```
- Note: [WideAndDeep](./models/wide_and_deep.md)模型的optimizer设置:
- 设置两个optimizer时, 第一个optimizer仅用于wide参数;
- 如果要给deep embedding单独设置optimizer, 需要设置3个optimizer.

- sync_replicas: true # 是否同步训练,默认是false
- Note: [WideAndDeep](./models/wide_and_deep.md)模型的 optimizer 设置:
- 设置两个 optimizer 时, 第一个 optimizer 仅用于 wide 参数;
- 如果要给 deep embedding 单独设置 optimizer, 需要设置 3 个 optimizer.

- sync_replicas: true # 是否同步训练,默认是 false

- 使用SyncReplicasOptimizer进行分布式训练(同步模式)
- 仅在train_distribute为NoStrategy时可以设置成true,其它情况应该设置为false
- PS异步训练也设置为false
- 使用 SyncReplicasOptimizer 进行分布式训练(同步模式)
- 仅在 train_distribute 为 NoStrategy 时可以设置成 true,其它情况应该设置为 false
- PS 异步训练也设置为 false
- 注意在设置为 true 时,总共的训练步数为:min(total_sample_num \* num_epochs / batch_size, num_steps) / num_workers

- train_distribute: 默认不开启Strategy(NoStrategy), strategy确定分布式执行的方式, 可以分成两种模式: PS-Worker模式 和 All-Reduce模式
- train_distribute: 默认不开启 Strategy(NoStrategy), strategy 确定分布式执行的方式, 可以分成两种模式: PS-Worker 模式 和 All-Reduce 模式

- PS-Worker模式:
- NoStrategy: 根据sync_replicas的取值决定采用同步或者异步训练
- sync_replicas=true,采用ps worker同步训练
- 注意: 该模式容易导致ps存在通信瓶颈, 建议用混合并行的模式进行同步训练
- sync_replicas=false, 采用ps worker异步训练
- All-Reduce模式:
- PS-Worker 模式:
- NoStrategy: 根据 sync_replicas 的取值决定采用同步或者异步训练
- sync_replicas=true,采用 ps worker 同步训练
- 注意: 该模式容易导致 ps 存在通信瓶颈, 建议用混合并行的模式进行同步训练
- sync_replicas=false, 采用 ps worker 异步训练
- All-Reduce 模式:
- 数据并行:
- MirroredStrategy: 单机多卡模式,仅在PAI上可以使用,本地和EMR上不能使用
- MultiWorkerMirroredStrategy: 多机多卡模式,在TF版本>=1.15时可以使用
- HorovodStragtegy: horovod多机多卡并行, 需要安装horovod
- 混合并行: 数据并行 + Embedding分片, 需要安装horovod
- EmbeddingParallelStrategy: 在horovod多机多卡并行的基础上, 增加了Embedding分片的功能
- SokStrategy: 在horovod多机多卡并行的基础上, 增加了[SOK](https://github.com/NVIDIA-Merlin/HugeCTR/tree/main/sparse_operation_kit) Key-Value Embedding和Embedding分片的功能
- 注意: 该模式仅支持GPU模式, 需要安装SOK.
- MirroredStrategy: 单机多卡模式,仅在 PAI 上可以使用,本地和 EMR 上不能使用
- MultiWorkerMirroredStrategy: 多机多卡模式,在 TF 版本>=1.15 时可以使用
- HorovodStragtegy: horovod 多机多卡并行, 需要安装 horovod
- 混合并行: 数据并行 + Embedding 分片, 需要安装 horovod
- EmbeddingParallelStrategy: 在 horovod 多机多卡并行的基础上, 增加了 Embedding 分片的功能
- SokStrategy: 在 horovod 多机多卡并行的基础上, 增加了[SOK](https://github.com/NVIDIA-Merlin/HugeCTR/tree/main/sparse_operation_kit) Key-Value Embedding 和 Embedding 分片的功能
- 注意: 该模式仅支持 GPU 模式, 需要安装 SOK.

- num_gpus_per_worker: 仅在MirrorredStrategy, MultiWorkerMirroredStrategy, PSStrategy的时候有用
- num_gpus_per_worker: 仅在 MirrorredStrategy, MultiWorkerMirroredStrategy, PSStrategy 的时候有用

- num_steps: 1000

- 总共训练多少轮
- num_steps = total_sample_num * num_epochs / batch_size / num_workers
- num_steps = total_sample_num \* num_epochs / batch_size / num_workers

- fine_tune_checkpoint: 需要restore的checkpoint路径,也可以是包含checkpoint的目录,如果目录里面有多个checkpoint,将使用最新的checkpoint
- fine_tune_checkpoint: 需要 restore 的 checkpoint 路径,也可以是包含 checkpoint 的目录,如果目录里面有多个 checkpoint,将使用最新的 checkpoint

- fine_tune_ckpt_var_map: 需要restore的参数列表文件路径,文件的每一行是{variable_name in current model}\\t{variable name in old model ckpt}
- fine_tune_ckpt_var_map: 需要 restore 的参数列表文件路径,文件的每一行是{variable_name in current model}\\t{variable name in old model ckpt}

- 需要设置fine_tune_ckpt_var_map的情形:
- current ckpt和old ckpt不完全匹配, 如embedding的名字不一样:
- 需要设置 fine_tune_ckpt_var_map 的情形:
- current ckpt 和 old ckpt 不完全匹配, 如 embedding 的名字不一样:
- old: input_layer/shopping_level_embedding/embedding_weights
- new: input_layer/shopping_embedding/embedding_weights
- 仅需要restore old ckpt里面的部分variable, 如embedding_weights
- 仅需要 restore old ckpt 里面的部分 variable, 如 embedding_weights
- 可以通过下面的文件查看参数列表

```python
Expand All @@ -106,15 +110,15 @@
print(key)
```

- save_checkpoints_steps: 每隔多少步保存一次checkpoint, 默认是1000。当训练数据量很大的时候,这个值要设置大一些
- save_checkpoints_steps: 每隔多少步保存一次 checkpoint, 默认是 1000。当训练数据量很大的时候,这个值要设置大一些

- save_checkpoints_secs: 每隔多少s保存一次checkpoint, 不可以和save_checkpoints_steps同时指定
- save_checkpoints_secs: 每隔多少 s 保存一次 checkpoint, 不可以和 save_checkpoints_steps 同时指定

- keep_checkpoint_max: 最多保存多少个checkpoint, 默认是10。当模型较大的时候可以设置为5,可节约存储
- keep_checkpoint_max: 最多保存多少个 checkpoint, 默认是 10。当模型较大的时候可以设置为 5,可节约存储

- log_step_count_steps: 每隔多少轮,打印一次训练信息,默认是10
- log_step_count_steps: 每隔多少轮,打印一次训练信息,默认是 10

- save_summary_steps: 每隔多少轮,保存一次summary信息,默认是1000
- save_summary_steps: 每隔多少轮,保存一次 summary 信息,默认是 1000

- 更多参数请参考[easy_rec/python/protos/train.proto](./reference.md)

Expand All @@ -126,15 +130,15 @@
python -m easy_rec.python.train_eval --pipeline_config_path dwd_avazu_ctr_deepmodel.config
```

- --pipeline_config_path: config文件路径
- --continue_train: restore之前的checkpoint,继续训练
- --model_dir: 如果指定了model_dir将会覆盖config里面的model_dir,一般在周期性调度的时候使用
- --edit_config_json: 使用json的方式对config的一些字段进行修改,如:
- --pipeline_config_path: config 文件路径
- --continue_train: restore 之前的 checkpoint,继续训练
- --model_dir: 如果指定了 model_dir 将会覆盖 config 里面的 model_dir,一般在周期性调度的时候使用
- --edit_config_json: 使用 json 的方式对 config 的一些字段进行修改,如:
```bash
--edit_config_json='{"train_config.fine_tune_checkpoint": "experiments/ctr/model.ckpt-50"}'
```
- Extend Args: 命令行参数修改config, 类似edit_config_json
- 支持train_config.*, eval_config.*, data_config.*, feature_config.*
- Extend Args: 命令行参数修改 config, 类似 edit_config_json
- 支持 train*config.*, eval*config.*, data*config.*, feature*config.*
- 示例:
```bash
--train_config.fine_tune_checkpoint=experiments/ctr/model.ckpt-50
Expand All @@ -158,28 +162,28 @@ pai -name easy_rec_ext -project algo_public

- -Dtrain_tables: 训练表,可以指定多个,逗号分隔
- -Deval_tables: 评估表,可以指定多个,逗号分隔
- -Dcluster: 定义PS的数目和worker的数目,如果设置了--eval_method=separate,有一个worker将被用于做评估
- -Dcluster: 定义 PS 的数目和 worker 的数目,如果设置了--eval_method=separate,有一个 worker 将被用于做评估
- -Dconfig: 训练用的配置文件
- -Dcmd: train   模型训练
- -Deval_method: 训练时需要评估, 可选参数:
- separate: 有一个worker被单独用来做评估(不参与训练)
- separate: 有一个 worker 被单独用来做评估(不参与训练)
- none: 不需要评估
- master: 在master结点上做评估,master结点也参与训练
- -Darn: rolearn  注意这个的arn要替换成客户自己的。可以从dataworks的设置中查看arn
- -DossHost: ossHost地址
- -Dbuckets: config所在的bucket和保存模型的bucket; 如果有多个bucket,逗号分割
- master: 在 master 结点上做评估,master 结点也参与训练
- -Darn: rolearn   注意这个的 arn 要替换成客户自己的。可以从 dataworks 的设置中查看 arn
- -DossHost: ossHost 地址
- -Dbuckets: config 所在的 bucket 和保存模型的 bucket; 如果有多个 bucket,逗号分割
- -Dselected_cols 表里面用于训练和评估的列, 有助于提高训练速度
- -Dmodel_dir: 如果指定了model_dir将会覆盖config里面的model_dir,一般在周期性调度的时候使用。
- -Dedit_config_json: 使用json的方式对config的一些字段进行修改,如:
- -Dmodel_dir: 如果指定了 model_dir 将会覆盖 config 里面的 model_dir,一般在周期性调度的时候使用。
- -Dedit_config_json: 使用 json 的方式对 config 的一些字段进行修改,如:
```sql
-Dedit_config_json='{"train_config.fine_tune_checkpoint": "oss://easyrec/model.ckpt-50"}'
```
- 如果是pai内部版,则不需要指定arn和ossHost, arn和ossHost放在-Dbuckets里面
- 如果是 pai 内部版,则不需要指定 arn 和 ossHost, arn 和 ossHost 放在-Dbuckets 里面
- -Dbuckets=oss://easyrec/?role_arn=acs:ram::xxx:role/ev-ext-test-oss&host=oss-cn-beijing-internal.aliyuncs.com

### On DLC

- 基于Kubeflow的云原生的训练方式
- 基于 Kubeflow 的云原生的训练方式
- [参考文档](./quick_start/dlc_tutorial.md)

### On EMR
Expand All @@ -189,27 +193,27 @@ pai -name easy_rec_ext -project algo_public

## 混合并行(EmbeddingParallel)

混合并行模式下Embedding参数会分片, 均匀分布到各个worker上, 通过all2all的通信方式来聚合不同worker上的Embedding。MLP参数在每个worker上都有完整的一份复制, 在参数更新时,会通过allreduce的方式同步不同worker的更新
混合并行模式下 Embedding 参数会分片, 均匀分布到各个 worker 上, 通过 all2all 的通信方式来聚合不同 worker 上的 Embedding。MLP 参数在每个 worker 上都有完整的一份复制, 在参数更新时,会通过 allreduce 的方式同步不同 worker 的更新

### 依赖

- 混合并行使用Horovod做底层的通信, 因此需要安装Horovod, 可以直接使用下面的镜像
- 混合并行使用 Horovod 做底层的通信, 因此需要安装 Horovod, 可以直接使用下面的镜像
- mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/easyrec/easyrec:sok-tf212-gpus-v5
```
sudo docker run --gpus=all --privileged -v /home/easyrec/:/home/easyrec/ -ti mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/easyrec/easyrec:sok-tf212-gpus-v5 bash
```

### 配置

- 修改train_config.train_distribute为EmbeddingParallelStrategy
- 修改 train_config.train_distribute 为 EmbeddingParallelStrategy
```
train_config {
...
train_distribute: EmbeddingParallelStrategy
...
}
```
- 如使用key-Value Embedding, 需要设置model_config.ev_params
- 如使用 key-Value Embedding, 需要设置 model_config.ev_params
```
model_config {
...
Expand All @@ -229,7 +233,7 @@ pai -name easy_rec_ext -project algo_public
```
CUDA_VISIBLE_DEVICES=0 horovodrun -np 1 python -m easy_rec.python.eval --pipeline_config_path samples/model_config/dlrm_on_criteo_parquet_ep_v2.config
```
- 注意: 评估目前仅支持单个worker评估
- 注意: 评估目前仅支持单个 worker 评估
- 导出
```
CUDA_VISIBLE_DEVICES=0 horovodrun -np 1 python -m easy_rec.python.export --pipeline_config_path samples/model_config/dlrm_on_criteo_parquet_ep_v2.config --export_dir dlrm_criteo_export/
Expand Down