@@ -69,18 +69,18 @@ $ python3 setup.py install
6969
7070## 快速上手
7171
72- 在本节中,你将学习如何在一个分类数据集上微调[ BERT] ( https://arxiv.org/abs/1810.04805 ) 模型。
72+ 在本节中,你将学习如何在一个分类数据集上微调 [ BERT] ( https://arxiv.org/abs/1810.04805 ) 模型。
7373
74- ### 1. 初始化BMTrain后端
75- 首先,你需要在代码开头引入` bmtrain ` 并使用` bmtrain.init_distributed() ` 。
74+ ### 1. 初始化 BMTrain 后端
75+ 首先,你需要在代码开头引入 ` bmtrain ` 并使用 ` bmtrain.init_distributed() ` 。
7676
7777``` python
7878import bmtrain as bmt
7979bmt.init_distributed(seed = 0 )
8080```
8181
8282### 2. 准备模型
83- 接下来,你可以从` model_center ` 中获取预训练好的BERT模型 ,例如* bert-base-uncased* 。由于我们是在一个分类任务上微调BERT模型 ,所以需要在最后一层后添加一个全连接层。
83+ 接下来,你可以从 ` model_center ` 中获取预训练好的 BERT 模型 ,例如 * bert-base-uncased* 。由于我们是在一个分类任务上微调 BERT 模型 ,所以需要在最后一层后添加一个全连接层。
8484
8585``` python
8686import torch
@@ -104,7 +104,7 @@ model = BertModel(config)
104104```
105105
106106### 3. 准备数据集
107- 下一步是准备数据集,用于训练和验证模型。这里,我们使用[ SuperGLUE benchmark] ( https://super.gluebenchmark.com/ ) 中的 [ BoolQ] ( https://github.com/google-research-datasets/boolean-questions ) 数据集。你需要下载该数据集,并将解压后的文件夹放在` your_path_to_dataset ` 路径下。
107+ 下一步是准备数据集,用于训练和验证模型。这里,我们使用 [ SuperGLUE benchmark] ( https://super.gluebenchmark.com/ ) 中的 [ BoolQ] ( https://github.com/google-research-datasets/boolean-questions ) 数据集。你需要下载该数据集,并将解压后的文件夹放在 ` your_path_to_dataset ` 路径下。
108108
109109``` python
110110from model_center.dataset.bertdataset import DATASET
@@ -124,7 +124,7 @@ dev_dataloader = DistributedDataLoader(dataset['dev'], batch_size=batch_size, sh
124124```
125125
126126### 4. 训练模型
127- 现在,在设置优化器、学习率调整策略和损失函数后,就可以开始训练模型了!这里,我们训练BERT模型5轮 ,并且在每轮训练结束后验证模型的性能。
127+ 现在,在设置优化器、学习率调整策略和损失函数后,就可以开始训练模型了!这里,我们训练 BERT 模型5轮 ,并且在每轮训练结束后验证模型的性能。
128128
129129``` python
130130optimizer = bmt.optim.AdamOffloadOptimizer(model.parameters())
@@ -152,6 +152,9 @@ for epoch in range(5):
152152 # 计算损失
153153 loss = loss_func(logits.view(- 1 , logits.shape[- 1 ]), labels.view(- 1 ))
154154
155+ # 使用bmt.sum_loss(loss)聚合所有进程上的损失
156+ global_loss = bmt.sum_loss(loss).item()
157+
155158 # 缩放损失以避免fp16精度下溢
156159 loss = optimizer.loss_scale(loss)
157160
@@ -164,10 +167,9 @@ for epoch in range(5):
164167 bmt.optim_step(optimizer, lr_scheduler)
165168
166169 # 在分布式训练时,只在rank为0时打印信息
167- # 使用bmt.sum_loss(loss)聚合所有进程上的损失
168170 bmt.print_rank(
169171 " loss: {:.4f } | lr: {:.4e } , scale: {:10.4f } | grad_norm: {:.4f } |" .format(
170- bmt.sum_loss(loss).item() ,
172+ global_loss ,
171173 lr_scheduler.current_lr,
172174 int (optimizer.scale),
173175 grad_norm,
@@ -203,13 +205,13 @@ for epoch in range(5):
203205```
204206
205207### 5. 运行代码
206- 你可以使用PyTorch原生的分布式训练启动器来运行上述代码,根据你的PyTorch版本选择下列命令中的一个 。
208+ 你可以使用 PyTorch 原生的分布式训练启动器来运行上述代码,根据你的 PyTorch 版本选择下列命令中的一个 。
207209
208- * ` ${MASTER_ADDR} ` means the IP address of the master node.
209- * ` ${MASTER_PORT} ` means the port of the master node.
210- * ` ${NNODES} ` means the total number of nodes.
211- * ` ${GPU_PER_NODE} ` means the number of GPUs per node.
212- * ` ${NODE_RANK} ` means the rank of this node.
210+ * ` ${MASTER_ADDR} ` 为主节点的 IP 地址
211+ * ` ${MASTER_PORT} ` 为主节点的端口
212+ * ` ${NNODES} ` 为节点数量
213+ * ` ${GPU_PER_NODE} ` 为每个节点的 GPU 数量
214+ * ` ${NODE_RANK} ` 为本节点的 rank
213215
214216#### torch.distributed.launch
215217``` shell
@@ -222,7 +224,7 @@ $ python3 -m torch.distributed.launch --master_addr ${MASTER_ADDR} --master_port
222224$ torchrun --nnodes=${NNODES} --nproc_per_node=${GPU_PER_NODE} --rdzv_id=1 --rdzv_backend=c10d --rdzv_endpoint=${MASTER_ADDR} :${MASTER_PORT} train.py
223225```
224226
225- 更多信息请参考PyTorch [ 官方文档] ( https://pytorch.org/docs/stable/distributed.html#launch-utility ) 。
227+ 更多信息请参考 PyTorch [ 官方文档] ( https://pytorch.org/docs/stable/distributed.html#launch-utility ) 。
226228
227229
228230## 模型支持
@@ -273,7 +275,7 @@ $ torchrun --nnodes=${NNODES} --nproc_per_node=${GPU_PER_NODE} --rdzv_id=1 --rdz
273275
274276您也可以在其他平台与我们沟通交流:
275277- QQ群: 735930538
276- - 官方网站: http ://www.openbmb.org
278+ - 官方网站: https ://www.openbmb.org
277279- 微博: http://weibo.cn/OpenBMB
278280- Twitter: https://twitter.com/OpenBMB
279281
0 commit comments