Optimized rocm specific multicast transpose kernel#586
Open
alextmagro wants to merge 3 commits into
Open
Conversation
ipanfilo
reviewed
May 15, 2026
| HIP_CHECK(hipEventCreate(&stop)); | ||
|
|
||
| nvte_multi_cast_transpose(num_experts, nvte_in.data(), nvte_out.data(), stream); | ||
| HIP_CHECK(hipStreamSynchronize(stream)); |
Collaborator
There was a problem hiding this comment.
Is synchronize needed here?
Contributor
Author
There was a problem hiding this comment.
No, it isn't here. I had copied over the cast_transpose benchmark and edited that, but since we don't have rtc we don't need the pre-call and sync.
aris134
reviewed
May 19, 2026
aris134
reviewed
May 19, 2026
aris134
reviewed
May 19, 2026
Comment on lines
+150
to
+232
| } else { | ||
| #pragma unroll | ||
| for (int iter = 0; iter < NUM_ITERS; iter++) { | ||
| const int i1 = tidy + iter * WARPS_PER_TILE; | ||
| const int j1 = tidx; | ||
| #pragma unroll | ||
| for (int i2 = 0; i2 < NVEC_OUT; i2++) { | ||
| const int row = row_base + i1 * NVEC_OUT + i2; | ||
| const int col = col_base + j1 * NVEC_IN; | ||
|
|
||
| IVec in; | ||
| OVecC out_c; | ||
|
|
||
| if (row < num_rows) { | ||
| in.load(&input[row * row_length + col]); | ||
| } else { | ||
| #pragma unroll | ||
| for (int j2 = 0; j2 < NVEC_IN; j2++) in.val[j2] = IType(0); | ||
| } | ||
|
|
||
| #ifdef HAS_PACK_4xFLOAT8 | ||
| if constexpr (sizeof(OType) == 1) { | ||
| #pragma unroll | ||
| for (int j2 = 0; j2 < NVEC_IN; j2 += 4) { | ||
| const float v0 = static_cast<float>(in.val[j2]); | ||
| const float v1 = (j2+1 < NVEC_IN) ? static_cast<float>(in.val[j2+1]) : 0.0f; | ||
| const float v2 = (j2+2 < NVEC_IN) ? static_cast<float>(in.val[j2+2]) : 0.0f; | ||
| const float v3 = (j2+3 < NVEC_IN) ? static_cast<float>(in.val[j2+3]) : 0.0f; | ||
| if (row < num_rows) | ||
| amax = fmaxf(amax, fmaxf(fmaxf(fabsf(v0), fabsf(v1)), fmaxf(fabsf(v2), fabsf(v3)))); | ||
| uint32_t packed = rocm_pack_4xfloat8<OType>( | ||
| v0 * scale, v1 * scale, v2 * scale, v3 * scale); | ||
| uint8_t *bytes = reinterpret_cast<uint8_t *>(&packed); | ||
| #pragma unroll | ||
| for (int k = 0; k < 4 && j2 + k < NVEC_IN; k++) { | ||
| out_c.val[j2 + k] = reinterpret_cast<OType &>(bytes[k]); | ||
| local_t[j2 + k][iter].val[i2] = out_c.val[j2 + k]; | ||
| } | ||
| } | ||
| } else | ||
| #endif | ||
| { | ||
| #pragma unroll | ||
| for (int j2 = 0; j2 < NVEC_IN; j2++) { | ||
| const float v = static_cast<float>(in.val[j2]); | ||
| if (row < num_rows) | ||
| amax = fmaxf(amax, fabsf(v)); | ||
| const OType o = static_cast<OType>(v * scale); | ||
| out_c.val[j2] = o; | ||
| local_t[j2][iter].val[i2] = o; | ||
| } | ||
| } | ||
|
|
||
| if (row < num_rows) | ||
| out_c.nt_store(&output_c[row * row_length + col]); | ||
| } | ||
| } | ||
|
|
||
| #pragma unroll | ||
| for (int j2 = 0; j2 < NVEC_IN; j2++) { | ||
| #pragma unroll | ||
| for (int iter = 0; iter < NUM_ITERS; iter++) { | ||
| smem[tidx][tidy + iter * WARPS_PER_TILE] = local_t[j2][iter]; | ||
| } | ||
| __syncthreads(); | ||
| #pragma unroll | ||
| for (int iter = 0; iter < NUM_ITERS; iter++) { | ||
| const int i1 = tidx; | ||
| const int j1 = tidy + iter * WARPS_PER_TILE; | ||
| const int row = row_base + i1 * NVEC_OUT; | ||
| const int col = col_base + j1 * NVEC_IN + j2; | ||
| if (row + NVEC_OUT <= num_rows) { | ||
| smem[j1][i1].nt_store(&output_t[col * num_rows + row]); | ||
| } else if (row < num_rows) { | ||
| for (int k = 0; k < NVEC_OUT && row + k < num_rows; k++) | ||
| output_t[col * num_rows + row + k] = smem[j1][i1].val[k]; | ||
| } | ||
| } | ||
| if (j2 + 1 < NVEC_IN) { | ||
| __syncthreads(); | ||
| } | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
There seems to be a fair amount of duplication between the interior-tile and row-edge-tile paths. Most of the load/cast/pack/amax/local_t logic is identical, with the edge path only adding row predicates and partial-vector stores. Is there some way we can re-factor some of the duplicated parts into device inline helpers?
aris134
requested changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimizes the multi_cast_transpose kernel for rocm.
Benchmark Results
Qwen has 128 experts, DS has 256 experts. Benchmarked with shapes derived from MBS={1,2,4}
Balanced Experts
Skewed routing
Performance Summary
Average speedup (balanced): 5.0x
Average speedup (skewed): 5.8x
Average % peak (balanced): 49.4%
Average % peak (skewed): 49.0%
Change Summary
Vec::store_towhich pollutes L2 (CDNA4 L2 is write-allocate)rocm_pack_4xfloat8— 2v_cvt_pk_fp8_f32per 4 values vs upstream's scalarOType(scale * x)castsrocm_block_reduce_maxwithrocm_atomicMaxFloat— usesatomicMaxon int-reinterpreted float (single instruction) vs upstream's CAS looptile_m = local_bid % tiles_mfor L2 input localityRejected/skipped optimizations (click to expand)