MindSpore训练场景
前提条件
根据模型开发&迁移中的MindSpore部分,完成MindSpore场景的训练任务。

在执行性能数据采集前请先将训练脚本(mindspore_main.py文件)中的MindSpore训练场景精度数据采集相关接口删除,因为精度数据采集和性能数据采集不可同时执行。
执行采集
采集动作主要以MindSpore 2.7.0版本为例,若需要进行性能比对操作,也可以再采集MindSpore 2.6.0版本的性能数据。
- 在昇腾NPU环境下的训练脚本(mindspore_main.py文件)中添加MindSpore Profiler接口工具,如下所示。完整示例代码请参见MindSpore 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
... from mindspore.profiler import ProfilerLevel, ProfilerActivity, AicoreMetrics ... if __name__ == "__main__": mindspore.set_context(mode=mindspore.PYNATIVE_MODE) mindspore.set_device("Ascend") # Init Profiler # pylint: disable=protected-access experimental_config = mindspore.profiler._ExperimentalConfig( profiler_level=ProfilerLevel.Level0, aic_metrics=AicoreMetrics.AiCoreNone, l2_cache=False, mstx=False, data_simplification=False, ) step = 0 # Note that the Profiler should be initialized before model.train with mindspore.profiler.profile( activities=[ProfilerActivity.CPU, ProfilerActivity.NPU], schedule=mindspore.profiler.schedule( wait=0, warmup=0, active=1, repeat=1, skip_first=0 ), on_trace_ready=mindspore.profiler.tensorboard_trace_handler("./profiling_data"), profile_memory=False, experimental_config=experimental_config, ) as prof: # Train Model for data, label in ds.GeneratorDataset(generator_net(), ["data", "label"]): train_step(data, label) print(f"train step {step}") step += 1 prof.step() print("train finish")
- 执行训练脚本命令,工具会采集模型训练过程中的性能数据。
python mindspore_main.py
- 查看采集到的MindSpore训练性能数据结果文件。
训练结束后,在mindspore.profiler.tensorboard_trace_handler接口指定的目录下生成MindSpore 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
└── msprof_1198203_20250617064811905_ascend_ms ├── ASCEND_PROFILER_OUTPUT │ ├── api_statistic.csv │ ├── dataset.csv │ ├── kernel_details.csv │ ├── step_trace_time.csv │ └── trace_view.json ├── FRAMEWORK ... ├── PROF_000001_20250617064812088_QGGJHHNAODRDOJFB │ ├── device_0 │ │ ├── data ... │ ├── host │ │ ├── data ... │ ├── mindstudio_profiler_log ... │ └── mindstudio_profiler_output │ ├── api_statistic_20250617064814.csv │ ├── msprof_20250617064813.json │ ├── op_summary_20250617064814.csv │ ├── README.txt │ └── task_time_20250617064814.csv └── profiler_info_0.json
MindSpore Profiler接口采集的性能数据可以使用mstt的msprof-analyze工具进行辅助分析,也可以直接使用MindStudio Insight工具进行可视化分析,详细操作请参见使用msprof-analyze工具分析性能数据和使用MindStudio Insight工具可视化性能数据。
父主题: 性能数据采集