Merged
perf(mega_moe): 支持 A4W4 Wave 流水调度 #10319
perf(mega_moe): 支持 A4W4 Wave 流水调度 #10319
Merged
luobqAdd on Aug 20
Total: 12 files changed+535-501
@@ -939,11 +939,10 @@ static uint64_t CalcCombineSyncSlotCountPerExpert(const MegaMoeTilingData *tilin
939 939 
940static uint64_t CalcHostFlagElementCount(const MegaMoeTilingData *tilingData)940static uint64_t CalcHostFlagElementCount(const MegaMoeTilingData *tilingData)
941{941{
942- bool isA8W8 = tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_GENERAL ||942+ bool useMteA8W8Wave =
943- tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ;943+ tilingData->topoType == TOPO_TYPE_MTE && (tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_GENERAL ||
944- bool useMteA8W8Wave = tilingData->topoType == TOPO_TYPE_MTE && isA8W8;944+ tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ);
945- bool isA8W4 = tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W4;945+ bool useGroupGrainedActivationFlag = tilingData->topoType == TOPO_TYPE_MTE;
946- bool useGroupGrainedActivationFlag = tilingData->topoType == TOPO_TYPE_MTE && (isA8W8 || isA8W4);
947 bool useGroupSyncCounters =946 bool useGroupSyncCounters =
948 tilingData->topoType == TOPO_TYPE_URMA || (tilingData->combineQuantMode != COMBINE_NO_QUANT && !useMteA8W8Wave);947 tilingData->topoType == TOPO_TYPE_URMA || (tilingData->combineQuantMode != COMBINE_NO_QUANT && !useMteA8W8Wave);
949 uint64_t maxWavesPerExpert = ops::CeilDiv<uint64_t>(tilingData->maxOutputSize, L1_TILE_M_256);948 uint64_t maxWavesPerExpert = ops::CeilDiv<uint64_t>(tilingData->maxOutputSize, L1_TILE_M_256);
@@ -16,7 +16,9 @@
16#include "../mega_moe_tiling.h"16#include "../mega_moe_tiling.h"
17#include "mega_moe_constants.h"17#include "mega_moe_constants.h"
18#include "mega_moe_workspace.h"18#include "mega_moe_workspace.h"
19+#if defined(ENABLE_MEGA_MOE_LAYERED_KERNEL)
19#include "adv_api/hcomm/hcomm.h"20#include "adv_api/hcomm/hcomm.h"
21+#endif
20 22 
21namespace MegaMoeImpl {23namespace MegaMoeImpl {
22 24 
@@ -32,6 +34,14 @@ struct ExpertLoopState {
32 uint32_t expertIdx = 0U;34 uint32_t expertIdx = 0U;
33};35};
34 36 
37+// GMM1/GMM2 执行期间共同维护的流水状态;引用成员将更新直接回写到调用方持有的状态。
38+struct GmmRuntimeState {
39+ uint32_t &startBlockIdx;
40+ int32_t &vecSetSyncCom;
41+ int32_t &gmTileSequence;
42+ uint16_t &pingpongIdx;
43+};
44+ 
35// 标识 MoE 专家序列中的二维 token 位置。45// 标识 MoE 专家序列中的二维 token 位置。
36struct ExpertTokenPosition {46struct ExpertTokenPosition {
37 uint32_t expertIdx = 0U;47 uint32_t expertIdx = 0U;
@@ -62,11 +72,13 @@ struct GMMAddrInfo {
62 __gm__ int32_t *sharedExpertGmm2TileCounter;72 __gm__ int32_t *sharedExpertGmm2TileCounter;
63};73};
64 74 
75+#if defined(ENABLE_MEGA_MOE_LAYERED_KERNEL)
65struct CombineCommParams {76struct CombineCommParams {
66 uint32_t rankId;77 uint32_t rankId;
67 Hcomm<COMM_PROTOCOL_UBC_CTP> *hcomm;78 Hcomm<COMM_PROTOCOL_UBC_CTP> *hcomm;
68 __gm__ Mc2MoeContext *mc2Context;79 __gm__ Mc2MoeContext *mc2Context;
69};80};
81+#endif
70 82 
71// 保存 TensorList 入口地址,供按 expert 布局解析当前专家权重。83// 保存 TensorList 入口地址,供按 expert 布局解析当前专家权重。
72struct ExpertWeightTensorListAddrs {84struct ExpertWeightTensorListAddrs {
@@ -93,7 +105,9 @@ struct Params {
93 WorkspaceInfo workspaceInfo;105 WorkspaceInfo workspaceInfo;
94 PeermemInfo peermemInfo;106 PeermemInfo peermemInfo;
95 MegaMoeTilingData *tilingData;107 MegaMoeTilingData *tilingData;
108+#if defined(ENABLE_MEGA_MOE_LAYERED_KERNEL)
96 CombineCommParams combineCommParams;109 CombineCommParams combineCommParams;
110+#endif
97};111};
98 112 
99enum class AddrUpdateMode : int32_t {113enum class AddrUpdateMode : int32_t {
@@ -298,6 +298,24 @@ __aicore__ inline void UpdateExpertLoopState(ExpertLoopState &state, uint32_t ex
298 Get<M_VALUE>(state.problemShape) = static_cast<int64_t>(expertTokenCount);298 Get<M_VALUE>(state.problemShape) = static_cast<int64_t>(expertTokenCount);
299}299}
300 300 
301+// 按当前 Wave 的剩余容量推进专家 token 位置,返回本次推进覆盖的 token 数。
302+template <uint32_t TileM>
303+__aicore__ inline uint32_t AdvanceExpertTokenPositionInWave(uint32_t expertTokenCount, uint32_t waveMGroupTarget,
304+ uint32_t &waveMGroupCount, ExpertTokenPosition &position)
305+{
306+ uint32_t expertRemainingTokenCount = expertTokenCount - position.tokenIndexInExpert;
307+ uint32_t waveRemainingTokenCapacity = (waveMGroupTarget - waveMGroupCount) * TileM;
308+ uint32_t sliceTokenCount =
309+ expertRemainingTokenCount < waveRemainingTokenCapacity ? expertRemainingTokenCount : waveRemainingTokenCapacity;
310+ waveMGroupCount += Ops::Base::CeilDiv(sliceTokenCount, TileM);
311+ position.tokenIndexInExpert += sliceTokenCount;
312+ if (position.tokenIndexInExpert >= expertTokenCount) {
313+ ++position.expertIdx;
314+ position.tokenIndexInExpert = 0U;
315+ }
316+ return sliceTokenCount;
317+}
318+ 
301// 轮询 GM 中的 int32 ready flag,并在两次读取之间加入短暂退避。319// 轮询 GM 中的 int32 ready flag,并在两次读取之间加入短暂退避。
302__aicore__ inline void WaitUntilGmFlagIsNonZero(__gm__ int32_t *flagAddr)320__aicore__ inline void WaitUntilGmFlagIsNonZero(__gm__ int32_t *flagAddr)
303{321{
@@ -108,11 +108,10 @@ struct WorkspaceInfo {
108 workspaceSize += Ops::Base::CeilAlign(tilingData->maxOutputSize * ALIGN_32, ALIGN_512);108 workspaceSize += Ops::Base::CeilAlign(tilingData->maxOutputSize * ALIGN_32, ALIGN_512);
109 109 
110 // 以下三组 flag 仅由 MoE 专家使用;共享专家路径不使用这些 flag。110 // 以下三组 flag 仅由 MoE 专家使用;共享专家路径不使用这些 flag。
111- bool isGenericGmm = tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_GENERAL ||111+ bool useGroupGrainedActivationFlag = tilingData->topoType == TOPO_TYPE_MTE;
112- tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ;112+ bool useMteA8W8Wave =
113- bool isA8W4 = tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W4;113+ tilingData->topoType == TOPO_TYPE_MTE && (tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_GENERAL ||
114- bool useGroupGrainedActivationFlag = tilingData->topoType == TOPO_TYPE_MTE && (isGenericGmm || isA8W4);114+ tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ);
115- bool useMteA8W8Wave = tilingData->topoType == TOPO_TYPE_MTE && isGenericGmm;
116 bool useGroupSyncCounters = tilingData->topoType == TOPO_TYPE_URMA ||115 bool useGroupSyncCounters = tilingData->topoType == TOPO_TYPE_URMA ||
117 (tilingData->combineQuantMode != COMBINE_NO_QUANT && !useMteA8W8Wave);116 (tilingData->combineQuantMode != COMBINE_NO_QUANT && !useMteA8W8Wave);
118 117 
@@ -172,19 +171,23 @@ struct WorkspaceInfo {
172 // A8W4 / Combine 量化路径的条件 workspace 分配。171 // A8W4 / Combine 量化路径的条件 workspace 分配。
173 // 以下条件分配与 mega_moe.h 编译期守卫 (ENABLE_A8W4 / ENABLE_A4W4 / CombineQuantMode) 一致,172 // 以下条件分配与 mega_moe.h 编译期守卫 (ENABLE_A8W4 / ENABLE_A4W4 / CombineQuantMode) 一致,
174 // 由 TilingKey 保证同步。173 // 由 TilingKey 保证同步。
175- // 仅 A8W4 使用:cumsum GM 备份和 GMM1 中间结果。174+ // A8W4 以及 MTE A4W4 Wave 使用 cumsum GM 备份;A8W4/prefetch 使用 GMM1 中间结果。
176 cumsumInfoPtr = nullptr;175 cumsumInfoPtr = nullptr;
177 gmm1MmadResPtr = nullptr;176 gmm1MmadResPtr = nullptr;
178 gmm2MmadResPtr = nullptr;177 gmm2MmadResPtr = nullptr;
179- if (tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W4 || tilingData->topkWeightsPrefetch == 1) {178+ bool usePersistentDispatchCumsum =
179+ tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W4 ||
180+ (tilingData->topoType == TOPO_TYPE_MTE && (tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A4W4 ||
181+ tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A4W4_NZ));
182+ if (usePersistentDispatchCumsum) {
180 // cumsumInfo:逐核备份 cumsum 状态,每核 moeExpertPerRank × epWorldSize 个 int32。183 // cumsumInfo:逐核备份 cumsum 状态,每核 moeExpertPerRank × epWorldSize 个 int32。
181- if (tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W4) {184+ cumsumInfoPtr = base + workspaceSize;
182- cumsumInfoPtr = base + workspaceSize;185+ workspaceSize += Ops::Base::CeilAlign(static_cast<int64_t>(SIZE_INT_32 * tilingData->moeExpertPerRank *
183- workspaceSize += Ops::Base::CeilAlign(static_cast<int64_t>(SIZE_INT_32 * tilingData->moeExpertPerRank *186+ tilingData->epWorldSize),
184- tilingData->epWorldSize),187+ ALIGN_32) *
185- ALIGN_32) *188+ tilingData->aicNum;
186- tilingData->aicNum;189+ }
187- }190+ if (tilingData->groupedMatmulMode == GROUPED_MATMUL_MODE_A8W4 || tilingData->topkWeightsPrefetch == 1) {
188 // gmm1MmadRes:GMM1 matmul 输出,布局为 maxOutputSize × hiddenDim 个 BF16。191 // gmm1MmadRes:GMM1 matmul 输出,布局为 maxOutputSize × hiddenDim 个 BF16。
189 gmm1MmadResPtr = base + workspaceSize;192 gmm1MmadResPtr = base + workspaceSize;
190 workspaceSize += SIZE_BF_16 * tilingData->maxOutputSize * tilingData->hiddenDim;193 workspaceSize += SIZE_BF_16 * tilingData->maxOutputSize * tilingData->hiddenDim;
@@ -34,9 +34,9 @@
34#endif34#endif
35 35 
36#ifdef ENABLE_TENSOR_API36#ifdef ENABLE_TENSOR_API
37-#include "mega_moe_arch35.h"37+#include "mega_moe_wave_a8w8.h"
38+#include "mega_moe_wave_a4w4.h"
38#include "mega_moe_wave_a8w4.h"39#include "mega_moe_wave_a8w4.h"
39-#include "mega_moe_wave.h"
40#endif40#endif
41 41 
42#if defined(ENABLE_MEGA_MOE_LAYERED_KERNEL)42#if defined(ENABLE_MEGA_MOE_LAYERED_KERNEL)
@@ -59,6 +59,35 @@ using namespace AscendC;
59// default. Builds that provide an interleaved weight can override this to 1.59// default. Builds that provide an interleaved weight can override this to 1.
60static constexpr bool WEIGHT1_INTERLEAVED = MEGA_MOE_WEIGHT1_INTERLEAVED != 0;60static constexpr bool WEIGHT1_INTERLEAVED = MEGA_MOE_WEIGHT1_INTERLEAVED != 0;
61 61 
62+#ifdef ENABLE_TENSOR_API
63+namespace MegaMoeImpl {
64+ 
65+template <typename XType, typename OutputType, typename TopkWeightsType, typename Weight1Type, int32_t QuantMode,
66+ int32_t CombineQuantMode, bool TopkWeightsPrefetch, bool IsGmm1Interleaved>
67+class MegaMoeMteWave : public MegaMoeA8W8Wave<XType, OutputType, TopkWeightsType, Weight1Type, QuantMode,
68+ CombineQuantMode, TopkWeightsPrefetch, IsGmm1Interleaved> {
69+ static_assert((QuantMode == DISPATCH_QUANT_OUT_DTYPE_E5M2 && Std::IsSame<Weight1Type, fp8_e5m2_t>::value) ||
70+ (QuantMode == DISPATCH_QUANT_OUT_DTYPE_E4M3FN && Std::IsSame<Weight1Type, fp8_e4m3fn_t>::value),
71+ "A8W8 requires matching FP8 activation and weight types");
72+};
73+ 
74+template <typename XType, typename OutputType, typename TopkWeightsType, int32_t CombineQuantMode,
75+ bool TopkWeightsPrefetch, bool IsGmm1Interleaved>
76+class MegaMoeMteWave<XType, OutputType, TopkWeightsType, fp4x2_e2m1_t, DISPATCH_QUANT_OUT_DTYPE_E4M3FN,
77+ CombineQuantMode, TopkWeightsPrefetch, IsGmm1Interleaved>
78+ : public MegaMoeA8W4Wave<XType, OutputType, TopkWeightsType, fp4x2_e2m1_t, DISPATCH_QUANT_OUT_DTYPE_E4M3FN,
79+ CombineQuantMode, TopkWeightsPrefetch> {};
80+ 
81+template <typename XType, typename OutputType, typename TopkWeightsType, int32_t CombineQuantMode,
82+ bool TopkWeightsPrefetch, bool IsGmm1Interleaved>
83+class MegaMoeMteWave<XType, OutputType, TopkWeightsType, fp4x2_e2m1_t, DISPATCH_QUANT_OUT_DTYPE_E2M1, CombineQuantMode,
84+ TopkWeightsPrefetch, IsGmm1Interleaved>
85+ : public MegaMoeA4W4Wave<XType, OutputType, TopkWeightsType, fp4x2_e2m1_t, DISPATCH_QUANT_OUT_DTYPE_E2M1,
86+ CombineQuantMode, TopkWeightsPrefetch> {};
87+ 
88+} // namespace MegaMoeImpl
89+#endif
90+ 
62template <uint8_t DispatchQuantMode, uint8_t DispatchQuantOutType, uint8_t CombineQuantOutType, uint8_t CommModeType,91template <uint8_t DispatchQuantMode, uint8_t DispatchQuantOutType, uint8_t CombineQuantOutType, uint8_t CommModeType,
63 bool TopkWeightsPrefetch>92 bool TopkWeightsPrefetch>
64__global__ __aicore__ void mega_moe(GM_ADDR context, GM_ADDR x, GM_ADDR topkIds, GM_ADDR topkWeights, GM_ADDR weight1,93__global__ __aicore__ void mega_moe(GM_ADDR context, GM_ADDR x, GM_ADDR topkIds, GM_ADDR topkWeights, GM_ADDR weight1,
@@ -79,34 +108,16 @@ __global__ __aicore__ void mega_moe(GM_ADDR context, GM_ADDR x, GM_ADDR topkIds,
79 defined(ORIG_DTYPE_WEIGHT2) && (ORIG_DTYPE_WEIGHT2 == ORIG_DTYPE_WEIGHT1)108 defined(ORIG_DTYPE_WEIGHT2) && (ORIG_DTYPE_WEIGHT2 == ORIG_DTYPE_WEIGHT1)
80 if constexpr (CommModeType == TILINGKEY_TPL_MTE) {109 if constexpr (CommModeType == TILINGKEY_TPL_MTE) {
81 if constexpr (DispatchQuantMode == DISPATCH_QUANT_MODE_MXFP) {110 if constexpr (DispatchQuantMode == DISPATCH_QUANT_MODE_MXFP) {
82- // Dispatch 输出类型和权重类型可区分 A8W8/A8W4 Wave,无需增加 tiling key 维度。111+ constexpr bool isSupportedMteDtypePair = (DispatchQuantOutType == DISPATCH_QUANT_OUT_DTYPE_E5M2 &&
83- constexpr bool isA8W8DtypePair =112+ Std::IsSame<DTYPE_WEIGHT1, fp8_e5m2_t>::value) ||
84- (DispatchQuantOutType == DISPATCH_QUANT_OUT_DTYPE_E5M2 &&113+ (DispatchQuantOutType == DISPATCH_QUANT_OUT_DTYPE_E4M3FN &&
85- Std::IsSame<DTYPE_WEIGHT1, fp8_e5m2_t>::value) ||114+ (Std::IsSame<DTYPE_WEIGHT1, fp8_e4m3fn_t>::value ||
86- (DispatchQuantOutType == DISPATCH_QUANT_OUT_DTYPE_E4M3FN &&115+ Std::IsSame<DTYPE_WEIGHT1, fp4x2_e2m1_t>::value)) ||
87- Std::IsSame<DTYPE_WEIGHT1, fp8_e4m3fn_t>::value);116+ (DispatchQuantOutType == DISPATCH_QUANT_OUT_DTYPE_E2M1 &&
88- constexpr bool isA8W4DtypePair =117+ Std::IsSame<DTYPE_WEIGHT1, fp4x2_e2m1_t>::value);
89- DispatchQuantOutType == DISPATCH_QUANT_OUT_DTYPE_E4M3FN &&118+ if constexpr (isSupportedMteDtypePair) {
90- Std::IsSame<DTYPE_WEIGHT1, fp4x2_e2m1_t>::value;119+ MegaMoeImpl::MegaMoeMteWave<DTYPE_X, DTYPE_Y, DTYPE_TOPK_WEIGHTS, DTYPE_WEIGHT1, DispatchQuantOutType,
91- if constexpr (isA8W8DtypePair) {120+ CombineQuantOutType, TopkWeightsPrefetch, WEIGHT1_INTERLEAVED>
92- MegaMoeImpl::MegaMoeWave<DTYPE_X, DTYPE_Y, DTYPE_TOPK_WEIGHTS, DTYPE_WEIGHT1, DispatchQuantOutType,
93- CombineQuantOutType, TopkWeightsPrefetch, WEIGHT1_INTERLEAVED>
94- op;
95- op.Init(context, x, topkIds, topkWeights, weight1, weight2, xActiveMask, weightScales1, weightScales2,
96- scales, sharedWeight1, sharedWeight2, sharedWeightScales1, sharedWeightScales2, yOut,
97- expertTokenNumsOut, workspaceGM, &tilingData);
98- op.Process();
99- } else if constexpr (isA8W4DtypePair) {
100- MegaMoeImpl::MegaMoeA8W4Wave<DTYPE_X, DTYPE_Y, DTYPE_TOPK_WEIGHTS, DTYPE_WEIGHT1,
101- DispatchQuantOutType, CombineQuantOutType, TopkWeightsPrefetch>
102- op;
103- op.Init(context, x, topkIds, topkWeights, weight1, weight2, xActiveMask, weightScales1, weightScales2,
104- scales, sharedWeight1, sharedWeight2, sharedWeightScales1, sharedWeightScales2, yOut,
105- expertTokenNumsOut, workspaceGM, &tilingData);
106- op.Process();
107- } else {
108- MegaMoeImpl::MegaMoe<DTYPE_X, DTYPE_Y, DTYPE_TOPK_WEIGHTS, DTYPE_WEIGHT1, DispatchQuantOutType,
109- CombineQuantOutType, TopkWeightsPrefetch>
110 op;121 op;
111 op.Init(context, x, topkIds, topkWeights, weight1, weight2, xActiveMask, weightScales1, weightScales2,122 op.Init(context, x, topkIds, topkWeights, weight1, weight2, xActiveMask, weightScales1, weightScales2,
112 scales, sharedWeight1, sharedWeight2, sharedWeightScales1, sharedWeightScales2, yOut,123 scales, sharedWeight1, sharedWeight2, sharedWeightScales1, sharedWeightScales2, yOut,
@@ -78,7 +78,6 @@ public:
78 GM_ADDR scales, GM_ADDR sharedWeight1, GM_ADDR sharedWeight2,78 GM_ADDR scales, GM_ADDR sharedWeight1, GM_ADDR sharedWeight2,
79 GM_ADDR sharedWeightScales1, GM_ADDR sharedWeightScales2, GM_ADDR yOut,79 GM_ADDR sharedWeightScales1, GM_ADDR sharedWeightScales2, GM_ADDR yOut,
80 GM_ADDR expertTokenNumsOut, GM_ADDR workspaceGM, MegaMoeTilingData *tilingData);80 GM_ADDR expertTokenNumsOut, GM_ADDR workspaceGM, MegaMoeTilingData *tilingData);
81- __aicore__ inline void Process();
luobq
luobqluobqAug 21
Outdated

process留在基类

likedislike
82 81 
83private:82private:
84 using SendMaskBufferConfig = MegaMoeSendMaskBufferConfig;83 using SendMaskBufferConfig = MegaMoeSendMaskBufferConfig;
@@ -95,13 +94,14 @@ protected:
95 __aicore__ inline void DispatchBuffInit();94 __aicore__ inline void DispatchBuffInit();
96 __aicore__ inline void InitCombineBuffers();95 __aicore__ inline void InitCombineBuffers();
97 __aicore__ inline UnpermuteBufferConfig InitTokenUnpermuteBuffers();96 __aicore__ inline UnpermuteBufferConfig InitTokenUnpermuteBuffers();
97+ __aicore__ inline void ProcessInputPreparationStage();
98+ __aicore__ inline uint32_t DispatchMoeExpert(uint32_t expertIdx);
99+ template <bool WaitForTokenCountReady>
100+ __aicore__ inline void PrepareGmmExpertState(ExpertLoopState &state, uint32_t expertIdx);
98 __aicore__ inline void ProcessSharedExpertGmm1(int32_t &gmTileSequence);101 __aicore__ inline void ProcessSharedExpertGmm1(int32_t &gmTileSequence);
99- __aicore__ inline bool PrepareGmm1ExpertState(ExpertLoopState &state, uint32_t expertIdx, uint32_t sendCnt);
100- __aicore__ inline void RunGmm1ActivationForExpert(ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo,
101- Gmm1ActivationState &runtimeState, uint32_t expertIdx,
102- uint32_t sendCnt);
103- __aicore__ inline void ProcessMoeExpertStages(int32_t &gmTileSequence);
104 __aicore__ inline void ProcessSharedExpertGmm2(int32_t &gmTileSequence);102 __aicore__ inline void ProcessSharedExpertGmm2(int32_t &gmTileSequence);
103+ template <typename Derived>
104+ __aicore__ inline void ProcessWave(Derived &derived);
105 105 
106 __gm__ Mc2MoeContext *mc2Context_{nullptr};106 __gm__ Mc2MoeContext *mc2Context_{nullptr};
107 Params params_{};107 Params params_{};
@@ -121,8 +121,8 @@ protected:
121 QuantCombineConfig quantCombineConfig_;121 QuantCombineConfig quantCombineConfig_;
122 QuantCombineBufferConfig quantCombineBufferConfig_;122 QuantCombineBufferConfig quantCombineBufferConfig_;
123 TokenUnpermuteConfig tokenUnpermuteConfig_;123 TokenUnpermuteConfig tokenUnpermuteConfig_;
124- // A8W4 路径下 RunGmm1A8W4 会覆盖 V1 UB,导致 UB 上跨 expert 的状态124+ // A8W4 GMM1 和 A4W4 Wave GMM2 会覆盖 V1 UB,导致 UB 上跨 expert 的状态无法保持。
125- // 无法保持。tokenDispatchScratch_ 中的 cumsumInfoGlobalTensor 作为 GM 持久备份:125+ // tokenDispatchScratch_ 中的 cumsumInfoGlobalTensor 作为 GM 持久备份:
126 // ComputeExpertTokenCountAndNotify 中 Load → 计算 → Store;Dispatch/Export 从 GM 恢复。126 // ComputeExpertTokenCountAndNotify 中 Load → 计算 → Store;Dispatch/Export 从 GM 恢复。
127 127 
128 uint32_t k_ = 0;128 uint32_t k_ = 0;
@@ -136,6 +136,7 @@ protected:
136 // 主线 shared-expert 特性成员136 // 主线 shared-expert 特性成员
137 uint32_t sharedExpertNum_ = 0;137 uint32_t sharedExpertNum_ = 0;
138 uint32_t moeExpertPerRank_ = 0;138 uint32_t moeExpertPerRank_ = 0;
139+ uint32_t mGroupsPerWave_ = 1U;
139 140 
140 static constexpr uint32_t A_ELEMS_PER_BYTE = PackedElementTraits<QuantOutType>::ELEMENTS_PER_BYTE;141 static constexpr uint32_t A_ELEMS_PER_BYTE = PackedElementTraits<QuantOutType>::ELEMENTS_PER_BYTE;
141 static constexpr uint32_t B_ELEMS_PER_BYTE = PackedElementTraits<Weight1Type>::ELEMENTS_PER_BYTE;142 static constexpr uint32_t B_ELEMS_PER_BYTE = PackedElementTraits<Weight1Type>::ELEMENTS_PER_BYTE;
@@ -230,6 +231,7 @@ __aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::Init(
230 worldSize_ = tilingData->epWorldSize;231 worldSize_ = tilingData->epWorldSize;
231 moeExpertPerRank_ = tilingData->moeExpertPerRank;232 moeExpertPerRank_ = tilingData->moeExpertPerRank;
232 sharedExpertNum_ = tilingData->sharedExpertNum;233 sharedExpertNum_ = tilingData->sharedExpertNum;
234+ mGroupsPerWave_ = tilingData->mGroupsPerWave;
233 gmm2PingPongIdx_ = 0;235 gmm2PingPongIdx_ = 0;
234 mc2Context_ = reinterpret_cast<__gm__ Mc2MoeContext *>(context);236 mc2Context_ = reinterpret_cast<__gm__ Mc2MoeContext *>(context);
235 rankId_ = mc2Context_->epRankId;237 rankId_ = mc2Context_->epRankId;
@@ -273,11 +275,8 @@ __aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::Init(
273 const int64_t maxOutput = static_cast<int64_t>(tilingData->maxOutputSize);275 const int64_t maxOutput = static_cast<int64_t>(tilingData->maxOutputSize);
274 const int64_t tileM = static_cast<int64_t>(GMM1_TILE_M);276 const int64_t tileM = static_cast<int64_t>(GMM1_TILE_M);
275 int32_t dispatchFlagSlotsPerExpert = static_cast<int32_t>(Ops::Base::CeilDiv(maxOutput, tileM)) * INT_CACHELINE;277 int32_t dispatchFlagSlotsPerExpert = static_cast<int32_t>(Ops::Base::CeilDiv(maxOutput, tileM)) * INT_CACHELINE;
276- int32_t activationFlagSlotsPerExpert = INT_CACHELINE;278+ int32_t activationFlagSlotsPerExpert =
277- if constexpr (ENABLE_A8W4) {279+ static_cast<int32_t>(Ops::Base::CeilDiv(maxOutput, static_cast<int64_t>(L1_TILE_M_256))) * INT_CACHELINE;
278- activationFlagSlotsPerExpert =
279- static_cast<int32_t>(Ops::Base::CeilDiv(maxOutput, static_cast<int64_t>(L1_TILE_M_256))) * INT_CACHELINE;
280- }
281 InitInputPrepareConfigs();280 InitInputPrepareConfigs();
282 tokenDispatchConfig_ = CreateTokenDispatchConfig(params_, quantProcessConfig_, sendMaskConfig_.maskAlignSize);281 tokenDispatchConfig_ = CreateTokenDispatchConfig(params_, quantProcessConfig_, sendMaskConfig_.maskAlignSize);
283 InitSyncWorkspaceConfigs(dispatchFlagSlotsPerExpert, activationFlagSlotsPerExpert);282 InitSyncWorkspaceConfigs(dispatchFlagSlotsPerExpert, activationFlagSlotsPerExpert);
@@ -309,7 +308,7 @@ __aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::DispatchBuffInit()
309 uint32_t cumsumInfoTensorSize = Ops::Base::CeilAlign(308 uint32_t cumsumInfoTensorSize = Ops::Base::CeilAlign(
310 static_cast<int64_t>(commonConfig_.worldSize * commonConfig_.moeExpertPerRank * sizeof(int32_t)),309 static_cast<int64_t>(commonConfig_.worldSize * commonConfig_.moeExpertPerRank * sizeof(int32_t)),
311 static_cast<int64_t>(ALIGN_32));310 static_cast<int64_t>(ALIGN_32));
312- if constexpr (ENABLE_A8W4) {311+ if constexpr (ENABLE_A8W4 || ENABLE_A4W4) {
313 scratch.cumsumInfoGlobalTensor.SetGlobalBuffer(312 scratch.cumsumInfoGlobalTensor.SetGlobalBuffer(
314 reinterpret_cast<__gm__ int32_t *>(params_.workspaceInfo.cumsumInfoPtr +313 reinterpret_cast<__gm__ int32_t *>(params_.workspaceInfo.cumsumInfoPtr +
315 static_cast<uint64_t>(cumsumInfoTensorSize) * countWorkspace_.blockIdx));314 static_cast<uint64_t>(cumsumInfoTensorSize) * countWorkspace_.blockIdx));
@@ -458,7 +457,7 @@ template <TemplateMegaMoeTypeClass>
458__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessSharedExpertGmm1(int32_t &gmTileSequence)457__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessSharedExpertGmm1(int32_t &gmTileSequence)
459{458{
460 if (gmmExecutionConfig_.blockJob.totalJobs == 0U ||459 if (gmmExecutionConfig_.blockJob.totalJobs == 0U ||
461- gmmExecutionConfig_.blockJob.jobIndex >= gmmExecutionConfig_.blockJob.totalJobs || sharedExpertNum_ == 0U) {460+ gmmExecutionConfig_.blockJob.jobIndex >= gmmExecutionConfig_.blockJob.totalJobs) {
462 return;461 return;
463 }462 }
464 typename SharedBlockEpilogue::Params epilogueParams{463 typename SharedBlockEpilogue::Params epilogueParams{
@@ -482,7 +481,7 @@ __aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessSharedExpertGmm1
482 uint32_t startBlockIdx = 0U;481 uint32_t startBlockIdx = 0U;
483 int32_t vecSetSyncCom = 0;482 int32_t vecSetSyncCom = 0;
484 uint16_t pingpongIdx = 0U;483 uint16_t pingpongIdx = 0U;
485- SharedExpertGmm1ActivationState runtimeState{startBlockIdx, vecSetSyncCom, gmTileSequence, pingpongIdx};484+ GmmRuntimeState runtimeState{startBlockIdx, vecSetSyncCom, gmTileSequence, pingpongIdx};
486 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {485 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {
487 UpdateSharedExpertGmm1GlobalBuffer<ActivationType, Weight1Type, ActivationQuantOutType, QuantScaleOutType,486 UpdateSharedExpertGmm1GlobalBuffer<ActivationType, Weight1Type, ActivationQuantOutType, QuantScaleOutType,
488 ENABLE_A8W4>(commonConfig_, gmmExecutionConfig_, params_.workspaceInfo,487 ENABLE_A8W4>(commonConfig_, gmmExecutionConfig_, params_.workspaceInfo,
@@ -499,131 +498,64 @@ __aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessSharedExpertGmm1
499template <TemplateMegaMoeTypeClass>498template <TemplateMegaMoeTypeClass>
500__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessSharedExpertGmm2(int32_t &gmTileSequence)499__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessSharedExpertGmm2(int32_t &gmTileSequence)
501{500{
501+ ProblemShape problemShape;
502+ Get<M_VALUE>(problemShape) = commonConfig_.tokenNum;
503+ Get<N_VALUE>(problemShape) = commonConfig_.gmm1OutputDim;
504+ Get<K_VALUE>(problemShape) = commonConfig_.tokenHiddenDim;
502 uint32_t startBlockIdx = 0U;505 uint32_t startBlockIdx = 0U;
503 int32_t vecSetSyncCom = 0;506 int32_t vecSetSyncCom = 0;
504- Gmm2RuntimeState runtimeState{startBlockIdx, vecSetSyncCom, gmTileSequence, gmm2PingPongIdx_};507+ GmmRuntimeState runtimeState{startBlockIdx, vecSetSyncCom, gmTileSequence, gmm2PingPongIdx_};
505 GMMAddrInfo gmmAddrInfo{};508 GMMAddrInfo gmmAddrInfo{};
506 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {509 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {
507- RunSharedExpertGmm2Stage<QuantOutType, ActivationQuantOutType, Weight1Type, QuantScaleOutType, ENABLE_A8W4,510+ UpdateSharedExpertGmm2GlobalBuffer<ActivationQuantOutType, Weight1Type, QuantScaleOutType, GMM1_TILE_M,
508- ENABLE_A4W4, GMM1_TILE_M, false, false, false>(511+ ENABLE_A8W4 || ENABLE_A4W4>(
509- commonConfig_, gmmExecutionConfig_, params_, sharedWeightTensorListAddrs_, gmmAddrInfo, runtimeState,512+ commonConfig_, gmmExecutionConfig_, params_.workspaceInfo, sharedWeightTensorListAddrs_, gmmAddrInfo,
510 sharedExpertIdx);513 sharedExpertIdx);
514+ RunGmm2ByMode<COMBINE_NO_QUANT, QuantOutType, ActivationQuantOutType, Weight1Type, QuantScaleOutType,
515+ ENABLE_A8W4, ENABLE_A4W4, GMM1_TILE_M, false, true, false, false>(
516+ gmmExecutionConfig_, params_, gmmAddrInfo, problemShape, runtimeState);
511 }517 }
512}518}
513 519 
514-// 进入一个 MoE 专家时获取 token 数,并准备其完整 GMM1 状态。520+// 计算当前专家的 token 数,并在非空时执行完整 Dispatch。
515template <TemplateMegaMoeTypeClass>521template <TemplateMegaMoeTypeClass>
516-__aicore__ inline bool MegaMoe<TemplateMegaMoeTypeFunc>::PrepareGmm1ExpertState(ExpertLoopState &state,522+__aicore__ inline uint32_t MegaMoe<TemplateMegaMoeTypeFunc>::DispatchMoeExpert(uint32_t expertIdx)
517- uint32_t expertIdx, uint32_t sendCnt)
518{523{
519- if (GetSubBlockIdx() == 0) {524+ uint32_t expertTokenCount = 0U;
520- WaitForMoeExpertTokenCountReady(params_.workspaceInfo.flagSendCntCalToUpdParamsPtr, countWorkspace_, expertIdx);525+ ComputeExpertTokenCountAndNotify<ActivationType, ENABLE_A8W4 || ENABLE_A4W4, TopkWeightsPrefetch>(
521- sendCnt =526+ tokenDispatchConfig_, commonConfig_, countWorkspace_, params_, tokenDispatchScratch_, expertIdx,
522- GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_, expertIdx);527+ expertTokenCount);
528+ if (expertTokenCount != 0U) {
529+ int32_t expertGlobalRowBegin =
530+ GetExpertGlobalRowBegin(tokenDispatchScratch_, expertIdx, commonConfig_.worldSize);
531+ DispatchExpertTokensByRankShard<ActivationType, QuantScaleOutType, GMM1_TILE_M, TopkWeightsPrefetch>(
532+ tokenDispatchConfig_, commonConfig_, gmmExecutionConfig_.blockJob, countWorkspace_, syncWorkspaceLayout_,
533+ params_, winRankAddr_, tokenDispatchScratch_, expertIdx, expertGlobalRowBegin);
523 }534 }
524- UpdateExpertLoopState(state, expertIdx, sendCnt);535+ return expertTokenCount;
525- return sendCnt != 0U;
526}536}
527 537 
538+// 从 workspace 读取 token 数并准备 GMM 专家状态;GMM1 可按需先等待 token count ready。
528template <TemplateMegaMoeTypeClass>539template <TemplateMegaMoeTypeClass>
529-__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::RunGmm1ActivationForExpert(ExpertLoopState &state,540+template <bool WaitForTokenCountReady>
530- GMMAddrInfo &gmmAddrInfo,541+__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::PrepareGmmExpertState(ExpertLoopState &state,
531- Gmm1ActivationState &runtimeState,542+ uint32_t expertIdx)
532- uint32_t expertIdx,
533- uint32_t sendCnt)
534{543{
535- if (!PrepareGmm1ExpertState(state, expertIdx, sendCnt)) {544+ if constexpr (WaitForTokenCountReady) {
536- return;545+ if (GetSubBlockIdx() == 0U) {
537- }546+ WaitForMoeExpertTokenCountReady(params_.workspaceInfo.flagSendCntCalToUpdParamsPtr, countWorkspace_,
538- UpdateMoeExpertGmm1GlobalBuffer<ActivationType, Weight1Type, ActivationQuantOutType, QuantScaleOutType,547+ expertIdx);
539- PackedElementTraits<QuantOutType>::ELEMENTS_PER_BYTE, ENABLE_A8W4,
540- TopkWeightsPrefetch>(gmmExecutionConfig_, syncWorkspaceLayout_,
541- params_.workspaceInfo, moeWeightTensorListAddrs_, epilogueOp_,
542- gmmAddrInfo, state);
543- RunGmm1ActivationByMode<QuantOutType, Weight1Type, ActivationQuantOutType, QuantScaleOutType, ENABLE_A8W4,
544- GMM1_TILE_M, EPILOGUE_TILE_M, TopkWeightsPrefetch, false, false>(
545- gmmExecutionConfig_, params_, epilogueOp_, gmmAddrInfo, state.problemShape,
546- static_cast<uint32_t>(state.globalTokenStartIndex), runtimeState, state.expertIdx);
547-}
548- 
549-template <TemplateMegaMoeTypeClass>
550-__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessMoeExpertStages(int32_t &gmTileSequence)
551-{
552- DispatchBuffInit();
553- ExpertLoopState gmm1State = CreateExpertLoopState(commonConfig_);
554- GMMAddrInfo gmm1AddrInfo{};
555- uint32_t gmm1StartBlockIdx = 0U;
556- int32_t gmm1VecSetSyncCom = 0;
557- uint16_t gmm1PingPongIdx = 0U;
558- Gmm1ActivationState gmm1RuntimeState{gmm1StartBlockIdx, gmm1VecSetSyncCom, gmTileSequence, gmm1PingPongIdx};
559- uint32_t currentSendCnt = 0U;
560- uint32_t nextSendCnt = 0U;
561- 
562- RunMoeExpertDispatchStage<ActivationType, QuantScaleOutType, ENABLE_A8W4, GMM1_TILE_M, TopkWeightsPrefetch>(
563- tokenDispatchConfig_, commonConfig_, gmmExecutionConfig_.blockJob, countWorkspace_, syncWorkspaceLayout_,
564- params_, winRankAddr_, tokenDispatchScratch_, 0U, nextSendCnt);
565- for (uint32_t expertIdx = 0U; expertIdx < commonConfig_.moeExpertPerRank; ++expertIdx) {
566- currentSendCnt = nextSendCnt;
567- if (expertIdx + 1U < commonConfig_.moeExpertPerRank) {
568- RunMoeExpertDispatchStage<ActivationType, QuantScaleOutType, ENABLE_A8W4, GMM1_TILE_M, TopkWeightsPrefetch>(
569- tokenDispatchConfig_, commonConfig_, gmmExecutionConfig_.blockJob, countWorkspace_,
570- syncWorkspaceLayout_, params_, winRankAddr_, tokenDispatchScratch_, expertIdx + 1U, nextSendCnt);
571- }
572- RunGmm1ActivationForExpert(gmm1State, gmm1AddrInfo, gmm1RuntimeState, expertIdx, currentSendCnt);
573- }
574- FinishMoeGmm1ActivationStage<ENABLE_A8W4, TopkWeightsPrefetch>(commonConfig_, syncWorkspaceLayout_,
575- params_.workspaceInfo, gmm1RuntimeState);
576- 
577- if constexpr (g_coreType == AIV) {
578- if (GetSubBlockIdx() == 1U) {
579- ExportExpertTokenCounts<ActivationType, ENABLE_A8W4>(commonConfig_, tokenDispatchScratch_, params_);
580 }548 }
581 }549 }
582- if constexpr (TopkWeightsPrefetch) {550+ uint32_t expertTokenCount =
583- SyncAll<false>();551+ GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_, expertIdx);
584- }552+ UpdateExpertLoopState(state, expertIdx, expertTokenCount);
585- 
586- uint32_t gmm2StartBlockIdx = gmm1RuntimeState.startBlockIdx;
587- int32_t gmm2VecSetSyncCom = 0;
588- Gmm2RuntimeState gmm2RuntimeState{gmm2StartBlockIdx, gmm2VecSetSyncCom, gmTileSequence, gmm2PingPongIdx_};
589- ExpertLoopState gmm2State = CreateExpertLoopState(commonConfig_);
590- GMMAddrInfo gmm2AddrInfo{};
591- constexpr bool configureGmm2Output = ENABLE_A8W4 || ENABLE_A4W4 || CombineQuantMode != COMBINE_NO_QUANT;
592- constexpr bool configureCombineCounter = CombineQuantMode != COMBINE_NO_QUANT;
593- for (uint32_t expertIdx = 0U; expertIdx < commonConfig_.moeExpertPerRank; ++expertIdx) {
594- uint32_t expertTokenCount =
595- GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_, expertIdx);
596- UpdateExpertLoopState(gmm2State, expertIdx, expertTokenCount);
597- if (expertTokenCount == 0U) {
598- continue;
599- }
600- UpdateMoeExpertGmm2GlobalBuffer<Weight1Type, ActivationQuantOutType, QuantScaleOutType, configureGmm2Output,
601- configureCombineCounter, ENABLE_A8W4 || ENABLE_A4W4>(
602- gmmExecutionConfig_, syncWorkspaceLayout_, params_.workspaceInfo, moeWeightTensorListAddrs_, gmm2AddrInfo,
603- gmm2State);
604- RunMoeExpertGmm2Stage<CombineQuantMode, QuantOutType, ActivationQuantOutType, Weight1Type, QuantScaleOutType,
605- ENABLE_A8W4, ENABLE_A4W4, GMM1_TILE_M, TopkWeightsPrefetch, false, false>(
606- gmmExecutionConfig_, params_, gmm2AddrInfo, gmm2State.problemShape, gmm2RuntimeState);
607- ScheduleQuantizedMoeExpertCombine<CombineQuantMode>(
608- quantCombineConfig_, commonConfig_, quantCombineBufferConfig_, params_, gmm2AddrInfo,
609- static_cast<uint32_t>(Get<M_VALUE>(gmm2State.problemShape)),
610- static_cast<uint32_t>(gmm2State.globalTokenStartIndex), gmm2State.expertIdx, 0U,
611- static_cast<uint32_t>(Get<M_VALUE>(gmm2State.problemShape)));
612- }
613- if constexpr (CombineQuantMode == COMBINE_NO_QUANT) {
614- EndGMM2Sync(gmm2RuntimeState.vecSetSyncCom, gmm2RuntimeState.pingpongIdx);
615- }
616}553}
617 554 
618template <TemplateMegaMoeTypeClass>555template <TemplateMegaMoeTypeClass>
619-__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::Process()556+__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessInputPreparationStage()
620{557{
621- // 1.本卡数据处理
622- int64_t oriOverflowMode = GetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>();
623- SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(0);
624 SendAndQuantBuffInit();558 SendAndQuantBuffInit();
625- 
626- // Phase 1: 量化 + 共享专家输入拆分 + mask/reset (AIV)
627 QuantizeLocalTokens<QuantMode, QuantOutType, ActivationType, TopkWeightsType, TopkWeightsPrefetch>(559 QuantizeLocalTokens<QuantMode, QuantOutType, ActivationType, TopkWeightsType, TopkWeightsPrefetch>(
628 aivJob_, commonConfig_, params_, quantProcessConfig_, quantScratch_);560 aivJob_, commonConfig_, params_, quantProcessConfig_, quantScratch_);
629 GatherAndSendExpertMasks(aivJob_, commonConfig_, params_, winRankAddr_, sendMaskConfig_, sendMaskScratch_);561 GatherAndSendExpertMasks(aivJob_, commonConfig_, params_, winRankAddr_, sendMaskConfig_, sendMaskScratch_);
@@ -632,37 +564,58 @@ __aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::Process()
632 PrepareSharedExpertInput<ActivationType, QuantScaleOutType, A_ELEMS_PER_BYTE>(564 PrepareSharedExpertInput<ActivationType, QuantScaleOutType, A_ELEMS_PER_BYTE>(
633 aivJob_, commonConfig_, params_, quantProcessConfig_, sharedExpertPrepareScratch_);565 aivJob_, commonConfig_, params_, quantProcessConfig_, sharedExpertPrepareScratch_);
634 }566 }
567+}
568+ 
569+/*
570+ * 所有 MTE Wave 路径共用相同的阶段边界,派生类只负责 MoE 专家 Wave 的具体编排。
571+ */
572+template <TemplateMegaMoeTypeClass>
573+template <typename Derived>
574+__aicore__ inline void MegaMoe<TemplateMegaMoeTypeFunc>::ProcessWave(Derived &derived)
575+{
576+ // 保存入口时的溢出模式,计算期间关闭溢出检查。
577+ int64_t oriOverflowMode = GetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>();
578+ SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(0);
579+ 
580+ // 阶段 1:量化本卡输入、推送路由 mask、清零同步状态,并准备共享专家输入。
581+ ProcessInputPreparationStage();
635 if constexpr (g_coreType == AIV) {582 if constexpr (g_coreType == AIV) {
636 PipeBarrier<PIPE_ALL>();583 PipeBarrier<PIPE_ALL>();
637 }584 }
638- SyncAll<false>(); // aic需要等待flag位reset清理完成585+ SyncAll<false>(); // AIC 等待 AIV 完成输入准备后再进入 GMM。
639 586 
640 int32_t gmTileSequence = 0;587 int32_t gmTileSequence = 0;
641- if (sharedExpertNum_ > 0) {588+ if (sharedExpertNum_ > 0U) {
589+ // 阶段 2:可选的共享专家 GMM1 及 Activation。
642 ProcessSharedExpertGmm1(gmTileSequence);590 ProcessSharedExpertGmm1(gmTileSequence);
643 }591 }
644 592 
593+ // 等待所有 rank 完成输入准备,再消费远端 Dispatch 数据。
645 CrossRankSyncInWorldSize(params_.peermemInfo.rankSyncInWorldPtr, rankId_, worldSize_, aivJob_);594 CrossRankSyncInWorldSize(params_.peermemInfo.rankSyncInWorldPtr, rankId_, worldSize_, aivJob_);
646- 595+ // 阶段 3:由派生类编排 MoE 专家的 Dispatch、GMM1/Activation 和 GMM2/Combine。
647- // 2. MoE 专家 Dispatch -> GMM1/SwiGLU -> GMM2/Combine。596+ derived.ProcessMoeExpertStages(gmTileSequence);
648- ProcessMoeExpertStages(gmTileSequence);
649- 
650 if constexpr (g_coreType == AIV) {597 if constexpr (g_coreType == AIV) {
598+ // 导出各 MoE 专家最终 token 数,并在进入后续阶段前完成核间同步。
599+ if (GetSubBlockIdx() == 1U && countWorkspace_.blockIdx == 0U) {
600+ ExportExpertTokenCounts<ActivationType, true>(commonConfig_, tokenDispatchScratch_, params_);
601+ }
651 PipeBarrier<PIPE_ALL>();602 PipeBarrier<PIPE_ALL>();
652 SyncAll<true>();603 SyncAll<true>();
653 }604 }
654- 605+ if (sharedExpertNum_ > 0U) {
655- if (sharedExpertNum_ > 0) {606+ // 阶段 4:可选的共享专家 GMM2,其结果由输出聚合阶段合并。
656 ProcessSharedExpertGmm2(gmTileSequence);607 ProcessSharedExpertGmm2(gmTileSequence);
657 }608 }
658 609 
659- // 3. 本卡数据Unpermute610+ // 阶段 5:所有 rank 完成 Combine 结果发送后,AIV 执行输出聚合。
660 if constexpr (g_coreType == AIV) {611 if constexpr (g_coreType == AIV) {
661 CrossRankSyncInWorldSize(params_.peermemInfo.rankSyncInWorldPtr, rankId_, worldSize_, aivJob_);612 CrossRankSyncInWorldSize(params_.peermemInfo.rankSyncInWorldPtr, rankId_, worldSize_, aivJob_);
662- UnpermuteBufferConfig unpermuteBufferConfig = InitTokenUnpermuteBuffers();613+ MegaMoeUnpermuteBufferConfig unpermuteBufferConfig = InitTokenUnpermuteBuffers();
663 UnpermuteTokens<CombineQuantMode, TopkWeightsType, TopkWeightsPrefetch, GMM1_TILE_M>(614 UnpermuteTokens<CombineQuantMode, TopkWeightsType, TopkWeightsPrefetch, GMM1_TILE_M>(
664 tokenUnpermuteConfig_, commonConfig_, params_, tokenUnpermuteScratch_, unpermuteBufferConfig);615 tokenUnpermuteConfig_, commonConfig_, params_, tokenUnpermuteScratch_, unpermuteBufferConfig);
665 }616 }
617+ 
618+ // 恢复入口时的溢出模式。
666 SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(oriOverflowMode);619 SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(oriOverflowMode);
667}620}
668 621 
@@ -0,0 +1,249 @@
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 mega_moe_wave_a4w4.h
13+ * \brief MegaMoe A4W4 动态 Wave 调度
14+ */
15+ 
16+#ifndef MEGA_MOE_WAVE_A4W4_H
17+#define MEGA_MOE_WAVE_A4W4_H
18+ 
19+#include "common/mega_moe_utils.h"
20+#include "mega_moe_arch35.h"
21+ 
22+namespace MegaMoeImpl {
23+ 
24+#define TemplateMegaMoeA4W4WaveTypeClass \
25+ typename XType, typename OutputType, typename TopkWeightsType, typename Weight1Type, int32_t QuantMode, \
26+ int32_t CombineQuantMode, bool TopkWeightsPrefetch
27+#define TemplateMegaMoeA4W4WaveTypeFunc \
28+ XType, OutputType, TopkWeightsType, Weight1Type, QuantMode, CombineQuantMode, TopkWeightsPrefetch
29+ 
30+template <TemplateMegaMoeA4W4WaveTypeClass>
31+class MegaMoeA4W4Wave : public MegaMoe<TemplateMegaMoeA4W4WaveTypeFunc> {
32+private:
33+ using MegaMoeBase = MegaMoe<TemplateMegaMoeA4W4WaveTypeFunc>;
34+ friend MegaMoeBase;
35+ 
36+public:
37+ using MegaMoeBase::Init;
38+ __aicore__ inline void Process();
39+ 
40+private:
41+ using QuantOutType = typename MegaMoeBase::QuantOutType;
42+ using ActivationType = typename MegaMoeBase::ActivationType;
43+ using QuantScaleOutType = typename MegaMoeBase::QuantScaleOutType;
44+ using ActivationQuantOutType = typename MegaMoeBase::ActivationQuantOutType;
45+ 
46+ static constexpr uint32_t A_ELEMS_PER_BYTE = MegaMoeBase::A_ELEMS_PER_BYTE;
47+ static constexpr uint32_t GMM1_TILE_M = MegaMoeBase::GMM1_TILE_M;
48+ static constexpr uint32_t EPILOGUE_TILE_M = MegaMoeBase::EPILOGUE_TILE_M;
49+ 
50+ using MegaMoeBase::DispatchBuffInit;
51+ using MegaMoeBase::DispatchMoeExpert;
52+ using MegaMoeBase::commonConfig_;
53+ using MegaMoeBase::countWorkspace_;
54+ using MegaMoeBase::epilogueOp_;
55+ using MegaMoeBase::gmmExecutionConfig_;
56+ using MegaMoeBase::mGroupsPerWave_;
57+ using MegaMoeBase::moeWeightTensorListAddrs_;
58+ using MegaMoeBase::params_;
59+ using MegaMoeBase::quantCombineBufferConfig_;
60+ using MegaMoeBase::quantCombineConfig_;
61+ using MegaMoeBase::syncWorkspaceLayout_;
62+ 
63+ __aicore__ inline bool IsPositionWithinWave(const ExpertTokenPosition &position, uint32_t waveMGroupCount) const
64+ {
65+ return position.expertIdx < commonConfig_.moeExpertPerRank && waveMGroupCount < mGroupsPerWave_;
66+ }
67+ 
68+ __aicore__ inline void RunGmm1ActivationForExpert(ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo,
69+ GmmRuntimeState &runtimeState, uint32_t tokenStartIndexInExpert,
70+ uint32_t sliceTokenCount, uint32_t gmm1TilesPerMGroup);
71+ __aicore__ inline void RunGmm2CombineForExpert(ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo,
72+ uint32_t &startBlockIdx, int32_t &gmTileSequence,
73+ uint32_t tokenStartIndexInExpert, uint32_t sliceTokenCount);
74+ __aicore__ inline void ProcessMoeExpertStages(int32_t &gmTileSequence);
75+};
76+ 
77+// A4W4 的 GMM1 使用 generic kernel,Activation 将中间结果提升为 FP8 后供 GMM2 使用。
78+template <TemplateMegaMoeA4W4WaveTypeClass>
79+__aicore__ inline void MegaMoeA4W4Wave<TemplateMegaMoeA4W4WaveTypeFunc>::RunGmm1ActivationForExpert(
80+ ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo, GmmRuntimeState &runtimeState, uint32_t tokenStartIndexInExpert,
81+ uint32_t sliceTokenCount, uint32_t gmm1TilesPerMGroup)
82+{
83+ UpdateMoeExpertGmm1GlobalBuffer<ActivationType, Weight1Type, ActivationQuantOutType, QuantScaleOutType,
84+ A_ELEMS_PER_BYTE, false, TopkWeightsPrefetch>(
85+ gmmExecutionConfig_, syncWorkspaceLayout_, params_.workspaceInfo, moeWeightTensorListAddrs_, epilogueOp_,
86+ gmmAddrInfo, state, tokenStartIndexInExpert, gmm1TilesPerMGroup);
87+ ProblemShape sliceProblemShape = state.problemShape;
88+ Get<M_VALUE>(sliceProblemShape) = sliceTokenCount;
89+ RunGmm1GenericByWeightFormat<QuantOutType, ActivationQuantOutType, QuantScaleOutType, GMM1_TILE_M, EPILOGUE_TILE_M,
90+ TopkWeightsPrefetch, false, true>(
91+ gmmExecutionConfig_, params_, epilogueOp_, gmmAddrInfo, sliceProblemShape,
92+ static_cast<uint32_t>(state.globalTokenStartIndex) + tokenStartIndexInExpert, runtimeState, state.expertIdx);
93+}
94+ 
95+// A4W4 的 GMM2 复用 A8W4 prologue,将 FP4 weight 解包为 FP8 后执行矩阵乘与 Combine。
96+template <TemplateMegaMoeA4W4WaveTypeClass>
97+__aicore__ inline void MegaMoeA4W4Wave<TemplateMegaMoeA4W4WaveTypeFunc>::RunGmm2CombineForExpert(
98+ ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo, uint32_t &startBlockIdx, int32_t &gmTileSequence,
99+ uint32_t tokenStartIndexInExpert, uint32_t sliceTokenCount)
100+{
101+ uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(state.problemShape));
102+ constexpr bool configureCombineCounter = CombineQuantMode != COMBINE_NO_QUANT;
103+ UpdateMoeExpertGmm2GlobalBuffer<Weight1Type, ActivationQuantOutType, QuantScaleOutType, true,
104+ configureCombineCounter, true>(gmmExecutionConfig_, syncWorkspaceLayout_,
105+ params_.workspaceInfo, moeWeightTensorListAddrs_,
106+ gmmAddrInfo, state, tokenStartIndexInExpert);
107+ ProblemShape sliceProblemShape = state.problemShape;
108+ Get<M_VALUE>(sliceProblemShape) = sliceTokenCount;
109+ RunGmm2A8W4<CombineQuantMode, ActivationQuantOutType, Weight1Type, bfloat16_t, QuantScaleOutType, QuantScaleOutType,
110+ GMM1_TILE_M, TopkWeightsPrefetch, false, false, true>(
111+ params_, sliceProblemShape, gmmAddrInfo, startBlockIdx, gmTileSequence, gmmExecutionConfig_.blockJob,
112+ expertTokenCount, tokenStartIndexInExpert);
113+ ScheduleQuantizedMoeExpertCombine<CombineQuantMode>(
114+ quantCombineConfig_, commonConfig_, quantCombineBufferConfig_, params_, gmmAddrInfo, expertTokenCount,
115+ static_cast<uint32_t>(state.globalTokenStartIndex), state.expertIdx, tokenStartIndexInExpert, sliceTokenCount);
116+}
117+ 
118+/*
119+ * 按专家顺序将 token 划分为连续 Wave。Wave 以 256 行 M group 为单位,根据 GMM1 的 N tile 数
120+ * 控制总负载;空间不足时可在专家内部切分,但同一 Wave 的 GMM1 和 GMM2 始终处理相同的 token 范围。
121+ *
122+ * Dispatch 预取用于提前准备下一 Wave 的 GMM1 输入。启动阶段先 Dispatch 第一个完整 Wave;稳态阶段
123+ * 由 AIV1 准备下一 Wave,同时 AIC/AIV0 执行当前 Wave 的 GMM1/Activation。当前 Wave 完成后执行其
124+ * GMM2/Combine,再切换到已经准备好的下一 Wave。
125+ */
126+template <TemplateMegaMoeA4W4WaveTypeClass>
127+__aicore__ inline void MegaMoeA4W4Wave<TemplateMegaMoeA4W4WaveTypeFunc>::ProcessMoeExpertStages(int32_t &gmTileSequence)
128+{
129+ DispatchBuffInit();
130+ 
131+ ExpertLoopState gmm1State = CreateExpertLoopState(commonConfig_);
132+ ExpertLoopState gmm2State = CreateExpertLoopState(commonConfig_);
133+ GMMAddrInfo gmm1AddrInfo{};
134+ GMMAddrInfo gmm2AddrInfo{};
135+ 
136+ // 同一 Block 内绑定的 1C2V 各自持有分核游标,必须按相同调用顺序推进并始终保持一致。
137+ // 某个角色即使不参与当前 GMM 计算,也必须使用该 problem 的 tile 数更新游标。
138+ uint32_t startBlockIdx = 0U;
139+ int32_t vecSetSyncCom = 0;
140+ uint16_t gmm1PingPongIdx = 0U;
141+ GmmRuntimeState gmm1RuntimeState{startBlockIdx, vecSetSyncCom, gmTileSequence, gmm1PingPongIdx};
142+ 
143+ const uint32_t gmm1TilesPerMGroup =
luobq
luobqluobqAug 21

复用tiling

likedislike
144+ Ops::Base::CeilDiv(commonConfig_.gmm1OutputDim / ACTIVATION_N_HALF, static_cast<uint32_t>(L1_TILE_N));
145+ 
146+ ExpertTokenPosition dispatchPosition{};
147+ 
148+ // 启动阶段:GMM1 开始消费输入前,由 AIV1 先 Dispatch 第一个完整 Wave。
149+ if constexpr (g_coreType == AIV) {
150+ if (GetSubBlockIdx() == 1U) {
151+ uint32_t dispatchedWaveMGroupCount = 0U;
152+ while (IsPositionWithinWave(dispatchPosition, dispatchedWaveMGroupCount)) {
153+ uint32_t expertTokenCount =
154+ dispatchPosition.tokenIndexInExpert == 0U ?
155+ DispatchMoeExpert(dispatchPosition.expertIdx) :
156+ GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_,
157+ dispatchPosition.expertIdx);
158+ AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(expertTokenCount, mGroupsPerWave_,
159+ dispatchedWaveMGroupCount, dispatchPosition);
160+ }
161+ }
162+ }
163+ 
164+ ExpertTokenPosition gmm1Position{};
165+ while (gmm1Position.expertIdx < commonConfig_.moeExpertPerRank) {
166+ ExpertTokenPosition waveBeginPosition = gmm1Position;
167+ ExpertTokenPosition waveEndPosition = gmm1Position;
168+ uint32_t currentWaveMGroupCount = 0U;
169+ bool currentWaveNeedsGmm1 = true;
170+ 
171+ uint32_t nextDispatchWaveMGroupCount = 0U;
172+ bool nextWaveNeedsDispatch = false;
173+ if constexpr (g_coreType == AIV) {
174+ if (GetSubBlockIdx() == 1U) {
175+ nextWaveNeedsDispatch = IsPositionWithinWave(dispatchPosition, nextDispatchWaveMGroupCount);
176+ }
177+ }
178+ 
179+ // 两侧进度可能不同:AIV1 可能先完成下一 Wave 的 Dispatch,AIC/AIV0 也可能先完成当前 Wave。
180+ // 只有当前计算和下轮输入准备都结束后,才能切换到当前 Wave 的 GMM2。
181+ while (currentWaveNeedsGmm1 || nextWaveNeedsDispatch) {
182+ // AIV1 逐专家准备下一 Wave;AIC/AIV0 同时推进当前 Wave 的 GMM1/Activation。
183+ if constexpr (g_coreType == AIV) {
184+ if (GetSubBlockIdx() == 1U && nextWaveNeedsDispatch) {
185+ uint32_t expertTokenCount =
186+ dispatchPosition.tokenIndexInExpert == 0U ?
187+ DispatchMoeExpert(dispatchPosition.expertIdx) :
188+ GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr,
189+ countWorkspace_, dispatchPosition.expertIdx);
190+ AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(expertTokenCount, mGroupsPerWave_,
191+ nextDispatchWaveMGroupCount, dispatchPosition);
192+ nextWaveNeedsDispatch = IsPositionWithinWave(dispatchPosition, nextDispatchWaveMGroupCount);
193+ }
194+ }
195+ 
196+ if (currentWaveNeedsGmm1) {
197+ if (gmm1Position.tokenIndexInExpert == 0U) {
198+ this->template PrepareGmmExpertState<true>(gmm1State, gmm1Position.expertIdx);
199+ }
200+ uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(gmm1State.problemShape));
201+ uint32_t sliceTokenStartIndexInExpert = gmm1Position.tokenIndexInExpert;
202+ uint32_t sliceTokenCount = AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(
203+ expertTokenCount, mGroupsPerWave_, currentWaveMGroupCount, gmm1Position);
204+ if (sliceTokenCount != 0U) {
205+ RunGmm1ActivationForExpert(gmm1State, gmm1AddrInfo, gmm1RuntimeState, sliceTokenStartIndexInExpert,
206+ sliceTokenCount, gmm1TilesPerMGroup);
207+ }
208+ waveEndPosition = gmm1Position;
209+ currentWaveNeedsGmm1 = IsPositionWithinWave(gmm1Position, currentWaveMGroupCount);
210+ }
211+ }
212+ 
213+ // Wave 在专家内结束时需包含该专家;在专家边界结束时,waveEndPosition 已指向下一专家。
214+ uint32_t waveGmm2ExpertEndExclusive =
215+ waveEndPosition.expertIdx + (waveEndPosition.tokenIndexInExpert == 0U ? 0U : 1U);
216+ for (uint32_t expertIdx = waveBeginPosition.expertIdx; expertIdx < waveGmm2ExpertEndExclusive; ++expertIdx) {
217+ uint32_t sliceTokenStartIndexInExpert =
218+ expertIdx == waveBeginPosition.expertIdx ? waveBeginPosition.tokenIndexInExpert : 0U;
219+ if (sliceTokenStartIndexInExpert == 0U) {
220+ this->template PrepareGmmExpertState<false>(gmm2State, expertIdx);
221+ }
222+ uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(gmm2State.problemShape));
223+ uint32_t sliceTokenEndIndexInExpert =
224+ expertIdx == waveEndPosition.expertIdx ? waveEndPosition.tokenIndexInExpert : expertTokenCount;
225+ uint32_t sliceTokenCount = sliceTokenEndIndexInExpert - sliceTokenStartIndexInExpert;
226+ if (sliceTokenCount != 0U) {
227+ RunGmm2CombineForExpert(gmm2State, gmm2AddrInfo, startBlockIdx, gmTileSequence,
228+ sliceTokenStartIndexInExpert, sliceTokenCount);
229+ }
230+ }
231+ }
232+ 
233+ if constexpr (!TopkWeightsPrefetch) {
234+ EndSync(gmm1RuntimeState.vecSetSyncCom);
235+ }
236+}
237+ 
238+template <TemplateMegaMoeA4W4WaveTypeClass>
239+__aicore__ inline void MegaMoeA4W4Wave<TemplateMegaMoeA4W4WaveTypeFunc>::Process()
240+{
241+ this->ProcessWave(*this);
242+}
243+ 
244+} // namespace MegaMoeImpl
245+ 
246+#undef TemplateMegaMoeA4W4WaveTypeClass
247+#undef TemplateMegaMoeA4W4WaveTypeFunc
248+ 
249+#endif
@@ -27,31 +27,11 @@ namespace MegaMoeImpl {
27#define TemplateMegaMoeA8W4WaveTypeFunc \27#define TemplateMegaMoeA8W4WaveTypeFunc \
28 XType, OutputType, TopkWeightsType, Weight1Type, QuantMode, CombineQuantMode, TopkWeightsPrefetch28 XType, OutputType, TopkWeightsType, Weight1Type, QuantMode, CombineQuantMode, TopkWeightsPrefetch
29 29 
30-// 每个逻辑 AIC 任务以两个 GMM1 scheduler tile 作为 Wave 目标负载。
31-constexpr uint32_t A8W4_GMM1_TILES_PER_JOB_TARGET = 2U;
32- 
33-// 按当前 Wave 的剩余容量推进专家 token 位置,返回本次推进覆盖的 token 数。
34-template <uint32_t TileM>
35-__aicore__ inline uint32_t AdvanceExpertTokenPositionInWave(uint32_t expertTokenCount, uint32_t waveMGroupTarget,
36- uint32_t &waveMGroupCount, ExpertTokenPosition &position)
37-{
38- uint32_t expertRemainingTokenCount = expertTokenCount - position.tokenIndexInExpert;
39- uint32_t waveRemainingTokenCapacity = (waveMGroupTarget - waveMGroupCount) * TileM;
40- uint32_t sliceTokenCount =
41- expertRemainingTokenCount < waveRemainingTokenCapacity ? expertRemainingTokenCount : waveRemainingTokenCapacity;
42- waveMGroupCount += Ops::Base::CeilDiv(sliceTokenCount, TileM);
43- position.tokenIndexInExpert += sliceTokenCount;
44- if (position.tokenIndexInExpert >= expertTokenCount) {
45- ++position.expertIdx;
46- position.tokenIndexInExpert = 0U;
47- }
48- return sliceTokenCount;
49-}
50- 
51template <TemplateMegaMoeA8W4WaveTypeClass>30template <TemplateMegaMoeA8W4WaveTypeClass>
52class MegaMoeA8W4Wave : public MegaMoe<TemplateMegaMoeA8W4WaveTypeFunc> {31class MegaMoeA8W4Wave : public MegaMoe<TemplateMegaMoeA8W4WaveTypeFunc> {
53private:32private:
54 using MegaMoeBase = MegaMoe<TemplateMegaMoeA8W4WaveTypeFunc>;33 using MegaMoeBase = MegaMoe<TemplateMegaMoeA8W4WaveTypeFunc>;
34+ friend MegaMoeBase;
55 35 
56public:36public:
57 using MegaMoeBase::Init;37 using MegaMoeBase::Init;
@@ -67,48 +47,23 @@ private:
67 static constexpr uint32_t GMM1_TILE_M = MegaMoeBase::GMM1_TILE_M;47 static constexpr uint32_t GMM1_TILE_M = MegaMoeBase::GMM1_TILE_M;
68 static constexpr uint32_t EPILOGUE_TILE_M = MegaMoeBase::EPILOGUE_TILE_M;48 static constexpr uint32_t EPILOGUE_TILE_M = MegaMoeBase::EPILOGUE_TILE_M;
69 49 
70- static_assert(Std::IsSame<Weight1Type, fp4x2_e2m1_t>::value && Std::IsSame<QuantOutType, fp8_e4m3fn_t>::value,
71- "MegaMoeA8W4Wave requires fp8 activation and fp4 weight");
72- 
73 using MegaMoeBase::DispatchBuffInit;50 using MegaMoeBase::DispatchBuffInit;
74- using MegaMoeBase::InitTokenUnpermuteBuffers;51+ using MegaMoeBase::DispatchMoeExpert;
75- using MegaMoeBase::ProcessSharedExpertGmm1;
76- using MegaMoeBase::ProcessSharedExpertGmm2;
77- using MegaMoeBase::SendAndQuantBuffInit;
78- using MegaMoeBase::aivJob_;
79- using MegaMoeBase::rankId_;
80- using MegaMoeBase::worldSize_;
81 using MegaMoeBase::epilogueOp_;52 using MegaMoeBase::epilogueOp_;
82 using MegaMoeBase::commonConfig_;53 using MegaMoeBase::commonConfig_;
83 using MegaMoeBase::countWorkspace_;54 using MegaMoeBase::countWorkspace_;
84 using MegaMoeBase::gmmExecutionConfig_;55 using MegaMoeBase::gmmExecutionConfig_;
56+ using MegaMoeBase::mGroupsPerWave_;
85 using MegaMoeBase::syncWorkspaceLayout_;57 using MegaMoeBase::syncWorkspaceLayout_;
86 using MegaMoeBase::moeWeightTensorListAddrs_;58 using MegaMoeBase::moeWeightTensorListAddrs_;
87 using MegaMoeBase::params_;59 using MegaMoeBase::params_;
88- using MegaMoeBase::quantProcessConfig_;
89- using MegaMoeBase::quantScratch_;
90 using MegaMoeBase::quantCombineBufferConfig_;60 using MegaMoeBase::quantCombineBufferConfig_;
91 using MegaMoeBase::quantCombineConfig_;61 using MegaMoeBase::quantCombineConfig_;
92- using MegaMoeBase::resetTensor_;
93- using MegaMoeBase::resetBatchElementCount_;
94- using MegaMoeBase::sendMaskConfig_;
95- using MegaMoeBase::sendMaskScratch_;
96- using MegaMoeBase::sharedExpertNum_;
97- using MegaMoeBase::sharedExpertPrepareScratch_;
98- using MegaMoeBase::tokenDispatchConfig_;
99- using MegaMoeBase::tokenDispatchScratch_;
100- using MegaMoeBase::tokenUnpermuteConfig_;
101- using MegaMoeBase::tokenUnpermuteScratch_;
102- uint32_t waveMGroupTarget_ = 0U;
103- 
104 __aicore__ inline bool IsPositionWithinWave(const ExpertTokenPosition &position, uint32_t waveMGroupCount) const62 __aicore__ inline bool IsPositionWithinWave(const ExpertTokenPosition &position, uint32_t waveMGroupCount) const
105 {63 {
106- return position.expertIdx < commonConfig_.moeExpertPerRank && waveMGroupCount < waveMGroupTarget_;64+ return position.expertIdx < commonConfig_.moeExpertPerRank && waveMGroupCount < mGroupsPerWave_;
107 }65 }
108 66 
109- __aicore__ inline uint32_t DispatchMoeExpert(uint32_t expertIdx);
110- template <bool WaitForTokenCountReady>
111- __aicore__ inline void PrepareGmmExpertState(ExpertLoopState &state, uint32_t expertIdx);
112 __aicore__ inline void RunGmm1ActivationForExpert(ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo,67 __aicore__ inline void RunGmm1ActivationForExpert(ExpertLoopState &state, GMMAddrInfo &gmmAddrInfo,
113 uint32_t &startBlockIdx, int32_t &gmTileSequence,68 uint32_t &startBlockIdx, int32_t &gmTileSequence,
114 uint32_t tokenStartIndexInExpert, uint32_t sliceTokenCount,69 uint32_t tokenStartIndexInExpert, uint32_t sliceTokenCount,
@@ -119,34 +74,6 @@ private:
119 __aicore__ inline void ProcessMoeExpertStages(int32_t &gmTileSequence);74 __aicore__ inline void ProcessMoeExpertStages(int32_t &gmTileSequence);
120};75};
121 76 
122-// 计算当前专家的 token 数,并在非空时执行完整 Dispatch(复用单专家 Dispatch 入口)。
123-template <TemplateMegaMoeA8W4WaveTypeClass>
124-__aicore__ inline uint32_t MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::DispatchMoeExpert(uint32_t expertIdx)
125-{
126- uint32_t expertTokenCount = 0U;
127- RunMoeExpertDispatchStage<ActivationType, QuantScaleOutType, true, GMM1_TILE_M, TopkWeightsPrefetch>(
128- tokenDispatchConfig_, commonConfig_, gmmExecutionConfig_.blockJob, countWorkspace_, syncWorkspaceLayout_,
129- params_, winRankAddr_, tokenDispatchScratch_, expertIdx, expertTokenCount);
130- return expertTokenCount;
131-}
132- 
133-// 从 workspace 读取 token 数并准备 GMM 专家状态;GMM1 可按需先等待 token count ready。
134-template <TemplateMegaMoeA8W4WaveTypeClass>
135-template <bool WaitForTokenCountReady>
136-__aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::PrepareGmmExpertState(ExpertLoopState &state,
137- uint32_t expertIdx)
138-{
139- if constexpr (WaitForTokenCountReady) {
140- if (GetSubBlockIdx() == 0U) {
141- WaitForMoeExpertTokenCountReady(params_.workspaceInfo.flagSendCntCalToUpdParamsPtr, countWorkspace_,
142- expertIdx);
143- }
144- }
145- uint32_t expertTokenCount =
146- GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_, expertIdx);
147- UpdateExpertLoopState(state, expertIdx, expertTokenCount);
148-}
149- 
150// 更新当前专家切片的输入、输出及同步地址,并执行 GMM1 和 Activation。77// 更新当前专家切片的输入、输出及同步地址,并执行 GMM1 和 Activation。
151template <TemplateMegaMoeA8W4WaveTypeClass>78template <TemplateMegaMoeA8W4WaveTypeClass>
152__aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::RunGmm1ActivationForExpert(79__aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::RunGmm1ActivationForExpert(
@@ -209,9 +136,6 @@ __aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process
209 136 
210 const uint32_t gmm1TilesPerMGroup =137 const uint32_t gmm1TilesPerMGroup =
211 Ops::Base::CeilDiv(commonConfig_.gmm1OutputDim / ACTIVATION_N_HALF, static_cast<uint32_t>(L1_TILE_N));138 Ops::Base::CeilDiv(commonConfig_.gmm1OutputDim / ACTIVATION_N_HALF, static_cast<uint32_t>(L1_TILE_N));
212- // Wave 以完整 M group 为切分单位;一个 M group 会产生 gmm1TilesPerMGroup 个 scheduler tile。
213- waveMGroupTarget_ =
214- Ops::Base::CeilDiv(A8W4_GMM1_TILES_PER_JOB_TARGET * gmmExecutionConfig_.blockJob.totalJobs, gmm1TilesPerMGroup);
215 139 
216 ExpertTokenPosition dispatchPosition{};140 ExpertTokenPosition dispatchPosition{};
217 141 
@@ -225,7 +149,7 @@ __aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process
225 DispatchMoeExpert(dispatchPosition.expertIdx) :149 DispatchMoeExpert(dispatchPosition.expertIdx) :
226 GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_,150 GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr, countWorkspace_,
227 dispatchPosition.expertIdx);151 dispatchPosition.expertIdx);
228- AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(expertTokenCount, waveMGroupTarget_,152+ AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(expertTokenCount, mGroupsPerWave_,
229 dispatchedWaveMGroupCount, dispatchPosition);153 dispatchedWaveMGroupCount, dispatchPosition);
230 }154 }
231 }155 }
@@ -255,7 +179,7 @@ __aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process
255 DispatchMoeExpert(dispatchPosition.expertIdx) :179 DispatchMoeExpert(dispatchPosition.expertIdx) :
256 GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr,180 GetExpertTokenCountFromWorkspace(params_.workspaceInfo.expertRevTokenNumsPtr,
257 countWorkspace_, dispatchPosition.expertIdx);181 countWorkspace_, dispatchPosition.expertIdx);
258- AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(expertTokenCount, waveMGroupTarget_,182+ AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(expertTokenCount, mGroupsPerWave_,
259 nextDispatchWaveMGroupCount, dispatchPosition);183 nextDispatchWaveMGroupCount, dispatchPosition);
260 nextWaveNeedsDispatch = IsPositionWithinWave(dispatchPosition, nextDispatchWaveMGroupCount);184 nextWaveNeedsDispatch = IsPositionWithinWave(dispatchPosition, nextDispatchWaveMGroupCount);
261 }185 }
@@ -263,12 +187,12 @@ __aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process
263 187 
264 if (currentWaveNeedsGmm1) {188 if (currentWaveNeedsGmm1) {
265 if (gmm1Position.tokenIndexInExpert == 0U) {189 if (gmm1Position.tokenIndexInExpert == 0U) {
266- PrepareGmmExpertState<true>(gmm1State, gmm1Position.expertIdx);190+ this->template PrepareGmmExpertState<true>(gmm1State, gmm1Position.expertIdx);
267 }191 }
268 uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(gmm1State.problemShape));192 uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(gmm1State.problemShape));
269 uint32_t sliceTokenStartIndexInExpert = gmm1Position.tokenIndexInExpert;193 uint32_t sliceTokenStartIndexInExpert = gmm1Position.tokenIndexInExpert;
270 uint32_t sliceTokenCount = AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(194 uint32_t sliceTokenCount = AdvanceExpertTokenPositionInWave<GMM1_TILE_M>(
271- expertTokenCount, waveMGroupTarget_, currentWaveMGroupCount, gmm1Position);195+ expertTokenCount, mGroupsPerWave_, currentWaveMGroupCount, gmm1Position);
272 if (sliceTokenCount != 0U) {196 if (sliceTokenCount != 0U) {
273 RunGmm1ActivationForExpert(gmm1State, gmm1AddrInfo, startBlockIdx, gmTileSequence,197 RunGmm1ActivationForExpert(gmm1State, gmm1AddrInfo, startBlockIdx, gmTileSequence,
274 sliceTokenStartIndexInExpert, sliceTokenCount, gmm1TilesPerMGroup);198 sliceTokenStartIndexInExpert, sliceTokenCount, gmm1TilesPerMGroup);
@@ -286,7 +210,7 @@ __aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process
286 expertIdx == waveBeginPosition.expertIdx ? waveBeginPosition.tokenIndexInExpert : 0U;210 expertIdx == waveBeginPosition.expertIdx ? waveBeginPosition.tokenIndexInExpert : 0U;
287 // Wave 从专家起点开始时,准备该专家的 GMM2 状态。211 // Wave 从专家起点开始时,准备该专家的 GMM2 状态。
288 if (sliceTokenStartIndexInExpert == 0U) {212 if (sliceTokenStartIndexInExpert == 0U) {
289- PrepareGmmExpertState<false>(gmm2State, expertIdx);213+ this->template PrepareGmmExpertState<false>(gmm2State, expertIdx);
290 }214 }
291 uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(gmm2State.problemShape));215 uint32_t expertTokenCount = static_cast<uint32_t>(Get<M_VALUE>(gmm2State.problemShape));
292 uint32_t sliceTokenEndIndexInExpert =216 uint32_t sliceTokenEndIndexInExpert =
@@ -303,54 +227,7 @@ __aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process
303template <TemplateMegaMoeA8W4WaveTypeClass>227template <TemplateMegaMoeA8W4WaveTypeClass>
304__aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process()228__aicore__ inline void MegaMoeA8W4Wave<TemplateMegaMoeA8W4WaveTypeFunc>::Process()
305{229{
306- int64_t oriOverflowMode = GetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>();230+ this->ProcessWave(*this);
307- SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(0);
308- SendAndQuantBuffInit();
309- 
310- // 输入准备:量化本卡 token、发布路由 mask,并重置共享同步状态。
311- QuantizeLocalTokens<QuantMode, QuantOutType, ActivationType, TopkWeightsType, TopkWeightsPrefetch>(
312- aivJob_, commonConfig_, params_, quantProcessConfig_, quantScratch_);
313- GatherAndSendExpertMasks(aivJob_, commonConfig_, params_, winRankAddr_, sendMaskConfig_, sendMaskScratch_);
314- ResetSyncStatus<TopkWeightsPrefetch>(aivJob_, params_, resetBatchElementCount_, resetTensor_);
315- if (sharedExpertNum_ > 0U) {
316- PrepareSharedExpertInput<ActivationType, QuantScaleOutType, A_ELEMS_PER_BYTE>(
317- aivJob_, commonConfig_, params_, quantProcessConfig_, sharedExpertPrepareScratch_);
318- }
319- if constexpr (g_coreType == AIV) {
320- PipeBarrier<PIPE_ALL>();
321- }
322- SyncAll<false>();
323- 
324- int32_t gmTileSequence = 0;
325- if (sharedExpertNum_ > 0U) {
326- ProcessSharedExpertGmm1(gmTileSequence);
327- }
328- 
329- CrossRankSyncInWorldSize(params_.peermemInfo.rankSyncInWorldPtr, rankId_, worldSize_, aivJob_);
330- 
331- // MoE 专家流水:动态 Wave Dispatch、GMM1/Activation、GMM2 和 Combine。
luobq
luobqluobqAug 21
Outdated

注释恢复一下

likedislike
332- ProcessMoeExpertStages(gmTileSequence);
333- 
334- if constexpr (g_coreType == AIV) {
335- if (GetSubBlockIdx() == 1U && countWorkspace_.blockIdx == 0U) {
336- ExportExpertTokenCounts<ActivationType, true>(commonConfig_, tokenDispatchScratch_, params_);
337- }
338- PipeBarrier<PIPE_ALL>();
339- SyncAll<true>();
340- }
341- 
342- if (sharedExpertNum_ > 0U) {
343- ProcessSharedExpertGmm2(gmTileSequence);
344- }
345- 
346- // 所有 rank 完成 Combine 结果发送后,再开始输出聚合。
347- if constexpr (g_coreType == AIV) {
348- CrossRankSyncInWorldSize(params_.peermemInfo.rankSyncInWorldPtr, rankId_, worldSize_, aivJob_);
349- MegaMoeUnpermuteBufferConfig unpermuteBufferConfig = InitTokenUnpermuteBuffers();
350- UnpermuteTokens<CombineQuantMode, TopkWeightsType, TopkWeightsPrefetch, GMM1_TILE_M>(
351- tokenUnpermuteConfig_, commonConfig_, params_, tokenUnpermuteScratch_, unpermuteBufferConfig);
352- }
353- SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(oriOverflowMode);
354}231}
355 232 
356} // namespace MegaMoeImpl233} // namespace MegaMoeImpl
Rmc2/mega_moe/op_kernel/arch35/mega_moe_wave.h→mc2/mega_moe/op_kernel/arch35/mega_moe_wave_a8w8.h+74-67
@@ -9,12 +9,12 @@
9 */9 */
10 10 
11/*!11/*!
12- * \file mega_moe_wave.h12+ * \file mega_moe_wave_a8w8.h
13 * \brief MegaMoe A8W8 wave 流水实现13 * \brief MegaMoe A8W8 wave 流水实现
14 */14 */
15 15 
16-#ifndef MEGA_MOE_WAVE_H16+#ifndef MEGA_MOE_WAVE_A8W8_H
17-#define MEGA_MOE_WAVE_H17+#define MEGA_MOE_WAVE_A8W8_H
18 18 
19#include "kernel_operator.h"19#include "kernel_operator.h"
20#include "kernel_tiling/kernel_tiling.h"20#include "kernel_tiling/kernel_tiling.h"
@@ -47,14 +47,14 @@ namespace MegaMoeImpl {
47using namespace AscendC;47using namespace AscendC;
48 48 
49// 预留:XType OutputType TopkWeightsType Weight1Type49// 预留:XType OutputType TopkWeightsType Weight1Type
50-#define TemplateMegaMoeWaveTypeClass \50+#define TemplateMegaMoeA8W8WaveTypeClass \
51 typename XType, typename OutputType, typename TopkWeightsType, typename Weight1Type, int32_t QuantMode, \51 typename XType, typename OutputType, typename TopkWeightsType, typename Weight1Type, int32_t QuantMode, \
52 int32_t CombineQuantMode, bool TopkWeightsPrefetch, bool IsGmm1Interleaved52 int32_t CombineQuantMode, bool TopkWeightsPrefetch, bool IsGmm1Interleaved
53-#define TemplateMegaMoeWaveTypeFunc \53+#define TemplateMegaMoeA8W8WaveTypeFunc \
54 XType, OutputType, TopkWeightsType, Weight1Type, QuantMode, CombineQuantMode, TopkWeightsPrefetch, IsGmm1Interleaved54 XType, OutputType, TopkWeightsType, Weight1Type, QuantMode, CombineQuantMode, TopkWeightsPrefetch, IsGmm1Interleaved
55 55 
56-template <TemplateMegaMoeWaveTypeClass>56+template <TemplateMegaMoeA8W8WaveTypeClass>
57-class MegaMoeWave {57+class MegaMoeA8W8Wave {
58public:58public:
59 template <int32_t QM>59 template <int32_t QM>
60 struct QuantTraits {60 struct QuantTraits {
@@ -71,7 +71,7 @@ public:
71 using QuantOutType = typename QuantTraits<QuantMode>::OutType;71 using QuantOutType = typename QuantTraits<QuantMode>::OutType;
72 using QuantScaleOutType = typename std::conditional<(QuantMode >= E5M2_QUANT), fp8_e8m0_t, float>::type;72 using QuantScaleOutType = typename std::conditional<(QuantMode >= E5M2_QUANT), fp8_e8m0_t, float>::type;
73 using ActivationType = QuantOutType;73 using ActivationType = QuantOutType;
74- __aicore__ inline MegaMoeWave(){};74+ __aicore__ inline MegaMoeA8W8Wave(){};
75 __aicore__ inline void Init(GM_ADDR context, GM_ADDR x, GM_ADDR topkIds, GM_ADDR topkWeights, GM_ADDR weight1,75 __aicore__ inline void Init(GM_ADDR context, GM_ADDR x, GM_ADDR topkIds, GM_ADDR topkWeights, GM_ADDR weight1,
76 GM_ADDR weight2, GM_ADDR xActiveMask, GM_ADDR weightScales1, GM_ADDR weightScales2,76 GM_ADDR weight2, GM_ADDR xActiveMask, GM_ADDR weightScales1, GM_ADDR weightScales2,
77 GM_ADDR scales, GM_ADDR sharedWeight1, GM_ADDR sharedWeight2,77 GM_ADDR scales, GM_ADDR sharedWeight1, GM_ADDR sharedWeight2,
@@ -100,10 +100,10 @@ private:
100 const ExpertTokenPosition &targetPosition) const;100 const ExpertTokenPosition &targetPosition) const;
luobq
luobqluobqAug 21

a8w8

likedislike
101 __aicore__ inline ExpertTokenPosition ProcessGmm1Wave(ExpertTokenPosition &gmm1Position,101 __aicore__ inline ExpertTokenPosition ProcessGmm1Wave(ExpertTokenPosition &gmm1Position,
102 ExpertLoopState &gmm1ExpertState, GMMAddrInfo &gmm1AddrInfo,102 ExpertLoopState &gmm1ExpertState, GMMAddrInfo &gmm1AddrInfo,
103- Gmm1ActivationState &runtimeState);103+ GmmRuntimeState &runtimeState);
104 __aicore__ inline void ProcessGmm2Wave(ExpertTokenPosition &gmm2Position,104 __aicore__ inline void ProcessGmm2Wave(ExpertTokenPosition &gmm2Position,
105 const ExpertTokenPosition &waveEndPosition, ExpertLoopState &gmm2ExpertState,105 const ExpertTokenPosition &waveEndPosition, ExpertLoopState &gmm2ExpertState,
106- GMMAddrInfo &gmm2AddrInfo, Gmm2RuntimeState &runtimeState);106+ GMMAddrInfo &gmm2AddrInfo, GmmRuntimeState &runtimeState);
107 __aicore__ inline void ProcessCombineExperts(uint32_t expertBegin, uint32_t expertEnd,107 __aicore__ inline void ProcessCombineExperts(uint32_t expertBegin, uint32_t expertEnd,
108 ExpertLoopState &combineState, GMMAddrInfo &combineAddrInfo,108 ExpertLoopState &combineState, GMMAddrInfo &combineAddrInfo,
109 const CombineBufferConfig &bufferConfig);109 const CombineBufferConfig &bufferConfig);
@@ -180,8 +180,8 @@ private:
180 SharedBlockEpilogue sharedEpilogueOp_;180 SharedBlockEpilogue sharedEpilogueOp_;
181};181};
182 182 
183-template <TemplateMegaMoeWaveTypeClass>183+template <TemplateMegaMoeA8W8WaveTypeClass>
184-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitInputPrepareConfigs()184+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::InitInputPrepareConfigs()
185{185{
186 aivJob_ = {.jobIndex = aivCoreIdx_, .totalJobs = blockAivNum_};186 aivJob_ = {.jobIndex = aivCoreIdx_, .totalJobs = blockAivNum_};
187 quantProcessConfig_ = CreateQuantProcessConfig<ActivationType, QuantScaleOutType, TopkWeightsPrefetch,187 quantProcessConfig_ = CreateQuantProcessConfig<ActivationType, QuantScaleOutType, TopkWeightsPrefetch,
@@ -189,8 +189,9 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitInputPrepar
189 sendMaskConfig_ = CreateSendMaskConfig(params_, aivCoreIdx_);189 sendMaskConfig_ = CreateSendMaskConfig(params_, aivCoreIdx_);
190}190}
191 191 
192-template <TemplateMegaMoeWaveTypeClass>192+template <TemplateMegaMoeA8W8WaveTypeClass>
193-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitGmmConfigs(int32_t dispatchFlagSlotsPerExpert)193+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::InitGmmConfigs(
194+ int32_t dispatchFlagSlotsPerExpert)
194{195{
195 gmmExecutionConfig_ = {.blockJob = {.jobIndex = blockIdx_, .totalJobs = blockNum_},196 gmmExecutionConfig_ = {.blockJob = {.jobIndex = blockIdx_, .totalJobs = blockNum_},
196 .groupedMatmulMode = params_.tilingData->groupedMatmulMode,197 .groupedMatmulMode = params_.tilingData->groupedMatmulMode,
@@ -203,8 +204,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitGmmConfigs(
203 waveCombineJob_ = {.jobIndex = blockIdx_, .totalJobs = blockNum_};204 waveCombineJob_ = {.jobIndex = blockIdx_, .totalJobs = blockNum_};
204}205}
205 206 
206-template <TemplateMegaMoeWaveTypeClass>207+template <TemplateMegaMoeA8W8WaveTypeClass>
207-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitTokenUnpermuteConfig()208+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::InitTokenUnpermuteConfig()
208{209{
209 uint32_t quantTokenSizeBytes = 0U;210 uint32_t quantTokenSizeBytes = 0U;
210 if constexpr (CombineQuantMode != COMBINE_NO_QUANT && g_coreType == AIV) {211 if constexpr (CombineQuantMode != COMBINE_NO_QUANT && g_coreType == AIV) {
@@ -220,8 +221,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitTokenUnperm
220// ========================221// ========================
221// Init:初始化成员并计算地址偏移222// Init:初始化成员并计算地址偏移
222// ========================223// ========================
223-template <TemplateMegaMoeWaveTypeClass>224+template <TemplateMegaMoeA8W8WaveTypeClass>
224-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::Init(225+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::Init(
225 GM_ADDR context, GM_ADDR x, GM_ADDR topkIds, GM_ADDR topkWeights, GM_ADDR weight1, GM_ADDR weight2,226 GM_ADDR context, GM_ADDR x, GM_ADDR topkIds, GM_ADDR topkWeights, GM_ADDR weight1, GM_ADDR weight2,
226 GM_ADDR xActiveMask, GM_ADDR weightScales1, GM_ADDR weightScales2, GM_ADDR scales, GM_ADDR sharedWeight1,227 GM_ADDR xActiveMask, GM_ADDR weightScales1, GM_ADDR weightScales2, GM_ADDR scales, GM_ADDR sharedWeight1,
227 GM_ADDR sharedWeight2, GM_ADDR sharedWeightScales1, GM_ADDR sharedWeightScales2, GM_ADDR yOut,228 GM_ADDR sharedWeight2, GM_ADDR sharedWeightScales1, GM_ADDR sharedWeightScales2, GM_ADDR yOut,
@@ -285,8 +286,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::Init(
285// =================================================================================================286// =================================================================================================
286// DispatchBuffInit:申请公共 SendCount 和 Token Dispatch 函数使用的 buffer。287// DispatchBuffInit:申请公共 SendCount 和 Token Dispatch 函数使用的 buffer。
287// =================================================================================================288// =================================================================================================
288-template <TemplateMegaMoeWaveTypeClass>289+template <TemplateMegaMoeA8W8WaveTypeClass>
289-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::DispatchBuffInit()290+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::DispatchBuffInit()
290{291{
291 DispatchBufferConfig bufferConfig{};292 DispatchBufferConfig bufferConfig{};
292 if constexpr (g_coreType == AIC) {293 if constexpr (g_coreType == AIC) {
@@ -367,9 +368,9 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::DispatchBuffIni
367 tokenDispatchScratch_.cumsumRevCntInRank = 0U;368 tokenDispatchScratch_.cumsumRevCntInRank = 0U;
368}369}
369 370 
370-template <TemplateMegaMoeWaveTypeClass>371+template <TemplateMegaMoeA8W8WaveTypeClass>
371-__aicore__ inline typename MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::CombineBufferConfig372+__aicore__ inline typename MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::CombineBufferConfig
372-MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitCombineBuffers()373+MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::InitCombineBuffers()
373{374{
374 CombineBufferConfig bufferConfig{};375 CombineBufferConfig bufferConfig{};
375 if constexpr (g_coreType == AIC) {376 if constexpr (g_coreType == AIC) {
@@ -408,8 +409,8 @@ MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitCombineBuffers()
408 return bufferConfig;409 return bufferConfig;
409}410}
410 411 
411-template <TemplateMegaMoeWaveTypeClass>412+template <TemplateMegaMoeA8W8WaveTypeClass>
412-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitExpertTokenCountExportBuffers()413+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::InitExpertTokenCountExportBuffers()
413{414{
414 if constexpr (g_coreType == AIC) {415 if constexpr (g_coreType == AIC) {
415 return;416 return;
@@ -430,8 +431,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitExpertToken
430// ======================================================================================431// ======================================================================================
431// SendAndQuantBuffInit:申请公共 mask、workspace reset 和 token quant 函数使用的 buffer。432// SendAndQuantBuffInit:申请公共 mask、workspace reset 和 token quant 函数使用的 buffer。
432// ======================================================================================433// ======================================================================================
433-template <TemplateMegaMoeWaveTypeClass>434+template <TemplateMegaMoeA8W8WaveTypeClass>
434-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::SendAndQuantBuffInit()435+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::SendAndQuantBuffInit()
435{436{
436 SendMaskBufferConfig bufferConfig{};437 SendMaskBufferConfig bufferConfig{};
437 if constexpr (g_coreType == AIC) {438 if constexpr (g_coreType == AIC) {
@@ -511,11 +512,11 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::SendAndQuantBuf
511// ===============================================================512// ===============================================================
512// 可选共享专家流程:依次执行共享专家 GMM1 与 SwiGLU,并在结束后重置 GMM 调度状态。513// 可选共享专家流程:依次执行共享专家 GMM1 与 SwiGLU,并在结束后重置 GMM 调度状态。
513// ===============================================================514// ===============================================================
514-template <TemplateMegaMoeWaveTypeClass>515+template <TemplateMegaMoeA8W8WaveTypeClass>
515-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessSharedExpertGmm1()516+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessSharedExpertGmm1()
516{517{
517 if (gmmExecutionConfig_.blockJob.totalJobs == 0U ||518 if (gmmExecutionConfig_.blockJob.totalJobs == 0U ||
518- gmmExecutionConfig_.blockJob.jobIndex >= gmmExecutionConfig_.blockJob.totalJobs || sharedExpertNum_ == 0U) {519+ gmmExecutionConfig_.blockJob.jobIndex >= gmmExecutionConfig_.blockJob.totalJobs) {
519 return;520 return;
520 }521 }
521 typename SharedBlockEpilogue::Params epilogueParams{522 typename SharedBlockEpilogue::Params epilogueParams{
@@ -538,7 +539,7 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessSharedEx
538 GMMAddrInfo gmmAddrInfo{};539 GMMAddrInfo gmmAddrInfo{};
539 int32_t vecSetSyncCom = 0;540 int32_t vecSetSyncCom = 0;
540 int32_t gmTileSequence = 0;541 int32_t gmTileSequence = 0;
541- SharedExpertGmm1ActivationState runtimeState{startBlockIdx_, vecSetSyncCom, gmTileSequence, gmm1PingPongIdx_};542+ GmmRuntimeState runtimeState{startBlockIdx_, vecSetSyncCom, gmTileSequence, gmm1PingPongIdx_};
542 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {543 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {
543 UpdateSharedExpertGmm1GlobalBuffer<ActivationType, Weight1Type, ActivationType, QuantScaleOutType, false>(544 UpdateSharedExpertGmm1GlobalBuffer<ActivationType, Weight1Type, ActivationType, QuantScaleOutType, false>(
544 commonConfig_, gmmExecutionConfig_, params_.workspaceInfo, sharedWeightTensorListAddrs_, sharedEpilogueOp_,545 commonConfig_, gmmExecutionConfig_, params_.workspaceInfo, sharedWeightTensorListAddrs_, sharedEpilogueOp_,
@@ -553,18 +554,24 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessSharedEx
553 startBlockIdx_ = 0; // 共享专家 GMM1 修改了 startBlockIdx_,重置后供 MoE 专家 GMM1 使用554 startBlockIdx_ = 0; // 共享专家 GMM1 修改了 startBlockIdx_,重置后供 MoE 专家 GMM1 使用
554}555}
555 556 
556-template <TemplateMegaMoeWaveTypeClass>557+template <TemplateMegaMoeA8W8WaveTypeClass>
557-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessSharedExpertGmm2()558+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessSharedExpertGmm2()
558{559{
560+ ProblemShape problemShape;
561+ Get<M_VALUE>(problemShape) = commonConfig_.tokenNum;
562+ Get<N_VALUE>(problemShape) = commonConfig_.gmm1OutputDim;
563+ Get<K_VALUE>(problemShape) = commonConfig_.tokenHiddenDim;
559 int32_t vecSetSyncCom = 0;564 int32_t vecSetSyncCom = 0;
560 int32_t gmTileSequence = 0;565 int32_t gmTileSequence = 0;
561- Gmm2RuntimeState runtimeState{startBlockIdx_, vecSetSyncCom, gmTileSequence, gmm2PingPongIdx_};566+ GmmRuntimeState runtimeState{startBlockIdx_, vecSetSyncCom, gmTileSequence, gmm2PingPongIdx_};
562 GMMAddrInfo gmmAddrInfo{};567 GMMAddrInfo gmmAddrInfo{};
563 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {568 for (uint32_t sharedExpertIdx = 0U; sharedExpertIdx < sharedExpertNum_; ++sharedExpertIdx) {
564- RunSharedExpertGmm2Stage<QuantOutType, ActivationType, Weight1Type, QuantScaleOutType, false, false,569+ UpdateSharedExpertGmm2GlobalBuffer<ActivationType, Weight1Type, QuantScaleOutType, GMM1_TILE_M, false>(
565- GMM1_TILE_M, TopkWeightsPrefetch, GMM1_INTERLEAVED, true>(570+ commonConfig_, gmmExecutionConfig_, params_.workspaceInfo, sharedWeightTensorListAddrs_, gmmAddrInfo,
566- commonConfig_, gmmExecutionConfig_, params_, sharedWeightTensorListAddrs_, gmmAddrInfo, runtimeState,571+ sharedExpertIdx);
567- sharedExpertIdx, nullptr, true);572+ RunGmm2ByMode<COMBINE_NO_QUANT, QuantOutType, ActivationType, Weight1Type, QuantScaleOutType, false, false,
573+ GMM1_TILE_M, TopkWeightsPrefetch, true, GMM1_INTERLEAVED, true>(
574+ gmmExecutionConfig_, params_, gmmAddrInfo, problemShape, runtimeState, nullptr, true);
568 }575 }
569}576}
570 577 
@@ -572,16 +579,16 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessSharedEx
572// Wave 模板主流程。579// Wave 模板主流程。
573// ===============================================================580// ===============================================================
574// 在 wave 模板内构造 Unpermute 使用的 UB 视图,并返回当前 AIV 对应的 buffer 配置。581// 在 wave 模板内构造 Unpermute 使用的 UB 视图,并返回当前 AIV 对应的 buffer 配置。
575-template <TemplateMegaMoeWaveTypeClass>582+template <TemplateMegaMoeA8W8WaveTypeClass>
576-__aicore__ inline typename MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::UnpermuteBufferConfig583+__aicore__ inline typename MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::UnpermuteBufferConfig
577-MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::InitTokenUnpermuteBuffers()584+MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::InitTokenUnpermuteBuffers()
578{585{
579 return CreateTokenUnpermuteBuffers<TopkWeightsType, CombineQuantMode>(586 return CreateTokenUnpermuteBuffers<TopkWeightsType, CombineQuantMode>(
580 tokenUnpermuteConfig_, commonConfig_.tokenHiddenDim, tokenUnpermuteScratch_);587 tokenUnpermuteConfig_, commonConfig_.tokenHiddenDim, tokenUnpermuteScratch_);
581}588}
582 589 
583-template <TemplateMegaMoeWaveTypeClass>590+template <TemplateMegaMoeA8W8WaveTypeClass>
584-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::DispatchAllExpertTokens()591+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::DispatchAllExpertTokens()
585{592{
586 /**593 /**
587 * 在 GMM pipeline 开始前完成所有专家的 Dispatch。每个专家单独使用全部594 * 在 GMM pipeline 开始前完成所有专家的 Dispatch。每个专家单独使用全部
@@ -595,18 +602,18 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::DispatchAllExpe
595 }602 }
596}603}
597 604 
598-template <TemplateMegaMoeWaveTypeClass>605+template <TemplateMegaMoeA8W8WaveTypeClass>
599-__aicore__ inline bool MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::IsSameExpertTokenPosition(606+__aicore__ inline bool MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::IsSameExpertTokenPosition(
600 const ExpertTokenPosition &currentPosition, const ExpertTokenPosition &targetPosition) const607 const ExpertTokenPosition &currentPosition, const ExpertTokenPosition &targetPosition) const
601{608{
602 return currentPosition.expertIdx == targetPosition.expertIdx &&609 return currentPosition.expertIdx == targetPosition.expertIdx &&
603 currentPosition.tokenIndexInExpert == targetPosition.tokenIndexInExpert;610 currentPosition.tokenIndexInExpert == targetPosition.tokenIndexInExpert;
604}611}
605 612 
606-template <TemplateMegaMoeWaveTypeClass>613+template <TemplateMegaMoeA8W8WaveTypeClass>
607-__aicore__ inline ExpertTokenPosition MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessGmm1Wave(614+__aicore__ inline ExpertTokenPosition MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessGmm1Wave(
608 ExpertTokenPosition &gmm1Position, ExpertLoopState &gmm1ExpertState, GMMAddrInfo &gmm1AddrInfo,615 ExpertTokenPosition &gmm1Position, ExpertLoopState &gmm1ExpertState, GMMAddrInfo &gmm1AddrInfo,
609- Gmm1ActivationState &runtimeState)616+ GmmRuntimeState &runtimeState)
610{617{
611 if constexpr (g_coreType == AIV) {618 if constexpr (g_coreType == AIV) {
612 if (GetSubBlockIdx() == 1U) {619 if (GetSubBlockIdx() == 1U) {
@@ -650,8 +657,8 @@ __aicore__ inline ExpertTokenPosition MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::
650 gmm1Position.tokenIndexInExpert == 0U && static_cast<uint64_t>(waveEndTokenIndexInExpert) == expertRowCount;657 gmm1Position.tokenIndexInExpert == 0U && static_cast<uint64_t>(waveEndTokenIndexInExpert) == expertRowCount;
651 uint32_t waveTokenStartIndex =658 uint32_t waveTokenStartIndex =
652 static_cast<uint32_t>(gmm1ExpertState.globalTokenStartIndex) + gmm1Position.tokenIndexInExpert;659 static_cast<uint32_t>(gmm1ExpertState.globalTokenStartIndex) + gmm1Position.tokenIndexInExpert;
653- RunGmm1ActivationByMode<QuantOutType, Weight1Type, ActivationType, QuantScaleOutType, false, GMM1_TILE_M,660+ RunGmm1GenericByWeightFormat<QuantOutType, ActivationType, QuantScaleOutType, GMM1_TILE_M, EPILOGUE_TILE_M,
654- EPILOGUE_TILE_M, TopkWeightsPrefetch, GMM1_INTERLEAVED, true>(661+ TopkWeightsPrefetch, GMM1_INTERLEAVED, true>(
655 gmmExecutionConfig_, params_, epilogueOp_, gmm1AddrInfo, gmm1WaveProblemShape, waveTokenStartIndex,662 gmmExecutionConfig_, params_, epilogueOp_, gmm1AddrInfo, gmm1WaveProblemShape, waveTokenStartIndex,
656 runtimeState, gmm1Position.expertIdx, nullptr, isWholeExpert);663 runtimeState, gmm1Position.expertIdx, nullptr, isWholeExpert);
657 664 
@@ -665,10 +672,10 @@ __aicore__ inline ExpertTokenPosition MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::
665 return gmm1Position;672 return gmm1Position;
666}673}
667 674 
668-template <TemplateMegaMoeWaveTypeClass>675+template <TemplateMegaMoeA8W8WaveTypeClass>
669-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessGmm2Wave(676+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessGmm2Wave(
670 ExpertTokenPosition &gmm2Position, const ExpertTokenPosition &waveEndPosition, ExpertLoopState &gmm2ExpertState,677 ExpertTokenPosition &gmm2Position, const ExpertTokenPosition &waveEndPosition, ExpertLoopState &gmm2ExpertState,
671- GMMAddrInfo &gmm2AddrInfo, Gmm2RuntimeState &runtimeState)678+ GMMAddrInfo &gmm2AddrInfo, GmmRuntimeState &runtimeState)
672{679{
673 if constexpr (g_coreType == AIV) {680 if constexpr (g_coreType == AIV) {
674 if (GetSubBlockIdx() == 1U) {681 if (GetSubBlockIdx() == 1U) {
@@ -702,8 +709,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessGmm2Wave
702 // GMM2 与 GMM1 使用相同保护:只有完整专家 problem 才允许进一步判断是否绕过 L2。709 // GMM2 与 GMM1 使用相同保护:只有完整专家 problem 才允许进一步判断是否绕过 L2。
703 bool isWholeExpert =710 bool isWholeExpert =
704 gmm2Position.tokenIndexInExpert == 0U && static_cast<uint64_t>(waveEndTokenIndexInExpert) == expertRowCount;711 gmm2Position.tokenIndexInExpert == 0U && static_cast<uint64_t>(waveEndTokenIndexInExpert) == expertRowCount;
705- RunMoeExpertGmm2Stage<COMBINE_NO_QUANT, QuantOutType, ActivationType, Weight1Type, QuantScaleOutType, false,712+ RunGmm2ByMode<COMBINE_NO_QUANT, QuantOutType, ActivationType, Weight1Type, QuantScaleOutType, false, false,
706- false, GMM1_TILE_M, TopkWeightsPrefetch, GMM1_INTERLEAVED, true>(713+ GMM1_TILE_M, TopkWeightsPrefetch, false, GMM1_INTERLEAVED, true>(
707 gmmExecutionConfig_, params_, gmm2AddrInfo, gmm2WaveProblemShape, runtimeState, nullptr, isWholeExpert);714 gmmExecutionConfig_, params_, gmm2AddrInfo, gmm2WaveProblemShape, runtimeState, nullptr, isWholeExpert);
708 715 
709 gmm2Position.tokenIndexInExpert = waveEndTokenIndexInExpert;716 gmm2Position.tokenIndexInExpert = waveEndTokenIndexInExpert;
@@ -715,8 +722,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessGmm2Wave
715 }722 }
716}723}
717 724 
718-template <TemplateMegaMoeWaveTypeClass>725+template <TemplateMegaMoeA8W8WaveTypeClass>
719-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessCombineExperts(726+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessCombineExperts(
720 uint32_t expertBegin, uint32_t expertEnd, ExpertLoopState &combineState, GMMAddrInfo &combineAddrInfo,727 uint32_t expertBegin, uint32_t expertEnd, ExpertLoopState &combineState, GMMAddrInfo &combineAddrInfo,
721 const CombineBufferConfig &bufferConfig)728 const CombineBufferConfig &bufferConfig)
722{729{
@@ -745,8 +752,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessCombineE
745 DrainWaveCombineRowRing<CombineQuantMode>(rowSequence);752 DrainWaveCombineRowRing<CombineQuantMode>(rowSequence);
746}753}
747 754 
748-template <TemplateMegaMoeWaveTypeClass>755+template <TemplateMegaMoeA8W8WaveTypeClass>
749-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessMoeExpertStages()756+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessMoeExpertStages()
750{757{
751 tokenDispatchScratch_.expertRevNumsGlobalTensor.SetGlobalBuffer(758 tokenDispatchScratch_.expertRevNumsGlobalTensor.SetGlobalBuffer(
752 reinterpret_cast<__gm__ int32_t *>(params_.workspaceInfo.expertRevTokenNumsPtr));759 reinterpret_cast<__gm__ int32_t *>(params_.workspaceInfo.expertRevTokenNumsPtr));
@@ -762,7 +769,7 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessMoeExper
762 ExpertLoopState combineExpertState = CreateExpertLoopState(commonConfig_);769 ExpertLoopState combineExpertState = CreateExpertLoopState(commonConfig_);
763 int32_t vecSetSyncCom = 0;770 int32_t vecSetSyncCom = 0;
764 int32_t gmm1TileSequence = 0;771 int32_t gmm1TileSequence = 0;
765- Gmm1ActivationState gmm1RuntimeState{startBlockIdx_, vecSetSyncCom, gmm1TileSequence, gmm1PingPongIdx_};772+ GmmRuntimeState gmm1RuntimeState{startBlockIdx_, vecSetSyncCom, gmm1TileSequence, gmm1PingPongIdx_};
766 773 
767 ExpertTokenPosition waveBeginPosition{};774 ExpertTokenPosition waveBeginPosition{};
768 ExpertTokenPosition gmm1Position{};775 ExpertTokenPosition gmm1Position{};
@@ -793,7 +800,7 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessMoeExper
793 const uint32_t gmm1EndBlockIndex = startBlockIdx_;800 const uint32_t gmm1EndBlockIndex = startBlockIdx_;
794 801 
795 int32_t gmm2TileSequence = 0;802 int32_t gmm2TileSequence = 0;
796- Gmm2RuntimeState gmm2RuntimeState{startBlockIdx_, vecSetSyncCom, gmm2TileSequence, gmm2PingPongIdx_};803+ GmmRuntimeState gmm2RuntimeState{startBlockIdx_, vecSetSyncCom, gmm2TileSequence, gmm2PingPongIdx_};
797 ProcessGmm2Wave(gmm2Position, waveEndPosition, gmm2ExpertState, gmm2AddrInfo, gmm2RuntimeState);804 ProcessGmm2Wave(gmm2Position, waveEndPosition, gmm2ExpertState, gmm2AddrInfo, gmm2RuntimeState);
798 805 
799 const bool hasNextWave = waveEndPosition.expertIdx < moeExpertPerRank_;806 const bool hasNextWave = waveEndPosition.expertIdx < moeExpertPerRank_;
@@ -818,8 +825,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessMoeExper
818 expertTokenCountExportScratch_);825 expertTokenCountExportScratch_);
819}826}
820 827 
821-template <TemplateMegaMoeWaveTypeClass>828+template <TemplateMegaMoeA8W8WaveTypeClass>
822-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessGmmPipeline()829+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::ProcessGmmPipeline()
823{830{
824 if (sharedExpertNum_ > 0) {831 if (sharedExpertNum_ > 0) {
825 ProcessSharedExpertGmm1();832 ProcessSharedExpertGmm1();
@@ -841,8 +848,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::ProcessGmmPipel
841 }848 }
842}849}
843 850 
844-template <TemplateMegaMoeWaveTypeClass>851+template <TemplateMegaMoeA8W8WaveTypeClass>
845-__aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::Process()852+__aicore__ inline void MegaMoeA8W8Wave<TemplateMegaMoeA8W8WaveTypeFunc>::Process()
846{853{
847 // 保存入口时的溢出模式,并初始化输入准备阶段使用的 UB。854 // 保存入口时的溢出模式,并初始化输入准备阶段使用的 UB。
848 int64_t oriOverflowMode = GetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>();855 int64_t oriOverflowMode = GetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>();
@@ -878,8 +885,8 @@ __aicore__ inline void MegaMoeWave<TemplateMegaMoeWaveTypeFunc>::Process()
878 SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(oriOverflowMode);885 SetCtrlSpr<OVERFLOW_MODE_CTRL, OVERFLOW_MODE_CTRL>(oriOverflowMode);
879}886}
880 887 
881-#undef TemplateMegaMoeWaveTypeClass888+#undef TemplateMegaMoeA8W8WaveTypeClass
882-#undef TemplateMegaMoeWaveTypeFunc889+#undef TemplateMegaMoeA8W8WaveTypeFunc
883 890 
884} // namespace MegaMoeImpl891} // namespace MegaMoeImpl
885#endif892#endif
@@ -28,20 +28,6 @@ using namespace AscendC;
28constexpr uint32_t MAX_SINGLE_MN_ALIGN32_NUM_256 = 256U * 256U;28constexpr uint32_t MAX_SINGLE_MN_ALIGN32_NUM_256 = 256U * 256U;
29constexpr uint32_t MAX_SINGLE_MN_ALIGN32_NUM_128 = 128U * 256U;29constexpr uint32_t MAX_SINGLE_MN_ALIGN32_NUM_128 = 128U * 256U;
30 30 
31-struct Gmm1ActivationState {
32- uint32_t &startBlockIdx;
33- int32_t &vecSetSyncCom;
34- int32_t &gmTileSequence;
35- uint16_t &pingpongIdx;
36-};
37- 
38-struct SharedExpertGmm1ActivationState {
39- uint32_t &startBlockIdx;
40- int32_t &vecSetSyncCom;
41- int32_t &gmTileSequence;
42- uint16_t &pingpongIdx;
43-};
44- 
45// 等待当前 GMM1 tile 对应的 Dispatch 输入就绪。31// 等待当前 GMM1 tile 对应的 Dispatch 输入就绪。
46template <bool IsShared, typename Config>32template <bool IsShared, typename Config>
47__aicore__ inline void WaitForGmm1InputReady(const GMMAddrInfo &gmmAddrInfo, const Config &config, uint32_t mLoc)33__aicore__ inline void WaitForGmm1InputReady(const GMMAddrInfo &gmmAddrInfo, const Config &config, uint32_t mLoc)
@@ -1044,31 +1030,26 @@ __aicore__ inline void UpdateMoeExpertGmm1GlobalBuffer(
1044 }1030 }
1045}1031}
1046 1032 
1047-template <typename QuantOutType, typename WeightType, typename ActivationOutType, typename QuantScaleType,1033+template <typename QuantOutType, typename ActivationOutType, typename QuantScaleType, uint32_t Gmm1TileM,
1048- bool EnableA8W4, uint32_t Gmm1TileM, uint32_t EpilogueTileM, bool TopkWeightsPrefetch,1034+ uint32_t EpilogueTileM, bool TopkWeightsPrefetch, bool IsGmm1Interleaved = false,
1049- bool IsGmm1Interleaved = false, bool IsWaveFlagGrained = false, typename BlockEpilogue>1035+ bool IsWaveFlagGrained = false, bool IsShared = false, typename BlockEpilogue>
1050-__aicore__ inline void RunGmm1ActivationByMode(const GmmExecutionConfig &gmmConfig, const Params &gmmParams,1036+__aicore__ inline void RunGmm1GenericByWeightFormat(const GmmExecutionConfig &gmmConfig, const Params &gmmParams,
1051- BlockEpilogue &epilogueOp, const GMMAddrInfo &gmmAddrInfo,1037+ BlockEpilogue &epilogueOp, const GMMAddrInfo &gmmAddrInfo,
1052- const ProblemShape &problemShape, uint32_t globalTokenStartIndex,1038+ const ProblemShape &problemShape, uint32_t globalTokenStartIndex,
1053- Gmm1ActivationState &runtimeState, uint32_t expertIdx,1039+ GmmRuntimeState &runtimeState, uint32_t expertIdx,
1054- void *persistentBlockMmadContext = nullptr,1040+ void *persistentBlockMmadContext = nullptr,
1055- bool allowWeightL2Bypass = false)1041+ bool allowWeightL2Bypass = false)
1056{1042{
1057- if constexpr (EnableA8W4) {1043+ if (gmmConfig.groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ ||
1058- RunGmm1A8W4<QuantOutType, WeightType, bfloat16_t, QuantScaleType, QuantScaleType, Gmm1TileM, EpilogueTileM,1044+ gmmConfig.groupedMatmulMode == GROUPED_MATMUL_MODE_A4W4_NZ) {
1059- TopkWeightsPrefetch, false, IsWaveFlagGrained>(
1060- epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.gmTileSequence,
1061- gmmConfig.blockJob, globalTokenStartIndex, expertIdx);
1062- } else if (gmmConfig.groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ ||
1063- gmmConfig.groupedMatmulMode == GROUPED_MATMUL_MODE_A4W4_NZ) {
1064 RunGmm1Generic<QuantOutType, ActivationOutType, QuantOutType, bfloat16_t, QuantScaleType, QuantScaleType, true,1045 RunGmm1Generic<QuantOutType, ActivationOutType, QuantOutType, bfloat16_t, QuantScaleType, QuantScaleType, true,
1065- Gmm1TileM, EpilogueTileM, TopkWeightsPrefetch, false, IsGmm1Interleaved, IsWaveFlagGrained>(1046+ Gmm1TileM, EpilogueTileM, TopkWeightsPrefetch, IsShared, IsGmm1Interleaved, IsWaveFlagGrained>(
1066 epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.vecSetSyncCom,1047 epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.vecSetSyncCom,
1067 gmmConfig.blockJob, globalTokenStartIndex, expertIdx, runtimeState.pingpongIdx, persistentBlockMmadContext,1048 gmmConfig.blockJob, globalTokenStartIndex, expertIdx, runtimeState.pingpongIdx, persistentBlockMmadContext,
1068 allowWeightL2Bypass);1049 allowWeightL2Bypass);
1069 } else {1050 } else {
1070 RunGmm1Generic<QuantOutType, ActivationOutType, QuantOutType, bfloat16_t, QuantScaleType, QuantScaleType, false,1051 RunGmm1Generic<QuantOutType, ActivationOutType, QuantOutType, bfloat16_t, QuantScaleType, QuantScaleType, false,
1071- Gmm1TileM, EpilogueTileM, TopkWeightsPrefetch, false, IsGmm1Interleaved, IsWaveFlagGrained>(1052+ Gmm1TileM, EpilogueTileM, TopkWeightsPrefetch, IsShared, IsGmm1Interleaved, IsWaveFlagGrained>(
1072 epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.vecSetSyncCom,1053 epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.vecSetSyncCom,
1073 gmmConfig.blockJob, globalTokenStartIndex, expertIdx, runtimeState.pingpongIdx, persistentBlockMmadContext,1054 gmmConfig.blockJob, globalTokenStartIndex, expertIdx, runtimeState.pingpongIdx, persistentBlockMmadContext,
1074 allowWeightL2Bypass);1055 allowWeightL2Bypass);
@@ -1131,11 +1112,13 @@ __aicore__ inline void UpdateSharedExpertGmm1GlobalBuffer(const MoeStageCommonCo
1131template <typename QuantOutType, typename WeightType, typename ActivationOutType, typename QuantScaleType,1112template <typename QuantOutType, typename WeightType, typename ActivationOutType, typename QuantScaleType,
1132 bool EnableA8W4, uint32_t Gmm1TileM, bool IsGmm1Interleaved = false, bool IsWaveFlagGrained = false,1113 bool EnableA8W4, uint32_t Gmm1TileM, bool IsGmm1Interleaved = false, bool IsWaveFlagGrained = false,
1133 typename BlockEpilogue>1114 typename BlockEpilogue>
1134-__aicore__ inline void RunSharedExpertGmm1ActivationStage(1115+__aicore__ inline void RunSharedExpertGmm1ActivationStage(const MoeStageCommonConfig &commonConfig,
1135- const MoeStageCommonConfig &commonConfig, const GmmExecutionConfig &gmmConfig, const Params &gmmParams,1116+ const GmmExecutionConfig &gmmConfig, const Params &gmmParams,
1136- BlockEpilogue &epilogueOp, const GMMAddrInfo &gmmAddrInfo, const ProblemShape &problemShape,1117+ BlockEpilogue &epilogueOp, const GMMAddrInfo &gmmAddrInfo,
1137- SharedExpertGmm1ActivationState &runtimeState, uint32_t sharedExpertIdx, void *persistentBlockMmadContext = nullptr,1118+ const ProblemShape &problemShape,
1138- bool allowWeightL2Bypass = false)1119+ GmmRuntimeState &runtimeState, uint32_t sharedExpertIdx,
1120+ void *persistentBlockMmadContext = nullptr,
1121+ bool allowWeightL2Bypass = false)
1139{1122{
1140 uint32_t expertBeforeCnt = sharedExpertIdx * commonConfig.tokenNum;1123 uint32_t expertBeforeCnt = sharedExpertIdx * commonConfig.tokenNum;
1141 if constexpr (EnableA8W4) {1124 if constexpr (EnableA8W4) {
@@ -1143,43 +1126,11 @@ __aicore__ inline void RunSharedExpertGmm1ActivationStage(
1143 false, true, IsWaveFlagGrained>(epilogueOp, gmmParams, problemShape, gmmAddrInfo,1126 false, true, IsWaveFlagGrained>(epilogueOp, gmmParams, problemShape, gmmAddrInfo,
1144 runtimeState.startBlockIdx, runtimeState.gmTileSequence,1127 runtimeState.startBlockIdx, runtimeState.gmTileSequence,
1145 gmmConfig.blockJob, expertBeforeCnt, sharedExpertIdx);1128 gmmConfig.blockJob, expertBeforeCnt, sharedExpertIdx);
1146- } else if (gmmConfig.groupedMatmulMode == GROUPED_MATMUL_MODE_A8W8_NZ ||
1147- gmmConfig.groupedMatmulMode == GROUPED_MATMUL_MODE_A4W4_NZ) {
1148- RunGmm1Generic<QuantOutType, ActivationOutType, QuantOutType, bfloat16_t, QuantScaleType, QuantScaleType, true,
1149- Gmm1TileM, L1_TILE_M_256, false, true, IsGmm1Interleaved, IsWaveFlagGrained>(
1150- epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.vecSetSyncCom,
1151- gmmConfig.blockJob, expertBeforeCnt, sharedExpertIdx, runtimeState.pingpongIdx, persistentBlockMmadContext,
1152- allowWeightL2Bypass);
1153 } else {1129 } else {
1154- RunGmm1Generic<QuantOutType, ActivationOutType, QuantOutType, bfloat16_t, QuantScaleType, QuantScaleType, false,1130+ RunGmm1GenericByWeightFormat<QuantOutType, ActivationOutType, QuantScaleType, Gmm1TileM, L1_TILE_M_256, false,
1155- Gmm1TileM, L1_TILE_M_256, false, true, IsGmm1Interleaved, IsWaveFlagGrained>(1131+ IsGmm1Interleaved, IsWaveFlagGrained, true>(
1156- epilogueOp, gmmParams, problemShape, gmmAddrInfo, runtimeState.startBlockIdx, runtimeState.vecSetSyncCom,1132+ gmmConfig, gmmParams, epilogueOp, gmmAddrInfo, problemShape, expertBeforeCnt, runtimeState, sharedExpertIdx,
1157- gmmConfig.blockJob, expertBeforeCnt, sharedExpertIdx, runtimeState.pingpongIdx, persistentBlockMmadContext,1133+ persistentBlockMmadContext, allowWeightL2Bypass);
1158- allowWeightL2Bypass);
1159- }
1160-}
1161- 
1162-// 完成 MoE GMM1/SwiGLU 阶段的末尾同步。
1163-template <bool EnableA8W4, bool TopkWeightsPrefetch>
1164-__aicore__ inline void FinishMoeGmm1ActivationStage(const MoeStageCommonConfig &commonConfig,
1165- const MoeSyncWorkspaceLayout &syncLayout,
1166- const WorkspaceInfo &workspace, Gmm1ActivationState &runtimeState)
1167-{
1168- if constexpr (TopkWeightsPrefetch) {
1169- int32_t allDoneTag = static_cast<int32_t>(commonConfig.moeExpertPerRank + 1U);
1170- __gm__ int32_t *allDoneAddr = reinterpret_cast<__gm__ int32_t *>(workspace.gmm1TileStatusPtr) +
1171- static_cast<uint64_t>(commonConfig.moeExpertPerRank) *
1172- syncLayout.gmm1TileStatusCountPerExpert * INT_CACHELINE;
1173- if constexpr (g_coreType == AIV) {
1174- constexpr uint32_t epilogueSubBlockIdx = EnableA8W4 ? 1U : 0U;
1175- if (GetSubBlockIdx() == epilogueSubBlockIdx) {
1176- AscendC::WriteGmByPassDCache(allDoneAddr, allDoneTag);
1177- }
1178- } else {
1179- WaitUntilGmFlagEquals(allDoneAddr, allDoneTag);
1180- }
1181- } else {
1182- EndSync(runtimeState.vecSetSyncCom);
1183 }1134 }
1184}1135}
1185 1136 
@@ -239,13 +239,6 @@ __aicore__ inline void CombineTokenGroup(uint32_t tokenStart, uint32_t tokenCoun
239constexpr uint32_t META_INFO_TENSOR_ADDR = 200U * 1024U;239constexpr uint32_t META_INFO_TENSOR_ADDR = 200U * 1024U;
240constexpr int32_t MAX_AICORE_NUM = 36;240constexpr int32_t MAX_AICORE_NUM = 36;
241 241 
242-struct Gmm2RuntimeState {
243- uint32_t &startBlockIdx;
244- int32_t &vecSetSyncCom;
245- int32_t &gmTileSequence;
246- uint16_t &pingpongIdx;
247-};
248- 
249struct QuantCombineBufferConfig {242struct QuantCombineBufferConfig {
250 int64_t combineUbElementCount;243 int64_t combineUbElementCount;
251 uint32_t quantTokenSizeBytes;244 uint32_t quantTokenSizeBytes;
@@ -389,7 +382,7 @@ __aicore__ inline void SendWaveCombineToken(const MoeStageCommonConfig &common,
389 SetFlag<HardEvent::MTE3_MTE2>(eventId);382 SetFlag<HardEvent::MTE3_MTE2>(eventId);
390}383}
391 384 
392-// 原型:MegaMoeWave::ProcessCombineGm。发送一个逻辑 AIV 任务负责的已就绪 token 范围。385+// 使用行级流水发送当前逻辑 AIV 任务负责的已就绪 token 区间。
393template <uint8_t CombineMode>386template <uint8_t CombineMode>
394__aicore__ inline void CombineWaveTokenRange(const MoeStageCommonConfig &common,387__aicore__ inline void CombineWaveTokenRange(const MoeStageCommonConfig &common,
395 const WaveCombineBufferConfig &bufferConfig, WaveCombineScratch &scratch,388 const WaveCombineBufferConfig &bufferConfig, WaveCombineScratch &scratch,
@@ -982,7 +975,7 @@ template <uint8_t CombineMode, typename GenericElementA, typename A8W4ElementA,
982 bool IsShared, bool IsGmm1Interleaved = false, bool IsWaveFlagGrained = false>975 bool IsShared, bool IsGmm1Interleaved = false, bool IsWaveFlagGrained = false>
983__aicore__ inline void RunGmm2ByMode(const GmmExecutionConfig &gmmConfig, const Params &params,976__aicore__ inline void RunGmm2ByMode(const GmmExecutionConfig &gmmConfig, const Params &params,
984 const GMMAddrInfo &gmmAddrInfo, const ProblemShape &problemShape,977 const GMMAddrInfo &gmmAddrInfo, const ProblemShape &problemShape,
985- Gmm2RuntimeState &runtimeState, void *persistentBlockMmadContext = nullptr,978+ GmmRuntimeState &runtimeState, void *persistentBlockMmadContext = nullptr,
986 bool allowWeightL2Bypass = false)979 bool allowWeightL2Bypass = false)
987{980{
988 if constexpr (EnableA8W4 || EnableA4W4) {981 if constexpr (EnableA8W4 || EnableA4W4) {
@@ -1004,44 +997,6 @@ __aicore__ inline void RunGmm2ByMode(const GmmExecutionConfig &gmmConfig, const
1004 }997 }
1005}998}
1006 999 
1007-template <uint8_t CombineMode, typename GenericElementA, typename A8W4ElementA, typename WeightType,
1008- typename QuantScaleType, bool EnableA8W4, bool EnableA4W4, uint32_t Gmm1TileM, bool TopkWeightsPrefetch,
1009- bool IsGmm1Interleaved, bool IsWaveFlagGrained>
1010-__aicore__ inline void RunMoeExpertGmm2Stage(const GmmExecutionConfig &gmmConfig, const Params &params,
1011- const GMMAddrInfo &gmmAddrInfo, const ProblemShape &problemShape,
1012- Gmm2RuntimeState &runtimeState, void *persistentBlockMmadContext = nullptr,
1013- bool allowWeightL2Bypass = false)
1014-{
1015- RunGmm2ByMode<CombineMode, GenericElementA, A8W4ElementA, WeightType, QuantScaleType, EnableA8W4, EnableA4W4,
1016- Gmm1TileM, TopkWeightsPrefetch, false, IsGmm1Interleaved, IsWaveFlagGrained>(
1017- gmmConfig, params, gmmAddrInfo, problemShape, runtimeState, persistentBlockMmadContext, allowWeightL2Bypass);
1018-}
1019- 
1020-template <typename GenericElementA, typename A8W4ElementA, typename WeightType, typename QuantScaleType,
1021- bool EnableA8W4, bool EnableA4W4, uint32_t Gmm1TileM, bool TopkWeightsPrefetch, bool IsGmm1Interleaved,
1022- bool IsWaveFlagGrained>
1023-__aicore__ inline bool RunSharedExpertGmm2Stage(const MoeStageCommonConfig &commonConfig,
1024- const GmmExecutionConfig &gmmConfig, const Params &params,
1025- const ExpertWeightTensorListAddrs &weights, GMMAddrInfo &gmmAddrInfo,
1026- Gmm2RuntimeState &runtimeState, uint32_t sharedExpertIdx,
1027- void *persistentBlockMmadContext = nullptr,
1028- bool allowWeightL2Bypass = false)
1029-{
1030- if (commonConfig.tokenNum == 0U) {
1031- return false;
1032- }
1033- ProblemShape problemShape;
1034- Get<M_VALUE>(problemShape) = commonConfig.tokenNum;
1035- Get<N_VALUE>(problemShape) = commonConfig.gmm1OutputDim;
1036- Get<K_VALUE>(problemShape) = commonConfig.tokenHiddenDim;
1037- UpdateSharedExpertGmm2GlobalBuffer<A8W4ElementA, WeightType, QuantScaleType, Gmm1TileM, EnableA8W4 || EnableA4W4>(
1038- commonConfig, gmmConfig, params.workspaceInfo, weights, gmmAddrInfo, sharedExpertIdx);
1039- RunGmm2ByMode<COMBINE_NO_QUANT, GenericElementA, A8W4ElementA, WeightType, QuantScaleType, EnableA8W4, EnableA4W4,
1040- Gmm1TileM, TopkWeightsPrefetch, true, IsGmm1Interleaved, IsWaveFlagGrained>(
1041- gmmConfig, params, gmmAddrInfo, problemShape, runtimeState, persistentBlockMmadContext, allowWeightL2Bypass);
1042- return true;
1043-}
1044- 
1045// 调度当前 MoE 专家指定 token 范围的量化 Combine。1000// 调度当前 MoE 专家指定 token 范围的量化 Combine。
1046template <uint8_t CombineMode>1001template <uint8_t CombineMode>
1047__aicore__ inline void ScheduleQuantizedMoeExpertCombine(1002__aicore__ inline void ScheduleQuantizedMoeExpertCombine(
@@ -569,9 +569,6 @@ template <typename ActivationType, bool ReloadCumsumFromGm>
569__aicore__ inline void ExportExpertTokenCounts(const MoeStageCommonConfig &common,569__aicore__ inline void ExportExpertTokenCounts(const MoeStageCommonConfig &common,
570 TokenDispatchScratch<ActivationType> &scratch, const Params &params)570 TokenDispatchScratch<ActivationType> &scratch, const Params &params)
571{571{
572- if constexpr (g_coreType == AIC) {
573- return;
574- }
575 if constexpr (ReloadCumsumFromGm) {572 if constexpr (ReloadCumsumFromGm) {
576 DataCopyPad(573 DataCopyPad(
577 scratch.cumsumInfoTensor, scratch.cumsumInfoGlobalTensor,574 scratch.cumsumInfoTensor, scratch.cumsumInfoGlobalTensor,