更新时间:2024-06-07 GMT+08:00
分享

性能调优

算子优化

为了更好地发挥昇腾设备的性能,将ChatGLM-6B原模型中的部分算子替换成了NPU亲和的算子,修改的是modeling_chatglm.py文件,下图通过对比列举了对应的修改方式,图示中左边为原始方式,右边为修改后的方式。

  1. 使用torch.bmm替换torch.baddbmm。
    图1 torch.bmm替换

    因为toch.baddbmm函数中beta=0.0、alpha=1.0,所以是等价替换。

  2. npu_scaled_masked_softmax亲和api替换。
    图2 亲和api替换
  3. 连续性转换。
    图3 连续性转换
  4. 数组切片操作改用torch接口方式。
    图4 数组切片操作修改1
    图5 数组切片操作修改2
  5. gelu小算子使用torch的fast_gelu()、gelu()融合算子替换。
    图6 融合算子替换

profiling数据采集

在本例chatglm-6B中,添加profiling接口入口在ptuning/trainer.py的_inner_training_loop()下。具体采集方式参考Ascend PyTorch Porfiler数据采集与分析方式

调优结果

这里对deepspeed单机8卡环境下,调优之前和调优之后的train metrics做了统计,结果如下。

性能基线:

***** train metrics *****
epoch                    =       0.06
train_loss               =     3.0146
train_runtime            = 2:15:20.44
train_samples            =    1649399
train_samples_per_second =      12.61
train_steps_per_second   =      0.012

算子调优后结果:

***** train metrics *****
epoch                    =       0.06
train_loss               =     3.0128
train_runtime            = 1:39:41.32
train_samples            =    1649399
train_samples_per_second =      17.12
train_steps_per_second   =      0.017
分享:

    相关文档

    相关产品