性能数据采集
前提条件
完成模型开发&迁移,得到可正常执行训练任务的GPU和NPU环境。

在执行性能数据采集前请先将训练脚本(main.py文件)中的精度数据采集接口删除,因为精度数据采集和性能数据采集不可同时执行。
执行采集
- 在NPU环境下的训练脚本(main.py文件)中添加Ascend PyTorch Profiler接口工具,如下所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
23 24 import torch_npu 25 from torch_npu.contrib import transfer_to_npu 26 ... 322 experimental_config = torch_npu.profiler._ExperimentalConfig( 323 profiler_level=torch_npu.profiler.ProfilerLevel.Level0, 324 data_simplification=False) 325 with torch_npu.profiler.profile( 326 activities=[ 327 torch_npu.profiler.ProfilerActivity.CPU, 328 torch_npu.profiler.ProfilerActivity.NPU 329 ], 330 schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1, skip_first=1), 331 on_trace_ready=torch_npu.profiler.tensorboard_trace_handler("./profiling_data"), 332 experimental_config=experimental_config) as prof: 333 for i, (images, target) in enumerate(train_loader): 334 # measure data loading time 335 data_time.update(time.time() - end) 336 337 # move data to the same device as model 338 images = images.to(device, non_blocking=True) 339 target = target.to(device, non_blocking=True) 340 341 # compute output 342 output = model(images) 343 loss = criterion(output, target) 344 345 # measure accuracy and record loss 346 acc1, acc5 = accuracy(output, target, topk=(1, 5)) 347 losses.update(loss.item(), images.size(0)) 348 top1.update(acc1[0], images.size(0)) 349 top5.update(acc5[0], images.size(0)) 350 351 # compute gradient and do SGD step 352 optimizer.zero_grad() 353 loss.backward() 354 optimizer.step() 355 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工具进行辅助分析,详细操作请参见使用MindStudio Insight工具可视化性能数据和使用msprof-analyze工具分析性能数据。
父主题: 模型性能调优