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.
|
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) |
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.
|
Matrix A |
Matrix B |
Matrix C |
Supported Platform |
|---|---|---|---|
|
half |
half |
int8_t |
Atlas 350 Accelerator Card |
|
bfloat16_t |
bfloat16_t |
int8_t |
Atlas 350 Accelerator Card |
|
int8_t |
int8_t |
half |
Atlas 350 Accelerator Card |
|
int4b_t |
int4b_t |
half |
|
|
int8_t |
int8_t |
int8_t |
Atlas 350 Accelerator Card |
|
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. |
For a complete operator example, see matmul_quant sample.
1 2 3 4 5 6 7 8 9 10 11 12 |
auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo()); matmul_tiling::MatmulApiTiling tiling(ascendcPlatform); tiling.SetAType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_INT8); tiling.SetBType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_INT8); tiling.SetCType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16); tiling.SetBiasType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_INT32); tiling.SetShape(M, N, K); tiling.SetOrgShape(M, N, K); tiling.EnableBias(true); tiling.SetDequantType(DequantType::SCALAR); // Set the quantization or dequantization of the same coefficient. // tiling.SetDequantType(DequantType::TENSOR); //: Set the vector quantization or dequantization. ... // Perform other configuration operations. |
1 2 3 4 5 6 7 8 |
REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling); float tmp = 0.1; // Multiplied by 0.1 during GM output uint64_t ans = static_cast<uint64_t>(*reinterpret_cast<int32_t*>(&tmp)); // Quantization coefficient of the floating-point value converted to the uint64_t type for setting. mm.SetQuantScalar(ans); mm.SetTensorA(gm_a); mm.SetTensorB(gm_b); mm.SetBias(gm_bias); mm.IterateAll(gm_c); |
1 2 3 4 5 6 7 8 |
GlobalTensor gmQuant; ... REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling); mm.SetQuantVector(gmQuant); mm.SetTensorA(gm_a); mm.SetTensorB(gm_b); mm.SetBias(gm_bias); mm.IterateAll(gm_c); |