This section describes how to use high-level APIs to perform matrix multiplication. In cube programming, check the product models supported by the high-level APIs in API Reference.
Matmul formula: C = A × B + bias.
Before learning about the matrix multiplication data flow, you need to review several important concepts of logical storage locations.
Matrix multiplication data flow refers to the flow direction of the input and output of the matrix multiplication between storage locations. The following figure shows the data flow of logical locations. (To simplify the description, bias is not listed.)
Because A1 has a larger space than A2, data can be first moved from GM or VECOUT to A1 for buffering. Before Cube computation is performed on the data, the data is directly moved from A1 to A2. In this way, the waiting time before computation can be reduced when a large amount of data is moved, improving performance. The data flow of GM->A2 is used only when a small amount of data is moved.
Because B1 has a larger space than B2, data can be first moved from GM or VECOUT to B1 for buffering. Before Cube computation is performed on the data, the data is directly moved from B1 to B2. In this way, the waiting time before computation can be reduced when a large amount of data is moved, improving performance. The data flow of GM->B2 is used only when a small amount of data is moved.

Two fractal formats, that is, ND and NZ, are mainly involved in completing Matmul. For details about other data formats, see the data layout formats.
(..., N, H, W )->pad->(..., N, H1*H0, W1*W0)->reshape->(..., N, H1, H0, W1, W0)->transpose->(..., N, W1, H1, H0, W0)
As shown in the following figure, the (W, H) matrix is divided into (H1 × W1) fractals, which are arranged by column major, shaped as letter N. Each fractal has (H0 × W0) elements, which are arranged by row major, shaped as letter Z. Therefore, the data format is called NZ (large N small Z) format.

The following is an example to help you understand the differences between ND and NZ data layouts. Assume that the fractal format is 2 × 2, as the 4 × 4 matrix shown in the following figure. In the case of ND (1, 4, 4) and NZ (1, 2, 2, 2, 2) layouts, the data formats in the memory are as follows:
ND: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
NZ: 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15

For details about the example of converting the ND format to the NZ format, see Matmul operator sample for converting the ND format of the input matrix to the NZ format.
To implement multi-core parallelism, matrix data needs to be tiled and allocated across cores for processing. The tiling strategies are as follows:
As shown in the following figure, eight cores participate in the computation. Matrix A is tiled into four blocks along the M axis, and matrix B is tiled into two blocks along the N axis. A single core processes one block only (for example, the green part in the figure is the data computed on core3). The matrix A block with the size of singleCoreM × K is multiplied by the matrix B block with the size of singleCoreN × K to obtain the matrix C block with the size of singleCoreM × singleCoreN.

In addition, the length of the K axis processed on a single core is SingleCoreK. In scenarios where the value of the K-axis is large, the K axis can be tiled into multiple tiles of singleCoreK. For details, see Matmul High-Level API Enabling K-axis Tiling of Matrix Data in Multi-core Parallel Computation.
In most cases, the local memory cannot completely store the operator input and output. Therefore, you need to pass some inputs for multiple times until the complete final result is obtained. This process is intra-core tiling. The tiling strategies are as follows:

In addition to the basic block shapes baseM, baseN, and baseK, there are several common tiling parameters, which are described as follows:
In the preceding figure, iterateOrder is set to 0.