Matmul High-Level API Enabling MTE2 Preload

Case Study

This case shows the optimized operator performance when MTE2 Preload is enabled in the matrix multiplication of the high-level Matmul API in the matrix multiplication operator scenario. The doMTE2Preload parameter in MatmulConfig is used to enable the preload function in the M or N direction of the matrix, which means that the data of matrix A or B is loaded in advance during the MTE2 interval. After the preload function is enabled, the MTE2 interval can be reduced, thereby improving operator performance. For details about the doMTE2Preload parameter, see MatmulConfig.

The operator specifications are as follows.

Table 1 Operator specifications

Input

Shape

Data Type

Format

a

128, 512

float16

ND

b

512, 24576

float16

ND

On the AI processor used in the current case, there are 24 cores in total, and the high-level API Matmul in CUBE_ONLY is enabled for the operator. Use the MDL template. The tiling parameters are as follows:

Obtaining Profile Data

Use the msProf tool to obtain the Operator Simulation Pipeline and On-board Profiling data, and focus on analyzing Cube and FixPipe pipelines.

Analyzing Main Bottlenecks

Developing Optimization Solutions

Enable MTE2 Preload: When creating a Matmul object, enable doMTE2Preload. For details about the sample of enabling MTE2 Preload, see Matmul operator sample for preloading in the M direction. The specific procedure is as follows:

  1. Set the doMTE2Preload parameter in the MDL template to 2 to enable the Preload function in the N direction.

    1
    2
     // preloadMode = 2
    static constexpr MatmulConfig MM_CFG = GetMDLConfig(false, false, preloadMode); 
    

  2. Create a Matmul object based on the customized MatmulConfig template parameters.

    1
    2
    3
    4
    AscendC::Matmul<AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, aType>,
        AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, bType>,
        AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, cType>,
        AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, biasType>, MM_CFG> matmulObj;
    

Verifying Optimization Benefits

Summary

When the MTE2 pipeline gap is large and the value of M or N is large, you can enable MTE2 Preload to load the data of matrix A or B in advance.