Skip to content

Commit 0a2b6c4

Browse files
[rocm-libraries] ROCm/rocm-libraries#4297 (commit 5ff580c)
moe flatmm xcd remap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit co-authors: @Chi-Chu319 @juuso-oskari Added XCD remapping for flatmm moe <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 15"> <link id=Main-File rel=Main-File href="file:///C:/Users/tianxiwu/AppData/Local/Temp/msohtmlclip1/01/clip.htm"> <link rel=File-List href="file:///C:/Users/tianxiwu/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml"> <style> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";} @page {margin:.75in .7in .75in .7in; mso-header-margin:.3in; mso-footer-margin:.3in;} tr {mso-height-source:auto;} col {mso-width-source:auto;} br {mso-data-placement:same-cell;} td {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:11.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:Arial, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;} --> </style> </head> <body link="#467886" vlink="#96607D"> batch | Mixtral (tflops, wip_355) | Mixtral-7B  (tflops, our branch) | perf boost -- | -- | -- | -- 64 | 865.424 | 995.455 | 15.0% 256 | 886.336 | 1020.96 | 15.2% 1024 | 890.808 | 1022.53 | 14.8% </body> </html>
1 parent 5cb8109 commit 0a2b6c4

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

include/ck_tile/ops/flatmm/kernel/moe_flatmm_kernel.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -901,16 +901,25 @@ struct MoeFlatmmKernel
901901
template <class MoeFlatmmKernelArgs>
902902
CK_TILE_DEVICE void operator()(MoeFlatmmKernelArgs kargs) const
903903
{
904-
int partition_idx = blockIdx.x;
905-
int total_work_tile_cnt = TilePartitioner::GridSize(kargs.M, kargs.N);
904+
// total number of tokens: sorted tokens + delimiter tokens + trailing padding tokens
905+
// we launch the grid based on the total number of tokens which needs to be static
906+
int partition_idx = blockIdx.x;
907+
auto max_token_id = kargs.p_max_token_id[0]; // sorted tokens + delimiter tokens
908+
int total_valid_tile_cnt = TilePartitioner::GridSize(max_token_id, kargs.N);
909+
auto tilePartitioner = TilePartitioner{max_token_id, kargs.N};
906910
do
907911
{
912+
if(partition_idx >= total_valid_tile_cnt)
913+
{
914+
return; // early exit for trailing padding tokens
915+
}
916+
partition_idx = tilePartitioner.RemapXCD(partition_idx, total_valid_tile_cnt);
908917
const auto [block_offset_m, block_offset_n] =
909-
TilePartitioner{kargs.M, kargs.N}.GetOutputTileIndex(partition_idx);
918+
tilePartitioner.GetOutputTileIndex(partition_idx);
910919

911920
this->operator()(kargs, block_offset_m, block_offset_n);
912921
partition_idx += gridDim.x;
913-
} while(UsePersistentKernel && partition_idx < total_work_tile_cnt);
922+
} while(UsePersistentKernel && partition_idx < total_valid_tile_cnt);
914923
}
915924

916925
template <class MoeFlatmmKernelArgs>
@@ -920,7 +929,6 @@ struct MoeFlatmmKernel
920929
// const auto [iM, iN] = TilePartitioner{kargs.M, kargs.N}.GetOutputTileIndex(blockIdx.x);
921930
const index_t coord_m = __builtin_amdgcn_readfirstlane(iM * TilePartitioner::MPerBlock);
922931
const index_t coord_n = __builtin_amdgcn_readfirstlane(iN * TilePartitioner::NPerBlock);
923-
const index_t max_token_id = kargs.p_max_token_id[0];
924932
// allocate LDS
925933
__shared__ char smem_ptr_ping[GetSmemPingSize()];
926934
__shared__ char smem_ptr_pong[GetSmemPongSize()];
@@ -948,8 +956,6 @@ struct MoeFlatmmKernel
948956
return gather_token_id;
949957
};
950958

951-
if(coord_m >= max_token_id)
952-
return;
953959
static_for<0, DramMRepeat, 1>{}([&](auto m0) {
954960
const auto row_idx =
955961
coord_m + m0 * (TilePartitioner::MPerBlock / DramMRepeat) + a_coord[I0];

include/ck_tile/ops/gemm/kernel/gemm_tile_partitioner.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,54 @@ struct GemmSpatiallyLocalTilePartitioner
265265
return integer_divide_ceil(K, KPerBlock);
266266
}
267267

268+
/**
269+
* @brief XCDs access ids in round robin format, this function remaps the 1D ids to continguous
270+
* XCD segments
271+
*
272+
* @param block_1d_id grid 1D id
273+
* @param total_num_tiles size of the 1D grid
274+
* @param NUM_XCDS number of XCDs
275+
* @return index_t The id after XCD remap
276+
*/
277+
CK_TILE_HOST_DEVICE static auto
278+
RemapXCD(index_t block_1d_id, index_t total_num_tiles, index_t NUM_XCDS = 8) noexcept -> index_t
279+
{
280+
// Number of ids per XCD in the new arrangement
281+
index_t ids_per_xcd = (total_num_tiles + NUM_XCDS - 1) / NUM_XCDS;
282+
283+
// When total_num_tiles cannot divide NUM_XCDS, some xcds will have
284+
// ids_per_xcd ids, the other will have ids_per_xcd - 1 ids.
285+
// We calculate the number of xcds that have ids_per_xcd ids as tall_xcds
286+
index_t tall_xcds = total_num_tiles % NUM_XCDS;
287+
tall_xcds = (tall_xcds == 0) ? NUM_XCDS : tall_xcds;
288+
289+
// Compute current XCD and local id within the XCD
290+
index_t xcd = block_1d_id % NUM_XCDS;
291+
index_t local_id = block_1d_id / NUM_XCDS;
292+
293+
// Calculate new id based on the new grouping
294+
if(xcd < tall_xcds)
295+
{
296+
block_1d_id = xcd * ids_per_xcd + local_id;
297+
}
298+
else
299+
{
300+
block_1d_id =
301+
tall_xcds * ids_per_xcd + (xcd - tall_xcds) * (ids_per_xcd - 1) + local_id;
302+
}
303+
304+
/**
305+
* original ids: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
306+
* XCD 0 gets: [0, 8], XCD 1 gets: [1, 9], ...
307+
*
308+
* post-remap ids: [0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15]
309+
* XCD 0 gets: [0, 1], XCD 1 gets: [2, 3], ...
310+
*
311+
* after remap the ids are continguous on each XCD
312+
*/
313+
return block_1d_id;
314+
}
315+
268316
/**
269317
* @brief Calculate workgroup 1D index mapping into 2D output C-tile space.
270318
*

0 commit comments

Comments
 (0)