已合并
feat(moe_ep_combine): 拆分combine为moe_ep_combine和moe_ep_combine_epilogue #10249
jvjvzhilian创建于 8月18日
feat(moe_ep_combine): 拆分combine为moe_ep_combine和moe_ep_combine_epilogue #10249
已合并
共 17 个文件变更+1140-321
| @@ -671,6 +671,10 @@ mc2_moe_advanced@ops-transformer: | |||
| 671 | - ops/ops-transformer/mc2/moe_ep_combine/op_api/ | 671 | - ops/ops-transformer/mc2/moe_ep_combine/op_api/ |
| 672 | - ops/ops-transformer/mc2/moe_ep_combine/op_kernel/ | 672 | - ops/ops-transformer/mc2/moe_ep_combine/op_kernel/ |
| 673 | - ops/ops-transformer/mc2/moe_ep_combine/CMakeLists.txt | 673 | - ops/ops-transformer/mc2/moe_ep_combine/CMakeLists.txt |
| 674 | + - ops/ops-transformer/mc2/moe_ep_combine_epilogue/op_host/ | ||
| 675 | + - ops/ops-transformer/mc2/moe_ep_combine_epilogue/op_api/ | ||
| 676 | + - ops/ops-transformer/mc2/moe_ep_combine_epilogue/op_kernel/ | ||
| 677 | + - ops/ops-transformer/mc2/moe_ep_combine_epilogue/CMakeLists.txt | ||
| 674 | - ops/ops-transformer/mc2/moe_ep_dispatch/op_host/ | 678 | - ops/ops-transformer/mc2/moe_ep_dispatch/op_host/ |
| 675 | - ops/ops-transformer/mc2/moe_ep_dispatch/op_api/ | 679 | - ops/ops-transformer/mc2/moe_ep_dispatch/op_api/ |
| 676 | - ops/ops-transformer/mc2/moe_ep_dispatch/op_kernel/ | 680 | - ops/ops-transformer/mc2/moe_ep_dispatch/op_kernel/ |
| @@ -22,15 +22,13 @@ constexpr int64_t NETWORK_HYBRID = 1; | |||
| 22 | } // namespace | 22 | } // namespace |
| 23 | 23 | ||
| 24 | static aclnnStatus CheckNotNull(const aclTensor *context, const aclTensor *x, const aclTensor *topkIdx, | 24 | static aclnnStatus CheckNotNull(const aclTensor *context, const aclTensor *x, const aclTensor *topkIdx, |
| 25 | - const aclTensor *recvSrcMetadata, const aclTensor *numRecvTokensPerExpert, | 25 | + const aclTensor *recvSrcMetadata, const aclTensor *numRecvTokensPerExpert) |
| 26 | - aclTensor *combinedX) | ||
| 27 | { | 26 | { |
| 28 | CHECK_RET(context != nullptr, ACLNN_ERR_PARAM_NULLPTR); | 27 | CHECK_RET(context != nullptr, ACLNN_ERR_PARAM_NULLPTR); |
| 29 | CHECK_RET(x != nullptr, ACLNN_ERR_PARAM_NULLPTR); | 28 | CHECK_RET(x != nullptr, ACLNN_ERR_PARAM_NULLPTR); |
| 30 | CHECK_RET(topkIdx != nullptr, ACLNN_ERR_PARAM_NULLPTR); | 29 | CHECK_RET(topkIdx != nullptr, ACLNN_ERR_PARAM_NULLPTR); |
| 31 | CHECK_RET(recvSrcMetadata != nullptr, ACLNN_ERR_PARAM_NULLPTR); | 30 | CHECK_RET(recvSrcMetadata != nullptr, ACLNN_ERR_PARAM_NULLPTR); |
| 32 | CHECK_RET(numRecvTokensPerExpert != nullptr, ACLNN_ERR_PARAM_NULLPTR); | 31 | CHECK_RET(numRecvTokensPerExpert != nullptr, ACLNN_ERR_PARAM_NULLPTR); |
| 33 | - CHECK_RET(combinedX != nullptr, ACLNN_ERR_PARAM_NULLPTR); | ||
| 34 | return ACLNN_SUCCESS; | 32 | return ACLNN_SUCCESS; |
| 35 | } | 33 | } |
| 36 | 34 | ||
| @@ -61,35 +59,31 @@ aclnnStatus MoeEpCombineGetWorkspaceSize(const aclTensor *context, const aclTens | |||
| 61 | const aclTensor *recvSrcMetadata, const aclTensor *numRecvTokensPerExpert, | 59 | const aclTensor *recvSrcMetadata, const aclTensor *numRecvTokensPerExpert, |
| 62 | const aclTensor *topkWeights, int64_t epWorldSize, int64_t epRankId, | 60 | const aclTensor *topkWeights, int64_t epWorldSize, int64_t epRankId, |
| 63 | int64_t numExperts, int64_t numMaxTokensPerRank, int64_t cclBufferSize, | 61 | int64_t numExperts, int64_t numMaxTokensPerRank, int64_t cclBufferSize, |
| 64 | - int64_t topoType, int64_t rankNumPerServer, | 62 | + int64_t topoType, int64_t rankNumPerServer, uint64_t *workspaceSize, |
| 65 | - aclTensor *combinedX, aclTensor *combinedTopkWeights, | 63 | + aclOpExecutor **executor) |
| 66 | - uint64_t *workspaceSize, aclOpExecutor **executor) | ||
| 67 | { | 64 | { |
| 68 | OP_LOGD("MoeEpCombine", "Begin to do MoeEpCombineGetWorkspaceSize"); | 65 | OP_LOGD("MoeEpCombine", "Begin to do MoeEpCombineGetWorkspaceSize"); |
| 69 | - auto retNotNull = CheckNotNull(context, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, combinedX); | 66 | + auto retNotNull = CheckNotNull(context, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert); |
| 70 | CHECK_RET(retNotNull == ACLNN_SUCCESS, retNotNull); | 67 | CHECK_RET(retNotNull == ACLNN_SUCCESS, retNotNull); |
| 71 | - auto retParams = CheckParams(epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, topoType, | 68 | + auto retParams = |
| 72 | - rankNumPerServer); | 69 | + CheckParams(epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, topoType, rankNumPerServer); |
| 73 | CHECK_RET(retParams == ACLNN_SUCCESS, retParams); | 70 | CHECK_RET(retParams == ACLNN_SUCCESS, retParams); |
| 74 | 71 | ||
| 75 | return aclnnInnerMoeEpCombineGetWorkspaceSize(context, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, | 72 | return aclnnInnerMoeEpCombineGetWorkspaceSize(context, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, |
| 76 | topkWeights, epWorldSize, epRankId, numExperts, numMaxTokensPerRank, | 73 | topkWeights, epWorldSize, epRankId, numExperts, numMaxTokensPerRank, |
| 77 | - cclBufferSize, topoType, rankNumPerServer, combinedX, | 74 | + cclBufferSize, topoType, rankNumPerServer, workspaceSize, executor); |
| 78 | - combinedTopkWeights, workspaceSize, executor); | ||
| 79 | } | 75 | } |
| 80 | 76 | ||
| 81 | aclnnStatus aclnnMoeEpCombineGetWorkspaceSize(const aclTensor *context, const aclTensor *x, const aclTensor *topkIdx, | 77 | aclnnStatus aclnnMoeEpCombineGetWorkspaceSize(const aclTensor *context, const aclTensor *x, const aclTensor *topkIdx, |
| 82 | const aclTensor *recvSrcMetadata, const aclTensor *numRecvTokensPerExpert, | 78 | const aclTensor *recvSrcMetadata, const aclTensor *numRecvTokensPerExpert, |
| 83 | const aclTensor *topkWeights, int64_t epWorldSize, int64_t epRankId, | 79 | const aclTensor *topkWeights, int64_t epWorldSize, int64_t epRankId, |
| 84 | int64_t numExperts, int64_t numMaxTokensPerRank, int64_t cclBufferSize, | 80 | int64_t numExperts, int64_t numMaxTokensPerRank, int64_t cclBufferSize, |
| 85 | - int64_t topoType, int64_t rankNumPerServer, | 81 | + int64_t topoType, int64_t rankNumPerServer, uint64_t *workspaceSize, |
| 86 | - aclTensor *combinedX, aclTensor *combinedTopkWeights, | 82 | + aclOpExecutor **executor) |
| 87 | - uint64_t *workspaceSize, aclOpExecutor **executor) | ||
| 88 | { | 83 | { |
| 89 | return MoeEpCombineGetWorkspaceSize(context, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, topkWeights, | 84 | return MoeEpCombineGetWorkspaceSize(context, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, topkWeights, |
| 90 | - epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, | 85 | + epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, topoType, |
| 91 | - topoType, rankNumPerServer, combinedX, combinedTopkWeights, workspaceSize, | 86 | + rankNumPerServer, workspaceSize, executor); |
| 92 | - executor); | ||
| 93 | } | 87 | } |
| 94 | 88 | ||
| 95 | extern "C" void __attribute__((weak)) NnopbaseSetHcclServerType(void *executor, NnopbaseHcclServerType sType); | 89 | extern "C" void __attribute__((weak)) NnopbaseSetHcclServerType(void *executor, NnopbaseHcclServerType sType); |
| @@ -14,7 +14,8 @@ namespace ops { | |||
| 14 | 14 | ||
| 15 | class MoeEpCombine : public OpDef { | 15 | class MoeEpCombine : public OpDef { |
| 16 | public: | 16 | public: |
| 17 | - explicit MoeEpCombine(const char *name) : OpDef(name) | 17 | + explicit MoeEpCombine(const char *name) |
| 18 | + : OpDef(name) | ||
| 18 | { | 19 | { |
| 19 | this->Input("context") | 20 | this->Input("context") |
| 20 | .ParamType(REQUIRED) | 21 | .ParamType(REQUIRED) |
| @@ -47,15 +48,6 @@ public: | |||
| 47 | .FormatList({ge::FORMAT_ND}) | 48 | .FormatList({ge::FORMAT_ND}) |
| 48 | .AutoContiguous(); | 49 | .AutoContiguous(); |
| 49 | 50 | ||
| 50 | - this->Output("combined_x") | ||
| 51 | - .ParamType(REQUIRED) | ||
| 52 | - .DataTypeList({ge::DT_BF16, ge::DT_FLOAT16}) | ||
| 53 | - .Format({ge::FORMAT_ND}); | ||
| 54 | - this->Output("combined_topk_weights") | ||
| 55 | - .ParamType(OPTIONAL) | ||
| 56 | - .DataType({ge::DT_FLOAT}) | ||
| 57 | - .Format({ge::FORMAT_ND}); | ||
| 58 | - | ||
| 59 | this->Attr("ep_world_size").AttrType(REQUIRED).Int(); | 51 | this->Attr("ep_world_size").AttrType(REQUIRED).Int(); |
| 60 | this->Attr("ep_rank_id").AttrType(REQUIRED).Int(); | 52 | this->Attr("ep_rank_id").AttrType(REQUIRED).Int(); |
| 61 | this->Attr("num_experts").AttrType(REQUIRED).Int(); | 53 | this->Attr("num_experts").AttrType(REQUIRED).Int(); |
| @@ -40,8 +40,8 @@ using namespace AscendC; | |||
| 40 | 40 | ||
| 41 | template <uint32_t HasTopkWeight, uint32_t ArchTag> | 41 | template <uint32_t HasTopkWeight, uint32_t ArchTag> |
| 42 | __global__ __aicore__ void moe_ep_combine(GM_ADDR context, GM_ADDR x, GM_ADDR topkIdx, GM_ADDR recvSrcMetadata, | 42 | __global__ __aicore__ void moe_ep_combine(GM_ADDR context, GM_ADDR x, GM_ADDR topkIdx, GM_ADDR recvSrcMetadata, |
| 43 | - GM_ADDR numRecvPerExpert, GM_ADDR topkWeights, GM_ADDR combinedX, | 43 | + GM_ADDR numRecvPerExpert, GM_ADDR topkWeights, GM_ADDR workspace, |
| 44 | - GM_ADDR combinedTopkWeights, GM_ADDR workspace, GM_ADDR tilingGM) | 44 | + GM_ADDR tilingGM) |
| 45 | { | 45 | { |
| 46 | REGISTER_TILING_DEFAULT(MoeEpCombineTilingData); | 46 | REGISTER_TILING_DEFAULT(MoeEpCombineTilingData); |
| 47 | REGISTER_TILING_FOR_TILINGKEY("ArchTag == TILINGKEY_TPL_A5", MoeEpCombineTilingData); | 47 | REGISTER_TILING_FOR_TILINGKEY("ArchTag == TILINGKEY_TPL_A5", MoeEpCombineTilingData); |
| @@ -49,8 +49,8 @@ __global__ __aicore__ void moe_ep_combine(GM_ADDR context, GM_ADDR x, GM_ADDR to | |||
| 49 | GET_TILING_DATA_WITH_STRUCT(MoeEpCombineTilingData, tilingData, tilingGM); | 49 | GET_TILING_DATA_WITH_STRUCT(MoeEpCombineTilingData, tilingData, tilingGM); |
| 50 | #if defined(ENABLE_MOE_EP_COMBINE_KERNEL) | 50 | #if defined(ENABLE_MOE_EP_COMBINE_KERNEL) |
| 51 | MoeEpCombine<DTYPE_X, HasTopkWeight> op; | 51 | MoeEpCombine<DTYPE_X, HasTopkWeight> op; |
| 52 | - op.Init(context, x, topkIdx, recvSrcMetadata, numRecvPerExpert, topkWeights, combinedX, combinedTopkWeights, | 52 | + op.Init(context, x, topkIdx, recvSrcMetadata, numRecvPerExpert, topkWeights, tilingGM, &pipe, |
| 53 | - workspace, tilingGM, &pipe, &tilingData.moeEpCombineInfo); | 53 | + &tilingData.moeEpCombineInfo); |
| 54 | op.Process(); | 54 | op.Process(); |
| 55 | #endif | 55 | #endif |
| 56 | } | 56 | } |
| @@ -10,7 +10,9 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file moe_ep_combine.h | 12 | * \file moe_ep_combine.h |
| 13 | - * \brief MoE Expert-Parallel Combine kernel implementation | 13 | + * \brief MoE Expert-Parallel Combine kernel implementation — send phase only. |
| 14 | + * Sends expert output tokens to remote ranks' HCCL Window buffers. | ||
| 15 | + * The recv+reduce phase is handled by moe_ep_combine_epilogue. | ||
| 14 | */ | 16 | */ |
| 15 | #ifndef MOE_EP_COMBINE_H | 17 | #ifndef MOE_EP_COMBINE_H |
| 16 | #define MOE_EP_COMBINE_H | 18 | #define MOE_EP_COMBINE_H |
| @@ -52,10 +54,6 @@ | |||
| 52 | #include "moe_ep_combine_base.h" | 54 | #include "moe_ep_combine_base.h" |
| 53 | #include "moe_ep_combine_tiling.h" | 55 | #include "moe_ep_combine_tiling.h" |
| 54 | 56 | ||
| 55 | -#ifndef ALIGN_UP | ||
| 56 | -#define ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1)) | ||
| 57 | -#endif | ||
| 58 | - | ||
| 59 | namespace MoeEpCombineImpl { | 57 | namespace MoeEpCombineImpl { |
| 60 | 58 | ||
| 61 | #if defined(ENABLE_MOE_EP_COMBINE_KERNEL) | 59 | #if defined(ENABLE_MOE_EP_COMBINE_KERNEL) |
| @@ -69,12 +67,9 @@ using namespace AscendC; | |||
| 69 | static constexpr uint32_t WIN_ADDR_ALIGN = 512; | 67 | static constexpr uint32_t WIN_ADDR_ALIGN = 512; |
| 70 | static constexpr uint32_t RECV_META_FIELDS = 4; | 68 | static constexpr uint32_t RECV_META_FIELDS = 4; |
| 71 | constexpr uint64_t UB_ALIGN = 32UL; | 69 | constexpr uint64_t UB_ALIGN = 32UL; |
| 72 | -constexpr uint32_t COMBINE_STATE_OFFSET = 64U * 1024U; // 本卡状态空间偏移地址,前面的地址给dispatch用 | ||
| 73 | -constexpr uint32_t STATE_OFFSET = 32U; | ||
| 74 | -constexpr uint32_t DCCI_OFFSET = 64U; | ||
| 75 | constexpr uint64_t ALIGNED_LEN_256 = 256UL; | 70 | constexpr uint64_t ALIGNED_LEN_256 = 256UL; |
| 76 | constexpr uint32_t FLOAT_PER_UB_ALIGN = 8U; | 71 | constexpr uint32_t FLOAT_PER_UB_ALIGN = 8U; |
| 77 | -constexpr uint32_t DOUBLE_BUFFER_NUM = 2U; | 72 | +constexpr uint32_t SEND_DOUBLE_BUFFER_NUM = 2U; |
| 78 | static constexpr struct UrmaWqeEntry DEFAULT_WQE_CONFIG = {.odr = 5, .fence = 1, .se = 0, .cqe = 0, .inlineEn = 0}; | 73 | static constexpr struct UrmaWqeEntry DEFAULT_WQE_CONFIG = {.odr = 5, .fence = 1, .se = 0, .cqe = 0, .inlineEn = 0}; |
| 79 | 74 | ||
| 80 | template <TemplateMoeEpCombineTypeClass> | 75 | template <TemplateMoeEpCombineTypeClass> |
| @@ -83,27 +78,18 @@ public: | |||
| 83 | __aicore__ inline MoeEpCombine(){}; | 78 | __aicore__ inline MoeEpCombine(){}; |
| 84 | 79 | ||
| 85 | __aicore__ inline void Init(GM_ADDR context, GM_ADDR x, GM_ADDR topkIdx, GM_ADDR recvSrcMetadata, | 80 | __aicore__ inline void Init(GM_ADDR context, GM_ADDR x, GM_ADDR topkIdx, GM_ADDR recvSrcMetadata, |
| 86 | - GM_ADDR numRecvPerExpert, GM_ADDR topkWeights, GM_ADDR combinedX, | 81 | + GM_ADDR numRecvPerExpert, GM_ADDR topkWeights, GM_ADDR tilingGM, TPipe *pipe, |
| 87 | - GM_ADDR combinedTopkWeights, GM_ADDR workspace, GM_ADDR tilingGM, TPipe *pipe, | ||
| 88 | const MoeEpCombineInfo *tilingData); | 82 | const MoeEpCombineInfo *tilingData); |
| 89 | 83 | ||
| 90 | __aicore__ inline void Process(); | 84 | __aicore__ inline void Process(); |
| 91 | 85 | ||
| 92 | private: | 86 | private: |
| 93 | - __aicore__ inline void SplitToCore(uint32_t curSendCnt, uint32_t curUseAivNum, uint32_t &startTokenId, | ||
| 94 | - uint32_t &endTokenId, uint32_t &tokenPerAivNum); | ||
| 95 | __aicore__ inline void SendLocalToken(uint32_t tokenIndex, GM_ADDR dstAddr); | 87 | __aicore__ inline void SendLocalToken(uint32_t tokenIndex, GM_ADDR dstAddr); |
| 96 | __aicore__ inline void SendSlot(uint32_t tokenIndex, int32_t srcRank, int32_t srcTokenIdx, int32_t srcTopKIdx, | 88 | __aicore__ inline void SendSlot(uint32_t tokenIndex, int32_t srcRank, int32_t srcTokenIdx, int32_t srcTopKIdx, |
| 97 | uint32_t channelIndex, uint32_t weight); | 89 | uint32_t channelIndex, uint32_t weight); |
| 98 | - __aicore__ inline bool WaitDispatch(uint32_t tokenIndex, uint32_t copyCount); | ||
| 99 | - __aicore__ inline void ProcessTopKToken(uint32_t tokenIndex); | ||
| 100 | __aicore__ inline void SendPhaseExpertToToken(); | 90 | __aicore__ inline void SendPhaseExpertToToken(); |
| 101 | __aicore__ inline void GetCoreAssignment(uint32_t totalBlocks, uint32_t &targetRank, uint32_t &coreIndexInGroup, | 91 | __aicore__ inline void GetCoreAssignment(uint32_t totalBlocks, uint32_t &targetRank, uint32_t &coreIndexInGroup, |
| 102 | uint32_t &groupSize); | 92 | uint32_t &groupSize); |
| 103 | - __aicore__ inline void BuffInit(); | ||
| 104 | - __aicore__ inline void MaskAlign(LocalTensor<half> maskCalcSelectedTensor); | ||
| 105 | - __aicore__ inline void MaskCheck(); | ||
| 106 | - __aicore__ inline void RecvPhaseReduce(); | ||
| 107 | 93 | ||
| 108 | __aicore__ inline uint64_t GetCommHandle(uint32_t rankId, uint32_t channelIndex) | 94 | __aicore__ inline uint64_t GetCommHandle(uint32_t rankId, uint32_t channelIndex) |
| 109 | { | 95 | { |
| @@ -118,10 +104,6 @@ private: | |||
| 118 | return (GM_ADDR)(winRankAddr_[rankId] + offset); | 104 | return (GM_ADDR)(winRankAddr_[rankId] + offset); |
| 119 | } | 105 | } |
| 120 | 106 | ||
| 121 | - __aicore__ inline GM_ADDR GetLocalSendDataWorkspaceAddr(const int32_t rankId) | ||
| 122 | - { | ||
| 123 | - return combineSendDataWorkspaceAddr_ + sendDataWorkspaceSizePerRank_ * rankId; | ||
| 124 | - } | ||
| 125 | __aicore__ inline uint32_t ReduceSumWorkNeedSize(int32_t count, int32_t typeSize) | 107 | __aicore__ inline uint32_t ReduceSumWorkNeedSize(int32_t count, int32_t typeSize) |
| 126 | { | 108 | { |
| 127 | int32_t elementsPerBlock = UB_ALIGN / typeSize; | 109 | int32_t elementsPerBlock = UB_ALIGN / typeSize; |
| @@ -138,103 +120,59 @@ private: | |||
| 138 | uint32_t rankId_{0}; | 120 | uint32_t rankId_{0}; |
| 139 | uint32_t epWorldSize_{0}; | 121 | uint32_t epWorldSize_{0}; |
| 140 | uint32_t channelsPerRank_{1}; | 122 | uint32_t channelsPerRank_{1}; |
| 141 | - uint32_t numMaxTokensPerRank_{0}; | ||
| 142 | - uint32_t numTokens_{0}; | ||
| 143 | uint32_t topK_{0}; | 123 | uint32_t topK_{0}; |
| 144 | uint32_t axisH_{0}; | 124 | uint32_t axisH_{0}; |
| 145 | - uint32_t hAlignSize_{0}; // UB对齐后的hidden size | 125 | + uint32_t hAlignSize_{0}; |
| 146 | uint64_t combineStateWinOffset_{0}; | 126 | uint64_t combineStateWinOffset_{0}; |
| 147 | uint64_t combineDataWinOffset_{0}; | 127 | uint64_t combineDataWinOffset_{0}; |
| 148 | 128 | ||
| 149 | - uint32_t hWeightAlignSize_{0}; // token+weight对齐后的hidden size | 129 | + uint32_t hWeightAlignSize_{0}; |
| 150 | uint32_t XTypeAlign32Size_{0}; | 130 | uint32_t XTypeAlign32Size_{0}; |
| 151 | uint32_t perSlotBytes_{0}; | 131 | uint32_t perSlotBytes_{0}; |
| 152 | uint64_t actualA_{0}; | 132 | uint64_t actualA_{0}; |
| 153 | uint32_t aivNum_{0}; | 133 | uint32_t aivNum_{0}; |
| 154 | uint32_t localmoeNum_{0}; | 134 | uint32_t localmoeNum_{0}; |
| 155 | - uint64_t sendDataWorkspaceSizePerRank_{0}; | ||
| 156 | 135 | ||
| 157 | - uint32_t tStart_{0}; | 136 | + uint32_t aivId_{0}; |
| 158 | - uint32_t tEnd_{0}; | ||
| 159 | - uint32_t tPerCore_{0}; | ||
| 160 | - uint32_t ubXBytes_{0}; | ||
| 161 | - uint32_t stateOffset_{0}; | ||
| 162 | - | ||
| 163 | - uint32_t mask_tokenNum_{0}; | ||
| 164 | - uint32_t bsKCastCnt_{0}; | ||
| 165 | - uint32_t activeMaskAlignSize_{0}; | ||
| 166 | 137 | ||
| 167 | GlobalTensor<XType> xGm_; | 138 | GlobalTensor<XType> xGm_; |
| 168 | - GlobalTensor<int32_t> topkIdxGm_; | ||
| 169 | GlobalTensor<int32_t> recvSrcMetadataGm_; | 139 | GlobalTensor<int32_t> recvSrcMetadataGm_; |
| 170 | GlobalTensor<int64_t> numRecvPerExpertGm_; | 140 | GlobalTensor<int64_t> numRecvPerExpertGm_; |
| 171 | GlobalTensor<float> topkWeightsGm_; | 141 | GlobalTensor<float> topkWeightsGm_; |
| 172 | 142 | ||
| 173 | - GlobalTensor<XType> combinedXGm_; | ||
| 174 | - GlobalTensor<float> combinedTopkWeightsGm_; | ||
| 175 | - | ||
| 176 | - LocalTensor<float> ubAccFp32_; | ||
| 177 | - LocalTensor<float> ubTmpFp32_; | ||
| 178 | LocalTensor<uint32_t> statusTensor_; | 143 | LocalTensor<uint32_t> statusTensor_; |
| 179 | LocalTensor<uint8_t> hcommTensor_; | 144 | LocalTensor<uint8_t> hcommTensor_; |
| 180 | - LocalTensor<uint32_t> stateResetTensor_; | ||
| 181 | 145 | ||
| 182 | - LocalTensor<half> maskGenerateTensor_; | ||
| 183 | - LocalTensor<bool> maskStrideTensor_; | ||
| 184 | - LocalTensor<half> tokenTargetTensor_; | ||
| 185 | - | ||
| 186 | - TQue<QuePosition::VECIN, 1> xInQue_; | ||
| 187 | - TQue<QuePosition::VECOUT, 1> xOutQue_; | ||
| 188 | - TQueBind<QuePosition::VECIN, QuePosition::VECOUT, 1> weightQue_; | ||
| 189 | - TBuf<QuePosition::VECIN> ubAccFp32Buf_; | ||
| 190 | - TBuf<QuePosition::VECIN> ubTmpFp32Buf_; | ||
| 191 | TBuf<> readStateBuf_; | 146 | TBuf<> readStateBuf_; |
| 192 | - TBuf<> tokenStatusBuf_; | ||
| 193 | - TBuf<> stateBuf_; | ||
| 194 | - TBuf<> stateSumBuf_; | ||
| 195 | - TBuf<> stateResetBuf_; | ||
| 196 | TBuf<> hcommBuf_; | 147 | TBuf<> hcommBuf_; |
| 197 | - | 148 | + TBuf<> metadataBuf_; |
| 198 | - TBuf<> compareBuf_; | ||
| 199 | - TBuf<> rowTmpFloatBuf_; | ||
| 200 | - TBuf<> tokenBuf_; | ||
| 201 | - TBuf<> tokenTargetTBuf_; | ||
| 202 | - TBuf<> metadataBuf_; // 发送阶段recvSrcMetadata的UB缓冲,批量搬运避免GetValue | ||
| 203 | TBuf<> weightsBuf_; | 149 | TBuf<> weightsBuf_; |
| 204 | 150 | ||
| 205 | - TQueBind<QuePosition::VECIN, QuePosition::VECOUT, 1> xQueue_; // 数据队列 | 151 | + TQueBind<QuePosition::VECIN, QuePosition::VECOUT, 1> xQueue_; |
| 206 | - AscendC::Hcomm<COMM_PROTOCOL_UBC_CTP> hcomm_; // 通信上下文 | 152 | + AscendC::Hcomm<COMM_PROTOCOL_UBC_CTP> hcomm_; |
| 207 | 153 | ||
| 208 | GM_ADDR winRankAddr_[Mc2Aclnn::HCCL_MAX_RANK_SIZE]; | 154 | GM_ADDR winRankAddr_[Mc2Aclnn::HCCL_MAX_RANK_SIZE]; |
| 209 | uint64_t hcommHandle_[Mc2Aclnn::HCCL_MAX_RANK_SIZE]; | 155 | uint64_t hcommHandle_[Mc2Aclnn::HCCL_MAX_RANK_SIZE]; |
| 210 | - GM_ADDR combineSendDataWorkspaceAddr_{nullptr}; | ||
| 211 | - uint32_t aivId_{0}; | ||
| 212 | }; | 156 | }; |
| 213 | 157 | ||
| 214 | template <TemplateMoeEpCombineTypeClass> | 158 | template <TemplateMoeEpCombineTypeClass> |
| 215 | __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::Init(GM_ADDR context, GM_ADDR x, GM_ADDR topkIdx, | 159 | __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::Init(GM_ADDR context, GM_ADDR x, GM_ADDR topkIdx, |
| 216 | GM_ADDR recvSrcMetadata, | 160 | GM_ADDR recvSrcMetadata, |
| 217 | GM_ADDR numRecvPerExpert, GM_ADDR topkWeights, | 161 | GM_ADDR numRecvPerExpert, GM_ADDR topkWeights, |
| 218 | - GM_ADDR combinedX, GM_ADDR combinedTopkWeights, | 162 | + GM_ADDR tilingGM, TPipe *pipe, |
| 219 | - GM_ADDR workspace, GM_ADDR tilingGM, | 163 | + const MoeEpCombineInfo *tilingData) |
| 220 | - TPipe *pipe, const MoeEpCombineInfo *tilingData) | ||
| 221 | { | 164 | { |
| 222 | tpipe_ = pipe; | 165 | tpipe_ = pipe; |
| 223 | tilingData_ = tilingData; | 166 | tilingData_ = tilingData; |
| 224 | aivId_ = GetBlockIdx(); | 167 | aivId_ = GetBlockIdx(); |
| 225 | - combineSendDataWorkspaceAddr_ = workspace; | ||
| 226 | epWorldSize_ = tilingData_->cfg.epWorldSize; | 168 | epWorldSize_ = tilingData_->cfg.epWorldSize; |
| 227 | - numMaxTokensPerRank_ = tilingData_->cfg.numMaxTokensPerRank; | ||
| 228 | - numTokens_ = tilingData_->cfg.numTokens; | ||
| 229 | topK_ = tilingData_->cfg.topK; | 169 | topK_ = tilingData_->cfg.topK; |
| 230 | axisH_ = tilingData_->cfg.hidden; | 170 | axisH_ = tilingData_->cfg.hidden; |
| 231 | perSlotBytes_ = tilingData_->cfg.perSlotBytes; | 171 | perSlotBytes_ = tilingData_->cfg.perSlotBytes; |
| 232 | aivNum_ = tilingData_->aivNum; | 172 | aivNum_ = tilingData_->aivNum; |
| 233 | localmoeNum_ = tilingData_->cfg.numLocalExperts; | 173 | localmoeNum_ = tilingData_->cfg.numLocalExperts; |
| 234 | - sendDataWorkspaceSizePerRank_ = tilingData->sendDataWorkspaceSizePerRank; | 174 | + hAlignSize_ = Ceil(axisH_ * sizeof(XType), UB_ALIGN) * UB_ALIGN; |
| 235 | - hAlignSize_ = Ceil(axisH_ * sizeof(XType), UB_ALIGN) * UB_ALIGN; // UB 32字节对齐 | 175 | + hWeightAlignSize_ = hAlignSize_ + UB_ALIGN; |
| 236 | - hWeightAlignSize_ = hAlignSize_ + UB_ALIGN; // UB 32字节对齐 | ||
| 237 | - stateOffset_ = STATE_OFFSET; | ||
| 238 | 176 | ||
| 239 | tpipe_->InitBuffer(hcommBuf_, HCOMM_INIT_SIZE); | 177 | tpipe_->InitBuffer(hcommBuf_, HCOMM_INIT_SIZE); |
| 240 | hcommTensor_ = hcommBuf_.Get<uint8_t>(); | 178 | hcommTensor_ = hcommBuf_.Get<uint8_t>(); |
| @@ -258,16 +196,14 @@ __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::Init(GM_ADDR | |||
| 258 | combineDataWinOffset_ = tilingData->combineDataWinOffset; | 196 | combineDataWinOffset_ = tilingData->combineDataWinOffset; |
| 259 | 197 | ||
| 260 | xGm_.SetGlobalBuffer((__gm__ XType *)x); | 198 | xGm_.SetGlobalBuffer((__gm__ XType *)x); |
| 261 | - topkIdxGm_.SetGlobalBuffer((__gm__ int32_t *)topkIdx); | ||
| 262 | recvSrcMetadataGm_.SetGlobalBuffer((__gm__ int32_t *)recvSrcMetadata); | 199 | recvSrcMetadataGm_.SetGlobalBuffer((__gm__ int32_t *)recvSrcMetadata); |
| 263 | numRecvPerExpertGm_.SetGlobalBuffer((__gm__ int64_t *)numRecvPerExpert); | 200 | numRecvPerExpertGm_.SetGlobalBuffer((__gm__ int64_t *)numRecvPerExpert); |
| 264 | - combinedXGm_.SetGlobalBuffer((__gm__ XType *)combinedX); | ||
| 265 | 201 | ||
| 266 | - // 计算actualA_的大小 | 202 | + // compute actualA_ from numRecvPerExpert |
| 267 | uint32_t numRecvBytes = Ceil(localmoeNum_ * sizeof(int64_t), UB_ALIGN) * UB_ALIGN; | 203 | uint32_t numRecvBytes = Ceil(localmoeNum_ * sizeof(int64_t), UB_ALIGN) * UB_ALIGN; |
| 268 | uint32_t reduceTmpBytes = ReduceSumWorkNeedSize(localmoeNum_, sizeof(int64_t)) * sizeof(int64_t); | 204 | uint32_t reduceTmpBytes = ReduceSumWorkNeedSize(localmoeNum_, sizeof(int64_t)) * sizeof(int64_t); |
| 269 | TBuf<TPosition::VECCALC> numRecvBuf; | 205 | TBuf<TPosition::VECCALC> numRecvBuf; |
| 270 | - tpipe_->InitBuffer(numRecvBuf, numRecvBytes + reduceTmpBytes + UB_ALIGN); // 数据 + ReduceSum tmp + 输出 | 206 | + tpipe_->InitBuffer(numRecvBuf, numRecvBytes + reduceTmpBytes + UB_ALIGN); |
| 271 | LocalTensor<int64_t> numRecvLocal = numRecvBuf.Get<int64_t>(); | 207 | LocalTensor<int64_t> numRecvLocal = numRecvBuf.Get<int64_t>(); |
| 272 | 208 | ||
| 273 | DataCopyExtParams numRecvCopyParams{1U, static_cast<uint32_t>(localmoeNum_ * sizeof(int64_t)), 0U, 0U, 0U}; | 209 | DataCopyExtParams numRecvCopyParams{1U, static_cast<uint32_t>(localmoeNum_ * sizeof(int64_t)), 0U, 0U, 0U}; |
| @@ -280,36 +216,17 @@ __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::Init(GM_ADDR | |||
| 280 | ReduceSum<int64_t>(sumOut, numRecvLocal, reduceTmp, static_cast<int32_t>(localmoeNum_)); | 216 | ReduceSum<int64_t>(sumOut, numRecvLocal, reduceTmp, static_cast<int32_t>(localmoeNum_)); |
| 281 | SyncFunc<AscendC::HardEvent::V_S>(); | 217 | SyncFunc<AscendC::HardEvent::V_S>(); |
| 282 | actualA_ = static_cast<uint64_t>(sumOut.GetValue(0)); | 218 | actualA_ = static_cast<uint64_t>(sumOut.GetValue(0)); |
| 283 | - ubXBytes_ = Ceil(axisH_ * sizeof(XType), UB_ALIGN) * UB_ALIGN; | 219 | + |
| 284 | XTypeAlign32Size_ = hAlignSize_; | 220 | XTypeAlign32Size_ = hAlignSize_; |
| 285 | if constexpr (HasTopkWeight == 1) { | 221 | if constexpr (HasTopkWeight == 1) { |
| 286 | XTypeAlign32Size_ = hWeightAlignSize_; | 222 | XTypeAlign32Size_ = hWeightAlignSize_; |
| 287 | topkWeightsGm_.SetGlobalBuffer((__gm__ float *)topkWeights); | 223 | topkWeightsGm_.SetGlobalBuffer((__gm__ float *)topkWeights); |
| 288 | - combinedTopkWeightsGm_.SetGlobalBuffer((__gm__ float *)combinedTopkWeights); | ||
| 289 | } | 224 | } |
| 290 | - tpipe_->InitBuffer(xQueue_, DOUBLE_BUFFER_NUM, XTypeAlign32Size_); | 225 | + tpipe_->InitBuffer(xQueue_, SEND_DOUBLE_BUFFER_NUM, XTypeAlign32Size_); |
| 291 | - tpipe_->InitBuffer(readStateBuf_, UB_ALIGN); // 32 | 226 | + tpipe_->InitBuffer(readStateBuf_, UB_ALIGN); |
| 292 | statusTensor_ = readStateBuf_.Get<uint32_t>(); | 227 | statusTensor_ = readStateBuf_.Get<uint32_t>(); |
| 293 | } | 228 | } |
| 294 | 229 | ||
| 295 | -template <TemplateMoeEpCombineTypeClass> | ||
| 296 | -__aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SplitToCore( | ||
| 297 | - uint32_t curSendCnt, uint32_t curUseAivNum, uint32_t &startTokenId, uint32_t &endTokenId, uint32_t &sendTokenNum) | ||
| 298 | -{ | ||
| 299 | - sendTokenNum = curSendCnt / curUseAivNum; // 每个aiv需要发送的token数 | ||
| 300 | - uint32_t remainderTokenNum = curSendCnt % curUseAivNum; // 余数 | ||
| 301 | - uint32_t newAivId = aivId_; | ||
| 302 | - | ||
| 303 | - startTokenId = sendTokenNum * newAivId; // 每个aiv发送时的起始rankid | ||
| 304 | - if (newAivId < remainderTokenNum) { // 前remainderRankNum个aiv需要多发1个卡的数据 | ||
| 305 | - sendTokenNum += 1; | ||
| 306 | - startTokenId += newAivId; | ||
| 307 | - } else { | ||
| 308 | - startTokenId += remainderTokenNum; | ||
| 309 | - } | ||
| 310 | - endTokenId = startTokenId + sendTokenNum; | ||
| 311 | -} | ||
| 312 | - | ||
| 313 | template <TemplateMoeEpCombineTypeClass> | 230 | template <TemplateMoeEpCombineTypeClass> |
| 314 | __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SendLocalToken(uint32_t tokenIndex, GM_ADDR dstAddr) | 231 | __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SendLocalToken(uint32_t tokenIndex, GM_ADDR dstAddr) |
| 315 | { | 232 | { |
| @@ -333,6 +250,7 @@ __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SendSlot(uint | |||
| 333 | { | 250 | { |
| 334 | uint64_t sendTokenOffset = (static_cast<uint64_t>(srcTokenIdx) * topK_ + srcTopKIdx) * perSlotBytes_; | 251 | uint64_t sendTokenOffset = (static_cast<uint64_t>(srcTokenIdx) * topK_ + srcTopKIdx) * perSlotBytes_; |
| 335 | uint64_t recvStateOffset = (srcTokenIdx * topK_ + srcTopKIdx) * WIN_ADDR_ALIGN; | 252 | uint64_t recvStateOffset = (srcTokenIdx * topK_ + srcTopKIdx) * WIN_ADDR_ALIGN; |
| 253 | + | ||
| 336 | GM_ADDR remoteRankWinAddr = GetUrmaWinAddrByRankId(srcRank, combineDataWinOffset_); | 254 | GM_ADDR remoteRankWinAddr = GetUrmaWinAddrByRankId(srcRank, combineDataWinOffset_); |
| 337 | GM_ADDR remoteRankStateAddr = GetUrmaStateAddrByRankId(srcRank, combineStateWinOffset_); | 255 | GM_ADDR remoteRankStateAddr = GetUrmaStateAddrByRankId(srcRank, combineStateWinOffset_); |
| 338 | 256 | ||
| @@ -410,9 +328,6 @@ __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SendPhaseExpe | |||
| 410 | } | 328 | } |
| 411 | uint32_t channelIndex = splitRankTokens ? coreIndexInGroup : 0; | 329 | uint32_t channelIndex = splitRankTokens ? coreIndexInGroup : 0; |
| 412 | 330 | ||
| 413 | - // 区间分片: splitRankTokens=true 时,同组 groupSize 个核各扫 1/groupSize 区间 | ||
| 414 | - // 合起来覆盖全量 actualA_,不漏 token | ||
| 415 | - // splitRankTokens=false 时保持全量扫描 + 取模分片 | ||
| 416 | uint64_t scanStart = 0; | 331 | uint64_t scanStart = 0; |
| 417 | uint64_t scanEnd = actualA_; | 332 | uint64_t scanEnd = actualA_; |
| 418 | if (splitRankTokens && groupSize > 1) { | 333 | if (splitRankTokens && groupSize > 1) { |
| @@ -427,11 +342,10 @@ __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SendPhaseExpe | |||
| 427 | } | 342 | } |
| 428 | } | 343 | } |
| 429 | 344 | ||
| 430 | - // statusTensor_ 懒初始化:仅本卡分支首次使用时触发,跨卡核跳过 V_MTE3 同步 | ||
| 431 | bool statusInitialized = false; | 345 | bool statusInitialized = false; |
| 432 | 346 | ||
| 433 | constexpr uint32_t metaBytesPerToken = RECV_META_FIELDS * sizeof(int32_t); | 347 | constexpr uint32_t metaBytesPerToken = RECV_META_FIELDS * sizeof(int32_t); |
| 434 | - constexpr uint32_t metaChunkTokenMax = 8192U; // 分块上限,平衡UB占用与搬运次数(128KB,A5 UB 256KB余量充足) | 348 | + constexpr uint32_t metaChunkTokenMax = 8192U; |
| 435 | 349 | ||
| 436 | uint32_t metaChunkTokens = (actualA_ < metaChunkTokenMax) ? actualA_ : metaChunkTokenMax; | 350 | uint32_t metaChunkTokens = (actualA_ < metaChunkTokenMax) ? actualA_ : metaChunkTokenMax; |
| 437 | uint32_t metaChunkBytes = Ceil(metaChunkTokens * metaBytesPerToken, UB_ALIGN) * UB_ALIGN; | 351 | uint32_t metaChunkBytes = Ceil(metaChunkTokens * metaBytesPerToken, UB_ALIGN) * UB_ALIGN; |
| @@ -487,168 +401,16 @@ __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::SendPhaseExpe | |||
| 487 | } | 401 | } |
| 488 | SendSlot(tokenIndex, srcRank, srcTokenIdx, srcTopKIdx, channelIndex, weight); | 402 | SendSlot(tokenIndex, srcRank, srcTokenIdx, srcTopKIdx, channelIndex, weight); |
| 489 | } | 403 | } |
| 490 | - SyncFunc<AscendC::HardEvent::S_MTE2>(); // 确保本轮 Scalar GetValue 读完,下一块 DataCopyPad 才可覆盖 | 404 | + SyncFunc<AscendC::HardEvent::S_MTE2>(); |
| 491 | - // metadataLocal | ||
| 492 | } | 405 | } |
| 493 | 406 | ||
| 494 | DataCacheCleanAndInvalid<int32_t, CacheLine::ENTIRE_DATA_CACHE, DcciDst::CACHELINE_OUT>(recvSrcMetadataGm_); | 407 | DataCacheCleanAndInvalid<int32_t, CacheLine::ENTIRE_DATA_CACHE, DcciDst::CACHELINE_OUT>(recvSrcMetadataGm_); |
| 495 | } | 408 | } |
| 496 | 409 | ||
| 497 | -template <TemplateMoeEpCombineTypeClass> | ||
| 498 | -__aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::BuffInit() | ||
| 499 | -{ | ||
| 500 | - tpipe_->Reset(); | ||
| 501 | - SplitToCore(numTokens_, aivNum_, tStart_, tEnd_, tPerCore_); | ||
| 502 | - if (tStart_ >= numTokens_) { | ||
| 503 | - return; | ||
| 504 | - } | ||
| 505 | - | ||
| 506 | - mask_tokenNum_ = tPerCore_ * topK_; | ||
| 507 | - uint32_t bsKInt32Align = Ceil(mask_tokenNum_ * sizeof(int32_t), UB_ALIGN) * UB_ALIGN; | ||
| 508 | - uint32_t bsKFloatAlign = Ceil(mask_tokenNum_ * sizeof(float), UB_ALIGN) * UB_ALIGN; | ||
| 509 | - uint32_t bsKHalfAlign = Ceil(mask_tokenNum_ * sizeof(half), UB_ALIGN) * UB_ALIGN; | ||
| 510 | - uint32_t bsHalfAlign = Ceil(tPerCore_ * sizeof(half), UB_ALIGN) * UB_ALIGN; | ||
| 511 | - | ||
| 512 | - bsKCastCnt_ = Ceil(mask_tokenNum_ * sizeof(int32_t), ALIGNED_LEN_256) * ALIGNED_LEN_256; | ||
| 513 | - activeMaskAlignSize_ = tPerCore_ * Ceil(topK_ * sizeof(half), UB_ALIGN) * UB_ALIGN; | ||
| 514 | - bsKInt32Align = (bsKInt32Align > bsKCastCnt_ ? bsKInt32Align : bsKCastCnt_); | ||
| 515 | - bsKHalfAlign = (bsKHalfAlign > activeMaskAlignSize_ ? bsKHalfAlign : activeMaskAlignSize_); | ||
| 516 | - bsKFloatAlign = (bsKFloatAlign > bsKCastCnt_ ? bsKFloatAlign : bsKCastCnt_); | ||
| 517 | - bsKFloatAlign = (bsKFloatAlign > activeMaskAlignSize_ ? bsKFloatAlign : activeMaskAlignSize_); | ||
| 518 | - | ||
| 519 | - tpipe_->InitBuffer(tokenTargetTBuf_, bsHalfAlign); | ||
| 520 | - tpipe_->InitBuffer(compareBuf_, bsKInt32Align); | ||
| 521 | - tpipe_->InitBuffer(rowTmpFloatBuf_, bsKFloatAlign); | ||
| 522 | - tpipe_->InitBuffer(tokenBuf_, bsKHalfAlign); | ||
| 523 | - | ||
| 524 | - if constexpr (HasTopkWeight == 1) { | ||
| 525 | - tpipe_->InitBuffer(weightQue_, DOUBLE_BUFFER_NUM, UB_ALIGN); | ||
| 526 | - } | ||
| 527 | - tpipe_->InitBuffer(xInQue_, DOUBLE_BUFFER_NUM, ubXBytes_); | ||
| 528 | - tpipe_->InitBuffer(xOutQue_, DOUBLE_BUFFER_NUM, ubXBytes_); | ||
| 529 | - tpipe_->InitBuffer(ubAccFp32Buf_, axisH_ * sizeof(float)); | ||
| 530 | - tpipe_->InitBuffer(ubTmpFp32Buf_, axisH_ * sizeof(float)); | ||
| 531 | - | ||
| 532 | - ubAccFp32_ = ubAccFp32Buf_.Get<float>(); | ||
| 533 | - ubTmpFp32_ = ubTmpFp32Buf_.Get<float>(); | ||
| 534 | -} | ||
| 535 | - | ||
| 536 | -template <TemplateMoeEpCombineTypeClass> | ||
| 537 | -__aicore__ inline bool MoeEpCombine<TemplateMoeEpCombineTypeFunc>::WaitDispatch(uint32_t tokenIndex, uint32_t copyCount) | ||
| 538 | -{ | ||
| 539 | - // 计算地址偏移 | ||
| 540 | - GM_ADDR stateGM = GetUrmaStateAddrByRankId(rankId_, combineStateWinOffset_) + tokenIndex * topK_ * WIN_ADDR_ALIGN; | ||
| 541 | - GlobalTensor<uint32_t> stateGMTensor; | ||
| 542 | - stateGMTensor.SetGlobalBuffer((__gm__ uint32_t *)stateGM); | ||
| 543 | - | ||
| 544 | - LocalTensor<uint32_t> stateTensor = stateBuf_.Get<uint32_t>(); | ||
| 545 | - SyncFunc<AscendC::HardEvent::S_MTE2>(); | ||
| 546 | - DataCopyExtParams params = {static_cast<uint16_t>(topK_), UB_ALIGN, WIN_ADDR_ALIGN - UB_ALIGN, 0, 0}; | ||
| 547 | - DataCopyPadExtParams<uint32_t> padParams = {false, 0, 0, 0}; | ||
| 548 | - | ||
| 549 | - DataCopyPad<uint32_t>(stateTensor, stateGMTensor, params, padParams); | ||
| 550 | - SyncFunc<AscendC::HardEvent::MTE2_V>(); | ||
| 551 | - | ||
| 552 | - LocalTensor<uint32_t> stateSumTensor = stateSumBuf_.Get<uint32_t>(); | ||
| 553 | - uint32_t shape[] = {topK_, UB_ALIGN / sizeof(uint32_t)}; | ||
| 554 | - ReduceSum<uint32_t, AscendC::Pattern::Reduce::RA, false>(stateSumTensor, stateTensor, shape, true); | ||
| 555 | - SyncFunc<AscendC::HardEvent::V_S>(); | ||
| 556 | - | ||
| 557 | - uint32_t localState = stateSumTensor(1); | ||
| 558 | - if (localState == copyCount) { | ||
| 559 | - return true; | ||
| 560 | - } | ||
| 561 | - return false; | ||
| 562 | -} | ||
| 563 | - | ||
| 564 | -template <TemplateMoeEpCombineTypeClass> | ||
| 565 | -__aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::ProcessTopKToken(uint32_t tokenIndex) | ||
| 566 | -{ | ||
| 567 | - Duplicate<float>(ubAccFp32_, (float)0, axisH_); | ||
| 568 | - DataCopyPadParams padParams = {false, 0, 0, 0}; | ||
| 569 | - DataCopyParams xCopyParams = {1U, static_cast<uint16_t>(hAlignSize_), 0U, 0U}; | ||
| 570 | - DataCopyParams weightCopyParams = {1U, static_cast<uint16_t>(sizeof(float)), 0U, 0U}; | ||
| 571 | - for (uint32_t topkId = 0U; topkId < topK_; topkId++) { | ||
| 572 | - // 读取expert_id | ||
| 573 | - uint64_t slotOffset = (static_cast<uint64_t>(tokenIndex) * topK_ + topkId) * perSlotBytes_; | ||
| 574 | - GM_ADDR wAddr = GetUrmaWinAddrByRankId(rankId_, combineDataWinOffset_) + slotOffset; | ||
| 575 | - GlobalTensor<XType> srcTokenTensor; | ||
| 576 | - srcTokenTensor.SetGlobalBuffer(reinterpret_cast<__gm__ XType *>(wAddr)); | ||
| 577 | - LocalTensor<XType> xLocal = xInQue_.AllocTensor<XType>(); | ||
| 578 | - DataCopyPad(xLocal, srcTokenTensor, xCopyParams, padParams); | ||
| 579 | - xInQue_.EnQue(xLocal); | ||
| 580 | - LocalTensor<XType> xIn = xInQue_.DeQue<XType>(); | ||
| 581 | - Cast(ubTmpFp32_, xIn, AscendC::RoundMode::CAST_NONE, axisH_); | ||
| 582 | - Add(ubAccFp32_, ubAccFp32_, ubTmpFp32_, axisH_); | ||
| 583 | - xInQue_.FreeTensor(xIn); | ||
| 584 | - | ||
| 585 | - if constexpr (HasTopkWeight == 1) { | ||
| 586 | - GM_ADDR weightAddr = GetUrmaStateAddrByRankId(rankId_, combineStateWinOffset_) + | ||
| 587 | - (tokenIndex * topK_ + topkId) * WIN_ADDR_ALIGN; | ||
| 588 | - GlobalTensor<float> srcWeightTensor; | ||
| 589 | - srcWeightTensor.SetGlobalBuffer(reinterpret_cast<__gm__ float *>(weightAddr)); | ||
| 590 | - LocalTensor<float> weightLocal = weightQue_.AllocTensor<float>(); | ||
| 591 | - DataCopyPad(weightLocal, srcWeightTensor, weightCopyParams, padParams); | ||
| 592 | - weightQue_.EnQue(weightLocal); | ||
| 593 | - LocalTensor<float> weightOut = weightQue_.DeQue<float>(); | ||
| 594 | - DataCopyPad(combinedTopkWeightsGm_[tokenIndex * topK_ + topkId], weightOut, weightCopyParams); | ||
| 595 | - weightQue_.FreeTensor(weightOut); | ||
| 596 | - } | ||
| 597 | - } | ||
| 598 | - // 累加结果双缓冲入队 | ||
| 599 | - LocalTensor<XType> ubResultBf16 = xOutQue_.AllocTensor<XType>(); | ||
| 600 | - Cast(ubResultBf16, ubAccFp32_, RoundMode::CAST_RINT, axisH_); | ||
| 601 | - xOutQue_.EnQue(ubResultBf16); | ||
| 602 | -} | ||
| 603 | - | ||
| 604 | -template <TemplateMoeEpCombineTypeClass> | ||
| 605 | -__aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::RecvPhaseReduce() | ||
| 606 | -{ | ||
| 607 | - DataCopyPadParams padParams = {false, 0, 0, 0}; | ||
| 608 | - DataCopyParams xCopyParams = {1U, static_cast<uint16_t>(axisH_ * sizeof(XType)), 0U, 0U}; | ||
| 609 | - tpipe_->InitBuffer(tokenStatusBuf_, Ceil(tPerCore_ * sizeof(int32_t), UB_ALIGN) * UB_ALIGN); | ||
| 610 | - tpipe_->InitBuffer(stateBuf_, topK_ * STATE_OFFSET); | ||
| 611 | - tpipe_->InitBuffer(stateSumBuf_, UB_ALIGN); | ||
| 612 | - tpipe_->InitBuffer(stateResetBuf_, topK_ * STATE_OFFSET); // 清理状态区 | ||
| 613 | - stateResetTensor_ = stateResetBuf_.Get<uint32_t>(); | ||
| 614 | - Duplicate<uint32_t>(stateResetTensor_, (uint32_t)0.0, static_cast<uint32_t>(topK_ * FLOAT_PER_UB_ALIGN)); | ||
| 615 | - LocalTensor<int32_t> tokenStatusTensor = tokenStatusBuf_.Get<int32_t>(); | ||
| 616 | - Duplicate<int32_t>(tokenStatusTensor, static_cast<int32_t>(0), tPerCore_); | ||
| 617 | - SyncFunc<AscendC::HardEvent::V_S>(); | ||
| 618 | - uint32_t CompletedtokenNum = static_cast<uint32_t>(0); | ||
| 619 | - uint32_t copyCount = topK_; | ||
| 620 | - while (CompletedtokenNum != tPerCore_) { | ||
| 621 | - for (uint32_t tokenIdx = tStart_; tokenIdx < tEnd_; ++tokenIdx) { | ||
| 622 | - if (tokenStatusTensor(tokenIdx - tStart_) == 1) { | ||
| 623 | - continue; | ||
| 624 | - } | ||
| 625 | - if (!WaitDispatch(tokenIdx, copyCount)) { | ||
| 626 | - continue; | ||
| 627 | - } | ||
| 628 | - CompletedtokenNum++; | ||
| 629 | - tokenStatusTensor.SetValue(tokenIdx - tStart_, 1); | ||
| 630 | - ProcessTopKToken(tokenIdx); | ||
| 631 | - LocalTensor<XType> ubResultBf16 = xOutQue_.DeQue<XType>(); | ||
| 632 | - DataCopyPad(combinedXGm_[tokenIdx * axisH_], ubResultBf16, xCopyParams); | ||
| 633 | - xOutQue_.FreeTensor(ubResultBf16); | ||
| 634 | - | ||
| 635 | - GM_ADDR stateGM = | ||
| 636 | - GetUrmaStateAddrByRankId(rankId_, combineStateWinOffset_) + tokenIdx * topK_ * WIN_ADDR_ALIGN; | ||
| 637 | - GlobalTensor<uint32_t> stateGMTensor; | ||
| 638 | - stateGMTensor.SetGlobalBuffer((__gm__ uint32_t *)stateGM); | ||
| 639 | - DataCopyExtParams resetParams = {static_cast<uint16_t>(topK_), UB_ALIGN, 0, WIN_ADDR_ALIGN - UB_ALIGN, 0}; | ||
| 640 | - DataCopyPad<uint32_t>(stateGMTensor, stateResetTensor_, resetParams); | ||
| 641 | - } | ||
| 642 | - } | ||
| 643 | -} | ||
| 644 | - | ||
| 645 | template <TemplateMoeEpCombineTypeClass> | 410 | template <TemplateMoeEpCombineTypeClass> |
| 646 | __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::Process() | 411 | __aicore__ inline void MoeEpCombine<TemplateMoeEpCombineTypeFunc>::Process() |
| 647 | { | 412 | { |
| 648 | SendPhaseExpertToToken(); | 413 | SendPhaseExpertToToken(); |
| 649 | - PipeBarrier<PIPE_ALL>(); // MaskCheck中包含reset操作,需确保前面操作完成 | ||
| 650 | - BuffInit(); | ||
| 651 | - RecvPhaseReduce(); | ||
| 652 | } | 414 | } |
| 653 | 415 | ||
| 654 | #endif | 416 | #endif |
| @@ -27,7 +27,7 @@ ASCENDC_TPL_ARGS_DECL(MoeEpCombine, ASCENDC_TPL_BOOL_DECL(TILINGKEY_HAS_TOPK_WEI | |||
| 27 | 27 | ||
| 28 | ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(TILINGKEY_HAS_TOPK_WEIGHTS, 0, 1), | 28 | ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(TILINGKEY_HAS_TOPK_WEIGHTS, 0, 1), |
| 29 | ASCENDC_TPL_UINT_SEL(ARCH_TAG, ASCENDC_TPL_UI_LIST, TILINGKEY_TPL_A5), | 29 | ASCENDC_TPL_UINT_SEL(ARCH_TAG, ASCENDC_TPL_UI_LIST, TILINGKEY_TPL_A5), |
| 30 | - ASCENDC_TPL_TILING_STRUCT_SEL(MoeEpCombineTilingData)), ); | 30 | + ASCENDC_TPL_TILING_STRUCT_SEL(MoeEpCombineTilingData))); |
| 31 | 31 | ||
| 32 | } // namespace Mc2Tiling | 32 | } // namespace Mc2Tiling |
| 33 | #endif // MOE_EP_COMBINE_TILING_KEY_H | 33 | #endif // MOE_EP_COMBINE_TILING_KEY_H |
| @@ -0,0 +1,10 @@ | |||
| 1 | +file(GLOB CURRENT_DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*) | ||
| 2 | +if(NOT ENABLE_TEST) | ||
| 3 | + list(REMOVE_ITEM CURRENT_DIRS tests) | ||
| 4 | +endif() | ||
| 5 | +foreach(SUB_DIR ${CURRENT_DIRS}) | ||
| 6 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt") | ||
| 7 | + add_subdirectory(${SUB_DIR}) | ||
| 8 | + endif() | ||
| 9 | +endforeach() | ||
| 10 | +set(MC2_COMPILE ${SUB_MC2_COMPILE} PARENT_SCOPE) | ||
| @@ -0,0 +1,100 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include "common/utils/op_mc2.h" | ||
| 12 | +#include "common/utils/op_mc2_def.h" | ||
| 13 | +#include "aclnn_kernels/common/op_error_check.h" | ||
| 14 | +#include "opdev/op_log.h" | ||
| 15 | +#include "opdev/common_types.h" | ||
| 16 | +#include "aclnnInner_moe_ep_combine_epilogue.h" | ||
| 17 | + | ||
| 18 | +using namespace op; | ||
| 19 | + | ||
| 20 | +namespace { | ||
| 21 | +constexpr int64_t NETWORK_DIRECT = 0; | ||
| 22 | +constexpr int64_t NETWORK_HYBRID = 1; | ||
| 23 | +} // namespace | ||
| 24 | + | ||
| 25 | +static aclnnStatus CheckNotNull(const aclTensor *context, const aclTensor *topkIdx, aclTensor *combinedX) | ||
| 26 | +{ | ||
| 27 | + CHECK_RET(context != nullptr, ACLNN_ERR_PARAM_NULLPTR); | ||
| 28 | + CHECK_RET(topkIdx != nullptr, ACLNN_ERR_PARAM_NULLPTR); | ||
| 29 | + CHECK_RET(combinedX != nullptr, ACLNN_ERR_PARAM_NULLPTR); | ||
| 30 | + return ACLNN_SUCCESS; | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +static aclnnStatus CheckParams(int64_t epWorldSize, int64_t epRankId, int64_t numExperts, int64_t numMaxTokensPerRank, | ||
| 34 | + int64_t cclBufferSize, int64_t topoType, int64_t rankNumPerServer) | ||
| 35 | +{ | ||
| 36 | + CHECK_RET(epWorldSize > 1, ACLNN_ERR_PARAM_INVALID); | ||
| 37 | + CHECK_RET(epRankId >= 0 && epRankId < epWorldSize, ACLNN_ERR_PARAM_INVALID); | ||
| 38 | + CHECK_RET(numExperts % epWorldSize == 0, ACLNN_ERR_PARAM_INVALID); | ||
| 39 | + CHECK_RET(numMaxTokensPerRank > 0, ACLNN_ERR_PARAM_INVALID); | ||
| 40 | + CHECK_RET(cclBufferSize > 0, ACLNN_ERR_PARAM_INVALID); | ||
| 41 | + CHECK_RET(topoType == NETWORK_DIRECT || topoType == NETWORK_HYBRID, ACLNN_ERR_PARAM_INVALID); | ||
| 42 | + CHECK_RET(rankNumPerServer > 0, ACLNN_ERR_PARAM_INVALID); | ||
| 43 | + CHECK_RET(epWorldSize % rankNumPerServer == 0, ACLNN_ERR_PARAM_INVALID); | ||
| 44 | + return ACLNN_SUCCESS; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +#ifdef __cplusplus | ||
| 48 | +extern "C" { | ||
| 49 | +#endif | ||
| 50 | + | ||
| 51 | +enum NnopbaseHcclServerType { | ||
| 52 | + NNOPBASE_HCCL_SERVER_TYPE_AICPU = 0, | ||
| 53 | + NNOPBASE_HCCL_SERVER_TYPE_MTE | ||
| 54 | +}; | ||
| 55 | + | ||
| 56 | +aclnnStatus MoeEpCombineEpilogueGetWorkspaceSize(const aclTensor *context, const aclTensor *topkIdx, | ||
| 57 | + int64_t epWorldSize, int64_t epRankId, int64_t numExperts, | ||
| 58 | + int64_t numMaxTokensPerRank, int64_t cclBufferSize, | ||
| 59 | + bool hasTopkWeights, int64_t topoType, int64_t rankNumPerServer, | ||
| 60 | + aclTensor *combinedX, aclTensor *combinedTopkWeights, | ||
| 61 | + uint64_t *workspaceSize, aclOpExecutor **executor) | ||
| 62 | +{ | ||
| 63 | + OP_LOGD("MoeEpCombineEpilogue", "Begin to do MoeEpCombineEpilogueGetWorkspaceSize"); | ||
| 64 | + auto retNotNull = CheckNotNull(context, topkIdx, combinedX); | ||
| 65 | + CHECK_RET(retNotNull == ACLNN_SUCCESS, retNotNull); | ||
| 66 | + auto retParams = | ||
| 67 | + CheckParams(epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, topoType, rankNumPerServer); | ||
| 68 | + CHECK_RET(retParams == ACLNN_SUCCESS, retParams); | ||
| 69 | + | ||
| 70 | + return aclnnInnerMoeEpCombineEpilogueGetWorkspaceSize( | ||
| 71 | + context, topkIdx, epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, hasTopkWeights, | ||
| 72 | + topoType, rankNumPerServer, combinedX, combinedTopkWeights, workspaceSize, executor); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +aclnnStatus aclnnMoeEpCombineEpilogueGetWorkspaceSize(const aclTensor *context, const aclTensor *topkIdx, | ||
| 76 | + int64_t epWorldSize, int64_t epRankId, int64_t numExperts, | ||
| 77 | + int64_t numMaxTokensPerRank, int64_t cclBufferSize, | ||
| 78 | + bool hasTopkWeights, int64_t topoType, int64_t rankNumPerServer, | ||
| 79 | + aclTensor *combinedX, aclTensor *combinedTopkWeights, | ||
| 80 | + uint64_t *workspaceSize, aclOpExecutor **executor) | ||
| 81 | +{ | ||
| 82 | + return MoeEpCombineEpilogueGetWorkspaceSize( | ||
| 83 | + context, topkIdx, epWorldSize, epRankId, numExperts, numMaxTokensPerRank, cclBufferSize, hasTopkWeights, | ||
| 84 | + topoType, rankNumPerServer, combinedX, combinedTopkWeights, workspaceSize, executor); | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +extern "C" void __attribute__((weak)) NnopbaseSetHcclServerType(void *executor, NnopbaseHcclServerType sType); | ||
| 88 | + | ||
| 89 | +aclnnStatus aclnnMoeEpCombineEpilogue(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, | ||
| 90 | + aclrtStream stream) | ||
| 91 | +{ | ||
| 92 | + if (NnopbaseSetHcclServerType) { | ||
| 93 | + NnopbaseSetHcclServerType(executor, NNOPBASE_HCCL_SERVER_TYPE_MTE); | ||
| 94 | + } | ||
| 95 | + return aclnnInnerMoeEpCombineEpilogue(workspace, workspaceSize, executor, stream); | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +#ifdef __cplusplus | ||
| 99 | +} | ||
| 100 | +#endif | ||
| @@ -0,0 +1,29 @@ | |||
| 1 | +if (BUILD_OPEN_PROJECT) | ||
| 2 | + target_sources(op_host_aclnnInner PRIVATE | ||
| 3 | + moe_ep_combine_epilogue_def.cpp | ||
| 4 | + ) | ||
| 5 | + add_modules_sources_with_soc( | ||
| 6 | + OP_API_INDEPENDENT ON | ||
| 7 | + OP_API_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../op_api | ||
| 8 | + OP_MC2_ENABLE ON | ||
| 9 | + OPTYPE moe_ep_combine_epilogue moe_ep_combine_epilogue ACLNNTYPE aclnn aclnn_inner) | ||
| 10 | + set(SUB_MC2_COMPILE TRUE PARENT_SCOPE) | ||
| 11 | + set(MC2_OPT ON PARENT_SCOPE) | ||
| 12 | + set(moe_ep_combine_epilogue_depends mc2/common mc2/3rd mc2/moe_ep_combine PARENT_SCOPE) | ||
| 13 | + | ||
| 14 | + set(CONDITION_UNIT ${ASCEND_COMPUTE_UNIT}) | ||
| 15 | + if("${CONDITION_UNIT}" STREQUAL "ascend950") | ||
| 16 | + add_ops_compile_options( | ||
| 17 | + OP_NAME MoeEpCombineEpilogue | ||
| 18 | + OPTIONS --cce-auto-sync=off | ||
| 19 | + -mllvm -cce-aicore-dcci-before-kernel-end=false | ||
| 20 | + ) | ||
| 21 | + elseif("${CONDITION_UNIT}" STREQUAL "ascend910_93") | ||
| 22 | + add_ops_compile_options(OP_NAME MoeEpCombineEpilogue OPTIONS --cce-auto-sync=off) | ||
| 23 | + elseif("${CONDITION_UNIT}" STREQUAL "ascend910b") | ||
| 24 | + add_ops_compile_options(OP_NAME MoeEpCombineEpilogue OPTIONS --cce-auto-sync=off) | ||
| 25 | + endif() | ||
| 26 | +else() | ||
| 27 | + add_mc2_modules_sources(OPTYPE moe_ep_combine_epilogue moe_ep_combine_epilogue ACLNNTYPE aclnn aclnn_inner) | ||
| 28 | + set(SUB_MC2_COMPILE TRUE PARENT_SCOPE) | ||
| 29 | +endif() | ||
| @@ -0,0 +1,63 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include "register/op_def_registry.h" | ||
| 12 | + | ||
| 13 | +namespace ops { | ||
| 14 | + | ||
| 15 | +class MoeEpCombineEpilogue : public OpDef { | ||
| 16 | +public: | ||
| 17 | + explicit MoeEpCombineEpilogue(const char *name) | ||
| 18 | + : OpDef(name) | ||
| 19 | + { | ||
| 20 | + this->Input("context") | ||
| 21 | + .ParamType(REQUIRED) | ||
| 22 | + .DataTypeList({ge::DT_INT32}) | ||
| 23 | + .FormatList({ge::FORMAT_ND}) | ||
| 24 | + .AutoContiguous(); | ||
| 25 | + this->Input("topk_idx") | ||
| 26 | + .ParamType(REQUIRED) | ||
| 27 | + .DataType({ge::DT_INT32}) | ||
| 28 | + .FormatList({ge::FORMAT_ND}) | ||
| 29 | + .AutoContiguous(); | ||
| 30 | + | ||
| 31 | + this->Output("combined_x") | ||
| 32 | + .ParamType(REQUIRED) | ||
| 33 | + .DataTypeList({ge::DT_BF16, ge::DT_FLOAT16}) | ||
| 34 | + .Format({ge::FORMAT_ND}); | ||
| 35 | + this->Output("combined_topk_weights").ParamType(OPTIONAL).DataType({ge::DT_FLOAT}).Format({ge::FORMAT_ND}); | ||
| 36 | + | ||
| 37 | + this->Attr("ep_world_size").AttrType(REQUIRED).Int(); | ||
| 38 | + this->Attr("ep_rank_id").AttrType(REQUIRED).Int(); | ||
| 39 | + this->Attr("num_experts").AttrType(REQUIRED).Int(); | ||
| 40 | + this->Attr("num_max_tokens_per_rank").AttrType(REQUIRED).Int(); | ||
| 41 | + this->Attr("ccl_buffer_size").AttrType(REQUIRED).Int(); | ||
| 42 | + this->Attr("has_topk_weights").AttrType(OPTIONAL).Bool(true); | ||
| 43 | + this->Attr("topo_type").AttrType(OPTIONAL).Int(0); | ||
| 44 | + this->Attr("rank_num_per_server").AttrType(OPTIONAL).Int(1); | ||
| 45 | + | ||
| 46 | + OpAICoreConfig aicore_config; | ||
| 47 | + aicore_config.DynamicCompileStaticFlag(true) | ||
| 48 | + .DynamicFormatFlag(true) | ||
| 49 | + .DynamicRankSupportFlag(true) | ||
| 50 | + .DynamicShapeSupportFlag(true) | ||
| 51 | + .NeedCheckSupportFlag(false) | ||
| 52 | + .PrecisionReduceFlag(true) | ||
| 53 | + .ExtendCfgInfo("aclnnSupport.value", "support_aclnn") | ||
| 54 | + .ExtendCfgInfo("prebuildPattern.value", "Opaque") | ||
| 55 | + .ExtendCfgInfo("jitCompile.flag", "static_true") | ||
| 56 | + .ExtendCfgInfo("multiKernelSupportDynamicGraph.value", "multi_kernel"); | ||
| 57 | + | ||
| 58 | + this->AICore().AddConfig("ascend950", aicore_config); | ||
| 59 | + } | ||
| 60 | +}; | ||
| 61 | +OP_ADD(MoeEpCombineEpilogue); | ||
| 62 | + | ||
| 63 | +} // namespace ops | ||
| @@ -0,0 +1,383 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file moe_ep_combine_epilogue_tiling.cpp | ||
| 13 | + * \brief Combine Epilogue tiling — computes Window offsets for recv+reduce phase. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <cmath> | ||
| 17 | +#include <cstdint> | ||
| 18 | +#include <cstdio> | ||
| 19 | + | ||
| 20 | +#include "mc2_log.h" | ||
| 21 | +#include "graph/utils/type_utils.h" | ||
| 22 | +#include "mc2_tiling_utils.h" | ||
| 23 | +#include "register/op_def_registry.h" | ||
| 24 | +#include "register/tilingdata_base.h" | ||
| 25 | +#include "tiling/tiling_api.h" | ||
| 26 | +#include "../../op_kernel/moe_ep_combine_epilogue_tiling.h" | ||
| 27 | +#include "../../op_kernel/moe_ep_combine_epilogue_tiling_key.h" | ||
| 28 | + | ||
| 29 | +using namespace AscendC; | ||
| 30 | +using namespace ge; | ||
| 31 | +using namespace Mc2Tiling; | ||
| 32 | + | ||
| 33 | +namespace { | ||
| 34 | + | ||
| 35 | +constexpr uint32_t CONTEXT_INDEX = 0U; | ||
| 36 | +constexpr uint32_t TOPK_IDX_INDEX = 1U; | ||
| 37 | + | ||
| 38 | +constexpr uint32_t OUT_COMBINED_X_INDEX = 0U; | ||
| 39 | +constexpr uint32_t OUT_COMBINED_TOPK_WEIGHTS_INDEX = 1U; | ||
| 40 | + | ||
| 41 | +constexpr uint32_t ATTR_EP_WORLD_SIZE_INDEX = 0; | ||
| 42 | +constexpr uint32_t ATTR_EP_RANK_ID_INDEX = 1; | ||
| 43 | +constexpr uint32_t ATTR_NUM_EXPERTS_INDEX = 2; | ||
| 44 | +constexpr uint32_t ATTR_NUM_MAX_TPR_INDEX = 3; | ||
| 45 | +constexpr uint32_t ATTR_CCL_BUFFER_SIZE_INDEX = 4; | ||
| 46 | +constexpr uint32_t ATTR_HAS_TOPK_WEIGHTS_INDEX = 5; | ||
| 47 | +constexpr uint32_t ATTR_TOPO_TYPE_INDEX = 6; | ||
| 48 | +constexpr uint32_t ATTR_RANK_NUM_PER_SERVER_INDEX = 7; | ||
| 49 | + | ||
| 50 | +constexpr uint32_t ONE_DIMS = 1U; | ||
| 51 | +constexpr uint32_t TWO_DIMS = 2U; | ||
| 52 | +constexpr int64_t MAX_EP_WORLD_SIZE = 1024; | ||
| 53 | +constexpr int64_t MIN_EP_WORLD_SIZE = 2; | ||
| 54 | +constexpr int64_t MAX_NUM_EXPERTS = 2048; | ||
| 55 | +constexpr int64_t MIN_NUM_EXPERTS = 2; | ||
| 56 | +constexpr uint32_t SYSTEM_NEED_WORKSPACE = 16U * 1024U * 1024U; | ||
| 57 | +constexpr uint64_t MB_SIZE = 1024UL * 1024UL; | ||
| 58 | +constexpr uint64_t UB_ALIGN = 32UL; | ||
| 59 | +constexpr uint64_t COMM_ALIGN = 512UL; | ||
| 60 | +constexpr uint64_t NOTIFY_CNT_ALIGN = 15000UL; | ||
| 61 | +constexpr uint64_t MAX_OUT_DTYPE_SIZE = 2UL; | ||
| 62 | +constexpr uint64_t METADATA_DTYPE_SIZE = 4UL; | ||
| 63 | +constexpr int64_t H_MIN = 1; | ||
| 64 | +constexpr int64_t H_MAX = 8192; | ||
| 65 | +constexpr int64_t K_MAX = 32; | ||
| 66 | +constexpr uint32_t NETWORK_DIRECT = 0U; | ||
| 67 | +constexpr uint32_t NETWORK_HYBRID = 1U; | ||
| 68 | + | ||
| 69 | +static void PrintTilingDataInfo(const char *nodeName, const MoeEpCombineEpilogueInfo &info) | ||
| 70 | +{ | ||
| 71 | + OP_LOGD(nodeName, "epWorldSize=%u, epRankId=%u, numExperts=%u, numLocalExperts=%u", info.cfg.epWorldSize, | ||
| 72 | + info.cfg.epRankId, info.cfg.numExperts, info.cfg.numLocalExperts); | ||
| 73 | + OP_LOGD(nodeName, "numTokens=%u, hidden=%u, topK=%u, numMaxTokensPerRank=%u", info.cfg.numTokens, info.cfg.hidden, | ||
| 74 | + info.cfg.topK, info.cfg.numMaxTokensPerRank); | ||
| 75 | + OP_LOGD(nodeName, "perSlotBytes=%u, hasTopkWeights=%u, aivNum=%u", info.cfg.perSlotBytes, info.hasTopkWeights, | ||
| 76 | + info.aivNum); | ||
| 77 | + OP_LOGD(nodeName, "combineStateWinOffset=%lu, combineDataWinOffset=%lu, totalUbSize=%lu", | ||
| 78 | + info.combineStateWinOffset, info.combineDataWinOffset, info.totalUbSize); | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +static ge::graphStatus CheckInputTensorShape(const gert::TilingContext *context, const char *nodeName, | ||
| 82 | + MoeEpCombineEpilogueInfo &info) | ||
| 83 | +{ | ||
| 84 | + const gert::StorageShape *contextStorageShape = context->GetInputShape(CONTEXT_INDEX); | ||
| 85 | + OP_CHECK_NULL_WITH_CONTEXT(context, contextStorageShape); | ||
| 86 | + OP_TILING_CHECK( | ||
| 87 | + contextStorageShape->GetStorageShape().GetDimNum() != 1, | ||
| 88 | + OP_LOGE(nodeName, "context dims must be 1, but got %lu.", contextStorageShape->GetStorageShape().GetDimNum()), | ||
| 89 | + return ge::GRAPH_FAILED); | ||
| 90 | + | ||
| 91 | + const gert::StorageShape *topkIdxShape = context->GetInputShape(TOPK_IDX_INDEX); | ||
| 92 | + OP_CHECK_NULL_WITH_CONTEXT(context, topkIdxShape); | ||
| 93 | + OP_TILING_CHECK( | ||
| 94 | + topkIdxShape->GetStorageShape().GetDimNum() != TWO_DIMS, | ||
| 95 | + OP_LOGE(nodeName, "topk_idx dims must be 2, but got %lu.", topkIdxShape->GetStorageShape().GetDimNum()), | ||
| 96 | + return ge::GRAPH_FAILED); | ||
| 97 | + | ||
| 98 | + const int64_t topkDim0 = topkIdxShape->GetStorageShape().GetDim(0); | ||
| 99 | + const int64_t topkDim1 = topkIdxShape->GetStorageShape().GetDim(1); | ||
| 100 | + int64_t numExperts = static_cast<int64_t>(info.cfg.numExperts); | ||
| 101 | + OP_TILING_CHECK(topkDim0 <= 0, | ||
| 102 | + OP_LOGE(nodeName, "topk_idx dim0(num_tokens) must be positive, but got %ld.", topkDim0), | ||
| 103 | + return ge::GRAPH_FAILED); | ||
| 104 | + OP_TILING_CHECK( | ||
| 105 | + (topkDim1 <= 0) || (topkDim1 > K_MAX) || (topkDim1 > numExperts), | ||
| 106 | + OP_LOGE(nodeName, "topk_idx dim1(top_k) is invalid, should be in (0, min(%ld, num_experts=%ld)], but got %ld.", | ||
| 107 | + K_MAX, numExperts, topkDim1), | ||
| 108 | + return ge::GRAPH_FAILED); | ||
| 109 | + | ||
| 110 | + info.cfg.numTokens = static_cast<uint32_t>(topkDim0); | ||
| 111 | + info.cfg.topK = static_cast<uint32_t>(topkDim1); | ||
| 112 | + | ||
| 113 | + const gert::StorageShape *combinedXShape = context->GetOutputShape(OUT_COMBINED_X_INDEX); | ||
| 114 | + OP_CHECK_NULL_WITH_CONTEXT(context, combinedXShape); | ||
| 115 | + OP_TILING_CHECK( | ||
| 116 | + combinedXShape->GetStorageShape().GetDimNum() != TWO_DIMS, | ||
| 117 | + OP_LOGE(nodeName, "combined_x dims must be 2, but got %lu.", combinedXShape->GetStorageShape().GetDimNum()), | ||
| 118 | + return ge::GRAPH_FAILED); | ||
| 119 | + const int64_t combinedXDim0 = combinedXShape->GetStorageShape().GetDim(0); | ||
| 120 | + const int64_t combinedXDim1 = combinedXShape->GetStorageShape().GetDim(1); | ||
| 121 | + OP_TILING_CHECK( | ||
| 122 | + combinedXDim0 != topkDim0, | ||
| 123 | + OP_LOGE(nodeName, "combined_x dim0 must equal topk_idx dim0(%ld), but got %ld.", topkDim0, combinedXDim0), | ||
| 124 | + return ge::GRAPH_FAILED); | ||
| 125 | + OP_TILING_CHECK((combinedXDim1 < H_MIN) || (combinedXDim1 > H_MAX), | ||
| 126 | + OP_LOGE(nodeName, "combined_x dim1(hidden) is invalid, should be in [%ld, %ld], but got %ld.", | ||
| 127 | + H_MIN, H_MAX, combinedXDim1), | ||
| 128 | + return ge::GRAPH_FAILED); | ||
| 129 | + info.cfg.hidden = static_cast<uint32_t>(combinedXDim1); | ||
| 130 | + | ||
| 131 | + if (info.hasTopkWeights) { | ||
| 132 | + const gert::StorageShape *combinedTopkWeightsShape = context->GetOutputShape(OUT_COMBINED_TOPK_WEIGHTS_INDEX); | ||
| 133 | + OP_CHECK_NULL_WITH_CONTEXT(context, combinedTopkWeightsShape); | ||
| 134 | + OP_TILING_CHECK(combinedTopkWeightsShape->GetStorageShape().GetDimNum() != TWO_DIMS, | ||
| 135 | + OP_LOGE(nodeName, "combined_topk_weights dims must be 2, but got %lu.", | ||
| 136 | + combinedTopkWeightsShape->GetStorageShape().GetDimNum()), | ||
| 137 | + return ge::GRAPH_FAILED); | ||
| 138 | + OP_TILING_CHECK(combinedTopkWeightsShape->GetStorageShape().GetDim(0) != topkDim0, | ||
| 139 | + OP_LOGE(nodeName, "combined_topk_weights dim0 must equal topk_idx dim0(%ld), but got %ld.", | ||
| 140 | + topkDim0, combinedTopkWeightsShape->GetStorageShape().GetDim(0)), | ||
| 141 | + return ge::GRAPH_FAILED); | ||
| 142 | + OP_TILING_CHECK(combinedTopkWeightsShape->GetStorageShape().GetDim(1) != topkDim1, | ||
| 143 | + OP_LOGE(nodeName, "combined_topk_weights dim1 must equal topk_idx dim1(%ld), but got %ld.", | ||
| 144 | + topkDim1, combinedTopkWeightsShape->GetStorageShape().GetDim(1)), | ||
| 145 | + return ge::GRAPH_FAILED); | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + return ge::GRAPH_SUCCESS; | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +static ge::graphStatus CheckInputDataType(const gert::TilingContext *context, const char *nodeName, | ||
| 152 | + const MoeEpCombineEpilogueInfo &info) | ||
| 153 | +{ | ||
| 154 | + auto contextDesc = context->GetInputDesc(CONTEXT_INDEX); | ||
| 155 | + OP_CHECK_NULL_WITH_CONTEXT(context, contextDesc); | ||
| 156 | + OP_TILING_CHECK(contextDesc->GetDataType() != ge::DT_INT32, | ||
| 157 | + OP_LOGE(nodeName, "context dtype must be DT_INT32, but got %s.", | ||
| 158 | + ge::TypeUtils::DataTypeToSerialString(contextDesc->GetDataType()).c_str()), | ||
| 159 | + return ge::GRAPH_FAILED); | ||
| 160 | + | ||
| 161 | + auto topkIdxDesc = context->GetInputDesc(TOPK_IDX_INDEX); | ||
| 162 | + OP_CHECK_NULL_WITH_CONTEXT(context, topkIdxDesc); | ||
| 163 | + OP_TILING_CHECK(topkIdxDesc->GetDataType() != ge::DT_INT32, | ||
| 164 | + OP_LOGE(nodeName, "topk_idx dtype must be DT_INT32, but got %s.", | ||
| 165 | + ge::TypeUtils::DataTypeToSerialString(topkIdxDesc->GetDataType()).c_str()), | ||
| 166 | + return ge::GRAPH_FAILED); | ||
| 167 | + | ||
| 168 | + auto combinedXDesc = context->GetOutputDesc(OUT_COMBINED_X_INDEX); | ||
| 169 | + OP_CHECK_NULL_WITH_CONTEXT(context, combinedXDesc); | ||
| 170 | + OP_TILING_CHECK(combinedXDesc->GetDataType() != ge::DT_BF16 && combinedXDesc->GetDataType() != ge::DT_FLOAT16, | ||
| 171 | + OP_LOGE(nodeName, "combined_x dtype must be DT_BF16 or DT_FLOAT16, but got %s.", | ||
| 172 | + ge::TypeUtils::DataTypeToSerialString(combinedXDesc->GetDataType()).c_str()), | ||
| 173 | + return ge::GRAPH_FAILED); | ||
| 174 | + | ||
| 175 | + auto combinedTopkWeightsDesc = context->GetOutputDesc(OUT_COMBINED_TOPK_WEIGHTS_INDEX); | ||
| 176 | + if (info.hasTopkWeights) { | ||
| 177 | + OP_CHECK_NULL_WITH_CONTEXT(context, combinedTopkWeightsDesc); | ||
| 178 | + OP_TILING_CHECK(combinedTopkWeightsDesc->GetDataType() != ge::DT_FLOAT, | ||
| 179 | + OP_LOGE(nodeName, "combined_topk_weights dtype must be DT_FLOAT, but got %s.", | ||
| 180 | + ge::TypeUtils::DataTypeToSerialString(combinedTopkWeightsDesc->GetDataType()).c_str()), | ||
| 181 | + return ge::GRAPH_FAILED); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + return ge::GRAPH_SUCCESS; | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +static ge::graphStatus CheckAttrParams(const gert::TilingContext *context, const char *nodeName, | ||
| 188 | + MoeEpCombineEpilogueInfo &info, uint32_t &networkMode, uint32_t &serverNum) | ||
| 189 | +{ | ||
| 190 | + auto attrs = context->GetAttrs(); | ||
| 191 | + OP_TILING_CHECK(attrs == nullptr, OP_LOGE(nodeName, "attrs is nullptr."), return ge::GRAPH_FAILED); | ||
| 192 | + | ||
| 193 | + auto epWorldSizePtr = attrs->GetAttrPointer<int64_t>(ATTR_EP_WORLD_SIZE_INDEX); | ||
| 194 | + auto epRankIdPtr = attrs->GetAttrPointer<int64_t>(ATTR_EP_RANK_ID_INDEX); | ||
| 195 | + auto numExpertsPtr = attrs->GetAttrPointer<int64_t>(ATTR_NUM_EXPERTS_INDEX); | ||
| 196 | + auto nmtPtr = attrs->GetAttrPointer<int64_t>(ATTR_NUM_MAX_TPR_INDEX); | ||
| 197 | + auto cclBufferSizePtr = attrs->GetAttrPointer<int64_t>(ATTR_CCL_BUFFER_SIZE_INDEX); | ||
| 198 | + auto hasTopkWeightsPtr = attrs->GetAttrPointer<bool>(ATTR_HAS_TOPK_WEIGHTS_INDEX); | ||
| 199 | + auto requestedNetworkModePtr = attrs->GetAttrPointer<int64_t>(ATTR_TOPO_TYPE_INDEX); | ||
| 200 | + auto rankNumPerServerPtr = attrs->GetAttrPointer<int64_t>(ATTR_RANK_NUM_PER_SERVER_INDEX); | ||
| 201 | + | ||
| 202 | + OP_TILING_CHECK(epWorldSizePtr == nullptr, OP_LOGE(nodeName, "epWorldSizePtr is null."), return ge::GRAPH_FAILED); | ||
| 203 | + OP_TILING_CHECK(epRankIdPtr == nullptr, OP_LOGE(nodeName, "epRankIdPtr is null."), return ge::GRAPH_FAILED); | ||
| 204 | + OP_TILING_CHECK(numExpertsPtr == nullptr, OP_LOGE(nodeName, "numExpertsPtr is null."), return ge::GRAPH_FAILED); | ||
| 205 | + OP_TILING_CHECK(nmtPtr == nullptr, OP_LOGE(nodeName, "nmtPtr is null."), return ge::GRAPH_FAILED); | ||
| 206 | + OP_TILING_CHECK(cclBufferSizePtr == nullptr, OP_LOGE(nodeName, "cclBufferSizePtr is null."), | ||
| 207 | + return ge::GRAPH_FAILED); | ||
| 208 | + OP_TILING_CHECK(hasTopkWeightsPtr == nullptr, OP_LOGE(nodeName, "hasTopkWeightsPtr is null."), | ||
| 209 | + return ge::GRAPH_FAILED); | ||
| 210 | + OP_TILING_CHECK(requestedNetworkModePtr == nullptr, OP_LOGE(nodeName, "requestedNetworkModePtr is null."), | ||
| 211 | + return ge::GRAPH_FAILED); | ||
| 212 | + OP_TILING_CHECK(rankNumPerServerPtr == nullptr, OP_LOGE(nodeName, "rankNumPerServerPtr is null."), | ||
| 213 | + return ge::GRAPH_FAILED); | ||
| 214 | + | ||
| 215 | + int64_t epWorldSize = *epWorldSizePtr; | ||
| 216 | + int64_t requestedNetworkMode = *requestedNetworkModePtr; | ||
| 217 | + int64_t rankNumPerServer = *rankNumPerServerPtr; | ||
| 218 | + OP_TILING_CHECK((epWorldSize < MIN_EP_WORLD_SIZE) || (epWorldSize > MAX_EP_WORLD_SIZE), | ||
| 219 | + OP_LOGE(nodeName, "ep_world_size is invalid, should be in [%ld, %ld], but got %ld.", | ||
| 220 | + MIN_EP_WORLD_SIZE, MAX_EP_WORLD_SIZE, epWorldSize), | ||
| 221 | + return ge::GRAPH_FAILED); | ||
| 222 | + OP_TILING_CHECK( | ||
| 223 | + (*epRankIdPtr < 0) || (*epRankIdPtr >= epWorldSize), | ||
| 224 | + OP_LOGE(nodeName, "ep_rank_id is invalid, should be in [0, %ld), but got %ld.", epWorldSize, *epRankIdPtr), | ||
| 225 | + return ge::GRAPH_FAILED); | ||
| 226 | + OP_TILING_CHECK( | ||
| 227 | + (*numExpertsPtr < MIN_NUM_EXPERTS) || (*numExpertsPtr > MAX_NUM_EXPERTS) || (*numExpertsPtr % epWorldSize != 0), | ||
| 228 | + OP_LOGE(nodeName, | ||
| 229 | + "num_experts is invalid, should be in [%ld, %ld] and divisible by ep_world_size, but got " | ||
| 230 | + "num_experts=%ld, ep_world_size=%ld.", | ||
| 231 | + MIN_NUM_EXPERTS, MAX_NUM_EXPERTS, *numExpertsPtr, epWorldSize), | ||
| 232 | + return ge::GRAPH_FAILED); | ||
| 233 | + OP_TILING_CHECK(*nmtPtr <= 0, OP_LOGE(nodeName, "num_max_tokens_per_rank must be positive, but got %ld.", *nmtPtr), | ||
| 234 | + return ge::GRAPH_FAILED); | ||
| 235 | + OP_TILING_CHECK(*cclBufferSizePtr <= 0, | ||
| 236 | + OP_LOGE(nodeName, "ccl_buffer_size must be positive, but got %ld.", *cclBufferSizePtr), | ||
| 237 | + return ge::GRAPH_FAILED); | ||
| 238 | + OP_TILING_CHECK((requestedNetworkMode != NETWORK_DIRECT) && (requestedNetworkMode != NETWORK_HYBRID), | ||
| 239 | + OP_LOGE(nodeName, "topo_type is invalid, expected %u or %u, got %ld.", NETWORK_DIRECT, | ||
| 240 | + NETWORK_HYBRID, requestedNetworkMode), | ||
| 241 | + return ge::GRAPH_FAILED); | ||
| 242 | + OP_TILING_CHECK(rankNumPerServer <= 0, | ||
| 243 | + OP_LOGE(nodeName, "rank_num_per_server must be positive, got %ld.", rankNumPerServer), | ||
| 244 | + return ge::GRAPH_FAILED); | ||
| 245 | + OP_TILING_CHECK(epWorldSize % rankNumPerServer != 0, | ||
| 246 | + OP_LOGE(nodeName, | ||
| 247 | + "epWorldSize must be divisible by rank_num_per_server, got epWorldSize=%ld, " | ||
| 248 | + "rank_num_per_server=%ld.", | ||
| 249 | + epWorldSize, rankNumPerServer), | ||
| 250 | + return ge::GRAPH_FAILED); | ||
| 251 | + | ||
| 252 | + info.cfg.epWorldSize = static_cast<uint32_t>(epWorldSize); | ||
| 253 | + info.cfg.epRankId = static_cast<uint32_t>(*epRankIdPtr); | ||
| 254 | + info.cfg.numExperts = static_cast<uint32_t>(*numExpertsPtr); | ||
| 255 | + info.cfg.numLocalExperts = static_cast<uint32_t>(*numExpertsPtr / epWorldSize); | ||
| 256 | + info.cfg.numMaxTokensPerRank = static_cast<uint32_t>(*nmtPtr); | ||
| 257 | + info.hasTopkWeights = *hasTopkWeightsPtr ? 1 : 0; | ||
| 258 | + serverNum = static_cast<uint32_t>(epWorldSize / rankNumPerServer); | ||
| 259 | + networkMode = (requestedNetworkMode == NETWORK_HYBRID && serverNum > 1U) ? NETWORK_HYBRID : NETWORK_DIRECT; | ||
| 260 | + | ||
| 261 | + return ge::GRAPH_SUCCESS; | ||
| 262 | +} | ||
| 263 | + | ||
| 264 | +static uint64_t AlignUpWin(const uint64_t data) { return (data + COMM_ALIGN - 1) / COMM_ALIGN * COMM_ALIGN; } | ||
| 265 | + | ||
| 266 | +static ge::graphStatus BuildAndCheckWindowLayout(const gert::TilingContext *context, MoeEpCombineEpilogueInfo &info, | ||
| 267 | + uint32_t networkMode, uint32_t serverNum, const char *nodeName) | ||
| 268 | +{ | ||
| 269 | + auto attrs = context->GetAttrs(); | ||
| 270 | + auto cclBufferSizePtr = attrs->GetAttrPointer<int64_t>(ATTR_CCL_BUFFER_SIZE_INDEX); | ||
| 271 | + auto nmtPtr = attrs->GetAttrPointer<int64_t>(ATTR_NUM_MAX_TPR_INDEX); | ||
| 272 | + | ||
| 273 | + uint64_t maxWindowSize = static_cast<uint64_t>(*cclBufferSizePtr); | ||
| 274 | + uint64_t epWorldSize = static_cast<uint64_t>(info.cfg.epWorldSize); | ||
| 275 | + uint64_t nmt = static_cast<uint64_t>(*nmtPtr); | ||
| 276 | + uint64_t topK = static_cast<uint64_t>(info.cfg.topK); | ||
| 277 | + uint64_t moeExpertNumPerRank = static_cast<uint64_t>(info.cfg.numLocalExperts); | ||
| 278 | + uint64_t combineDataSizePerRank = nmt * topK * static_cast<uint64_t>(info.cfg.perSlotBytes); | ||
| 279 | + | ||
| 280 | + uint64_t dispatchNotifySize = (nmt + NOTIFY_CNT_ALIGN - 1) / NOTIFY_CNT_ALIGN * epWorldSize * COMM_ALIGN; | ||
| 281 | + uint64_t dispatchWinStateSize = | ||
| 282 | + epWorldSize * AlignUpWin(moeExpertNumPerRank * sizeof(int32_t)) + epWorldSize * COMM_ALIGN + dispatchNotifySize; | ||
| 283 | + uint64_t combineWinStateSize = nmt * topK * COMM_ALIGN + epWorldSize * COMM_ALIGN; | ||
| 284 | + uint64_t hiddenAlign = (info.cfg.hidden * MAX_OUT_DTYPE_SIZE + UB_ALIGN - 1) / UB_ALIGN * UB_ALIGN; | ||
| 285 | + uint64_t topKAlign = (topK * METADATA_DTYPE_SIZE + UB_ALIGN - 1) / UB_ALIGN * UB_ALIGN; | ||
| 286 | + uint64_t dispatchReservedPerSlotBytes = AlignUpWin(hiddenAlign + topKAlign * 2 + UB_ALIGN); | ||
| 287 | + uint64_t superNodeCount = static_cast<uint64_t>(serverNum); | ||
| 288 | + uint64_t scaleoutSlotRawBytes = dispatchReservedPerSlotBytes + topK * sizeof(int32_t); | ||
| 289 | + uint64_t scaleoutReservedPerSlotBytes = AlignUpWin(scaleoutSlotRawBytes); | ||
| 290 | + | ||
| 291 | + uint64_t combineDataWinOffset; | ||
| 292 | + uint64_t winNeedTotal; | ||
| 293 | + if (networkMode == NETWORK_HYBRID) { | ||
| 294 | + uint64_t scaleoutRecvDataSize = superNodeCount * nmt * scaleoutReservedPerSlotBytes; | ||
| 295 | + uint64_t scaleupFinalRecvDataSize = epWorldSize * nmt * dispatchReservedPerSlotBytes; | ||
| 296 | + uint64_t scaleoutRecvStatusSize = AlignUpWin(superNodeCount * nmt * COMM_ALIGN); | ||
| 297 | + uint64_t scaleoutRecvDataOffset = dispatchWinStateSize + combineWinStateSize; | ||
| 298 | + uint64_t scaleupFinalRecvDataOffset = scaleoutRecvDataOffset + scaleoutRecvDataSize; | ||
| 299 | + uint64_t scaleoutRecvStatusOffset = scaleupFinalRecvDataOffset + scaleupFinalRecvDataSize; | ||
| 300 | + combineDataWinOffset = scaleoutRecvStatusOffset + scaleoutRecvStatusSize; | ||
| 301 | + uint64_t payloadStashWinOffset = combineDataWinOffset + combineDataSizePerRank; | ||
| 302 | + uint64_t payloadStashWinSize = nmt * scaleoutReservedPerSlotBytes; | ||
| 303 | + winNeedTotal = payloadStashWinOffset + payloadStashWinSize; | ||
| 304 | + } else { | ||
| 305 | + uint64_t dispatchRecvWinDataReservedSize = epWorldSize * nmt * dispatchReservedPerSlotBytes; | ||
| 306 | + uint64_t stateAndRecvDataWinSize = | ||
| 307 | + dispatchWinStateSize + combineWinStateSize + dispatchRecvWinDataReservedSize + combineDataSizePerRank; | ||
| 308 | + uint64_t dispatchSendWinDataReservedSize = dispatchRecvWinDataReservedSize; | ||
| 309 | + combineDataWinOffset = dispatchWinStateSize + combineWinStateSize + dispatchRecvWinDataReservedSize; | ||
| 310 | + winNeedTotal = stateAndRecvDataWinSize + dispatchSendWinDataReservedSize; | ||
| 311 | + } | ||
| 312 | + OP_TILING_CHECK(winNeedTotal > maxWindowSize, | ||
| 313 | + OP_LOGE(nodeName, "HCCL_BUFFSIZE too small, need %luMB, available %luMB.", | ||
| 314 | + (winNeedTotal / MB_SIZE) + 1UL, maxWindowSize / MB_SIZE), | ||
| 315 | + return ge::GRAPH_FAILED); | ||
| 316 | + info.combineStateWinOffset = dispatchWinStateSize; | ||
| 317 | + info.combineDataWinOffset = combineDataWinOffset; | ||
| 318 | + | ||
| 319 | + return ge::GRAPH_SUCCESS; | ||
| 320 | +} | ||
| 321 | + | ||
| 322 | +static ge::graphStatus MoeEpCombineEpilogueTilingFunc(gert::TilingContext *context) | ||
| 323 | +{ | ||
| 324 | + context->SetScheduleMode(1U); | ||
| 325 | + const char *nodeName = context->GetNodeName(); | ||
| 326 | + OP_TILING_CHECK(nodeName == nullptr, OP_LOGE("unKnownNodeName", "nodeName is nullptr."), return ge::GRAPH_FAILED); | ||
| 327 | + | ||
| 328 | + MoeEpCombineEpilogueTilingData *tilingData = context->GetTilingData<MoeEpCombineEpilogueTilingData>(); | ||
| 329 | + OP_TILING_CHECK(tilingData == nullptr, OP_LOGE(nodeName, "tilingData is nullptr."), return ge::GRAPH_FAILED); | ||
| 330 | + OP_LOGI(nodeName, "Enter MoeEpCombineEpilogue tiling func."); | ||
| 331 | + | ||
| 332 | + MoeEpCombineEpilogueInfo &info = tilingData->moeEpCombineEpilogueInfo; | ||
| 333 | + uint32_t networkMode = NETWORK_DIRECT; | ||
| 334 | + uint32_t serverNum = 1U; | ||
| 335 | + | ||
| 336 | + OP_TILING_CHECK(CheckAttrParams(context, nodeName, info, networkMode, serverNum) != ge::GRAPH_SUCCESS, | ||
| 337 | + OP_LOGE(nodeName, "Check attr params failed."), return ge::GRAPH_FAILED); | ||
| 338 | + | ||
| 339 | + OP_TILING_CHECK(CheckInputTensorShape(context, nodeName, info) != ge::GRAPH_SUCCESS, | ||
| 340 | + OP_LOGE(nodeName, "Check input tensor shape failed."), return ge::GRAPH_FAILED); | ||
| 341 | + | ||
| 342 | + OP_TILING_CHECK(CheckInputDataType(context, nodeName, info) != ge::GRAPH_SUCCESS, | ||
| 343 | + OP_LOGE(nodeName, "Check input tensor dtype failed."), return ge::GRAPH_FAILED); | ||
| 344 | + | ||
| 345 | + uint32_t hAlign32 = ((info.cfg.hidden * MAX_OUT_DTYPE_SIZE + UB_ALIGN - 1UL) / UB_ALIGN) * UB_ALIGN; | ||
| 346 | + info.cfg.perSlotBytes = static_cast<uint32_t>(((hAlign32 + UB_ALIGN + COMM_ALIGN - 1UL) / COMM_ALIGN) * COMM_ALIGN); | ||
| 347 | + | ||
| 348 | + OP_TILING_CHECK(BuildAndCheckWindowLayout(context, info, networkMode, serverNum, nodeName) != ge::GRAPH_SUCCESS, | ||
| 349 | + OP_LOGE(nodeName, "Check window size failed."), return ge::GRAPH_FAILED); | ||
| 350 | + | ||
| 351 | + size_t *workSpaces = context->GetWorkspaceSizes(1); | ||
| 352 | + OP_TILING_CHECK(workSpaces == nullptr, OP_LOGE(nodeName, "workSpaces is nullptr."), return ge::GRAPH_FAILED); | ||
| 353 | + workSpaces[0] = SYSTEM_NEED_WORKSPACE; | ||
| 354 | + | ||
| 355 | + uint32_t tplHasTopkWeights = info.hasTopkWeights ? 1 : 0; | ||
| 356 | + uint64_t tilingKey = GET_TPL_TILING_KEY(tplHasTopkWeights, TILINGKEY_TPL_A5); | ||
| 357 | + context->SetTilingKey(tilingKey); | ||
| 358 | + | ||
| 359 | + auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo()); | ||
| 360 | + uint32_t aivNum = ascendcPlatform.GetCoreNumAiv(); | ||
| 361 | + uint64_t ubSize = 0UL; | ||
| 362 | + ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); | ||
| 363 | + uint32_t blockDim = ascendcPlatform.CalcTschBlockDim(aivNum, 0, aivNum); | ||
| 364 | + context->SetBlockDim(blockDim); | ||
| 365 | + info.aivNum = aivNum; | ||
| 366 | + info.totalUbSize = ubSize; | ||
| 367 | + | ||
| 368 | + OP_LOGD(nodeName, "tilingKey=%lu, blockDim=%u, aivNum=%u", tilingKey, blockDim, aivNum); | ||
| 369 | + PrintTilingDataInfo(nodeName, info); | ||
| 370 | + return ge::GRAPH_SUCCESS; | ||
| 371 | +} | ||
| 372 | + | ||
| 373 | +struct MoeEpCombineEpilogueCompileInfo {}; | ||
| 374 | +ge::graphStatus TilingParseForMoeEpCombineEpilogue(gert::TilingParseContext *context) | ||
| 375 | +{ | ||
| 376 | + (void)context; | ||
| 377 | + return ge::GRAPH_SUCCESS; | ||
| 378 | +} | ||
| 379 | + | ||
| 380 | +IMPL_OP_OPTILING(MoeEpCombineEpilogue) | ||
| 381 | + .Tiling(MoeEpCombineEpilogueTilingFunc) | ||
| 382 | + .TilingParse<MoeEpCombineEpilogueCompileInfo>(TilingParseForMoeEpCombineEpilogue); | ||
| 383 | +} // namespace | ||
| @@ -0,0 +1,55 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file moe_ep_combine_epilogue.cpp | ||
| 13 | + * \brief MoE Expert-Parallel Combine Epilogue kernel entry — recv + reduce phase | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#if __has_include("version/asc_devkit_version.h") && __has_include("version/hcomm_version.h") | ||
| 17 | +#include "version/asc_devkit_version.h" | ||
| 18 | +#include "version/hcomm_version.h" | ||
| 19 | + | ||
| 20 | +#if (ASC_DEVKIT_MAJOR > 9 || (ASC_DEVKIT_MAJOR == 9 && ASC_DEVKIT_MINOR > 0)) && \ | ||
| 21 | + (HCOMM_MAJOR > 9 || (HCOMM_MAJOR == 9 && HCOMM_MINOR > 0)) | ||
| 22 | +#define ENABLE_MOE_EP_COMBINE_EPILOGUE_KERNEL | ||
| 23 | +#endif | ||
| 24 | + | ||
| 25 | +#endif | ||
| 26 | + | ||
| 27 | +#if ASC_DEVKIT_MAJOR >= 9 | ||
| 28 | +#include "basic_api/kernel_basic_intf.h" | ||
| 29 | +#else | ||
| 30 | +#include "kernel_operator.h" | ||
| 31 | +#endif | ||
| 32 | + | ||
| 33 | +#include "moe_ep_combine_epilogue_tiling_key.h" | ||
| 34 | +#include "moe_ep_combine_epilogue_tiling.h" | ||
| 35 | +#include "moe_ep_combine_epilogue.h" | ||
| 36 | + | ||
| 37 | +using namespace MoeEpCombineEpilogueImpl; | ||
| 38 | +using namespace Mc2Tiling; | ||
| 39 | +using namespace AscendC; | ||
| 40 | + | ||
| 41 | +template <uint32_t HasTopkWeight, uint32_t ArchTag> | ||
| 42 | +__global__ __aicore__ void moe_ep_combine_epilogue(GM_ADDR context, GM_ADDR topkIdx, GM_ADDR combinedX, | ||
| 43 | + GM_ADDR combinedTopkWeights, GM_ADDR workspace, GM_ADDR tilingGM) | ||
| 44 | +{ | ||
| 45 | + REGISTER_TILING_DEFAULT(MoeEpCombineEpilogueTilingData); | ||
| 46 | + REGISTER_TILING_FOR_TILINGKEY("ArchTag == TILINGKEY_TPL_A5", MoeEpCombineEpilogueTilingData); | ||
| 47 | + TPipe pipe; | ||
| 48 | + GET_TILING_DATA_WITH_STRUCT(MoeEpCombineEpilogueTilingData, tilingData, tilingGM); | ||
| 49 | +#if defined(ENABLE_MOE_EP_COMBINE_EPILOGUE_KERNEL) | ||
| 50 | + MoeEpCombineEpilogue<DTYPE_COMBINED_X, HasTopkWeight> op; | ||
| 51 | + op.Init(context, topkIdx, combinedX, combinedTopkWeights, workspace, tilingGM, &pipe, | ||
| 52 | + &tilingData.moeEpCombineEpilogueInfo); | ||
| 53 | + op.Process(); | ||
| 54 | +#endif | ||
| 55 | +} | ||
| @@ -0,0 +1,326 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file moe_ep_combine_epilogue.h | ||
| 13 | + * \brief MoE Expert-Parallel Combine Epilogue kernel — recv + reduce phase. | ||
| 14 | + * Reads expert outputs from HCCL Window, accumulates per-token topK results, | ||
| 15 | + * and writes combined_x / combined_topk_weights. | ||
| 16 | + */ | ||
| 17 | +#ifndef MOE_EP_COMBINE_EPILOGUE_H | ||
| 18 | +#define MOE_EP_COMBINE_EPILOGUE_H | ||
| 19 | + | ||
| 20 | +#if __has_include("version/asc_devkit_version.h") && __has_include("version/hcomm_version.h") | ||
| 21 | +#include "version/asc_devkit_version.h" | ||
| 22 | +#include "version/hcomm_version.h" | ||
| 23 | + | ||
| 24 | +#if (ASC_DEVKIT_MAJOR > 9 || (ASC_DEVKIT_MAJOR == 9 && ASC_DEVKIT_MINOR > 0)) && \ | ||
| 25 | + (HCOMM_MAJOR > 9 || (HCOMM_MAJOR == 9 && HCOMM_MINOR > 0)) | ||
| 26 | +#define ENABLE_MOE_EP_COMBINE_EPILOGUE_KERNEL | ||
| 27 | +#endif | ||
| 28 | + | ||
| 29 | +#endif | ||
| 30 | + | ||
| 31 | +#if ASC_DEVKIT_MAJOR >= 9 | ||
| 32 | +#include "basic_api/kernel_basic_intf.h" | ||
| 33 | +#else | ||
| 34 | +#include "kernel_operator.h" | ||
| 35 | +#endif | ||
| 36 | + | ||
| 37 | +#include "kernel_tiling/kernel_tiling.h" | ||
| 38 | +#include "adv_api/reduce/reduce.h" | ||
| 39 | +#include "adv_api/reduce/sum.h" | ||
| 40 | +#if __has_include("../common/moe_distribute_base.h") | ||
| 41 | +#include "../common/moe_distribute_base.h" | ||
| 42 | +#include "../common/mc2_kernel_utils.h" | ||
| 43 | +#include "../common/mc2_moe_context.h" | ||
| 44 | +#else | ||
| 45 | +#include "../../common/op_kernel/moe_distribute_base.h" | ||
| 46 | +#include "../../common/op_kernel/mc2_kernel_utils.h" | ||
| 47 | +#include "../../common/op_kernel/mc2_moe_context.h" | ||
| 48 | +#endif | ||
| 49 | + | ||
| 50 | +#include "moe_ep_combine_epilogue_tiling_key.h" | ||
| 51 | +#include "moe_ep_combine_epilogue_tiling.h" | ||
| 52 | + | ||
| 53 | +namespace MoeEpCombineEpilogueImpl { | ||
| 54 | + | ||
| 55 | +#if defined(ENABLE_MOE_EP_COMBINE_EPILOGUE_KERNEL) | ||
| 56 | + | ||
| 57 | +using namespace AscendC; | ||
| 58 | + | ||
| 59 | +#define TemplateMoeEpCombineEpilogueTypeClass typename XType, uint32_t HasTopkWeight | ||
| 60 | +#define TemplateMoeEpCombineEpilogueTypeFunc XType, HasTopkWeight | ||
| 61 | + | ||
| 62 | +static constexpr uint32_t WIN_ADDR_ALIGN = 512; | ||
| 63 | +constexpr uint64_t UB_ALIGN = 32UL; | ||
| 64 | +constexpr uint32_t STATE_OFFSET = 32U; | ||
| 65 | +constexpr uint32_t DOUBLE_BUFFER_NUM = 2U; | ||
| 66 | +constexpr uint32_t FLOAT_PER_UB_ALIGN = 8U; | ||
| 67 | + | ||
| 68 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 69 | +class MoeEpCombineEpilogue { | ||
| 70 | +public: | ||
| 71 | + __aicore__ inline MoeEpCombineEpilogue(){}; | ||
| 72 | + | ||
| 73 | + __aicore__ inline void Init(GM_ADDR context, GM_ADDR topkIdx, GM_ADDR combinedX, GM_ADDR combinedTopkWeights, | ||
| 74 | + GM_ADDR workspace, GM_ADDR tilingGM, TPipe *pipe, | ||
| 75 | + const MoeEpCombineEpilogueInfo *tilingData); | ||
| 76 | + | ||
| 77 | + __aicore__ inline void Process(); | ||
| 78 | + | ||
| 79 | +private: | ||
| 80 | + __aicore__ inline void SplitToCore(uint32_t curSendCnt, uint32_t curUseAivNum, uint32_t &startTokenId, | ||
| 81 | + uint32_t &endTokenId, uint32_t &tokenPerAivNum); | ||
| 82 | + __aicore__ inline void BuffInit(); | ||
| 83 | + __aicore__ inline bool WaitDispatch(uint32_t tokenIndex, uint32_t copyCount); | ||
| 84 | + __aicore__ inline void ProcessTopKToken(uint32_t tokenIndex); | ||
| 85 | + __aicore__ inline void RecvPhaseReduce(); | ||
| 86 | + | ||
| 87 | + __aicore__ inline GM_ADDR GetUrmaWinAddrByRankId(uint32_t rankId, uint64_t offset) | ||
| 88 | + { | ||
| 89 | + return (GM_ADDR)(winRankAddr_[rankId] + offset); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + __aicore__ inline GM_ADDR GetUrmaStateAddrByRankId(uint32_t rankId, uint64_t offset) | ||
| 93 | + { | ||
| 94 | + return (GM_ADDR)(winRankAddr_[rankId] + offset); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + TPipe *tpipe_{nullptr}; | ||
| 98 | + const MoeEpCombineEpilogueInfo *tilingData_{nullptr}; | ||
| 99 | + __gm__ Mc2Aclnn::MoeCommContext *mc2Context_{nullptr}; | ||
| 100 | + | ||
| 101 | + uint32_t rankId_{0}; | ||
| 102 | + uint32_t epWorldSize_{0}; | ||
| 103 | + uint32_t numTokens_{0}; | ||
| 104 | + uint32_t topK_{0}; | ||
| 105 | + uint32_t axisH_{0}; | ||
| 106 | + uint32_t hAlignSize_{0}; | ||
| 107 | + uint64_t combineStateWinOffset_{0}; | ||
| 108 | + uint64_t combineDataWinOffset_{0}; | ||
| 109 | + | ||
| 110 | + uint32_t aivNum_{0}; | ||
| 111 | + uint32_t aivId_{0}; | ||
| 112 | + | ||
| 113 | + uint32_t tStart_{0}; | ||
| 114 | + uint32_t tEnd_{0}; | ||
| 115 | + uint32_t tPerCore_{0}; | ||
| 116 | + | ||
| 117 | + GlobalTensor<XType> combinedXGm_; | ||
| 118 | + GlobalTensor<float> combinedTopkWeightsGm_; | ||
| 119 | + | ||
| 120 | + LocalTensor<float> ubAccFp32_; | ||
| 121 | + LocalTensor<float> ubTmpFp32_; | ||
| 122 | + | ||
| 123 | + TQue<QuePosition::VECIN, 1> xInQue_; | ||
| 124 | + TQue<QuePosition::VECOUT, 1> xOutQue_; | ||
| 125 | + TQueBind<QuePosition::VECIN, QuePosition::VECOUT, 1> weightQue_; | ||
| 126 | + TBuf<QuePosition::VECIN> ubAccFp32Buf_; | ||
| 127 | + TBuf<QuePosition::VECIN> ubTmpFp32Buf_; | ||
| 128 | + TBuf<> tokenStatusBuf_; | ||
| 129 | + TBuf<> stateBuf_; | ||
| 130 | + TBuf<> stateSumBuf_; | ||
| 131 | + TBuf<> stateResetBuf_; | ||
| 132 | + | ||
| 133 | + GM_ADDR winRankAddr_[Mc2Aclnn::HCCL_MAX_RANK_SIZE]; | ||
| 134 | +}; | ||
| 135 | + | ||
| 136 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 137 | +__aicore__ inline void MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::Init( | ||
| 138 | + GM_ADDR context, GM_ADDR topkIdx, GM_ADDR combinedX, GM_ADDR combinedTopkWeights, GM_ADDR workspace, | ||
| 139 | + GM_ADDR tilingGM, TPipe *pipe, const MoeEpCombineEpilogueInfo *tilingData) | ||
| 140 | +{ | ||
| 141 | + tpipe_ = pipe; | ||
| 142 | + tilingData_ = tilingData; | ||
| 143 | + aivId_ = GetBlockIdx(); | ||
| 144 | + epWorldSize_ = tilingData_->cfg.epWorldSize; | ||
| 145 | + numTokens_ = tilingData_->cfg.numTokens; | ||
| 146 | + topK_ = tilingData_->cfg.topK; | ||
| 147 | + axisH_ = tilingData_->cfg.hidden; | ||
| 148 | + aivNum_ = tilingData->aivNum; | ||
| 149 | + hAlignSize_ = Ceil(axisH_ * sizeof(XType), UB_ALIGN) * UB_ALIGN; | ||
| 150 | + | ||
| 151 | + mc2Context_ = reinterpret_cast<__gm__ Mc2Aclnn::MoeCommContext *>(context); | ||
| 152 | + rankId_ = mc2Context_->epRankId; | ||
| 153 | + for (uint32_t i = 0; i < epWorldSize_; ++i) { | ||
| 154 | + winRankAddr_[i] = (GM_ADDR)mc2Context_->epHcclBuffer[i]; | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + combineStateWinOffset_ = tilingData->combineStateWinOffset; | ||
| 158 | + combineDataWinOffset_ = tilingData->combineDataWinOffset; | ||
| 159 | + | ||
| 160 | + combinedXGm_.SetGlobalBuffer((__gm__ XType *)combinedX); | ||
| 161 | + | ||
| 162 | + if constexpr (HasTopkWeight == 1) { | ||
| 163 | + combinedTopkWeightsGm_.SetGlobalBuffer((__gm__ float *)combinedTopkWeights); | ||
| 164 | + } | ||
| 165 | +} | ||
| 166 | + | ||
| 167 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 168 | +__aicore__ inline void MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::SplitToCore( | ||
| 169 | + uint32_t curSendCnt, uint32_t curUseAivNum, uint32_t &startTokenId, uint32_t &endTokenId, uint32_t &sendTokenNum) | ||
| 170 | +{ | ||
| 171 | + sendTokenNum = curSendCnt / curUseAivNum; | ||
| 172 | + uint32_t remainderTokenNum = curSendCnt % curUseAivNum; | ||
| 173 | + uint32_t newAivId = aivId_; | ||
| 174 | + | ||
| 175 | + startTokenId = sendTokenNum * newAivId; | ||
| 176 | + if (newAivId < remainderTokenNum) { | ||
| 177 | + sendTokenNum += 1; | ||
| 178 | + startTokenId += newAivId; | ||
| 179 | + } else { | ||
| 180 | + startTokenId += remainderTokenNum; | ||
| 181 | + } | ||
| 182 | + endTokenId = startTokenId + sendTokenNum; | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 186 | +__aicore__ inline void MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::BuffInit() | ||
| 187 | +{ | ||
| 188 | + SplitToCore(numTokens_, aivNum_, tStart_, tEnd_, tPerCore_); | ||
| 189 | + if (tStart_ >= numTokens_) { | ||
| 190 | + return; | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + if constexpr (HasTopkWeight == 1) { | ||
| 194 | + tpipe_->InitBuffer(weightQue_, DOUBLE_BUFFER_NUM, UB_ALIGN); | ||
| 195 | + } | ||
| 196 | + tpipe_->InitBuffer(xInQue_, DOUBLE_BUFFER_NUM, hAlignSize_); | ||
| 197 | + tpipe_->InitBuffer(xOutQue_, DOUBLE_BUFFER_NUM, hAlignSize_); | ||
| 198 | + uint32_t ubFp32Bytes = Ceil(axisH_ * sizeof(float), UB_ALIGN) * UB_ALIGN; | ||
| 199 | + tpipe_->InitBuffer(ubAccFp32Buf_, ubFp32Bytes); | ||
| 200 | + tpipe_->InitBuffer(ubTmpFp32Buf_, ubFp32Bytes); | ||
| 201 | + | ||
| 202 | + ubAccFp32_ = ubAccFp32Buf_.Get<float>(); | ||
| 203 | + ubTmpFp32_ = ubTmpFp32Buf_.Get<float>(); | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 207 | +__aicore__ inline bool MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::WaitDispatch(uint32_t tokenIndex, | ||
| 208 | + uint32_t copyCount) | ||
| 209 | +{ | ||
| 210 | + GM_ADDR stateGM = GetUrmaStateAddrByRankId(rankId_, combineStateWinOffset_) + tokenIndex * topK_ * WIN_ADDR_ALIGN; | ||
| 211 | + GlobalTensor<uint32_t> stateGMTensor; | ||
| 212 | + stateGMTensor.SetGlobalBuffer((__gm__ uint32_t *)stateGM); | ||
| 213 | + | ||
| 214 | + LocalTensor<uint32_t> stateTensor = stateBuf_.Get<uint32_t>(); | ||
| 215 | + SyncFunc<AscendC::HardEvent::S_MTE2>(); | ||
| 216 | + DataCopyExtParams params = {static_cast<uint16_t>(topK_), UB_ALIGN, WIN_ADDR_ALIGN - UB_ALIGN, 0, 0}; | ||
| 217 | + DataCopyPadExtParams<uint32_t> padParams = {false, 0, 0, 0}; | ||
| 218 | + | ||
| 219 | + DataCopyPad<uint32_t>(stateTensor, stateGMTensor, params, padParams); | ||
| 220 | + SyncFunc<AscendC::HardEvent::MTE2_V>(); | ||
| 221 | + | ||
| 222 | + LocalTensor<uint32_t> stateSumTensor = stateSumBuf_.Get<uint32_t>(); | ||
| 223 | + uint32_t shape[] = {topK_, UB_ALIGN / sizeof(uint32_t)}; | ||
| 224 | + ReduceSum<uint32_t, AscendC::Pattern::Reduce::RA, false>(stateSumTensor, stateTensor, shape, true); | ||
| 225 | + SyncFunc<AscendC::HardEvent::V_S>(); | ||
| 226 | + | ||
| 227 | + uint32_t localState = stateSumTensor(1); | ||
| 228 | + if (localState == copyCount) { | ||
| 229 | + return true; | ||
| 230 | + } | ||
| 231 | + return false; | ||
| 232 | +} | ||
| 233 | + | ||
| 234 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 235 | +__aicore__ inline void MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::ProcessTopKToken(uint32_t tokenIndex) | ||
| 236 | +{ | ||
| 237 | + Duplicate<float>(ubAccFp32_, (float)0, axisH_); | ||
| 238 | + DataCopyPadParams padParams = {false, 0, 0, 0}; | ||
| 239 | + DataCopyParams xCopyParams = {1U, static_cast<uint16_t>(hAlignSize_), 0U, 0U}; | ||
| 240 | + DataCopyParams weightCopyParams = {1U, static_cast<uint16_t>(sizeof(float)), 0U, 0U}; | ||
| 241 | + for (uint32_t topkId = 0U; topkId < topK_; topkId++) { | ||
| 242 | + uint64_t slotOffset = (static_cast<uint64_t>(tokenIndex) * topK_ + topkId) * tilingData_->cfg.perSlotBytes; | ||
| 243 | + GM_ADDR wAddr = GetUrmaWinAddrByRankId(rankId_, combineDataWinOffset_) + slotOffset; | ||
| 244 | + GlobalTensor<XType> srcTokenTensor; | ||
| 245 | + srcTokenTensor.SetGlobalBuffer(reinterpret_cast<__gm__ XType *>(wAddr)); | ||
| 246 | + LocalTensor<XType> xLocal = xInQue_.AllocTensor<XType>(); | ||
| 247 | + DataCopyPad(xLocal, srcTokenTensor, xCopyParams, padParams); | ||
| 248 | + xInQue_.EnQue(xLocal); | ||
| 249 | + LocalTensor<XType> xIn = xInQue_.DeQue<XType>(); | ||
| 250 | + Cast(ubTmpFp32_, xIn, AscendC::RoundMode::CAST_NONE, axisH_); | ||
| 251 | + Add(ubAccFp32_, ubAccFp32_, ubTmpFp32_, axisH_); | ||
| 252 | + xInQue_.FreeTensor(xIn); | ||
| 253 | + if constexpr (HasTopkWeight == 1) { | ||
| 254 | + GM_ADDR weightAddr = GetUrmaStateAddrByRankId(rankId_, combineStateWinOffset_) + | ||
| 255 | + (tokenIndex * topK_ + topkId) * WIN_ADDR_ALIGN; | ||
| 256 | + GlobalTensor<float> srcWeightTensor; | ||
| 257 | + srcWeightTensor.SetGlobalBuffer(reinterpret_cast<__gm__ float *>(weightAddr)); | ||
| 258 | + LocalTensor<float> weightLocal = weightQue_.AllocTensor<float>(); | ||
| 259 | + DataCopyPad(weightLocal, srcWeightTensor, weightCopyParams, padParams); | ||
| 260 | + weightQue_.EnQue(weightLocal); | ||
| 261 | + LocalTensor<float> weightOut = weightQue_.DeQue<float>(); | ||
| 262 | + DataCopyPad(combinedTopkWeightsGm_[tokenIndex * topK_ + topkId], weightOut, weightCopyParams); | ||
| 263 | + weightQue_.FreeTensor(weightOut); | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + LocalTensor<XType> ubResultBf16 = xOutQue_.AllocTensor<XType>(); | ||
| 267 | + Cast(ubResultBf16, ubAccFp32_, RoundMode::CAST_RINT, axisH_); | ||
| 268 | + xOutQue_.EnQue(ubResultBf16); | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 272 | +__aicore__ inline void MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::RecvPhaseReduce() | ||
| 273 | +{ | ||
| 274 | + if (tPerCore_ == 0) { | ||
| 275 | + return; | ||
| 276 | + } | ||
| 277 | + DataCopyPadParams padParams = {false, 0, 0, 0}; | ||
| 278 | + DataCopyParams xCopyParams = {1U, static_cast<uint16_t>(axisH_ * sizeof(XType)), 0U, 0U}; | ||
| 279 | + tpipe_->InitBuffer(tokenStatusBuf_, Ceil(tPerCore_ * sizeof(int32_t), UB_ALIGN) * UB_ALIGN); | ||
| 280 | + tpipe_->InitBuffer(stateBuf_, topK_ * STATE_OFFSET); | ||
| 281 | + tpipe_->InitBuffer(stateSumBuf_, UB_ALIGN); | ||
| 282 | + tpipe_->InitBuffer(stateResetBuf_, topK_ * STATE_OFFSET); | ||
| 283 | + LocalTensor<uint32_t> stateResetTensor = stateResetBuf_.Get<uint32_t>(); | ||
| 284 | + Duplicate<uint32_t>(stateResetTensor, (uint32_t)0.0, static_cast<uint32_t>(topK_ * FLOAT_PER_UB_ALIGN)); | ||
| 285 | + LocalTensor<int32_t> tokenStatusTensor = tokenStatusBuf_.Get<int32_t>(); | ||
| 286 | + Duplicate<int32_t>(tokenStatusTensor, static_cast<int32_t>(0), tPerCore_); | ||
| 287 | + SyncFunc<AscendC::HardEvent::V_S>(); | ||
| 288 | + uint32_t CompletedtokenNum = static_cast<uint32_t>(0); | ||
| 289 | + uint32_t copyCount = topK_; | ||
| 290 | + while (CompletedtokenNum != tPerCore_) { | ||
| 291 | + for (uint32_t tokenIdx = tStart_; tokenIdx < tEnd_; ++tokenIdx) { | ||
| 292 | + if (tokenStatusTensor(tokenIdx - tStart_) == 1) { | ||
| 293 | + continue; | ||
| 294 | + } | ||
| 295 | + if (!WaitDispatch(tokenIdx, copyCount)) { | ||
| 296 | + continue; | ||
| 297 | + } | ||
| 298 | + CompletedtokenNum++; | ||
| 299 | + tokenStatusTensor.SetValue(tokenIdx - tStart_, 1); | ||
| 300 | + ProcessTopKToken(tokenIdx); | ||
| 301 | + LocalTensor<XType> ubResultBf16 = xOutQue_.DeQue<XType>(); | ||
| 302 | + DataCopyPad(combinedXGm_[tokenIdx * axisH_], ubResultBf16, xCopyParams); | ||
| 303 | + xOutQue_.FreeTensor(ubResultBf16); | ||
| 304 | + | ||
| 305 | + GM_ADDR stateGM = | ||
| 306 | + GetUrmaStateAddrByRankId(rankId_, combineStateWinOffset_) + tokenIdx * topK_ * WIN_ADDR_ALIGN; | ||
| 307 | + GlobalTensor<uint32_t> stateGMTensor; | ||
| 308 | + stateGMTensor.SetGlobalBuffer((__gm__ uint32_t *)stateGM); | ||
| 309 | + DataCopyExtParams resetParams = {static_cast<uint16_t>(topK_), UB_ALIGN, 0, WIN_ADDR_ALIGN - UB_ALIGN, 0}; | ||
| 310 | + DataCopyPad<uint32_t>(stateGMTensor, stateResetTensor, resetParams); | ||
| 311 | + } | ||
| 312 | + } | ||
| 313 | +} | ||
| 314 | + | ||
| 315 | +template <TemplateMoeEpCombineEpilogueTypeClass> | ||
| 316 | +__aicore__ inline void MoeEpCombineEpilogue<TemplateMoeEpCombineEpilogueTypeFunc>::Process() | ||
| 317 | +{ | ||
| 318 | + BuffInit(); | ||
| 319 | + RecvPhaseReduce(); | ||
| 320 | +} | ||
| 321 | + | ||
| 322 | +#endif | ||
| 323 | + | ||
| 324 | +} // namespace MoeEpCombineEpilogueImpl | ||
| 325 | + | ||
| 326 | +#endif // MOE_EP_COMBINE_EPILOGUE_H | ||
| @@ -0,0 +1,41 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MOE_EP_COMBINE_EPILOGUE_TILING_H | ||
| 12 | +#define MOE_EP_COMBINE_EPILOGUE_TILING_H | ||
| 13 | + | ||
| 14 | +struct MoeEpCommonTilingData { | ||
| 15 | + uint32_t epWorldSize; | ||
| 16 | + uint32_t epRankId; | ||
| 17 | + uint32_t numExperts; | ||
| 18 | + uint32_t numLocalExperts; | ||
| 19 | + uint32_t numTokens; | ||
| 20 | + uint32_t hidden; | ||
| 21 | + uint32_t topK; | ||
| 22 | + uint32_t numMaxTokensPerRank; | ||
| 23 | + uint32_t scalesBytes; | ||
| 24 | + uint32_t perSlotBytes; | ||
| 25 | + uint32_t expertAlignment; | ||
| 26 | +}; | ||
| 27 | + | ||
| 28 | +struct MoeEpCombineEpilogueInfo { | ||
| 29 | + MoeEpCommonTilingData cfg; | ||
| 30 | + uint32_t hasTopkWeights = 0; | ||
| 31 | + uint32_t aivNum = 0; | ||
| 32 | + uint64_t totalUbSize = 0; | ||
| 33 | + uint64_t combineStateWinOffset = 0; | ||
| 34 | + uint64_t combineDataWinOffset = 0; | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +struct MoeEpCombineEpilogueTilingData { | ||
| 38 | + MoeEpCombineEpilogueInfo moeEpCombineEpilogueInfo; | ||
| 39 | +}; | ||
| 40 | + | ||
| 41 | +#endif // MOE_EP_COMBINE_EPILOGUE_TILING_H | ||
| @@ -0,0 +1,28 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef MOE_EP_COMBINE_EPILOGUE_TILING_KEY_H | ||
| 12 | +#define MOE_EP_COMBINE_EPILOGUE_TILING_KEY_H | ||
| 13 | + | ||
| 14 | +#include "ascendc/host_api/tiling/template_argument.h" | ||
| 15 | + | ||
| 16 | +namespace Mc2Tiling { | ||
| 17 | + | ||
| 18 | +#define TILINGKEY_TPL_A5 2 | ||
| 19 | + | ||
| 20 | +ASCENDC_TPL_ARGS_DECL(MoeEpCombineEpilogue, ASCENDC_TPL_BOOL_DECL(TILINGKEY_HAS_TOPK_WEIGHTS, 0, 1), | ||
| 21 | + ASCENDC_TPL_UINT_DECL(ARCH_TAG, ASCENDC_TPL_2_BW, ASCENDC_TPL_UI_LIST, TILINGKEY_TPL_A5)); | ||
| 22 | + | ||
| 23 | +ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(TILINGKEY_HAS_TOPK_WEIGHTS, 0, 1), | ||
| 24 | + ASCENDC_TPL_UINT_SEL(ARCH_TAG, ASCENDC_TPL_UI_LIST, TILINGKEY_TPL_A5), | ||
| 25 | + ASCENDC_TPL_TILING_STRUCT_SEL(MoeEpCombineEpilogueTilingData))); | ||
| 26 | + | ||
| 27 | +} // namespace Mc2Tiling | ||
| 28 | +#endif // MOE_EP_COMBINE_EPILOGUE_TILING_KEY_H | ||
| @@ -786,7 +786,7 @@ public: | |||
| 786 | using DispatchTensorList = std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor, at::Tensor, at::Tensor>; | 786 | using DispatchTensorList = std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor, at::Tensor, at::Tensor>; |
| 787 | using DispatchEpilogueTensorList = | 787 | using DispatchEpilogueTensorList = |
| 788 | std::tuple<at::Tensor, at::Tensor, c10::optional<at::Tensor>, c10::optional<at::Tensor>>; | 788 | std::tuple<at::Tensor, at::Tensor, c10::optional<at::Tensor>, c10::optional<at::Tensor>>; |
| 789 | - using CombineTensorList = std::tuple<at::Tensor, c10::optional<at::Tensor>>; | 789 | + using CombineEpilogueTensorList = std::tuple<at::Tensor, c10::optional<at::Tensor>>; |
| 790 | 790 | ||
| 791 | DispatchTensorList MoeEpDispatch(const at::Tensor &x, const at::Tensor &topkIdx, | 791 | DispatchTensorList MoeEpDispatch(const at::Tensor &x, const at::Tensor &topkIdx, |
| 792 | const c10::optional<at::Tensor> &topkWeights, | 792 | const c10::optional<at::Tensor> &topkWeights, |
| @@ -802,10 +802,14 @@ public: | |||
| 802 | const c10::optional<at::Tensor> &cachedRecvSrcMetadata, int64_t epWorldSize, int64_t epRankId, | 802 | const c10::optional<at::Tensor> &cachedRecvSrcMetadata, int64_t epWorldSize, int64_t epRankId, |
| 803 | int64_t numExperts, int64_t numMaxTokensPerRank, at::Tensor &recvX, at::Tensor &recvSrcMetadata, | 803 | int64_t numExperts, int64_t numMaxTokensPerRank, at::Tensor &recvX, at::Tensor &recvSrcMetadata, |
| 804 | const c10::optional<at::Tensor> &recvTopkWeightsOpt, const c10::optional<at::Tensor> &recvScalesOpt); | 804 | const c10::optional<at::Tensor> &recvTopkWeightsOpt, const c10::optional<at::Tensor> &recvScalesOpt); |
| 805 | - CombineTensorList MoeEpCombine(const at::Tensor &x, const at::Tensor &topkIdx, const at::Tensor &recvSrcMetadata, | 805 | + void MoeEpCombine(const at::Tensor &x, const at::Tensor &topkIdx, const at::Tensor &recvSrcMetadata, |
| 806 | - const at::Tensor &numRecvTokensPerExpert, | 806 | + const at::Tensor &numRecvTokensPerExpert, const c10::optional<at::Tensor> &topkWeights, |
| 807 | - const c10::optional<at::Tensor> &topkWeights, int64_t epWorldSize, int64_t epRankId, | 807 | + int64_t epWorldSize, int64_t epRankId, int64_t numExperts, int64_t numMaxTokensPerRank); |
| 808 | - int64_t numExperts, int64_t numMaxTokensPerRank); | 808 | + CombineEpilogueTensorList MoeEpCombineEpilogue(const at::Tensor &topkIdx, |
| 809 | + const c10::optional<at::Tensor> &topkWeights, int64_t epWorldSize, | ||
| 810 | + int64_t epRankId, int64_t numExperts, int64_t numMaxTokensPerRank, | ||
| 811 | + at::Tensor &combinedX, | ||
| 812 | + const c10::optional<at::Tensor> &combinedTopkWeightsOpt); | ||
| 809 | 813 | ||
| 810 | private: | 814 | private: |
| 811 | void EnsureEngramContext(); | 815 | void EnsureEngramContext(); |
| @@ -1298,10 +1302,10 @@ Mc2Api::ElasticBuffer::DispatchEpilogueTensorList Mc2Api::ElasticBuffer::MoeEpDi | |||
| 1298 | return std::make_tuple(recvX, recvSrcMetadata, recvTopkWeightsOutput, recvScalesOutput); | 1302 | return std::make_tuple(recvX, recvSrcMetadata, recvTopkWeightsOutput, recvScalesOutput); |
| 1299 | } | 1303 | } |
| 1300 | 1304 | ||
| 1301 | -Mc2Api::ElasticBuffer::CombineTensorList Mc2Api::ElasticBuffer::MoeEpCombine( | 1305 | +void Mc2Api::ElasticBuffer::MoeEpCombine(const at::Tensor &x, const at::Tensor &topkIdx, |
| 1302 | - const at::Tensor &x, const at::Tensor &topkIdx, const at::Tensor &recvSrcMetadata, | 1306 | + const at::Tensor &recvSrcMetadata, const at::Tensor &numRecvTokensPerExpert, |
| 1303 | - const at::Tensor &numRecvTokensPerExpert, const c10::optional<at::Tensor> &topkWeights, int64_t epWorldSize, | 1307 | + const c10::optional<at::Tensor> &topkWeights, int64_t epWorldSize, |
| 1304 | - int64_t epRankId, int64_t numExperts, int64_t numMaxTokensPerRank) | 1308 | + int64_t epRankId, int64_t numExperts, int64_t numMaxTokensPerRank) |
| 1305 | { | 1309 | { |
| 1306 | TORCH_CHECK(x.dim() == DIM_TWO, "x dims must be 2, but got ", x.dim()); | 1310 | TORCH_CHECK(x.dim() == DIM_TWO, "x dims must be 2, but got ", x.dim()); |
| 1307 | TORCH_CHECK(topkIdx.dim() == DIM_TWO, "topk_idx dims must be 2, but got ", topkIdx.dim()); | 1311 | TORCH_CHECK(topkIdx.dim() == DIM_TWO, "topk_idx dims must be 2, but got ", topkIdx.dim()); |
| @@ -1309,29 +1313,35 @@ Mc2Api::ElasticBuffer::CombineTensorList Mc2Api::ElasticBuffer::MoeEpCombine( | |||
| 1309 | int64_t rankNumPerServer = ResolveRankNumPerServer(epWorldSize); | 1313 | int64_t rankNumPerServer = ResolveRankNumPerServer(epWorldSize); |
| 1310 | int64_t topoType = ResolveTopoType(epWorldSize, rankNumPerServer); | 1314 | int64_t topoType = ResolveTopoType(epWorldSize, rankNumPerServer); |
| 1311 | 1315 | ||
| 1312 | - int64_t numTokens = topkIdx.size(0); | ||
| 1313 | - int64_t hidden = x.size(1); | ||
| 1314 | - int64_t topK = topkIdx.size(1); | ||
| 1315 | - | ||
| 1316 | - at::Tensor combinedX = at::empty({numTokens, hidden}, x.options()); | ||
| 1317 | - at::Tensor combinedTopkWeights; | ||
| 1318 | - if (topkWeights.has_value()) { | ||
| 1319 | - combinedTopkWeights = at::empty({numTokens, topK}, x.options().dtype(at::kFloat)); | ||
| 1320 | - } else { | ||
| 1321 | - combinedTopkWeights = at::Tensor(); | ||
| 1322 | - } | ||
| 1323 | - | ||
| 1324 | c10::optional<at::Tensor> topkWeightsOpt = topkWeights; | 1316 | c10::optional<at::Tensor> topkWeightsOpt = topkWeights; |
| 1325 | 1317 | ||
| 1326 | ACLNN_CMD(aclnnMoeEpCombine, moeContextTensor_, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, topkWeightsOpt, | 1318 | ACLNN_CMD(aclnnMoeEpCombine, moeContextTensor_, x, topkIdx, recvSrcMetadata, numRecvTokensPerExpert, topkWeightsOpt, |
| 1327 | - epWorldSize, epRankId, numExperts, numMaxTokensPerRank, moeCclBufferSize_, topoType, rankNumPerServer, | 1319 | + epWorldSize, epRankId, numExperts, numMaxTokensPerRank, moeCclBufferSize_, topoType, rankNumPerServer); |
| 1328 | - combinedX, combinedTopkWeights); | 1320 | +} |
| 1329 | 1321 | ||
| 1330 | - c10::optional<at::Tensor> combinedTopkWeightsOpt; | 1322 | +Mc2Api::ElasticBuffer::CombineEpilogueTensorList Mc2Api::ElasticBuffer::MoeEpCombineEpilogue( |
| 1331 | - if (topkWeights.has_value()) { | 1323 | + const at::Tensor &topkIdx, const c10::optional<at::Tensor> &topkWeights, int64_t epWorldSize, int64_t epRankId, |
| 1332 | - combinedTopkWeightsOpt = combinedTopkWeights; | 1324 | + int64_t numExperts, int64_t numMaxTokensPerRank, at::Tensor &combinedX, |
| 1325 | + const c10::optional<at::Tensor> &combinedTopkWeightsOpt) | ||
| 1326 | +{ | ||
| 1327 | + EnsureMoeContext(); | ||
| 1328 | + int64_t rankNumPerServer = ResolveRankNumPerServer(epWorldSize); | ||
| 1329 | + int64_t topoType = ResolveTopoType(epWorldSize, rankNumPerServer); | ||
| 1330 | + | ||
| 1331 | + bool hasTopkWeights = topkWeights.has_value(); | ||
| 1332 | + at::Tensor combinedTopkWeightsTensor = combinedTopkWeightsOpt.has_value() ? | ||
| 1333 | + *combinedTopkWeightsOpt : | ||
| 1334 | + at::empty({1}, combinedX.options().dtype(at::kFloat)); | ||
| 1335 | + | ||
| 1336 | + ACLNN_CMD(aclnnMoeEpCombineEpilogue, moeContextTensor_, topkIdx, epWorldSize, epRankId, numExperts, | ||
| 1337 | + numMaxTokensPerRank, moeCclBufferSize_, hasTopkWeights, topoType, rankNumPerServer, combinedX, | ||
| 1338 | + combinedTopkWeightsTensor); | ||
| 1339 | + | ||
| 1340 | + c10::optional<at::Tensor> combinedTopkWeightsOutput; | ||
| 1341 | + if (hasTopkWeights) { | ||
| 1342 | + combinedTopkWeightsOutput = *combinedTopkWeightsOpt; | ||
| 1333 | } | 1343 | } |
| 1334 | - return std::make_tuple(combinedX, combinedTopkWeightsOpt); | 1344 | + return std::make_tuple(combinedX, combinedTopkWeightsOutput); |
| 1335 | } | 1345 | } |
| 1336 | 1346 | ||
| 1337 | // PyBind11 module definition | 1347 | // PyBind11 module definition |
| @@ -1376,6 +1386,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) | |||
| 1376 | .def("moe_ep_dispatch", &Mc2Api::ElasticBuffer::MoeEpDispatch) | 1386 | .def("moe_ep_dispatch", &Mc2Api::ElasticBuffer::MoeEpDispatch) |
| 1377 | .def("moe_ep_dispatch_epilogue", &Mc2Api::ElasticBuffer::MoeEpDispatchEpilogue) | 1387 | .def("moe_ep_dispatch_epilogue", &Mc2Api::ElasticBuffer::MoeEpDispatchEpilogue) |
| 1378 | .def("moe_ep_combine", &Mc2Api::ElasticBuffer::MoeEpCombine) | 1388 | .def("moe_ep_combine", &Mc2Api::ElasticBuffer::MoeEpCombine) |
| 1389 | + .def("moe_ep_combine_epilogue", &Mc2Api::ElasticBuffer::MoeEpCombineEpilogue) | ||
| 1379 | .def_static("get_engram_storage_size_hint", &Mc2Api::ElasticBuffer::GetEngramStorageSizeHint, | 1390 | .def_static("get_engram_storage_size_hint", &Mc2Api::ElasticBuffer::GetEngramStorageSizeHint, |
| 1380 | pybind11::arg("numEntries"), pybind11::arg("hiddenSize"), pybind11::arg("dtype") = at::kBFloat16); | 1391 | pybind11::arg("numEntries"), pybind11::arg("hiddenSize"), pybind11::arg("dtype") = at::kBFloat16); |
| 1381 | } | 1392 | } |
| @@ -926,7 +926,17 @@ class ElasticBuffer: | |||
| 926 | lambda: ("bias is not supported, please set bias to None."), | 926 | lambda: ("bias is not supported, please set bias to None."), |
| 927 | ) | 927 | ) |
| 928 | 928 | ||
| 929 | - combined_x, combined_topk_weights = self._runtime.moe_ep_combine( | 929 | + num_tokens = handle.topk_idx.shape[0] |
| 930 | + hidden = x.shape[1] | ||
| 931 | + topk = handle.topk_idx.shape[1] | ||
| 932 | + combined_x = torch.empty((num_tokens, hidden), dtype=x.dtype, device=x.device) | ||
| 933 | + combined_topk_weights = ( | ||
| 934 | + None | ||
| 935 | + if topk_weights is None | ||
| 936 | + else torch.empty((num_tokens, topk), dtype=torch.float32, device=x.device) | ||
| 937 | + ) | ||
| 938 | + | ||
| 939 | + self._runtime.moe_ep_combine( | ||
| 930 | x, | 940 | x, |
| 931 | handle.topk_idx, | 941 | handle.topk_idx, |
| 932 | handle.recv_src_metadata, | 942 | handle.recv_src_metadata, |
| @@ -937,6 +947,17 @@ class ElasticBuffer: | |||
| 937 | handle.num_experts, | 947 | handle.num_experts, |
| 938 | handle.num_max_tokens_per_rank, | 948 | handle.num_max_tokens_per_rank, |
| 939 | ) | 949 | ) |
| 950 | + | ||
| 951 | + combined_x, combined_topk_weights = self._runtime.moe_ep_combine_epilogue( | ||
| 952 | + handle.topk_idx, | ||
| 953 | + topk_weights, | ||
| 954 | + self._ep_world_size, | ||
| 955 | + self._rank_id, | ||
| 956 | + handle.num_experts, | ||
| 957 | + handle.num_max_tokens_per_rank, | ||
| 958 | + combined_x, | ||
| 959 | + combined_topk_weights, | ||
| 960 | + ) | ||
| 940 | return combined_x, combined_topk_weights | 961 | return combined_x, combined_topk_weights |
| 941 | 962 | ||
| 942 | def destroy(self) -> None: | 963 | def destroy(self) -> None: |