38 template <
typename GridwiseGemm,
 
   39           bool HasMainKBlockLoop,
 
   44 #if CK_USE_LAUNCH_BOUNDS 
   50 #if(!defined(__HIP_DEVICE_COMPILE__) || defined(__gfx9__)) 
   51     __shared__ 
char p_shared[GridwiseGemm::GetSharedMemoryNumberOfByte()];
 
   53     auto splitk_batch_offset = 
typename GridwiseGemm::SplitKBatchOffset(karg, blockIdx.z);
 
   55     GridwiseGemm::template Run<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
 
   56         karg.p_sorted_token_ids,
 
   57         karg.p_sorted_expert_ids,
 
   59         karg.p_a_grid + splitk_batch_offset.a_k_split_offset,
 
   60         karg.p_a_scale_grid + splitk_batch_offset.a_k_split_offset,
 
   61         karg.p_b_grid + splitk_batch_offset.b_k_split_offset,
 
   62         karg.p_b_scale_grid + splitk_batch_offset.b_k_split_offset,
 
   76 template <
typename GridwiseGemm,
 
   77           bool HasMainKBlockLoop,
 
   82 #if CK_USE_LAUNCH_BOUNDS 
   88 #if(!defined(__HIP_DEVICE_COMPILE__) || defined(__gfx9__)) 
   89     __shared__ 
char p_shared[GridwiseGemm::GetSharedMemoryNumberOfByte()];
 
   90     __shared__ 
char p_shared1[GridwiseGemm::GetSharedMemoryNumberOfByte()];
 
   94     GridwiseGemm::template Run_2Lds<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
 
   95         karg.p_sorted_token_ids,
 
   96         karg.p_sorted_expert_ids,
 
  116 template <
typename ALayout,
 
  121           typename AScaleDataType,
 
  123           typename BScaleDataType,
 
  124           typename AccDataType,
 
  125           typename CShuffleDataType,
 
  128           typename AElementwiseOperation,
 
  129           typename BElementwiseOperation,
 
  130           typename CElementwiseOperation,
 
  143           typename ABlockTransferThreadClusterLengths_AK0_M_AK1,
 
  144           typename ABlockTransferThreadClusterArrangeOrder,
 
  145           typename ABlockTransferSrcAccessOrder,
 
  146           index_t ABlockTransferSrcVectorDim,
 
  147           index_t ABlockTransferSrcScalarPerVector,
 
  148           index_t ABlockTransferDstScalarPerVector_AK1,
 
  149           bool AThreadTransferSrcResetCoordinateAfterRun,
 
  151           typename BBlockTransferThreadClusterLengths_BK0_N_BK1,
 
  152           typename BBlockTransferThreadClusterArrangeOrder,
 
  153           typename BBlockTransferSrcAccessOrder,
 
  154           index_t BBlockTransferSrcVectorDim,
 
  155           index_t BBlockTransferSrcScalarPerVector,
 
  156           index_t BBlockTransferDstScalarPerVector_BK1,
 
  157           bool BThreadTransferSrcResetCoordinateAfterRun,
 
  159           index_t CShuffleMXdlPerWavePerShuffle,
 
  160           index_t CShuffleNXdlPerWavePerShuffle,
 
  161           typename CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock,
 
  162           typename CDEShuffleBlockTransferScalarPerVectors,
 
  165           index_t ActivationOperation                 = 0,
 
  166           bool NSwizzle                               = 
false,
 
  167           bool IsInputGemm                            = 
true,
 
  168           bool MulRoutedWeight                        = 
true,
 
  170           typename ComputeTypeA                       = ADataType,
 
  171           typename ComputeTypeB                       = BDataType>
 
  189         CDEShuffleBlockTransferScalarPerVectors{}[
I0];
 
  225                 return static_cast<const DDataType*
>(
nullptr);
 
  238         const index_t gridx  = NSwizzle ? nblock * mblock : nblock;
 
  239         const index_t gridy  = NSwizzle ? 1 : mblock;
 
  261         auto K_t = K_Batch * KPerBlock;
 
  262         return (K + K_t - 1) / K_t * (KPerBlock / AK1Value);
 
  267         auto K_t = K_Batch * KPerBlock;
 
  268         return (K + K_t - 1) / K_t * (KPerBlock / BK1Value);
 
  273         auto K_t = K_Batch * KPerBlock;
 
  274         return (K + K_t - 1) / K_t * KPerBlock;
 
  280         auto K_t                = K_Batch * KReadVec;
 
  281         return (K + K_t - 1) / K_t * KReadVec;
 
  294     template <
index_t MNXdlPerWave,
 
  298               typename TileDesc_K0_MN_K1>
 
  316         IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
 
  318         const auto a_grid_desc_mraw_kraw = [&]() {
 
  319             if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
 
  323             else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
 
  331         if constexpr(GemmSpec == GemmSpecialization::MKPadding ||
 
  332                      GemmSpec == GemmSpecialization::MNKPadding)
 
  335             const auto a_grid_desc_m_k =
 
  349             return a_grid_desc_ak0_m_ak1;
 
  351         else if constexpr(GemmSpec == GemmSpecialization::MPadding ||
 
  352                           GemmSpec == GemmSpecialization::MNPadding)
 
  356                 a_grid_desc_mraw_kraw,
 
  362             return a_grid_desc_ak0_m_ak1;
 
  364         else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
 
  365                           GemmSpec == GemmSpecialization::NKPadding)
 
  369                 a_grid_desc_mraw_kraw,
 
  381             return a_grid_desc_ak0_m_ak1;
 
  387                 a_grid_desc_mraw_kraw,
 
  393             return a_grid_desc_ak0_m_ak1;
 
  400         const auto b_grid_desc_nraw_kraw = [&]() {
 
  414                         GemmSpec != GemmSpecialization::Default),
 
  415                       "pk_i4_t does not support padding");
 
  417                         GemmSpec != GemmSpecialization::Default),
 
  418                       "f4x2_pk_t does not support padding");
 
  420         if constexpr(GemmSpec == GemmSpecialization::NKPadding ||
 
  421                      GemmSpec == GemmSpecialization::MNKPadding)
 
  424             const auto b_grid_desc_n_k =
 
  438             return b_grid_desc_bk0_n_bk1;
 
  440         else if constexpr(GemmSpec == GemmSpecialization::NPadding ||
 
  441                           GemmSpec == GemmSpecialization::MNPadding)
 
  445                 b_grid_desc_nraw_kraw,
 
  451             return b_grid_desc_bk0_n_bk1;
 
  453         else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
 
  454                           GemmSpec == GemmSpecialization::MKPadding)
 
  458                 b_grid_desc_nraw_kraw,
 
  470             return b_grid_desc_bk0_n_bk1;
 
  476                 b_grid_desc_nraw_kraw,
 
  482             return b_grid_desc_bk0_n_bk1;
 
  486     template <
typename ABlockDesc_AK0_M_AK1>
 
  487     __host__ __device__ 
static constexpr 
auto 
  490         constexpr 
index_t MWaves = MPerBlock / (MXdlPerWave * MPerXdl);
 
  492         return MakeGemmMmaTileDescriptor<MXdlPerWave, MWaves, MXdlPack, MPerXdl>(
 
  493             ABlockDesc_AK0_M_AK1{});
 
  496     template <
typename BBlockDesc_BK0_N_BK1>
 
  497     __host__ __device__ 
static constexpr 
auto 
  500         constexpr 
index_t NWaves = NPerBlock / (NXdlPerWave * NPerXdl);
 
  502         return MakeGemmMmaTileDescriptor<NXdlPerWave, NWaves, NXdlPack, NPerXdl>(
 
  503             BBlockDesc_BK0_N_BK1{});
 
  506     template <
typename ELayout>
 
  508         IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
 
  510         const auto c_grid_desc_mraw_nraw = [&]() {
 
  529     template <
typename DLayout>
 
  530     __host__ __device__ 
static auto 
  533         const auto c_grid_desc_mraw_nraw = [&]() {
 
  558                 return MakeDGridDescriptor_M_N<DLayout>(M, MPad, N, NPad, StrideDs[i]);
 
  563     template <
typename DsGr
idDesc>
 
  565         const DsGridDesc& ds_grid_desc_m_n, 
index_t MBlock, 
index_t NBlock)
 
  570                     ds_grid_desc_m_n[i], MBlock, NBlock);
 
  586                          std::array<index_t, NumDTensor> StrideDs_,
 
  614             std::cout << 
"problem {" 
  616                       << 
"TopK:" << 
TopK << 
", " 
  627                       << 
"KRead:" << 
KRead << 
", " 
  629                       << 
"AK0:" << 
AK0 << 
", " 
  630                       << 
"BK0:" << 
BK0 << 
", " 
  631                       << 
"MBlock: " << 
MBlock << 
", " 
  632                       << 
"NBlock: " << 
NBlock << 
"}" << std::endl;
 
  661                           const index_t* p_sorted_expert_ids_,
 
  662                           const index_t* p_max_token_id_,
 
  663                           const ADataType* p_a_grid_,
 
  664                           const AScaleDataType* p_a_scale_grid_,
 
  665                           const BDataType* p_b_grid_,
 
  666                           const BScaleDataType* p_b_scale_grid_,
 
  667                           std::array<const void*, NumDTensor> p_ds_grid_,
 
  668                           CDataType* p_c_grid_,
 
  678                           std::array<index_t, NumDTensor> StrideDs_,
 
  681                           AElementwiseOperation a_element_op_,
 
  682                           BElementwiseOperation b_element_op_,
 
  683                           CElementwiseOperation c_element_op_)
 
  715                 p_ds_grid(i) = 
