目录

Data Layout Formats

The format is the physical layout of data and determines the dimensions for data interpretation, such as 1D, 2D, 3D, 4D, and 5D.

NCHW and NHWC

In deep learning, n-dimensional data is stored by using an n-dimensional array. For example, a feature map of a convolutional neural network (CNN) is stored by using a 4D array, including:

Data can be stored only in linear mode, so the four dimensions are stored in a certain layout. Different deep learning frameworks store feature maps in different layouts. For example, Caffe uses the layout [Batch, Channels, Height, Width], that is, NCHW, while TensorFlow uses the layout [Batch, Height, Width, Channels], that is, NHWC.

As shown in the following figure, for an RGB image, the pixel values of each channel are clustered in sequence as RRRRRRGGGGGGBBBBBB with the NCHW format. However, with the NHWC format, the pixel values are interleaved as RGBRGBRGBRGBRGBRGB.

Data access characteristics vary with the data storage sequence, though the stored data is the same. As such, the compute performance varies correspondingly even with the same operation.

NC1HWC0

On a Kirin AI processor, to improve data access efficiency of General Matrix Multiply (GEMM) data blocks, the tensor data is stored in NC1HWC0 in 5D format. C0, closely related to the micro architecture, is the size of the Cube Unit in the AI Core.

C1 = (C + C0 – 1)/C0. When the division is not exact, the result is rounded up.

Steps of NHWC/NCHW -> NC1HWC0 conversion: Tile data into C1 pieces of NHWC0/NC0HW along the C dimension, and arrange them in the memory into NC1HWC0, as shown in the following figure.

FRACTAL_NZ

FRACTAL_NZ is a fractal format for storing data such as feature maps. For example, the cube unit outputs matrices in NW1H1H0W0 format. A matrix is divided into (H1 × W1) fractals in column-major order, which looks like an N-shaped layout. Each fractal consists of (H0 x W0) elements in row-major order, resembling a Z-shaped layout. Therefore, the NW1H1H0W0 format is referred to as the Nz format. (H0 × W0) indicates the size of a fractal, as shown in the following figure.

ND-to-FRACTAL_NZ conversion:

(..., N, H, W )->pad->(..., N, H1*H0, W1*W0)->reshape->(..., N, H1, H0, W1, W0)->transpose->(..., N, W1, H1, H0, W0)

FRACTAL_Z

FRACTAL_Z is a format to define convolution weights, which is converted from the Filter Matrix. It is transferred to the cube unit in 4D format of "C1HW,N1,N0,C0".

The data is tiled into two layers, as shown in the following figure.

The data of the first layer, related to the cube size, is contiguously stored in column-major order (n format). The data of the second layer, related to the matrix size, is contiguously stored in row-major order (Z format).

For example, HWCN = (2, 2, 32, 32) can be reshaped into FRACTAL_Z (C1HW, N1, N0, C0) = (8, 2, 16, 16).

HWCN-to-FRACTAL_Z conversion:

Tensor.padding([ [0,0], [0,0], [0,(C0–C%C0)%C0], [0,(N0–N%N0)%N0] ]).reshape( [H, W, C1, C0, N1, N0]).transpose( [2, 0, 1, 4, 5, 3] ).reshape( [C1*H*W, N1, N0, C0])

NCHW-to-FRACTAL_Z conversion:

Tensor.padding([ [0,(N0–N%N0)%N0], [0,(C0–C%C0)%C0], [0,0], [0,0] ]).reshape( [N1, N0, C1, C0, H, W]).transpose( [2, 4, 5, 0, 1, 3] ).reshape( [C1*H*W, N1, N0, C0])