Basic Functions of Batch Matmul

Overview

Batch Matmul refers to the scenario where Matmul compute is processed in batches. In this scenario, the IterateBatch API is provided externally. You can call IterateBatch once to compute multiple matrices C with the size of singleCoreM × singleCoreN.

Data needs to be moved in and out during a single Matmul compute. If multiple Matmul compute operations are performed and the input shape in a single Matmul compute operation is small, the movement overhead accounts for a large proportion of the total duration. Using the IterateBatch API to process Matmul in batches can effectively improve bandwidth utilization.

Currently, Batch Matmul supports four layout types: BSNGD, SBNGD, BNGS1S2, and NORMAL (BMNK data format). For details about the data format, see IterateBatch.

The following figure shows the Batch Matmul compute in NORMAL data format. The entire Matmul compute involves four matrix multiplication operations: mat_a1 × mat_b1, mat_a2 × mat_b2, mat_a3 × mat_b3, and mat_a4 × mat_b4. Four singleCoreM × singleCoreN operations need to be computed on a single core. In this scenario, if the shape is small, you can consider it as a Batch Matmul scenario for batch processing to improve performance. mat_c1 = mat_a1 × mat_b1, mat_c2 = mat_a2 × mat_b2, mat_c3 = mat_a3 × mat_b3, and mat_c4 = mat_a4 × mat_b4 can be computed concurrently when IterateBatch is called once.

Figure 1 Batch Matmul in NORMAL format

Application Scenarios

During Matmul compute, multiple matrices C with the size of singleCoreM × singleCoreN need to be computed, and the shape processed by a single Matmul compute operation is small.

Constraints

Example

The following is an example of calling Batch Matmul in NORMAL format. For details about the complete example of Batch Matmul in BSNDG format, see BatchMatmul sample.