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 MMAD pipeline and FIXPIPE pipeline of the operator are executed in serial mode. The FIXPIPE pipeline moves out the result only after the MMAD computation is complete. The instruction synchronization time accounts for a large proportion of the overall operator execution time. In this scenario, you can enable UnitFlag to obtain the performance benefits of parallel execution of the MMAD and FIXPIPE pipelines. If the original MMAD and FIXPIPE pipelines of the operator can be overlapped by other pipelines (for example, MTE2 Bound), enabling UnitFlag will bring only a small overall benefit.
The operator specifications are as follows.
|
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:
For matrix B, the tiling is performed along the N axis, resulting in 20 single-core tiles (singleCoreN). A single core processes K × singleCoreN data. For matrix A, the M axis is not tiled, that is, singleCoreM = M. A single core processes singleCoreM × K data. A total of 20 cores are involved in the computation.
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.
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.
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.
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 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(); |
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; |

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.