38 template <
typename GridwiseGemm,
39 bool HasMainKBlockLoop,
44 #if CK_USE_LAUNCH_BOUNDS
51 __shared__
char p_shared[GridwiseGemm::GetSharedMemoryNumberOfByte()];
53 auto splitk_batch_offset =
typename GridwiseGemm::SplitKBatchOffset(karg, blockIdx.z);
55 GridwiseGemm::template Run<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
56 karg.p_sorted_token_ids,
57 karg.p_sorted_expert_ids,
59 karg.p_a_grid + splitk_batch_offset.a_k_split_offset,
60 karg.p_a_scale_grid + splitk_batch_offset.a_k_split_offset,
61 karg.p_b_grid + splitk_batch_offset.b_k_split_offset,
62 karg.p_b_scale_grid + splitk_batch_offset.b_k_split_offset,
76 template <
typename GridwiseGemm,
77 bool HasMainKBlockLoop,
82 #if CK_USE_LAUNCH_BOUNDS
89 __shared__
char p_shared[GridwiseGemm::GetSharedMemoryNumberOfByte()];
90 __shared__
char p_shared1[GridwiseGemm::GetSharedMemoryNumberOfByte()];
94 GridwiseGemm::template Run_2Lds<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
95 karg.p_sorted_token_ids,
96 karg.p_sorted_expert_ids,
116 template <
typename ALayout,
121 typename AScaleDataType,
123 typename BScaleDataType,
124 typename AccDataType,
125 typename CShuffleDataType,
128 typename AElementwiseOperation,
129 typename BElementwiseOperation,
130 typename CElementwiseOperation,
143 typename ABlockTransferThreadClusterLengths_AK0_M_AK1,
144 typename ABlockTransferThreadClusterArrangeOrder,
145 typename ABlockTransferSrcAccessOrder,
146 index_t ABlockTransferSrcVectorDim,
147 index_t ABlockTransferSrcScalarPerVector,
148 index_t ABlockTransferDstScalarPerVector_AK1,
149 bool AThreadTransferSrcResetCoordinateAfterRun,
151 typename BBlockTransferThreadClusterLengths_BK0_N_BK1,
152 typename BBlockTransferThreadClusterArrangeOrder,
153 typename BBlockTransferSrcAccessOrder,
154 index_t BBlockTransferSrcVectorDim,
155 index_t BBlockTransferSrcScalarPerVector,
156 index_t BBlockTransferDstScalarPerVector_BK1,
157 bool BThreadTransferSrcResetCoordinateAfterRun,
159 index_t CShuffleMXdlPerWavePerShuffle,
160 index_t CShuffleNXdlPerWavePerShuffle,
161 typename CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock,
162 typename CDEShuffleBlockTransferScalarPerVectors,
165 index_t ActivationOperation = 0,
166 bool NSwizzle =
false,
167 bool IsInputGemm =
true,
168 bool MulRoutedWeight =
true,
170 typename ComputeTypeA = ADataType,
171 typename ComputeTypeB = BDataType>
189 CDEShuffleBlockTransferScalarPerVectors{}[
I0];
225 return static_cast<const DDataType*
>(
nullptr);
238 const index_t gridx = NSwizzle ? nblock * mblock : nblock;
239 const index_t gridy = NSwizzle ? 1 : mblock;
261 auto K_t = K_Batch * KPerBlock;
262 return (K + K_t - 1) / K_t * (KPerBlock / AK1Value);
267 auto K_t = K_Batch * KPerBlock;
268 return (K + K_t - 1) / K_t * (KPerBlock / BK1Value);
273 auto K_t = K_Batch * KPerBlock;
274 return (K + K_t - 1) / K_t * KPerBlock;
280 auto K_t = K_Batch * KReadVec;
281 return (K + K_t - 1) / K_t * KReadVec;
294 template <
index_t MNXdlPerWave,
298 typename TileDesc_K0_MN_K1>
316 IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
318 const auto a_grid_desc_mraw_kraw = [&]() {
319 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
323 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
331 if constexpr(GemmSpec == GemmSpecialization::MKPadding ||
332 GemmSpec == GemmSpecialization::MNKPadding)
335 const auto a_grid_desc_m_k =
349 return a_grid_desc_ak0_m_ak1;
351 else if constexpr(GemmSpec == GemmSpecialization::MPadding ||
352 GemmSpec == GemmSpecialization::MNPadding)
356 a_grid_desc_mraw_kraw,
362 return a_grid_desc_ak0_m_ak1;
364 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
365 GemmSpec == GemmSpecialization::NKPadding)
369 a_grid_desc_mraw_kraw,
381 return a_grid_desc_ak0_m_ak1;
387 a_grid_desc_mraw_kraw,
393 return a_grid_desc_ak0_m_ak1;
400 const auto b_grid_desc_nraw_kraw = [&]() {
414 GemmSpec != GemmSpecialization::Default),
415 "pk_i4_t does not support padding");
417 GemmSpec != GemmSpecialization::Default),
418 "f4x2_pk_t does not support padding");
420 if constexpr(GemmSpec == GemmSpecialization::NKPadding ||
421 GemmSpec == GemmSpecialization::MNKPadding)
424 const auto b_grid_desc_n_k =
438 return b_grid_desc_bk0_n_bk1;
440 else if constexpr(GemmSpec == GemmSpecialization::NPadding ||
441 GemmSpec == GemmSpecialization::MNPadding)
445 b_grid_desc_nraw_kraw,
451 return b_grid_desc_bk0_n_bk1;
453 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
454 GemmSpec == GemmSpecialization::MKPadding)
458 b_grid_desc_nraw_kraw,
470 return b_grid_desc_bk0_n_bk1;
476 b_grid_desc_nraw_kraw,
482 return b_grid_desc_bk0_n_bk1;
486 template <
typename ABlockDesc_AK0_M_AK1>
487 __host__ __device__
static constexpr
auto
490 constexpr
index_t MWaves = MPerBlock / (MXdlPerWave * MPerXdl);
492 return MakeGemmMmaTileDescriptor<MXdlPerWave, MWaves, MXdlPack, MPerXdl>(
493 ABlockDesc_AK0_M_AK1{});
496 template <
typename BBlockDesc_BK0_N_BK1>
497 __host__ __device__
static constexpr
auto
500 constexpr
index_t NWaves = NPerBlock / (NXdlPerWave * NPerXdl);
502 return MakeGemmMmaTileDescriptor<NXdlPerWave, NWaves, NXdlPack, NPerXdl>(
503 BBlockDesc_BK0_N_BK1{});
506 template <
typename ELayout>
508 IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
510 const auto c_grid_desc_mraw_nraw = [&]() {
529 template <
typename DLayout>
530 __host__ __device__
static auto
533 const auto c_grid_desc_mraw_nraw = [&]() {
558 return MakeDGridDescriptor_M_N<DLayout>(M, MPad, N, NPad, StrideDs[i]);
563 template <
typename DsGr
idDesc>
565 const DsGridDesc& ds_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
570 ds_grid_desc_m_n[i], MBlock, NBlock);
586 std::array<index_t, NumDTensor> StrideDs_,
614 std::cout <<
"problem {" <<
"NumTokens:" <<
NumTokens <<
", " <<
"TopK:" <<
TopK <<
", "
615 <<
"M:" <<
M <<
", " <<
"N:" <<
N <<
", " <<
"K:" <<
K <<
", "
619 <<
", " <<
"KRead:" <<
KRead <<
", " <<
"KP:" <<
KPadded <<
", "
620 <<
"AK0:" <<
AK0 <<
", " <<
"BK0:" <<
BK0 <<
", " <<
"MBlock: " <<
MBlock
621 <<
", " <<
"NBlock: " <<
NBlock <<
"}" << std::endl;
650 const index_t* p_sorted_expert_ids_,
651 const index_t* p_max_token_id_,
652 const ADataType* p_a_grid_,
653 const AScaleDataType* p_a_scale_grid_,
654 const BDataType* p_b_grid_,
655 const BScaleDataType* p_b_scale_grid_,
656 std::array<const void*, NumDTensor> p_ds_grid_,
657 CDataType* p_c_grid_,
667 std::array<index_t, NumDTensor> StrideDs_,
670 AElementwiseOperation a_element_op_,
671 BElementwiseOperation b_element_op_,
672 CElementwiseOperation c_element_op_)
704 p_ds_grid(i) =
static_cast<const DDataType_*
>(p_ds_grid_[i]);
727 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
731 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
736 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
740 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
747 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
751 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
758 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
763 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
768 if(k_id < karg.
KBatch - 1)
797 constexpr
auto a_lds_block_desc =
809 return a_lds_block_desc_permuted;
816 constexpr
auto WaveSize = 64;
817 constexpr
auto M0 = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
818 constexpr
auto M1 = MPerBlock / M0;
820 constexpr
auto KThreadWrite = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
821 constexpr
auto K0PerThreadWrite =
AK0Number / KThreadWrite;
822 constexpr
auto KThreadRead = WaveSize / MPerXdl;
823 constexpr
auto K0PerThreadRead =
AK0Number / KThreadRead;
825 constexpr
auto kfold = (
AK1Number * M0 *
sizeof(ADataType) > 128)
827 : 128 / (
AK1Number * M0 *
sizeof(ADataType));
828 constexpr
auto KThreadReadPerm =
829 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
830 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
834 constexpr
auto mpair = (
AK1Number * MPerXdl *
sizeof(ADataType) > 128)
836 : ((128 / (
AK1Number * MPerXdl *
sizeof(ADataType))) > M0
838 : 128 / (
AK1Number * MPerXdl *
sizeof(ADataType)));
844 Number<kfold * M0 / mpair>{},
863 a_lds_block_desc_permuted,
885 a_lds_block_desc_unmerged,
888 Number<KThreadWrite / kfold / KThreadReadPerm>{},
897 return a_lds_block_desc_ak0_m_ak1;
913 constexpr
auto b_lds_block_desc =
925 return b_lds_block_desc_permuted;
929 constexpr
auto WaveSize = 64;
930 constexpr
auto N0 = BBlockTransferThreadClusterLengths_BK0_N_BK1{}.At(
I1);
931 constexpr
auto N1 = NPerBlock / N0;
933 constexpr
auto KThreadWrite = BBlockTransferThreadClusterLengths_BK0_N_BK1{}.At(
I0);
934 constexpr
auto K0PerThreadWrite =
BK0Number / KThreadWrite;
935 constexpr
auto KThreadRead = WaveSize / NPerXdl;
936 constexpr
auto K0PerThreadRead =
BK0Number / KThreadRead;
938 constexpr
auto kfold = (
BK1Number * N0 *
sizeof(BDataType) > 128)
940 : 128 / (
BK1Number * N0 *
sizeof(BDataType));
941 constexpr
auto KThreadReadPerm =
942 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
943 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
947 constexpr
auto npair = (
BK1Number * NPerXdl *
sizeof(BDataType) > 128)
949 : ((128 / (
BK1Number * NPerXdl *
sizeof(BDataType))) > N0
951 : 128 / (
BK1Number * NPerXdl *
sizeof(BDataType)));
957 Number<kfold * N0 / npair>{},
976 b_lds_block_desc_permuted,
998 b_lds_block_desc_unmerged,
1001 Number<KThreadWrite / kfold / KThreadReadPerm>{},
1010 return b_lds_block_desc_bk0_n_bk1;
1016 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1017 constexpr
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
1019 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1026 return c_shuffle_block_desc_mblock_mperblock_nblock_nperblock;
1047 ABlockTransferSrcScalarPerVector,
1048 BBlockTransferSrcScalarPerVector,
1069 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1072 b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
1075 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1078 constexpr
auto c_block_size =
1079 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize();
1081 if constexpr(IsInputGemm)
1083 return math::max((a_block_space_size_aligned *
sizeof(ADataType) +
1084 b_block_space_size_aligned *
sizeof(BDataType)) *
1086 c_block_size *
sizeof(CShuffleDataType));
1090 return math::max((a_block_space_size_aligned *
sizeof(ADataType) +
1091 b_block_space_size_aligned *
sizeof(BDataType)),
1092 c_block_size *
sizeof(CShuffleDataType));
1099 static_assert((MPerBlock % (MPerXdl * MXdlPerWave) == 0) &&
1100 (NPerBlock % (NXdlPerWave * NPerXdl)) == 0,
1101 "Invalid tuning param!");
1103 static_assert(KPerBlock % (ScaleBlockSize /
BPackedSize) == 0,
1104 "KPerBlock should be multiple of ScaleBlockSize");
1112 if(!(karg.
M % MPerBlock == 0))
1116 std::cout <<
"Arg M value is not a multiple of MPerBlock! M: " << karg.
M <<
" "
1117 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1130 if(!(karg.
N % NPerBlock == 0))
1134 std::cout <<
"Arg N value is not a multiple of NPerBlock! N: " << karg.
N <<
" "
1135 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1147 auto K_t = karg.
KBatch * KPerBlock;
1148 if(!(karg.
K % K_t == 0))
1152 std::cout <<
"Arg K value is not a multiple of K_Batch * K0PerBlock * K1! K: "
1153 << karg.
K <<
" " << __FILE__ <<
":" << __LINE__
1154 <<
", in function: " << __func__ << std::endl;
1162 auto K_t = karg.
KBatch * KReadVec;
1164 if((KReadPadSplited * (karg.
KBatch - 1)) >= karg.
K)
1172 if(karg.
K % ABlockTransferSrcScalarPerVector != 0)
1176 std::cout <<
"Arg K (" << karg.
K
1177 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1178 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1179 << __LINE__ <<
", in function: " << __func__ << std::endl;
1186 if(karg.
M % ABlockTransferSrcScalarPerVector != 0)
1190 std::cout <<
"Arg M (" << karg.
M
1191 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1192 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1193 << __LINE__ <<
", in function: " << __func__ << std::endl;
1201 if(karg.
N % BBlockTransferSrcScalarPerVector != 0)
1205 std::cout <<
"Arg N (" << karg.
N
1206 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1207 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1208 << __LINE__ <<
", in function: " << __func__ << std::endl;
1215 if(karg.
K % BBlockTransferSrcScalarPerVector != 0)
1219 std::cout <<
"Arg K (" << karg.
K
1220 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1221 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1222 << __LINE__ <<
", in function: " << __func__ << std::endl;
1234 std::cout <<
"Arg N (" << karg.
N
1235 <<
") value is not a multiple of "
1236 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1238 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1250 std::cout <<
"Arg M (" << karg.
M
1251 <<
") value is not a multiple of "
1252 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1254 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1264 const auto num_k_loop = karg.
AK0 / (KPerBlock / AK1Value);
1266 if(num_k_loop <= BlockwiseGemmPipe::PrefetchStages)
1277 const index_t num_loop = K / KPerBlock;
1279 return BlockwiseGemmPipe::BlockHasHotloop(num_loop);
1284 const index_t num_loop = K / KPerBlock;
1286 return BlockwiseGemmPipe::BlockLoopTailNum(num_loop);
1289 template <
typename CGr
idDesc>
1291 const CGridDesc& c_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
1300 return c_grid_desc_mblock_mperblock_nblock_nperblock;
1312 "A scale pack data type too large!");
1314 "B scale pack data type too large!");
1316 template <
bool HasMainKBlockLoop,
1320 const index_t* p_sorted_expert_ids,
1321 const index_t* p_max_token_id,
1322 const ADataType* p_a_grid,
1323 const AScaleDataType* p_a_scale_grid,
1324 const BDataType* p_b_grid,
1325 const BScaleDataType* p_b_scale_grid,
1327 CDataType* p_c_grid,
1330 AElementwiseOperation a_element_op,
1331 BElementwiseOperation b_element_op,
1332 CElementwiseOperation c_element_op)
1344 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
1363 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
1367 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
1368 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
1369 if(expert_block_id * MPerBlock >= max_token_id)
1372 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
1374 const auto block_mn = [&]() -> std::pair<int, int> {
1375 if constexpr(NSwizzle)
1377 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
1379 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
1380 const index_t expert_swizzle =
1381 ecnt > 0 ? ecnt : 1;
1382 const index_t bid_new = blockIdx.x - prefix_block;
1383 const index_t nid = __builtin_amdgcn_readfirstlane(
1384 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
1386 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
1391 return {blockIdx.x, blockIdx.y};
1395 const index_t block_n_id = block_mn.first;
1396 const index_t block_m_id = block_mn.second;
1398 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
1401 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
1402 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
1403 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
1404 constexpr
auto AKThreads = AK0Threads * AK1Threads;
1405 constexpr
auto AMRepeats = MPerBlock / AMThreads;
1406 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
1408 if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
1412 const index_t fused_token = p_sorted_token_ids[token_pos + m0];
1413 index_t token_offset = fused_token & 0xffffff;
1414 if constexpr(!IsInputGemm)
1416 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
1418 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
K;
1422 __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
1423 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
1424 problem.
N * (IsInputGemm ? 2 : 1) *
1428 const index_t n_block_data_idx_on_grid =
1429 __builtin_amdgcn_readfirstlane(block_n_id * NPerBlock);
1432 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1433 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
1434 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1435 p_b_grid + expert_id * expert_stride, b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
1438 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1439 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
1440 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1441 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
1442 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1456 AElementwiseOperation,
1460 ABlockTransferThreadClusterLengths_AK0_M_AK1,
1461 ABlockTransferThreadClusterArrangeOrder,
1464 decltype(a_grid_desc_ak0_m_ak1),
1465 decltype(a_block_desc_ak0_m_ak1),
1466 ABlockTransferSrcAccessOrder,
1468 ABlockTransferSrcVectorDim,
1470 ABlockTransferSrcScalarPerVector,
1471 ABlockTransferDstScalarPerVector_AK1,
1474 AThreadTransferSrcResetCoordinateAfterRun,
1478 BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
1481 a_block_desc_ak0_m_ak1,
1487 auto b_blockwise_copy =
1489 BElementwiseOperation,
1493 BBlockTransferThreadClusterLengths_BK0_N_BK1,
1494 BBlockTransferThreadClusterArrangeOrder,
1497 decltype(b_grid_desc_bk0_n_bk1),
1498 decltype(b_block_desc_bk0_n_bk1),
1499 BBlockTransferSrcAccessOrder,
1501 BBlockTransferSrcVectorDim,
1503 BBlockTransferSrcScalarPerVector,
1504 BBlockTransferDstScalarPerVector_BK1,
1507 BThreadTransferSrcResetCoordinateAfterRun,
1509 BlockwiseGemmPipe::GlobalBufferNum>(
1510 b_grid_desc_bk0_n_bk1,
1513 b_block_desc_bk0_n_bk1,
1519 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1522 auto a_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1523 static_cast<ADataType*
>(p_shared), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
1525 auto b_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1526 reinterpret_cast<BDataType*
>(
static_cast<char*
>(p_shared) +
1527 a_block_space_size_aligned *
sizeof(ADataType)),
1528 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1534 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
1536 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
1537 decltype(c_thread_buf) c_thread_buf_up;
1541 c_thread_buf.num_of_v_,
1542 c_thread_buf.s_per_v,
1546 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
1547 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
1551 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
1552 const auto waveId_m = wave_idx[
I0];
1553 const auto waveId_n = wave_idx[
I1];
1555 auto thread_offset_shuffled =
1558 auto a_thread_offset_m = waveId_m;
1563 decltype(a_scale_grid_desc_am_ak),
1564 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
1570 true>(a_scale_grid_desc_am_ak,
1576 auto b_thread_offset_n = waveId_n;
1581 decltype(b_scale_grid_desc_bn_ak),
1582 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1588 true>(b_scale_grid_desc_bn_ak,
1593 if constexpr(IsInputGemm)
1596 b_block_desc_bk0_n_bk1.GetElementSpaceSize(), max_lds_align);
1597 auto b_block_buf_up = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1598 reinterpret_cast<BDataType*
>(
static_cast<char*
>(p_shared) +
1599 a_block_space_size_aligned *
sizeof(ADataType) +
1600 b_block_space_size_aligned *
sizeof(BDataType)),
1601 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1603 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
1604 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1605 p_b_grid_up + expert_id * expert_stride,
1606 b_grid_desc_bk0_n_bk1.GetElementSpaceSize());
1608 auto b_blockwise_copy_up =
1610 BElementwiseOperation,
1614 BBlockTransferThreadClusterLengths_BK0_N_BK1,
1615 BBlockTransferThreadClusterArrangeOrder,
1618 decltype(b_grid_desc_bk0_n_bk1),
1619 decltype(b_block_desc_bk0_n_bk1),
1620 BBlockTransferSrcAccessOrder,
1622 BBlockTransferSrcVectorDim,
1624 BBlockTransferSrcScalarPerVector,
1625 BBlockTransferDstScalarPerVector_BK1,
1628 BThreadTransferSrcResetCoordinateAfterRun,
1630 BlockwiseGemmPipe::GlobalBufferNum>(
1631 b_grid_desc_bk0_n_bk1,
1634 b_block_desc_bk0_n_bk1,
1638 const BScaleDataType* p_b_scale_grid_up =
1639 p_b_scale_grid + expert_scale_stride / 2 /
sizeof(BScaleDataType);
1640 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1641 p_b_scale_grid_up + expert_id * expert_scale_stride /
sizeof(BScaleDataType),
1642 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1647 decltype(b_scale_grid_desc_bn_ak),
1648 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1655 b_scale_grid_desc_bn_ak,
1660 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1662 a_grid_desc_ak0_m_ak1,
1663 a_block_desc_ak0_m_ak1,
1667 a_block_slice_copy_step,
1669 b_grid_desc_bk0_n_bk1,
1670 b_block_desc_bk0_n_bk1,
1672 b_blockwise_copy_up,
1677 b_block_slice_copy_step,
1682 a_scale_grid_desc_am_ak,
1683 a_scale_thread_copy,
1686 b_scale_grid_desc_bn_ak,
1687 b_scale_thread_copy,
1688 b_scale_thread_copy_up,
1690 b_scale_grid_buf_up,
1691 num_k_block_main_loop);
1695 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1696 a_grid_desc_ak0_m_ak1,
1697 a_block_desc_ak0_m_ak1,
1701 a_block_slice_copy_step,
1702 b_grid_desc_bk0_n_bk1,
1703 b_block_desc_bk0_n_bk1,
1707 b_block_slice_copy_step,
1709 a_scale_grid_desc_am_ak,
1710 a_scale_thread_copy,
1712 b_scale_grid_desc_bn_ak,
1713 b_scale_thread_copy,
1715 num_k_block_main_loop);
1720 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
1721 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
1723 static_assert(CShuffleMXdlPerWavePerShuffle %
MXdlPack == 0 &&
1724 CShuffleNXdlPerWavePerShuffle %
NXdlPack == 0,
1727 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1728 constexpr
index_t NWave = NPerBlock / (NXdlPerWave * NPerXdl);
1731 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
1732 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
1736 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
1737 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
1739 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
1740 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
1741 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
1742 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
1743 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
1744 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
1745 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
1746 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
1747 constexpr
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
1748 constexpr
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
1751 static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
1752 static_assert(M5 == 4);
1762 const index_t m_pos = block_m_id * MPerBlock +
1763 m0 * M2 * M1 * M3 * M4 * M5 +
1764 m1 * M2 * M3 * M4 * M5 +
1765 imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
1767 if constexpr(MulRoutedWeight)
1770 *c_style_pointer_cast<const vector_type<float, M5>*>(
1771 p_ds_grid[
I2] + m_pos);
1775 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
1776 make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
1779 if constexpr(IsInputGemm)
1781 if constexpr(ActivationOperation ==
1784 float gate = c_thread_buf[cidx];
1785 float up = c_thread_buf_up[cidx];
1786 if constexpr(MulRoutedWeight)
1788 gate = gate * topk_weights.AsType<
float>()[m5];
1789 up = up * topk_weights.AsType<
float>()[m5];
1792 c_thread_buf_fp32(cidx) = gate * up;
1796 float gate = c_thread_buf[cidx];
1797 float up = c_thread_buf_up[cidx];
1798 if constexpr(MulRoutedWeight)
1800 gate = gate * topk_weights.AsType<
float>()[m5];
1801 up = up * topk_weights.AsType<
float>()[m5];
1804 c_thread_buf_fp32(cidx) = gate * up;
1819 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
1820 if constexpr(MulRoutedWeight)
1822 c_thread_buf_fp32(cidx) =
1823 topk_weights.AsType<
float>()[m5] *
1824 c_thread_buf_fp32[cidx];
1834 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1837 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1838 static_cast<CShuffleDataType*
>(p_shared),
1839 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1842 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
1868 const auto c_thread_mtx_on_block =
1869 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
1871 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
1872 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
1874 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
1880 const auto m_thread_data_on_block_idx =
1881 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
1884 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
1890 const auto n_thread_data_on_block_idx =
1891 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
1898 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1899 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1902 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
1911 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
1916 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1919 m_thread_data_on_block_idx[
I1],
1920 n_thread_data_on_block_idx[
I1],
1921 m_thread_data_on_block_idx[
I2],
1922 n_thread_data_on_block_idx[
I2],
1923 m_thread_data_on_block_idx[
I3],
1924 m_thread_data_on_block_idx[
I4],
1925 m_thread_data_on_block_idx[
I5],
1926 n_thread_data_on_block_idx[
I3]),
1929 using EDataType = CDataType;
1934 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
1940 return make_dynamic_buffer<AddressSpaceEnum::Global>(
1941 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
1947 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
1949 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
1954 tie(c_shuffle_block_buf),
1956 {
return ds_grid_buf[i]; },
1960 const auto idx_c_ds_block_begin =
1970 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
1971 c_grid_desc_mblock_mperblock_nblock_nperblock;
1973 using CDEBlockTransferCluster =
1974 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
1975 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
1976 constexpr
index_t scatter_weight_idx = 3;
1981 decltype(c_ds_desc_refs),
1982 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
1983 CElementwiseOperation,
1988 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1990 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>,
1991 CDEBlockTransferCluster,
1997 CDEShuffleBlockTransferScalarPerVectors,
2009 idx_c_ds_block_begin,
2010 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2014 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2015 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2017 constexpr
auto sfc_c_vgpr =
2028 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2030 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2040 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2043 constexpr
auto sfc_cde_block =
2047 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2049 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>>{};
2051 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2052 constexpr
auto EMThreads =
2053 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2054 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2055 constexpr
auto ENThreads =
2056 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2061 auto dstidx = sfc_cde_block.GetIndex(access_id);
2063 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2065 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2066 IndexType token_offset = fused_token & 0xffffff;
2067 if constexpr(IsInputGemm)
2069 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2071 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
N;
2077 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2078 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2080 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2081 c_shuffle_block_buf);
2087 cde_block_copy_lds_and_global.Run(
2090 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2094 if constexpr(access_id < num_access - 1)
2096 constexpr
auto cde_lds_and_global_step =
2097 sfc_cde_block.GetForwardStep(access_id);
2101 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2102 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2106 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2107 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2109 cde_lds_and_global_step);
2116 template <
bool HasMainKBlockLoop,
2119 __device__
static void Run_2Lds(
const index_t* p_sorted_token_ids,
2120 const index_t* p_sorted_expert_ids,
2121 const index_t* p_max_token_id,
2122 const ADataType* p_a_grid,
2123 const AScaleDataType* p_a_scale_grid,
2124 const BDataType* p_b_grid,
2125 const BScaleDataType* p_b_scale_grid,
2127 CDataType* p_c_grid,
2130 const Problem& problem,
2131 AElementwiseOperation a_element_op,
2132 BElementwiseOperation b_element_op,
2133 CElementwiseOperation c_element_op)
2137 IsInputGemm ? problem.NumTokens : problem.NumTokens * problem.TopK,
2144 problem.K, problem.KPadded, problem.N, problem.NPadded, problem.StrideB, problem.BK0);
2145 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
2146 IsInputGemm ? problem.NumTokens * problem.TopK : problem.NumTokens,
2164 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
2166 c_grid_desc_m_n, problem.MBlock, problem.NBlock);
2167 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
2169 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.NBlock : blockIdx.y;
2170 if(expert_block_id * MPerBlock >= max_token_id)
2173 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
2174 const auto block_mn = [&]() -> std::pair<int, int> {
2175 if constexpr(NSwizzle)
2177 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
2178 const index_t prefix_block = ecnt_prefix * problem.NBlock;
2179 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
2180 const index_t expert_swizzle =
2181 ecnt > 0 ? ecnt : 1;
2182 const index_t bid_new = blockIdx.x - prefix_block;
2183 const index_t nid = __builtin_amdgcn_readfirstlane(
2184 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
2186 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
2191 return {blockIdx.x, blockIdx.y};
2195 const index_t block_n_id = block_mn.first;
2196 const index_t block_m_id = block_mn.second;
2198 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
2201 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
2202 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
2203 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
2204 constexpr
auto AKThreads = AK0Threads * AK1Threads;
2205 constexpr
auto AMRepeats = MPerBlock / AMThreads;
2206 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
2208 if(token_pos >= max_token_id || token0 >= problem.NumTokens)
2210 StaticallyIndexedArray<IndexType, AMRepeats> gather_offsets;
2211 static_for<0, AMRepeats, 1>{}([&](
auto m0) {
2212 const index_t fused_token = p_sorted_token_ids[token_pos + m0];
2213 index_t token_offset = fused_token & 0xffffff;
2214 if constexpr(!IsInputGemm)
2216 token_offset = token_offset * problem.TopK + (fused_token >> 24);
2218 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.K;
2222 __builtin_amdgcn_readfirstlane(problem.N * problem.K * (IsInputGemm ? 2 : 1));
2223 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
2227 const index_t n_block_data_idx_on_grid =
2228 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave);
2230 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2231 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
2233 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2234 p_b_grid + expert_id * expert_stride, b_grid_desc_bpreshuffled.GetElementSpaceSize());
2236 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2237 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
2238 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2239 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
2240 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2249 auto a_blockwise_copy = ThreadGroupTensorSliceTransfer_v4r1_gather<
2251 AElementwiseOperation,
2254 Sequence<AK0Number, MPerBlock, AK1Number>,
2255 ABlockTransferThreadClusterLengths_AK0_M_AK1,
2256 ABlockTransferThreadClusterArrangeOrder,
2259 decltype(a_grid_desc_ak0_m_ak1),
2260 decltype(a_block_desc_ak0_m_ak1),
2261 ABlockTransferSrcAccessOrder,
2263 ABlockTransferSrcVectorDim,
2265 ABlockTransferSrcScalarPerVector,
2266 ABlockTransferDstScalarPerVector_AK1,
2269 AThreadTransferSrcResetCoordinateAfterRun,
2273 BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
2276 a_block_desc_ak0_m_ak1,
2283 auto b_block_buf_ping = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2284 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2285 auto b_block_buf_pong = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2286 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2287 auto b_block_bufs =
make_tuple(b_block_buf_ping, b_block_buf_pong);
2289 auto b_blockwise_copy =
2290 ThreadwiseTensorSliceTransfer_v2<BDataType,
2292 decltype(b_grid_desc_bpreshuffled),
2293 decltype(b_block_desc_bk0_n_bk1),
2298 Number<BK1Value>{}>,
2299 Sequence<1, 2, 0, 3, 4>,
2301 BBlockTransferSrcScalarPerVector,
2302 BThreadTransferSrcResetCoordinateAfterRun,
2304 b_grid_desc_bpreshuffled,
2313 auto a_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2314 static_cast<ADataType*
>(p_shared), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2315 auto a_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2316 static_cast<ADataType*
>(p_shared1), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2317 auto a_block_bufs =
make_tuple(a_block_buf_ping, a_block_buf_pong);
2320 constexpr
auto b_block_slice_copy_step =
make_multi_index(0, 0, 0, KRepeat, 0);
2323 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
2325 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
2326 decltype(c_thread_buf) c_thread_buf_up;
2330 c_thread_buf.num_of_v_,
2331 c_thread_buf.s_per_v,
2335 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
2336 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
2340 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
2341 const auto waveId_m = wave_idx[
I0];
2342 const auto waveId_n = wave_idx[
I1];
2344 auto thread_offset_shuffled =
2347 auto a_thread_offset_m = waveId_m;
2350 const index_t token_scale_pos = block_m_id * MPerBlock;
2351 if(token_scale_pos >= max_token_id || token0 >= problem.NumTokens)
2354 auto a_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
2357 decltype(a_scale_grid_desc_am_ak),
2358 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
2364 true>(a_scale_grid_desc_am_ak,
2370 auto b_thread_offset_n = waveId_n;
2372 auto b_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
2375 decltype(b_scale_grid_desc_bn_ak),
2376 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2382 true>(b_scale_grid_desc_bn_ak,
2387 if constexpr(IsInputGemm)
2389 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2 /
BPackedSize;
2390 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2391 p_b_grid_up + expert_id * expert_stride /
BPackedSize,
2392 b_grid_desc_bpreshuffled.GetElementSpaceSize());
2393 auto b_blockwise_copy_up = ThreadwiseTensorSliceTransfer_v2<
2396 decltype(b_grid_desc_bpreshuffled),
2397 decltype(b_block_desc_bk0_n_bk1),
2398 Sequence<Number<NXdlPerWave>{},
I1, Number<KRepeat>{}, Number<BK1Value>{}>,
2399 Sequence<1, 2, 0, 3>,
2401 BBlockTransferSrcScalarPerVector,
2402 BThreadTransferSrcResetCoordinateAfterRun,
2403 true>(b_grid_desc_bpreshuffled,
2408 const BScaleDataType* p_b_scale_grid_up = p_b_scale_grid + expert_scale_stride / 2;
2409 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2410 p_b_scale_grid_up + expert_id * expert_scale_stride,
2411 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2412 auto b_scale_thread_copy_up = ThreadwiseTensorSliceTransfer_v2<
2415 decltype(b_scale_grid_desc_bn_ak),
2416 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2423 b_scale_grid_desc_bn_ak,
2428 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2429 a_grid_desc_ak0_m_ak1,
2430 a_block_desc_ak0_m_ak1,
2434 a_block_slice_copy_step,
2435 b_grid_desc_bpreshuffled,
2436 b_block_desc_bk0_n_bk1,
2438 b_blockwise_copy_up,
2442 b_block_slice_copy_step,
2445 a_scale_grid_desc_am_ak,
2446 a_scale_thread_copy,
2448 b_scale_grid_desc_bn_ak,
2449 b_scale_thread_copy,
2450 b_scale_thread_copy_up,
2452 b_scale_grid_buf_up,
2453 num_k_block_main_loop);
2457 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2458 a_grid_desc_ak0_m_ak1,
2459 a_block_desc_ak0_m_ak1,
2463 a_block_slice_copy_step,
2464 b_grid_desc_bpreshuffled,
2465 b_block_desc_bk0_n_bk1,
2469 b_block_slice_copy_step,
2471 a_scale_grid_desc_am_ak,
2472 a_scale_thread_copy,
2474 b_scale_grid_desc_bn_ak,
2475 b_scale_thread_copy,
2477 num_k_block_main_loop);
2482 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
2483 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
2487 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
2488 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
2492 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
2493 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
2495 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
2496 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
2497 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
2498 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
2499 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
2500 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
2501 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
2502 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
2506 static_assert(M0 * M1 * M2 * M3 * M4 == MPerBlock);
2507 static_assert(M4 == 4);
2511 vector_type<float, 4> topk_weights;
2512 static_for<0, NXdlPerWave, 1>{}([&](
auto n0) {
2513 static_for<0, MXdlPerWave, 1>{}([&](
auto m0) {
2514 static_for<0, M2, 1>{}([&](
auto m2) {
2515 const index_t m_pos = block_m_id * MPerBlock + m0 * M1 * M2 * M3 * M4 +
2516 m1 * M2 * M3 * M4 + m2 * M3 * M4 + m3 * M4;
2517 if constexpr(MulRoutedWeight)
2519 topk_weights = *c_style_pointer_cast<const vector_type<float, M4>*>(
2520 p_ds_grid[
I2] + m_pos);
2522 static_for<0, M4, 1>{}([&](
auto m4) {
2524 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
2530 constexpr
auto cidx = Number<c_offset>{};
2532 if constexpr(IsInputGemm)
2536 float gate = c_thread_buf[cidx];
2537 float up = c_thread_buf_up[cidx];
2538 if constexpr(MulRoutedWeight)
2540 gate = gate * topk_weights.AsType<
float>()[m4];
2541 up = up * topk_weights.AsType<
float>()[m4];
2543 tensor_operation::element_wise::Silu{}(gate, gate);
2544 c_thread_buf_fp32(cidx) = gate * up;
2548 float gate = c_thread_buf[cidx];
2549 float up = c_thread_buf_up[cidx];
2550 if constexpr(MulRoutedWeight)
2552 gate = gate * topk_weights.AsType<
float>()[m4];
2553 up = up * topk_weights.AsType<
float>()[m4];
2555 tensor_operation::element_wise::Gelu{}(gate, gate);
2556 c_thread_buf_fp32(cidx) = gate * up;
2561 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
2562 if constexpr(MulRoutedWeight)
2564 c_thread_buf_fp32(cidx) =
2565 topk_weights.AsType<
float>()[m4] * c_thread_buf_fp32[cidx];
2573 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
2576 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2577 static_cast<CShuffleDataType*
>(p_shared),
2578 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2581 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
2584 Number<CShuffleMXdlPerWavePerShuffle>{},
2592 Number<CShuffleNXdlPerWavePerShuffle>{},
2596 make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}),
2598 Sequence<>{}, Sequence<0, 2, 4, 5, 6>{}, Sequence<>{}, Sequence<1, 3, 7>{}));
2602 const auto c_thread_mtx_on_block =
2603 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
2605 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
2606 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
2608 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
2614 const auto m_thread_data_on_block_idx =
2615 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
2618 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
2624 const auto n_thread_data_on_block_idx =
2625 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
2629 auto c_thread_copy_vgpr_to_lds =
2630 ThreadwiseTensorSliceTransfer_v1r3<AccDataType,
2632 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2633 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2635 Sequence<CShuffleMXdlPerWavePerShuffle,
2636 CShuffleNXdlPerWavePerShuffle,
2643 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
2649 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2652 m_thread_data_on_block_idx[
I1],
2653 n_thread_data_on_block_idx[
I1],
2654 m_thread_data_on_block_idx[
I2],
2655 m_thread_data_on_block_idx[
I3],
2656 m_thread_data_on_block_idx[
I4],
2657 n_thread_data_on_block_idx[
I2]),
2660 using EDataType = CDataType;
2663 problem.M, problem.MPadded, problem.N, problem.NPadded, problem.StrideDs);
2665 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
2667 ds_grid_desc_m_n, problem.MBlock, problem.NBlock);
2671 return make_dynamic_buffer<AddressSpaceEnum::Global>(
2672 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
2674 Number<NumDTensor>{});
2678 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
2680 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
2681 Number<NumDTensor>{}));
2685 tie(c_shuffle_block_buf),
2687 {
return ds_grid_buf[i]; },
2688 Number<NumDTensor>{}));
2691 const auto idx_c_ds_block_begin =
2699 Number<NumDTensor>{}));
2701 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
2702 c_grid_desc_mblock_mperblock_nblock_nperblock;
2704 using CDEBlockTransferCluster =
2705 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
2706 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
2707 constexpr
index_t scatter_weight_idx = 3;
2708 auto cde_block_copy_lds_and_global = ThreadGroupTensorSliceTransfer_v7r3_scatter<
2712 decltype(c_ds_desc_refs),
2713 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
2714 CElementwiseOperation,
2715 Sequence<static_cast<index_t>(EGlobalMemoryDataOperation)>,
2719 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2721 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>,
2722 CDEBlockTransferCluster,
2723 Sequence<0, 1, 2, 3>,
2724 Sequence<0, 1, 2, 3>,
2725 Sequence<0, 1, 2, 3>,
2728 CDEShuffleBlockTransferScalarPerVectors,
2740 idx_c_ds_block_begin,
2741 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2745 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2746 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2747 constexpr
auto sfc_c_vgpr =
2748 SpaceFillingCurve<Sequence<MXdlPerWave, NXdlPerWave, 1, 1, M2, 1, M4, 1>,
2749 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
2750 Sequence<CShuffleMXdlPerWavePerShuffle,
2751 CShuffleNXdlPerWavePerShuffle,
2759 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2762 constexpr
auto sfc_cde_block =
2763 SpaceFillingCurve<Sequence<1, MPerBlock, 1, NPerBlock>,
2764 Sequence<0, 2, 1, 3>,
2766 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2768 CShuffleNXdlPerWavePerShuffle * NWave * NPerXdl>>{};
2770 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2771 constexpr
auto EMThreads =
2772 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2773 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2774 constexpr
auto ENThreads =
2775 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2776 static_for<0, num_access, 1>{}([&](
auto access_id) {
2778 StaticallyIndexedArray<IndexType, EMRepeats> scatter_offsets;
2780 auto dstidx = sfc_cde_block.GetIndex(access_id);
2782 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2783 static_for<0, EMRepeats, 1>{}([&](
auto m0) {
2784 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2785 IndexType token_offset = fused_token & 0xffffff;
2786 if constexpr(IsInputGemm)
2788 token_offset = token_offset * problem.TopK + (fused_token >> 24);
2790 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.N;
2796 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2797 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2799 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2800 c_shuffle_block_buf);
2806 cde_block_copy_lds_and_global.Run(
2809 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2813 if constexpr(access_id < num_access - 1)
2815 constexpr
auto cde_lds_and_global_step =
2816 sfc_cde_block.GetForwardStep(access_id);
2819 static_for<0, NumDTensor, 1>{}([&](
auto i) {
2820 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2821 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2825 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2826 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2828 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: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
constexpr auto BlockGemmMXNBSPipeline_Selector()
Definition: blockwise_gemm_pipeline_xdlops_mx_moe_nbs_selector.hpp:37
integral_constant< index_t, N > Number
Definition: number.hpp:12
Definition: gridwise_moe_mx_gemm_bns.hpp:648
const ADataType * p_a_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:711
const index_t * p_sorted_token_ids
Definition: gridwise_moe_mx_gemm_bns.hpp:708
const index_t * p_sorted_expert_ids
Definition: gridwise_moe_mx_gemm_bns.hpp:709
const index_t * p_max_token_id
Definition: gridwise_moe_mx_gemm_bns.hpp:710
DsGridPointer p_ds_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:715
const CElementwiseOperation c_element_op
Definition: gridwise_moe_mx_gemm_bns.hpp:720
CDataType * p_c_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:716
const AElementwiseOperation a_element_op
Definition: gridwise_moe_mx_gemm_bns.hpp:718
const BScaleDataType * p_b_scale_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:714
const BDataType * p_b_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:713
const AScaleDataType * p_a_scale_grid
Definition: gridwise_moe_mx_gemm_bns.hpp:712
__host__ Argument(const index_t *p_sorted_token_ids_, const index_t *p_sorted_expert_ids_, const index_t *p_max_token_id_, const ADataType *p_a_grid_, const AScaleDataType *p_a_scale_grid_, const BDataType *p_b_grid_, const BScaleDataType *p_b_scale_grid_, std::array< const void *, NumDTensor > p_ds_grid_, CDataType *p_c_grid_, index_t NumTokens_, index_t TopK_, index_t M_, index_t N_, index_t K_, index_t StrideA_, index_t StrideScaleA_, index_t StrideB_, index_t StrideScaleB_, std::array< index_t, NumDTensor > StrideDs_, index_t StrideC_, index_t k_batch_, AElementwiseOperation a_element_op_, BElementwiseOperation b_element_op_, CElementwiseOperation c_element_op_)
Definition: gridwise_moe_mx_gemm_bns.hpp:649
const BElementwiseOperation b_element_op
Definition: gridwise_moe_mx_gemm_bns.hpp:719
Definition: gridwise_moe_mx_gemm_bns.hpp:576
index_t M
Definition: gridwise_moe_mx_gemm_bns.hpp:626
index_t TopK
Definition: gridwise_moe_mx_gemm_bns.hpp:625
index_t NPadded
Definition: gridwise_moe_mx_gemm_bns.hpp:637
index_t MPadded
Definition: gridwise_moe_mx_gemm_bns.hpp:636
index_t StrideScaleB
Definition: gridwise_moe_mx_gemm_bns.hpp:632
index_t StrideScaleA
Definition: gridwise_moe_mx_gemm_bns.hpp:630
index_t MBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:642
index_t StrideC
Definition: gridwise_moe_mx_gemm_bns.hpp:634
index_t AK0
Definition: gridwise_moe_mx_gemm_bns.hpp:640
index_t KPadded
Definition: gridwise_moe_mx_gemm_bns.hpp:639
index_t NBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:643
__host__ Problem(index_t NumTokens_, index_t TopK_, index_t M_, index_t N_, index_t K_, index_t StrideA_, index_t StrideScaleA_, index_t StrideB_, index_t StrideScaleB_, std::array< index_t, NumDTensor > StrideDs_, index_t StrideC_, index_t KBatch_)
Definition: gridwise_moe_mx_gemm_bns.hpp:577
index_t StrideA
Definition: gridwise_moe_mx_gemm_bns.hpp:629
index_t StrideB
Definition: gridwise_moe_mx_gemm_bns.hpp:631
index_t KBatch
Definition: gridwise_moe_mx_gemm_bns.hpp:635
index_t BK0
Definition: gridwise_moe_mx_gemm_bns.hpp:641
index_t KRead
Definition: gridwise_moe_mx_gemm_bns.hpp:638
__host__ void Print() const
Definition: gridwise_moe_mx_gemm_bns.hpp:612
index_t K
Definition: gridwise_moe_mx_gemm_bns.hpp:628
index_t N
Definition: gridwise_moe_mx_gemm_bns.hpp:627
index_t NumTokens
Definition: gridwise_moe_mx_gemm_bns.hpp:624
std::array< index_t, NumDTensor > StrideDs
Definition: gridwise_moe_mx_gemm_bns.hpp:633
Definition: gridwise_moe_mx_gemm_bns.hpp:724
index_t a_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:778
index_t b_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:779
__device__ SplitKBatchOffset(Argument &karg, index_t k_id)
Definition: gridwise_moe_mx_gemm_bns.hpp:725
index_t b_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:781
index_t a_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bns.hpp:780
Definition: gridwise_moe_mx_gemm_bns.hpp:173
static constexpr auto is_scale_mfma
Definition: gridwise_moe_mx_gemm_bns.hpp:206
static constexpr auto I1
Definition: gridwise_moe_mx_gemm_bns.hpp:178
static constexpr index_t scale_pack_size_a
Definition: gridwise_moe_mx_gemm_bns.hpp:1309
static constexpr index_t NumDTensor
Definition: gridwise_moe_mx_gemm_bns.hpp:196
static __host__ auto CalculateMPadded(index_t M)
Definition: gridwise_moe_mx_gemm_bns.hpp:244
static constexpr auto MXdlPack
Definition: gridwise_moe_mx_gemm_bns.hpp:198
static constexpr auto BK0Number
Definition: gridwise_moe_mx_gemm_bns.hpp:192
static constexpr auto BK1Number
Definition: gridwise_moe_mx_gemm_bns.hpp:194
ADataType LDSTypeA
Definition: gridwise_moe_mx_gemm_bns.hpp:174
static constexpr __host__ bool CalculateHasMainKBlockLoop(index_t K)
Definition: gridwise_moe_mx_gemm_bns.hpp:1275
static constexpr bool is_single_rate_mfma
Definition: gridwise_moe_mx_gemm_bns.hpp:205
ThisThreadBlock< BlockSize > ThisThreadBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:232
static constexpr auto I5
Definition: gridwise_moe_mx_gemm_bns.hpp:182
__host__ static __device__ auto MakeBGridDescriptor_BK0_N_BK1(index_t K, index_t KPad, index_t N, index_t NPad, index_t StrideB, index_t BK0)
Definition: gridwise_moe_mx_gemm_bns.hpp:397
BDataType LDSTypeB
Definition: gridwise_moe_mx_gemm_bns.hpp:175
static constexpr __host__ TailNumber CalculateKBlockLoopTailNum(index_t K)
Definition: gridwise_moe_mx_gemm_bns.hpp:1282
decltype(MakeDsGridPointer()) DsGridPointer
Definition: gridwise_moe_mx_gemm_bns.hpp:230
__host__ static __device__ auto MakeAGridDescriptor_AK0_M_AK1(IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
Definition: gridwise_moe_mx_gemm_bns.hpp:315
static constexpr __host__ bool CheckValidity(const Argument &karg)
Definition: gridwise_moe_mx_gemm_bns.hpp:1097
__host__ static __device__ auto MakeDsGridDescriptor_M_N(index_t M, index_t MPad, index_t N, index_t NPad, std::array< index_t, NumDTensor > StrideDs)
Definition: gridwise_moe_mx_gemm_bns.hpp:552
static __device__ void Run(const index_t *p_sorted_token_ids, const index_t *p_sorted_expert_ids, const index_t *p_max_token_id, const ADataType *p_a_grid, const AScaleDataType *p_a_scale_grid, const BDataType *p_b_grid, const BScaleDataType *p_b_scale_grid, DsGridPointer &p_ds_grid, CDataType *p_c_grid, void *p_shared, const Problem &problem, AElementwiseOperation a_element_op, BElementwiseOperation b_element_op, CElementwiseOperation c_element_op)
Definition: gridwise_moe_mx_gemm_bns.hpp:1319
static constexpr auto I4
Definition: gridwise_moe_mx_gemm_bns.hpp:181
static constexpr __device__ index_t GetSharedMemoryNumberOfByte()
Definition: gridwise_moe_mx_gemm_bns.hpp:1059
static constexpr index_t APackedSize
Definition: gridwise_moe_mx_gemm_bns.hpp:202
static constexpr auto MakeDsGridPointer()
Definition: gridwise_moe_mx_gemm_bns.hpp:219
static constexpr __device__ auto MakeDsGridDescriptor_MBlock_MPerBlock_NBlock_NPerBlock(const DsGridDesc &ds_grid_desc_m_n, index_t MBlock, index_t NBlock)
Definition: gridwise_moe_mx_gemm_bns.hpp:564
static __host__ auto CalculateNPadded(index_t N)
Definition: gridwise_moe_mx_gemm_bns.hpp:249
static __host__ auto CalculateAK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:259
__host__ static constexpr __device__ auto MakeBMmaTileDescriptor_N0_N1_N2_N3_K(const BBlockDesc_BK0_N_BK1 &)
Definition: gridwise_moe_mx_gemm_bns.hpp:498
static constexpr auto I8
Definition: gridwise_moe_mx_gemm_bns.hpp:185
static constexpr auto I7
Definition: gridwise_moe_mx_gemm_bns.hpp:184
static constexpr auto KXdlPack
Definition: gridwise_moe_mx_gemm_bns.hpp:200
__host__ static constexpr __device__ auto MakeCGridDescriptor_MBlock_MPerBlock_NBlock_NPerBlock(const CGridDesc &c_grid_desc_m_n, index_t MBlock, index_t NBlock)
Definition: gridwise_moe_mx_gemm_bns.hpp:1290
static constexpr auto I2
Definition: gridwise_moe_mx_gemm_bns.hpp:179
__host__ static constexpr __device__ auto MakeAMmaTileDescriptor_M0_M1_M2_M3_K(const ABlockDesc_AK0_M_AK1 &)
Definition: gridwise_moe_mx_gemm_bns.hpp:488
static constexpr index_t scale_pack_size_b
Definition: gridwise_moe_mx_gemm_bns.hpp:1310
static __host__ auto CalculateKRead(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:277
__host__ static constexpr __device__ auto MakeGemmMmaTileDescriptor(const TileDesc_K0_MN_K1 &)
Definition: gridwise_moe_mx_gemm_bns.hpp:299
remove_cvref_t< decltype(BlockGemmMXNBSPipeline_Selector< BlkGemmPipelineVer, BlkGemmPipeSched, BlockSize, ScaleBlockSize, ADataType, AScaleDataType, BDataType, BScaleDataType, ComputeTypeA, AccDataType, decltype(GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()), decltype(GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()), decltype(MakeAMmaTileDescriptor_M0_M1_M2_M3_K(GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1())), decltype(MakeBMmaTileDescriptor_N0_N1_N2_N3_K(GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1())), ABlockTransferSrcScalarPerVector, BBlockTransferSrcScalarPerVector, MPerBlock, NPerBlock, KPerBlock, MPerXdl, NPerXdl, MXdlPerWave, NXdlPerWave, KPack, IsInputGemm >())> BlockwiseGemmPipe
Definition: gridwise_moe_mx_gemm_bns.hpp:1057
static constexpr auto I6
Definition: gridwise_moe_mx_gemm_bns.hpp:183
static constexpr index_t KPack
Definition: gridwise_moe_mx_gemm_bns.hpp:213
static __host__ auto CalculateBK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:265
static constexpr auto I3
Definition: gridwise_moe_mx_gemm_bns.hpp:180
static constexpr auto AK1Number
Definition: gridwise_moe_mx_gemm_bns.hpp:193
static constexpr __device__ auto GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()
Definition: gridwise_moe_mx_gemm_bns.hpp:901
static constexpr index_t SortedTileSize
Definition: gridwise_moe_mx_gemm_bns.hpp:217
static constexpr auto I9
Definition: gridwise_moe_mx_gemm_bns.hpp:186
__host__ static __device__ auto MakeDGridDescriptor_M_N(index_t M, index_t MPad, index_t N, index_t NPad, index_t StrideC)
Definition: gridwise_moe_mx_gemm_bns.hpp:531
static constexpr auto AK0Number
Definition: gridwise_moe_mx_gemm_bns.hpp:191
static constexpr __device__ auto GetCShuffleBlockDescriptor_MBlock_MPerBlock_NBlock_NPerBlock()
Definition: gridwise_moe_mx_gemm_bns.hpp:1014
static __host__ auto CalculateMBlock(index_t M)
Definition: gridwise_moe_mx_gemm_bns.hpp:284
static constexpr auto CShuffleBlockTransferScalarPerVector_NPerBlock
Definition: gridwise_moe_mx_gemm_bns.hpp:188
static __host__ auto CalculateKPadded(index_t K)
Definition: gridwise_moe_mx_gemm_bns.hpp:254
static constexpr auto NXdlPack
Definition: gridwise_moe_mx_gemm_bns.hpp:199
static __host__ auto CalculateNBlock(index_t N)
Definition: gridwise_moe_mx_gemm_bns.hpp:289
static constexpr index_t BPackedSize
Definition: gridwise_moe_mx_gemm_bns.hpp:203
__host__ static __device__ auto MakeCGridDescriptor_M_N(IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
Definition: gridwise_moe_mx_gemm_bns.hpp:507
static __host__ auto CalculateKPadded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bns.hpp:271
static constexpr __device__ auto GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()
Definition: gridwise_moe_mx_gemm_bns.hpp:784
static __host__ auto CalculateGridSize(index_t M, index_t N)
Definition: gridwise_moe_mx_gemm_bns.hpp:234
static constexpr auto I0
Definition: gridwise_moe_mx_gemm_bns.hpp:177
Definition: xdlops_gemm.hpp:942
static constexpr auto selected_mfma
Definition: xdlops_gemm.hpp:1343
Definition: sequence.hpp:43
Definition: tensor_space_filling_curve.hpp:20
Definition: static_buffer.hpp:75
Blockwise data transfer.
Definition: thread_group_tensor_slice_transfer_v4r1_gather.hpp:48
Blockwise data transfer.
Definition: thread_group_tensor_slice_transfer_v4r1.hpp:46
Definition: thread_group_tensor_slice_transfer_v7r3_scatter.hpp:51
Definition: threadwise_tensor_slice_transfer.hpp:39
Helper structure that facilitates transfer of source (grid) data to destination threads.
Definition: threadwise_tensor_slice_transfer.hpp:234
Definition: tuple.hpp:117
Unsigned representation of a conventional biased Float32 exponent.
Definition: e8m0.hpp:25
Definition: data_type.hpp:41
Definition: integral_constant.hpp:20
Definition: data_type.hpp: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