Matmul High-Level API Enabling UnitFlag

Case Study

This case shows the optimized operator performance when UnitFlag is enabled in the matrix multiplication of the high-level Matmul API in the matrix multiplication operator scenario. UnitFlag provides fine-grained synchronization based on memory access for the MMAD computation instructions and FIXPIPE data transfer instructions in AICs, enabling parallel execution of computation and transfer. To enable UnitFlag, set the enUnitFlag parameter in MatmulConfig to true. For details about the enUnitFlag parameter, see MatmulConfig.

The operator specifications are as follows.

Table 1 Operator specifications

Input

Shape

Data Type

Format

a

128, 64

float16

ND

b

64, 30720

float16

ND

The AI Processor used in this case has 20 cores, each of which contains one AIC and two AIVs.

The tiling parameters of the operator are as follows:

Obtaining Profile Data

Use the msProf tool to obtain the Operator Simulation Pipeline and On-board Profiling data. UnitFlag is mainly used to optimize the serial execution of MMAD and FIXPIPE pipelines. Therefore, after obtaining the profile data, the focus is on analyzing Cube and FIXPIPE pipelines.

Analyzing Main Bottlenecks

Developing Optimization Solutions

As shown in the following figure, when UnitFlag is disabled, MMAD and FIXPIPE are synchronized at the instruction level. The FIXPIPE instruction transfers the result only after the MMAD instruction is executed. MMAD and FIXPIPE pipelines are executed in serial mode.

Figure 1 UnitFlag disabled

As shown in the following figure, when UnitFlag is enabled, MMAD and FIXPIPE instructions are synchronized at a fine granularity of 512 bytes. During the execution of an MMAD instruction, each time the computation of a 512-byte data result is complete, FIXPIPE immediately starts to transfer the 512-byte data out. In this way, the MMAD and FIXPIPE pipelines are executed in parallel, improving operator performance.

Figure 2 UnitFlag enabled

For details about the complete example of enabling UnitFlag by using the Matmul API, see Matmul API performance optimization sample. The procedure for enabling UnitFlag is as follows:

  1. Customize MatmulConfig template parameters and set enUnitFlag to true to enable UnitFlag.

    1
    2
    3
    4
    5
    6
    7
    __aicore__ inline constexpr MatmulConfig GetCustomMDLCFG()
    {
        auto mmCfg = CFG_MDL;
        mmCfg.enUnitFlag = true;
        return mmCfg;
    }
    constexpr static MatmulConfig CUSTOM_CFG_MDL = GetCustomMDLCFG();
    

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

    1
    2
    3
    4
    5
    using A_TYPE = AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, AType>;
    using B_TYPE = AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, BType>;
    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, CUSTOM_CFG_MDL > matmulObj;
    

Verifying Optimization Benefits

Summary

When the MMAD computation pipeline and FIXPIPE data movement pipeline of an operator are executed in serial mode and are not overlapped by other pipelines (such as MTE2 Bound), consider enabling UnitFlag to implement parallel execution of the MMAD computation pipeline and FIXPIPE data movement pipeline, improving operator performance.