Skip to content

Commit d954c9c

Browse files
mgoincodex
andcommitted
Document masked CUDA layout mapping
Co-authored-by: OpenAI Codex <codex@openai.com> Signed-off-by: mgoin <mgoin64@gmail.com>
1 parent c4cc8ef commit d954c9c

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

csrc/libtorch_stable/activation_kernels.cu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,14 @@ __device__ __forceinline__ bool get_masked_row_range(
501501
const int* __restrict__ valid_token_counts, const int max_num_tokens,
502502
int64_t& first_row, int64_t& end_row) {
503503
if constexpr (BATCHED_EXPERTS) {
504+
// [E, T, *]: each block handles one expert's valid token prefix.
504505
const int expert = blockIdx.y;
505506
const int num_tokens =
506507
max(0, min(valid_token_counts[expert], max_num_tokens));
507508
first_row = static_cast<int64_t>(expert) * max_num_tokens;
508509
end_row = first_row + num_tokens;
509510
} else {
511+
// [T, *]: each block handles one row, masked by a single valid prefix.
510512
const int row = blockIdx.x;
511513
const int num_tokens = max(0, min(valid_token_counts[0], max_num_tokens));
512514
if (row >= num_tokens) {
@@ -520,6 +522,7 @@ __device__ __forceinline__ bool get_masked_row_range(
520522

521523
template <bool BATCHED_EXPERTS>
522524
__device__ __forceinline__ int get_masked_feature_index() {
525+
// Feature tiles occupy x for batched layouts and y for flat layouts.
523526
if constexpr (BATCHED_EXPERTS) {
524527
return blockIdx.x * blockDim.x + threadIdx.x;
525528
} else {
@@ -835,6 +838,7 @@ void masked_moe_activation(
835838

836839
constexpr int block_size = 256;
837840
const int feature_blocks = (d + block_size - 1) / block_size;
841+
// Batched grid: (feature tile, expert); flat grid: (row, feature tile).
838842
dim3 grid = batched_experts ? dim3(feature_blocks, num_experts)
839843
: dim3(max_num_tokens, feature_blocks);
840844
dim3 block(block_size);

0 commit comments

Comments
 (0)