Closed
[Bug-Report|缺陷反馈]: moeinitroutingv3 mxfp8量化对于nan、INF、-Inf处理逻辑不一致 #4358
cpy_123456Add on Aug 17Closed at Aug 24
Aug 17 Added label: bug-report
weihao18
Aug 17 Comments:
Aug 17 Comments:
/assign @cpy_123456


Aug 17 Assign cpy_123456 as responsible person
Aug 17 Modify description of issue
Aug 24 Close issue
Aug 24 Added label: resolved
Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.
Describe the current behavior / 问题描述 (Mandatory / 必填)
MoeInitRoutingV3 MXFP8量化规格文档
概述
本文档描述了 Ascend NPU Arch35 架构下 MoE(混合专家模型)路由操作中使用的三个 FP8 量化 Kernel。这些 Kernel 在 Gather 操作的同时执行不同策略的 FP8 量化,以实现高效的数据路由与计算。
1. MXFP8 量化 Kernel
文件:
moe_v3_gather_mxfp8_quant.h类名:
MoeGatherOutMxfp8Quant<T, U, CLAMP_AMAX>功能描述
执行 MX(Microscaling)FP8 量化,每个 scale 因子对应 32 个元素。
量化参数
数学公式
Scale 计算
量化计算
特殊值处理
输入:NaN / Inf / -Inf
amax >= 0x7f80→mxScale = FP8_E8M0_NAN_VAL输入:全零
amax == 0→mxScale = 0x * invScale = 0关键实现细节
Amax 计算(
vfComputeAmax,第 34-98 行):Scale 计算(
vfComputeRoundScale,第 100-198 行):expBits = amax >> 7mantBits > 0x0060→ 指数加 1数据量化(
vfComputeData,第 200-313 行):Tiling 参数
来自
MoeInitRoutingV3Arch35TilingData:cols:隐藏维度大小(必须为 32 的倍数)validScaleCols:ceil(cols / 32)scaleCols:ceil_align(validScaleCols, 2)(向上对齐到偶数)expandedScale 输出形状
根据 infershape 定义,MXFP8 的 expandedScale 为 2D 形状:
其中:
outNum = n * kceil_align(ceil(cols / 32), 2):每 32 个元素对应一个 scale 因子,向上对齐到偶数2. FP8 PerBlock 量化 Kernel
文件:
moe_v3_gather_fp8_perblock_quant.h类名:
MoeGatherOutFP8PerBlockQuant<T, U>功能描述
执行 FP8 逐块量化,每个块包含 128 个元素,使用 FP32 类型的 scale 因子。
量化参数
数学公式
Scale 计算
量化计算
特殊值处理
输入:NaN / Inf / -Inf
scaleReg >= 0x7f800000→ 写入 NaN输入:全零
amax = 0→scale = 0x / scale = 0(或饱和为 0)关键实现细节
双块处理(
ComputeVF,第 34-231 行):Scale 计算(第 79-89 行,第 93-103 行):
abs = x & 0x7fff量化计算(第 106-130 行):
Tiling 参数
来自
MoeInitRoutingV3Arch35TilingData:cols:隐藏维度大小(必须为 128 的倍数)validScaleCols:ceil(cols / 128)scaleCols:ceil_align(validScaleCols, 2)(向上对齐到偶数)expandedScale 输出形状
根据 infershape 定义,PerBlock 的 expandedScale 为 3D 形状:
其中:
outNum = n * kceil(cols / 256):每 256 个元素(2 个 128 块)为一组2:每组包含 2 个 scale 因子(对应 2 个 128 块)3. FP8 Group 量化 Kernel
文件:
moe_v3_gather_fp8_group_quant.h类名:
MoeV3GatherFP8GroupQuant<T, U, CLAMP_AMAX>功能描述
执行 FP8 逐组动态量化,每组包含 128 个元素,使用 FP32 类型的 scale 因子,采用基于指数的 scale 计算方式。
量化参数
数学公式
Scale 计算
量化计算
特殊值处理
输入:NaN / Inf / -Inf
amax >= 0x7F800000→ 写入 NaN输入:全零
roundedExp = 1→scale = 2^(-126)x * invScale = 0关键实现细节
三阶段计算(
Compute,第 552-588 行):VfComputeAmax— 计算组内绝对值最大值VfComputeRoundScale— 计算 scale 和 invScaleVfComputeData— 使用 invScale 量化数据Amax 计算(
VfComputeAmax,第 55-118 行):Scale 计算(
VfComputeRoundScale,第 120-196 行):数据量化(
VfComputeData,第 198-259 行):Tiling 参数
来自
MoeInitRoutingV3Arch35TilingData:cols:隐藏维度大小(必须为 128 的倍数)scaleCols:ceil(cols / 128)expandedScale 输出形状
根据 infershape 定义,Group 的 expandedScale 为 2D 形状:
其中:
outNum = n * kceil(cols / 128):每 128 个元素对应一个 scale 因子三者对比表
x * invScalex / scalex * invScale[outNum, ceil_align(ceil(cols/32), 2)][outNum, ceil(cols/256), 2][outNum, ceil(cols/128)]维度规格
输入维度
x[n, cols]expert_idx[n, k]scaleoffset输出维度
expandedX
[xOutNum, cols]expandedScale(三种量化 Kernel 对比)
[outNum, ceil_align(ceil(cols/32), 2)][outNum, ceil(cols/256), 2][outNum, ceil(cols/128)]outNum 与 xOutNum 的计算
Scale 列数详细计算
dim1 = ceil_align(ceil(cols/32), 2)[outNum, dim1]dim1 = ceil(cols/256),dim2 = 2[outNum, dim1, 2]dim1 = ceil(cols/128)[outNum, dim1]维度示例
假设
n=8, k=2, cols=512, activeNum=-1(即 outNum=16, xOutNum=16):[8, 512][16, 512][16, 16](E8M0),ceil_align(ceil(512/32), 2) = 16[8, 512][16, 512][16, 2, 2](FP32),ceil(512/256) = 2[8, 512][16, 512][16, 4](FP32),ceil(512/128) = 4假设
n=4, k=1, cols=192, activeNum=-1(即 outNum=4, xOutNum=4):[4, 192][4, 192][4, 6](E8M0),ceil_align(ceil(192/32), 2) = 6[4, 192][4, 192][4, 1, 2](FP32),ceil(192/256) = 1[4, 192][4, 192][4, 2](FP32),ceil(192/128) = 2假设
n=8, k=2, cols=512, activeNum=10(即 outNum=16, xOutNum=10):[8, 512][10, 512][16, 16](E8M0)[8, 512][10, 512][16, 2, 2](FP32)[8, 512][10, 512][16, 4](FP32)维度关系图
公共参数
输入张量
x[n, cols]expert_idx[n, k]scaleoffset输出张量
expandedX[xOutNum, cols]expandedScaleTiling 数据字段
coreNumnkcolsactualExpertNumrowIdxTypeuseGatherCopy特殊值常量
FP16_EMASK_AND_INF_VALBF16_EMASK_AND_INF_VALFP32_INF_VALUEFP32_NAN_VALUEBF16_NAN_VALFP8_E8M0_NAN_VALFP8_E4M3_MAX_VALUEFP8_E5M2_MAX_VALUE异常处理
溢出检测
所有 Kernel 在 Arch3510 上禁用溢出模式:
#if (__NPU_ARCH__ == 3510) SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(0); #endif饱和模式
所有 FP8 类型转换使用饱和模式(
SatMode::SAT),具体行为:性能考量
参考资料
Environment / 环境信息 (Mandatory / 必填)
950
Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)
moeinitroutingv3 mxfp8量化对于nan、INF、-Inf
Describe the expected behavior / 预期结果 (Mandatory / 必填)
输出expandedx为0,expanedscale为nan
Related log / screenshot / 日志 / 截图 (Mandatory / 必填)
是
Special notes for this issue/备注 (Optional / 选填)