static_cast<const DDataType_*
>(p_ds_grid_[i]);
 
  738             if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
 
  742             else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
 
  747             if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
 
  751             else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
 
  758             if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
 
  762             else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
 
  769             if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
 
  774             else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
 
  779             if(k_id < karg.
KBatch - 1)
 
  808             constexpr 
auto a_lds_block_desc =
 
  820             return a_lds_block_desc_permuted;
 
  827             constexpr 
auto WaveSize = 64;
 
  828             constexpr 
auto M0       = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
 
  829             constexpr 
auto M1       = MPerBlock / M0;
 
  831             constexpr 
auto KThreadWrite     = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
 
  832             constexpr 
auto K0PerThreadWrite = 
AK0Number / KThreadWrite;
 
  833             constexpr 
auto KThreadRead      = WaveSize / MPerXdl;
 
  834             constexpr 
auto K0PerThreadRead  = 
AK0Number / KThreadRead;
 
  836             constexpr 
auto kfold = (
AK1Number * M0 * 
sizeof(ADataType) > 128)
 
  838                                        : 128 / (
AK1Number * M0 * 
sizeof(ADataType));
 
  839             constexpr 
auto KThreadReadPerm =
 
  840                 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
 
  841                     ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
 
  845             constexpr 
auto mpair = (
AK1Number * MPerXdl * 
sizeof(ADataType) > 128)
 
  847                                        : ((128 / (
AK1Number * MPerXdl * 
sizeof(ADataType))) > M0
 
  849                                               : 128 / (
AK1Number * MPerXdl * 
sizeof(ADataType)));
 
  855                            Number<kfold * M0 / mpair>{},
 
  874                 a_lds_block_desc_permuted,
 
  896                 a_lds_block_desc_unmerged,
 
  899                                           Number<KThreadWrite / kfold / KThreadReadPerm>{},
 
  908             return a_lds_block_desc_ak0_m_ak1;
 
  924             constexpr 
auto b_lds_block_desc =
 
  936             return b_lds_block_desc_permuted;
 
  940             constexpr 
auto WaveSize = 64;
 
  941             constexpr 
auto N0       = BBlockTransferThreadClusterLengths_BK0_N_BK1{}.At(
I1);
 
  942             constexpr 
auto N1       = NPerBlock / N0;
 
  944             constexpr 
auto KThreadWrite     = BBlockTransferThreadClusterLengths_BK0_N_BK1{}.At(
I0);
 
  945             constexpr 
auto K0PerThreadWrite = 
BK0Number / KThreadWrite;
 
  946             constexpr 
auto KThreadRead      = WaveSize / NPerXdl;
 
  947             constexpr 
auto K0PerThreadRead  = 
BK0Number / KThreadRead;
 
  949             constexpr 
auto kfold = (
BK1Number * N0 * 
sizeof(BDataType) > 128)
 
  951                                        : 128 / (
BK1Number * N0 * 
sizeof(BDataType));
 
  952             constexpr 
auto KThreadReadPerm =
 
  953                 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
 
  954                     ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
 
  958             constexpr 
auto npair = (
BK1Number * NPerXdl * 
sizeof(BDataType) > 128)
 
  960                                        : ((128 / (
BK1Number * NPerXdl * 
sizeof(BDataType))) > N0
 
  962                                               : 128 / (
BK1Number * NPerXdl * 
sizeof(BDataType)));
 
  968                            Number<kfold * N0 / npair>{},
 
  987                 b_lds_block_desc_permuted,
 
 1009                 b_lds_block_desc_unmerged,
 
 1012                                           Number<KThreadWrite / kfold / KThreadReadPerm>{},
 
 1021             return b_lds_block_desc_bk0_n_bk1;
 
 1027         constexpr 
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
 
 1028         constexpr 
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
 
 1030         constexpr 
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
 
 1037         return c_shuffle_block_desc_mblock_mperblock_nblock_nperblock;
 
 1058                                 ABlockTransferSrcScalarPerVector,
 
 1059                                 BBlockTransferSrcScalarPerVector,
 
 1080             a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
 
 1083             b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
 
 1086         constexpr 
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
 
 1089         constexpr 
auto c_block_size =
 
 1090             c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize();
 
 1092         if constexpr(IsInputGemm)
 
 1094             return math::max((a_block_space_size_aligned * 
sizeof(ADataType) +
 
 1095                               b_block_space_size_aligned * 
sizeof(BDataType)) *
 
 1097                              c_block_size * 
sizeof(CShuffleDataType));
 
 1101             return math::max((a_block_space_size_aligned * 
sizeof(ADataType) +
 
 1102                               b_block_space_size_aligned * 
sizeof(BDataType)),
 
 1103                              c_block_size * 
sizeof(CShuffleDataType));
 
 1110         static_assert((MPerBlock % (MPerXdl * MXdlPerWave) == 0) &&
 
 1111                           (NPerBlock % (NXdlPerWave * NPerXdl)) == 0,
 
 1112                       "Invalid tuning param!");
 
 1114         static_assert(KPerBlock % (ScaleBlockSize / 
BPackedSize) == 0,
 
 1115                       "KPerBlock should be multiple of ScaleBlockSize");
 
 1123             if(!(karg.
M % MPerBlock == 0))
 
 1127                     std::cout << 
"Arg M value is not a multiple of MPerBlock! M: " << karg.
M << 
" " 
 1128                               << __FILE__ << 
":" << __LINE__ << 
", in function: " << __func__
 
 1141             if(!(karg.
N % NPerBlock == 0))
 
 1145                     std::cout << 
"Arg N value is not a multiple of NPerBlock! N: " << karg.
N << 
" " 
 1146                               << __FILE__ << 
":" << __LINE__ << 
", in function: " << __func__
 
 1158             auto K_t = karg.
KBatch * KPerBlock;
 
 1159             if(!(karg.
K % K_t == 0))
 
 1163                     std::cout << 
"Arg K value is not a multiple of K_Batch * K0PerBlock * K1! K: " 
 1164                               << karg.
K << 
" " << __FILE__ << 
":" << __LINE__
 
 1165                               << 
", in function: " << __func__ << std::endl;
 
 1173             auto K_t                = karg.
KBatch * KReadVec;
 
 1175             if((KReadPadSplited * (karg.
KBatch - 1)) >= karg.
K)
 
 1183             if(karg.
K % ABlockTransferSrcScalarPerVector != 0)
 
 1187                     std::cout << 
"Arg K (" << karg.
K 
 1188                               << 
") value is not a multiple of ABlockTransferSrcScalarPerVector (" 
 1189                               << ABlockTransferSrcScalarPerVector << 
" )! " << __FILE__ << 
":" 
 1190                               << __LINE__ << 
", in function: " << __func__ << std::endl;
 
 1197             if(karg.
M % ABlockTransferSrcScalarPerVector != 0)
 
 1201                     std::cout << 
"Arg M (" << karg.
M 
 1202                               << 
") value is not a multiple of ABlockTransferSrcScalarPerVector (" 
 1203                               << ABlockTransferSrcScalarPerVector << 
" )! " << __FILE__ << 
":" 
 1204                               << __LINE__ << 
", in function: " << __func__ << std::endl;
 
 1212             if(karg.
N % BBlockTransferSrcScalarPerVector != 0)
 
 1216                     std::cout << 
"Arg N (" << karg.
N 
 1217                               << 
") value is not a multiple of BBlockTransferSrcScalarPerVector (" 
 1218                               << BBlockTransferSrcScalarPerVector << 
" )! " << __FILE__ << 
":" 
 1219                               << __LINE__ << 
", in function: " << __func__ << std::endl;
 
 1226             if(karg.
K % BBlockTransferSrcScalarPerVector != 0)
 
 1230                     std::cout << 
"Arg K (" << karg.
K 
 1231                               << 
") value is not a multiple of BBlockTransferSrcScalarPerVector (" 
 1232                               << BBlockTransferSrcScalarPerVector << 
" )! " << __FILE__ << 
":" 
 1233                               << __LINE__ << 
", in function: " << __func__ << std::endl;
 
 1245                     std::cout << 
"Arg N (" << karg.
N 
 1246                               << 
") value is not a multiple of " 
 1247                                  "CShuffleBlockTransferScalarPerVector_NPerBlock (" 
 1249                               << __FILE__ << 
":" << __LINE__ << 
", in function: " << __func__
 
 1261                     std::cout << 
"Arg M (" << karg.
M 
 1262                               << 
") value is not a multiple of " 
 1263                                  "CShuffleBlockTransferScalarPerVector_NPerBlock (" 
 1265                               << __FILE__ << 
":" << __LINE__ << 
", in function: " << __func__
 
 1275         const auto num_k_loop = karg.
AK0 / (KPerBlock / AK1Value);
 
 1277         if(num_k_loop <= BlockwiseGemmPipe::PrefetchStages)
 
 1288         const index_t num_loop = K / KPerBlock;
 
 1290         return BlockwiseGemmPipe::BlockHasHotloop(num_loop);
 
 1295         const index_t num_loop = K / KPerBlock;
 
 1297         return BlockwiseGemmPipe::BlockLoopTailNum(num_loop);
 
 1300     template <
typename CGr
idDesc>
 
 1302         const CGridDesc& c_grid_desc_m_n, 
index_t MBlock, 
index_t NBlock)
 
 1311         return c_grid_desc_mblock_mperblock_nblock_nperblock;
 
 1323                   "A scale pack data type too large!");
 
 1325                   "B scale pack data type too large!");
 
 1327     template <
