---
title: MSPTI调优工具
description: "MSPTI调优工具（MSPTI，MindStudio Profiling Tool Interface）是MindStudio针对Ascend设备提出的一套Profiling API，用户可以通过MSPTI构建针对NPU应用程序的工具，用于分析应用程序的性能。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/devaids/Profiling/atlasprofiling_16_0031.html
sourcePath: /source/zh/canncommercial/900/devaids/Profiling/atlasprofiling_16_0031.html
indexId: 7632d6d7daed5bf94e69081b84e9a3fbda534c58dc8b826436b14f90965fbe7c75
---
# MSPTI调优工具

#### 简介

MSPTI调优工具（MSPTI，MindStudio Profiling Tool Interface）是MindStudio针对Ascend设备提出的一套Profiling API，用户可以通过MSPTI构建针对NPU应用程序的工具，用于分析应用程序的性能。

MSPTI为通用场景接口，使用MSPTI API开发的Profiling分析工具可以在各种框架的推理训练场景生效。

MSPTI主要包括以下功能：

- Tracing：在MSPTI中Tracing是指CANN应用程序执行启动CANN活动的时间戳和附加信息的收集，如CANN API、Kernel、内存拷贝等。通过了解程序运行耗时，识别CANN代码的性能问题。可以使用Activity API和Callback API收集Tracing信息。
- Profiling：在MSPTI中Profiling是指单独收集一个或一组Kernel的NPU性能指标。

MSPTI当前提供使用C开发的一套C API(https://gitcode.com/Ascend/mspti/blob/26.0.0/docs/zh/c_api/README.md)以及将C API的功能作为底层逻辑封装的一套Python API(https://gitcode.com/Ascend/mspti/blob/26.0.0/docs/zh/python_api/README.md)。


#### 产品支持情况

AI处理器与昇腾产品的对应关系，请参见《昇腾产品形态说明(https://www.hiascend.com/document/detail/zh/AscendFAQ/ProduTech/productform/hardwaredesc_0001.html)》。


| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | √ |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | √ |
| Atlas 推理系列产品 | x |
| Atlas 训练系列产品 | x |


#### 使用前准备

msPTI Python API部分的样例依赖于PyTorch框架和torch_npu插件，请确保安装。参见《Ascend Extension for PyTorch(https://gitcode.com/Ascend/pytorch/blob/v2.7.1-7.3.0/docs/zh/installation_guide/installing_PyTorch.md)》中的“安装PyTorch”。


#### 约束

MSPTI工具不可与任何其他性能数据采集工具同时使用，否则会导致采集的数据丢失。


#### msPTI样例集

本节提供msPTI各种接口的使用样例，供用户理解使用msPTI接口。

构建样例执行

1. 安装CANN软件后，需要以CANN运行用户登录环境，执行source /${install_path}/set_env.sh命令设置环境变量。其中${install_path}为CANN软件安装后文件存储路径，例如：/usr/local/Ascend/cann。 示例如下：

```
source /usr/local/Ascend/cann/set_env.sh
```


2. 进入样例目录。
  msPTI样例代码集成在CANN Toolkit开发套件包中，路径为${install_path}/tools/mspti/samples。

  ${install_path}请替换为CANN软件安装后文件存储路径。以root安装举例，则安装后文件存储路径为：/usr/local/Ascend/cann。

  示例如下：

```
cd ${install_path}/tools/mspti/samples/callback_domain
```


3. 执行对应样例目录下的sample_run.sh。

```
bash sample_run.sh
```


下表为当前提供的样例介绍：

- Callback API| 样例 | 说明 |
| --- | --- |
| callback\_domain(https://gitcode.com/Ascend/mspti/tree/master/samples/callback\_domain) | 展示Callback API功能，可以通过msptiEnableDomain，在runtime API的前后执行Callback操作。 |
| callback\_mstx(https://gitcode.com/Ascend/mspti/tree/master/samples/callback\_mstx) | 展示Callback与mstx接口相结合功能，使用Callback API和mstx打点功能，在runtime的Launch Kernel前后打点，采集算子数据。 演示Callback中userdata用法，用户可以通过userdata透传配置或者部分运行参数。 |


- Activity API| 样例 | 说明 |
| --- | --- |
| mspti\_activity(https://gitcode.com/Ascend/mspti/tree/master/samples/mspti\_activity) | 展示Activity API接口的基本功能，样例展示如何采集Kernel和Memory等数据。 演示Activity API的基本运行，讲述Activity API的基本使用，包括Activity Buffer内存分配，Buffer消费等逻辑。 |
| mspti\_correlation(https://gitcode.com/Ascend/mspti/tree/master/samples/mspti\_correlation) | 展示Activity API接口的基本功能，展示如何通过correlationId字段将API和Kernel数据做关联。 演示runtime API下发与Kernel实际执行数据的关联，关联后可以将算子的下发和执行一一对应，方便分析性能瓶颈。 |
| mspti\_external\_correlation(https://gitcode.com/Ascend/mspti/tree/master/samples/mspti\_external\_correlation) | 展示msPTI External Correlation功能。 演示msptiActivityPopExternalCorrelationId和msptiActivityPushExternalCorrelationId两接口使用方法，用户可以通过接口将各种API关联到一起，方便回溯函数的调用栈。 |
| mspti\_hccl\_activity(https://gitcode.com/Ascend/mspti/tree/master/samples/mspti\_hccl\_activity) | 展示Activity API接口的基本功能，样例展示如何通过Hccl开关采集通信数据。 |
| mspti\_mstx\_activity\_domain(https://gitcode.com/Ascend/mspti/tree/master/samples/mspti\_mstx\_activity\_domain) | 展示msPTI控制mstxDomain功能，通过开关控制打点数据是否采集。 用户可以通过msPTI开关实时开关采集打点，减小性能损耗。 |


- Python API| 样例 | 说明 |
| --- | --- |
| python\_monitor(https://gitcode.com/Ascend/mspti/tree/master/samples/python\_monitor) | 展示Monitor基本使用方式，通过KernelMonitor、HcclMonitor获取计算算子和通信算子的耗时。 |
| python\_mstx\_monitor(https://gitcode.com/Ascend/mspti/tree/master/samples/python\_mstx\_monitor) | 展示MstxMonitor基本使用方式，用户可以通过Mstx打点采集对应算子（如matmul）耗时。 |
