N-Direction Alignment of Matrix Multiplication Outputs

Overview

N-direction alignment of matrix multiplication outputs means that the matrix multiplication result, which is matrix C, is output in ND_ALIGN format. In Matmul matrix multiplication, the common matrix data formats are ND and NZ. For details, see Data Format. ND_ALIGN is another data format of a matrix. This format is generally used in matrix multiplication where the N direction is not 32-byte aligned. After the result matrix C is configured to be in ND_ALIGN format, matrix C is output according to the padding rules for 32-byte alignment in the N direction. For details, see ND_ALIGN.

The ND_ALIGN output function is described by using Matmul with M = 16, K = 16, N = 14, and the data type of matrix A and matrix B being half, as an example. When matrix C is configured to be in ND format and output to the global memory, the original output is not 32-byte aligned in the N direction, as shown in Figure 1. Figure 2 illustrates how the output is 32-byte aligned in the N direction when matrix C is set to be in ND format. Specifically, the last two columns in the N direction of matrix C are padded with the actual data from the next row, which ensures that the data in the N direction is 32-byte aligned and output. When matrix C is set to be in ND_ALIGN format, the Matmul API fills the last two columns in the N direction of matrix C by adding invalid data, which ensures that the data in the N direction is 32-byte aligned and output, as shown in Figure 3.

Figure 1 Non-32-byte alignment of matrix C in ND format in the N direction
Figure 2 32-byte alignment of matrix C in ND format in the N direction
Figure 3 32-byte alignment of matrix C in ND_ALIGN format in the N direction

Application Scenarios

In Matmul computation, the N direction is not 32-byte aligned, but the N direction of the output matrix C is required to be 32-byte aligned.

Constraints

If matrix C is set to be in ND_ALIGN format, the buffer space allocated for matrix C is the space obtained after the N direction is 32-byte aligned.

Example

For a complete operator example, see matmul_nd_align operator sample.