bool HasMainKBlockLoop,
 
 1331                                const index_t* p_sorted_expert_ids,
 
 1332                                const index_t* p_max_token_id,
 
 1333                                const ADataType* p_a_grid,
 
 1334                                const AScaleDataType* p_a_scale_grid,
 
 1335                                const BDataType* p_b_grid,
 
 1336                                const BScaleDataType* p_b_scale_grid,
 
 1338                                CDataType* p_c_grid,
 
 1341                                AElementwiseOperation a_element_op,
 
 1342                                BElementwiseOperation b_element_op,
 
 1343                                CElementwiseOperation c_element_op)
 
 1355         const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
 
 1374         const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
 
 1378         const index_t max_token_id    = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
 
 1379         const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
 
 1380         if(expert_block_id * MPerBlock >= max_token_id)
 
 1383             __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
 
 1385         const auto block_mn = [&]() -> std::pair<int, int> {
 
 1386             if constexpr(NSwizzle)
 
 1388                 const index_t ecnt_prefix  = p_max_token_id[1 + expert_id];
 
 1390                 const index_t ecnt         = p_max_token_id[2 + expert_id] - ecnt_prefix;
 
 1391                 const index_t expert_swizzle =
 
 1392                     ecnt > 0 ? ecnt : 1; 
 
 1393                 const index_t bid_new = blockIdx.x - prefix_block;
 
 1394                 const index_t nid     = __builtin_amdgcn_readfirstlane(
 
 1395                     bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
 
 1397                     __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
 
 1402                 return {blockIdx.x, blockIdx.y};
 
 1406         const index_t block_n_id = block_mn.first;
 
 1407         const index_t block_m_id = block_mn.second;
 
 1409             __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
 
 1412         constexpr 
auto AMThreads  = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
 
 1413         constexpr 
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
 
 1414         constexpr 
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
 
 1415         constexpr 
auto AKThreads  = AK0Threads * AK1Threads;
 
 1416         constexpr 
auto AMRepeats  = MPerBlock / AMThreads;
 
 1417         const index_t token_pos   = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
 
 1419         if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
 
 1423             const index_t fused_token = p_sorted_token_ids[token_pos + m0];
 
 1424             index_t token_offset      = fused_token & 0xffffff;
 
 1425             if constexpr(!IsInputGemm)
 
 1427                 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
 
 1429             gather_offsets(m0) = 
static_cast<IndexType
>(token_offset) * problem.
K;
 
 1433             __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
 
 1434         const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
 
 1435             problem.
N * (IsInputGemm ? 2 : 1) *
 
 1439         const index_t n_block_data_idx_on_grid =
 
 1440             __builtin_amdgcn_readfirstlane(block_n_id * NPerBlock);
 
 1443         const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1444             p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
 
 1445         const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1446             p_b_grid + expert_id * expert_stride, b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
 
 1449         const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1450             p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
 
 1451         const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1452             p_b_scale_grid + (expert_id * expert_scale_stride) / 
sizeof(BScaleDataType),
 
 1453             b_scale_grid_desc_bn_ak.GetElementSpaceSize());
 
 1467             AElementwiseOperation,
 
 1471             ABlockTransferThreadClusterLengths_AK0_M_AK1,
 
 1472             ABlockTransferThreadClusterArrangeOrder,
 
 1475             decltype(a_grid_desc_ak0_m_ak1),
 
 1476             decltype(a_block_desc_ak0_m_ak1),
 
 1477             ABlockTransferSrcAccessOrder,
 
 1479             ABlockTransferSrcVectorDim,
 
 1481             ABlockTransferSrcScalarPerVector,
 
 1482             ABlockTransferDstScalarPerVector_AK1,
 
 1485             AThreadTransferSrcResetCoordinateAfterRun,
 
 1489             BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
 
 1492                                                 a_block_desc_ak0_m_ak1,
 
 1498         auto b_blockwise_copy =
 
 1500                                                 BElementwiseOperation,
 
 1504                                                 BBlockTransferThreadClusterLengths_BK0_N_BK1,
 
 1505                                                 BBlockTransferThreadClusterArrangeOrder,
 
 1508                                                 decltype(b_grid_desc_bk0_n_bk1),
 
 1509                                                 decltype(b_block_desc_bk0_n_bk1),
 
 1510                                                 BBlockTransferSrcAccessOrder,
 
 1512                                                 BBlockTransferSrcVectorDim,
 
 1514                                                 BBlockTransferSrcScalarPerVector,
 
 1515                                                 BBlockTransferDstScalarPerVector_BK1,
 
 1518                                                 BThreadTransferSrcResetCoordinateAfterRun,
 
 1520                                                 BlockwiseGemmPipe::GlobalBufferNum>(
 
 1521                 b_grid_desc_bk0_n_bk1,
 
 1524                 b_block_desc_bk0_n_bk1,
 
 1530             a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
 
 1533         auto a_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 1534             static_cast<ADataType*
>(p_shared), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
 
 1536         auto b_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 1537             reinterpret_cast<BDataType*
>(
static_cast<char*
>(p_shared) +
 
 1538                                          a_block_space_size_aligned * 
sizeof(ADataType)),
 
 1539             b_block_desc_bk0_n_bk1.GetElementSpaceSize());
 
 1545         static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
 
 1547         auto c_thread_buf            = blockwise_gemm_pipeline.GetCThreadBuffer();
 
 1548         decltype(c_thread_buf) c_thread_buf_up;
 
 1552                                   c_thread_buf.num_of_v_,
 
 1553                                   c_thread_buf.s_per_v,
 
 1557         const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
 
 1558             (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
 
 1562         const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
 
 1563         const auto waveId_m = wave_idx[
I0];
 
 1564         const auto waveId_n = wave_idx[
I1];
 
 1566         auto thread_offset_shuffled =
 
 1569         auto a_thread_offset_m = waveId_m;
 
 1574             decltype(a_scale_grid_desc_am_ak),
 
 1575             decltype(BlockwiseGemmPipe::a_scale_thread_desc),
 
 1581             true>(a_scale_grid_desc_am_ak,
 
 1587         auto b_thread_offset_n = waveId_n;
 
 1592             decltype(b_scale_grid_desc_bn_ak),
 
 1593             decltype(BlockwiseGemmPipe::b_scale_thread_desc),
 
 1599             true>(b_scale_grid_desc_bn_ak,
 
 1604         if constexpr(IsInputGemm)
 
 1607                 b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
 
 1608             auto b_block_buf_up = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 1609                 reinterpret_cast<BDataType*
>(
static_cast<char*
>(p_shared) +
 
 1610                                              a_block_space_size_aligned * 
sizeof(ADataType) +
 
 1611                                              b_block_space_size_aligned * 
sizeof(BDataType)),
 
 1612                 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
 
 1614             const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
 
 1615             const auto b_grid_buf_up     = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1616                 p_b_grid_up + expert_id * expert_stride,
 
 1617                 b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
 
 1619             auto b_blockwise_copy_up =
 
 1621                                                     BElementwiseOperation,
 
 1625                                                     BBlockTransferThreadClusterLengths_BK0_N_BK1,
 
 1626                                                     BBlockTransferThreadClusterArrangeOrder,
 
 1629                                                     decltype(b_grid_desc_bk0_n_bk1),
 
 1630                                                     decltype(b_block_desc_bk0_n_bk1),
 
 1631                                                     BBlockTransferSrcAccessOrder,
 
 1633                                                     BBlockTransferSrcVectorDim,
 
 1635                                                     BBlockTransferSrcScalarPerVector,
 
 1636                                                     BBlockTransferDstScalarPerVector_BK1,
 
 1639                                                     BThreadTransferSrcResetCoordinateAfterRun,
 
 1641                                                     BlockwiseGemmPipe::GlobalBufferNum>(
 
 1642                     b_grid_desc_bk0_n_bk1,
 
 1645                     b_block_desc_bk0_n_bk1,
 
 1649             const BScaleDataType* p_b_scale_grid_up =
 
 1650                 p_b_scale_grid + expert_scale_stride / 2 / 
sizeof(BScaleDataType);
 
 1651             const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1652                 p_b_scale_grid_up + expert_id * expert_scale_stride / 
sizeof(BScaleDataType),
 
 1653                 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
 
 1658                 decltype(b_scale_grid_desc_bn_ak),
 
 1659                 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
 
 1666                 b_scale_grid_desc_bn_ak,
 
 1671             blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
 
 1673                 a_grid_desc_ak0_m_ak1,
 
 1674                 a_block_desc_ak0_m_ak1,
 
 1678                 a_block_slice_copy_step,
 
 1680                 b_grid_desc_bk0_n_bk1,
 
 1681                 b_block_desc_bk0_n_bk1,
 
 1683                 b_blockwise_copy_up,
 
 1688                 b_block_slice_copy_step,
 
 1693                 a_scale_grid_desc_am_ak,
 
 1694                 a_scale_thread_copy,
 
 1697                 b_scale_grid_desc_bn_ak,
 
 1698                 b_scale_thread_copy,
 
 1699                 b_scale_thread_copy_up,
 
 1701                 b_scale_grid_buf_up,
 
 1702                 num_k_block_main_loop);
 
 1706             blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
 
 1707                 a_grid_desc_ak0_m_ak1, 
 
 1708                 a_block_desc_ak0_m_ak1,
 
 1712                 a_block_slice_copy_step,
 
 1713                 b_grid_desc_bk0_n_bk1, 
 
 1714                 b_block_desc_bk0_n_bk1,
 
 1718                 b_block_slice_copy_step,
 
 1720                 a_scale_grid_desc_am_ak, 
 
 1721                 a_scale_thread_copy,
 
 1723                 b_scale_grid_desc_bn_ak, 
 
 1724                 b_scale_thread_copy,
 
 1726                 num_k_block_main_loop);
 
 1731             static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
 
 1732                               NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
 
 1734             static_assert(CShuffleMXdlPerWavePerShuffle % 
MXdlPack == 0 &&
 
 1735                               CShuffleNXdlPerWavePerShuffle % 
NXdlPack == 0,
 
 1738             constexpr 
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
 
 1739             constexpr 
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
 
 1742             constexpr 
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
 
 1743                 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
 
 1747             constexpr 
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
 
 1748                 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
 
 1750             constexpr 
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
 
 1751             constexpr 
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
 
 1752             constexpr 
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
 
 1753             constexpr 
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
 
 1754             constexpr 
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
 
 1755             constexpr 
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
 
 1756             constexpr 
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
 
 1757             constexpr 
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
 
 1758             constexpr 
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
 
 1759             constexpr 
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
 
 1762             static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
 
 1763             static_assert(M5 == 4);
 
 1773                                 const index_t m_pos = block_m_id * MPerBlock +
 
 1774                                                       m0 * M2 * M1 * M3 * M4 * M5 +
 
 1775                                                       m1 * M2 * M3 * M4 * M5 +
 
 1776                                                       imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
 
 1778                                 if constexpr(MulRoutedWeight)
 
 1781                                         *c_style_pointer_cast<const vector_type<float, M5>*>(
 
 1782                                             p_ds_grid[
I2] + m_pos);
 
 1786                                         blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
 
 1787                                             make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
 
 1790                                     if constexpr(IsInputGemm) 
 
 1792                                         if constexpr(ActivationOperation ==
 
 1795                                             float gate = c_thread_buf[cidx];
 
 1796                                             float up   = c_thread_buf_up[cidx];
 
 1797                                             if constexpr(MulRoutedWeight)
 
 1799                                                 gate = gate * topk_weights.AsType<
float>()[m5];
 
 1800                                                 up   = up * topk_weights.AsType<
float>()[m5];
 
 1803                                             c_thread_buf_fp32(cidx) = gate * up;
 
 1807                                             float gate = c_thread_buf[cidx];
 
 1808                                             float up   = c_thread_buf_up[cidx];
 
 1809                                             if constexpr(MulRoutedWeight)
 
 1811                                                 gate = gate * topk_weights.AsType<
float>()[m5];
 
 1812                                                 up   = up * topk_weights.AsType<
float>()[m5];
 
 1815                                             c_thread_buf_fp32(cidx) = gate * up;
 
 1830                                         c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
 
 1831                                         if constexpr(MulRoutedWeight)
 
 1833                                             c_thread_buf_fp32(cidx) =
 
 1834                                                 topk_weights.AsType<
float>()[m5] *
 
 1835                                                 c_thread_buf_fp32[cidx];
 
 1845             constexpr 
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
 
 1848             auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 1849                 static_cast<CShuffleDataType*
>(p_shared),
 
 1850                 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
 
 1853                 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
 
 1879             const auto c_thread_mtx_on_block =
 
 1880                 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0, 
I0, 
I0, 
I0);
 
 1882             const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
 
 1883             const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
 
 1885             const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
 
 1891             const auto m_thread_data_on_block_idx =
 
 1892                 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
 
 1895             const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
 
 1901             const auto n_thread_data_on_block_idx =
 
 1902                 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
 
 1909                 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
 
 1910                 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
 
 1913                          CShuffleNXdlPerWavePerShuffle / 
NXdlPack,
 
 1922                 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
 
 1927                 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
 
 1930                                        m_thread_data_on_block_idx[
I1],
 
 1931                                        n_thread_data_on_block_idx[
I1],
 
 1932                                        m_thread_data_on_block_idx[
I2],
 
 1933                                        n_thread_data_on_block_idx[
I2],
 
 1934                                        m_thread_data_on_block_idx[
I3],
 
 1935                                        m_thread_data_on_block_idx[
I4],
 
 1936                                        m_thread_data_on_block_idx[
I5],
 
 1937                                        n_thread_data_on_block_idx[
I3]),
 
 1940             using EDataType = CDataType;
 
 1945             const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
 
 1951                     return make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 1952                         p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
 
 1958                 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
 
 1960                     [&](
auto i) -> 
const auto& 
 
 1961                     { 
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
 
 1966                 tie(c_shuffle_block_buf),
 
 1968                     [&](
auto i) -> 
const auto& 
 
 1969                     { 
return ds_grid_buf[i]; },
 
 1973             const auto idx_c_ds_block_begin =
 
 1983             const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
 
 1984                 c_grid_desc_mblock_mperblock_nblock_nperblock;
 
 1986             using CDEBlockTransferCluster =
 
 1987                 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
 
 1988             const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
 
 1989             constexpr 
index_t scatter_weight_idx  = 3; 
 
 1994                 decltype(c_ds_desc_refs),
 
 1995                 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
 
 1996                 CElementwiseOperation,
 
 2001                          CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
 
 2003                          CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>, 
 
 2004                 CDEBlockTransferCluster,
 
 2010                 CDEShuffleBlockTransferScalarPerVectors,
 
 2022                   idx_c_ds_block_begin,
 
 2023                   tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
 
 2027             auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2028                 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
 
 2030             constexpr 
auto sfc_c_vgpr =
 
 2041                                   Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
 
 2043                                            CShuffleNXdlPerWavePerShuffle / 
NXdlPack,
 
 2053             constexpr 
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
 
 2056             constexpr 
auto sfc_cde_block =
 
 2060                                            CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
 
 2062                                            CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>>{};
 
 2064             static_assert(num_access == sfc_cde_block.GetNumOfAccess(), 
"wrong!");
 
 2065             constexpr 
auto EMThreads =
 
 2066                 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
 
 2067             constexpr 
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
 
 2068             constexpr 
auto ENThreads =
 
 2069                 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
 
 2074                 auto dstidx = sfc_cde_block.GetIndex(access_id);
 
 2076                     block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
 
 2078                     const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
 
 2079                     IndexType token_offset    = fused_token & 0xffffff;
 
 2080                     if constexpr(IsInputGemm)
 
 2082                         token_offset = token_offset * problem.
TopK + (fused_token >> 24);
 
 2084                     scatter_offsets(m0) = 
static_cast<IndexType
>(token_offset) * problem.
N;
 
 2090                 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
 
 2091                                               sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
 
 2093                                               c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
 
 2094                                               c_shuffle_block_buf);
 
 2100                 cde_block_copy_lds_and_global.Run(
 
 2103                     tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
 
 2107                 if constexpr(access_id < num_access - 1)
 
 2109                     constexpr 
auto cde_lds_and_global_step =
 
 2110                         sfc_cde_block.GetForwardStep(access_id);
 
 2114                         cde_block_copy_lds_and_global.MoveSrcSliceWindow(
 
 2115                             c_ds_desc_refs, i + 
I1, cde_lds_and_global_step);
 
 2119                     cde_block_copy_lds_and_global.MoveDstSliceWindow(
 
 2120                         tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
 
 2122                         cde_lds_and_global_step);
 
 2129     template <
bool HasMainKBlockLoop,
 
 2132     __device__ 
static void Run_2Lds(
const index_t* p_sorted_token_ids,
 
 2133                                     const index_t* p_sorted_expert_ids,
 
 2134                                     const index_t* p_max_token_id,
 
 2135                                     const ADataType* p_a_grid,
 
 2136                                     const AScaleDataType* p_a_scale_grid,
 
 2137                                     const BDataType* p_b_grid,
 
 2138                                     const BScaleDataType* p_b_scale_grid,
 
 2140                                     CDataType* p_c_grid,
 
 2143                                     const Problem& problem,
 
 2144                                     AElementwiseOperation a_element_op,
 
 2145                                     BElementwiseOperation b_element_op,
 
 2146                                     CElementwiseOperation c_element_op)
 
 2150             IsInputGemm ? problem.NumTokens : problem.NumTokens * problem.TopK,
 
 2157             problem.K, problem.KPadded, problem.N, problem.NPadded, problem.StrideB, problem.BK0);
 
 2158         const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
 
 2159             IsInputGemm ? problem.NumTokens * problem.TopK : problem.NumTokens,
 
 2177         const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
 
 2179                 c_grid_desc_m_n, problem.MBlock, problem.NBlock);
 
 2180         const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
 
 2182         const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.NBlock : blockIdx.y;
 
 2183         if(expert_block_id * MPerBlock >= max_token_id)
 
 2186             __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
 
 2187         const auto block_mn = [&]() -> std::pair<int, int> {
 
 2188             if constexpr(NSwizzle)
 
 2190                 const index_t ecnt_prefix  = p_max_token_id[1 + expert_id];
 
 2191                 const index_t prefix_block = ecnt_prefix * problem.NBlock;
 
 2192                 const index_t ecnt         = p_max_token_id[2 + expert_id] - ecnt_prefix;
 
 2193                 const index_t expert_swizzle =
 
 2194                     ecnt > 0 ? ecnt : 1; 
 
 2195                 const index_t bid_new = blockIdx.x - prefix_block;
 
 2196                 const index_t nid     = __builtin_amdgcn_readfirstlane(
 
 2197                     bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
 
 2199                     __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
 
 2204                 return {blockIdx.x, blockIdx.y};
 
 2208         const index_t block_n_id = block_mn.first;
 
 2209         const index_t block_m_id = block_mn.second;
 
 2211             __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
 
 2214         constexpr 
auto AMThreads  = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
 
 2215         constexpr 
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
 
 2216         constexpr 
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
 
 2217         constexpr 
auto AKThreads  = AK0Threads * AK1Threads;
 
 2218         constexpr 
auto AMRepeats  = MPerBlock / AMThreads;
 
 2219         const index_t token_pos   = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
 
 2221         if(token_pos >= max_token_id || token0 >= problem.NumTokens)
 
 2223         StaticallyIndexedArray<IndexType, AMRepeats> gather_offsets;
 
 2224         static_for<0, AMRepeats, 1>{}([&](
auto m0) {
 
 2225             const index_t fused_token = p_sorted_token_ids[token_pos + m0];
 
 2226             index_t token_offset      = fused_token & 0xffffff;
 
 2227             if constexpr(!IsInputGemm)
 
 2229                 token_offset = token_offset * problem.TopK + (fused_token >> 24);
 
 2231             gather_offsets(m0) = 
static_cast<IndexType
>(token_offset) * problem.K;
 
 2235             __builtin_amdgcn_readfirstlane(problem.N * problem.K * (IsInputGemm ? 2 : 1));
 
 2236         const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
 
 2240         const index_t n_block_data_idx_on_grid =
 
 2241             __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave);
 
 2243         const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2244             p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
 
 2246         const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2247             p_b_grid + expert_id * expert_stride, b_grid_desc_bpreshuffled.GetElementSpaceSize());
 
 2249         const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2250             p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
 
 2251         const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2252             p_b_scale_grid + (expert_id * expert_scale_stride) / 
sizeof(BScaleDataType),
 
 2253             b_scale_grid_desc_bn_ak.GetElementSpaceSize());
 
 2262         auto a_blockwise_copy = ThreadGroupTensorSliceTransfer_v4r1_gather<
 
 2264             AElementwiseOperation,
 
 2267             Sequence<AK0Number, MPerBlock, AK1Number>,
 
 2268             ABlockTransferThreadClusterLengths_AK0_M_AK1,
 
 2269             ABlockTransferThreadClusterArrangeOrder,
 
 2272             decltype(a_grid_desc_ak0_m_ak1),
 
 2273             decltype(a_block_desc_ak0_m_ak1),
 
 2274             ABlockTransferSrcAccessOrder,
 
 2276             ABlockTransferSrcVectorDim,
 
 2278             ABlockTransferSrcScalarPerVector,
 
 2279             ABlockTransferDstScalarPerVector_AK1,
 
 2282             AThreadTransferSrcResetCoordinateAfterRun,
 
 2286             BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
 
 2289                                                 a_block_desc_ak0_m_ak1,
 
 2296         auto b_block_buf_ping = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
 
 2297             b_block_desc_bk0_n_bk1.GetElementSpaceSize());
 
 2298         auto b_block_buf_pong = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
 
 2299             b_block_desc_bk0_n_bk1.GetElementSpaceSize());
 
 2300         auto b_block_bufs = 
make_tuple(b_block_buf_ping, b_block_buf_pong);
 
 2302         auto b_blockwise_copy =
 
 2303             ThreadwiseTensorSliceTransfer_v2<BDataType,
 
 2305                                              decltype(b_grid_desc_bpreshuffled),
 
 2306                                              decltype(b_block_desc_bk0_n_bk1),
 
 2311                                                       Number<BK1Value>{}>,
 
 2312                                              Sequence<1, 2, 0, 3, 4>,
 
 2314                                              BBlockTransferSrcScalarPerVector,
 
 2315                                              BThreadTransferSrcResetCoordinateAfterRun,
 
 2317                 b_grid_desc_bpreshuffled,
 
 2326         auto a_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 2327             static_cast<ADataType*
>(p_shared), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
 
 2328         auto a_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 2329             static_cast<ADataType*
>(p_shared1), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
 
 2330         auto a_block_bufs = 
make_tuple(a_block_buf_ping, a_block_buf_pong);
 
 2333         constexpr 
auto b_block_slice_copy_step = 
make_multi_index(0, 0, 0, KRepeat, 0);
 
 2336         static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
 
 2338         auto c_thread_buf            = blockwise_gemm_pipeline.GetCThreadBuffer();
 
 2339         decltype(c_thread_buf) c_thread_buf_up;
 
 2343                                   c_thread_buf.num_of_v_,
 
 2344                                   c_thread_buf.s_per_v,
 
 2348         const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
 
 2349             (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
 
 2353         const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
 
 2354         const auto waveId_m = wave_idx[
I0];
 
 2355         const auto waveId_n = wave_idx[
I1];
 
 2357         auto thread_offset_shuffled =
 
 2360         auto a_thread_offset_m = waveId_m;
 
 2363         const index_t token_scale_pos = block_m_id * MPerBlock;
 
 2364         if(token_scale_pos >= max_token_id || token0 >= problem.NumTokens)
 
 2367         auto a_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
 
 2370             decltype(a_scale_grid_desc_am_ak),
 
 2371             decltype(BlockwiseGemmPipe::a_scale_thread_desc),
 
 2377             true>(a_scale_grid_desc_am_ak,
 
 2383         auto b_thread_offset_n = waveId_n;
 
 2385         auto b_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
 
 2388             decltype(b_scale_grid_desc_bn_ak),
 
 2389             decltype(BlockwiseGemmPipe::b_scale_thread_desc),
 
 2395             true>(b_scale_grid_desc_bn_ak,
 
 2400         if constexpr(IsInputGemm)
 
 2402             const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2 / 
BPackedSize;
 
 2403             const auto b_grid_buf_up     = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2404                 p_b_grid_up + expert_id * expert_stride / 
BPackedSize,
 
 2405                 b_grid_desc_bpreshuffled.GetElementSpaceSize());
 
 2406             auto b_blockwise_copy_up = ThreadwiseTensorSliceTransfer_v2<
 
 2409                 decltype(b_grid_desc_bpreshuffled),
 
 2410                 decltype(b_block_desc_bk0_n_bk1),
 
 2411                 Sequence<Number<NXdlPerWave>{}, 
I1, Number<KRepeat>{}, Number<BK1Value>{}>,
 
 2412                 Sequence<1, 2, 0, 3>,
 
 2414                 BBlockTransferSrcScalarPerVector,
 
 2415                 BThreadTransferSrcResetCoordinateAfterRun,
 
 2416                 true>(b_grid_desc_bpreshuffled,
 
 2421             const BScaleDataType* p_b_scale_grid_up = p_b_scale_grid + expert_scale_stride / 2;
 
 2422             const auto b_scale_grid_buf_up          = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2423                 p_b_scale_grid_up + expert_id * expert_scale_stride,
 
 2424                 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
 
 2425             auto b_scale_thread_copy_up = ThreadwiseTensorSliceTransfer_v2<
 
 2428                 decltype(b_scale_grid_desc_bn_ak),
 
 2429                 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
 
 2436                 b_scale_grid_desc_bn_ak,
 
 2441             blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
 
 2442                 a_grid_desc_ak0_m_ak1,
 
 2443                 a_block_desc_ak0_m_ak1,
 
 2447                 a_block_slice_copy_step,
 
 2448                 b_grid_desc_bpreshuffled,
 
 2449                 b_block_desc_bk0_n_bk1,
 
 2451                 b_blockwise_copy_up,
 
 2455                 b_block_slice_copy_step,
 
 2458                 a_scale_grid_desc_am_ak,
 
 2459                 a_scale_thread_copy,
 
 2461                 b_scale_grid_desc_bn_ak,
 
 2462                 b_scale_thread_copy,
 
 2463                 b_scale_thread_copy_up,
 
 2465                 b_scale_grid_buf_up,
 
 2466                 num_k_block_main_loop);
 
 2470             blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
 
 2471                 a_grid_desc_ak0_m_ak1,
 
 2472                 a_block_desc_ak0_m_ak1,
 
 2476                 a_block_slice_copy_step,
 
 2477                 b_grid_desc_bpreshuffled,
 
 2478                 b_block_desc_bk0_n_bk1,
 
 2482                 b_block_slice_copy_step,
 
 2484                 a_scale_grid_desc_am_ak,
 
 2485                 a_scale_thread_copy,
 
 2487                 b_scale_grid_desc_bn_ak,
 
 2488                 b_scale_thread_copy,
 
 2490                 num_k_block_main_loop);
 
 2495             static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
 
 2496                               NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
 
 2500             constexpr 
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
 
 2501                 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
 
 2505             constexpr 
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
 
 2506                 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
 
 2508             constexpr 
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
 
 2509             constexpr 
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
 
 2510             constexpr 
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
 
 2511             constexpr 
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
 
 2512             constexpr 
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
 
 2513             constexpr 
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
 
 2514             constexpr 
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
 
 2515             constexpr 
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
 
 2519             static_assert(M0 * M1 * M2 * M3 * M4 == MPerBlock);
 
 2520             static_assert(M4 == 4);
 
 2524             vector_type<float, 4> topk_weights; 
 
 2525             static_for<0, NXdlPerWave, 1>{}([&](
auto n0) {
 
 2526                 static_for<0, MXdlPerWave, 1>{}([&](
auto m0) { 
 
 2527                     static_for<0, M2, 1>{}([&](
auto m2) {      
 
 2528                         const index_t m_pos = block_m_id * MPerBlock + m0 * M1 * M2 * M3 * M4 +
 
 2529                                               m1 * M2 * M3 * M4 + m2 * M3 * M4 + m3 * M4;
 
 2530                         if constexpr(MulRoutedWeight)
 
 2532                             topk_weights = *c_style_pointer_cast<const vector_type<float, M4>*>(
 
 2533                                 p_ds_grid[
I2] + m_pos);
 
 2535                         static_for<0, M4, 1>{}([&](
auto m4) { 
 
 2537                                 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
 
 2543                             constexpr 
auto cidx = Number<c_offset>{};
 
 2545                             if constexpr(IsInputGemm) 
 
 2549                                     float gate = c_thread_buf[cidx];
 
 2550                                     float up   = c_thread_buf_up[cidx];
 
 2551                                     if constexpr(MulRoutedWeight)
 
 2553                                         gate = gate * topk_weights.AsType<
float>()[m4];
 
 2554                                         up   = up * topk_weights.AsType<
float>()[m4];
 
 2556                                     tensor_operation::element_wise::Silu{}(gate, gate);
 
 2557                                     c_thread_buf_fp32(cidx) = gate * up;
 
 2561                                     float gate = c_thread_buf[cidx];
 
 2562                                     float up   = c_thread_buf_up[cidx];
 
 2563                                     if constexpr(MulRoutedWeight)
 
 2565                                         gate = gate * topk_weights.AsType<
float>()[m4];
 
 2566                                         up   = up * topk_weights.AsType<
float>()[m4];
 
 2568                                     tensor_operation::element_wise::Gelu{}(gate, gate);
 
 2569                                     c_thread_buf_fp32(cidx) = gate * up;
 
 2574                                 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
 
 2575                                 if constexpr(MulRoutedWeight)
 
 2577                                     c_thread_buf_fp32(cidx) =
 
 2578                                         topk_weights.AsType<
float>()[m4] * c_thread_buf_fp32[cidx];
 
 2586             constexpr 
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
 
 2589             auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
 
 2590                 static_cast<CShuffleDataType*
>(p_shared),
 
 2591                 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
 
 2594                 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
 
 2597                                Number<CShuffleMXdlPerWavePerShuffle>{}, 
 
 2605                                Number<CShuffleNXdlPerWavePerShuffle>{}, 
 
 2609                 make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}),
 
 2611                     Sequence<>{}, Sequence<0, 2, 4, 5, 6>{}, Sequence<>{}, Sequence<1, 3, 7>{}));
 
 2615             const auto c_thread_mtx_on_block =
 
 2616                 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0, 
I0, 
I0, 
I0);
 
 2618             const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
 
 2619             const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
 
 2621             const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
 
 2627             const auto m_thread_data_on_block_idx =
 
 2628                 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
 
 2631             const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
 
 2637             const auto n_thread_data_on_block_idx =
 
 2638                 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
 
 2642             auto c_thread_copy_vgpr_to_lds =
 
 2643                 ThreadwiseTensorSliceTransfer_v1r3<AccDataType,
 
 2645                                                    decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
 
 2646                                                    decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
 
 2648                                                    Sequence<CShuffleMXdlPerWavePerShuffle,
 
 2649                                                             CShuffleNXdlPerWavePerShuffle,
 
 2656                                                    Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
 
 2662                     c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
 
 2665                                      m_thread_data_on_block_idx[
I1],
 
 2666                                      n_thread_data_on_block_idx[
I1],
 
 2667                                      m_thread_data_on_block_idx[
I2],
 
 2668                                      m_thread_data_on_block_idx[
I3],
 
 2669                                      m_thread_data_on_block_idx[
I4],
 
 2670                                      n_thread_data_on_block_idx[
I2]),
 
 2673             using EDataType = CDataType;
 
 2676                 problem.M, problem.MPadded, problem.N, problem.NPadded, problem.StrideDs);
 
 2678             const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
 
 2680                     ds_grid_desc_m_n, problem.MBlock, problem.NBlock);
 
 2684                     return make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2685                         p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
 
 2687                 Number<NumDTensor>{});
 
 2691                 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
 
 2693                              { 
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
 
 2694                              Number<NumDTensor>{}));
 
 2698                 tie(c_shuffle_block_buf),
 
 2700                              { 
return ds_grid_buf[i]; },
 
 2701                              Number<NumDTensor>{}));
 
 2704             const auto idx_c_ds_block_begin =
 
 2712                                      Number<NumDTensor>{}));
 
 2714             const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
 
 2715                 c_grid_desc_mblock_mperblock_nblock_nperblock;
 
 2717             using CDEBlockTransferCluster =
 
 2718                 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
 
 2719             const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
 
 2720             constexpr 
