Quantization and Dequantization of Matrix Multiplication Outputs

Overview

For specific input and output data types, Matmul allows you to quantize or dequantize the output C matrix elements when the computation result is moved from CO1 to the global memory.

There are two Matmul quantization or dequantization modes: quantization or dequantization of the same coefficient, and vector quantization or dequantization. You can call the SetDequantType API in the operator tiling process to set the quantization or dequantization mode. The differences between the two modes are as follows:
  • Quantization or dequantization of the same coefficient (PER_TENSOR mode): The entire matrix C corresponds to one quantization parameter, whose shape is [1]. You can call the SetQuantScalar API on the operator kernel to set quantization parameters.
  • Vector quantization or dequantization (PER_CHANNEL mode): The shape of matrix C is [m, n]. Each channel dimension, that is, each column of matrix C, corresponds to a quantization parameter, whose shape is [n]. You can call the SetQuantVector API on the operator kernel to set quantization parameters.
Table 1 API configurations corresponding to the quantization or dequantization modes

Mode

Tiling API

Kernel API

Quantization or dequantization of the same coefficient

SetDequantType(DequantType::SCALAR)

SetQuantScalar(gmScalar)

Vector quantization or dequantization

SetDequantType(DequantType::TENSOR)

SetQuantVector(gmTensor)

Application Scenarios

Matrix computation results need to be quantized or dequantized. The following table lists the data types supported by the Matmul input and output matrices in this scenario.

Table 2 Data types supported by Matmul quantization or dequantization

Matrix A

Matrix B

Matrix C

Supported Platform

half

half

int8_t

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

bfloat16_t

bfloat16_t

int8_t

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

int8_t

int8_t

half

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

int4b_t

int4b_t

half

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

int8_t

int8_t

int8_t

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

int8_t

int8_t

bfloat16_t

Atlas 350 Accelerator Card

fp8_e4m3fn_t/fp8_e5m2_t

fp8_e4m3fn_t/fp8_e5m2_t

fp8_e4m3fn_t/half/bfloat16_t/float

Atlas 350 Accelerator Card

hifloat8_t

hifloat8_t

hifloat8_t/half/bfloat16_t/float

Atlas 350 Accelerator Card

Note:

For outputs of the hifloat8_t type, the Half to Away Round quantization method is applied.

If the output type in the quantization scenario is float, the precision of this quantization mode cannot reach 1/10,000 but can reach 1/1,000. If the precision is required to be dual-0.01%, you are advised to use the AscendDeQuant high-level API.

Constraints

Example

For a complete operator example, see matmul_quant sample.