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,
470 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
471 constexpr
index_t WaveSize = BlockSize / (MWave *
NWave);
480 const auto b_grid_desc_nraw_kraw = [&]() {
494 GemmSpec != GemmSpecialization::Default),
495 "pk_i4_t does not support padding");
497 GemmSpec != GemmSpecialization::Default),
498 "f4x2_pk_t does not support padding");
500 if constexpr(GemmSpec == GemmSpecialization::NKPadding ||
501 GemmSpec == GemmSpecialization::MNKPadding)
504 const auto b_grid_desc_n_k =
518 return b_grid_desc_bk0_n_bk1;
520 else if constexpr(GemmSpec == GemmSpecialization::NPadding ||
521 GemmSpec == GemmSpecialization::MNPadding)
525 b_grid_desc_nraw_kraw,
531 return b_grid_desc_bk0_n_bk1;
533 else if constexpr(GemmSpec == GemmSpecialization::KPadding ||
534 GemmSpec == GemmSpecialization::MKPadding)
538 b_grid_desc_nraw_kraw,
550 return b_grid_desc_bk0_n_bk1;
556 b_grid_desc_nraw_kraw,
563 b_grid_desc_bk0_n_bk1,
571 b_grid_desc_permuted,
583 template <
typename ABlockDesc_AK0_M_AK1>
584 __host__ __device__
static constexpr
auto
587 constexpr
index_t MWaves = MPerBlock / (MXdlPerWave * MPerXdl);
589 return MakeGemmMmaTileDescriptor<MXdlPerWave, MWaves, MXdlPack, MPerXdl, true>(
590 ABlockDesc_AK0_M_AK1{});
593 template <
typename BBlockDesc_BK0_N_BK1>
594 __host__ __device__
static constexpr
auto
597 constexpr
index_t NWaves = NPerBlock / (NXdlPerWave * NPerXdl);
599 return MakeGemmMmaTileDescriptor<NXdlPerWave, NWaves, NXdlPack, NPerXdl, false>(
600 BBlockDesc_BK0_N_BK1{});
603 template <
typename ELayout>
605 IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
607 const auto c_grid_desc_mraw_nraw = [&]() {
626 template <
typename DLayout>
627 __host__ __device__
static auto
630 const auto c_grid_desc_mraw_nraw = [&]() {
655 return MakeDGridDescriptor_M_N<DLayout>(M, MPad, N, NPad, StrideDs[i]);
660 template <
typename DsGr
idDesc>
662 const DsGridDesc& ds_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
667 ds_grid_desc_m_n[i], MBlock, NBlock);
683 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;
747 const index_t* p_sorted_expert_ids_,
748 const index_t* p_max_token_id_,
749 const ADataType* p_a_grid_,
750 const AScaleDataType* p_a_scale_grid_,
751 const BDataType* p_b_grid_,
752 const BScaleDataType* p_b_scale_grid_,
753 std::array<const void*, NumDTensor> p_ds_grid_,
754 CDataType* p_c_grid_,
764 std::array<index_t, NumDTensor> StrideDs_,
767 AElementwiseOperation a_element_op_,
768 BElementwiseOperation b_element_op_,
769 CElementwiseOperation c_element_op_)
801 p_ds_grid(i) =
static_cast<const DDataType_*
>(p_ds_grid_[i]);
824 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, ALayout>)
828 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, ALayout>)
833 if constexpr(is_same_v<tensor_layout::gemm::RowMajor, BLayout>)
837 else if constexpr(is_same_v<tensor_layout::gemm::ColumnMajor, BLayout>)
851 if(k_id < karg.
KBatch - 1)
869 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
870 constexpr
index_t WaveSize = BlockSize / (MWave *
NWave);
884 constexpr
auto a_lds_block_desc =
896 return a_lds_block_desc_permuted;
903 constexpr
auto M0 = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
904 constexpr
auto M1 = MPerBlock / M0;
906 constexpr
auto KThreadWrite = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
907 constexpr
auto K0PerThreadWrite =
AK0Number / KThreadWrite;
908 constexpr
auto KThreadRead = WaveSize / MPerXdl;
909 constexpr
auto K0PerThreadRead =
AK0Number / KThreadRead;
911 constexpr
auto kfold = (
AK1Number * M0 *
sizeof(ADataType) > 128)
913 : 128 / (
AK1Number * M0 *
sizeof(ADataType));
914 constexpr
auto KThreadReadPerm =
915 (kfold * K0PerThreadWrite / K0PerThreadRead) > 1
916 ? KThreadRead / (kfold * K0PerThreadWrite / K0PerThreadRead)
920 constexpr
auto mpair = (
AK1Number * MPerXdl *
sizeof(ADataType) > 128)
922 : ((128 / (
AK1Number * MPerXdl *
sizeof(ADataType))) > M0
924 : 128 / (
AK1Number * MPerXdl *
sizeof(ADataType)));
930 Number<kfold * M0 / mpair>{},
949 a_lds_block_desc_permuted,
971 a_lds_block_desc_unmerged,
974 Number<KThreadWrite / kfold / KThreadReadPerm>{},
983 return a_lds_block_desc_ak0_m_ak1;
999 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
1001 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1008 return c_shuffle_block_desc_mblock_mperblock_nblock_nperblock;
1029 ABlockTransferSrcScalarPerVector,
1030 BBlockTransferSrcScalarPerVector,
1049 a_block_desc_ak0_m_ak1.GetElementSpaceSize(), max_lds_align);
1052 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1055 constexpr
auto c_block_size =
1056 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize();
1058 return math::max(a_block_space_size_aligned *
sizeof(ADataType),
1059 c_block_size *
sizeof(CShuffleDataType));
1065 static_assert((MPerBlock % (MPerXdl * MXdlPerWave) == 0) &&
1066 (NPerBlock % (NXdlPerWave * NPerXdl)) == 0,
1067 "Invalid tuning param!");
1069 static_assert(KPerBlock % (ScaleBlockSize /
BPackedSize) == 0,
1070 "KPerBlock should be multiple of ScaleBlockSize");
1078 if(!(karg.
M % MPerBlock == 0))
1082 std::cout <<
"Arg M value is not a multiple of MPerBlock! M: " << karg.
M <<
" "
1083 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1096 if(!(karg.
N % NPerBlock == 0))
1100 std::cout <<
"Arg N value is not a multiple of NPerBlock! N: " << karg.
N <<
" "
1101 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1113 auto K_t = karg.
KBatch * KPerBlock;
1114 if(!(karg.
K % K_t == 0))
1118 std::cout <<
"Arg K value is not a multiple of K_Batch * K0PerBlock * K1! K: "
1119 << karg.
K <<
" " << __FILE__ <<
":" << __LINE__
1120 <<
", in function: " << __func__ << std::endl;
1128 auto K_t = karg.
KBatch * KReadVec;
1130 if((KReadPadSplited * (karg.
KBatch - 1)) >= karg.
K)
1138 if(karg.
K % ABlockTransferSrcScalarPerVector != 0)
1142 std::cout <<
"Arg K (" << karg.
K
1143 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1144 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1145 << __LINE__ <<
", in function: " << __func__ << std::endl;
1152 if(karg.
M % ABlockTransferSrcScalarPerVector != 0)
1156 std::cout <<
"Arg M (" << karg.
M
1157 <<
") value is not a multiple of ABlockTransferSrcScalarPerVector ("
1158 << ABlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1159 << __LINE__ <<
", in function: " << __func__ << std::endl;
1167 if(karg.
N % BBlockTransferSrcScalarPerVector != 0)
1171 std::cout <<
"Arg N (" << karg.
N
1172 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1173 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1174 << __LINE__ <<
", in function: " << __func__ << std::endl;
1181 if(karg.
K % BBlockTransferSrcScalarPerVector != 0)
1185 std::cout <<
"Arg K (" << karg.
K
1186 <<
") value is not a multiple of BBlockTransferSrcScalarPerVector ("
1187 << BBlockTransferSrcScalarPerVector <<
" )! " << __FILE__ <<
":"
1188 << __LINE__ <<
", in function: " << __func__ << std::endl;
1200 std::cout <<
"Arg N (" << karg.
N
1201 <<
") value is not a multiple of "
1202 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1204 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1216 std::cout <<
"Arg M (" << karg.
M
1217 <<
") value is not a multiple of "
1218 "CShuffleBlockTransferScalarPerVector_NPerBlock ("
1220 << __FILE__ <<
":" << __LINE__ <<
", in function: " << __func__
1230 const auto num_k_loop = karg.
AK0 / (KPerBlock / AK1Value);
1232 if(num_k_loop <= BlockwiseGemmPipe::PrefetchStages)
1243 const index_t num_loop = K / KPerBlock;
1245 return BlockwiseGemmPipe::BlockHasHotloop(num_loop);
1250 const index_t num_loop = K / KPerBlock;
1252 return BlockwiseGemmPipe::BlockLoopTailNum(num_loop);
1255 template <
typename CGr
idDesc>
1257 const CGridDesc& c_grid_desc_m_n,
index_t MBlock,
index_t NBlock)
1266 return c_grid_desc_mblock_mperblock_nblock_nperblock;
1275 template <
bool HasMainKBlockLoop,
1278 __device__
static void Run(
const index_t* p_sorted_token_ids,
1279 const index_t* p_sorted_expert_ids,
1280 const index_t* p_max_token_id,
1281 const ADataType* p_a_grid,
1282 const AScaleDataType* p_a_scale_grid,
1283 const BDataType* p_b_grid,
1284 const BScaleDataType* p_b_scale_grid,
1286 CDataType* p_c_grid,
1288 const Problem& problem,
1289 AElementwiseOperation a_element_op,
1290 BElementwiseOperation b_element_op,
1291 CElementwiseOperation c_element_op)
1297 IsInputGemm ? problem.NumTokens : problem.NumTokens * problem.TopK,
1303 const auto b_grid_desc_bpreshuffled =
1305 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
1306 IsInputGemm ? problem.NumTokens * problem.TopK : problem.NumTokens,
1324 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
1326 c_grid_desc_m_n, problem.MBlock, problem.NBlock);
1327 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
1329 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.NBlock : blockIdx.y;
1330 if(expert_block_id * MPerBlock >= max_token_id)
1333 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
1335 const auto block_mn = [&]() -> std::pair<int, int> {
1336 if constexpr(NSwizzle)
1338 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
1339 const index_t prefix_block = ecnt_prefix * problem.NBlock;
1340 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
1341 const index_t expert_swizzle =
1342 ecnt > 0 ? ecnt : 1;
1343 const index_t bid_new = blockIdx.x - prefix_block;
1344 const index_t nid = __builtin_amdgcn_readfirstlane(
1345 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
1347 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
1352 return {blockIdx.x, blockIdx.y};
1356 const index_t block_n_id = block_mn.first;
1357 const index_t block_m_id = block_mn.second;
1359 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
1362 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
1363 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
1364 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
1365 constexpr
auto AKThreads = AK0Threads * AK1Threads;
1366 constexpr
auto AMRepeats = MPerBlock / AMThreads;
1367 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads * AMRepeats;
1369 if(token_pos >= max_token_id || token0 >= problem.NumTokens)
1371 StaticallyIndexedArray<IndexType, AMRepeats> gather_offsets;
1372 static_for<0, AMRepeats, 1>{}([&](
auto m0) {
1373 const index_t fused_token = p_sorted_token_ids[token_pos + m0];
1374 index_t token_offset = fused_token & 0xffffff;
1375 if constexpr(!IsInputGemm)
1377 token_offset = token_offset * problem.TopK + (fused_token >> 24);
1379 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.K /
APackedSize;
1382 __builtin_amdgcn_readfirstlane(problem.N * problem.K * (IsInputGemm ? 2 : 1));
1383 const index_t expert_scale_stride =
1384 __builtin_amdgcn_readfirstlane(problem.N * (IsInputGemm ? 2 : 1) *
1388 const index_t n_block_data_idx_on_grid =
1389 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave);
1391 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1392 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
1393 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1394 p_b_grid + expert_id * expert_stride /
BPackedSize,
1395 b_grid_desc_bpreshuffled.GetElementSpaceSize());
1398 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1399 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
1400 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1401 p_b_scale_grid + expert_id * expert_scale_stride,
1402 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1411 auto a_blockwise_copy = ThreadGroupTensorSliceTransfer_v4r1_gather<
1413 AElementwiseOperation,
1416 Sequence<AK0Number, MPerBlock, AK1Number>,
1417 ABlockTransferThreadClusterLengths_AK0_M_AK1,
1418 ABlockTransferThreadClusterArrangeOrder,
1421 decltype(a_grid_desc_ak0_m_ak1),
1422 decltype(a_block_desc_ak0_m_ak1),
1423 ABlockTransferSrcAccessOrder,
1425 ABlockTransferSrcVectorDim,
1427 ABlockTransferSrcScalarPerVector,
1428 ABlockTransferDstScalarPerVector_AK1,
1431 AThreadTransferSrcResetCoordinateAfterRun,
1435 BlockwiseGemmPipe::GlobalBufferNum>(a_grid_desc_ak0_m_ak1,
1438 a_block_desc_ak0_m_ak1,
1445 auto b_block_buf = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
1446 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
1448 auto b_blockwise_copy =
1449 ThreadwiseTensorSliceTransfer_v2<BDataType,
1451 decltype(b_grid_desc_bpreshuffled),
1452 decltype(b_block_desc_bk0_n_bk1),
1457 Number<BK1Value>{}>,
1458 Sequence<1, 2, 0, 3>,
1460 BBlockTransferSrcScalarPerVector,
1461 BThreadTransferSrcResetCoordinateAfterRun,
1463 b_grid_desc_bpreshuffled,
1471 auto a_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1473 a_block_desc_ak0_m_ak1.GetElementSpaceSize() /
APackedSize);
1479 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
1481 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
1482 decltype(c_thread_buf) c_thread_buf_up;
1486 c_thread_buf.num_of_v_,
1487 c_thread_buf.s_per_v,
1491 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
1492 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
1496 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
1497 const auto waveId_m = wave_idx[
I0];
1498 const auto waveId_n = wave_idx[
I1];
1500 static constexpr
auto mfma = BlockwiseGemmPipe::xdlops_gemm.mfma;
1502 auto thread_offset_shuffled =
1505 auto a_thread_offset_m = waveId_m;
1507 auto a_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1510 decltype(a_scale_grid_desc_am_ak),
1511 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
1517 true>(a_scale_grid_desc_am_ak,
1523 auto b_thread_offset_n = waveId_n;
1525 auto b_scale_thread_copy = ThreadwiseTensorSliceTransfer_v2<
1528 decltype(b_scale_grid_desc_bn_ak),
1529 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1535 true>(b_scale_grid_desc_bn_ak,
1540 if constexpr(IsInputGemm)
1542 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2 /
BPackedSize;
1543 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1544 p_b_grid_up + expert_id * expert_stride /
BPackedSize,
1545 b_grid_desc_bpreshuffled.GetElementSpaceSize());
1546 auto b_blockwise_copy_up = ThreadwiseTensorSliceTransfer_v2<
1549 decltype(b_grid_desc_bpreshuffled),
1550 decltype(b_block_desc_bk0_n_bk1),
1551 Sequence<Number<NXdlPerWave>{},
I1, Number<KRepeat>{}, Number<BK1Value>{}>,
1552 Sequence<1, 2, 0, 3>,
1554 BBlockTransferSrcScalarPerVector,
1555 BThreadTransferSrcResetCoordinateAfterRun,
1556 true>(b_grid_desc_bpreshuffled,
1561 const BScaleDataType* p_b_scale_grid_up = p_b_scale_grid + expert_scale_stride / 2;
1562 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
1563 p_b_scale_grid_up + expert_id * expert_scale_stride,
1564 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
1565 auto b_scale_thread_copy_up = ThreadwiseTensorSliceTransfer_v2<
1568 decltype(b_scale_grid_desc_bn_ak),
1569 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
1576 b_scale_grid_desc_bn_ak,
1581 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1582 a_grid_desc_ak0_m_ak1,
1583 a_block_desc_ak0_m_ak1,
1587 a_block_slice_copy_step,
1588 b_grid_desc_bpreshuffled,
1589 b_block_desc_bk0_n_bk1,
1591 b_blockwise_copy_up,
1595 b_block_slice_copy_step,
1598 a_scale_grid_desc_am_ak,
1599 a_scale_thread_copy,
1601 b_scale_grid_desc_bn_ak,
1602 b_scale_thread_copy,
1603 b_scale_thread_copy_up,
1605 b_scale_grid_buf_up,
1606 num_k_block_main_loop);
1610 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
1611 a_grid_desc_ak0_m_ak1,
1612 a_block_desc_ak0_m_ak1,
1616 a_block_slice_copy_step,
1617 b_grid_desc_bpreshuffled,
1618 b_block_desc_bk0_n_bk1,
1622 b_block_slice_copy_step,
1624 a_scale_grid_desc_am_ak,
1625 a_scale_thread_copy,
1627 b_scale_grid_desc_bn_ak,
1628 b_scale_thread_copy,
1630 num_k_block_main_loop);
1635 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
1636 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
1640 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
1641 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
1645 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
1646 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_M3_M4_N2();
1648 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
1649 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
1650 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
1651 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
1652 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
1653 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
1654 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
1655 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
1658 static_assert(M0 * M1 * M2 * M3 * M4 == MPerBlock);
1659 static_assert(M4 == 4);
1663 vector_type<float, 4> topk_weights;
1664 static_for<0, NXdlPerWave, 1>{}([&](
auto n0) {
1665 static_for<0, MXdlPerWave, 1>{}([&](
auto m0) {
1666 static_for<0, M2, 1>{}([&](
auto m2) {
1667 const index_t m_pos = block_m_id * MPerBlock + m0 * M1 * M2 * M3 * M4 +
1668 m1 * M2 * M3 * M4 + m2 * M3 * M4 + m3 * M4;
1669 if constexpr(MulRoutedWeight)
1671 topk_weights = *c_style_pointer_cast<const vector_type<float, M4>*>(
1672 p_ds_grid[
I2] + m_pos);
1674 static_for<0, M4, 1>{}([&](
auto m4) {
1676 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
1678 constexpr
auto cidx = Number<c_offset>{};
1680 if constexpr(IsInputGemm)
1684 float gate = c_thread_buf[cidx];
1685 float up = c_thread_buf_up[cidx];
1686 if constexpr(MulRoutedWeight)
1688 gate = gate * topk_weights.AsType<
float>()[m4];
1689 up = up * topk_weights.AsType<
float>()[m4];
1691 tensor_operation::element_wise::Silu{}(gate, gate);
1692 c_thread_buf_fp32(cidx) = gate * up;
1696 float gate = c_thread_buf[cidx];
1697 float up = c_thread_buf_up[cidx];
1698 if constexpr(MulRoutedWeight)
1700 gate = gate * topk_weights.AsType<
float>()[m4];
1701 up = up * topk_weights.AsType<
float>()[m4];
1703 tensor_operation::element_wise::Gelu{}(gate, gate);
1704 c_thread_buf_fp32(cidx) = gate * up;
1709 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
1710 if constexpr(MulRoutedWeight)
1712 c_thread_buf_fp32(cidx) =
1713 topk_weights.AsType<
float>()[m4] * c_thread_buf_fp32[cidx];
1721 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
1724 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
1725 static_cast<CShuffleDataType*
>(p_shared),
1726 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1729 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
1733 Number<CShuffleMXdlPerWavePerShuffle>{},
1740 Number<CShuffleNXdlPerWavePerShuffle>{},
1743 make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}),
1745 Sequence<>{}, Sequence<0, 2, 4, 5, 6>{}, Sequence<>{}, Sequence<1, 3, 7>{}));
1749 const auto c_thread_mtx_on_block =
1750 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
1752 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
1753 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
1755 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
1761 const auto m_thread_data_on_block_idx =
1762 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
1765 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
1771 const auto n_thread_data_on_block_idx =
1772 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
1776 auto c_thread_copy_vgpr_to_lds =
1777 ThreadwiseTensorSliceTransfer_v1r3<AccDataType,
1779 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1780 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
1782 Sequence<CShuffleMXdlPerWavePerShuffle,
1783 CShuffleNXdlPerWavePerShuffle,
1790 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
1796 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1799 m_thread_data_on_block_idx[
I1],
1800 n_thread_data_on_block_idx[
I1],
1801 m_thread_data_on_block_idx[
I2],
1802 m_thread_data_on_block_idx[
I3],
1803 m_thread_data_on_block_idx[
I4],
1804 n_thread_data_on_block_idx[
I2]),
1807 using EDataType = CDataType;
1810 problem.M, problem.MPadded, problem.N, problem.NPadded, problem.StrideDs);
1812 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
1814 ds_grid_desc_m_n, problem.MBlock, problem.NBlock);
1818 return make_dynamic_buffer<AddressSpaceEnum::Global>(
1819 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
1821 Number<NumDTensor>{});
1825 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
1827 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
1828 Number<NumDTensor>{}));
1832 tie(c_shuffle_block_buf),
1834 {
return ds_grid_buf[i]; },
1835 Number<NumDTensor>{}));
1838 const auto idx_c_ds_block_begin =
1846 Number<NumDTensor>{}));
1848 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
1849 c_grid_desc_mblock_mperblock_nblock_nperblock;
1851 using CDEBlockTransferCluster =
1852 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
1853 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
1854 constexpr
index_t scatter_weight_idx = 1;
1855 auto cde_block_copy_lds_and_global = ThreadGroupTensorSliceTransfer_v7r3_scatter<
1859 decltype(c_ds_desc_refs),
1860 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
1861 CElementwiseOperation,
1862 Sequence<static_cast<index_t>(EGlobalMemoryDataOperation)>,
1865 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1867 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>,
1868 CDEBlockTransferCluster,
1869 Sequence<0, 1, 2, 3>,
1870 Sequence<0, 1, 2, 3>,
1871 Sequence<0, 1, 2, 3>,
1874 CDEShuffleBlockTransferScalarPerVectors,
1886 idx_c_ds_block_begin,
1887 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1891 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
1892 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
1893 constexpr
auto sfc_c_vgpr =
1894 SpaceFillingCurve<Sequence<MXdlPerWave, NXdlPerWave, 1, 1, M2, 1, M4, 1>,
1895 Sequence<0, 1, 2, 3, 4, 5, 6, 7>,
1896 Sequence<CShuffleMXdlPerWavePerShuffle,
1897 CShuffleNXdlPerWavePerShuffle,
1905 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
1908 constexpr
auto sfc_cde_block =
1909 SpaceFillingCurve<Sequence<1, MPerBlock, 1, NPerBlock>,
1910 Sequence<0, 2, 1, 3>,
1912 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
1914 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>>{};
1916 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
1917 constexpr
auto EMThreads =
1918 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
1919 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
1920 constexpr
auto ENThreads =
1921 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
1922 static_for<0, num_access, 1>{}([&](
auto access_id) {
1924 StaticallyIndexedArray<IndexType, EMRepeats> scatter_offsets;
1926 auto dstidx = sfc_cde_block.GetIndex(access_id);
1928 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
1929 static_for<0, EMRepeats, 1>{}([&](
auto m0) {
1930 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
1931 IndexType token_offset = fused_token & 0xffffff;
1932 if constexpr(IsInputGemm)
1934 token_offset = token_offset * problem.TopK + (fused_token >> 24);
1936 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.N;
1942 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1943 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
1945 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
1946 c_shuffle_block_buf);
1952 cde_block_copy_lds_and_global.Run(
1955 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1959 if constexpr(access_id < num_access - 1)
1961 constexpr
auto cde_lds_and_global_step =
1962 sfc_cde_block.GetForwardStep(access_id);
1965 static_for<0, NumDTensor, 1>{}([&](
auto i) {
1966 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
1967 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
1971 cde_block_copy_lds_and_global.MoveDstSliceWindow(
1972 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
1974 cde_lds_and_global_step);
1981 template <
bool HasMainKBlockLoop,
1985 const index_t* p_sorted_expert_ids,
1986 const index_t* p_max_token_id,
1987 const ADataType* p_a_grid,
1988 const AScaleDataType* p_a_scale_grid,
1989 const BDataType* p_b_grid,
1990 const BScaleDataType* p_b_scale_grid,
1992 CDataType* p_c_grid,
1996 AElementwiseOperation a_element_op,
1997 BElementwiseOperation b_element_op,
1998 CElementwiseOperation c_element_op)
2011 const auto b_grid_desc_bpreshuffled =
2013 const auto c_grid_desc_m_n = MakeCGridDescriptor_M_N<CLayout>(
2021 const auto Padded_Scale_M =
2045 const auto c_grid_desc_mblock_mperblock_nblock_nperblock =
2049 const index_t max_token_id = __builtin_amdgcn_readfirstlane(p_max_token_id[0]);
2050 const index_t expert_block_id = NSwizzle ? blockIdx.x / problem.
NBlock : blockIdx.y;
2051 if(expert_block_id * MPerBlock >= max_token_id)
2054 __builtin_amdgcn_readfirstlane(p_sorted_expert_ids[expert_block_id]);
2055 const auto block_mn = [&]() -> std::pair<int, int> {
2056 if constexpr(NSwizzle)
2058 const index_t ecnt_prefix = p_max_token_id[1 + expert_id];
2060 const index_t ecnt = p_max_token_id[2 + expert_id] - ecnt_prefix;
2061 const index_t expert_swizzle =
2062 ecnt > 0 ? ecnt : 1;
2063 const index_t bid_new = blockIdx.x - prefix_block;
2064 const index_t nid = __builtin_amdgcn_readfirstlane(
2065 bid_new % 8 + bid_new / (8 * expert_swizzle) * 8);
2067 __builtin_amdgcn_readfirstlane(ecnt_prefix + bid_new / 8 % expert_swizzle);
2072 return {blockIdx.x, blockIdx.y};
2076 const index_t block_n_id = block_mn.first;
2077 const index_t block_m_id = block_mn.second;
2079 __builtin_amdgcn_readfirstlane(p_sorted_token_ids[block_m_id * MPerBlock] & 0xffffff);
2082 constexpr
auto AMThreads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I1);
2083 constexpr
auto AK0Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I0);
2084 constexpr
auto AK1Threads = ABlockTransferThreadClusterLengths_AK0_M_AK1{}.At(
I2);
2085 constexpr
auto AKThreads = AK0Threads * AK1Threads;
2086 constexpr
auto AMRepeats = MPerBlock / AMThreads;
2087 const index_t token_pos = block_m_id * MPerBlock + threadIdx.x / AKThreads;
2089 if(token_pos >= max_token_id || token0 >= problem.
NumTokens)
2093 const index_t fused_token = p_sorted_token_ids[token_pos + m0 * AMThreads];
2094 index_t token_offset = fused_token & 0xffffff;
2095 if constexpr(!IsInputGemm)
2097 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2099 gather_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
K;
2103 __builtin_amdgcn_readfirstlane(problem.
N * problem.
K * (IsInputGemm ? 2 : 1));
2104 const index_t expert_scale_stride = __builtin_amdgcn_readfirstlane(
2105 problem.
N * (IsInputGemm ? 2 : 1) *
2109 const index_t n_block_data_idx_on_grid =
2110 __builtin_amdgcn_readfirstlane(block_n_id * NXdlPerWave /
NXdlPack);
2113 const auto a_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2114 p_a_grid, a_grid_desc_ak0_m_ak1.GetElementSpaceSize());
2115 const auto b_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2116 p_b_grid + expert_id * expert_stride, b_grid_desc_bpreshuffled.GetElementSpaceSize());
2119 const auto a_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2120 p_a_scale_grid, a_scale_grid_desc_am_ak.GetElementSpaceSize());
2121 const auto b_scale_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2122 p_b_scale_grid + (expert_id * expert_scale_stride) /
sizeof(BScaleDataType),
2123 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2135 ABlockTransferThreadClusterLengths_AK0_M_AK1,
2136 ABlockTransferThreadClusterArrangeOrder,
2139 decltype(a_grid_desc_ak0_m_ak1),
2140 decltype(a_block_desc_ak0_m_ak1),
2141 ABlockTransferSrcAccessOrder,
2142 ABlockTransferSrcVectorDim,
2144 ABlockTransferSrcScalarPerVector,
2146 1>(a_grid_desc_ak0_m_ak1,
2148 a_block_desc_ak0_m_ak1,
2154 auto b_block_buf_ping = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2155 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2156 auto b_block_buf_pong = make_static_buffer<AddressSpaceEnum::Vgpr, BDataType>(
2157 b_block_desc_bk0_n_bk1.GetElementSpaceSize());
2158 auto b_block_bufs =
make_tuple(b_block_buf_ping, b_block_buf_pong);
2160 auto b_blockwise_copy =
2163 decltype(b_grid_desc_bpreshuffled),
2164 decltype(b_block_desc_bk0_n_bk1),
2172 BBlockTransferSrcScalarPerVector,
2173 BThreadTransferSrcResetCoordinateAfterRun,
2175 b_grid_desc_bpreshuffled,
2184 auto a_block_buf_ping = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2185 static_cast<ADataType*
>(p_shared_0), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2186 auto a_block_buf_pong = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2187 static_cast<ADataType*
>(p_shared_1), a_block_desc_ak0_m_ak1.GetElementSpaceSize());
2188 auto a_block_bufs =
make_tuple(a_block_buf_ping, a_block_buf_pong);
2194 static_assert(std::is_default_constructible_v<BlockwiseGemmPipe>);
2196 auto c_thread_buf = blockwise_gemm_pipeline.GetCThreadBuffer();
2197 decltype(c_thread_buf) c_thread_buf_up;
2201 c_thread_buf.num_of_v_,
2202 c_thread_buf.s_per_v,
2206 const index_t num_k_block_main_loop = __builtin_amdgcn_readfirstlane(
2207 (a_grid_desc_ak0_m_ak1.GetLength(
I0) * a_grid_desc_ak0_m_ak1.GetLength(
I2)) /
2211 const auto wave_idx = BlockwiseGemmPipe::GetWaveIdx();
2212 const auto waveId_m = wave_idx[
I0];
2213 const auto waveId_n = wave_idx[
I1];
2215 auto thread_offset_shuffled =
2218 auto a_thread_offset_m = waveId_m;
2221 const index_t token_scale_pos = block_m_id * MPerBlock;
2222 if(token_scale_pos >= max_token_id || token0 >= problem.
NumTokens)
2228 decltype(a_scale_grid_desc_am_ak),
2229 decltype(BlockwiseGemmPipe::a_scale_thread_desc),
2235 true>(a_scale_grid_desc_am_ak,
2241 auto b_thread_offset_n = waveId_n;
2246 decltype(b_scale_grid_desc_bn_ak),
2247 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2253 true>(b_scale_grid_desc_bn_ak,
2258 if constexpr(IsInputGemm)
2260 const BDataType* p_b_grid_up = p_b_grid + expert_stride / 2;
2261 const auto b_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2262 p_b_grid_up + expert_id * expert_stride,
2263 b_grid_desc_bpreshuffled.GetElementSpaceSize());
2264 auto b_blockwise_copy_up =
2267 decltype(b_grid_desc_bpreshuffled),
2268 decltype(b_block_desc_bk0_n_bk1),
2276 BBlockTransferSrcScalarPerVector,
2277 BThreadTransferSrcResetCoordinateAfterRun,
2279 b_grid_desc_bpreshuffled,
2285 const BScaleDataType* p_b_scale_grid_up =
2286 p_b_scale_grid + expert_scale_stride / 2 /
sizeof(BScaleDataType);
2287 const auto b_scale_grid_buf_up = make_dynamic_buffer<AddressSpaceEnum::Global>(
2288 p_b_scale_grid_up + expert_id * expert_scale_stride /
sizeof(BScaleDataType),
2289 b_scale_grid_desc_bn_ak.GetElementSpaceSize());
2294 decltype(b_scale_grid_desc_bn_ak),
2295 decltype(BlockwiseGemmPipe::b_scale_thread_desc),
2302 b_scale_grid_desc_bn_ak,
2307 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2309 a_grid_desc_ak0_m_ak1,
2310 a_block_desc_ak0_m_ak1,
2314 a_block_slice_copy_step,
2316 b_grid_desc_bpreshuffled,
2317 b_block_desc_bk0_n_bk1,
2319 b_blockwise_copy_up,
2323 b_block_slice_copy_step,
2328 a_scale_grid_desc_am_ak,
2329 a_scale_thread_copy,
2332 b_scale_grid_desc_bn_ak,
2333 b_scale_thread_copy,
2334 b_scale_thread_copy_up,
2336 b_scale_grid_buf_up,
2337 num_k_block_main_loop);
2341 blockwise_gemm_pipeline.template Run<HasMainKBlockLoop, TailNum>(
2342 a_grid_desc_ak0_m_ak1,
2343 a_block_desc_ak0_m_ak1,
2347 a_block_slice_copy_step,
2348 b_grid_desc_bpreshuffled,
2349 b_block_desc_bk0_n_bk1,
2353 b_block_slice_copy_step,
2355 a_scale_grid_desc_am_ak,
2356 a_scale_thread_copy,
2358 b_scale_grid_desc_bn_ak,
2359 b_scale_thread_copy,
2361 num_k_block_main_loop);
2366 static_assert(MXdlPerWave % CShuffleMXdlPerWavePerShuffle == 0 &&
2367 NXdlPerWave % CShuffleNXdlPerWavePerShuffle == 0,
2369 static_assert(CShuffleMXdlPerWavePerShuffle %
MXdlPack == 0 &&
2370 CShuffleNXdlPerWavePerShuffle %
NXdlPack == 0,
2373 constexpr
index_t MWave = MPerBlock / (MXdlPerWave * MPerXdl);
2376 constexpr
auto c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2 =
2377 blockwise_gemm_pipeline.GetCThreadDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2381 constexpr
auto c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp =
2382 blockwise_gemm_pipeline.GetCBlockDescriptor_M0_N0_M1_N1_M2_N2_M3_M4_M5_N3();
2384 constexpr
auto M0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I0);
2385 constexpr
auto N0 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I1);
2386 constexpr
auto M1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I2);
2387 constexpr
auto N1 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I3);
2388 constexpr
auto M2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I4);
2389 constexpr
auto N2 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I5);
2390 constexpr
auto M3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I6);
2391 constexpr
auto M4 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I7);
2392 constexpr
auto M5 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I8);
2393 constexpr
auto N3 = c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2_tmp.GetLength(
I9);
2397 static_assert(M0 * M1 * M2 * M3 * M4 * M5 == MPerBlock);
2398 static_assert(M5 == 4);
2408 const index_t m_pos = block_m_id * MPerBlock +
2409 m0 * M2 * M1 * M3 * M4 * M5 +
2410 m1 * M2 * M3 * M4 * M5 +
2411 imxdl * M3 * M4 * M5 + m3 * M4 * M5 + m4 * M5;
2412 if constexpr(MulRoutedWeight)
2415 *c_style_pointer_cast<const vector_type<float, M5>*>(
2416 p_ds_grid[
I2] + m_pos);
2420 blockwise_gemm_pipeline.GetCThreadDesc().CalculateOffset(
2421 make_tuple(m0, n0, imxdl, inxdl, m3 * M5 + m5));
2424 if constexpr(IsInputGemm)
2426 if constexpr(ActivationOperation ==
2429 float gate = c_thread_buf[cidx];
2430 float up = c_thread_buf_up[cidx];
2431 if constexpr(MulRoutedWeight)
2433 gate = gate * topk_weights.AsType<
float>()[m5];
2434 up = up * topk_weights.AsType<
float>()[m5];
2437 c_thread_buf_fp32(cidx) = gate * up;
2441 float gate = c_thread_buf[cidx];
2442 float up = c_thread_buf_up[cidx];
2443 if constexpr(MulRoutedWeight)
2445 gate = gate * topk_weights.AsType<
float>()[m5];
2446 up = up * topk_weights.AsType<
float>()[m5];
2449 c_thread_buf_fp32(cidx) = gate * up;
2454 c_thread_buf_fp32(cidx) = c_thread_buf[cidx];
2455 if constexpr(MulRoutedWeight)
2457 c_thread_buf_fp32(cidx) =
2458 topk_weights.AsType<
float>()[m5] *
2459 c_thread_buf_fp32[cidx];
2469 constexpr
auto c_shuffle_block_desc_mblock_mperblock_nblock_nperblock =
2472 auto c_shuffle_block_buf = make_dynamic_buffer<AddressSpaceEnum::Lds>(
2473 static_cast<CShuffleDataType*
>(p_shared_0),
2474 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2477 c_shuffle_block_desc_mblock_mperblock_nblock_nperblock,
2503 const auto c_thread_mtx_on_block =
2504 blockwise_gemm_pipeline.CalculateCThreadOriginDataIndex(
I0,
I0,
I0,
I0);
2506 const index_t m_thread_data_on_block = c_thread_mtx_on_block[
I0];
2507 const index_t n_thread_data_on_block = c_thread_mtx_on_block[
I1];
2509 const auto m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor =
2515 const auto m_thread_data_on_block_idx =
2516 m_thread_data_on_block_to_m0_m1_m2_m3_m4_adaptor.CalculateBottomIndex(
2519 const auto n_thread_data_on_block_to_n0_n1_n2_adaptor =
2525 const auto n_thread_data_on_block_idx =
2526 n_thread_data_on_block_to_n0_n1_n2_adaptor.CalculateBottomIndex(
2533 decltype(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2534 decltype(c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2),
2537 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2546 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2551 true>{c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2554 m_thread_data_on_block_idx[
I1],
2555 n_thread_data_on_block_idx[
I1],
2556 m_thread_data_on_block_idx[
I2],
2557 n_thread_data_on_block_idx[
I2],
2558 m_thread_data_on_block_idx[
I3],
2559 m_thread_data_on_block_idx[
I4],
2560 m_thread_data_on_block_idx[
I5],
2561 n_thread_data_on_block_idx[
I3]),
2564 using EDataType = CDataType;
2569 const auto ds_grid_desc_mblock_mperblock_nblock_nperblock =
2575 return make_dynamic_buffer<AddressSpaceEnum::Global>(
2576 p_ds_grid[i], ds_grid_desc_m_n[i].GetElementSpaceSize());
2582 tie(c_shuffle_block_desc_mblock_mperblock_nblock_nperblock),
2584 {
return ds_grid_desc_mblock_mperblock_nblock_nperblock[i]; },
2589 tie(c_shuffle_block_buf),
2591 {
return ds_grid_buf[i]; },
2595 const auto idx_c_ds_block_begin =
2605 const auto e_grid_desc_mblock_mperblock_nblock_nperblock =
2606 c_grid_desc_mblock_mperblock_nblock_nperblock;
2608 using CDEBlockTransferCluster =
2609 CShuffleBlockTransferClusterLengths_MBlock_MPerBlock_NBlock_NPerBlock;
2610 const auto EGlobalMemoryDataOperation = CGlobalMemoryDataOperation;
2611 constexpr
index_t scatter_weight_idx = 3;
2616 decltype(c_ds_desc_refs),
2617 decltype(
tie(e_grid_desc_mblock_mperblock_nblock_nperblock)),
2618 CElementwiseOperation,
2623 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2625 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>,
2626 CDEBlockTransferCluster,
2632 CDEShuffleBlockTransferScalarPerVectors,
2644 idx_c_ds_block_begin,
2645 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2649 auto c_grid_buf = make_dynamic_buffer<AddressSpaceEnum::Global>(
2650 p_c_grid, c_grid_desc_mblock_mperblock_nblock_nperblock.GetElementSpaceSize());
2652 constexpr
auto sfc_c_vgpr =
2663 Sequence<0, 1, 2, 3, 4, 5, 6, 7, 8, 9>,
2665 CShuffleNXdlPerWavePerShuffle /
NXdlPack,
2675 constexpr
index_t num_access = sfc_c_vgpr.GetNumOfAccess();
2678 constexpr
auto sfc_cde_block =
2682 CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl,
2684 CShuffleNXdlPerWavePerShuffle *
NWave * NPerXdl>>{};
2686 static_assert(num_access == sfc_cde_block.GetNumOfAccess(),
"wrong!");
2687 constexpr
auto EMThreads =
2688 CDEBlockTransferCluster{}.At(
I0) * CDEBlockTransferCluster{}.At(
I1);
2689 constexpr
auto EMRepeats = CShuffleMXdlPerWavePerShuffle * MWave * MPerXdl / EMThreads;
2690 constexpr
auto ENThreads =
2691 CDEBlockTransferCluster{}.At(
I2) * CDEBlockTransferCluster{}.At(
I3);
2696 auto dstidx = sfc_cde_block.GetIndex(access_id);
2698 block_m_id * MPerBlock + threadIdx.x / ENThreads * EMRepeats + dstidx(
I1);
2700 const index_t fused_token = p_sorted_token_ids[c_token_pos + m0];
2701 IndexType token_offset = fused_token & 0xffffff;
2702 if constexpr(IsInputGemm)
2704 token_offset = token_offset * problem.
TopK + (fused_token >> 24);
2706 scatter_offsets(m0) =
static_cast<IndexType
>(token_offset) * problem.
N;
2712 c_thread_copy_vgpr_to_lds.Run(c_thread_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2713 sfc_c_vgpr.GetIndexTupleOfNumber(access_id),
2715 c_block_desc_m0_n0_m1_n1_m2_m3_m4_n2,
2716 c_shuffle_block_buf);
2722 cde_block_copy_lds_and_global.Run(
2725 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2729 if constexpr(access_id < num_access - 1)
2731 constexpr
auto cde_lds_and_global_step =
2732 sfc_cde_block.GetForwardStep(access_id);
2736 cde_block_copy_lds_and_global.MoveSrcSliceWindow(
2737 c_ds_desc_refs, i +
I1, cde_lds_and_global_step);
2741 cde_block_copy_lds_and_global.MoveDstSliceWindow(
2742 tie(e_grid_desc_mblock_mperblock_nblock_nperblock),
2744 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:56
__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:276
__host__ constexpr __device__ index_t get_warp_size()
Definition: get_id.hpp:42
__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:298
__device__ index_t get_thread_local_1d_id()
Definition: get_id.hpp:52
__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:745
const index_t * p_sorted_expert_ids
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:806
const index_t * p_sorted_token_ids
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:805
const BScaleDataType * p_b_scale_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:811
const BElementwiseOperation b_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:816
const BDataType * p_b_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:810
const index_t * p_max_token_id
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:807
DsGridPointer p_ds_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:812
const AElementwiseOperation a_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:815
CDataType * p_c_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:813
const AScaleDataType * p_a_scale_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:809
__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:746
const CElementwiseOperation c_element_op
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:817
const ADataType * p_a_grid
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:808
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:673
index_t AK0
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:737
index_t NPadded
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:734
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:674
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:821
index_t a_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:863
index_t b_scale_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:864
index_t a_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:861
__device__ SplitKBatchOffset(Argument &karg, index_t k_id)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:822
index_t b_k_split_offset
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:862
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:171
static constexpr auto I6
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:181
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:1041
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:649
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:1984
__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:1256
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:661
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:987
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:1039
static constexpr __host__ TailNumber CalculateKBlockLoopTailNum(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1248
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
__host__ static __device__ auto CalculateBK0Shuffled(index_t K)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:277
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:997
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:477
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:867
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 __device__ auto CalculateBN0Shuffled(index_t N)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:273
__host__ static constexpr __device__ auto MakeBMmaTileDescriptor_N0_N1_N2_N3_K(const BBlockDesc_BK0_N_BK1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:595
__host__ static __device__ auto MakeCGridDescriptor_M_N(IndexType M, IndexType MPad, IndexType N, IndexType NPad, IndexType StrideC)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:604
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:585
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:1241
static constexpr __host__ bool CheckValidity(const Argument &karg)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:1063
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:628
__host__ static constexpr __device__ auto MakeGemmMmaTileDescriptor(const TileDesc_K0_MN_K1 &)
Definition: gridwise_moe_mx_gemm_bpreshuffle.hpp:328
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:1126
static constexpr auto selected_mfma
Definition: xdlops_gemm.hpp:1647
Definition: sequence.hpp:43
Definition: tensor_space_filling_curve.hpp:20
Definition: static_buffer.hpp:75
Definition: thread_group_tensor_slice_transfer_gather_direct_load.hpp:57
Definition: thread_group_tensor_slice_transfer_v7r3_scatter.hpp:51
Definition: threadwise_tensor_slice_transfer.hpp:39
Helper structure that facilitates transfer of source (grid) data to destination threads.
Definition: threadwise_tensor_slice_transfer.hpp:234
Definition: tuple.hpp:117
Unsigned representation of a conventional biased Float32 exponent.
Definition: e8m0.hpp:25
Definition: data_type.hpp:41
Definition: integral_constant.hpp:20
Definition: data_type.hpp:186
Definition: functional2.hpp:33
Definition: device_base.hpp:51
Definition: unary_element_wise_operation.hpp:1007
Definition: unary_element_wise_operation.hpp:334
Definition: unary_element_wise_operation.hpp:1049
Definition: dtype_vector.hpp:10
#define CK_ENV(name)
Definition: env.hpp:129