
# advisor调优总体步骤
基于ModelArts performance advisor插件的昇腾PyTorch性能调优主要分为以下步骤：
1. [准确采集性能劣化时刻的profiling数据]。
2. [存储profiling数据]。
3. [创建advisor分析环境]。
#### 操作步骤
1. 明确性能问题类型，准确采集性能劣化时刻的profiling数据。
   
   1. 对于固定step出现性能劣化，如固定在16步出现性能劣化，则需要合理配置profiling参数，确保能采集到16步的数据。
   
   2. 对于所有step稳定劣化的场景，避免采集第一个step的profiling即可，可以采集任意step如第15个step即可。
   
   3. 对于偶现且劣化现象出现的step数不固定的场景，则需要确保能采集到该不固定的step。
   
   
   profiling数据采集请参考[Ascend PyTorch Profiler接口采集](https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/80RC3alpha003/devaids/auxiliarydevtool/atlasprofiling_16_0033.html)。文档中包含**torch_npu.profiler.profile、dynamic_profile** 等多种采集方式。任意torch_npu版本均支持**torch_npu.profiler.profile** 方式，而其他采集方式则要求特定版本的torch_npu（2024年0630之后版本）。推荐升级torch_npu后使用**dynamic_profile** 方式进行采集，如果升级成本过高，也可以使用**torch_npu.profiler.profile**。
   ![](https://support.huaweicloud.com/bestpractice-modelarts/public_sys-resources/caution_3.0-zh-cn.png)
   当不明确性能劣化的可能原因时，profiling关键参数配置请务必复用如下设置：
   ```
   # torch_npu.profiler._ExperimentalConfig
   aic_metrics=torch_npu.profiler.AiCMetrics.PipeUtilization
   profiler_level=torch_npu.profiler.ProfilerLevel.Level1
   data_simplification=True
   # torch_npu.profiler.profile
   activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU]
   with_stack=False
   ```
   - **torch_npu.profiler.profile** 采集方式介绍
     配置完如[图1]所示代码后需要启动训练作业触发采集且只能采集指定的step，对于已经明确需要采集step的场景可以使用该采集方式，此时需要重点关注[图1]中schedule参数以确保采集到需要的step数据。对于schedule参数，请参考[图2]。skip_first用于跳过指定的前n个step。wait、warmup和active三个参数构成了一次完整的采集，repeat参数表示重复多少次完整的采集。wait参数表示重复执行采集过程中每次采集跳过的step轮数，warmup表示预热的step轮数（推荐设置为1），active表示实际采集的连续m个step。
     参数示例：
     ```
     skip_first=10，wait=3, warmup=1, active=3, repeat=2
     ```
     采集时会先跳过前10个step（从step0开始）。然后完整的一次采集过程包括等待3个step，预热1个step和连续采集3个step（step14-step16）的profiling数据。这一次采集的step14-step16的profiling数据会保存在同一个json文件中。由于设置了repeat=2，则会再次重复一次采集过程，那么实际会二次采集step21-step23这三个连续step的profiling数据并保存至一个新的json文件中。
     图1torch_npu.profiler.profile   
     ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002125263545.png "点击放大")
     图2torch_npu.profiler.profile schedule参数释义   
     ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002125263549.png "点击放大") 
   
   - **dynamic_profile** 采集方式介绍
     对于上述提到的性能劣化且出现step不固定的场景，优先考虑使用动态profiling方式进行采集。如[图3]中所示"if step==5"处，需要在业务代码中添加如下判断逻辑：记录每一个step的耗时，如果某个step的耗时出现异常，即大于正常step耗时或者均值耗时的20%（根据训练日志的实际step耗时来确定异常耗时阈值），则认为出现性能劣化，从而执行'dp.start'触发profiling采集。
     图3**dynamic_profile**   
     ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002089703998.png "点击放大") 
   
   
   
   
2. 存储profiling数据。
   
   多机场景如果没有挂载共享存储如SFS Turbo，需要将多机上的profiling复制至同一个目录下才能进行性能分析，这个操作相对较为繁琐且耗时。使用ModelArts时推荐挂载共享网盘如sfs turbo，既能加快训练数据的读取速度又能用于存放性能profiling数据。如果没有共享网盘，profiling数据默认保存到ModelArts训练容器中，则请参考[创建ModelArts训练作业](https://support.huaweicloud.com/develop-modelarts/develop-modelarts-0022.html)中的配置训练参数部分配置好输出参数，训练过程中会自动将训练容器中输出路径下的数据回传至指定的OBS上。
   
   
3. 创建performance advisor分析环境。
   
   采集完profiling后如果ModelArts训练作业已经停止，则推荐参照[创建诊断任务](https://support.huaweicloud.com/bestpractice-modelarts/modelarts_advisor_2001.html)创建cpu规格的notebook进行性能分析，节省NPU计算资源。完成分析后，可以查看生成的html文件来进行快速的调优，html文件详情请参考[查看诊断报告](https://support.huaweicloud.com/bestpractice-modelarts/modelarts_advisor_2003.html)。
   下面以开发环境Notebook为例介绍一个典型的性能调优案例。
   64卡训练任务，模型为GPT MOE，tensor parallel(tp)为8，pipeline parallel(pp)为4。训练过程中发现每个step耗时均显著增大，基于**dynamic_profile** 方式采集profiling并上传至OBS。选择任意镜像如PyTorch，创建一个2U8GB CPU规格（如果CPU资源充足，建议创建8U32G的分析环境）的notebook开发环境。在notebook中使用performance advisor插件进行性能分析，源数据选择OBS并指定profiling所在的OBS路径（仅支持OBS并行文件系统）。由于pp参数为4，考虑到不同pp stage的计算量存在差异，advisor会自动对不同stage进行计算维度的分析，因此在Advanced Setting中设置分析进程为2（不建议设置太大，避免占用过多CPU资源导致OOM类问题）使能并行分析，加快分析速度，如下[图4]所示。
   图4基于performance advisor进行性能劣化分析   
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002125383265.png "点击放大")
   完成分析后单击下图[图5]中view查看报告。html（[图6]）中显示计算维度存在高优先级的AICORE降频问题，分别为pp stage0的8号卡和pp stage3的60号卡。查看对8号卡的降频分析（[图7]）可以发现节点降频主要影响了FlashAttention和MatMul两类算子，导致这两类算子的计算性能劣化，从而影响了整体的训练性能。按照html中给出的建议，需要检查8号卡和60号卡对应节点的温度和最大功率。
   图5性能分析报告展示   
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002089704002.png "点击放大")
   图6计算维度节点降频问题   
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002089863882.png "点击放大")
   图7节点降频及其影响算子   
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002089863878.png "点击放大")
   
   
 
