Matmul High-Level API Enabling IBShare Template for Sharing Matrix B Data

Case Study

This case shows the optimized operator performance when IBShare is enabled for matrix B in the matrix multiplication of the high-level Matmul API in the matrix multiplication operator scenario. IBShare shares the same matrix A or B data in the L1 buffer to reduce repeated MTE2 data movement overheads and improve operator performance. This function allows IBShare to be enabled for either matrix A or matrix B, or for both matrices A and B at the same time.

The operator specifications are as follows.

Table 1 Operator specifications

Input

Shape

Data Type

Format

a

64, 384

float16

ND

b

384, 256

float16

ND

On the AI processor used in the current case, there are 20 cores in total, and each core contains one AIC and two AIVs. Because the input shape is small, this case uses a single core as an example. You can refer to the usage of the SetDim API in MIX mode and set the number of cores involved in the computation to 2 in the tiling program. The tiling parameters are as follows:

Obtaining Profile Data

Use the msProf tool to obtain the Operator Simulation Pipeline and On-board Profiling data. IBShare is used to share the same matrix A or B data in the L1 Buffer to reduce the overhead of repeated MTE2 data transfer. Therefore, the focus is on analyzing the MTE2 pipeline.

Analyzing Main Bottlenecks

Developing Optimization Solutions

The following figure shows the Matmul computation pipeline when the IBShare template is disabled (the Norm template is used by default). MTE2 transfers base blocks from the global memory to A1 or B1 for multiple times. Even if the base block data of matrix B transferred in two consecutive times is the same, the data is still transferred repeatedly.

Figure 1 Matmul pipeline when the IBShare template is disabled

The following figure shows the Matmul computation pipeline when the IBShare template is enabled. MTE2 transfers base blocks from the global memory to A1 or B1 for multiple times. If the base block data of matrix B transferred in two consecutive times is the same, the data is not transferred repeatedly. Instead, the data transferred to B1 for the first time is reused.

Figure 2 Matmul pipeline when the IBShare template is enabled

For details about the complete example of enabling the IBShare template to share matrix B using the Matmul API, see sample of enabling IBShare only for matrix B. The procedure for enabling IBShare is as follows:

  1. Create a Matmul object.

    1
    2
    3
    4
    5
    6
    7
    8
    #define ASCENDC_CUBE_ONLY
    #include "lib/matmul_intf.h"
    
    using A_TYPE = AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, AType>;
    using B_TYPE = AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, BType, false, LayoutMode::NONE, true>; // Set the IBSHARE parameter of matrix B to true.
    using C_TYPE = AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, CType>;
    using BIAS_TYPE =  AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, BiasType>;
    AscendC::Matmul<A_TYPE, B_TYPE, C_TYPE, BIAS_TYPE, CFG_IBSHARE_NORM> MatmulObj; // Use the default IBShare template parameter CFG_IBSHARE_NORM to define the Matmul object.
    

Verifying Optimization Benefits

Summary

In the MIX scenario (including cube computation and vector computation), the GM addresses of matrix A or matrix B of multiple AIVs are the same, and the matrix A or matrix B reused by multiple AIVs are fully loaded on the L1 buffer. You can enable the IBShare template to share the same matrix A or B data in the L1 buffer, reducing repeated MTE2 data transfer overheads and improving operator performance.