mstx Extended Functions

Introduction to mstx APIs

The mstx APIs are performance analysis APIs provided by MindStudio. Users can insert specific tags into applications so that the key code area can be accurately located during performance analysis. For details, see Table 1 and Table 2. For details about how to use the APIs, see MindStudio mstx API Reference.

Table 1 C/C++ mstx API list

API

Description

msProf Support

mstxRangeStartA

Marks the start position of the mstx range capability.

Supported

mstxRangeEnd

Marks the end position of the mstx range capability.

Supported

Table 2 Python mstx API list

API

Description

msProf Support

mstx.range_start

Marks the start position of the mstx range capability.

Supported

mstx.range_end

Marks the end position of the mstx range capability.

Supported

Usage of mstx APIs

  • The msProf tool allows users to use the mstx APIs to tune specific operators, customize the start time and end time of the code segment or specified key functions, identify key functions or computing APIs, and quickly demarcate performance issues.
  • By default, the mstx APIs are disabled. If the mstx APIs are called in the application, the mstx dotting function is enabled based on the actual application scenario. For example, you can configure --mstx=on or --mstx-include to enable all mstx APIs or specific mstx APIs in the user program, respectively. For details, see the --mstx and --mstx-include parameters in Commands.
  • mstx provides two API usage modes: library file and header file. An example is shown in Link.
    • This example project does not support Atlas A3 Training Series Product.
    • Add library file libms_tools_ext.so to the ${git_clone_path}/samples/operator/ascendc/0_introduction/1_add_frameworklaunch/AclNNInvocation/src/CMakeLists.txt directory. The address is ${INSTALL_DIR}/lib64/libms_tools_ext.so.
      # Header path
      include_directories(
           ...
          ${CUST_PKG_PATH}/include
      )
      ...
      target_link_libraries( 
          ...
          dl
      )
    • In the ${git_clone_path}/samples/operator/ascendc/0_introduction/1_add_frameworklaunch/AclNNInvocation/src/main.cpp directory, compile and link the user program to the dl library. The corresponding header file ms_tools_ext.h is stored in ${INSTALL_DIR}/include/mstx.
      ...
      #include "mstx/ms_tools_ext.h"
      ...

      Replace ${INSTALL_DIR} with the CANN component directory. For example, if the installation is performed by the root user, the default file storage path is /usr/local/Ascend/cann.

Example

After msProf is started, run the msprof op --mstx=on --mstx-include=range1 --launch-count=2 python cal.py command. This command will profile the operators defined within the "range1" scope, specifically the sub and mul operators.

import mstx
import torch
import torch_npu
x = torch.Tensor([1,2,3,4]).npu()
y = torch.Tensor([1,2,3,4]).npu()
a = x + y
range1_id = mstx.range_start("range1", None)
b = a - x
c = a * x
mstx.range_end(range1_id)
f = x + e
range2_id = mstx.range_start("range2", None)
e = torch.abs(y)
mstx.range_end(range2_id)