Merged
Feat: operator interface of MegaMoe supports situ activation #9667
OblivionZHUAdd on Aug 6
Feat: operator interface of MegaMoe supports situ activation #9667
Merged
Merged fromDeleted :masterintocann/ops-transformermaster
Total: 4 files changed+164-119
| @@ -352,27 +352,53 @@ static ge::graphStatus CheckAttrPtrNullptr(const gert::TilingContext *context, M | |||
| 352 | OP_TILING_CHECK(commAlgPtr == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "commAlg"), return ge::GRAPH_FAILED); | 352 | OP_TILING_CHECK(commAlgPtr == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "commAlg"), return ge::GRAPH_FAILED); |
| 353 | OP_TILING_CHECK(numMaxTokensPerRankPtr == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "numMaxTokensPerRank"), | 353 | OP_TILING_CHECK(numMaxTokensPerRankPtr == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "numMaxTokensPerRank"), |
| 354 | return ge::GRAPH_FAILED); | 354 | return ge::GRAPH_FAILED); |
| 355 | - OP_TILING_CHECK(activationPtr == nullptr || std::strcmp(activationPtr, "swiglu") != 0, | 355 | + OP_TILING_CHECK(activationPtr == nullptr, |
| 356 | - OP_LOGE_FOR_INVALID_VALUE(nodeName, "activation", activationPtr == nullptr ? "null" : activationPtr, | 356 | + OP_LOGE_WITH_INVALID_INPUT(nodeName, "activation"), return ge::GRAPH_FAILED); |
| 357 | - "A5 only supports swiglu"), | 357 | + const bool isSwiglu = std::strcmp(activationPtr, "swiglu") == 0; |
| 358 | + const bool isSituGlu = std::strcmp(activationPtr, "situglu") == 0; | ||
| 359 | + OP_TILING_CHECK(!isSwiglu && !isSituGlu, | ||
| 360 | + OP_LOGE_FOR_INVALID_VALUE(nodeName, "activation", activationPtr, | ||
| 361 | + "A5 supports 'swiglu' and 'situglu'"), | ||
| 358 | return ge::GRAPH_FAILED); | 362 | return ge::GRAPH_FAILED); |
| 359 | OP_TILING_CHECK(activationParamsPtr == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "activationParams"), | 363 | OP_TILING_CHECK(activationParamsPtr == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "activationParams"), |
| 360 | return ge::GRAPH_FAILED); | 364 | return ge::GRAPH_FAILED); |
| 361 | - OP_TILING_CHECK(activationParamsPtr->GetSize() > 1U, | 365 | + const size_t paramCount = activationParamsPtr->GetSize(); |
| 362 | - OP_LOGE_FOR_INVALID_VALUE(nodeName, "activationParamsSize", | 366 | + const float *activationParams = activationParamsPtr->GetData(); |
| 363 | - std::to_string(activationParamsPtr->GetSize()).c_str(), | 367 | + OP_TILING_CHECK(activationParams == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "activationParamsData"), |
| 364 | - "should be 0 or 1 for swiglu"), | ||
| 365 | return ge::GRAPH_FAILED); | 368 | return ge::GRAPH_FAILED); |
| 366 | - if (activationParamsPtr->GetSize() == 1U) { | 369 | + if (isSwiglu) { |
| 367 | - const float *activationParams = activationParamsPtr->GetData(); | 370 | + OP_TILING_CHECK(paramCount > 1U, |
| 368 | - OP_TILING_CHECK(activationParams == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "activationParamsData"), | 371 | + OP_LOGE_FOR_INVALID_VALUE(nodeName, "activationParamsSize", |
| 372 | + std::to_string(paramCount).c_str(), | ||
| 373 | + "should be 0 or 1 for swiglu"), | ||
| 369 | return ge::GRAPH_FAILED); | 374 | return ge::GRAPH_FAILED); |
| 370 | - const float activationClamp = activationParams[0]; | 375 | + if (paramCount == 1U) { |
| 371 | - OP_TILING_CHECK(activationClamp < 0 || std::isnan(activationClamp), | 376 | + const float activationClamp = activationParams[0]; |
| 372 | - OP_LOGE_FOR_INVALID_VALUE(nodeName, "activationClamp", | 377 | + OP_TILING_CHECK(activationClamp < 0 || std::isnan(activationClamp), |
| 373 | - std::to_string(activationClamp).c_str(), | 378 | + OP_LOGE_FOR_INVALID_VALUE(nodeName, "activationClamp", |
| 374 | - "should be >= 0 and not NAN"), | 379 | + std::to_string(activationClamp).c_str(), |
| 380 | + "should be >= 0 and not NAN"), | ||
| 381 | + return ge::GRAPH_FAILED); | ||
| 382 | + } | ||
| 383 | + } else { | ||
| 384 | + OP_TILING_CHECK(paramCount < 1U || paramCount > 2U, | ||
| 385 | + OP_LOGE_FOR_INVALID_VALUE(nodeName, "activationParamsSize", | ||
| 386 | + std::to_string(paramCount).c_str(), | ||
| 387 | + "should be 1 or 2 for situglu"), | ||
| 375 | return ge::GRAPH_FAILED); | 388 | return ge::GRAPH_FAILED); |
| 389 | + const float beta = activationParams[0]; | ||
| 390 | + OP_TILING_CHECK(!std::isfinite(beta) || beta == 0.0f, | ||
| 391 | + OP_LOGE_FOR_INVALID_VALUE(nodeName, "situglu_beta", std::to_string(beta).c_str(), | ||
| 392 | + "should be finite and non-zero"), | ||
| 393 | + return ge::GRAPH_FAILED); | ||
| 394 | + if (paramCount == 2U) { | ||
| 395 | + const float linearBeta = activationParams[1]; | ||
| 396 | + OP_TILING_CHECK(!std::isfinite(linearBeta) || linearBeta == 0.0f, | ||
| 397 | + OP_LOGE_FOR_INVALID_VALUE(nodeName, "situglu_linear_beta", | ||
| 398 | + std::to_string(linearBeta).c_str(), | ||
| 399 | + "should be finite and non-zero"), | ||
| 400 | + return ge::GRAPH_FAILED); | ||
| 401 | + } | ||
| 376 | } | 402 | } |
| 377 | 403 | ||
| 378 | return ge::GRAPH_SUCCESS; | 404 | return ge::GRAPH_SUCCESS; |
| @@ -562,6 +588,7 @@ static ge::graphStatus SetAttrParams(const gert::TilingContext *context, MegaMoe | |||
| 562 | 588 | ||
| 563 | auto epWorldSizePtr = attrs->GetAttrPointer<int64_t>((config.attrEpWorldSizeIndex)); | 589 | auto epWorldSizePtr = attrs->GetAttrPointer<int64_t>((config.attrEpWorldSizeIndex)); |
| 564 | auto maxRecvTokenNumPtr = attrs->GetAttrPointer<int64_t>((config.attrMaxRecvTokenNumIndex)); | 590 | auto maxRecvTokenNumPtr = attrs->GetAttrPointer<int64_t>((config.attrMaxRecvTokenNumIndex)); |
| 591 | + auto activationPtr = attrs->GetAttrPointer<char>(static_cast<int>(config.attrActivationIndex)); | ||
| 565 | auto activationParamsPtr = attrs->GetListFloat(config.attrActivationParamsIndex); | 592 | auto activationParamsPtr = attrs->GetListFloat(config.attrActivationParamsIndex); |
| 566 | 593 | ||
| 567 | tilingData->epWorldSize = *epWorldSizePtr; | 594 | tilingData->epWorldSize = *epWorldSizePtr; |
| @@ -575,12 +602,26 @@ static ge::graphStatus SetAttrParams(const gert::TilingContext *context, MegaMoe | |||
| 575 | tilingData->moeExpertPerRank); | 602 | tilingData->moeExpertPerRank); |
| 576 | tilingData->blockNumPerEP = std::max(static_cast<uint32_t>(1), aicNum / tilingData->epWorldSize); | 603 | tilingData->blockNumPerEP = std::max(static_cast<uint32_t>(1), aicNum / tilingData->epWorldSize); |
| 577 | tilingData->combineQuantMode = GetCombineQuantModeByAttr(context, config); | 604 | tilingData->combineQuantMode = GetCombineQuantModeByAttr(context, config); |
| 578 | - tilingData->clampLimit = activationParamsPtr->GetSize() == 0U ? DEFAULT_ACTIVATION_CLAMP : | 605 | + const float *activationParams = activationParamsPtr->GetData(); |
| 579 | - activationParamsPtr->GetData()[0]; | 606 | + if (std::strcmp(activationPtr, "situglu") == 0) { |
| 580 | - tilingData->actMode = 0U; // ACT_MODE_SWIGLU, 功能关闭 | 607 | + tilingData->clampLimit = std::numeric_limits<float>::max(); |
| 581 | - tilingData->actSubMode = 0U; // ACT_SUB_MODE_DEFAULT | 608 | + tilingData->actMode = 1U; // ACT_MODE_SITU |
| 582 | - tilingData->activationAlpha = 1.0f; | 609 | + tilingData->activationBeta = activationParams[0]; |
| 583 | - tilingData->activationBeta = 1.0f; | 610 | + if (activationParamsPtr->GetSize() == 2U) { |
| 611 | + tilingData->actSubMode = 1U; // ACT_SUB_MODE_LINEAR | ||
| 612 | + tilingData->activationAlpha = activationParams[1]; | ||
| 613 | + } else { | ||
| 614 | + tilingData->actSubMode = 0U; // ACT_SUB_MODE_DEFAULT | ||
| 615 | + tilingData->activationAlpha = 0.0f; | ||
| 616 | + } | ||
| 617 | + } else { | ||
| 618 | + tilingData->clampLimit = activationParamsPtr->GetSize() == 0U ? DEFAULT_ACTIVATION_CLAMP : | ||
| 619 | + activationParams[0]; | ||
| 620 | + tilingData->actMode = 0U; // ACT_MODE_SWIGLU | ||
| 621 | + tilingData->actSubMode = 0U; // ACT_SUB_MODE_DEFAULT | ||
| 622 | + tilingData->activationAlpha = 1.0f; | ||
| 623 | + tilingData->activationBeta = 1.0f; | ||
| 624 | + } | ||
| 584 | 625 | ||
| 585 | tilingData->sharedExpertNum = static_cast<uint32_t>( | 626 | tilingData->sharedExpertNum = static_cast<uint32_t>( |
| 586 | GetWeightExpertCount(context, config.sharedWeight1Index, tilingData->isPerExpertWeightTensor)); | 627 | GetWeightExpertCount(context, config.sharedWeight1Index, tilingData->isPerExpertWeightTensor)); |
| @@ -125,10 +125,50 @@ static constexpr AscendC::MicroAPI::CastTrait CAST_32_TO_83 = { | |||
| 125 | DataTypeOut_, DataTypeIn_, DataTypeX2Scale_, DataTypeX1Scale_, IsTensorList_, TileM, TileN, TopkWeightsPrefetch, \ | 125 | DataTypeOut_, DataTypeIn_, DataTypeX2Scale_, DataTypeX1Scale_, IsTensorList_, TileM, TileN, TopkWeightsPrefetch, \ |
| 126 | IsInterleaved_, IsWaveFlagGrained_ | 126 | IsInterleaved_, IsWaveFlagGrained_ |
| 127 | 127 | ||
| 128 | -// 前向声明:激活分块上下文结构体(仅含标量与 UB 指针,可跨函数传递) | 128 | +/* |
| 129 | -// 完整定义见 BlockEpilogueSwigluMxQuant 类定义之后 | 129 | + * ActivationContext: 激活分块上下文 |
| 130 | + * 仅含标量与 UB 指针(不含 RegTensor),可安全跨函数传递给各 ActivationFlow* | ||
| 131 | + * 由 VFDoSwigluAndQuantForMX 填充后分派给具体激活函数 | ||
| 132 | + */ | ||
| 130 | template <typename DataTypeInT> | 133 | template <typename DataTypeInT> |
| 131 | -struct ActivationContext; | 134 | +struct ActivationContext { |
| 135 | + // 源数据地址 | ||
| 136 | + __ubuf__ DataTypeInT *firstSrc = nullptr; | ||
| 137 | + __ubuf__ DataTypeInT *secondSrc = nullptr; | ||
| 138 | + __ubuf__ bfloat16_t *gluResAddr = nullptr; | ||
| 139 | + | ||
| 140 | + // 对齐参数 | ||
| 141 | + uint32_t nSrcUbAligned = 0; | ||
| 142 | + uint32_t nDstUbAligned = 0; | ||
| 143 | + | ||
| 144 | + // 分块循环控制 | ||
| 145 | + uint16_t dim0VfTimes = 0; | ||
| 146 | + uint16_t dim1VfTimes = 0; | ||
| 147 | + uint32_t dim1Tail = 0; | ||
| 148 | + uint16_t dim1TailTimes = 0; | ||
| 149 | + uint16_t dim1Tail2 = 0; | ||
| 150 | + | ||
| 151 | + // 尾循环 mask | ||
| 152 | + uint32_t mask1Num = 0; | ||
| 153 | + uint32_t mask2Num = 0; | ||
| 154 | + uint32_t mask3Num = 0; | ||
| 155 | + | ||
| 156 | + // 尾循环地址 | ||
| 157 | + __ubuf__ DataTypeInT *firstTailAddr = nullptr; | ||
| 158 | + __ubuf__ DataTypeInT *secondTailAddr = nullptr; | ||
| 159 | + __ubuf__ bfloat16_t *swigluTailAddr1 = nullptr; | ||
| 160 | + __ubuf__ bfloat16_t *swigluTailAddr2 = nullptr; | ||
| 161 | + | ||
| 162 | + // 权重预取地址(nullptr 表示不启用 TopkWeightsPrefetch) | ||
| 163 | + __ubuf__ float *weightUbAddr = nullptr; | ||
| 164 | + | ||
| 165 | + // 激活参数(beta/alpha 默认 1.0,倒数同步 1.0,兜底为恒等变换) | ||
| 166 | + float clampLimit = 0.0f; | ||
| 167 | + float beta = 1.0f; | ||
| 168 | + float invBeta = 1.0f; | ||
| 169 | + float alpha = 1.0f; | ||
| 170 | + float invAlpha = 1.0f; | ||
| 171 | +}; | ||
| 132 | 172 | ||
| 133 | template <typename DataTypeOut_, typename DataTypeIn_, typename DataTypeX2Scale_, typename DataTypeX1Scale_, | 173 | template <typename DataTypeOut_, typename DataTypeIn_, typename DataTypeX2Scale_, typename DataTypeX1Scale_, |
| 134 | bool IsTensorList_, uint32_t TileM = 256, uint32_t TileN = 256, bool TopkWeightsPrefetch = false, | 174 | bool IsTensorList_, uint32_t TileM = 256, uint32_t TileN = 256, bool TopkWeightsPrefetch = false, |
| @@ -267,51 +307,6 @@ private: | |||
| 267 | float activationBeta_{1.0f}; | 307 | float activationBeta_{1.0f}; |
| 268 | }; | 308 | }; |
| 269 | 309 | ||
| 270 | -// ============================================================================ | ||
| 271 | -// ActivationContext: 激活分块上下文 | ||
| 272 | -// 仅含标量与 UB 指针(不含 RegTensor),可安全跨函数传递给各 ActivationFlow* | ||
| 273 | -// 由 VFDoSwigluAndQuantForMX 填充后分派给具体激活函数 | ||
| 274 | -// ============================================================================ | ||
| 275 | -template <typename DataTypeInT> | ||
| 276 | -struct ActivationContext { | ||
| 277 | - // 源数据地址 | ||
| 278 | - __ubuf__ DataTypeInT *firstSrc = nullptr; | ||
| 279 | - __ubuf__ DataTypeInT *secondSrc = nullptr; | ||
| 280 | - __ubuf__ bfloat16_t *gluResAddr = nullptr; | ||
| 281 | - | ||
| 282 | - // 对齐参数 | ||
| 283 | - uint32_t nSrcUbAligned = 0; | ||
| 284 | - uint32_t nDstUbAligned = 0; | ||
| 285 | - | ||
| 286 | - // 分块循环控制 | ||
| 287 | - uint16_t dim0VfTimes = 0; | ||
| 288 | - uint16_t dim1VfTimes = 0; | ||
| 289 | - uint32_t dim1Tail = 0; | ||
| 290 | - uint16_t dim1TailTimes = 0; | ||
| 291 | - uint16_t dim1Tail2 = 0; | ||
| 292 | - | ||
| 293 | - // 尾循环 mask | ||
| 294 | - uint32_t mask1Num = 0; | ||
| 295 | - uint32_t mask2Num = 0; | ||
| 296 | - uint32_t mask3Num = 0; | ||
| 297 | - | ||
| 298 | - // 尾循环地址 | ||
| 299 | - __ubuf__ DataTypeInT *firstTailAddr = nullptr; | ||
| 300 | - __ubuf__ DataTypeInT *secondTailAddr = nullptr; | ||
| 301 | - __ubuf__ bfloat16_t *swigluTailAddr1 = nullptr; | ||
| 302 | - __ubuf__ bfloat16_t *swigluTailAddr2 = nullptr; | ||
| 303 | - | ||
| 304 | - // 权重预取地址(nullptr 表示不启用 TopkWeightsPrefetch) | ||
| 305 | - __ubuf__ float *weightUbAddr = nullptr; | ||
| 306 | - | ||
| 307 | - // 激活参数(beta/alpha 默认 1.0,倒数同步 1.0,兜底为恒等变换) | ||
| 308 | - float clampLimit = 0.0f; | ||
| 309 | - float beta = 1.0f; | ||
| 310 | - float invBeta = 1.0f; | ||
| 311 | - float alpha = 1.0f; | ||
| 312 | - float invAlpha = 1.0f; | ||
| 313 | -}; | ||
| 314 | - | ||
| 315 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS | 310 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS |
| 316 | __aicore__ inline void BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Init(Params const ¶ms) | 311 | __aicore__ inline void BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Init(Params const ¶ms) |
| 317 | { | 312 | { |
| @@ -631,17 +626,17 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::ComputeDat | |||
| 631 | return; | 626 | return; |
| 632 | } | 627 | } |
| 633 | 628 | ||
| 634 | -// ============================================================================ | 629 | +/* |
| 635 | -// COMPUTE_TANH_TWOPATH: 两段式 tanh 宏(多项式路 + sigmoid 分解路) | 630 | + * COMPUTE_TANH_TWOPATH: 两段式 tanh 宏(多项式路 + sigmoid 分解路) |
| 636 | -// 多项式路(|x|<0.6): x*(1 + c1*x^2 + c2*x^4 + c3*x^6 + c4*x^8), Horner + FusedMulDstAdd | 631 | + * 多项式路(|x|<0.6): x*(1 + c1*x^2 + c2*x^4 + c3*x^6 + c4*x^8), Horner + FusedMulDstAdd |
| 637 | -// sigmoid路(|x|>=0.6): 2/(1+e^{-2x}) - 1, 天然保号无相消 | 632 | + * sigmoid路(|x|>=0.6): 2/(1+e^{-2x}) - 1, 天然保号无相消 |
| 638 | -// 系数来源: tanh_dag.h / situ_mx_quant_common.h | 633 | + * 系数来源: tanh_dag.h / situ_mx_quant_common.h |
| 639 | -// c1=-0.333327681, c2=0.133152977, c3=-0.0523039624, c4=0.0157396831 | 634 | + * c1=-0.333327681, c2=0.133152977, c3=-0.0523039624, c4=0.0157396831 |
| 640 | -// 用法: COMPUTE_TANH_TWOPATH(result, zReg, msk, oneReg, absReg, sqrReg, | 635 | + * 用法: COMPUTE_TANH_TWOPATH(result, zReg, msk, oneReg, absReg, sqrReg, |
| 641 | -// polyReg, tmpReg, expReg, c1Reg, c2Reg, cmpMask); | 636 | + * polyReg, tmpReg, expReg, c1Reg, c2Reg, cmpMask); |
| 642 | -// 注意: 宏在函数内展开,所用寄存器须在调用点函数作用域内声明; | 637 | + * 注意: 宏在函数内展开,所用寄存器须在调用点函数作用域内声明; |
| 643 | -// 宏会覆盖 sqrReg/polyReg/tmpReg/expReg/absReg/cmpMask,结果写入 result | 638 | + * 宏会覆盖 sqrReg/polyReg/tmpReg/expReg/absReg/cmpMask,结果写入 result |
| 644 | -// ============================================================================ | 639 | + */ |
| 645 | #define COMPUTE_TANH_TWOPATH(result, zReg, msk, oneReg, absReg, sqrReg, \ | 640 | #define COMPUTE_TANH_TWOPATH(result, zReg, msk, oneReg, absReg, sqrReg, \ |
| 646 | polyReg, tmpReg, expReg, c1Reg, c2Reg, cmpMask) \ | 641 | polyReg, tmpReg, expReg, c1Reg, c2Reg, cmpMask) \ |
| 647 | do { \ | 642 | do { \ |
| @@ -664,11 +659,11 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::ComputeDat | |||
| 664 | AscendC::MicroAPI::Select(result, tmpReg, polyReg, cmpMask); \ | 659 | AscendC::MicroAPI::Select(result, tmpReg, polyReg, cmpMask); \ |
| 665 | } while (0) | 660 | } while (0) |
| 666 | 661 | ||
| 667 | -// ============================================================================ | 662 | +/* |
| 668 | -// ActivationFlowSwiGLU: SwiGLU 激活完整计算流 | 663 | + * ActivationFlowSwiGLU: SwiGLU 激活完整计算流 |
| 669 | -// swish(x1) * x2, 其中 swish(x) = x / (exp(-x) + 1) = x * sigmoid(x) | 664 | + * swish(x1) * x2, 其中 swish(x) = x / (exp(-x) + 1) = x * sigmoid(x) |
| 670 | -// 含主循环 + 尾循环 + pad 填零,所有寄存器在本函数内声明 | 665 | + * 含主循环 + 尾循环 + pad 填零,所有寄存器在本函数内声明 |
| 671 | -// ============================================================================ | 666 | + */ |
| 672 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS | 667 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS |
| 673 | template <bool IsInterleaved> | 668 | template <bool IsInterleaved> |
| 674 | __aicore__ inline void | 669 | __aicore__ inline void |
| @@ -678,6 +673,13 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 678 | const float scalarOne = 1.0f; | 673 | const float scalarOne = 1.0f; |
| 679 | const float negScalarOne = -1.0f; | 674 | const float negScalarOne = -1.0f; |
| 680 | bfloat16_t numZero = 0; | 675 | bfloat16_t numZero = 0; |
| 676 | + uint32_t mask1Num = ctx.mask1Num; | ||
| 677 | + uint32_t mask2Num = ctx.mask2Num; | ||
| 678 | + uint32_t mask3Num = ctx.mask3Num; | ||
| 679 | + uint16_t dim0VfTimes = ctx.dim0VfTimes; | ||
| 680 | + uint16_t dim1VfTimes = ctx.dim1VfTimes; | ||
| 681 | + uint16_t dim1TailTimes = ctx.dim1TailTimes; | ||
| 682 | + uint16_t dim1Tail2 = ctx.dim1Tail2; | ||
| 681 | 683 | ||
| 682 | __VEC_SCOPE__ | 684 | __VEC_SCOPE__ |
| 683 | { | 685 | { |
| @@ -694,18 +696,15 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 694 | AscendC::MicroAPI::RegTensor<bfloat16_t> outTReg; | 696 | AscendC::MicroAPI::RegTensor<bfloat16_t> outTReg; |
| 695 | AscendC::MicroAPI::RegTensor<bfloat16_t> zeroReg; | 697 | AscendC::MicroAPI::RegTensor<bfloat16_t> zeroReg; |
| 696 | AscendC::MicroAPI::MaskReg mask = AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>(); | 698 | AscendC::MicroAPI::MaskReg mask = AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>(); |
| 697 | - uint32_t mask1Num = ctx.mask1Num; | ||
| 698 | - uint32_t mask2Num = ctx.mask2Num; | ||
| 699 | - uint32_t mask3Num = ctx.mask3Num; | ||
| 700 | AscendC::MicroAPI::MaskReg mask1 = AscendC::MicroAPI::UpdateMask<float>(mask1Num); | 699 | AscendC::MicroAPI::MaskReg mask1 = AscendC::MicroAPI::UpdateMask<float>(mask1Num); |
| 701 | AscendC::MicroAPI::MaskReg mask2 = AscendC::MicroAPI::UpdateMask<float>(mask2Num); | 700 | AscendC::MicroAPI::MaskReg mask2 = AscendC::MicroAPI::UpdateMask<float>(mask2Num); |
| 702 | AscendC::MicroAPI::MaskReg mask3 = AscendC::MicroAPI::UpdateMask<bfloat16_t>(mask3Num); | 701 | AscendC::MicroAPI::MaskReg mask3 = AscendC::MicroAPI::UpdateMask<bfloat16_t>(mask3Num); |
| 703 | - for (uint16_t dim0vfLoopIdx = 0; dim0vfLoopIdx < ctx.dim0VfTimes; dim0vfLoopIdx++) { | 702 | + for (uint16_t dim0vfLoopIdx = 0; dim0vfLoopIdx < dim0VfTimes; dim0vfLoopIdx++) { |
| 704 | if constexpr (TopkWeightsPrefetch) { | 703 | if constexpr (TopkWeightsPrefetch) { |
| 705 | AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>( | 704 | AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>( |
| 706 | weightReg, ctx.weightUbAddr + dim0vfLoopIdx * INT32_PER_256B + WEIGHT_INDEX); | 705 | weightReg, ctx.weightUbAddr + dim0vfLoopIdx * INT32_PER_256B + WEIGHT_INDEX); |
| 707 | } | 706 | } |
| 708 | - for (uint16_t dim1vfLoopIdx = 0; dim1vfLoopIdx < ctx.dim1VfTimes; dim1vfLoopIdx++) { | 707 | + for (uint16_t dim1vfLoopIdx = 0; dim1vfLoopIdx < dim1VfTimes; dim1vfLoopIdx++) { |
| 709 | AscendC::MicroAPI::AddrReg srcIdxOffset = AscendC::MicroAPI::CreateAddrReg<DataTypeIn>( | 708 | AscendC::MicroAPI::AddrReg srcIdxOffset = AscendC::MicroAPI::CreateAddrReg<DataTypeIn>( |
| 710 | dim0vfLoopIdx, ctx.nSrcUbAligned, dim1vfLoopIdx, VF_LEN_FP32); | 709 | dim0vfLoopIdx, ctx.nSrcUbAligned, dim1vfLoopIdx, VF_LEN_FP32); |
| 711 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( | 710 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( |
| @@ -741,7 +740,7 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 741 | AscendC::MicroAPI::CreateAddrReg<DataTypeIn>(dim0vfLoopIdx, ctx.nSrcUbAligned); | 740 | AscendC::MicroAPI::CreateAddrReg<DataTypeIn>(dim0vfLoopIdx, ctx.nSrcUbAligned); |
| 742 | AscendC::MicroAPI::AddrReg outOffset1 = | 741 | AscendC::MicroAPI::AddrReg outOffset1 = |
| 743 | AscendC::MicroAPI::CreateAddrReg<bfloat16_t>(dim0vfLoopIdx, ctx.nDstUbAligned); | 742 | AscendC::MicroAPI::CreateAddrReg<bfloat16_t>(dim0vfLoopIdx, ctx.nDstUbAligned); |
| 744 | - for (uint16_t aa = 0; aa < ctx.dim1TailTimes; aa++) { | 743 | + for (uint16_t tailIdx = 0; tailIdx < dim1TailTimes; tailIdx++) { |
| 745 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( | 744 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( |
| 746 | vregX1, ctx.firstTailAddr, srcIdxOffset1); | 745 | vregX1, ctx.firstTailAddr, srcIdxOffset1); |
| 747 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( | 746 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( |
| @@ -769,7 +768,7 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 769 | AscendC::MicroAPI::DataCopy<bfloat16_t, AscendC::MicroAPI::StoreDist::DIST_PACK_B32>( | 768 | AscendC::MicroAPI::DataCopy<bfloat16_t, AscendC::MicroAPI::StoreDist::DIST_PACK_B32>( |
| 770 | ctx.swigluTailAddr1, outTReg, outOffset1, mask2); | 769 | ctx.swigluTailAddr1, outTReg, outOffset1, mask2); |
| 771 | } | 770 | } |
| 772 | - for (uint16_t cc = 0; cc < ctx.dim1Tail2; cc++) { | 771 | + for (uint16_t padIdx = 0; padIdx < dim1Tail2; padIdx++) { |
| 773 | AscendC::MicroAPI::Duplicate(zeroReg, numZero); | 772 | AscendC::MicroAPI::Duplicate(zeroReg, numZero); |
| 774 | AscendC::MicroAPI::DataCopy<bfloat16_t>(ctx.swigluTailAddr2, zeroReg, outOffset1, mask3); | 773 | AscendC::MicroAPI::DataCopy<bfloat16_t>(ctx.swigluTailAddr2, zeroReg, outOffset1, mask3); |
| 775 | } | 774 | } |
| @@ -777,26 +776,34 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 777 | } | 776 | } |
| 778 | } | 777 | } |
| 779 | 778 | ||
| 780 | -// ============================================================================ | 779 | +/* |
| 781 | -// ActivationFlowSiTU: SiTU 激活完整计算流 | 780 | + * ActivationFlowSiTU: SiTU 激活完整计算流 |
| 782 | -// situ_a = beta * tanh(gate/beta) * sigmoid(gate) | 781 | + * situ_a = beta * tanh(gate/beta) * sigmoid(gate) |
| 783 | -// ActSub==LINEAR: out = situ_a * (alpha * tanh(up/alpha)) | 782 | + * ActSub==LINEAR: out = situ_a * (alpha * tanh(up/alpha)) |
| 784 | -// ActSub==DEFAULT: out = situ_a * up | 783 | + * ActSub==DEFAULT: out = situ_a * up |
| 785 | -// tanh 采用两段式(多项式路 + sigmoid 分解路),保号无相消 | 784 | + * tanh 采用两段式(多项式路 + sigmoid 分解路),保号无相消 |
| 786 | -// 参照 situ_mx_quant_common.h::ComputeVfSitu,含主循环 + 尾循环 + pad 填零 | 785 | + * 参照 situ_mx_quant_common.h::ComputeVfSitu,含主循环 + 尾循环 + pad 填零 |
| 787 | -// ============================================================================ | 786 | + */ |
| 788 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS | 787 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS |
| 789 | template <ActSubMode ActSub, bool IsInterleaved> | 788 | template <ActSubMode ActSub, bool IsInterleaved> |
| 790 | __aicore__ inline void | 789 | __aicore__ inline void |
| 791 | BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::ActivationFlowSiTU( | 790 | BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::ActivationFlowSiTU( |
| 792 | const ActivationContext<DataTypeIn> &ctx) | 791 | const ActivationContext<DataTypeIn> &ctx) |
| 793 | { | 792 | { |
| 794 | - // 标量常量 | 793 | + // tanh 多项式近似的奇次项系数(8 阶 Padé 近似,仅取奇次项) |
| 795 | - const float tanhC1 = -0.333327681f; | 794 | + // 参见 COMPUTE_TANH_TWOPATH 宏注释,c1/c2 对应 x^3/x^5 的系数 |
| 796 | - const float tanhC2 = 0.133152977f; | 795 | + const float tanhC1 = -0.333327681f; // c1: tanh 多项式 x^3 项系数,约 -1/3 |
| 796 | + const float tanhC2 = 0.133152977f; // c2: tanh 多项式 x^5 项系数,约 2/15 | ||
| 797 | const float scalarOne = 1.0f; | 797 | const float scalarOne = 1.0f; |
| 798 | const float negScalarOne = -1.0f; | 798 | const float negScalarOne = -1.0f; |
| 799 | bfloat16_t numZero = 0; | 799 | bfloat16_t numZero = 0; |
| 800 | + uint32_t mask1Num = ctx.mask1Num; | ||
| 801 | + uint32_t mask2Num = ctx.mask2Num; | ||
| 802 | + uint32_t mask3Num = ctx.mask3Num; | ||
| 803 | + uint16_t dim0VfTimes = ctx.dim0VfTimes; | ||
| 804 | + uint16_t dim1VfTimes = ctx.dim1VfTimes; | ||
| 805 | + uint16_t dim1TailTimes = ctx.dim1TailTimes; | ||
| 806 | + uint16_t dim1Tail2 = ctx.dim1Tail2; | ||
| 800 | 807 | ||
| 801 | __VEC_SCOPE__ | 808 | __VEC_SCOPE__ |
| 802 | { | 809 | { |
| @@ -829,9 +836,6 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 829 | AscendC::MicroAPI::MaskReg cmpMaskReg; | 836 | AscendC::MicroAPI::MaskReg cmpMaskReg; |
| 830 | 837 | ||
| 831 | AscendC::MicroAPI::MaskReg mask = AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>(); | 838 | AscendC::MicroAPI::MaskReg mask = AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>(); |
| 832 | - uint32_t mask1Num = ctx.mask1Num; | ||
| 833 | - uint32_t mask2Num = ctx.mask2Num; | ||
| 834 | - uint32_t mask3Num = ctx.mask3Num; | ||
| 835 | AscendC::MicroAPI::MaskReg mask1 = AscendC::MicroAPI::UpdateMask<float>(mask1Num); | 839 | AscendC::MicroAPI::MaskReg mask1 = AscendC::MicroAPI::UpdateMask<float>(mask1Num); |
| 836 | AscendC::MicroAPI::MaskReg mask2 = AscendC::MicroAPI::UpdateMask<float>(mask2Num); | 840 | AscendC::MicroAPI::MaskReg mask2 = AscendC::MicroAPI::UpdateMask<float>(mask2Num); |
| 837 | AscendC::MicroAPI::MaskReg mask3 = AscendC::MicroAPI::UpdateMask<bfloat16_t>(mask3Num); | 841 | AscendC::MicroAPI::MaskReg mask3 = AscendC::MicroAPI::UpdateMask<bfloat16_t>(mask3Num); |
| @@ -841,12 +845,12 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 841 | AscendC::MicroAPI::Duplicate(c1Reg, tanhC1); | 845 | AscendC::MicroAPI::Duplicate(c1Reg, tanhC1); |
| 842 | AscendC::MicroAPI::Duplicate(c2Reg, tanhC2); | 846 | AscendC::MicroAPI::Duplicate(c2Reg, tanhC2); |
| 843 | 847 | ||
| 844 | - for (uint16_t dim0vfLoopIdx = 0; dim0vfLoopIdx < ctx.dim0VfTimes; dim0vfLoopIdx++) { | 848 | + for (uint16_t dim0vfLoopIdx = 0; dim0vfLoopIdx < dim0VfTimes; dim0vfLoopIdx++) { |
| 845 | if constexpr (TopkWeightsPrefetch) { | 849 | if constexpr (TopkWeightsPrefetch) { |
| 846 | AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>( | 850 | AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>( |
| 847 | weightReg, ctx.weightUbAddr + dim0vfLoopIdx * INT32_PER_256B + WEIGHT_INDEX); | 851 | weightReg, ctx.weightUbAddr + dim0vfLoopIdx * INT32_PER_256B + WEIGHT_INDEX); |
| 848 | } | 852 | } |
| 849 | - for (uint16_t dim1vfLoopIdx = 0; dim1vfLoopIdx < ctx.dim1VfTimes; dim1vfLoopIdx++) { | 853 | + for (uint16_t dim1vfLoopIdx = 0; dim1vfLoopIdx < dim1VfTimes; dim1vfLoopIdx++) { |
| 850 | AscendC::MicroAPI::AddrReg srcIdxOffset = AscendC::MicroAPI::CreateAddrReg<DataTypeIn>( | 854 | AscendC::MicroAPI::AddrReg srcIdxOffset = AscendC::MicroAPI::CreateAddrReg<DataTypeIn>( |
| 851 | dim0vfLoopIdx, ctx.nSrcUbAligned, dim1vfLoopIdx, VF_LEN_FP32); | 855 | dim0vfLoopIdx, ctx.nSrcUbAligned, dim1vfLoopIdx, VF_LEN_FP32); |
| 852 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( | 856 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( |
| @@ -905,7 +909,7 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 905 | AscendC::MicroAPI::CreateAddrReg<DataTypeIn>(dim0vfLoopIdx, ctx.nSrcUbAligned); | 909 | AscendC::MicroAPI::CreateAddrReg<DataTypeIn>(dim0vfLoopIdx, ctx.nSrcUbAligned); |
| 906 | AscendC::MicroAPI::AddrReg outOffset1 = | 910 | AscendC::MicroAPI::AddrReg outOffset1 = |
| 907 | AscendC::MicroAPI::CreateAddrReg<bfloat16_t>(dim0vfLoopIdx, ctx.nDstUbAligned); | 911 | AscendC::MicroAPI::CreateAddrReg<bfloat16_t>(dim0vfLoopIdx, ctx.nDstUbAligned); |
| 908 | - for (uint16_t aa = 0; aa < ctx.dim1TailTimes; aa++) { | 912 | + for (uint16_t tailIdx = 0; tailIdx < dim1TailTimes; tailIdx++) { |
| 909 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( | 913 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( |
| 910 | vregX1, ctx.firstTailAddr, srcIdxOffset1); | 914 | vregX1, ctx.firstTailAddr, srcIdxOffset1); |
| 911 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( | 915 | AscendC::MicroAPI::DataCopy<DataTypeIn, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>( |
| @@ -948,7 +952,7 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 948 | AscendC::MicroAPI::DataCopy<bfloat16_t, AscendC::MicroAPI::StoreDist::DIST_PACK_B32>( | 952 | AscendC::MicroAPI::DataCopy<bfloat16_t, AscendC::MicroAPI::StoreDist::DIST_PACK_B32>( |
| 949 | ctx.swigluTailAddr1, outTReg, outOffset1, mask2); | 953 | ctx.swigluTailAddr1, outTReg, outOffset1, mask2); |
| 950 | } | 954 | } |
| 951 | - for (uint16_t cc = 0; cc < ctx.dim1Tail2; cc++) { | 955 | + for (uint16_t padIdx = 0; padIdx < dim1Tail2; padIdx++) { |
| 952 | AscendC::MicroAPI::Duplicate(zeroReg, numZero); | 956 | AscendC::MicroAPI::Duplicate(zeroReg, numZero); |
| 953 | AscendC::MicroAPI::DataCopy<bfloat16_t>(ctx.swigluTailAddr2, zeroReg, outOffset1, mask3); | 957 | AscendC::MicroAPI::DataCopy<bfloat16_t>(ctx.swigluTailAddr2, zeroReg, outOffset1, mask3); |
| 954 | } | 958 | } |
| @@ -956,13 +960,13 @@ BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::Activation | |||
| 956 | } | 960 | } |
| 957 | } | 961 | } |
| 958 | 962 | ||
| 959 | -// ============================================================================ | 963 | +/* |
| 960 | -// VFDoSwigluAndQuantForMX: 激活 + MX 量化主入口(重构后为分派器) | 964 | + * VFDoSwigluAndQuantForMX: 激活 + MX 量化主入口(重构后为分派器) |
| 961 | -// 1. 标量分块计算(对齐、tail、mask) | 965 | + * 1. 标量分块计算(对齐、tail、mask) |
| 962 | -// 2. 填充 ActivationContext | 966 | + * 2. 填充 ActivationContext |
| 963 | -// 3. 按模板参数 Activation 分派到 ActivationFlowSwiGLU / ActivationFlowSiTU | 967 | + * 3. 按模板参数 Activation 分派到 ActivationFlowSwiGLU / ActivationFlowSiTU |
| 964 | -// 4. MX 量化调度(ComputeMaxExp / ComputeScale / ComputeDataForQuantTargetFp8/Fp4) | 968 | + * 4. MX 量化调度(ComputeMaxExp / ComputeScale / ComputeDataForQuantTargetFp8/Fp4) |
| 965 | -// ============================================================================ | 969 | + */ |
| 966 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS | 970 | BLOCK_EPILOGUE_SWIGLU_QUANT_CLASS_LOCAL_PARAMS |
| 967 | template <SwigluQuantMsg::QuantMode quantMode, bool IsInterleavedSrc, ActMode Activation, ActSubMode ActSub> | 971 | template <SwigluQuantMsg::QuantMode quantMode, bool IsInterleavedSrc, ActMode Activation, ActSubMode ActSub> |
| 968 | __aicore__ inline void BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::VFDoSwigluAndQuantForMX( | 972 | __aicore__ inline void BlockEpilogueSwigluMxQuant<BLOCK_EPILOGUE_DEQUANT_FUNC_LOCAL_PARAMS>::VFDoSwigluAndQuantForMX( |
| @@ -72,7 +72,8 @@ TEST_F(MegaMoeArch35TilingTest, H4096_BS128_FP8E4M3FN) | |||
| 72 | {"comm_alg", Ops::Transformer::AnyValue::CreateFrom<std::string>("")}, | 72 | {"comm_alg", Ops::Transformer::AnyValue::CreateFrom<std::string>("")}, |
| 73 | {"num_max_tokens_per_rank", Ops::Transformer::AnyValue::CreateFrom<int64_t>(0)}, | 73 | {"num_max_tokens_per_rank", Ops::Transformer::AnyValue::CreateFrom<int64_t>(0)}, |
| 74 | {"activation", Ops::Transformer::AnyValue::CreateFrom<std::string>("swiglu")}, | 74 | {"activation", Ops::Transformer::AnyValue::CreateFrom<std::string>("swiglu")}, |
| 75 | - {"activation_params", Ops::Transformer::AnyValue::CreateFrom<std::vector<float>>({})}, | 75 | + {"activation_params", |
| 76 | + Ops::Transformer::AnyValue::CreateFrom<std::vector<float>>({std::numeric_limits<float>::max()})}, | ||
| 76 | {"activation_out_dtype", | 77 | {"activation_out_dtype", |
| 77 | Ops::Transformer::AnyValue::CreateFrom<int64_t>(static_cast<int64_t>(ge::DT_UNDEFINED))}, | 78 | Ops::Transformer::AnyValue::CreateFrom<int64_t>(static_cast<int64_t>(ge::DT_UNDEFINED))}, |
| 78 | {"transpose_weight1", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 79 | {"transpose_weight1", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| @@ -356,7 +356,6 @@ def get_symm_buffer_for_mega_moe( | |||
| 356 | dispatch_quant_out_dtype: Optional[torch.dtype] = None, | 356 | dispatch_quant_out_dtype: Optional[torch.dtype] = None, |
| 357 | combine_quant_mode: int = 0, | 357 | combine_quant_mode: int = 0, |
| 358 | comm_alg: str = "", | 358 | comm_alg: str = "", |
| 359 | - use_ccl_buffer: bool = True, | ||
| 360 | topk_weights_type: int = 0, | 359 | topk_weights_type: int = 0, |
| 361 | ) -> SymmBuffer: | 360 | ) -> SymmBuffer: |
| 362 | return SymmBuffer( | 361 | return SymmBuffer( |
可以确认一下 这个should be 在宏里有没有