|
| 1 | +#include <torch/extension.h> |
| 2 | +#include <ATen/cuda/CUDAContext.h> |
| 3 | + |
| 4 | +#include <ATen/ATen.h> |
| 5 | +#include <THC/THCAtomics.cuh> |
| 6 | + |
| 7 | +#include "cuda_compat.h" |
| 8 | +#include "dispatch_utils.h" |
| 9 | + |
| 10 | +const static size_t NUM_MAX_EXPERTS = 64; |
| 11 | +#define CEILDIV(x,y) (((x) + (y) - 1) / (y)) |
| 12 | + |
| 13 | +namespace vllm { |
| 14 | +template <typename scalar_t> |
| 15 | +__global__ void moe_align_block_size_kernel(scalar_t *__restrict__ topk_ids, |
| 16 | + int32_t *sorted_token_ids, |
| 17 | + int32_t *expert_ids, |
| 18 | + int32_t *total_tokens_post_pad, |
| 19 | + int32_t num_experts, |
| 20 | + int32_t block_size, |
| 21 | + size_t numel) { |
| 22 | + const size_t tokens_per_thread = CEILDIV(numel, blockDim.x); |
| 23 | + const size_t start_idx = threadIdx.x * tokens_per_thread; |
| 24 | + __shared__ int32_t tokens_cnts[NUM_MAX_EXPERTS + 1][NUM_MAX_EXPERTS]; |
| 25 | + __shared__ int32_t cumsum[NUM_MAX_EXPERTS + 1]; |
| 26 | + for (int i = 0; i < num_experts; ++i) { |
| 27 | + tokens_cnts[threadIdx.x + 1][i] = 0; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * In the first step we compute token_cnts[thread_index + 1][expert_index], |
| 32 | + * which counts how many tokens in the token shard of thread_index are assigned |
| 33 | + * to expert expert_index. |
| 34 | + */ |
| 35 | + for (int i = start_idx; i < numel && i < start_idx + tokens_per_thread; ++i) { |
| 36 | + ++tokens_cnts[threadIdx.x + 1][topk_ids[i]]; |
| 37 | + } |
| 38 | + |
| 39 | + __syncthreads(); |
| 40 | + |
| 41 | + // For each expert we accumulate the token counts from the different threads. |
| 42 | + tokens_cnts[0][threadIdx.x] = 0; |
| 43 | + for (int i = 1; i <= blockDim.x; ++i) { |
| 44 | + tokens_cnts[i][threadIdx.x] += tokens_cnts[i-1][threadIdx.x]; |
| 45 | + } |
| 46 | + |
| 47 | + __syncthreads(); |
| 48 | + |
| 49 | + // We accumulate the token counts of all experts in thread 0. |
| 50 | + if (threadIdx.x == 0) { |
| 51 | + cumsum[0] = 0; |
| 52 | + for (int i = 1; i <= num_experts; ++i) { |
| 53 | + cumsum[i] = cumsum[i-1] + CEILDIV(tokens_cnts[blockDim.x][i - 1], block_size) * block_size; |
| 54 | + } |
| 55 | + *total_tokens_post_pad = cumsum[num_experts]; |
| 56 | + } |
| 57 | + |
| 58 | + __syncthreads(); |
| 59 | + |
| 60 | + /** |
| 61 | + * For each expert, each thread processes the tokens of the corresponding blocks |
| 62 | + * and stores the corresponding expert_id for each block. |
| 63 | + */ |
| 64 | + for (int i = cumsum[threadIdx.x];i < cumsum[threadIdx.x + 1];i += block_size) { |
| 65 | + expert_ids[i / block_size] = threadIdx.x; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Each thread processes a token shard, calculating the index of each token after |
| 70 | + * sorting by expert number. Given the example topk_ids = [0,1,2,1,2,3,0,3,4] and |
| 71 | + * block_size = 4, then the output would be [0, 6, *, *, 1, 3, *, *, 2, 4, *, *, 5, 7, *, *, 8, *, *, *], |
| 72 | + * where * represents a padding value(preset in python). |
| 73 | + */ |
| 74 | + for (int i = start_idx; i < numel && i < start_idx + tokens_per_thread; ++i) { |
| 75 | + int32_t expert_id = topk_ids[i]; |
| 76 | + /** The cumsum[expert_id] stores the starting index of the tokens that the |
| 77 | + * expert with expert_id needs to process, and tokens_cnts[threadIdx.x][expert_id] |
| 78 | + * stores the indices of the tokens processed by the expert with expert_id within |
| 79 | + * the current thread's token shard. |
| 80 | + */ |
| 81 | + int32_t rank_post_pad = tokens_cnts[threadIdx.x][expert_id] + cumsum[expert_id]; |
| 82 | + sorted_token_ids[rank_post_pad] = i; |
| 83 | + ++tokens_cnts[threadIdx.x][expert_id]; |
| 84 | + } |
| 85 | +} |
| 86 | +} |
| 87 | + |
| 88 | +void moe_align_block_size( |
| 89 | + torch::Tensor topk_ids, |
| 90 | + int num_experts, |
| 91 | + int block_size, |
| 92 | + torch::Tensor sorted_token_ids, |
| 93 | + torch::Tensor experts_ids, |
| 94 | + torch::Tensor num_tokens_post_pad) { |
| 95 | + const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); |
| 96 | + assert(num_experts <= NUM_MAX_EXPERTS); |
| 97 | + VLLM_DISPATCH_INTEGRAL_TYPES( |
| 98 | + topk_ids.scalar_type(), "moe_alig_block_size_kernel", [&] { |
| 99 | + vllm::moe_align_block_size_kernel<scalar_t><<<1, num_experts, 0, stream>>>( |
| 100 | + topk_ids.data_ptr<scalar_t>(), |
| 101 | + sorted_token_ids.data_ptr<int32_t>(), |
| 102 | + experts_ids.data_ptr<int32_t>(), |
| 103 | + num_tokens_post_pad.data_ptr<int32_t>(), |
| 104 | + num_experts, |
| 105 | + block_size, |
| 106 | + topk_ids.numel()); |
| 107 | + }); |
| 108 | +} |
0 commit comments