|
| 1 | +/* |
| 2 | + * Scatter and gather kernels for MoE batched NVFP4 GEMM pipeline. |
| 3 | + * |
| 4 | + * Scatter: copies packed FP4 data from concatenated token layout to |
| 5 | + * padded per-expert batched layout. Zero-fills padding rows. |
| 6 | + * |
| 7 | + * Gather: copies BF16 results from padded per-expert batched layout |
| 8 | + * back to concatenated token layout. |
| 9 | + * |
| 10 | + * Both kernels use one threadblock per expert with vectorized 128-bit |
| 11 | + * (uint4) loads/stores for bandwidth efficiency. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <cuda_runtime.h> |
| 15 | +#include <cstdint> |
| 16 | + |
| 17 | +// ========================================================================= |
| 18 | +// Scatter: concatenated FP4 → padded per-expert batched FP4 |
| 19 | +// ========================================================================= |
| 20 | +// Each threadblock handles one expert. Threads cooperatively copy |
| 21 | +// n_tokens * row_bytes from the concatenated source to the padded |
| 22 | +// destination, then zero-fill padding rows. |
| 23 | +// |
| 24 | +// Data layout: |
| 25 | +// Input: packed_concat [total_tokens * row_bytes] contiguous |
| 26 | +// Output: packed_batched [num_experts * max_M * row_bytes] with zero padding |
| 27 | +// |
| 28 | +// row_bytes = K / 2 (packed FP4: 2 values per byte) |
| 29 | +__global__ void kMoeScatterNVFP4( |
| 30 | + const uint8_t* __restrict__ input, // [total_tokens * row_bytes] |
| 31 | + uint8_t* __restrict__ output, // [num_experts * max_M * row_bytes] |
| 32 | + const int* __restrict__ expert_offsets, // [num_experts + 1] cumulative token offsets |
| 33 | + int max_M, // padded max tokens per expert |
| 34 | + int row_bytes // K / 2 |
| 35 | +) { |
| 36 | + int expert = blockIdx.x; |
| 37 | + int start = expert_offsets[expert]; |
| 38 | + int end = expert_offsets[expert + 1]; |
| 39 | + int n_tokens = end - start; |
| 40 | + |
| 41 | + // Source: contiguous in concatenated buffer |
| 42 | + const uint8_t* src = input + (long long)start * row_bytes; |
| 43 | + |
| 44 | + // Destination: padded slot for this expert |
| 45 | + uint8_t* dst = output + (long long)expert * max_M * row_bytes; |
| 46 | + |
| 47 | + // Total bytes to process for this expert (data + padding) |
| 48 | + long long total_bytes = (long long)max_M * row_bytes; |
| 49 | + long long data_bytes = (long long)n_tokens * row_bytes; |
| 50 | + |
| 51 | + // Use vectorized uint4 (16-byte) copies where possible |
| 52 | + int tid = threadIdx.x; |
| 53 | + int stride = blockDim.x; |
| 54 | + |
| 55 | + // Copy data rows using uint4 vectorization |
| 56 | + long long vec_data_bytes = (data_bytes / 16) * 16; |
| 57 | + const uint4* src4 = reinterpret_cast<const uint4*>(src); |
| 58 | + uint4* dst4 = reinterpret_cast<uint4*>(dst); |
| 59 | + long long n_vec = vec_data_bytes / 16; |
| 60 | + |
| 61 | + for (long long i = tid; i < n_vec; i += stride) { |
| 62 | + dst4[i] = src4[i]; |
| 63 | + } |
| 64 | + |
| 65 | + // Handle remaining bytes in data region |
| 66 | + for (long long i = vec_data_bytes + tid; i < data_bytes; i += stride) { |
| 67 | + dst[i] = src[i]; |
| 68 | + } |
| 69 | + |
| 70 | + // Zero-fill padding region using uint4 |
| 71 | + long long pad_start = data_bytes; |
| 72 | + long long pad_bytes = total_bytes - pad_start; |
| 73 | + |
| 74 | + if (pad_bytes > 0) { |
| 75 | + // Align pad_start up to 16-byte boundary for vectorized zeroing |
| 76 | + long long aligned_pad_start = ((pad_start + 15) / 16) * 16; |
| 77 | + |
| 78 | + // Zero unaligned bytes at start of padding |
| 79 | + for (long long i = pad_start + tid; i < aligned_pad_start && i < total_bytes; i += stride) { |
| 80 | + dst[i] = 0; |
| 81 | + } |
| 82 | + |
| 83 | + // Vectorized zero-fill |
| 84 | + uint4 zero4 = make_uint4(0, 0, 0, 0); |
| 85 | + long long vec_pad_end = (total_bytes / 16) * 16; |
| 86 | + uint4* dst4_pad = reinterpret_cast<uint4*>(dst); |
| 87 | + long long vec_start = aligned_pad_start / 16; |
| 88 | + long long vec_end = vec_pad_end / 16; |
| 89 | + |
| 90 | + for (long long i = vec_start + tid; i < vec_end; i += stride) { |
| 91 | + dst4_pad[i] = zero4; |
| 92 | + } |
| 93 | + |
| 94 | + // Zero remaining bytes at end |
| 95 | + for (long long i = vec_pad_end + tid; i < total_bytes; i += stride) { |
| 96 | + dst[i] = 0; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +// ========================================================================= |
| 103 | +// Gather: padded per-expert BF16 → concatenated BF16 |
| 104 | +// ========================================================================= |
| 105 | +// Each threadblock handles one expert. Threads cooperatively copy |
| 106 | +// n_tokens * row_elems BF16 values from the padded batched output |
| 107 | +// to the concatenated result. |
| 108 | +// |
| 109 | +// Data layout: |
| 110 | +// Input: D_batched [num_experts * max_M * N] bf16 |
| 111 | +// Output: D_concat [total_tokens * N] bf16 |
| 112 | +// |
| 113 | +// row_bytes = N * 2 (bf16 = 2 bytes per element) |
| 114 | +__global__ void kMoeGatherBF16( |
| 115 | + const uint8_t* __restrict__ input, // [num_experts * max_M * row_bytes] |
| 116 | + uint8_t* __restrict__ output, // [total_tokens * row_bytes] |
| 117 | + const int* __restrict__ expert_offsets, // [num_experts + 1] |
| 118 | + int max_M, |
| 119 | + int row_bytes // N * 2 |
| 120 | +) { |
| 121 | + int expert = blockIdx.x; |
| 122 | + int start = expert_offsets[expert]; |
| 123 | + int end = expert_offsets[expert + 1]; |
| 124 | + int n_tokens = end - start; |
| 125 | + |
| 126 | + if (n_tokens <= 0) return; |
| 127 | + |
| 128 | + // Source: padded slot for this expert |
| 129 | + const uint8_t* src = input + (long long)expert * max_M * row_bytes; |
| 130 | + |
| 131 | + // Destination: contiguous in concatenated buffer |
| 132 | + uint8_t* dst = output + (long long)start * row_bytes; |
| 133 | + |
| 134 | + long long data_bytes = (long long)n_tokens * row_bytes; |
| 135 | + |
| 136 | + int tid = threadIdx.x; |
| 137 | + int stride = blockDim.x; |
| 138 | + |
| 139 | + // Vectorized uint4 copy |
| 140 | + long long vec_bytes = (data_bytes / 16) * 16; |
| 141 | + const uint4* src4 = reinterpret_cast<const uint4*>(src); |
| 142 | + uint4* dst4 = reinterpret_cast<uint4*>(dst); |
| 143 | + long long n_vec = vec_bytes / 16; |
| 144 | + |
| 145 | + for (long long i = tid; i < n_vec; i += stride) { |
| 146 | + dst4[i] = src4[i]; |
| 147 | + } |
| 148 | + |
| 149 | + // Handle remaining bytes |
| 150 | + for (long long i = vec_bytes + tid; i < data_bytes; i += stride) { |
| 151 | + dst[i] = src[i]; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +// ========================================================================= |
| 157 | +// extern "C" launchers |
| 158 | +// ========================================================================= |
| 159 | + |
| 160 | +extern "C" void cmoe_scatter_nvfp4( |
| 161 | + const void* input, |
| 162 | + void* output, |
| 163 | + const int* expert_offsets, |
| 164 | + int max_M, |
| 165 | + int K, |
| 166 | + int num_experts, |
| 167 | + cudaStream_t stream |
| 168 | +) { |
| 169 | + int row_bytes = K / 2; // packed FP4: 2 values per byte |
| 170 | + |
| 171 | + // One threadblock per expert, 256 threads |
| 172 | + dim3 grid(num_experts); |
| 173 | + dim3 block(256); |
| 174 | + |
| 175 | + kMoeScatterNVFP4<<<grid, block, 0, stream>>>( |
| 176 | + static_cast<const uint8_t*>(input), |
| 177 | + static_cast<uint8_t*>(output), |
| 178 | + expert_offsets, |
| 179 | + max_M, |
| 180 | + row_bytes |
| 181 | + ); |
| 182 | +} |
| 183 | + |
| 184 | +extern "C" void cmoe_gather_bf16( |
| 185 | + const void* input, |
| 186 | + void* output, |
| 187 | + const int* expert_offsets, |
| 188 | + int max_M, |
| 189 | + int N, |
| 190 | + int num_experts, |
| 191 | + cudaStream_t stream |
| 192 | +) { |
| 193 | + int row_bytes = N * 2; // bf16: 2 bytes per element |
| 194 | + |
| 195 | + dim3 grid(num_experts); |
| 196 | + dim3 block(256); |
| 197 | + |
| 198 | + kMoeGatherBF16<<<grid, block, 0, stream>>>( |
| 199 | + static_cast<const uint8_t*>(input), |
| 200 | + static_cast<uint8_t*>(output), |
| 201 | + expert_offsets, |
| 202 | + max_M, |
| 203 | + row_bytes |
| 204 | + ); |
| 205 | +} |
0 commit comments