index_t scatter_weight_idx  = 3; 
 
 2721             auto cde_block_copy_lds_and_global    = ThreadGroupTensorSliceTransfer_v7r3_scatter<
 
 2725                    decltype(c_ds_desc_refs),
 
 2726                    decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
 
 2727                    CElementwiseOperation,
 
 2728                    Sequence<static_cast<index_t>(EGlobalMemoryDataOperation)>, 
 
 2732                             CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
 
 2734                             CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>, 
 
 2735                    CDEBlockTransferCluster,
 
 2736                    Sequence<0, 1, 2, 3>, 
 
 2737                    Sequence<0, 1, 2, 3>, 
 
 2738                    Sequence<0, 1, 2, 3>, 
 
 2741                    CDEShuffleBlockTransferScalarPerVectors,
 
 2753                      idx_c_ds_block_begin,
 
 2754                      tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
 
 2758             auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
 
 2759                 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
 
 2760             constexpr 
auto sfc_c_vgpr =
 
 2761                 SpaceFillingCurve<Sequence<MXdlPerWave, NXdlPerWave, 1, 1, M2, 1, M4, 1>,
 
 2762                                   Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
 
 2763                                   Sequence<CShuffleMXdlPerWavePerShuffle,
 
 2764                                            CShuffleNXdlPerWavePerShuffle,
 
 2772             constexpr 
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
 
 2775             constexpr 
