This document is relevant for: Trn2, Trn3
nki.isa.nc_matmul_mx#
- nki.isa.nc_matmul_mx(dst: NkiTensor, stationary: NkiTensor, moving: NkiTensor, stationary_scale: NkiTensor, moving_scale: NkiTensor, tile_position=None, tile_size=None, accumulate=None, name=None)[source]#
Compute matrix multiplication of MXFP8/MXFP4 quantized matrices with integrated dequantization using Tensor Engine.
Note
Available only on NeuronCore-v4 and newer.
The NeuronCore-v4 Tensor Engine supports matrix multiplication of MXFP8/MXFP4 quantized matrices as defined in the OCP Microscaling standard. This instruction performs matrix multiplication between quantized
stationaryandmovingmatrices while applying dequantization scales during computation. The micro-scaling group size is 32 elements in groups of 8 partitions × 4 elements per partition of bothstationaryandmovingtensors. See Trainium3 arch guide for more detailed discussion.Tiling Mode.
NeuronCore Tensor Engine is built upon a systolic array with 128 rows and 128 columns of processing elements (PEs). For
nc_matmul_mx, Tensor Engine supports only row tiling mode, which allows multiplenc_matmul_mxinstructions with a stationary partition dimension size smaller than 128 to run in parallel to improve hardware utilization. Row tiling mode slices the 128 PE rows into 2x 64 row tiles or 4x 32 row tiles.tile_sizeis a tuple(row_size, column_size)that sets the row tile size.row_sizemust be 128, 64, or 32, andcolumn_sizemust be 128. The stationary partition dimension size must not exceedrow_size.A given
nc_matmul_mxcan pick the exact row tile within the 128x128 systolic array by specifying the starting row intile_positionas a tuple(start_row, start_column), wherestart_columnmust be 0. Thestart_rowmust be a multiple ofrow_size, andstart_row + row_sizemust not exceed 128. Placestationary,moving, and their scales at SBUF partitionstart_row.Note
Known issue: NKI currently does not use
row_sizeto set the row tile size. When tiling mode is enabled, NKI derives the row tile size from the stationary partition dimension size. Setrow_sizeto that value so it matches the tile size actually used. A row tile smaller than 128 also currently requires a stationary free dimension size greater than 64. NKI also does not yet check thatrow_sizeis 128, 64, or 32, or thatstart_row + row_sizedoes not exceed 128. Whether such a configuration compiles depends on the stationary partition dimension size, which sets the actual row tile size.For example, setting
tile_positionto (64, 0) andtile_sizeto (64, 128) means using the bottom half of the systolic array.Note,
tile_positionandtile_sizemust both be set to enable tiling mode. If they are not set, the default is to use the full systolic array, which is equivalent totile_position=(0, 0)andtile_size=(128, 128). The values intile_positionandtile_sizetuples can be integers or affine expressions.Memory types.
The
nc_matmul_mxinstruction must read inputs from SBUF and write outputs to PSUM. Therefore, thestationary,moving,stationary_scale, andmoving_scalemust be SBUF tiles, anddsttile must be a PSUM tile.Data types.
The input
stationaryandmovingtiles must be float8_e5m2_x4, float8_e4m3fn_x4, or float4_e2m1fn_x4 (4-packed quantized data types). Thestationary_scaleandmoving_scaletiles must be float8_e8m0fnu or uint8 (preferfloat8_e8m0fnu: OCP MX standard; uint8 accepted for backward compatibility). Thedsttile can be float32 or bfloat16.Layout.
The contraction dimension of the matrix multiplication is along the partition dimension of
stationaryandmovingtensors and also the x4 dimension within each packed data type element (float8_e5m2_x4, float8_e4m3fn_x4, or float4_e2m1fn_x4).The free dimension of the
stationarytile matches the partition dimension of the outputdsttile in size, while the free dimension of themovingtile matches the free dimension of thedsttile in size.The scale tensors follow a special layout requirement. See more details in
nisa.quantize_mxAPI doc.Tile size
The partition dimension size of
stationaryandmovingmust be identical and be either 32, 64, or 128.The free dimension size of
stationarymust be even and not exceed 128.The free dimension size of
movingmust not exceed 4096 whendstis in float32 or 8192 whendstis in bfloat16, matching the size of 8x PSUM banks (the entire PSUM).The scale tensors have partition dimensions that depend on whether the data tensors span multiple quadrants. See more details in
nisa.quantize_mxAPI doc.dstcan have at most three free dimensions (maximum rank 4). Each ofstationary,moving,stationary_scale, andmoving_scalemust be contiguous in memory (multiple free dimensions are allowed if they collapse to a single contiguous run). These ranks include the partition dimension.
Profiler view syntax.
nc_matmul_mxuses the same profiler output format as nisa.nc_matmul, except the source access pattern is interpreted as an MX-quantized tensor:src=<dtype>@$MX[<data_addr>,<scale_addr>,<start_scale_partition>]@[<step_elem>][<num_elem>].Tensor indirection.
dstsupports tensor indirection (gather/scatter) independently by passing a view created with.indirect(index). The(stationary, stationary_scale)and(moving, moving_scale)pairs must agree on tensor indirection: either both members of a pair use.indirect()with the same index, or neither does — because in MX format the data bytes and scale exponents form a single logical tensor. Tensor indirection is incompatible with hardware tiling, sotile_positionmust be(0, 0)(or unset) when it is used. (nc_matmul_mxitself is only available on NeuronCore-v4 and later.)When operands are manually allocated, their base partitions must satisfy:
the
indexof every.indirect()view starts on a quadrant boundary (a multiple of 32);if
stationaryormovinguses.indirect(), the operand starts on the same partition as itsindex;if
dstuses.indirect(),dst’sindexstarts on the same partition asstationary.
- Parameters:
dst – the matrix multiplication output (PSUM tile)
stationary – the stationary quantized matrix (SBUF tile)
moving – the moving quantized matrix (SBUF tile)
stationary_scale – the dequantization scales for stationary matrix (SBUF tile)
moving_scale – the dequantization scales for moving matrix (SBUF tile)
tile_position – (optional) a 2D tuple (start_row, start_column) to control the starting row in Tensor Engine tiling mode;
start_columnmust be 0tile_size – (optional) a 2D tuple (row_size, column_size) setting the row tile size in Tensor Engine tiling mode;
column_sizemust be 128.accumulate – (optional) if True, accumulate the matmul result into the existing
dstPSUM tile content; if False, overwrite the existing content; if None (default), auto-detect based on whether this PSUM location was previously written
This document is relevant for: Trn2, Trn3