Closed
moe_init_routing_v3(MXFP8 量化、FP8 Group 量化、MXFP8 透传) 性能优化 #3950
cpy_123456Add on Jul 27Closed at Jul 28
cpy_123456
Jul 27 Comments:
Jul 27 Comments:
/assign


Jul 27 Assign cpy_123456 as responsible person
Jul 27 Modify description of issue
Jul 28 Close issue
Jul 28 Added label: resolved
moe_init_routing_v3(MXFP8 量化、FP8 Group 量化、MXFP8 透传) 性能优化
概述
本次提交针对
moe_init_routing_v3算子在 arch35 平台上的三个 Gather 阶段 kernel(MXFP8 量化、FP8 Group 量化、MXFP8 透传)进行了系统性性能优化,涉及 9 个文件,+946/-620 行变更。优化一:为量化/透传 Kernel 引入 GatherCopy 模式
优化逻辑
背景:原有 MXFP8 量化、FP8 Group 量化、MXFP8 透传三个 kernel 仅支持 Scatter 模式(逐 output row 独立 CopyIn+Compute+CopyOut)。当多个 output row 映射到同一个 source row 时(即
k > 1且 expert 分布集中),同一行 x 会被重复从 GM 读入多次。方案:在 Tiling 侧新增
useGatherCopy字段,当满足(expertEnd - expertStart) * k__ > expertNum* THREE_NUM时启用 GatherCopy 模式。GatherCopy 按 source row 遍历,对同一 source row 只执行一次 CopyIn+Compute,然后将结果 CopyOut 到所有对应的 output row。Tiling 侧决策逻辑:
void ComputeUseGatherCopy() { if (IsOneOf3GatherKernels()) { tilingDataPtr_->useGatherCopy = ((expertEnd_ - expertStart_) * 3 > expertNum_) && (k_ > 1); } else { tilingDataPtr_->useGatherCopy = (rowIdxType_ == ROW_IDX_GATHER); } }Kernel 侧
sortedRowIdxGm_地址切换:GatherCopy 模式下,SCATTER 索引从 workspace 读取(由 RowIdxGather 生成),GATHER 索引直接读 expandedRowIdx;非 GatherCopy 模式则相反。收益
k > 1且 expert 分布集中的场景下,GM 读带宽可降低至原来的1/k核心代码(以 MXFP8 透传为例)
// GatherCopy 模式:按 source row 遍历,一次 CopyIn 多次 CopyOut for (int64_t row = currentLoopStartRow; row <= currentLoopLastRow; row++) { // 跳过所有 output index 为负的 source row(无需 CopyIn) bool hasValidOut = false; while (curLoopRow < currentLoopRows_ && globalSortIdx / k_ == row) { if (indicesLocal.GetValue(curLoopRow) >= 0) { hasValidOut = true; break; } curLoopRow++; globalSortIdx++; } if (!hasValidOut) { continue; } CopyIn(row, j, loopCols); // 对同一 source row 的所有有效 output index 执行 CopyOut while (curLoopRow < currentLoopRows_ && globalSortIdx / k_ == row) { int32_t outIndex = indicesLocal.GetValue(curLoopRow); if (outIndex >= 0) { DataCopyPad<uint8_t>(expandedXOutGm_[outIndex * cols_ + ...], outLocal, ...); } curLoopRow++; globalSortIdx++; } }优化二:FP8 Group 量化计算流水线重构
优化逻辑
背景:原有
ComputeFp8GroupQuantVF是一个单体 VF 函数,对每个 group(128 元素)串行执行 Amax→Scale→Quantize 全流程。存在两个性能瓶颈:Div(maxReg, fp8MaxReg)和Div(1.0f, roundScaleReg),延迟高方案:将单体函数拆分为三个独立的流水线阶段:
vfComputeAmaxamaxBuffer_vfComputeRoundScalevfComputeDatax * invScale并 Cast 到 FP8关键优化点 — 消除浮点除法:
旧方案:
Div(rawScaleReg, maxReg, fp8MaxReg, maskAll); // amax / fp8Max (浮点除法) Div(invScaleReg, 1.0f, roundScaleReg, maskAll); // 1.0 / scale (浮点除法)新方案(整数位操作):
// 直接从 amax 的 FP32 指数位提取,减去 fp8Emax 得到 roundedScale 的指数 ShiftRights(expBitsReg, (RegTensor<uint32_t> &)maxReg, FP32_EXPONENT_SHIFT, maskAllUint); Sub<uint32_t>(roundedExpBitsReg, expBitsReg, fp8EmaxReg, maskAllUint); // invScale 的指数 = 2*BIAS - roundedExpBits Sub<uint32_t>(invExpBitsReg, invExpSubReg, roundedExpBitsReg, maskAllUint);收益
vfComputeRoundScale一次处理vfLen(16 个 float)个 group 的 scale,充分利用向量寄存器宽度核心代码
// 阶段2:批量计算 RoundScale 和 InvScale(整数位操作,无浮点除法) template <bool CLAMP_AMAX> __simd_vf__ inline void vfComputeRoundScale(__ubuf__ float *amaxInAddr, __ubuf__ float *scaleOutAddr, __ubuf__ float *invScaleOutAddr, uint16_t groupNum, uint32_t fp8EmaxValue) { for (uint16_t i = 0; i < vfLoopNum; i++) { DataCopy<float, PostLiteral::POST_MODE_UPDATE>(maxReg, amaxInAddr, vfLen); // 提取指数位 ShiftRights(expBitsReg, (RegTensor<uint32_t> &)maxReg, FP32_EXPONENT_SHIFT, maskAllUint); // 尾数进位判断 Compare<uint32_t, CMPMODE::GT>(maskMantHigh, mantBitsReg, fp8MantReg, maskAllUint); // roundedExpBits = expBits - fp8Emax + mantAdd Sub<uint32_t>(roundedExpBitsReg, expBitsReg, fp8EmaxReg, maskAllUint); Add<uint32_t>(roundedExpBitsReg, roundedExpBitsReg, mantAddReg, maskAllUint); // scale = roundedExpBits << 23 (构造 FP32) ShiftLefts((RegTensor<float> &)roundScaleReg, roundedExpBitsReg, FP32_EXPONENT_SHIFT, maskAllUint); // invScale = (2*BIAS - roundedExpBits) << 23 Sub<uint32_t>(invExpBitsReg, invExpSubReg, roundedExpBitsReg, maskAllUint); ShiftLefts((RegTensor<float> &)invScaleReg, invExpBitsReg, FP32_EXPONENT_SHIFT, maskAllUint); } }优化三:GatherCopy 模式下的 hasValidOut 提前跳过
优化逻辑
在 GatherCopy 模式中,部分 source row 的所有 output index 可能为负值(即该行没有被任何 expert 选中)。新增
hasValidOut预扫描:在 CopyIn 之前先遍历当前 source row 的所有 index,若全部为负则直接continue,跳过 CopyIn + Compute + CopyOut 全流程。收益
核心代码
bool hasValidOut = false; while (curLoopRow < currentLoopRows_ && globalSortIdx / k_ == row) { if (indicesLocal.GetValue(curLoopRow) >= 0) { hasValidOut = true; break; } curLoopRow++; globalSortIdx++; } if (!hasValidOut) { continue; }优化四:Tiling 侧 UB 预算与 Workspace 修正
4.1 MXFP8 量化 UB 预算修正
CalcMaxRowIdxPerLoopMxQuant中xInSize多算了AlignBytes(perLoopCols, sizeof(int8_t)):// 修正前(多算了 int8 buffer) int64_t xInSize = AlignBytes(perLoopCols, inputXDtypeSize_) + AlignBytes(perLoopCols, sizeof(int8_t)); // 修正后 int64_t xInSize = AlignBytes(perLoopCols, inputXDtypeSize_);收益:释放了被多算的 UB 空间,允许更多的
perLoopMaxIndicesElements(每循环处理的行数),减少循环次数。4.2 FP8 Group 量化独立 UB 预算函数
新增
CalcMaxRowIdxPerLoopFP8GroupQuant,精确覆盖 kernel 中amaxBuffer_和invScaleBuffer_两个 TBuf 的 UB 开销:int64_t CalcMaxRowIdxPerLoopFP8GroupQuant(int64_t perLoopCols) { int64_t xInSize = AlignBytes(perLoopCols, inputXDtypeSize_); int64_t xOutSize = AlignBytes(perLoopCols, sizeof(uint8_t)); int64_t scaleSize = AlignBytes(CeilDiv(perLoopCols, FP8_GROUP_SIZE), sizeof(float)); int64_t tmpBufSize = std::max(scaleSize, REG_SIZE); // amaxBuffer_ / invScaleBuffer_ return (availUbSize_ / 2 - (xInSize + xOutSize + scaleSize + tmpBufSize * 2)) / sizeof(int32_t); }4.3 Workspace 精简
新增
NeedQuantTempWorkspace()函数,MXFP8/FP8 Group 量化模式不再分配quantTempWorkspace(这些 kernel 不使用quantTempGm_):bool NeedQuantTempWorkspace() const { return (quantMode_ >= QUANT_MODE_DYNAMIC && quantMode_ != QUANT_MODE_HIF8_CAST && quantMode_ != QUANT_MODE_HIF8_PERTENSOR && quantMode_ != QUANT_MODE_MXFP8_E5M2 && quantMode_ != QUANT_MODE_MXFP8_E4M3FN && ...); }收益:减少
aivCoreNum * cols * sizeof(float)字节的 workspace 分配(对于典型场景如 64 核 × 7168 列 = 1.75MB)。优化五:RowIdxGather 与 ExpertTokensCount 适配
RowIdxGather 简化
useGatherCopy模式下 SCATTER 索引的反向映射(从 expandedRowIdx 读到 workspace)ExpertTokensCount 适配
useGatherCopy && SCATTER模式下,在 Init 阶段将 workspace 中的 gather row index 区域初始化为 -1涉及文件汇总
moe_init_routing_v3_tiling_arch35.cppuseGatherCopy决策、UB 预算修正、Workspace 精简moe_init_routing_v3_arch35_tiling_def.huseGatherCopy字段moe_v3_gather_mxfp8_quant.hmoe_v3_gather_fp8_group_quant.hmoe_v3_gather_out_mxfp8.hmoe_v3_row_idx_gather.hmoe_v3_expert_tokens_count.hmoe_init_routing_v3_apt.cpptest_moe_init_routing_v3_arch35_tiling.cpp