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,
75 template <
typename GridwiseGemm,
76 bool HasMainKBlockLoop,
81 #if CK_USE_LAUNCH_BOUNDS
88 __shared__
char p_shared_0[GridwiseGemm::GetSharedMemoryNumberOfByte()];
89 __shared__
char p_shared_1[GridwiseGemm::GetSharedMemoryNumberOfByte()];
91 auto splitk_batch_offset =
typename GridwiseGemm::SplitKBatchOffset(karg, blockIdx.z);
93 GridwiseGemm::template Run_2Lds<HasMainKBlockLoop, CGlobalMemoryDataOperation, TailNum>(
94 karg.p_sorted_token_ids,
95 karg.p_sorted_expert_ids,
97 karg.p_a_grid + splitk_batch_offset.a_k_split_offset,
98 karg.p_a_scale_grid + splitk_batch_offset.a_scale_k_split_offset,
99 karg.p_b_grid + splitk_batch_offset.b_k_split_offset,
100 karg.p_b_scale_grid + splitk_batch_offset.b_scale_k_split_offset,
114 template <
typename ALayout,
119 typename AScaleDataType,
121 typename BScaleDataType,
122 typename AccDataType,
123 typename CShuffleDataType,
126 typename AElementwiseOperation,
127 typename BElementwiseOperation,
128 typename CElementwiseOperation,
141 typename ABlockTransferThreadClusterLengths_AK0_M_AK1,
142 typename ABlockTransferThreadClusterArrangeOrder,
143 typename ABlockTransferSrcAccessOrder,
144 index_t ABlockTransferSrcVectorDim,
145 index_t ABlockTransferSrcScalarPerVector,
146 index_t ABlockTransferDstScalarPerVector_AK1,
147 bool AThreadTransferSrcResetCoordinateAfterRun,
149 typename BBlockTransferThreadClusterLengths_BK0_N_BK1,
150 typename BBlockTransferThreadClusterArrangeOrder,
151 typename BBlockTransferSrcAccessOrder,
152 index_t BBlockTransferSrcVectorDim,
153 index_t BBlockTransferSrcScalarPerVector,
154 index_t BBlockTransferDstScalarPerVector_BK1,
155 bool BThreadTransferSrcResetCoordinateAfterRun,
157 index_t CShuffleMXdlPerWavePerShuffle,
158 index_t CShuffleNXdlPerWavePerShuffle,
159 typename CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock,
160 typename CDEShuffleBlockTransferScalarPerVectors,
163 index_t ActivationOperation = 0,
164 bool NSwizzle =
false,
165 bool IsInputGemm =
true,
166 bool MulRoutedWeight =
true,
168 typename ComputeTypeA = ADataType,
169 typename ComputeTypeB = BDataType>
187 CDEShuffleBlockTransferScalarPerVectors{}[
I0];
234 "A scale pack data type too large!");
236 "B scale pack data type too large!");
244 return static_cast<const DDataType*
>(
nullptr);
257 const index_t gridx = NSwizzle ? nblock * mblock : nblock;
258 const index_t gridy = NSwizzle ? 1 : mblock;
289 auto K_t = K_Batch * KPerBlock;
290 return (K + K_t - 1) / K_t * (KPerBlock / AK1Value);
295 auto K_t = K_Batch * KPerBlock;
296 return (K + K_t - 1) / K_t * (KPerBlock / BK1Value);
301 auto K_t = K_Batch * KPerBlock;
302 return (K + K_t - 1) / K_t * KPerBlock;
308 auto K_t = K_Batch * KReadVec;
309 return (K + K_t - 1) / K_t * KReadVec;
322 template <
index_t MNXdlPerWave,
327 typename TileDesc_K0_MN_K1>
370 IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
372 const auto a_grid_desc_mraw_kraw = [&]() {
373 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
377 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
385 if constexpr(GemmSpec == GemmSpecialization::MKPadding ||
386 GemmSpec == GemmSpecialization::MNKPadding)
389 const auto a_grid_desc_m_k =
403 return a_grid_desc_ak0_m_ak1;
405 else if constexpr(GemmSpec == GemmSpecialization::MPadding ||
406 GemmSpec == GemmSpecialization::MNPadding)
410 a_grid_desc_mraw_kraw,
416 return a_grid_desc_ak0_m_ak1;
418 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
419 GemmSpec == GemmSpecialization::NKPadding)
423 a_grid_desc_mraw_kraw,
435 return a_grid_desc_ak0_m_ak1;
441 a_grid_desc_mraw_kraw,
448 a_grid_desc_ak0_m_ak1,
456 a_grid_desc_permuted,
478 const auto b_grid_desc_nraw_kraw = [&]() {
492 GemmSpec != GemmSpecialization::Default),
493 "pk_i4_t does not support padding");
495 GemmSpec != GemmSpecialization::Default),
496 "f4x2_pk_t does not support padding");
498 if constexpr(GemmSpec == GemmSpecialization::NKPadding ||
499 GemmSpec == GemmSpecialization::MNKPadding)
502 const auto b_grid_desc_n_k =
516 return b_grid_desc_bk0_n_bk1;
518 else if constexpr(GemmSpec == GemmSpecialization::NPadding ||
519 GemmSpec == GemmSpecialization::MNPadding)
523 b_grid_desc_nraw_kraw,
529 return b_grid_desc_bk0_n_bk1;
531 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
532 GemmSpec == GemmSpecialization::MKPadding)
536 b_grid_desc_nraw_kraw,
548 return b_grid_desc_bk0_n_bk1;
554 b_grid_desc_nraw_kraw,
561 b_grid_desc_bk0_n_bk1,
569 b_grid_desc_permuted,
581 template <
typename ABlockDesc_AK0_M_AK1>
582 __host__ __device__
static constexpr
auto
585 constexpr
index_t MWaves = MPerBlock / (MXdlPerWave * MPerXdl);
587 return MakeGemmMmaTileDescriptor<MXdlPerWave, MWaves, MXdlPack, MPerXdl, true>(
588 ABlockDesc_AK0_M_AK1{});
591 template <
typename BBlockDesc_BK0_N_BK1>
592 __host__ __device__
static constexpr
auto
595 constexpr
index_t NWaves = NPerBlock / (NXdlPerWave * NPerXdl);
597 return MakeGemmMmaTileDescriptor<NXdlPerWave, NWaves, NXdlPack, NPerXdl, false>(
598 BBlockDesc_BK0_N_BK1{});
601 template <
typename ELayout>
603 IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
605 const auto c_grid_desc_mraw_nraw = [&]() {
624 template <
typename DLayout>
625 __host__ __device__
static auto
628 const auto c_grid_desc_mraw_nraw = [&]() {
653 return MakeDGridDescriptor_M_N<DLayout>(M, MPad, N, NPad, StrideDs[i]);
658 template <
typename DsGr
idDesc>
660 const DsGridDesc& ds_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
665 ds_grid_desc_m_n[i], MBlock, NBlock);
681 std::array<index_t, NumDTensor> StrideDs_,
711 std::cout <<
"problem {" <<
"NumTokens:" <<
NumTokens <<
", " <<
"TopK:" <<
TopK <<
", "
712 <<
"M:" <<
M <<
", " <<
"N:" <<
N <<
", " <<
"K:" <<
K <<
", "
716 <<
", " <<
"KRead:" <<
KRead <<
", " <<
"KP:" <<
KPadded <<
", "
717 <<
"AK0:" <<
AK0 <<
", " <<
"BK0:" <<
BK0 <<
", " <<
"MBlock: " <<
MBlock
718 <<
", " <<
"NBlock: " <<
NBlock <<
"}" << std::endl;
750 const index_t* p_sorted_expert_ids_,
751 const index_t* p_max_token_id_,
752 const ADataType* p_a_grid_,
753 const AScaleDataType* p_a_scale_grid_,
754 const BDataType* p_b_grid_,
755 const BScaleDataType* p_b_scale_grid_,
756 std::array<const void*, NumDTensor> p_ds_grid_,
757 CDataType* p_c_grid_,
767 std::array<index_t, NumDTensor> StrideDs_,
770 AElementwiseOperation a_element_op_,
771 BElementwiseOperation b_element_op_,
772 CElementwiseOperation c_element_op_)
804 p_ds_grid(i) =
static_cast<const DDataType_*
>(p_ds_grid_[i]);
827 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
831 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
836 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
840 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
854 if(k_id < karg.
KBatch - 1)
884 constexpr
auto a_lds_block_desc =
896 return a_lds_block_desc_permuted;
903 constexpr
auto WaveSize = 64;
904 constexpr
auto M0 = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
905 constexpr
auto M1 = MPerBlock / M0;
907 constexpr
auto KThreadWrite = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
908 constexpr
auto K0PerThreadWrite =
AK0Number / KThreadWrite;
909 constexpr
auto KThreadRead = WaveSize / MPerXdl;
910 constexpr
auto K0PerThreadRead =
AK0Number / KThreadRead;
912 constexpr
auto kfold = (
AK1Number * M0 *
sizeof(ADataType) > 128)
914 : 128 / (
AK1Number * M0 *
sizeof(ADataType));
915 constexpr
auto KThreadReadPerm =
916 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
917 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
921 constexpr
auto mpair = (
AK1Number * MPerXdl *
sizeof(ADataType) > 128)
923 : ((128 / (
AK1Number * MPerXdl *
sizeof(ADataType))) > M0
925 : 128 / (
AK1Number * MPerXdl *
sizeof(ADataType)));
931 Number<kfold * M0 / mpair>{},
950 a_lds_block_desc_permuted,
972 a_lds_block_desc_unmerged,
975 Number<KThreadWrite / kfold / KThreadReadPerm>{},
984 return a_lds_block_desc_ak0_m_ak1;
1000 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1002 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1009 return c_shuffle_block_desc_mblock_mperblock_nblock_nperblock;
1030 ABlockTransferSrcScalarPerVector,
1031 BBlockTransferSrcScalarPerVector,
1050 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1053 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1056 constexpr
auto c_block_size =
1057 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize();
1059 return math::max(a_block_space_size_aligned *
sizeof(ADataType),
1060 c_block_size *
sizeof(CShuffleDataType));
1066 static_assert((MPerBlock % (MPerXdl * MXdlPerWave) == 0) &&
1067 (NPerBlock % (NXdlPerWave * NPerXdl)) == 0,
1068 "Invalid tuning param!");
1070 static_assert(KPerBlock % (ScaleBlockSize /
BPackedSize) == 0,
1071 "KPerBlock should be multiple of ScaleBlockSize");
1079 if(!(karg.
M % MPerBlock == 0))
1083 std::cout <<
"Arg M value is not a multiple of MPerBlock! M: " << karg.
M <<
" "
1084 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1097 if(!(karg.
N % NPerBlock == 0))
1101 std::cout <<
"Arg N value is not a multiple of NPerBlock! N: " << karg.
N <<
" "
1102 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1114 auto K_t = karg.
KBatch * KPerBlock;
1115 if(!(karg.
K % K_t == 0))
1119 std::cout <<
"Arg K value is not a multiple of K_Batch * K0PerBlock * K1! K: "
1120 << karg.
K <<
" " << __FILE__ <<
":" << __LINE__
1121 <<
", in function: " << __func__ << std::endl;
1129 auto K_t = karg.
KBatch * KReadVec;
1131 if((KReadPadSplited * (karg.
KBatch - 1)) >= karg.
K)
1139 if(karg.
K % ABlockTransferSrcScalarPerVector != 0)
1143 std::cout <<
"Arg K (" << karg.
K
1144 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1145 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1146 << __LINE__ <<
", in function: " << __func__ << std::endl;
1153 if(karg.
M % ABlockTransferSrcScalarPerVector != 0)
1157 std::cout <<
"Arg M (" << karg.
M
1158 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1159 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1160 << __LINE__ <<
", in function: " << __func__ << std::endl;
1168 if(karg.
N % BBlockTransferSrcScalarPerVector != 0)
1172 std::cout <<
"Arg N (" << karg.
N
1173 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1174 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1175 << __LINE__ <<
", in function: " << __func__ << std::endl;
1182 if(karg.
K % BBlockTransferSrcScalarPerVector != 0)
1186 std::cout <<
"Arg K (" << karg.
K
1187 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1188 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1189 << __LINE__ <<
", in function: " << __func__ << std::endl;
1201 std::cout <<
"Arg N (" << karg.
N
1202 <<
") value is not a multiple of "
1203 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1205 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1217 std::cout <<
"Arg M (" << karg.
M
1218 <<
") value is not a multiple of "
1219 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1221 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1231 const auto num_k_loop = karg.
AK0 / (KPerBlock / AK1Value);
1233 if(num_k_loop <= BlockwiseGemmPipe::PrefetchStages)
1244 const index_t num_loop = K / KPerBlock;
1246 return BlockwiseGemmPipe::BlockHasHotloop(num_loop);
1251 const index_t num_loop = K / KPerBlock;
1253 return BlockwiseGemmPipe::BlockLoopTailNum(num_loop);
1256 template <
typename CGr
idDesc>
1258 const CGridDesc& c_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
1267 return c_grid_desc_mblock_mperblock_nblock_nperblock;
1276 template <
bool HasMainKBlockLoop,
1279 __device__
static void Run(
const index_t* p_sorted_token_ids,
1280 const index_t* p_sorted_expert_ids,
1281 const index_t* p_max_token_id,
1282 const ADataType* p_a_grid,
1283 const AScaleDataType* p_a_scale_grid,
1284 const BDataType* p_b_grid,
1285 const BScaleDataType* p_b_scale_grid,
1287 CDataType* p_c_grid,
1289 const Problem& problem,
1290 AElementwiseOperation a_element_op,
1291 BElementwiseOperation b_element_op,
1292 CElementwiseOperation c_element_op)
1296 IsInputGemm ? problem.NumTokens : problem.NumTokens * problem.TopK,
1302 const auto b_grid_desc_bpreshuffled =
1304 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
1305 IsInputGemm ? problem.NumTokens * problem.TopK : problem.NumTokens,
1323 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
1325 c_grid_desc_m_n, problem.MBlock, problem.NBlock);
1326 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
1328 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.NBlock : blockIdx.y;
1329 if(expert_block_id * MPerBlock >= max_token_id)
1332 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
1334 const auto block_mn = [&]() -> std::pair<int, int> {
1335 if constexpr(NSwizzle)
1337 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
1338 const index_t prefix_block = ecnt_prefix * problem.NBlock;
1339 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
1340 const index_t expert_swizzle =
1341 ecnt > 0 ? ecnt : 1;
1342 const index_t bid_new = blockIdx.x - prefix_block;
1343 const index_t nid = __builtin_amdgcn_readfirstlane(
1344 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
1346 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
1351 return {blockIdx.x, blockIdx.y};
1355 const index_t block_n_id = block_mn.first;
1356 const index_t block_m_id = block_mn.second;
1358 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
1361 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
1362 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
1363 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
1364 constexpr
auto AKThreads = AK0Threads * AK1Threads;
1365 constexpr
auto AMRepeats = MPerBlock / AMThreads;
1366 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
1368 if(token_pos >= max_token_id || token0 >= problem.NumTokens)
1370 StaticallyIndexedArray<IndexType, AMRepeats> gather_offsets;
1371 static_for<0, AMRepeats, 1>{}([&](
auto m0) {
1372 const index_t fused_token = p_sorted_token_ids[token_pos + m0];
1373 index_t token_offset = fused_token & 0xffffff;
1374 if constexpr(!IsInputGemm)
1376 token_offset = token_offset * problem.TopK + (fused_token >> 24);
1378 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.K /
APackedSize;
1381 __builtin_amdgcn_readfirstlane(problem.N * problem.K * (IsInputGemm ? 2 : 1));
1382 const index_t expert_scale_stride =
1383 __builtin_amdgcn_readfirstlane(problem.N * (IsInputGemm ? 2 : 1) *
1387 const index_t n_block_data_idx_on_grid =
1388 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave);
1390 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1391 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
1392 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1393 p_b_grid + expert_id * expert_stride /
BPackedSize,
1394 b_grid_desc_bpreshuffled.GetElementSpaceSize());
1397 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1398 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
1399 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1400 p_b_scale_grid + expert_id * expert_scale_stride,
1401 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1410 auto a_blockwise_copy = ThreadGroupTensorSliceTransfer_v4r1_gather<
1412 AElementwiseOperation,
1415 Sequence<AK0Number, MPerBlock, AK1Number>,
1416 ABlockTransferThreadClusterLengths_AK0_M_AK1,
1417 ABlockTransferThreadClusterArrangeOrder,
1420 decltype(a_grid_desc_ak0_m_ak1),
1421 decltype(a_block_desc_ak0_m_ak1),
1422 ABlockTransferSrcAccessOrder,
1424 ABlockTransferSrcVectorDim,
1426 ABlockTransferSrcScalarPerVector,
1427 ABlockTransferDstScalarPerVector_AK1,
1430 AThreadTransferSrcResetCoordinateAfterRun,
1434 BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
1437 a_block_desc_ak0_m_ak1,
1444 auto b_block_buf = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
1445 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1447 auto b_blockwise_copy =
1448 ThreadwiseTensorSliceTransfer_v2<BDataType,
1450 decltype(b_grid_desc_bpreshuffled),
1451 decltype(b_block_desc_bk0_n_bk1),
1456 Number<BK1Value>{}>,
1457 Sequence<1, 2, 0, 3>,
1459 BBlockTransferSrcScalarPerVector,
1460 BThreadTransferSrcResetCoordinateAfterRun,
1462 b_grid_desc_bpreshuffled,
1470 auto a_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1472 a_block_desc_ak0_m_ak1.GetElementSpaceSize() /
APackedSize);
1478 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
1480 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
1481 decltype(c_thread_buf) c_thread_buf_up;
1485 c_thread_buf.num_of_v_,
1486 c_thread_buf.s_per_v,
1490 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
1491 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
1495 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
1496 const auto waveId_m = wave_idx[
I0];
1497 const auto waveId_n = wave_idx[
I1];
1499 static constexpr
auto mfma = BlockwiseGemmPipe::xdlops_gemm.mfma;
1501 auto thread_offset_shuffled =
1504 auto a_thread_offset_m = waveId_m;
1506 auto a_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1509 decltype(a_scale_grid_desc_am_ak),
1510 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
1516 true>(a_scale_grid_desc_am_ak,
1522 auto b_thread_offset_n = waveId_n;
1524 auto b_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1527 decltype(b_scale_grid_desc_bn_ak),
1528 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1534 true>(b_scale_grid_desc_bn_ak,
1539 if constexpr(IsInputGemm)
1541 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2 /
BPackedSize;
1542 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1543 p_b_grid_up + expert_id * expert_stride /
BPackedSize,
1544 b_grid_desc_bpreshuffled.GetElementSpaceSize());
1545 auto b_blockwise_copy_up = ThreadwiseTensorSliceTransfer_v2<
1548 decltype(b_grid_desc_bpreshuffled),
1549 decltype(b_block_desc_bk0_n_bk1),
1550 Sequence<Number<NXdlPerWave>{},
I1, Number<KRepeat>{}, Number<BK1Value>{}>,
1551 Sequence<1, 2, 0, 3>,
1553 BBlockTransferSrcScalarPerVector,
1554 BThreadTransferSrcResetCoordinateAfterRun,
1555 true>(b_grid_desc_bpreshuffled,
1560 const BScaleDataType* p_b_scale_grid_up = p_b_scale_grid + expert_scale_stride / 2;
1561 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1562 p_b_scale_grid_up + expert_id * expert_scale_stride,
1563 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1564 auto b_scale_thread_copy_up = ThreadwiseTensorSliceTransfer_v2<
1567 decltype(b_scale_grid_desc_bn_ak),
1568 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1575 b_scale_grid_desc_bn_ak,
1580 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1581 a_grid_desc_ak0_m_ak1,
1582 a_block_desc_ak0_m_ak1,
1586 a_block_slice_copy_step,
1587 b_grid_desc_bpreshuffled,
1588 b_block_desc_bk0_n_bk1,
1590 b_blockwise_copy_up,
1594 b_block_slice_copy_step,
1597 a_scale_grid_desc_am_ak,
1598 a_scale_thread_copy,
1600 b_scale_grid_desc_bn_ak,
1601 b_scale_thread_copy,
1602 b_scale_thread_copy_up,
1604 b_scale_grid_buf_up,
1605 num_k_block_main_loop);
1609 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1610 a_grid_desc_ak0_m_ak1,
1611 a_block_desc_ak0_m_ak1,
1615 a_block_slice_copy_step,
1616 b_grid_desc_bpreshuffled,
1617 b_block_desc_bk0_n_bk1,
1621 b_block_slice_copy_step,
1623 a_scale_grid_desc_am_ak,
1624 a_scale_thread_copy,
1626 b_scale_grid_desc_bn_ak,
1627 b_scale_thread_copy,
1629 num_k_block_main_loop);
1634 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
1635 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
1639 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
1640 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
1644 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
1645 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
1647 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
1648 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
1649 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
1650 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
1651 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
1652 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
1653 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
1654 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
1657 static_assert(M0 * M1 * M2 * M3 * M4 == MPerBlock);
1658 static_assert(M4 == 4);
1662 vector_type<float, 4> topk_weights;
1663 static_for<0, NXdlPerWave, 1>{}([&](
auto n0) {
1664 static_for<0, MXdlPerWave, 1>{}([&](
auto m0) {
1665 static_for<0, M2, 1>{}([&](
auto m2) {
1666 const index_t m_pos = block_m_id * MPerBlock + m0 * M1 * M2 * M3 * M4 +
1667 m1 * M2 * M3 * M4 + m2 * M3 * M4 + m3 * M4;
1668 if constexpr(MulRoutedWeight)
1670 topk_weights = *c_style_pointer_cast<const vector_type<float, M4>*>(
1671 p_ds_grid[
I2] + m_pos);
1673 static_for<0, M4, 1>{}([&](
auto m4) {
1675 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
1677 constexpr
auto cidx = Number<c_offset>{};
1679 if constexpr(IsInputGemm)
1683 float gate = c_thread_buf[cidx];
1684 float up = c_thread_buf_up[cidx];
1685 if constexpr(MulRoutedWeight)
1687 gate = gate * topk_weights.AsType<
float>()[m4];
1688 up = up * topk_weights.AsType<
float>()[m4];
1690 tensor_operation::element_wise::Silu{}(gate, gate);
1691 c_thread_buf_fp32(cidx) = gate * up;
1695 float gate = c_thread_buf[cidx];
1696 float up = c_thread_buf_up[cidx];
1697 if constexpr(MulRoutedWeight)
1699 gate = gate * topk_weights.AsType<
float>()[m4];
1700 up = up * topk_weights.AsType<
float>()[m4];
1702 tensor_operation::element_wise::Gelu{}(gate, gate);
1703 c_thread_buf_fp32(cidx) = gate * up;
1708 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
1709 if constexpr(MulRoutedWeight)
1711 c_thread_buf_fp32(cidx) =
1712 topk_weights.AsType<
float>()[m4] * c_thread_buf_fp32[cidx];
1720 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1723 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1724 static_cast<CShuffleDataType*
>(p_shared),
1725 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1728 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
1732 Number<CShuffleMXdlPerWavePerShuffle>{},
1739 Number<CShuffleNXdlPerWavePerShuffle>{},
1742 make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}),
1744 Sequence<>{}, Sequence<0, 2, 4, 5, 6>{}, Sequence<>{}, Sequence<1, 3, 7>{}));
1748 const auto c_thread_mtx_on_block =
1749 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
1751 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
1752 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
1754 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
1760 const auto m_thread_data_on_block_idx =
1761 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
1764 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
1770 const auto n_thread_data_on_block_idx =
1771 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
1775 auto c_thread_copy_vgpr_to_lds =
1776 ThreadwiseTensorSliceTransfer_v1r3<AccDataType,
1778 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1779 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1781 Sequence<CShuffleMXdlPerWavePerShuffle,
1782 CShuffleNXdlPerWavePerShuffle,
1789 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
1795 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1798 m_thread_data_on_block_idx[
I1],
1799 n_thread_data_on_block_idx[
I1],
1800 m_thread_data_on_block_idx[
I2],
1801 m_thread_data_on_block_idx[
I3],
1802 m_thread_data_on_block_idx[
I4],
1803 n_thread_data_on_block_idx[
I2]),
1806 using EDataType = CDataType;
1809 problem.M, problem.MPadded, problem.N, problem.NPadded, problem.StrideDs);
1811 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
1813 ds_grid_desc_m_n, problem.MBlock, problem.NBlock);
1817 return make_dynamic_buffer<AddressSpaceEnum::Global>(
1818 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
1820 Number<NumDTensor>{});
1824 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
1826 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
1827 Number<NumDTensor>{}));
1831 tie(c_shuffle_block_buf),
1833 {
return ds_grid_buf[i]; },
1834 Number<NumDTensor>{}));
1837 const auto idx_c_ds_block_begin =
1845 Number<NumDTensor>{}));
1847 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
1848 c_grid_desc_mblock_mperblock_nblock_nperblock;
1850 using CDEBlockTransferCluster =
1851 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
1852 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
1853 constexpr
index_t scatter_weight_idx = 1;
1854 auto cde_block_copy_lds_and_global = ThreadGroupTensorSliceTransfer_v7r3_scatter<
1858 decltype(c_ds_desc_refs),
1859 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
1860 CElementwiseOperation,
1861 Sequence<static_cast<index_t>(EGlobalMemoryDataOperation)>,
1864 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1866 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>,
1867 CDEBlockTransferCluster,
1868 Sequence<0, 1, 2, 3>,
1869 Sequence<0, 1, 2, 3>,
1870 Sequence<0, 1, 2, 3>,
1873 CDEShuffleBlockTransferScalarPerVectors,
1885 idx_c_ds_block_begin,
1886 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1890 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1891 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1892 constexpr
auto sfc_c_vgpr =
1893 SpaceFillingCurve<Sequence<MXdlPerWave, NXdlPerWave, 1, 1, M2, 1, M4, 1>,
1894 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
1895 Sequence<CShuffleMXdlPerWavePerShuffle,
1896 CShuffleNXdlPerWavePerShuffle,
1904 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
1907 constexpr
auto sfc_cde_block =
1908 SpaceFillingCurve<Sequence<1, MPerBlock, 1, NPerBlock>,
1909 Sequence<0, 2, 1, 3>,
1911 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1913 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>>{};
1915 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
1916 constexpr
auto EMThreads =
1917 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
1918 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
1919 constexpr
auto ENThreads =
1920 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
1921 static_for<0, num_access, 1>{}([&](
auto access_id) {
1923 StaticallyIndexedArray<IndexType, EMRepeats> scatter_offsets;
1925 auto dstidx = sfc_cde_block.GetIndex(access_id);
1927 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
1928 static_for<0, EMRepeats, 1>{}([&](
auto m0) {
1929 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
1930 IndexType token_offset = fused_token & 0xffffff;
1931 if constexpr(IsInputGemm)
1933 token_offset = token_offset * problem.TopK + (fused_token >> 24);
1935 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.N;
1941 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1942 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
1944 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1945 c_shuffle_block_buf);
1951 cde_block_copy_lds_and_global.Run(
1954 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1958 if constexpr(access_id < num_access - 1)
1960 constexpr
auto cde_lds_and_global_step =
1961 sfc_cde_block.GetForwardStep(access_id);
1964 static_for<0, NumDTensor, 1>{}([&](
auto i) {
1965 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
1966 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
1970 cde_block_copy_lds_and_global.MoveDstSliceWindow(
1971 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1973 cde_lds_and_global_step);
1980 template <
bool HasMainKBlockLoop,
1984 const index_t* p_sorted_expert_ids,
1985 const index_t* p_max_token_id,
1986 const ADataType* p_a_grid,
1987 const AScaleDataType* p_a_scale_grid,
1988 const BDataType* p_b_grid,
1989 const BScaleDataType* p_b_scale_grid,
1991 CDataType* p_c_grid,
1995 AElementwiseOperation a_element_op,
1996 BElementwiseOperation b_element_op,
1997 CElementwiseOperation c_element_op)
2008 const auto b_grid_desc_bpreshuffled =
2010 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
2018 const auto Padded_Scale_M =
2042 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
2046 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
2047 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
2048 if(expert_block_id * MPerBlock >= max_token_id)
2051 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
2052 const auto block_mn = [&]() -> std::pair<int, int> {
2053 if constexpr(NSwizzle)
2055 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
2057 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
2058 const index_t expert_swizzle =
2059 ecnt > 0 ? ecnt : 1;
2060 const index_t bid_new = blockIdx.x - prefix_block;
2061 const index_t nid = __builtin_amdgcn_readfirstlane(
2062 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
2064 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
2069 return {blockIdx.x, blockIdx.y};
2073 const index_t block_n_id = block_mn.first;
2074 const index_t block_m_id = block_mn.second;
2076 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
2079 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
2080 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
2081 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
2082 constexpr
auto AKThreads = AK0Threads * AK1Threads;
2083 constexpr
auto AMRepeats = MPerBlock / AMThreads;
2084 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads;
2086 if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
2090 const index_t fused_token = p_sorted_token_ids[token_pos + m0 * AMThreads];
2091 index_t token_offset = fused_token & 0xffffff;
2092 if constexpr(!IsInputGemm)
2094 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2096 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
K;
2100 __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
2101 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
2102 problem.
N * (IsInputGemm ? 2 : 1) *
2106 const index_t n_block_data_idx_on_grid =
2107 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave /
NXdlPack);
2110 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2111 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
2112 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2113 p_b_grid + expert_id * expert_stride, b_grid_desc_bpreshuffled.GetElementSpaceSize());
2116 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2117 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
2118 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2119 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
2120 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2132 ABlockTransferThreadClusterLengths_AK0_M_AK1,
2133 ABlockTransferThreadClusterArrangeOrder,
2136 decltype(a_grid_desc_ak0_m_ak1),
2137 decltype(a_block_desc_ak0_m_ak1),
2138 ABlockTransferSrcAccessOrder,
2139 ABlockTransferSrcVectorDim,
2141 ABlockTransferSrcScalarPerVector,
2143 1>(a_grid_desc_ak0_m_ak1,
2145 a_block_desc_ak0_m_ak1,
2151 auto b_block_buf_ping = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2152 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2153 auto b_block_buf_pong = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2154 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2155 auto b_block_bufs =
make_tuple(b_block_buf_ping, b_block_buf_pong);
2157 auto b_blockwise_copy =
2160 decltype(b_grid_desc_bpreshuffled),
2161 decltype(b_block_desc_bk0_n_bk1),
2169 BBlockTransferSrcScalarPerVector,
2170 BThreadTransferSrcResetCoordinateAfterRun,
2172 b_grid_desc_bpreshuffled,
2181 auto a_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2182 static_cast<ADataType*
>(p_shared_0), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2183 auto a_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2184 static_cast<ADataType*
>(p_shared_1), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2185 auto a_block_bufs =
make_tuple(a_block_buf_ping, a_block_buf_pong);
2191 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
2193 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
2194 decltype(c_thread_buf) c_thread_buf_up;
2198 c_thread_buf.num_of_v_,
2199 c_thread_buf.s_per_v,
2203 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
2204 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
2208 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
2209 const auto waveId_m = wave_idx[
I0];
2210 const auto waveId_n = wave_idx[
I1];
2212 auto thread_offset_shuffled =
2215 auto a_thread_offset_m = waveId_m;
2218 const index_t token_scale_pos = block_m_id * MPerBlock;
2219 if(token_scale_pos >= max_token_id || token0 >= problem.
NumTokens)
2225 decltype(a_scale_grid_desc_am_ak),
2226 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
2232 true>(a_scale_grid_desc_am_ak,
2238 auto b_thread_offset_n = waveId_n;
2243 decltype(b_scale_grid_desc_bn_ak),
2244 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2250 true>(b_scale_grid_desc_bn_ak,
2255 if constexpr(IsInputGemm)
2257 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
2258 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2259 p_b_grid_up + expert_id * expert_stride,
2260 b_grid_desc_bpreshuffled.GetElementSpaceSize());
2261 auto b_blockwise_copy_up =
2264 decltype(b_grid_desc_bpreshuffled),
2265 decltype(b_block_desc_bk0_n_bk1),
2273 BBlockTransferSrcScalarPerVector,
2274 BThreadTransferSrcResetCoordinateAfterRun,
2276 b_grid_desc_bpreshuffled,
2282 const BScaleDataType* p_b_scale_grid_up =
2283 p_b_scale_grid + expert_scale_stride / 2 /
sizeof(BScaleDataType);
2284 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2285 p_b_scale_grid_up + expert_id * expert_scale_stride /
sizeof(BScaleDataType),
2286 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2291 decltype(b_scale_grid_desc_bn_ak),
2292 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2299 b_scale_grid_desc_bn_ak,
2304 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2306 a_grid_desc_ak0_m_ak1,
2307 a_block_desc_ak0_m_ak1,
2311 a_block_slice_copy_step,
2313 b_grid_desc_bpreshuffled,
2314 b_block_desc_bk0_n_bk1,
2316 b_blockwise_copy_up,
2320 b_block_slice_copy_step,
2325 a_scale_grid_desc_am_ak,
2326 a_scale_thread_copy,
2329 b_scale_grid_desc_bn_ak,
2330 b_scale_thread_copy,
2331 b_scale_thread_copy_up,
2333 b_scale_grid_buf_up,
2334 num_k_block_main_loop);
2338 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2339 a_grid_desc_ak0_m_ak1,
2340 a_block_desc_ak0_m_ak1,
2344 a_block_slice_copy_step,
2345 b_grid_desc_bpreshuffled,
2346 b_block_desc_bk0_n_bk1,
2350 b_block_slice_copy_step,
2352 a_scale_grid_desc_am_ak,
2353 a_scale_thread_copy,
2355 b_scale_grid_desc_bn_ak,
2356 b_scale_thread_copy,
2358 num_k_block_main_loop);
2363 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
2364 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
2366 static_assert(CShuffleMXdlPerWavePerShuffle %
MXdlPack == 0 &&
2367 CShuffleNXdlPerWavePerShuffle %
NXdlPack == 0,
2370 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
2373 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
2374 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2378 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
2379 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2381 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
2382 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
2383 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
2384 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
2385 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
2386 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
2387 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
2388 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
2389 constexpr
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
2390 constexpr
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
2394 static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
2395 static_assert(M5 == 4);
2405 const index_t m_pos = block_m_id * MPerBlock +
2406 m0 * M2 * M1 * M3 * M4 * M5 +
2407 m1 * M2 * M3 * M4 * M5 +
2408 imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
2409 if constexpr(MulRoutedWeight)
2412 *c_style_pointer_cast<const vector_type<float, M5>*>(
2413 p_ds_grid[
I2] + m_pos);
2417 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
2418 make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
2421 if constexpr(IsInputGemm)
2423 if constexpr(ActivationOperation ==
2426 float gate = c_thread_buf[cidx];
2427 float up = c_thread_buf_up[cidx];
2428 if constexpr(MulRoutedWeight)
2430 gate = gate * topk_weights.AsType<
float>()[m5];
2431 up = up * topk_weights.AsType<
float>()[m5];
2434 c_thread_buf_fp32(cidx) = gate * up;
2438 float gate = c_thread_buf[cidx];
2439 float up = c_thread_buf_up[cidx];
2440 if constexpr(MulRoutedWeight)
2442 gate = gate * topk_weights.AsType<
float>()[m5];
2443 up = up * topk_weights.AsType<
float>()[m5];
2446 c_thread_buf_fp32(cidx) = gate * up;
2451 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
2452 if constexpr(MulRoutedWeight)
2454 c_thread_buf_fp32(cidx) =
2455 topk_weights.AsType<
float>()[m5] *
2456 c_thread_buf_fp32[cidx];
2466 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
2469 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2470 static_cast<CShuffleDataType*
>(p_shared_0),
2471 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2474 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
2500 const auto c_thread_mtx_on_block =
2501 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
2503 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
2504 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
2506 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
2512 const auto m_thread_data_on_block_idx =
2513 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
2516 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
2522 const auto n_thread_data_on_block_idx =
2523 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
2530 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2531 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2534 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2543 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2548 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2551 m_thread_data_on_block_idx[
I1],
2552 n_thread_data_on_block_idx[
I1],
2553 m_thread_data_on_block_idx[
I2],
2554 n_thread_data_on_block_idx[
I2],
2555 m_thread_data_on_block_idx[
I3],
2556 m_thread_data_on_block_idx[
I4],
2557 m_thread_data_on_block_idx[
I5],
2558 n_thread_data_on_block_idx[
I3]),
2561 using EDataType = CDataType;
2566 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
2572 return make_dynamic_buffer<AddressSpaceEnum::Global>(
2573 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
2579 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
2581 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
2586 tie(c_shuffle_block_buf),
2588 {
return ds_grid_buf[i]; },
2592 const auto idx_c_ds_block_begin =
2602 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
2603 c_grid_desc_mblock_mperblock_nblock_nperblock;
2605 using CDEBlockTransferCluster =
2606 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
2607 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
2608 constexpr
index_t scatter_weight_idx = 3;
2613 decltype(c_ds_desc_refs),
2614 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
2615 CElementwiseOperation,
2620 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2622 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>,
2623 CDEBlockTransferCluster,
2629 CDEShuffleBlockTransferScalarPerVectors,
2641 idx_c_ds_block_begin,
2642 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2646 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2647 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2649 constexpr
auto sfc_c_vgpr =
2660 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2662 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2672 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2675 constexpr
auto sfc_cde_block =
2679 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2681 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>>{};
2683 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2684 constexpr
auto EMThreads =
2685 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2686 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2687 constexpr
auto ENThreads =
2688 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2693 auto dstidx = sfc_cde_block.GetIndex(access_id);
2695 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2697 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2698 IndexType token_offset = fused_token & 0xffffff;
2699 if constexpr(IsInputGemm)
2701 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2703 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
N;
2709 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2710 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2712 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2713 c_shuffle_block_buf);
2719 cde_block_copy_lds_and_global.Run(
2722 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2726 if constexpr(access_id < num_access - 1)
2728 constexpr
auto cde_lds_and_global_step =
2729 sfc_cde_block.GetForwardStep(access_id);
2733 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2734 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2738 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2739 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2741 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
constexpr auto BlockGemmMXBPreshufflePipeline_Selector()
Definition: blockwise_gemm_pipeline_xdlops_b_preshuffle_mx_moe_selector.hpp:36
__host__ constexpr __device__ auto make_merge_transform(const LowLengths &low_lengths)
Definition: multi_index_transform_helper.hpp:55
BlockGemmPipelineVersion
Definition: blkgemmpipe_scheduler.hpp:12
__host__ constexpr __device__ auto make_merge_transform_v3_division_mod(const LowLengths &low_lengths)
Definition: multi_index_transform_helper.hpp:84
__global__ void kernel_moe_mxgemm_2lds(typename GridwiseGemm::Argument karg)
Definition: gridwise_moe_mx_gemm.hpp:87
TailNumber
Definition: blkgemmpipe_scheduler.hpp:31
__host__ constexpr __device__ auto make_single_stage_tensor_adaptor(const Transforms &transforms, LowerDimensionOldTopIdss, UpperDimensionNewTopIdss)
Definition: tensor_adaptor.hpp: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
integral_constant< index_t, N > Number
Definition: number.hpp:12
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:748
const index_t * p_sorted_expert_ids
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:809
const index_t * p_sorted_token_ids
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:808
const BScaleDataType * p_b_scale_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:814
const BElementwiseOperation b_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:819
const BDataType * p_b_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:813
const index_t * p_max_token_id
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:810
DsGridPointer p_ds_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:815
const AElementwiseOperation a_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:818
CDataType * p_c_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:816
const AScaleDataType * p_a_scale_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:812
__host__ Argument(const index_t *p_sorted_token_ids_, const index_t *p_sorted_expert_ids_, const index_t *p_max_token_id_, const ADataType *p_a_grid_, const AScaleDataType *p_a_scale_grid_, const BDataType *p_b_grid_, const BScaleDataType *p_b_scale_grid_, std::array< const void *, NumDTensor > p_ds_grid_, CDataType *p_c_grid_, index_t NumTokens_, index_t TopK_, index_t M_, index_t N_, index_t K_, index_t StrideA_, index_t StrideScaleA_, index_t StrideB_, index_t StrideScaleB_, std::array< index_t, NumDTensor > StrideDs_, index_t StrideC_, index_t k_batch_, AElementwiseOperation a_element_op_, BElementwiseOperation b_element_op_, CElementwiseOperation c_element_op_)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:749
const CElementwiseOperation c_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:820
const ADataType * p_a_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:811
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:671
index_t BN0Shuffled
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:742
index_t AK0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:737
index_t NPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:734
index_t BK0Shuffled
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:743
index_t KBatch
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:732
index_t BK0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:738
index_t TopK
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:722
__host__ Problem(index_t NumTokens_, index_t TopK_, index_t M_, index_t N_, index_t K_, index_t StrideA_, index_t StrideScaleA_, index_t StrideB_, index_t StrideScaleB_, std::array< index_t, NumDTensor > StrideDs_, index_t StrideC_, index_t KBatch_)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:672
index_t KRead
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:735
index_t K
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:725
index_t MPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:733
index_t StrideC
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:731
index_t StrideScaleB
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:729
index_t NumTokens
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:721
std::array< index_t, NumDTensor > StrideDs
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:730
index_t MBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:739
index_t StrideScaleA
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:727
index_t N
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:724
__host__ void Print() const
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:709
index_t StrideA
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:726
index_t StrideB
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:728
index_t M
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:723
index_t KPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:736
index_t NBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:740
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:824
index_t a_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:866
index_t b_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:867
index_t a_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:864
__device__ SplitKBatchOffset(Argument &karg, index_t k_id)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:825
index_t b_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:865
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:171
static constexpr auto I6
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:181
static __host__ auto CalculateBN0Shuffled(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:273
static __host__ auto CalculateKPadded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:299
static constexpr auto AK1Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:191
static constexpr auto AK0Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:189
static constexpr __device__ index_t GetSharedMemoryNumberOfByte()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1042
decltype(MakeDsGridPointer()) DsGridPointer
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:249
static constexpr auto I1
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:176
static __host__ auto CalculateKPadded(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:282
static __host__ auto CalculateGridSize(index_t M, index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:253
__host__ static __device__ auto MakeDsGridDescriptor_M_N(index_t M, index_t MPad, index_t N, index_t NPad, std::array< index_t, NumDTensor > StrideDs)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:647
static __device__ void Run_2Lds(const index_t *p_sorted_token_ids, const index_t *p_sorted_expert_ids, const index_t *p_max_token_id, const ADataType *p_a_grid, const AScaleDataType *p_a_scale_grid, const BDataType *p_b_grid, const BScaleDataType *p_b_scale_grid, DsGridPointer &p_ds_grid, CDataType *p_c_grid, void *p_shared_0, void *p_shared_1, const Problem &problem, AElementwiseOperation a_element_op, BElementwiseOperation b_element_op, CElementwiseOperation c_element_op)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1983
__host__ static constexpr __device__ auto MakeCGridDescriptor_MBlock_MPerBlock_NBlock_NPerBlock(const CGridDesc &c_grid_desc_m_n, index_t MBlock, index_t NBlock)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1257
static constexpr __device__ auto MakeDsGridDescriptor_MBlock_MPerBlock_NBlock_NPerBlock(const DsGridDesc &ds_grid_desc_m_n, index_t MBlock, index_t NBlock)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:659
static constexpr index_t NLane
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:222
static constexpr __device__ auto GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:988
static constexpr index_t SortedTileSize
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:228
static constexpr index_t scale_pack_size_b
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:232
static constexpr auto BK1Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:192
remove_cvref_t< decltype(BlockGemmMXBPreshufflePipeline_Selector< BlkGemmPipelineVer, BlkGemmPipeSched, BlockSize, ScaleBlockSize, ADataType, AScaleDataType, BDataType, BScaleDataType, ComputeTypeA, AccDataType, decltype(GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()), decltype(GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1()), decltype(MakeAMmaTileDescriptor_M0_M1_M2_M3_K(GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1())), decltype(MakeBMmaTileDescriptor_N0_N1_N2_N3_K(GetBBlockDescriptor_BK0PerBlock_NPerBlock_BK1())), ABlockTransferSrcScalarPerVector, BBlockTransferSrcScalarPerVector, MPerBlock, NPerBlock, KPerBlock, MPerXdl, NPerXdl, MXdlPerWave, NXdlPerWave, KPack, IsInputGemm >())> BlockwiseGemmPipe
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1040
static constexpr __host__ TailNumber CalculateKBlockLoopTailNum(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1249
static __host__ auto CalculateNPadded(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:268
static constexpr auto MakeDsGridPointer()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:238
__host__ static __device__ auto MakeAGridDescriptor_AK0_M_AK1(IndexType M, IndexType MPad, IndexType K, IndexType KPad, IndexType StrideA, IndexType AK0)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:369
static constexpr auto BK0Number
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:190
static constexpr auto I9
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:184
static constexpr auto I4
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:179
static constexpr auto I3
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:178
static __host__ auto CalculateKRead(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:305
static __host__ auto CalculateAK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:287
static __host__ auto CalculateMPadded(index_t M)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:263
static constexpr auto CShuffleBlockTransferScalarPerVector_NPerBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:186
static __host__ auto CalculateMBlock(index_t M)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:312
static constexpr index_t KRepeat
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:225
static constexpr auto NXdlPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:201
static constexpr __device__ auto GetCShuffleBlockDescriptor_MBlock_MPerBlock_NBlock_NPerBlock()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:998
static constexpr auto KXdlPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:202
BDataType LDSTypeB
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:173
__host__ static __device__ auto MakeBGridDescriptor_BK0_N_BK1(index_t K, index_t KPad, index_t N, index_t NPad, index_t StrideB, index_t BK0)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:475
static constexpr auto I0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:175
static constexpr auto I5
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:180
static constexpr index_t scale_pack_size_a
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:231
static constexpr auto lcm_AK1_BK1
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:194
static constexpr index_t KLane
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:223
static constexpr __device__ auto GetABlockDescriptor_AK0PerBlock_MPerBlock_AK1()
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:870
static constexpr auto MXdlPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:200
static constexpr auto I8
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:183
__host__ static constexpr __device__ auto MakeBMmaTileDescriptor_N0_N1_N2_N3_K(const BBlockDesc_BK0_N_BK1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:593
__host__ static __device__ auto MakeCGridDescriptor_M_N(IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:602
static constexpr bool is_single_rate_mfma
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:195
static constexpr auto I2
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:177
__host__ static constexpr __device__ auto MakeAMmaTileDescriptor_M0_M1_M2_M3_K(const ABlockDesc_AK0_M_AK1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:583
static constexpr index_t APackedSize
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:210
static constexpr index_t NumDTensor
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:198
static __host__ auto CalculateNBlock(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:317
static constexpr index_t BPackedSize
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:211
static constexpr index_t KPack
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:219
static constexpr __host__ bool CalculateHasMainKBlockLoop(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1242
static constexpr __host__ bool CheckValidity(const Argument &karg)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1064
ADataType LDSTypeA
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:172
static __host__ auto CalculateBK0Padded(index_t K, index_t K_Batch=1)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:293
__host__ static __device__ auto MakeDGridDescriptor_M_N(index_t M, index_t MPad, index_t N, index_t NPad, index_t StrideC)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:626
__host__ static constexpr __device__ auto MakeGemmMmaTileDescriptor(const TileDesc_K0_MN_K1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:328
static __host__ auto CalculateBK0Shuffled(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:277
ThisThreadBlock< BlockSize > ThisThreadBlock
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:251
static constexpr auto I7
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:182
__host__ static __device__ auto MakeBGridDescriptor_Preshuffled(index_t N0, index_t K0)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:468
static constexpr index_t NWave
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:224
static constexpr auto is_scale_mfma
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:196
Definition: xdlops_gemm.hpp:942
static constexpr auto selected_mfma
Definition: xdlops_gemm.hpp:1343
Definition: sequence.hpp:43
Definition: tensor_space_filling_curve.hpp:20
Definition: static_buffer.hpp:75
Definition: thread_group_tensor_slice_transfer_gather_direct_load.hpp:57
Definition: thread_group_tensor_slice_transfer_v7r3_scatter.hpp:51
Definition: threadwise_tensor_slice_transfer.hpp:39
Helper structure that facilitates transfer of source (grid) data to destination threads.
Definition: threadwise_tensor_slice_transfer.hpp:234
Definition: tuple.hpp:117
Unsigned representation of a conventional biased Float32 exponent.
Definition: e8m0.hpp:25
Definition: data_type.hpp:41
Definition: integral_constant.hpp:20
Definition: data_type.hpp: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