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,
75 template <
typename GridwiseGemm,
76 bool HasMainKBlockLoop,
81 #if CK_USE_LAUNCH_BOUNDS
87 #if(!defined(__HIP_DEVICE_COMPILE__) || defined(__gfx9__))
88 __shared__
char p_shared_0[GridwiseGemm::GetSharedMemoryNumberOfByte()];
89 __shared__
char p_shared_1[GridwiseGemm::GetSharedMemoryNumberOfByte()];
91 auto splitk_batch_offset =
typename GridwiseGemm::SplitKBatchOffset(karg, blockIdx.z);
93 GridwiseGemm::template Run_2Lds<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
94 karg.p_sorted_token_ids,
95 karg.p_sorted_expert_ids,
97 karg.p_a_grid + splitk_batch_offset.a_k_split_offset,
98 karg.p_a_scale_grid + splitk_batch_offset.a_scale_k_split_offset,
99 karg.p_b_grid + splitk_batch_offset.b_k_split_offset,
100 karg.p_b_scale_grid + splitk_batch_offset.b_scale_k_split_offset,
114 template <
typename ALayout,
119 typename AScaleDataType,
121 typename BScaleDataType,
122 typename AccDataType,
123 typename CShuffleDataType,
126 typename AElementwiseOperation,
127 typename BElementwiseOperation,
128 typename CElementwiseOperation,
141 typename ABlockTransferThreadClusterLengths_AK0_M_AK1,
142 typename ABlockTransferThreadClusterArrangeOrder,
143 typename ABlockTransferSrcAccessOrder,
144 index_t ABlockTransferSrcVectorDim,
145 index_t ABlockTransferSrcScalarPerVector,
146 index_t ABlockTransferDstScalarPerVector_AK1,
147 bool AThreadTransferSrcResetCoordinateAfterRun,
149 typename BBlockTransferThreadClusterLengths_BK0_N_BK1,
150 typename BBlockTransferThreadClusterArrangeOrder,
151 typename BBlockTransferSrcAccessOrder,
152 index_t BBlockTransferSrcVectorDim,
153 index_t BBlockTransferSrcScalarPerVector,
154 index_t BBlockTransferDstScalarPerVector_BK1,
155 bool BThreadTransferSrcResetCoordinateAfterRun,
157 index_t CShuffleMXdlPerWavePerShuffle,
158 index_t CShuffleNXdlPerWavePerShuffle,
159 typename CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock,
160 typename CDEShuffleBlockTransferScalarPerVectors,
163 index_t ActivationOperation = 0,
164 bool NSwizzle =
false,
165 bool IsInputGemm =
true,
166 bool MulRoutedWeight =
true,
168 typename ComputeTypeA = ADataType,
169 typename ComputeTypeB = BDataType>
187 CDEShuffleBlockTransferScalarPerVectors{}[
I0];
234 "A scale pack data type too large!");
236 "B scale pack data type too large!");
244 return static_cast<const DDataType*
>(
nullptr);
257 const index_t gridx = NSwizzle ? nblock * mblock : nblock;
258 const index_t gridy = NSwizzle ? 1 : mblock;
289 auto K_t = K_Batch * KPerBlock;
290 return (K + K_t - 1) / K_t * (KPerBlock / AK1Value);
295 auto K_t = K_Batch * KPerBlock;
296 return (K + K_t - 1) / K_t * (KPerBlock / BK1Value);
301 auto K_t = K_Batch * KPerBlock;
302 return (K + K_t - 1) / K_t * KPerBlock;
308 auto K_t = K_Batch * KReadVec;
309 return (K + K_t - 1) / K_t * KReadVec;
322 template <
index_t MNXdlPerWave,
327 typename TileDesc_K0_MN_K1>
370 IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
372 const auto a_grid_desc_mraw_kraw = [&]() {
373 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
377 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
385 if constexpr(GemmSpec == GemmSpecialization::MKPadding ||
386 GemmSpec == GemmSpecialization::MNKPadding)
389 const auto a_grid_desc_m_k =
403 return a_grid_desc_ak0_m_ak1;
405 else if constexpr(GemmSpec == GemmSpecialization::MPadding ||
406 GemmSpec == GemmSpecialization::MNPadding)
410 a_grid_desc_mraw_kraw,
416 return a_grid_desc_ak0_m_ak1;
418 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
419 GemmSpec == GemmSpecialization::NKPadding)
423 a_grid_desc_mraw_kraw,
435 return a_grid_desc_ak0_m_ak1;
441 a_grid_desc_mraw_kraw,
448 a_grid_desc_ak0_m_ak1,
456 a_grid_desc_permuted,
478 const auto b_grid_desc_nraw_kraw = [&]() {
492 GemmSpec != GemmSpecialization::Default),
493 "pk_i4_t does not support padding");
495 GemmSpec != GemmSpecialization::Default),
496 "f4x2_pk_t does not support padding");
498 if constexpr(GemmSpec == GemmSpecialization::NKPadding ||
499 GemmSpec == GemmSpecialization::MNKPadding)
502 const auto b_grid_desc_n_k =
516 return b_grid_desc_bk0_n_bk1;
518 else if constexpr(GemmSpec == GemmSpecialization::NPadding ||
519 GemmSpec == GemmSpecialization::MNPadding)
523 b_grid_desc_nraw_kraw,
529 return b_grid_desc_bk0_n_bk1;
531 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
532 GemmSpec == GemmSpecialization::MKPadding)
536 b_grid_desc_nraw_kraw,
548 return b_grid_desc_bk0_n_bk1;
554 b_grid_desc_nraw_kraw,
561 b_grid_desc_bk0_n_bk1,
569 b_grid_desc_permuted,
581 template <
typename ABlockDesc_AK0_M_AK1>
582 __host__ __device__
static constexpr
auto
585 constexpr
index_t MWaves = MPerBlock / (MXdlPerWave * MPerXdl);
587 return MakeGemmMmaTileDescriptor<MXdlPerWave, MWaves, MXdlPack, MPerXdl, true>(
588 ABlockDesc_AK0_M_AK1{});
591 template <
typename BBlockDesc_BK0_N_BK1>
592 __host__ __device__
static constexpr
auto
595 constexpr
index_t NWaves = NPerBlock / (NXdlPerWave * NPerXdl);
597 return MakeGemmMmaTileDescriptor<NXdlPerWave, NWaves, NXdlPack, NPerXdl, false>(
598 BBlockDesc_BK0_N_BK1{});
601 template <
typename ELayout>
603 IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
605 const auto c_grid_desc_mraw_nraw = [&]() {
624 template <
typename DLayout>
625 __host__ __device__
static auto
628 const auto c_grid_desc_mraw_nraw = [&]() {
653 return MakeDGridDescriptor_M_N<DLayout>(M, MPad, N, NPad, StrideDs[i]);
658 template <
typename DsGr
idDesc>
660 const DsGridDesc& ds_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
665 ds_grid_desc_m_n[i], MBlock, NBlock);
681 std::array<index_t, NumDTensor> StrideDs_,
711 std::cout <<
"problem {"
713 <<
"TopK:" <<
TopK <<
", "
724 <<
"KRead:" <<
KRead <<
", "
726 <<
"AK0:" <<
AK0 <<
", "
727 <<
"BK0:" <<
BK0 <<
", "
728 <<
"MBlock: " <<
MBlock <<
", "
729 <<
"NBlock: " <<
NBlock <<
"}" << std::endl;
761 const index_t* p_sorted_expert_ids_,
762 const index_t* p_max_token_id_,
763 const ADataType* p_a_grid_,
764 const AScaleDataType* p_a_scale_grid_,
765 const BDataType* p_b_grid_,
766 const BScaleDataType* p_b_scale_grid_,
767 std::array<const void*, NumDTensor> p_ds_grid_,
768 CDataType* p_c_grid_,
778 std::array<index_t, NumDTensor> StrideDs_,
781 AElementwiseOperation a_element_op_,
782 BElementwiseOperation b_element_op_,
783 CElementwiseOperation c_element_op_)
815 p_ds_grid(i) =
static_cast<const DDataType_*
>(p_ds_grid_[i]);
838 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
842 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
847 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
851 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
865 if(k_id < karg.
KBatch - 1)
895 constexpr
auto a_lds_block_desc =
907 return a_lds_block_desc_permuted;
914 constexpr
auto WaveSize = 64;
915 constexpr
auto M0 = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
916 constexpr
auto M1 = MPerBlock / M0;
918 constexpr
auto KThreadWrite = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
919 constexpr
auto K0PerThreadWrite =
AK0Number / KThreadWrite;
920 constexpr
auto KThreadRead = WaveSize / MPerXdl;
921 constexpr
auto K0PerThreadRead =
AK0Number / KThreadRead;
923 constexpr
auto kfold = (
AK1Number * M0 *
sizeof(ADataType) > 128)
925 : 128 / (
AK1Number * M0 *
sizeof(ADataType));
926 constexpr
auto KThreadReadPerm =
927 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
928 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
932 constexpr
auto mpair = (
AK1Number * MPerXdl *
sizeof(ADataType) > 128)
934 : ((128 / (
AK1Number * MPerXdl *
sizeof(ADataType))) > M0
936 : 128 / (
AK1Number * MPerXdl *
sizeof(ADataType)));
942 Number<kfold * M0 / mpair>{},
961 a_lds_block_desc_permuted,
983 a_lds_block_desc_unmerged,
986 Number<KThreadWrite / kfold / KThreadReadPerm>{},
995 return a_lds_block_desc_ak0_m_ak1;
1011 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1013 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1020 return c_shuffle_block_desc_mblock_mperblock_nblock_nperblock;
1041 ABlockTransferSrcScalarPerVector,
1042 BBlockTransferSrcScalarPerVector,
1061 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1064 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1067 constexpr
auto c_block_size =
1068 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize();
1070 return math::max(a_block_space_size_aligned *
sizeof(ADataType),
1071 c_block_size *
sizeof(CShuffleDataType));
1077 static_assert((MPerBlock % (MPerXdl * MXdlPerWave) == 0) &&
1078 (NPerBlock % (NXdlPerWave * NPerXdl)) == 0,
1079 "Invalid tuning param!");
1081 static_assert(KPerBlock % (ScaleBlockSize /
BPackedSize) == 0,
1082 "KPerBlock should be multiple of ScaleBlockSize");
1090 if(!(karg.
M % MPerBlock == 0))
1094 std::cout <<
"Arg M value is not a multiple of MPerBlock! M: " << karg.
M <<
" "
1095 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1108 if(!(karg.
N % NPerBlock == 0))
1112 std::cout <<
"Arg N value is not a multiple of NPerBlock! N: " << karg.
N <<
" "
1113 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1125 auto K_t = karg.
KBatch * KPerBlock;
1126 if(!(karg.
K % K_t == 0))
1130 std::cout <<
"Arg K value is not a multiple of K_Batch * K0PerBlock * K1! K: "
1131 << karg.
K <<
" " << __FILE__ <<
":" << __LINE__
1132 <<
", in function: " << __func__ << std::endl;
1140 auto K_t = karg.
KBatch * KReadVec;
1142 if((KReadPadSplited * (karg.
KBatch - 1)) >= karg.
K)
1150 if(karg.
K % ABlockTransferSrcScalarPerVector != 0)
1154 std::cout <<
"Arg K (" << karg.
K
1155 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1156 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1157 << __LINE__ <<
", in function: " << __func__ << std::endl;
1164 if(karg.
M % ABlockTransferSrcScalarPerVector != 0)
1168 std::cout <<
"Arg M (" << karg.
M
1169 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1170 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1171 << __LINE__ <<
", in function: " << __func__ << std::endl;
1179 if(karg.
N % BBlockTransferSrcScalarPerVector != 0)
1183 std::cout <<
"Arg N (" << karg.
N
1184 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1185 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1186 << __LINE__ <<
", in function: " << __func__ << std::endl;
1193 if(karg.
K % BBlockTransferSrcScalarPerVector != 0)
1197 std::cout <<
"Arg K (" << karg.
K
1198 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1199 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1200 << __LINE__ <<
", in function: " << __func__ << std::endl;
1212 std::cout <<
"Arg N (" << karg.
N
1213 <<
") value is not a multiple of "
1214 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1216 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1228 std::cout <<
"Arg M (" << karg.
M
1229 <<
") value is not a multiple of "
1230 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1232 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1242 const auto num_k_loop = karg.
AK0 / (KPerBlock / AK1Value);
1244 if(num_k_loop <= BlockwiseGemmPipe::PrefetchStages)
1255 const index_t num_loop = K / KPerBlock;
1257 return BlockwiseGemmPipe::BlockHasHotloop(num_loop);
1262 const index_t num_loop = K / KPerBlock;
1264 return BlockwiseGemmPipe::BlockLoopTailNum(num_loop);
1267 template <
typename CGr
idDesc>
1269 const CGridDesc& c_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
1278 return c_grid_desc_mblock_mperblock_nblock_nperblock;
1287 template <
bool HasMainKBlockLoop,
1290 __device__
static void Run(
const index_t* p_sorted_token_ids,
1291 const index_t* p_sorted_expert_ids,
1292 const index_t* p_max_token_id,
1293 const ADataType* p_a_grid,
1294 const AScaleDataType* p_a_scale_grid,
1295 const BDataType* p_b_grid,
1296 const BScaleDataType* p_b_scale_grid,
1298 CDataType* p_c_grid,
1300 const Problem& problem,
1301 AElementwiseOperation a_element_op,
1302 BElementwiseOperation b_element_op,
1303 CElementwiseOperation c_element_op)
1307 IsInputGemm ? problem.NumTokens : problem.NumTokens * problem.TopK,
1313 const auto b_grid_desc_bpreshuffled =
1315 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
1316 IsInputGemm ? problem.NumTokens * problem.TopK : problem.NumTokens,
1334 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
1336 c_grid_desc_m_n, problem.MBlock, problem.NBlock);
1337 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
1339 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.NBlock : blockIdx.y;
1340 if(expert_block_id * MPerBlock >= max_token_id)
1343 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
1345 const auto block_mn = [&]() -> std::pair<int, int> {
1346 if constexpr(NSwizzle)
1348 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
1349 const index_t prefix_block = ecnt_prefix * problem.NBlock;
1350 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
1351 const index_t expert_swizzle =
1352 ecnt > 0 ? ecnt : 1;
1353 const index_t bid_new = blockIdx.x - prefix_block;
1354 const index_t nid = __builtin_amdgcn_readfirstlane(
1355 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
1357 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
1362 return {blockIdx.x, blockIdx.y};
1366 const index_t block_n_id = block_mn.first;
1367 const index_t block_m_id = block_mn.second;
1369 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
1372 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
1373 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
1374 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
1375 constexpr
auto AKThreads = AK0Threads * AK1Threads;
1376 constexpr
auto AMRepeats = MPerBlock / AMThreads;
1377 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
1379 if(token_pos >= max_token_id || token0 >= problem.NumTokens)
1381 StaticallyIndexedArray<IndexType, AMRepeats> gather_offsets;
1382 static_for<0, AMRepeats, 1>{}([&](
auto m0) {
1383 const index_t fused_token = p_sorted_token_ids[token_pos + m0];
1384 index_t token_offset = fused_token & 0xffffff;
1385 if constexpr(!IsInputGemm)
1387 token_offset = token_offset * problem.TopK + (fused_token >> 24);
1389 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.K /
APackedSize;
1392 __builtin_amdgcn_readfirstlane(problem.N * problem.K * (IsInputGemm ? 2 : 1));
1393 const index_t expert_scale_stride =
1394 __builtin_amdgcn_readfirstlane(problem.N * (IsInputGemm ? 2 : 1) *
1398 const index_t n_block_data_idx_on_grid =
1399 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave);
1401 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1402 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
1403 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1404 p_b_grid + expert_id * expert_stride /
BPackedSize,
1405 b_grid_desc_bpreshuffled.GetElementSpaceSize());
1408 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1409 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
1410 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1411 p_b_scale_grid + expert_id * expert_scale_stride,
1412 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1421 auto a_blockwise_copy = ThreadGroupTensorSliceTransfer_v4r1_gather<
1423 AElementwiseOperation,
1426 Sequence<AK0Number, MPerBlock, AK1Number>,
1427 ABlockTransferThreadClusterLengths_AK0_M_AK1,
1428 ABlockTransferThreadClusterArrangeOrder,
1431 decltype(a_grid_desc_ak0_m_ak1),
1432 decltype(a_block_desc_ak0_m_ak1),
1433 ABlockTransferSrcAccessOrder,
1435 ABlockTransferSrcVectorDim,
1437 ABlockTransferSrcScalarPerVector,
1438 ABlockTransferDstScalarPerVector_AK1,
1441 AThreadTransferSrcResetCoordinateAfterRun,
1445 BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
1448 a_block_desc_ak0_m_ak1,
1455 auto b_block_buf = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
1456 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1458 auto b_blockwise_copy =
1459 ThreadwiseTensorSliceTransfer_v2<BDataType,
1461 decltype(b_grid_desc_bpreshuffled),
1462 decltype(b_block_desc_bk0_n_bk1),
1467 Number<BK1Value>{}>,
1468 Sequence<1, 2, 0, 3>,
1470 BBlockTransferSrcScalarPerVector,
1471 BThreadTransferSrcResetCoordinateAfterRun,
1473 b_grid_desc_bpreshuffled,
1481 auto a_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1483 a_block_desc_ak0_m_ak1.GetElementSpaceSize() /
APackedSize);
1489 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
1491 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
1492 decltype(c_thread_buf) c_thread_buf_up;
1496 c_thread_buf.num_of_v_,
1497 c_thread_buf.s_per_v,
1501 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
1502 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
1506 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
1507 const auto waveId_m = wave_idx[
I0];
1508 const auto waveId_n = wave_idx[
I1];
1510 static constexpr
auto mfma = BlockwiseGemmPipe::xdlops_gemm.mfma;
1512 auto thread_offset_shuffled =
1515 auto a_thread_offset_m = waveId_m;
1517 auto a_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1520 decltype(a_scale_grid_desc_am_ak),
1521 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
1527 true>(a_scale_grid_desc_am_ak,
1533 auto b_thread_offset_n = waveId_n;
1535 auto b_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1538 decltype(b_scale_grid_desc_bn_ak),
1539 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1545 true>(b_scale_grid_desc_bn_ak,
1550 if constexpr(IsInputGemm)
1552 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2 /
BPackedSize;
1553 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1554 p_b_grid_up + expert_id * expert_stride /
BPackedSize,
1555 b_grid_desc_bpreshuffled.GetElementSpaceSize());
1556 auto b_blockwise_copy_up = ThreadwiseTensorSliceTransfer_v2<
1559 decltype(b_grid_desc_bpreshuffled),
1560 decltype(b_block_desc_bk0_n_bk1),
1561 Sequence<Number<NXdlPerWave>{},
I1, Number<KRepeat>{}, Number<BK1Value>{}>,
1562 Sequence<1, 2, 0, 3>,
1564 BBlockTransferSrcScalarPerVector,
1565 BThreadTransferSrcResetCoordinateAfterRun,
1566 true>(b_grid_desc_bpreshuffled,
1571 const BScaleDataType* p_b_scale_grid_up = p_b_scale_grid + expert_scale_stride / 2;
1572 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1573 p_b_scale_grid_up + expert_id * expert_scale_stride,
1574 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1575 auto b_scale_thread_copy_up = ThreadwiseTensorSliceTransfer_v2<
1578 decltype(b_scale_grid_desc_bn_ak),
1579 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1586 b_scale_grid_desc_bn_ak,
1591 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1592 a_grid_desc_ak0_m_ak1,
1593 a_block_desc_ak0_m_ak1,
1597 a_block_slice_copy_step,
1598 b_grid_desc_bpreshuffled,
1599 b_block_desc_bk0_n_bk1,
1601 b_blockwise_copy_up,
1605 b_block_slice_copy_step,
1608 a_scale_grid_desc_am_ak,
1609 a_scale_thread_copy,
1611 b_scale_grid_desc_bn_ak,
1612 b_scale_thread_copy,
1613 b_scale_thread_copy_up,
1615 b_scale_grid_buf_up,
1616 num_k_block_main_loop);
1620 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1621 a_grid_desc_ak0_m_ak1,
1622 a_block_desc_ak0_m_ak1,
1626 a_block_slice_copy_step,
1627 b_grid_desc_bpreshuffled,
1628 b_block_desc_bk0_n_bk1,
1632 b_block_slice_copy_step,
1634 a_scale_grid_desc_am_ak,
1635 a_scale_thread_copy,
1637 b_scale_grid_desc_bn_ak,
1638 b_scale_thread_copy,
1640 num_k_block_main_loop);
1645 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
1646 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
1650 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
1651 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
1655 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
1656 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
1658 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
1659 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
1660 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
1661 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
1662 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
1663 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
1664 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
1665 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
1668 static_assert(M0 * M1 * M2 * M3 * M4 == MPerBlock);
1669 static_assert(M4 == 4);
1673 vector_type<float, 4> topk_weights;
1674 static_for<0, NXdlPerWave, 1>{}([&](
auto n0) {
1675 static_for<0, MXdlPerWave, 1>{}([&](
auto m0) {
1676 static_for<0, M2, 1>{}([&](
auto m2) {
1677 const index_t m_pos = block_m_id * MPerBlock + m0 * M1 * M2 * M3 * M4 +
1678 m1 * M2 * M3 * M4 + m2 * M3 * M4 + m3 * M4;
1679 if constexpr(MulRoutedWeight)
1681 topk_weights = *c_style_pointer_cast<const vector_type<float, M4>*>(
1682 p_ds_grid[
I2] + m_pos);
1684 static_for<0, M4, 1>{}([&](
auto m4) {
1686 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
1688 constexpr
auto cidx = Number<c_offset>{};
1690 if constexpr(IsInputGemm)
1694 float gate = c_thread_buf[cidx];
1695 float up = c_thread_buf_up[cidx];
1696 if constexpr(MulRoutedWeight)
1698 gate = gate * topk_weights.AsType<
float>()[m4];
1699 up = up * topk_weights.AsType<
float>()[m4];
1701 tensor_operation::element_wise::Silu{}(gate, gate);
1702 c_thread_buf_fp32(cidx) = gate * up;
1706 float gate = c_thread_buf[cidx];
1707 float up = c_thread_buf_up[cidx];
1708 if constexpr(MulRoutedWeight)
1710 gate = gate * topk_weights.AsType<
float>()[m4];
1711 up = up * topk_weights.AsType<
float>()[m4];
1713 tensor_operation::element_wise::Gelu{}(gate, gate);
1714 c_thread_buf_fp32(cidx) = gate * up;
1719 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
1720 if constexpr(MulRoutedWeight)
1722 c_thread_buf_fp32(cidx) =
1723 topk_weights.AsType<
float>()[m4] * c_thread_buf_fp32[cidx];
1731 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1734 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1735 static_cast<CShuffleDataType*
>(p_shared),
1736 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1739 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
1743 Number<CShuffleMXdlPerWavePerShuffle>{},
1750 Number<CShuffleNXdlPerWavePerShuffle>{},
1753 make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}),
1755 Sequence<>{}, Sequence<0, 2, 4, 5, 6>{}, Sequence<>{}, Sequence<1, 3, 7>{}));
1759 const auto c_thread_mtx_on_block =
1760 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
1762 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
1763 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
1765 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
1771 const auto m_thread_data_on_block_idx =
1772 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
1775 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
1781 const auto n_thread_data_on_block_idx =
1782 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
1786 auto c_thread_copy_vgpr_to_lds =
1787 ThreadwiseTensorSliceTransfer_v1r3<AccDataType,
1789 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1790 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1792 Sequence<CShuffleMXdlPerWavePerShuffle,
1793 CShuffleNXdlPerWavePerShuffle,
1800 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
1806 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1809 m_thread_data_on_block_idx[
I1],
1810 n_thread_data_on_block_idx[
I1],
1811 m_thread_data_on_block_idx[
I2],
1812 m_thread_data_on_block_idx[
I3],
1813 m_thread_data_on_block_idx[
I4],
1814 n_thread_data_on_block_idx[
I2]),
1817 using EDataType = CDataType;
1820 problem.M, problem.MPadded, problem.N, problem.NPadded, problem.StrideDs);
1822 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
1824 ds_grid_desc_m_n, problem.MBlock, problem.NBlock);
1828 return make_dynamic_buffer<AddressSpaceEnum::Global>(
1829 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
1831 Number<NumDTensor>{});
1835 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
1837 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
1838 Number<NumDTensor>{}));
1842 tie(c_shuffle_block_buf),
1844 {
return ds_grid_buf[i]; },
1845 Number<NumDTensor>{}));
1848 const auto idx_c_ds_block_begin =
1856 Number<NumDTensor>{}));
1858 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
1859 c_grid_desc_mblock_mperblock_nblock_nperblock;
1861 using CDEBlockTransferCluster =
1862 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
1863 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
1864 constexpr
index_t scatter_weight_idx = 1;
1865 auto cde_block_copy_lds_and_global = ThreadGroupTensorSliceTransfer_v7r3_scatter<
1869 decltype(c_ds_desc_refs),
1870 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
1871 CElementwiseOperation,
1872 Sequence<static_cast<index_t>(EGlobalMemoryDataOperation)>,
1875 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1877 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>,
1878 CDEBlockTransferCluster,
1879 Sequence<0, 1, 2, 3>,
1880 Sequence<0, 1, 2, 3>,
1881 Sequence<0, 1, 2, 3>,
1884 CDEShuffleBlockTransferScalarPerVectors,
1896 idx_c_ds_block_begin,
1897 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1901 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1902 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1903 constexpr
auto sfc_c_vgpr =
1904 SpaceFillingCurve<Sequence<MXdlPerWave, NXdlPerWave, 1, 1, M2, 1, M4, 1>,
1905 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
1906 Sequence<CShuffleMXdlPerWavePerShuffle,
1907 CShuffleNXdlPerWavePerShuffle,
1915 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
1918 constexpr
auto sfc_cde_block =
1919 SpaceFillingCurve<Sequence<1, MPerBlock, 1, NPerBlock>,
1920 Sequence<0, 2, 1, 3>,
1922 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1924 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>>{};
1926 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
1927 constexpr
auto EMThreads =
1928 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
1929 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
1930 constexpr
auto ENThreads =
1931 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
1932 static_for<0, num_access, 1>{}([&](
auto access_id) {
1934 StaticallyIndexedArray<IndexType, EMRepeats> scatter_offsets;
1936 auto dstidx = sfc_cde_block.GetIndex(access_id);
1938 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
1939 static_for<0, EMRepeats, 1>{}([&](
auto m0) {
1940 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
1941 IndexType token_offset = fused_token & 0xffffff;
1942 if constexpr(IsInputGemm)
1944 token_offset = token_offset * problem.TopK + (fused_token >> 24);
1946 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.N;
1952 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1953 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
1955 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1956 c_shuffle_block_buf);
1962 cde_block_copy_lds_and_global.Run(
1965 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1969 if constexpr(access_id < num_access - 1)
1971 constexpr
auto cde_lds_and_global_step =
1972 sfc_cde_block.GetForwardStep(access_id);
1975 static_for<0, NumDTensor, 1>{}([&](
auto i) {
1976 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
1977 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
1981 cde_block_copy_lds_and_global.MoveDstSliceWindow(
1982 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1984 cde_lds_and_global_step);
1991 template <
bool HasMainKBlockLoop,
1995 const index_t* p_sorted_expert_ids,
1996 const index_t* p_max_token_id,
1997 const ADataType* p_a_grid,
1998 const AScaleDataType* p_a_scale_grid,
1999 const BDataType* p_b_grid,
2000 const BScaleDataType* p_b_scale_grid,
2002 CDataType* p_c_grid,
2006 AElementwiseOperation a_element_op,
2007 BElementwiseOperation b_element_op,
2008 CElementwiseOperation c_element_op)
2019 const auto b_grid_desc_bpreshuffled =
2021 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
2029 const auto Padded_Scale_M =
2053 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
2057 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
2058 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
2059 if(expert_block_id * MPerBlock >= max_token_id)
2062 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
2063 const auto block_mn = [&]() -> std::pair<int, int> {
2064 if constexpr(NSwizzle)
2066 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
2068 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
2069 const index_t expert_swizzle =
2070 ecnt > 0 ? ecnt : 1;
2071 const index_t bid_new = blockIdx.x - prefix_block;
2072 const index_t nid = __builtin_amdgcn_readfirstlane(
2073 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
2075 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
2080 return {blockIdx.x, blockIdx.y};
2084 const index_t block_n_id = block_mn.first;
2085 const index_t block_m_id = block_mn.second;
2087 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
2090 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
2091 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
2092 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
2093 constexpr
auto AKThreads = AK0Threads * AK1Threads;
2094 constexpr
auto AMRepeats = MPerBlock / AMThreads;
2095 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads;
2097 if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
2101 const index_t fused_token = p_sorted_token_ids[token_pos + m0 * AMThreads];
2102 index_t token_offset = fused_token & 0xffffff;
2103 if constexpr(!IsInputGemm)
2105 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2107 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
K;
2111 __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
2112 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
2113 problem.
N * (IsInputGemm ? 2 : 1) *
2117 const index_t n_block_data_idx_on_grid =
2118 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave /
NXdlPack);
2121 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2122 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
2123 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2124 p_b_grid + expert_id * expert_stride, b_grid_desc_bpreshuffled.GetElementSpaceSize());
2127 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2128 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
2129 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2130 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
2131 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2143 ABlockTransferThreadClusterLengths_AK0_M_AK1,
2144 ABlockTransferThreadClusterArrangeOrder,
2147 decltype(a_grid_desc_ak0_m_ak1),
2148 decltype(a_block_desc_ak0_m_ak1),
2149 ABlockTransferSrcAccessOrder,
2150 ABlockTransferSrcVectorDim,
2152 ABlockTransferSrcScalarPerVector,
2154 1>(a_grid_desc_ak0_m_ak1,
2156 a_block_desc_ak0_m_ak1,
2162 auto b_block_buf_ping = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2163 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2164 auto b_block_buf_pong = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2165 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2166 auto b_block_bufs =
make_tuple(b_block_buf_ping, b_block_buf_pong);
2168 auto b_blockwise_copy =
2171 decltype(b_grid_desc_bpreshuffled),
2172 decltype(b_block_desc_bk0_n_bk1),
2180 BBlockTransferSrcScalarPerVector,
2181 BThreadTransferSrcResetCoordinateAfterRun,
2183 b_grid_desc_bpreshuffled,
2192 auto a_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2193 static_cast<ADataType*
>(p_shared_0), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2194 auto a_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2195 static_cast<ADataType*
>(p_shared_1), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2196 auto a_block_bufs =
make_tuple(a_block_buf_ping, a_block_buf_pong);
2202 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
2204 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
2205 decltype(c_thread_buf) c_thread_buf_up;
2209 c_thread_buf.num_of_v_,
2210 c_thread_buf.s_per_v,
2214 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
2215 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
2219 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
2220 const auto waveId_m = wave_idx[
I0];
2221 const auto waveId_n = wave_idx[
I1];
2223 auto thread_offset_shuffled =
2226 auto a_thread_offset_m = waveId_m;
2229 const index_t token_scale_pos = block_m_id * MPerBlock;
2230 if(token_scale_pos >= max_token_id || token0 >= problem.
NumTokens)
2236 decltype(a_scale_grid_desc_am_ak),
2237 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
2243 true>(a_scale_grid_desc_am_ak,
2249 auto b_thread_offset_n = waveId_n;
2254 decltype(b_scale_grid_desc_bn_ak),
2255 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2261 true>(b_scale_grid_desc_bn_ak,
2266 if constexpr(IsInputGemm)
2268 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
2269 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2270 p_b_grid_up + expert_id * expert_stride,
2271 b_grid_desc_bpreshuffled.GetElementSpaceSize());
2272 auto b_blockwise_copy_up =
2275 decltype(b_grid_desc_bpreshuffled),
2276 decltype(b_block_desc_bk0_n_bk1),
2284 BBlockTransferSrcScalarPerVector,
2285 BThreadTransferSrcResetCoordinateAfterRun,
2287 b_grid_desc_bpreshuffled,
2293 const BScaleDataType* p_b_scale_grid_up =
2294 p_b_scale_grid + expert_scale_stride / 2 /
sizeof(BScaleDataType);
2295 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2296 p_b_scale_grid_up + expert_id * expert_scale_stride /
sizeof(BScaleDataType),
2297 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2302 decltype(b_scale_grid_desc_bn_ak),
2303 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2310 b_scale_grid_desc_bn_ak,
2315 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2317 a_grid_desc_ak0_m_ak1,
2318 a_block_desc_ak0_m_ak1,
2322 a_block_slice_copy_step,
2324 b_grid_desc_bpreshuffled,
2325 b_block_desc_bk0_n_bk1,
2327 b_blockwise_copy_up,
2331 b_block_slice_copy_step,
2336 a_scale_grid_desc_am_ak,
2337 a_scale_thread_copy,
2340 b_scale_grid_desc_bn_ak,
2341 b_scale_thread_copy,
2342 b_scale_thread_copy_up,
2344 b_scale_grid_buf_up,
2345 num_k_block_main_loop);
2349 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2350 a_grid_desc_ak0_m_ak1,
2351 a_block_desc_ak0_m_ak1,
2355 a_block_slice_copy_step,
2356 b_grid_desc_bpreshuffled,
2357 b_block_desc_bk0_n_bk1,
2361 b_block_slice_copy_step,
2363 a_scale_grid_desc_am_ak,
2364 a_scale_thread_copy,
2366 b_scale_grid_desc_bn_ak,
2367 b_scale_thread_copy,
2369 num_k_block_main_loop);
2374 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
2375 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
2377 static_assert(CShuffleMXdlPerWavePerShuffle %
MXdlPack == 0 &&
2378 CShuffleNXdlPerWavePerShuffle %
NXdlPack == 0,
2381 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
2384 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
2385 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2389 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
2390 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2392 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
2393 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
2394 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
2395 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
2396 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
2397 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
2398 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
2399 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
2400 constexpr
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
2401 constexpr
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
2405 static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
2406 static_assert(M5 == 4);
2416 const index_t m_pos = block_m_id * MPerBlock +
2417 m0 * M2 * M1 * M3 * M4 * M5 +
2418 m1 * M2 * M3 * M4 * M5 +
2419 imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
2420 if constexpr(MulRoutedWeight)
2423 *c_style_pointer_cast<const vector_type<float, M5>*>(
2424 p_ds_grid[
I2] + m_pos);
2428 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
2429 make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
2432 if constexpr(IsInputGemm)
2434 if constexpr(ActivationOperation ==
2437 float gate = c_thread_buf[cidx];
2438 float up = c_thread_buf_up[cidx];
2439 if constexpr(MulRoutedWeight)
2441 gate = gate * topk_weights.AsType<
float>()[m5];
2442 up = up * topk_weights.AsType<
float>()[m5];
2445 c_thread_buf_fp32(cidx) = gate * up;
2449 float gate = c_thread_buf[cidx];
2450 float up = c_thread_buf_up[cidx];
2451 if constexpr(MulRoutedWeight)
2453 gate = gate * topk_weights.AsType<
float>()[m5];
2454 up = up * topk_weights.AsType<
float>()[m5];
2457 c_thread_buf_fp32(cidx) = gate * up;
2462 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
2463 if constexpr(MulRoutedWeight)
2465 c_thread_buf_fp32(cidx) =
2466 topk_weights.AsType<
float>()[m5] *
2467 c_thread_buf_fp32[cidx];
2477 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
2480 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2481 static_cast<CShuffleDataType*
>(p_shared_0),
2482 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2485 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
2511 const auto c_thread_mtx_on_block =
2512 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
2514 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
2515 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
2517 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
2523 const auto m_thread_data_on_block_idx =
2524 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
2527 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
2533 const auto n_thread_data_on_block_idx =
2534 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
2541 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2542 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2545 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2554 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2559 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2562 m_thread_data_on_block_idx[
I1],
2563 n_thread_data_on_block_idx[
I1],
2564 m_thread_data_on_block_idx[
I2],
2565 n_thread_data_on_block_idx[
I2],
2566 m_thread_data_on_block_idx[
I3],
2567 m_thread_data_on_block_idx[
I4],
2568 m_thread_data_on_block_idx[
I5],
2569 n_thread_data_on_block_idx[
I3]),
2572 using EDataType = CDataType;
2577 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
2583 return make_dynamic_buffer<AddressSpaceEnum::Global>(
2584 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
2590 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
2592 [&](
auto i) ->
const auto&
2593 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
2598 tie(c_shuffle_block_buf),
2600 [&](
auto i) ->
const auto&
2601 {
return ds_grid_buf[i]; },
2605 const auto idx_c_ds_block_begin =
2615 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
2616 c_grid_desc_mblock_mperblock_nblock_nperblock;
2618 using CDEBlockTransferCluster =
2619 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
2620 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
2621 constexpr
index_t scatter_weight_idx = 3;
2626 decltype(c_ds_desc_refs),
2627 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
2628 CElementwiseOperation,
2633 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2635 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>,
2636 CDEBlockTransferCluster,
2642 CDEShuffleBlockTransferScalarPerVectors,
2654 idx_c_ds_block_begin,
2655 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2659 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2660 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2662 constexpr
auto sfc_c_vgpr =
2673 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2675 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2685 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2688 constexpr
auto sfc_cde_block =
2692 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2694 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>>{};
2696 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2697 constexpr
auto EMThreads =
2698 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2699 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2700 constexpr
auto ENThreads =
2701 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2706 auto dstidx = sfc_cde_block.GetIndex(access_id);
2708 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2710 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2711 IndexType token_offset = fused_token & 0xffffff;
2712 if constexpr(IsInputGemm)
2714 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2716 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
N;
2722 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2723 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2725 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2726 c_shuffle_block_buf);
2732 cde_block_copy_lds_and_global.Run(
2735 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2739 if constexpr(access_id < num_access - 1)
2741 constexpr
auto cde_lds_and_global_step =
2742 sfc_cde_block.GetForwardStep(access_id);
2746 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2747 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2751 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2752 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2754 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
constexpr auto BlockGemmMXBPreshufflePipeline_Selector()
Definition: blockwise_gemm_pipeline_xdlops_b_preshuffle_mx_moe_selector.hpp:36
__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
integral_constant< index_t, N > Number
Definition: number.hpp:12
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:759
const index_t * p_sorted_expert_ids
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:820
const index_t * p_sorted_token_ids
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:819
const BScaleDataType * p_b_scale_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:825
const BElementwiseOperation b_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:830
const BDataType * p_b_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:824
const index_t * p_max_token_id
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:821
DsGridPointer p_ds_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:826
const AElementwiseOperation a_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:829
CDataType * p_c_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:827
const AScaleDataType * p_a_scale_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:823
__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_bpreshuffle.hpp:760
const CElementwiseOperation c_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:831
const ADataType * p_a_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:822
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:671
index_t BN0Shuffled
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:753
index_t AK0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:748
index_t NPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:745
index_t BK0Shuffled
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:754
index_t KBatch
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:743
index_t BK0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:749
index_t TopK
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:733
__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_bpreshuffle.hpp:672
index_t KRead
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:746
index_t K
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:736
index_t MPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:744
index_t StrideC
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:742
index_t StrideScaleB
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:740
index_t NumTokens
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:732
std::array< index_t, NumDTensor > StrideDs
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:741
index_t MBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:750
index_t StrideScaleA
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:738
index_t N
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:735
__host__ void Print() const
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:709
index_t StrideA
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:737
index_t StrideB
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:739
index_t M
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:734
index_t KPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:747
index_t NBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:751
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:835
index_t a_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:877
index_t b_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:878
index_t a_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:875
__device__ SplitKBatchOffset(Argument &karg, index_t k_id)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:836
index_t b_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:876
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:171
static constexpr auto I6
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:181
static __host__ auto CalculateBN0Shuffled(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:273
static __host__ auto CalculateKPadded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:299
static constexpr auto AK1Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:191
static constexpr auto AK0Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:189
static constexpr __device__ index_t GetSharedMemoryNumberOfByte()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1053
decltype(MakeDsGridPointer()) DsGridPointer
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:249
static constexpr auto I1
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:176
static __host__ auto CalculateKPadded(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:282
static __host__ auto CalculateGridSize(index_t M, index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:253
__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_bpreshuffle.hpp:647
static __device__ void Run_2Lds(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_0, void *p_shared_1, const Problem &problem, AElementwiseOperation a_element_op, BElementwiseOperation b_element_op, CElementwiseOperation c_element_op)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1994
__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_bpreshuffle.hpp:1268
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_bpreshuffle.hpp:659
static constexpr index_t NLane
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:222
static constexpr __device__ auto GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:999
static constexpr index_t SortedTileSize
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:228
static constexpr index_t scale_pack_size_b
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:232
static constexpr auto BK1Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:192
remove_cvref_t< decltype(BlockGemmMXBPreshufflePipeline_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_bpreshuffle.hpp:1051
static constexpr __host__ TailNumber CalculateKBlockLoopTailNum(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1260
static __host__ auto CalculateNPadded(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:268
static constexpr auto MakeDsGridPointer()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:238
__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_bpreshuffle.hpp:369
static constexpr auto BK0Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:190
static constexpr auto I9
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:184
static constexpr auto I4
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:179
static constexpr auto I3
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:178
static __host__ auto CalculateKRead(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:305
static __host__ auto CalculateAK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:287
static __host__ auto CalculateMPadded(index_t M)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:263
static constexpr auto CShuffleBlockTransferScalarPerVector_NPerBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:186
static __host__ auto CalculateMBlock(index_t M)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:312
static constexpr index_t KRepeat
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:225
static constexpr auto NXdlPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:201
static constexpr __device__ auto GetCShuffleBlockDescriptor_MBlock_MPerBlock_NBlock_NPerBlock()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1009
static constexpr auto KXdlPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:202
BDataType LDSTypeB
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:173
__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_bpreshuffle.hpp:475
static constexpr auto I0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:175
static constexpr auto I5
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:180
static constexpr index_t scale_pack_size_a
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:231
static constexpr auto lcm_AK1_BK1
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:194
static constexpr index_t KLane
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:223
static constexpr __device__ auto GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:881
static constexpr auto MXdlPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:200
static constexpr auto I8
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:183
__host__ static constexpr __device__ auto MakeBMmaTileDescriptor_N0_N1_N2_N3_K(const BBlockDesc_BK0_N_BK1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:593
__host__ static __device__ auto MakeCGridDescriptor_M_N(IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:602
static constexpr bool is_single_rate_mfma
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:195
static constexpr auto I2
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:177
__host__ static constexpr __device__ auto MakeAMmaTileDescriptor_M0_M1_M2_M3_K(const ABlockDesc_AK0_M_AK1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:583
static constexpr index_t APackedSize
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:210
static constexpr index_t NumDTensor
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:198
static __host__ auto CalculateNBlock(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:317
static constexpr index_t BPackedSize
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:211
static constexpr index_t KPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:219
static constexpr __host__ bool CalculateHasMainKBlockLoop(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1253
static constexpr __host__ bool CheckValidity(const Argument &karg)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1075
ADataType LDSTypeA
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:172
static __host__ auto CalculateBK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:293
__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_bpreshuffle.hpp:626
__host__ static constexpr __device__ auto MakeGemmMmaTileDescriptor(const TileDesc_K0_MN_K1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:328
static __host__ auto CalculateBK0Shuffled(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:277
ThisThreadBlock< BlockSize > ThisThreadBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:251
static constexpr auto I7
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:182
__host__ static __device__ auto MakeBGridDescriptor_Preshuffled(index_t N0, index_t K0)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:468
static constexpr index_t NWave
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:224
static constexpr auto is_scale_mfma
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:196
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
Definition: thread_group_tensor_slice_transfer_gather_direct_load.hpp:57
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