39 template <
typename GridwiseGemm,
40 bool HasMainKBlockLoop,
45 #if CK_USE_LAUNCH_BOUNDS
52 __shared__
char p_shared[GridwiseGemm::GetSharedMemoryNumberOfByte()];
54 auto splitk_batch_offset =
typename GridwiseGemm::SplitKBatchOffset(karg, blockIdx.z);
56 GridwiseGemm::template Run<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
57 karg.p_sorted_token_ids,
58 karg.p_sorted_expert_ids,
60 karg.p_a_grid + splitk_batch_offset.a_k_split_offset,
61 karg.p_a_scale_grid + splitk_batch_offset.a_k_split_offset,
62 karg.p_b_grid + splitk_batch_offset.b_k_split_offset,
63 karg.p_b_scale_grid + splitk_batch_offset.b_k_split_offset,
77 template <
typename GridwiseGemm,
78 bool HasMainKBlockLoop,
83 #if CK_USE_LAUNCH_BOUNDS
90 __shared__
char p_shared_0[GridwiseGemm::GetSharedMemoryNumberOfByte()];
91 __shared__
char p_shared_1[GridwiseGemm::GetSharedMemoryNumberOfByte()];
93 auto splitk_batch_offset =
typename GridwiseGemm::SplitKBatchOffset(karg, blockIdx.z);
95 GridwiseGemm::template Run_2Lds<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
96 karg.p_sorted_token_ids,
97 karg.p_sorted_expert_ids,
99 karg.p_a_grid + splitk_batch_offset.a_k_split_offset,
100 karg.p_a_scale_grid + splitk_batch_offset.a_scale_k_split_offset,
101 karg.p_b_grid + splitk_batch_offset.b_k_split_offset,
102 karg.p_b_scale_grid + splitk_batch_offset.b_scale_k_split_offset,
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];
233 return static_cast<const DDataType*
>(
nullptr);
246 const index_t gridx = NSwizzle ? nblock * mblock : nblock;
247 const index_t gridy = NSwizzle ? 1 : mblock;
269 auto K_t = K_Batch * KPerBlock;
270 return (K + K_t - 1) / K_t * (KPerBlock / AK1Value);
275 auto K_t = K_Batch * KPerBlock;
276 return (K + K_t - 1) / K_t * (KPerBlock / BK1Value);
281 auto K_t = K_Batch * KPerBlock;
282 return (K + K_t - 1) / K_t * KPerBlock;
288 auto K_t = K_Batch * KReadVec;
289 return (K + K_t - 1) / K_t * KReadVec;
302 template <
index_t MNXdlPerWave,
306 typename TileDesc_K0_MN_K1>
332 IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
334 const auto a_grid_desc_mraw_kraw = [&]() {
335 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
339 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
347 if constexpr(GemmSpec == GemmSpecialization::MKPadding ||
348 GemmSpec == GemmSpecialization::MNKPadding)
351 const auto a_grid_desc_m_k =
365 return a_grid_desc_ak0_m_ak1;
367 else if constexpr(GemmSpec == GemmSpecialization::MPadding ||
368 GemmSpec == GemmSpecialization::MNPadding)
372 a_grid_desc_mraw_kraw,
379 a_grid_desc_ak0_m_ak1,
387 a_grid_desc_permuted,
396 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
397 GemmSpec == GemmSpecialization::NKPadding)
401 a_grid_desc_mraw_kraw,
413 return a_grid_desc_ak0_m_ak1;
419 a_grid_desc_mraw_kraw,
426 a_grid_desc_ak0_m_ak1,
434 a_grid_desc_permuted,
449 const auto b_grid_desc_nraw_kraw = [&]() {
463 GemmSpec != GemmSpecialization::Default),
464 "pk_i4_t does not support padding");
466 (GemmSpec != GemmSpecialization::Default &&
467 GemmSpec != GemmSpecialization::MPadding)),
468 "f4x2_pk_t does not support K padding");
470 if constexpr(GemmSpec == GemmSpecialization::NKPadding ||
471 GemmSpec == GemmSpecialization::MNKPadding)
474 const auto b_grid_desc_n_k =
488 return b_grid_desc_bk0_n_bk1;
490 else if constexpr(GemmSpec == GemmSpecialization::NPadding ||
491 GemmSpec == GemmSpecialization::MNPadding)
495 b_grid_desc_nraw_kraw,
501 return b_grid_desc_bk0_n_bk1;
503 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
504 GemmSpec == GemmSpecialization::MKPadding)
508 b_grid_desc_nraw_kraw,
520 return b_grid_desc_bk0_n_bk1;
526 b_grid_desc_nraw_kraw,
533 b_grid_desc_bk0_n_bk1,
541 b_grid_desc_permuted,
553 template <
typename ABlockDesc_AK0_M_AK1>
554 __host__ __device__
static constexpr
auto
557 constexpr
index_t MWaves = MPerBlock / (MXdlPerWave * MPerXdl);
559 return MakeGemmMmaTileDescriptor<MXdlPerWave, MWaves, MXdlPack, MPerXdl>(
560 ABlockDesc_AK0_M_AK1{});
563 template <
typename BBlockDesc_BK0_N_BK1>
564 __host__ __device__
static constexpr
auto
567 constexpr
index_t NWaves = NPerBlock / (NXdlPerWave * NPerXdl);
569 return MakeGemmMmaTileDescriptor<NXdlPerWave, NWaves, NXdlPack, NPerXdl>(
570 BBlockDesc_BK0_N_BK1{});
573 template <
typename ELayout>
575 IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
577 const auto c_grid_desc_mraw_nraw = [&]() {
596 template <
typename DLayout>
597 __host__ __device__
static auto
600 const auto c_grid_desc_mraw_nraw = [&]() {
625 return MakeDGridDescriptor_M_N<DLayout>(M, MPad, N, NPad, StrideDs[i]);
630 template <
typename DsGr
idDesc>
632 const DsGridDesc& ds_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
637 ds_grid_desc_m_n[i], MBlock, NBlock);
653 std::array<index_t, NumDTensor> StrideDs_,
681 std::cout <<
"problem {" <<
"NumTokens:" <<
NumTokens <<
", " <<
"TopK:" <<
TopK <<
", "
682 <<
"M:" <<
M <<
", " <<
"N:" <<
N <<
", " <<
"K:" <<
K <<
", "
686 <<
", " <<
"KRead:" <<
KRead <<
", " <<
"KP:" <<
KPadded <<
", "
687 <<
"AK0:" <<
AK0 <<
", " <<
"BK0:" <<
BK0 <<
", " <<
"MBlock: " <<
MBlock
688 <<
", " <<
"NBlock: " <<
NBlock <<
"}" << std::endl;
717 const index_t* p_sorted_expert_ids_,
718 const index_t* p_max_token_id_,
719 const ADataType* p_a_grid_,
720 const AScaleDataType* p_a_scale_grid_,
721 const BDataType* p_b_grid_,
722 const BScaleDataType* p_b_scale_grid_,
723 std::array<const void*, NumDTensor> p_ds_grid_,
724 CDataType* p_c_grid_,
734 std::array<index_t, NumDTensor> StrideDs_,
737 AElementwiseOperation a_element_op_,
738 BElementwiseOperation b_element_op_,
739 CElementwiseOperation c_element_op_)
771 p_ds_grid(i) =
static_cast<const DDataType_*
>(p_ds_grid_[i]);
794 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
798 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
803 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
807 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
814 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
818 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
825 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
830 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
835 if(k_id < karg.
KBatch - 1)
865 constexpr
auto a_lds_block_desc =
877 return a_lds_block_desc_permuted;
884 constexpr
auto WaveSize = 64;
885 constexpr
auto M0 = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
886 constexpr
auto M1 = MPerBlock / M0;
888 constexpr
auto KThreadWrite = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
889 constexpr
auto K0PerThreadWrite =
AK0Number / KThreadWrite;
890 constexpr
auto KThreadRead = WaveSize / MPerXdl;
891 constexpr
auto K0PerThreadRead =
AK0Number / KThreadRead;
893 constexpr
auto kfold = (
AK1Number * M0 *
sizeof(ADataType) > 128)
895 : 128 / (
AK1Number * M0 *
sizeof(ADataType));
896 constexpr
auto KThreadReadPerm =
897 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
898 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
902 constexpr
auto mpair = (
AK1Number * MPerXdl *
sizeof(ADataType) > 128)
904 : ((128 / (
AK1Number * MPerXdl *
sizeof(ADataType))) > M0
906 : 128 / (
AK1Number * MPerXdl *
sizeof(ADataType)));
912 Number<kfold * M0 / mpair>{},
931 a_lds_block_desc_permuted,
953 a_lds_block_desc_unmerged,
956 Number<KThreadWrite / kfold / KThreadReadPerm>{},
965 return a_lds_block_desc_ak0_m_ak1;
982 constexpr
auto b_lds_block_desc =
994 return b_lds_block_desc_permuted;
998 constexpr
auto WaveSize = 64;
999 constexpr
auto N0 = BBlockTransferThreadClusterLengths_BK0_N_BK1{}.At(
I1);
1000 constexpr
auto N1 = NPerBlock / N0;
1002 constexpr
auto KThreadWrite = BBlockTransferThreadClusterLengths_BK0_N_BK1{}.At(
I0);
1003 constexpr
auto K0PerThreadWrite =
BK0Number / KThreadWrite;
1004 constexpr
auto KThreadRead = WaveSize / NPerXdl;
1005 constexpr
auto K0PerThreadRead =
BK0Number / KThreadRead;
1007 constexpr
auto kfold = (
BK1Number * N0 *
sizeof(BDataType) > 128)
1009 : 128 / (
BK1Number * N0 *
sizeof(BDataType));
1010 constexpr
auto KThreadReadPerm =
1011 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
1012 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
1016 constexpr
auto npair = (
BK1Number * NPerXdl *
sizeof(BDataType) > 128)
1018 : ((128 / (
BK1Number * NPerXdl *
sizeof(BDataType))) > N0
1020 : 128 / (
BK1Number * NPerXdl *
sizeof(BDataType)));
1026 Number<kfold * N0 / npair>{},
1045 b_lds_block_desc_permuted,
1067 b_lds_block_desc_unmerged,
1070 Number<KThreadWrite / kfold / KThreadReadPerm>{},
1079 return b_lds_block_desc_bk0_n_bk1;
1085 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1086 constexpr
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
1088 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1095 return c_shuffle_block_desc_mblock_mperblock_nblock_nperblock;
1116 ABlockTransferSrcScalarPerVector,
1117 BBlockTransferSrcScalarPerVector,
1138 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1141 b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
1144 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1147 constexpr
auto c_block_size =
1148 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize();
1150 if constexpr(IsInputGemm)
1152 return math::max(a_block_space_size_aligned *
sizeof(ADataType) +
1153 b_block_space_size_aligned *
sizeof(BDataType) * 2,
1154 c_block_size *
sizeof(CShuffleDataType));
1158 return math::max((a_block_space_size_aligned *
sizeof(ADataType) +
1159 b_block_space_size_aligned *
sizeof(BDataType)),
1160 c_block_size *
sizeof(CShuffleDataType));
1167 static_assert((MPerBlock % (MPerXdl * MXdlPerWave) == 0) &&
1168 (NPerBlock % (NXdlPerWave * NPerXdl)) == 0,
1169 "Invalid tuning param!");
1171 static_assert(KPerBlock % (ScaleBlockSize /
BPackedSize) == 0,
1172 "KPerBlock should be multiple of ScaleBlockSize");
1180 if(!(karg.
M % MPerBlock == 0))
1184 std::cout <<
"Arg M value is not a multiple of MPerBlock! M: " << karg.
M <<
" "
1185 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1198 if(!(karg.
N % NPerBlock == 0))
1202 std::cout <<
"Arg N value is not a multiple of NPerBlock! N: " << karg.
N <<
" "
1203 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1215 auto K_t = karg.
KBatch * KPerBlock;
1216 if(!(karg.
K % K_t == 0))
1220 std::cout <<
"Arg K value is not a multiple of K_Batch * K0PerBlock * K1! K: "
1221 << karg.
K <<
" " << __FILE__ <<
":" << __LINE__
1222 <<
", in function: " << __func__ << std::endl;
1230 auto K_t = karg.
KBatch * KReadVec;
1232 if((KReadPadSplited * (karg.
KBatch - 1)) >= karg.
K)
1240 if(karg.
K % ABlockTransferSrcScalarPerVector != 0)
1244 std::cout <<
"Arg K (" << karg.
K
1245 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1246 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1247 << __LINE__ <<
", in function: " << __func__ << std::endl;
1254 if(karg.
M % ABlockTransferSrcScalarPerVector != 0)
1258 std::cout <<
"Arg M (" << karg.
M
1259 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1260 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1261 << __LINE__ <<
", in function: " << __func__ << std::endl;
1269 if(karg.
N % BBlockTransferSrcScalarPerVector != 0)
1273 std::cout <<
"Arg N (" << karg.
N
1274 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1275 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1276 << __LINE__ <<
", in function: " << __func__ << std::endl;
1283 if(karg.
K % BBlockTransferSrcScalarPerVector != 0)
1287 std::cout <<
"Arg K (" << karg.
K
1288 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1289 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1290 << __LINE__ <<
", in function: " << __func__ << std::endl;
1302 std::cout <<
"Arg N (" << karg.
N
1303 <<
") value is not a multiple of "
1304 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1306 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1318 std::cout <<
"Arg M (" << karg.
M
1319 <<
") value is not a multiple of "
1320 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1322 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1332 const auto num_k_loop = karg.
AK0 / (KPerBlock / AK1Value);
1334 if(num_k_loop <= BlockwiseGemmPipe::PrefetchStages)
1345 const index_t num_loop = K / KPerBlock;
1347 return BlockwiseGemmPipe::BlockHasHotloop(num_loop);
1352 const index_t num_loop = K / KPerBlock;
1354 return BlockwiseGemmPipe::BlockLoopTailNum(num_loop);
1357 template <
typename CGr
idDesc>
1359 const CGridDesc& c_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
1368 return c_grid_desc_mblock_mperblock_nblock_nperblock;
1380 "A scale pack data type too large!");
1382 "B scale pack data type too large!");
1384 static_assert(is_same_v<AElementwiseOperation, tensor_operation::element_wise::PassThrough> &&
1385 is_same_v<BElementwiseOperation, tensor_operation::element_wise::PassThrough>,
1386 "A/B ElementwiseOperation should be PassThrough as load_to_lds is used!");
1389 template <
bool HasMainKBlockLoop,
1392 __device__
static void Run(
const index_t* p_sorted_token_ids,
1393 const index_t* p_sorted_expert_ids,
1394 const index_t* p_max_token_id,
1395 const ADataType* p_a_grid,
1396 const AScaleDataType* p_a_scale_grid,
1397 const BDataType* p_b_grid,
1398 const BScaleDataType* p_b_scale_grid,
1400 CDataType* p_c_grid,
1403 AElementwiseOperation a_element_op,
1404 BElementwiseOperation b_element_op,
1405 CElementwiseOperation c_element_op)
1418 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
1437 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
1441 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
1442 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
1443 if(expert_block_id * MPerBlock >= max_token_id)
1446 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
1448 const auto block_mn = [&]() -> std::pair<int, int> {
1449 if constexpr(NSwizzle)
1451 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
1453 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
1454 const index_t expert_swizzle =
1455 ecnt > 0 ? ecnt : 1;
1456 const index_t bid_new = blockIdx.x - prefix_block;
1457 const index_t nid = __builtin_amdgcn_readfirstlane(
1458 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
1460 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
1465 return {blockIdx.x, blockIdx.y};
1469 const index_t block_n_id = block_mn.first;
1470 const index_t block_m_id = block_mn.second;
1472 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
1475 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
1476 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
1477 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
1478 constexpr
auto AKThreads = AK0Threads * AK1Threads;
1479 constexpr
auto AMRepeats = MPerBlock / AMThreads;
1480 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
1482 if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
1484 StaticallyIndexedArray<IndexType, AMRepeats> gather_offsets;
1485 static_for<0, AMRepeats, 1>{}([&](
auto m0) {
1486 const index_t fused_token = p_sorted_token_ids[token_pos + m0];
1487 index_t token_offset = fused_token & 0xffffff;
1488 if constexpr(!IsInputGemm)
1490 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
1492 gather_offsets(m0) =
static_cast<IndexType
>(token_offset);
1496 __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
1497 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
1498 problem.
N * (IsInputGemm ? 2 : 1) *
1502 const index_t n_block_data_idx_on_grid =
1503 __builtin_amdgcn_readfirstlane(block_n_id * NPerBlock);
1506 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1507 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
1508 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1509 p_b_grid + expert_id * expert_stride, b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
1512 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1513 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
1514 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1515 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
1516 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1528 auto a_blockwise_copy = ThreadGroupTensorSliceTransfer_Gather_DirectLoad<
1530 Sequence<AK0Number, MPerBlock, AK1Number>,
1531 ABlockTransferThreadClusterLengths_AK0_M_AK1,
1532 ABlockTransferThreadClusterArrangeOrder,
1535 decltype(a_grid_desc_ak0_m_ak1),
1536 decltype(a_block_desc_ak0_m_ak1),
1537 ABlockTransferSrcAccessOrder,
1538 ABlockTransferSrcVectorDim,
1540 ABlockTransferSrcScalarPerVector,
1542 1>(a_grid_desc_ak0_m_ak1,
1544 a_block_desc_ak0_m_ak1,
1549 auto b_blockwise_copy =
1551 Sequence<BK0Number, NPerBlock, BK1Number>,
1552 BBlockTransferThreadClusterLengths_BK0_N_BK1,
1553 BBlockTransferThreadClusterArrangeOrder,
1556 decltype(b_grid_desc_bk0_n_bk1),
1557 decltype(b_block_desc_bk0_n_bk1),
1558 BBlockTransferSrcAccessOrder,
1559 BBlockTransferSrcVectorDim,
1561 BBlockTransferSrcScalarPerVector>(
1562 b_grid_desc_bk0_n_bk1,
1564 b_block_desc_bk0_n_bk1,
1569 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1572 auto a_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1573 static_cast<ADataType*
>(p_shared), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
1575 auto b_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1576 reinterpret_cast<BDataType*
>(
static_cast<char*
>(p_shared) +
1577 a_block_space_size_aligned *
sizeof(ADataType)),
1578 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1584 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
1586 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
1587 decltype(c_thread_buf) c_thread_buf_up;
1591 c_thread_buf.num_of_v_,
1592 c_thread_buf.s_per_v,
1596 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
1597 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
1601 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
1602 const auto waveId_m = wave_idx[
I0];
1603 const auto waveId_n = wave_idx[
I1];
1605 auto thread_offset_shuffled =
1608 auto a_thread_offset_m = waveId_m;
1610 auto a_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1613 decltype(a_scale_grid_desc_am_ak),
1614 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
1620 true>(a_scale_grid_desc_am_ak,
1626 auto b_thread_offset_n = waveId_n;
1628 auto b_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1631 decltype(b_scale_grid_desc_bn_ak),
1632 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1638 true>(b_scale_grid_desc_bn_ak,
1643 if constexpr(IsInputGemm)
1646 b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
1647 auto b_block_buf_up = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1648 reinterpret_cast<BDataType*
>(
static_cast<char*
>(p_shared) +
1649 a_block_space_size_aligned *
sizeof(ADataType) +
1650 b_block_space_size_aligned *
sizeof(BDataType)),
1651 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1653 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
1654 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1655 p_b_grid_up + expert_id * expert_stride,
1656 b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
1658 auto b_blockwise_copy_up = ThreadGroupTensorSliceTransfer_DirectLoad<
1660 Sequence<BK0Number, NPerBlock, BK1Number>,
1661 BBlockTransferThreadClusterLengths_BK0_N_BK1,
1662 BBlockTransferThreadClusterArrangeOrder,
1665 decltype(b_grid_desc_bk0_n_bk1),
1666 decltype(b_block_desc_bk0_n_bk1),
1667 BBlockTransferSrcAccessOrder,
1668 BBlockTransferSrcVectorDim,
1670 BBlockTransferSrcScalarPerVector>(b_grid_desc_bk0_n_bk1,
1672 b_block_desc_bk0_n_bk1,
1675 const BScaleDataType* p_b_scale_grid_up =
1676 p_b_scale_grid + expert_scale_stride / 2 /
sizeof(BScaleDataType);
1677 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1678 p_b_scale_grid_up + expert_id * expert_scale_stride /
sizeof(BScaleDataType),
1679 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1681 auto b_scale_thread_copy_up = ThreadwiseTensorSliceTransfer_v2<
1684 decltype(b_scale_grid_desc_bn_ak),
1685 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1692 b_scale_grid_desc_bn_ak,
1697 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1699 a_grid_desc_ak0_m_ak1,
1700 a_block_desc_ak0_m_ak1,
1704 a_block_slice_copy_step,
1706 b_grid_desc_bk0_n_bk1,
1707 b_block_desc_bk0_n_bk1,
1709 b_blockwise_copy_up,
1714 b_block_slice_copy_step,
1719 a_scale_grid_desc_am_ak,
1720 a_scale_thread_copy,
1723 b_scale_grid_desc_bn_ak,
1724 b_scale_thread_copy,
1725 b_scale_thread_copy_up,
1727 b_scale_grid_buf_up,
1728 num_k_block_main_loop);
1732 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1733 a_grid_desc_ak0_m_ak1,
1734 a_block_desc_ak0_m_ak1,
1738 a_block_slice_copy_step,
1739 b_grid_desc_bk0_n_bk1,
1740 b_block_desc_bk0_n_bk1,
1744 b_block_slice_copy_step,
1746 a_scale_grid_desc_am_ak,
1747 a_scale_thread_copy,
1749 b_scale_grid_desc_bn_ak,
1750 b_scale_thread_copy,
1752 num_k_block_main_loop);
1757 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
1758 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
1760 static_assert(CShuffleMXdlPerWavePerShuffle %
MXdlPack == 0 &&
1761 CShuffleNXdlPerWavePerShuffle %
NXdlPack == 0,
1764 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1765 constexpr
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
1768 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
1769 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
1773 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
1774 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
1776 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
1777 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
1778 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
1779 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
1780 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
1781 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
1782 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
1783 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
1784 constexpr
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
1785 constexpr
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
1788 static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
1789 static_assert(M5 == 4);
1793 vector_type<float, 4> topk_weights;
1794 static_for<0, NXdlPerWave /
NXdlPack, 1>{}([&](
auto n0) {
1795 static_for<0, NXdlPack, 1>{}([&](
auto inxdl) {
1796 static_for<0, MXdlPerWave /
MXdlPack, 1>{}([&](
auto m0) {
1797 static_for<0, MXdlPack, 1>{}([&](
auto imxdl) {
1798 static_for<0, M3, 1>{}([&](
auto m3) {
1799 const index_t m_pos = block_m_id * MPerBlock +
1800 m0 * M2 * M1 * M3 * M4 * M5 +
1801 m1 * M2 * M3 * M4 * M5 +
1802 imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
1803 if constexpr(MulRoutedWeight)
1806 *c_style_pointer_cast<const vector_type<float, M5>*>(
1807 p_ds_grid[
I2] + m_pos);
1809 static_for<0, M5, 1>{}([&](
auto m5) {
1811 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
1812 make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
1813 constexpr
auto cidx = Number<c_offset>{};
1815 if constexpr(IsInputGemm)
1817 if constexpr(ActivationOperation ==
1820 float gate = c_thread_buf[cidx];
1821 float up = c_thread_buf_up[cidx];
1822 if constexpr(MulRoutedWeight)
1824 gate = gate * topk_weights.AsType<
float>()[m5];
1825 up = up * topk_weights.AsType<
float>()[m5];
1827 tensor_operation::element_wise::Silu{}(gate, gate);
1828 c_thread_buf_fp32(cidx) = gate * up;
1832 float gate = c_thread_buf[cidx];
1833 float up = c_thread_buf_up[cidx];
1834 if constexpr(MulRoutedWeight)
1836 gate = gate * topk_weights.AsType<
float>()[m5];
1837 up = up * topk_weights.AsType<
float>()[m5];
1839 tensor_operation::element_wise::Gelu{}(gate, gate);
1840 c_thread_buf_fp32(cidx) = gate * up;
1855 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
1856 if constexpr(MulRoutedWeight)
1858 c_thread_buf_fp32(cidx) =
1859 topk_weights.AsType<
float>()[m5] *
1860 c_thread_buf_fp32[cidx];
1870 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1873 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1874 static_cast<CShuffleDataType*
>(p_shared),
1875 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1878 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
1882 Number<CShuffleMXdlPerWavePerShuffle / MXdlPack>{},
1891 Number<CShuffleNXdlPerWavePerShuffle / NXdlPack>{},
1896 make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}),
1898 Sequence<0, 2, 4, 6, 7, 8>{},
1900 Sequence<1, 3, 5, 9>{}));
1904 const auto c_thread_mtx_on_block =
1905 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
1907 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
1908 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
1910 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
1916 const auto m_thread_data_on_block_idx =
1917 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
1920 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
1926 const auto n_thread_data_on_block_idx =
1927 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
1931 auto c_thread_copy_vgpr_to_lds = ThreadwiseTensorSliceTransfer_v1r3<
1934 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1935 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1937 Sequence<CShuffleMXdlPerWavePerShuffle /
MXdlPack,
1938 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
1947 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
1952 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1955 m_thread_data_on_block_idx[
I1],
1956 n_thread_data_on_block_idx[
I1],
1957 m_thread_data_on_block_idx[
I2],
1958 n_thread_data_on_block_idx[
I2],
1959 m_thread_data_on_block_idx[
I3],
1960 m_thread_data_on_block_idx[
I4],
1961 m_thread_data_on_block_idx[
I5],
1962 n_thread_data_on_block_idx[
I3]),
1965 using EDataType = CDataType;
1970 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
1976 return make_dynamic_buffer<AddressSpaceEnum::Global>(
1977 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
1979 Number<NumDTensor>{});
1983 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
1985 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
1986 Number<NumDTensor>{}));
1990 tie(c_shuffle_block_buf),
1992 {
return ds_grid_buf[i]; },
1993 Number<NumDTensor>{}));
1996 const auto idx_c_ds_block_begin =
2004 Number<NumDTensor>{}));
2006 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
2007 c_grid_desc_mblock_mperblock_nblock_nperblock;
2009 using CDEBlockTransferCluster =
2010 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
2011 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
2012 constexpr
index_t scatter_weight_idx = 3;
2013 auto cde_block_copy_lds_and_global = ThreadGroupTensorSliceTransfer_v7r3_scatter<
2017 decltype(c_ds_desc_refs),
2018 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
2019 CElementwiseOperation,
2020 Sequence<static_cast<index_t>(EGlobalMemoryDataOperation)>,
2024 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2026 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>,
2027 CDEBlockTransferCluster,
2028 Sequence<0, 1, 2, 3>,
2029 Sequence<0, 1, 2, 3>,
2030 Sequence<0, 1, 2, 3>,
2033 CDEShuffleBlockTransferScalarPerVectors,
2045 idx_c_ds_block_begin,
2046 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2050 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2051 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2053 constexpr
auto sfc_c_vgpr =
2054 SpaceFillingCurve<Sequence<MXdlPerWave /
MXdlPack,
2064 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2065 Sequence<CShuffleMXdlPerWavePerShuffle /
MXdlPack,
2066 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2076 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2079 constexpr
auto sfc_cde_block =
2080 SpaceFillingCurve<Sequence<1, MPerBlock, 1, NPerBlock>,
2081 Sequence<0, 2, 1, 3>,
2083 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2085 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>>{};
2087 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2088 constexpr
auto EMThreads =
2089 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2090 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2091 constexpr
auto ENThreads =
2092 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2093 static_for<0, num_access, 1>{}([&](
auto access_id) {
2095 StaticallyIndexedArray<IndexType, EMRepeats> scatter_offsets;
2097 auto dstidx = sfc_cde_block.GetIndex(access_id);
2099 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2100 static_for<0, EMRepeats, 1>{}([&](
auto m0) {
2101 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2102 IndexType token_offset = fused_token & 0xffffff;
2103 if constexpr(IsInputGemm)
2105 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2107 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
N;
2113 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2114 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2116 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2117 c_shuffle_block_buf);
2123 cde_block_copy_lds_and_global.Run(
2126 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2130 if constexpr(access_id < num_access - 1)
2132 constexpr
auto cde_lds_and_global_step =
2133 sfc_cde_block.GetForwardStep(access_id);
2136 static_for<0, NumDTensor, 1>{}([&](
auto i) {
2137 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2138 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2142 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2143 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2145 cde_lds_and_global_step);
2152 template <
bool HasMainKBlockLoop,
2156 const index_t* p_sorted_expert_ids,
2157 const index_t* p_max_token_id,
2158 const ADataType* p_a_grid,
2159 const AScaleDataType* p_a_scale_grid,
2160 const BDataType* p_b_grid,
2161 const BScaleDataType* p_b_scale_grid,
2163 CDataType* p_c_grid,
2167 AElementwiseOperation a_element_op,
2168 BElementwiseOperation b_element_op,
2169 CElementwiseOperation c_element_op)
2182 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
2201 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
2205 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
2206 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
2207 if(expert_block_id * MPerBlock >= max_token_id)
2210 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
2211 const auto block_mn = [&]() -> std::pair<int, int> {
2212 if constexpr(NSwizzle)
2214 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
2216 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
2217 const index_t expert_swizzle =
2218 ecnt > 0 ? ecnt : 1;
2219 const index_t bid_new = blockIdx.x - prefix_block;
2220 const index_t nid = __builtin_amdgcn_readfirstlane(
2221 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
2223 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
2228 return {blockIdx.x, blockIdx.y};
2232 const index_t block_n_id = block_mn.first;
2233 const index_t block_m_id = block_mn.second;
2235 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
2238 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
2239 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
2240 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
2241 constexpr
auto AKThreads = AK0Threads * AK1Threads;
2242 constexpr
auto AMRepeats = MPerBlock / AMThreads;
2243 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads;
2245 if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
2249 const index_t fused_token = p_sorted_token_ids[token_pos + m0 * AMThreads];
2250 index_t token_offset = fused_token & 0xffffff;
2251 if constexpr(!IsInputGemm)
2253 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2255 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
K;
2259 __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
2260 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
2261 problem.
N * (IsInputGemm ? 2 : 1) *
2265 const index_t n_block_data_idx_on_grid =
2266 __builtin_amdgcn_readfirstlane(block_n_id * NPerBlock);
2269 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2270 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
2271 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2272 p_b_grid + expert_id * expert_stride, b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
2275 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2276 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
2277 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2278 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
2279 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2294 ABlockTransferThreadClusterLengths_AK0_M_AK1,
2295 ABlockTransferThreadClusterArrangeOrder,
2298 decltype(a_grid_desc_ak0_m_ak1),
2299 decltype(a_block_desc_ak0_m_ak1),
2300 ABlockTransferSrcAccessOrder,
2301 ABlockTransferSrcVectorDim,
2303 ABlockTransferSrcScalarPerVector,
2305 1>(a_grid_desc_ak0_m_ak1,
2307 a_block_desc_ak0_m_ak1,
2312 auto b_blockwise_copy =
2315 BBlockTransferThreadClusterLengths_BK0_N_BK1,
2316 BBlockTransferThreadClusterArrangeOrder,
2319 decltype(b_grid_desc_bk0_n_bk1),
2320 decltype(b_block_desc_bk0_n_bk1),
2321 BBlockTransferSrcAccessOrder,
2322 BBlockTransferSrcVectorDim,
2324 BBlockTransferSrcScalarPerVector>(
2325 b_grid_desc_bk0_n_bk1,
2327 b_block_desc_bk0_n_bk1,
2332 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
2334 auto a_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2335 static_cast<ADataType*
>(p_shared_0), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2337 auto b_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2338 bit_cast<BDataType*>(
static_cast<char*
>(p_shared_0) +
2339 a_block_space_size_aligned *
sizeof(ADataType)),
2340 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2342 auto a_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2343 static_cast<ADataType*
>(p_shared_1), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2345 auto b_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2346 bit_cast<BDataType*>(bit_cast<char*>(p_shared_1) +
2347 a_block_space_size_aligned *
sizeof(ADataType)),
2348 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2350 auto a_block_bufs =
make_tuple(a_block_buf_ping, a_block_buf_pong);
2351 auto b_block_bufs =
make_tuple(b_block_buf_ping, b_block_buf_pong);
2357 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
2359 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
2360 decltype(c_thread_buf) c_thread_buf_up;
2364 c_thread_buf.num_of_v_,
2365 c_thread_buf.s_per_v,
2369 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
2370 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
2374 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
2375 const auto waveId_m = wave_idx[
I0];
2376 const auto waveId_n = wave_idx[
I1];
2378 auto thread_offset_shuffled =
2381 auto a_thread_offset_m = waveId_m;
2384 const index_t token_scale_pos = block_m_id * MPerBlock;
2385 if(token_scale_pos >= max_token_id || token0 >= problem.
NumTokens)
2391 decltype(a_scale_grid_desc_am_ak),
2392 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
2398 true>(a_scale_grid_desc_am_ak,
2404 auto b_thread_offset_n = waveId_n;
2409 decltype(b_scale_grid_desc_bn_ak),
2410 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2416 true>(b_scale_grid_desc_bn_ak,
2421 if constexpr(IsInputGemm)
2423 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
2424 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2425 p_b_grid_up + expert_id * expert_stride,
2426 b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
2430 b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
2431 auto b_block_buf_up_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2432 bit_cast<BDataType*>(
static_cast<char*
>(p_shared_0) +
2433 a_block_space_size_aligned *
sizeof(ADataType) +
2434 b_block_space_size_aligned *
sizeof(BDataType)),
2435 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2436 auto b_block_buf_up_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2437 bit_cast<BDataType*>(bit_cast<char*>(p_shared_1) +
2438 a_block_space_size_aligned *
sizeof(ADataType) +
2439 b_block_space_size_aligned *
sizeof(BDataType)),
2440 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2442 auto b_block_bufs_up =
make_tuple(b_block_buf_up_ping, b_block_buf_up_pong);
2447 BBlockTransferThreadClusterLengths_BK0_N_BK1,
2448 BBlockTransferThreadClusterArrangeOrder,
2451 decltype(b_grid_desc_bk0_n_bk1),
2452 decltype(b_block_desc_bk0_n_bk1),
2453 BBlockTransferSrcAccessOrder,
2454 BBlockTransferSrcVectorDim,
2456 BBlockTransferSrcScalarPerVector>(b_grid_desc_bk0_n_bk1,
2458 b_block_desc_bk0_n_bk1,
2461 const BScaleDataType* p_b_scale_grid_up =
2462 p_b_scale_grid + expert_scale_stride / 2 /
sizeof(BScaleDataType);
2463 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2464 p_b_scale_grid_up + expert_id * expert_scale_stride /
sizeof(BScaleDataType),
2465 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2470 decltype(b_scale_grid_desc_bn_ak),
2471 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2478 b_scale_grid_desc_bn_ak,
2483 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2485 a_grid_desc_ak0_m_ak1,
2486 a_block_desc_ak0_m_ak1,
2490 a_block_slice_copy_step,
2492 b_grid_desc_bk0_n_bk1,
2493 b_block_desc_bk0_n_bk1,
2495 b_blockwise_copy_up,
2500 b_block_slice_copy_step,
2505 a_scale_grid_desc_am_ak,
2506 a_scale_thread_copy,
2509 b_scale_grid_desc_bn_ak,
2510 b_scale_thread_copy,
2511 b_scale_thread_copy_up,
2513 b_scale_grid_buf_up,
2514 num_k_block_main_loop);
2518 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2519 a_grid_desc_ak0_m_ak1,
2520 a_block_desc_ak0_m_ak1,
2524 a_block_slice_copy_step,
2525 b_grid_desc_bk0_n_bk1,
2526 b_block_desc_bk0_n_bk1,
2530 b_block_slice_copy_step,
2532 a_scale_grid_desc_am_ak,
2533 a_scale_thread_copy,
2535 b_scale_grid_desc_bn_ak,
2536 b_scale_thread_copy,
2538 num_k_block_main_loop);
2543 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
2544 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
2546 static_assert(CShuffleMXdlPerWavePerShuffle %
MXdlPack == 0 &&
2547 CShuffleNXdlPerWavePerShuffle %
NXdlPack == 0,
2550 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
2551 constexpr
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
2554 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
2555 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2559 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
2560 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2562 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
2563 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
2564 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
2565 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
2566 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
2567 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
2568 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
2569 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
2570 constexpr
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
2571 constexpr
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
2575 static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
2576 static_assert(M5 == 4);
2586 const index_t m_pos = block_m_id * MPerBlock +
2587 m0 * M2 * M1 * M3 * M4 * M5 +
2588 m1 * M2 * M3 * M4 * M5 +
2589 imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
2590 if constexpr(MulRoutedWeight)
2593 *c_style_pointer_cast<const vector_type<float, M5>*>(
2594 p_ds_grid[
I2] + m_pos);
2598 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
2599 make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
2602 if constexpr(IsInputGemm)
2604 if constexpr(ActivationOperation ==
2607 float gate = c_thread_buf[cidx];
2608 float up = c_thread_buf_up[cidx];
2609 if constexpr(MulRoutedWeight)
2611 gate = gate * topk_weights.AsType<
float>()[m5];
2612 up = up * topk_weights.AsType<
float>()[m5];
2615 c_thread_buf_fp32(cidx) = gate * up;
2619 float gate = c_thread_buf[cidx];
2620 float up = c_thread_buf_up[cidx];
2621 if constexpr(MulRoutedWeight)
2623 gate = gate * topk_weights.AsType<
float>()[m5];
2624 up = up * topk_weights.AsType<
float>()[m5];
2627 c_thread_buf_fp32(cidx) = gate * up;
2632 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
2633 if constexpr(MulRoutedWeight)
2635 c_thread_buf_fp32(cidx) =
2636 topk_weights.AsType<
float>()[m5] *
2637 c_thread_buf_fp32[cidx];
2647 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
2650 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2651 static_cast<CShuffleDataType*
>(p_shared_0),
2652 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2655 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
2681 const auto c_thread_mtx_on_block =
2682 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
2684 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
2685 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
2687 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
2693 const auto m_thread_data_on_block_idx =
2694 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
2697 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
2703 const auto n_thread_data_on_block_idx =
2704 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
2711 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2712 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2715 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2724 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2729 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2732 m_thread_data_on_block_idx[
I1],
2733 n_thread_data_on_block_idx[
I1],
2734 m_thread_data_on_block_idx[
I2],
2735 n_thread_data_on_block_idx[
I2],
2736 m_thread_data_on_block_idx[
I3],
2737 m_thread_data_on_block_idx[
I4],
2738 m_thread_data_on_block_idx[
I5],
2739 n_thread_data_on_block_idx[
I3]),
2742 using EDataType = CDataType;
2747 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
2753 return make_dynamic_buffer<AddressSpaceEnum::Global>(
2754 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
2760 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
2762 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
2767 tie(c_shuffle_block_buf),
2769 {
return ds_grid_buf[i]; },
2773 const auto idx_c_ds_block_begin =
2783 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
2784 c_grid_desc_mblock_mperblock_nblock_nperblock;
2786 using CDEBlockTransferCluster =
2787 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
2788 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
2789 constexpr
index_t scatter_weight_idx = 3;
2794 decltype(c_ds_desc_refs),
2795 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
2796 CElementwiseOperation,
2801 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2803 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>,
2804 CDEBlockTransferCluster,
2810 CDEShuffleBlockTransferScalarPerVectors,
2822 idx_c_ds_block_begin,
2823 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2827 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2828 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2830 constexpr
auto sfc_c_vgpr =
2841 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2843 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2853 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2856 constexpr
auto sfc_cde_block =
2860 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2862 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>>{};
2864 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2865 constexpr
auto EMThreads =
2866 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2867 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2868 constexpr
auto ENThreads =
2869 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2874 auto dstidx = sfc_cde_block.GetIndex(access_id);
2876 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2878 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2879 IndexType token_offset = fused_token & 0xffffff;
2880 if constexpr(IsInputGemm)
2882 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2884 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
N;
2890 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2891 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2893 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2894 c_shuffle_block_buf);
2900 cde_block_copy_lds_and_global.Run(
2903 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2907 if constexpr(access_id < num_access - 1)
2909 constexpr
auto cde_lds_and_global_step =
2910 sfc_cde_block.GetForwardStep(access_id);
2914 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2915 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2919 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2920 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2922 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
constexpr auto BlockGemmMXPipeline_Selector()
Definition: blockwise_gemm_pipeline_xdlops_mx_moe_selector.hpp:36
__host__ constexpr __device__ auto generate_tuple(F &&f, Number< N >)
Definition: tuple_helper.hpp:21
InMemoryDataOperationEnum
Definition: ck.hpp:275
__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:425
__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:140
__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:297
__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
Definition: gridwise_moe_mx_gemm.hpp:715
const index_t * p_max_token_id
Definition: gridwise_moe_mx_gemm.hpp:777
CDataType * p_c_grid
Definition: gridwise_moe_mx_gemm.hpp:783
const AElementwiseOperation a_element_op
Definition: gridwise_moe_mx_gemm.hpp:785
const index_t * p_sorted_expert_ids
Definition: gridwise_moe_mx_gemm.hpp:776
const CElementwiseOperation c_element_op
Definition: gridwise_moe_mx_gemm.hpp:787
const BDataType * p_b_grid
Definition: gridwise_moe_mx_gemm.hpp:780
const BScaleDataType * p_b_scale_grid
Definition: gridwise_moe_mx_gemm.hpp:781
DsGridPointer p_ds_grid
Definition: gridwise_moe_mx_gemm.hpp:782
__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.hpp:716
const index_t * p_sorted_token_ids
Definition: gridwise_moe_mx_gemm.hpp:775
const BElementwiseOperation b_element_op
Definition: gridwise_moe_mx_gemm.hpp:786
const AScaleDataType * p_a_scale_grid
Definition: gridwise_moe_mx_gemm.hpp:779
const ADataType * p_a_grid
Definition: gridwise_moe_mx_gemm.hpp:778
Definition: gridwise_moe_mx_gemm.hpp:643
index_t MBlock
Definition: gridwise_moe_mx_gemm.hpp:709
index_t NPadded
Definition: gridwise_moe_mx_gemm.hpp:704
index_t K
Definition: gridwise_moe_mx_gemm.hpp:695
index_t N
Definition: gridwise_moe_mx_gemm.hpp:694
index_t NumTokens
Definition: gridwise_moe_mx_gemm.hpp:691
index_t M
Definition: gridwise_moe_mx_gemm.hpp:693
index_t StrideA
Definition: gridwise_moe_mx_gemm.hpp:696
index_t StrideScaleB
Definition: gridwise_moe_mx_gemm.hpp:699
index_t KRead
Definition: gridwise_moe_mx_gemm.hpp:705
index_t NBlock
Definition: gridwise_moe_mx_gemm.hpp:710
index_t StrideC
Definition: gridwise_moe_mx_gemm.hpp:701
index_t StrideB
Definition: gridwise_moe_mx_gemm.hpp:698
__host__ void Print() const
Definition: gridwise_moe_mx_gemm.hpp:679
index_t BK0
Definition: gridwise_moe_mx_gemm.hpp:708
index_t StrideScaleA
Definition: gridwise_moe_mx_gemm.hpp:697
std::array< index_t, NumDTensor > StrideDs
Definition: gridwise_moe_mx_gemm.hpp:700
index_t MPadded
Definition: gridwise_moe_mx_gemm.hpp:703
index_t KBatch
Definition: gridwise_moe_mx_gemm.hpp:702
index_t KPadded
Definition: gridwise_moe_mx_gemm.hpp:706
index_t TopK
Definition: gridwise_moe_mx_gemm.hpp:692
index_t AK0
Definition: gridwise_moe_mx_gemm.hpp:707
__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.hpp:644
Definition: gridwise_moe_mx_gemm.hpp:791
__device__ SplitKBatchOffset(Argument &karg, index_t k_id)
Definition: gridwise_moe_mx_gemm.hpp:792
index_t b_k_split_offset
Definition: gridwise_moe_mx_gemm.hpp:846
index_t b_scale_k_split_offset
Definition: gridwise_moe_mx_gemm.hpp:848
index_t a_k_split_offset
Definition: gridwise_moe_mx_gemm.hpp:845
index_t a_scale_k_split_offset
Definition: gridwise_moe_mx_gemm.hpp:847
Definition: gridwise_moe_mx_gemm.hpp:173
static __host__ auto CalculateGridSize(index_t M, index_t N)
Definition: gridwise_moe_mx_gemm.hpp:242
static constexpr index_t APackedSize
Definition: gridwise_moe_mx_gemm.hpp:212
static constexpr auto MakeDsGridPointer()
Definition: gridwise_moe_mx_gemm.hpp:227
static constexpr __host__ bool CheckValidity(const Argument &karg)
Definition: gridwise_moe_mx_gemm.hpp:1165
__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.hpp:619
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.hpp:631
static __host__ auto CalculateBK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm.hpp:273
static constexpr auto AK0Number
Definition: gridwise_moe_mx_gemm.hpp:191
static constexpr auto I7
Definition: gridwise_moe_mx_gemm.hpp:184
static constexpr index_t BPackedSize
Definition: gridwise_moe_mx_gemm.hpp:213
remove_cvref_t< decltype(BlockGemmMXPipeline_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.hpp:1126
static __host__ auto CalculateMPadded(index_t M)
Definition: gridwise_moe_mx_gemm.hpp:252
static __host__ auto CalculateKPadded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm.hpp:279
static constexpr auto KXdlPack
Definition: gridwise_moe_mx_gemm.hpp:204
static constexpr __device__ auto GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()
Definition: gridwise_moe_mx_gemm.hpp:969
__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.hpp:331
static constexpr auto I6
Definition: gridwise_moe_mx_gemm.hpp:183
static constexpr __host__ TailNumber CalculateKBlockLoopTailNum(index_t K)
Definition: gridwise_moe_mx_gemm.hpp:1350
static constexpr auto AK1Number
Definition: gridwise_moe_mx_gemm.hpp:193
static constexpr auto I9
Definition: gridwise_moe_mx_gemm.hpp:186
static __host__ auto CalculateKRead(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm.hpp:285
decltype(MakeDsGridPointer()) DsGridPointer
Definition: gridwise_moe_mx_gemm.hpp:238
static constexpr auto I8
Definition: gridwise_moe_mx_gemm.hpp:185
__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.hpp:446
static constexpr auto I0
Definition: gridwise_moe_mx_gemm.hpp:177
BDataType LDSTypeB
Definition: gridwise_moe_mx_gemm.hpp:175
__host__ static constexpr __device__ auto MakeAMmaTileDescriptor_M0_M1_M2_M3_K(const ABlockDesc_AK0_M_AK1 &)
Definition: gridwise_moe_mx_gemm.hpp:555
static constexpr auto NXdlPack
Definition: gridwise_moe_mx_gemm.hpp:203
__host__ static __device__ auto MakeCGridDescriptor_M_N(IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
Definition: gridwise_moe_mx_gemm.hpp:574
static constexpr auto I3
Definition: gridwise_moe_mx_gemm.hpp:180
static constexpr index_t scale_pack_size_b
Definition: gridwise_moe_mx_gemm.hpp:1378
static constexpr index_t scale_pack_size_a
Definition: gridwise_moe_mx_gemm.hpp:1377
static __host__ auto CalculateMBlock(index_t M)
Definition: gridwise_moe_mx_gemm.hpp:292
static __host__ auto CalculateKPadded(index_t K)
Definition: gridwise_moe_mx_gemm.hpp:262
static constexpr index_t NumDTensor
Definition: gridwise_moe_mx_gemm.hpp:200
static constexpr index_t SortedTileSize
Definition: gridwise_moe_mx_gemm.hpp:225
static __host__ auto CalculateNBlock(index_t N)
Definition: gridwise_moe_mx_gemm.hpp:297
static constexpr __device__ auto GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()
Definition: gridwise_moe_mx_gemm.hpp:851
static constexpr auto CShuffleBlockTransferScalarPerVector_NPerBlock
Definition: gridwise_moe_mx_gemm.hpp:188
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.hpp:2155
ADataType LDSTypeA
Definition: gridwise_moe_mx_gemm.hpp:174
__host__ static constexpr __device__ auto MakeBMmaTileDescriptor_N0_N1_N2_N3_K(const BBlockDesc_BK0_N_BK1 &)
Definition: gridwise_moe_mx_gemm.hpp:565
ThisThreadBlock< BlockSize > ThisThreadBlock
Definition: gridwise_moe_mx_gemm.hpp:240
static constexpr auto lcm_AK1_BK1
Definition: gridwise_moe_mx_gemm.hpp:196
static constexpr auto I1
Definition: gridwise_moe_mx_gemm.hpp:178
static constexpr auto BK1Number
Definition: gridwise_moe_mx_gemm.hpp:194
static constexpr auto is_scale_mfma
Definition: gridwise_moe_mx_gemm.hpp:198
__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.hpp:598
static __host__ auto CalculateNPadded(index_t N)
Definition: gridwise_moe_mx_gemm.hpp:257
static constexpr __device__ index_t GetSharedMemoryNumberOfByte()
Definition: gridwise_moe_mx_gemm.hpp:1128
static constexpr auto I5
Definition: gridwise_moe_mx_gemm.hpp:182
static constexpr index_t KPack
Definition: gridwise_moe_mx_gemm.hpp:221
static constexpr auto I4
Definition: gridwise_moe_mx_gemm.hpp:181
static constexpr auto I2
Definition: gridwise_moe_mx_gemm.hpp:179
__host__ static constexpr __device__ auto MakeGemmMmaTileDescriptor(const TileDesc_K0_MN_K1 &)
Definition: gridwise_moe_mx_gemm.hpp:307
static constexpr auto MXdlPack
Definition: gridwise_moe_mx_gemm.hpp:202
static constexpr bool is_single_rate_mfma
Definition: gridwise_moe_mx_gemm.hpp:197
static constexpr __device__ auto GetCShuffleBlockDescriptor_MBlock_MPerBlock_NBlock_NPerBlock()
Definition: gridwise_moe_mx_gemm.hpp:1083
__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.hpp:1358
static __host__ auto CalculateAK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm.hpp:267
static constexpr auto BK0Number
Definition: gridwise_moe_mx_gemm.hpp:192
static constexpr __host__ bool CalculateHasMainKBlockLoop(index_t K)
Definition: gridwise_moe_mx_gemm.hpp:1343
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_direct_load.hpp:55
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:197
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:129