00 引言
GPU迁移到昇腾的大模型,或是基于昇腾开发的大模型在训练过程中除了会遇到精度问题外,性能也是非常常见的问题。
文本将主要针对昇腾环境下的PyTorch训练场景,推荐通过Ascend PyTorch Profiler接口工具采集并解析性能数据,使用mstt的msprof-analyze工具统计、分析以及输出相关的调优建议,使用MindStudio insight工具对性能数据进行可视化展示。
02 准备工作
完成模型开发&迁移后,得到可正常执行训练任务的GPU和NPU环境。安装msprof-analyze,命令如下:
pip3 install msprof-analyze
提示如下信息则表示安装成功。
Successfully installed msprof-analyze-{version}
03 性能数据采集
执行采集
- 分别在GPU和NPU环境下的训练脚本(main.py文件)中添加Ascend PyTorch Profiler接口工具,如下所示。
23
24 import torch_npu
25 from torch_npu.contrib import transfer_to_npu
26
...
322 experimental_config = torch_npu.profiler._ExperimentalConfig(
323 export_type=torch_npu.profiler.ExportType.Text,
324 profiler_level=torch_npu.profiler.ProfilerLevel.Level1,
325 msprof_tx=False,
326 aic_metrics=torch_npu.profiler.AiCMetrics.AiCoreNone,
327 l2_cache=False,
328 op_attr=False,
329 data_simplification=False,
330 record_op_args=False,
331 gc_detect_threshold=None)
332 with torch_npu.profiler.profile(
333 activities=[
334 torch_npu.profiler.ProfilerActivity.CPU,
335 torch_npu.profiler.ProfilerActivity.NPU
336 ],
337 schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1, skip_first=1),
338 on_trace_ready=torch_npu.profiler.tensorboard_trace_handler("./profiling_data"),
339 record_shapes=False,
340 profile_memory=False,
341 with_stack=False,
342 with_modules=False,
343 with_flops=False,
344 experimental_config=experimental_config) as prof:
345 for i, (images, target) in enumerate(train_loader):
346 # measure data loading time
347 data_time.update(time.time() - end)
348
349 # move data to the same device as model
350 images = images.to(device, non_blocking=True)
351 target = target.to(device, non_blocking=True)
352
353 # compute output
354 output = model(images)
355 loss = criterion(output, target)
356
357 # measure accuracy and record loss
358 acc1, acc5 = accuracy(output, target, topk=(1, 5))
359 losses.update(loss.item(), images.size(0))
360 top1.update(acc1[0], images.size(0))
361 top5.update(acc5[0], images.size(0))
362
363 # compute gradient and do SGD step
364 optimizer.zero_grad()
365 loss.backward()
366 optimizer.step()
367 prof.step()
...
- 执行训练脚本命令,工具会将模型训练过程中的性能数据采集下来。
python main.py -a resnet50 -b 32 --gpu 1 --dummy
结果查看
查看采集到的PyTorch训练性能数据结果文件。
训练结束后,在torch_npu.profiler.tensorboard_trace_handler接口指定的目录下生成Ascend PyTorch Profiler接口的采集结果目录,如下示例。
└── localhost-247.localdomain_2201189_20241114070751139_ascend_pt
├── ASCEND_PROFILER_OUTPUT
│ ├── api_statistic.csv
│ ├── kernel_details.csv
│ ├── operator_details.csv
│ ├── op_statistic.csv
│ ├── step_trace_time.csv
│ └── trace_view.json
├── FRAMEWORK
...
├── PROF_000001_20241114151021952_PGRJNNCFAIJQMERA
│ ├── device_1
│ │ ├── data
...
│ ├── host
│ │ ├── data
...
│ ├── mindstudio_profiler_log
...
│ └── mindstudio_profiler_output
│ ├── api_statistic_20241114151110.csv
│ ├── msprof_20241114151108.json
│ ├── op_statistic_20241114151110.csv
│ ├── op_summary_20241114151110.csv
│ ├── prof_rule_1_20241114151110.json
│ ├── README.txt
│ └── task_time_20241114151110.csv
└── profiler_info.json
Ascend PyTorch Profiler接口采集的性能数据建议使用MindStudio Insight工具进行可视化分析,也可以使用mstt的msprof-analyze工具进行辅助分析。
04 使用msprof-analyze工具分析性能数据
执行advisor分析
msprof-analyze的advisor功能是将Ascend PyTorch Profiler采集的性能数据进行分析,并输出性能调优建议,命令如下:
msprof-analyze advisor all -d $HOME/profiling_data/
分析结果输出相关简略建议到执行终端中,并在命令执行目录下生成mstt_advisor_{timestamp}.html和mstt_advisor_{timestamp}.xlsx文件供用户查看。
advisor工具的分析结果主要提供可能存在性能问题的专家建议,示例如下:
执行compare_tools性能比对
compare_tools功能用于在PyTorch训练工程从GPU迁移至NPU后出现性能劣化或者npu性能数据彼此有差距,通过工具分析出劣化点,操作如下:
- 将GPU环境下的性能数据拷贝到NPU环境。
- 执行性能比对操作。
msprof-analyze compare -d $HOME/profiling_data/ -bp HOME/gpu/profiling_data/ --output_path=./compare_result/profiler_compare
分析结果输出到执行终端中,并在命令执行目录下生成performance_comparison_result_{timestamp}.xlsx文件供用户查看。
性能比对工具将总体性能拆解为训练耗时和内存占用,其中训练耗时可拆分为算子(包括算子和nn.Module)、通信、调度三个维度,以打屏的形式输出总体指标,帮助用户定界劣化的方向。与此同时,工具还会生成performance_comparison_result_{timestamp}.xlsx,展示每个算子在执行耗时、通信耗时、内存占用的优劣,可通过DIFF列大于0筛选出劣化算子。
05 使用MindStudio Insight工具可视化性能数据
安装MindStudio Insight。
MindStudio Insight为可视化工具推荐使用Windows环境。- 下载工具安装包MindStudio-Insight_{version}_win.exe。
- 双击MindStudio-Insight_{version}_win.exe安装包,开始安装MindStudio Insight。
根据提示安装即可。
- 双击桌面的MindStudio Insight快捷方式图标,启动MindStudio Insight。
导入性能数据。
- 前置步骤中采集的性能数据拷贝至Windows环境。
- 单击MindStudio Insight界面左上方“Import
Data”,在弹框中选择性能数据文件或目录,然后单击“确认”进行导入,如下所示。
- 分析性能数据。
MindStudio Insight工具将性能数据可视化后可以更直观地分析性能瓶颈,详细分析方法请参见《MindStudio Insight 用户指南》。
05 总结
通过本文的详细介绍,我们了解了在昇腾环境下使用PyTorch进行大模型训练时如何应对性能挑战。首先,我们强调了迁移或开发基于昇腾的大模型时可能遇到的精度和性能问题,并推荐使用Ascend PyTorch Profiler接口工具来采集性能数据。接着,详细说明了如何安装并使用msprof-analyze工具进行性能数据的统计、分析以及获取调优建议。此外,还介绍了MindStudio Insight工具的应用,它能够帮助用户以更直观的方式查看和理解性能瓶颈所在。整个流程包括准备工作、性能数据采集、数据分析与可视化展示,旨在为用户提供一套完整的解决方案,助力于提高模型训练效率和效果。通过合理利用这些工具和技术,开发者可以有效地优化其模型在昇腾平台上的表现,实现更加高效、准确的大规模模型训练。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。