auto sfc_cde_block =
 
 2776                 SpaceFillingCurve<Sequence<1, MPerBlock, 1, NPerBlock>,
 
 2777                                   Sequence<0, 2, 1, 3>,
 
 2779                                            CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
 
 2781                                            CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>>{};
 
 2783             static_assert(num_access == sfc_cde_block.GetNumOfAccess(), 
"wrong!");
 
 2784             constexpr 
auto EMThreads =
 
 2785                 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
 
 2786             constexpr 
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
 
 2787             constexpr 
auto ENThreads =
 
 2788                 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
 
 2789             static_for<0, num_access, 1>{}([&](
auto access_id) {
 
 2791                 StaticallyIndexedArray<IndexType, EMRepeats> scatter_offsets;
 
 2793                 auto dstidx = sfc_cde_block.GetIndex(access_id);
 
 2795                     block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
 
 2796                 static_for<0, EMRepeats, 1>{}([&](
auto m0) {
 
 2797                     const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
 
 2798                     IndexType token_offset    = fused_token & 0xffffff;
 
 2799                     if constexpr(IsInputGemm)
 
 2801                         token_offset = token_offset * problem.TopK + (fused_token >> 24);
 
 2803                     scatter_offsets(m0) = 
static_cast<IndexType
>(token_offset) * problem.N;
 
 2809                 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
 
 2810                                               sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
 
 2812                                               c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
 
 2813                                               c_shuffle_block_buf);
 
 2819                 cde_block_copy_lds_and_global.Run(
 
 2822                     tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
 
 2826                 if constexpr(access_id < num_access - 1)
 
 2828                     constexpr 
auto cde_lds_and_global_step =
 
 2829                         sfc_cde_block.GetForwardStep(access_id);
 
 2832                     static_for<0, NumDTensor, 1>{}([&](
auto i) {
 
 2833                         cde_block_copy_lds_and_global.MoveSrcSliceWindow(
 
 2834                             c_ds_desc_refs, i + 
I1, cde_lds_and_global_step);
 
 2838                     cde_block_copy_lds_and_global.MoveDstSliceWindow(
 
 2839                         tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
 
 2841                         cde_lds_and_global_step);
 
#define CK_MAX_THREAD_PER_BLOCK
Definition: ck.hpp:29
 
Y __host__ constexpr __device__ auto lcm(X x, Y y)
Definition: math.hpp:198
 
__host__ constexpr __device__ auto integer_least_multiple(X x, Y y)
Definition: math.hpp:78
 
__host__ constexpr __device__ auto integer_divide_ceil(X x, Y y)
Definition: math.hpp:72
 
__host__ constexpr __device__ T max(T x)
Definition: math.hpp:84
 
GemmSpecialization
Definition: gemm_specialization.hpp:11
 
typename detail::StaticallyIndexedArrayImpl< T, N >::type StaticallyIndexedArray
Definition: statically_indexed_array.hpp:45
 
__host__ constexpr __device__ auto make_multi_index(Xs &&... xs)
Definition: array_multi_index.hpp:15
 
__device__ index_t get_warp_local_1d_id()
Definition: get_id.hpp:23
 
__host__ constexpr __device__ auto generate_tie(F &&f, Number< N >)
Definition: tuple_helper.hpp:34
 
__host__ constexpr __device__ auto make_naive_tensor_descriptor(const Tuple< Lengths... > &lengths, const Tuple< Strides... > &strides)
Definition: tensor_descriptor_helper.hpp:49
 
__global__ void kernel_moe_mxgemm(typename GridwiseGemm::Argument karg)
Definition: gridwise_moe_mx_gemm_bns.hpp:48
 
typename uniform_sequence_gen< NSize, I >::type uniform_sequence_gen_t
Definition: sequence.hpp:928
 
typename tuple_element< I, TTuple >::type tuple_element_t
Definition: tuple.hpp:208
 
__host__ constexpr __device__ auto generate_tuple(F &&f, Number< N >)
Definition: tuple_helper.hpp:21
 
InMemoryDataOperationEnum
Definition: ck.hpp:278
 
__host__ constexpr __device__ index_t get_warp_size()
Definition: get_id.hpp:10
 
__host__ constexpr __device__ auto make_naive_tensor_descriptor_packed(const Tuple< Lengths... > &lengths)
Definition: tensor_descriptor_helper.hpp:101
 
__host__ constexpr __device__ auto make_merge_transform(const LowLengths &low_lengths)
Definition: multi_index_transform_helper.hpp:55
 
BlockGemmPipelineVersion
Definition: blkgemmpipe_scheduler.hpp:12
 
__host__ constexpr __device__ auto make_merge_transform_v3_division_mod(const LowLengths &low_lengths)
Definition: multi_index_transform_helper.hpp:84
 
__global__ void kernel_moe_mxgemm_2lds(typename GridwiseGemm::Argument karg)
Definition: gridwise_moe_mx_gemm.hpp:87
 
TailNumber
Definition: blkgemmpipe_scheduler.hpp:31
 
__host__ constexpr __device__ auto make_single_stage_tensor_adaptor(const Transforms &transforms, LowerDimensionOldTopIdss, UpperDimensionNewTopIdss)
Definition: tensor_adaptor.hpp:429
 
__host__ constexpr __device__ auto make_freeze_transform(const LowerIndex &low_idx)
Definition: multi_index_transform_helper.hpp:98
 
constexpr detail::ignore_t ignore
Definition: ignore.hpp:20
 
constexpr Tuple< Args &... > tie(Args &... args) noexcept
Definition: tuple.hpp:218
 
__host__ constexpr __device__ auto make_xor_with_modulo_transform(const LowLengths &low_lengths)
Definition: multi_index_transform_helper.hpp:132
 
Activation
Definition: gridwise_moe_gemm.hpp:31
 
@ silu_and_mul
Definition: gridwise_moe_gemm.hpp:33
 
@ gelu_and_mul
Definition: gridwise_moe_gemm.hpp:32
 
bool EnvIsEnabled(EnvVar)
Definition: env.hpp:139
 
__host__ constexpr __device__ auto container_concat(const X &x, const Ys &... ys)
Definition: container_helper.hpp:320
 
__host__ constexpr __device__ auto make_pass_through_transform(const LowLength &low_length)
Definition: multi_index_transform_helper.hpp:12
 
__host__ constexpr __device__ auto concat_tuple_of_reference(const Tuple< X &... > &tx, const Tuple< Y &... > &ty)
Definition: tuple_helper.hpp:42
 
constexpr bool is_same_v
Definition: type.hpp:283
 
typename sequence_merge< Sx, Sy >::type sequence_merge_t
Definition: sequence.hpp:925
 
BlockGemmPipelineScheduler
Definition: blkgemmpipe_scheduler.hpp:25
 
__host__ constexpr __device__ auto make_tuple(Xs &&... xs)
Definition: tuple.hpp:211
 
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition: type.hpp:297
 
__host__ constexpr __device__ auto make_unmerge_transform(const UpLengths &up_lengths, integral_constant< bool, Use24BitIntegerCalculation >=integral_constant< bool, false >{})
Definition: multi_index_transform_helper.hpp:90
 
int32_t index_t
Definition: ck.hpp:300
 
__device__ index_t get_thread_local_1d_id()
Definition: get_id.hpp:19
 
__host__ constexpr __device__ auto transform_tensor_descriptor(const OldTensorDescriptor &old_tensor_desc, const NewTransforms &new_transforms, NewLowerDimensionOldVisibleIdss, NewUpperDimensionNewVisibleIdss)
Definition: tensor_descriptor.hpp:319
 
__host__ constexpr __device__ auto make_right_pad_transform(const LowLength &low_length, const RightPadLength &right_pad, integral_constant< bool, SkipIsValidCheck >=integral_constant< bool, false >{})
Definition: multi_index_transform_helper.hpp:37
 
__device__ void block_sync_lds()
Definition: synchronization.hpp:10
 
constexpr auto BlockGemmMXNBSPipeline_Selector()
Definition: blockwise_gemm_pipeline_xdlops_mx_moe_nbs_selector.hpp:37
 
integral_constant< index_t, N > Number
Definition: number.hpp:12
 
Definition: gridwise_moe_mx_gemm_bns.hpp:659
 
const ADataType * p_a_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:722
 
const index_t * p_sorted_token_ids
Definition: gridwise_moe_mx_gemm_bns.hpp:719
 
const index_t * p_sorted_expert_ids
Definition: gridwise_moe_mx_gemm_bns.hpp:720
 
const index_t * p_max_token_id
Definition: gridwise_moe_mx_gemm_bns.hpp:721
 
DsGridPointer p_ds_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:726
 
const CElementwiseOperation c_element_op
Definition: gridwise_moe_mx_gemm_bns.hpp:731
 
CDataType * p_c_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:727
 
const AElementwiseOperation a_element_op
Definition: gridwise_moe_mx_gemm_bns.hpp:729
 
const BScaleDataType * p_b_scale_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:725
 
const BDataType * p_b_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:724
 
const AScaleDataType * p_a_scale_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:723
 
__host__ Argument(const index_t *p_sorted_token_ids_, const index_t *p_sorted_expert_ids_, const index_t *p_max_token_id_, const ADataType *p_a_grid_, const AScaleDataType *p_a_scale_grid_, const BDataType *p_b_grid_, const BScaleDataType *p_b_scale_grid_, std::array< const void *, NumDTensor > p_ds_grid_, CDataType *p_c_grid_, index_t NumTokens_, index_t TopK_, index_t M_, index_t N_, index_t K_, index_t StrideA_, index_t StrideScaleA_, index_t StrideB_, index_t StrideScaleB_, std::array< index_t, NumDTensor > StrideDs_, index_t StrideC_, index_t k_batch_, AElementwiseOperation a_element_op_, BElementwiseOperation b_element_op_, CElementwiseOperation c_element_op_)
Definition: gridwise_moe_mx_gemm_bns.hpp:660
 
const BElementwiseOperation b_element_op
Definition: gridwise_moe_mx_gemm_bns.hpp:730
 
Definition: gridwise_moe_mx_gemm_bns.hpp:576
 
index_t M
Definition: gridwise_moe_mx_gemm_bns.hpp:637
 
index_t TopK
Definition: gridwise_moe_mx_gemm_bns.hpp:636
 
index_t NPadded
Definition: gridwise_moe_mx_gemm_bns.hpp:648
 
index_t MPadded
Definition: gridwise_moe_mx_gemm_bns.hpp:647
 
index_t StrideScaleB
Definition: gridwise_moe_mx_gemm_bns.hpp:643
 
index_t StrideScaleA
Definition: gridwise_moe_mx_gemm_bns.hpp:641
 
index_t MBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:653
 
index_t StrideC
Definition: gridwise_moe_mx_gemm_bns.hpp:645
 
index_t AK0
Definition: gridwise_moe_mx_gemm_bns.hpp:651
 
index_t KPadded
Definition: gridwise_moe_mx_gemm_bns.hpp:650
 
index_t NBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:654
 
__host__ Problem(index_t NumTokens_, index_t TopK_, index_t M_, index_t N_, index_t K_, index_t StrideA_, index_t StrideScaleA_, index_t StrideB_, index_t StrideScaleB_, std::array< index_t, NumDTensor > StrideDs_, index_t StrideC_, index_t KBatch_)
Definition: gridwise_moe_mx_gemm_bns.hpp:577
 
index_t StrideA
Definition: gridwise_moe_mx_gemm_bns.hpp:640
 
index_t StrideB
Definition: gridwise_moe_mx_gemm_bns.hpp:642
 
index_t KBatch
Definition: gridwise_moe_mx_gemm_bns.hpp:646
 
index_t BK0
Definition: gridwise_moe_mx_gemm_bns.hpp:652
 
index_t KRead
Definition: gridwise_moe_mx_gemm_bns.hpp:649
 
__host__ void Print() const
Definition: gridwise_moe_mx_gemm_bns.hpp:612
 
index_t K
Definition: gridwise_moe_mx_gemm_bns.hpp:639
 
index_t N
Definition: gridwise_moe_mx_gemm_bns.hpp:638
 
index_t NumTokens
Definition: gridwise_moe_mx_gemm_bns.hpp:635
 
std::array< index_t, NumDTensor > StrideDs
Definition: gridwise_moe_mx_gemm_bns.hpp:644
 
Definition: gridwise_moe_mx_gemm_bns.hpp:735
 
index_t a_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:789
 
index_t b_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:790
 
__device__ SplitKBatchOffset(Argument &karg, index_t k_id)
Definition: gridwise_moe_mx_gemm_bns.hpp:736
 
index_t b_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:792
 
index_t a_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:791
 
Definition: gridwise_moe_mx_gemm_bns.hpp:173
 
static constexpr auto is_scale_mfma
Definition: gridwise_moe_mx_gemm_bns.hpp:206
 
static constexpr auto I1
Definition: gridwise_moe_mx_gemm_bns.hpp:178
 
static constexpr index_t scale_pack_size_a
Definition: gridwise_moe_mx_gemm_bns.hpp:1320
 
static constexpr index_t NumDTensor
Definition: gridwise_moe_mx_gemm_bns.hpp:196
 
static __host__ auto CalculateMPadded(index_t M)
Definition: gridwise_moe_mx_gemm_bns.hpp:244
 
static constexpr auto MXdlPack
Definition: gridwise_moe_mx_gemm_bns.hpp:198
 
static constexpr auto BK0Number
Definition: gridwise_moe_mx_gemm_bns.hpp:192
 
static constexpr auto BK1Number
Definition: gridwise_moe_mx_gemm_bns.hpp:194
 
ADataType LDSTypeA
Definition: gridwise_moe_mx_gemm_bns.hpp:174
 
static constexpr __host__ bool CalculateHasMainKBlockLoop(index_t K)
Definition: gridwise_moe_mx_gemm_bns.hpp:1286
 
static constexpr bool is_single_rate_mfma
Definition: gridwise_moe_mx_gemm_bns.hpp:205
 
ThisThreadBlock< BlockSize > ThisThreadBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:232
 
static constexpr auto I5
Definition: gridwise_moe_mx_gemm_bns.hpp:182
 
__host__ static __device__ auto MakeBGridDescriptor_BK0_N_BK1(index_t K, index_t KPad, index_t N, index_t NPad, index_t StrideB, index_t BK0)
Definition: gridwise_moe_mx_gemm_bns.hpp:397
 
BDataType LDSTypeB
Definition: gridwise_moe_mx_gemm_bns.hpp:175
 
static constexpr __host__ TailNumber CalculateKBlockLoopTailNum(index_t K)
Definition: gridwise_moe_mx_gemm_bns.hpp:1293
 
decltype(MakeDsGridPointer()) DsGridPointer
Definition: gridwise_moe_mx_gemm_bns.hpp:230
 
__host__ static __device__ auto MakeAGridDescriptor_AK0_M_AK1(IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
Definition: gridwise_moe_mx_gemm_bns.hpp:315
 
static constexpr __host__ bool CheckValidity(const Argument &karg)
Definition: gridwise_moe_mx_gemm_bns.hpp:1108
 
__host__ static __device__ auto MakeDsGridDescriptor_M_N(index_t M, index_t MPad, index_t N, index_t NPad, std::array< index_t, NumDTensor > StrideDs)
Definition: gridwise_moe_mx_gemm_bns.hpp:552
 
static __device__ void Run(const index_t *p_sorted_token_ids, const index_t *p_sorted_expert_ids, const index_t *p_max_token_id, const ADataType *p_a_grid, const AScaleDataType *p_a_scale_grid, const BDataType *p_b_grid, const BScaleDataType *p_b_scale_grid, DsGridPointer &p_ds_grid, CDataType *p_c_grid, void *p_shared, const Problem &problem, AElementwiseOperation a_element_op, BElementwiseOperation b_element_op, CElementwiseOperation c_element_op)
Definition: gridwise_moe_mx_gemm_bns.hpp:1330
 
static constexpr auto I4
Definition: gridwise_moe_mx_gemm_bns.hpp:181
 
static constexpr __device__ index_t GetSharedMemoryNumberOfByte()
Definition: gridwise_moe_mx_gemm_bns.hpp:1070
 
static constexpr index_t APackedSize
Definition: gridwise_moe_mx_gemm_bns.hpp:202
 
static constexpr auto MakeDsGridPointer()
Definition: gridwise_moe_mx_gemm_bns.hpp:219
 
static constexpr __device__ auto MakeDsGridDescriptor_MBlock_MPerBlock_NBlock_NPerBlock(const DsGridDesc &ds_grid_desc_m_n, index_t MBlock, index_t NBlock)
Definition: gridwise_moe_mx_gemm_bns.hpp:564
 
static __host__ auto CalculateNPadded(index_t N)
Definition: gridwise_moe_mx_gemm_bns.hpp:249
 
static __host__ auto CalculateAK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:259
 
__host__ static constexpr __device__ auto MakeBMmaTileDescriptor_N0_N1_N2_N3_K(const BBlockDesc_BK0_N_BK1 &)
Definition: gridwise_moe_mx_gemm_bns.hpp:498
 
static constexpr auto I8
Definition: gridwise_moe_mx_gemm_bns.hpp:185
 
static constexpr auto I7
Definition: gridwise_moe_mx_gemm_bns.hpp:184
 
static constexpr auto KXdlPack
Definition: gridwise_moe_mx_gemm_bns.hpp:200
 
__host__ static constexpr __device__ auto MakeCGridDescriptor_MBlock_MPerBlock_NBlock_NPerBlock(const CGridDesc &c_grid_desc_m_n, index_t MBlock, index_t NBlock)
Definition: gridwise_moe_mx_gemm_bns.hpp:1301
 
static constexpr auto I2
Definition: gridwise_moe_mx_gemm_bns.hpp:179
 
__host__ static constexpr __device__ auto MakeAMmaTileDescriptor_M0_M1_M2_M3_K(const ABlockDesc_AK0_M_AK1 &)
Definition: gridwise_moe_mx_gemm_bns.hpp:488
 
static constexpr index_t scale_pack_size_b
Definition: gridwise_moe_mx_gemm_bns.hpp:1321
 
static __host__ auto CalculateKRead(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:277
 
__host__ static constexpr __device__ auto MakeGemmMmaTileDescriptor(const TileDesc_K0_MN_K1 &)
Definition: gridwise_moe_mx_gemm_bns.hpp:299
 
remove_cvref_t< decltype(BlockGemmMXNBSPipeline_Selector< BlkGemmPipelineVer, BlkGemmPipeSched, BlockSize, ScaleBlockSize, ADataType, AScaleDataType, BDataType, BScaleDataType, ComputeTypeA, AccDataType, decltype(GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()), decltype(GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()), decltype(MakeAMmaTileDescriptor_M0_M1_M2_M3_K(GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1())), decltype(MakeBMmaTileDescriptor_N0_N1_N2_N3_K(GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1())), ABlockTransferSrcScalarPerVector, BBlockTransferSrcScalarPerVector, MPerBlock, NPerBlock, KPerBlock, MPerXdl, NPerXdl, MXdlPerWave, NXdlPerWave, KPack, IsInputGemm >())> BlockwiseGemmPipe
Definition: gridwise_moe_mx_gemm_bns.hpp:1068
 
static constexpr auto I6
Definition: gridwise_moe_mx_gemm_bns.hpp:183
 
static constexpr index_t KPack
Definition: gridwise_moe_mx_gemm_bns.hpp:213
 
static __host__ auto CalculateBK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:265
 
static constexpr auto I3
Definition: gridwise_moe_mx_gemm_bns.hpp:180
 
static constexpr auto AK1Number
Definition: gridwise_moe_mx_gemm_bns.hpp:193
 
static constexpr __device__ auto GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()
Definition: gridwise_moe_mx_gemm_bns.hpp:912
 
static constexpr index_t SortedTileSize
Definition: gridwise_moe_mx_gemm_bns.hpp:217
 
static constexpr auto I9
Definition: gridwise_moe_mx_gemm_bns.hpp:186
 
__host__ static __device__ auto MakeDGridDescriptor_M_N(index_t M, index_t MPad, index_t N, index_t NPad, index_t StrideC)
Definition: gridwise_moe_mx_gemm_bns.hpp:531
 
static constexpr auto AK0Number
Definition: gridwise_moe_mx_gemm_bns.hpp:191
 
static constexpr __device__ auto GetCShuffleBlockDescriptor_MBlock_MPerBlock_NBlock_NPerBlock()
Definition: gridwise_moe_mx_gemm_bns.hpp:1025
 
static __host__ auto CalculateMBlock(index_t M)
Definition: gridwise_moe_mx_gemm_bns.hpp:284
 
static constexpr auto CShuffleBlockTransferScalarPerVector_NPerBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:188
 
static __host__ auto CalculateKPadded(index_t K)
Definition: gridwise_moe_mx_gemm_bns.hpp:254
 
static constexpr auto NXdlPack
Definition: gridwise_moe_mx_gemm_bns.hpp:199
 
static __host__ auto CalculateNBlock(index_t N)
Definition: gridwise_moe_mx_gemm_bns.hpp:289
 
static constexpr index_t BPackedSize
Definition: gridwise_moe_mx_gemm_bns.hpp:203
 
__host__ static __device__ auto MakeCGridDescriptor_M_N(IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
Definition: gridwise_moe_mx_gemm_bns.hpp:507
 
static __host__ auto CalculateKPadded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:271
 
static constexpr __device__ auto GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()
Definition: gridwise_moe_mx_gemm_bns.hpp:795
 
static __host__ auto CalculateGridSize(index_t M, index_t N)
Definition: gridwise_moe_mx_gemm_bns.hpp:234
 
static constexpr auto I0
Definition: gridwise_moe_mx_gemm_bns.hpp:177
 
Definition: xdlops_gemm.hpp:942
 
static constexpr auto selected_mfma
Definition: xdlops_gemm.hpp:1343
 
Definition: sequence.hpp:43
 
Definition: tensor_space_filling_curve.hpp:20
 
Definition: static_buffer.hpp:75
 
Blockwise data transfer.
Definition: thread_group_tensor_slice_transfer_v4r1_gather.hpp:48
 
Blockwise data transfer.
Definition: thread_group_tensor_slice_transfer_v4r1.hpp:46
 
Definition: thread_group_tensor_slice_transfer_v7r3_scatter.hpp:51
 
Definition: threadwise_tensor_slice_transfer.hpp:39
 
Helper structure that facilitates transfer of source (grid) data to destination threads.
Definition: threadwise_tensor_slice_transfer.hpp:234
 
Definition: tuple.hpp:117
 
Unsigned representation of a conventional biased Float32 exponent.
Definition: e8m0.hpp:25
 
Definition: data_type.hpp:41
 
Definition: integral_constant.hpp:20
 
Definition: data_type.hpp:186
 
Definition: functional2.hpp:33
 
Definition: device_base.hpp:51
 
Definition: unary_element_wise_operation.hpp:981
 
Definition: unary_element_wise_operation.hpp:308
 
Definition: unary_element_wise_operation.hpp:1023
 
Definition: dtype_vector.hpp:10
 
#define CK_ENV(name)
Definition: env.hpp:128