feat: xllm MoE CUDA kernels — fused_topk + compute_index + combine

3 MoE kernel files adapted for corex:
  moe_fused_topk.cu: LOG(FATAL)→TORCH_CHECK, +torch/extension.h
  moe_compute_index.cu: CHECK_LE→TORCH_CHECK, uses cub::BlockScan (corex CUB)
  moe_combine.cu: fixed duplicate include, +torch/extension.h

New pybind binding: xllm_moe_bind.cpp
  → moe_fused_topk(gating, topk, renormalize, bias, scoring_func)
  → moe_compute_index(expert_id, num_experts)
  → moe_combine_result(gemm2, weights, N, topk)

AST verification added for all 3 functions
This commit is contained in:
claude
2026-08-14 11:23:49 +00:00
parent 49cd7def89
commit a50adefdfc
6 changed files with 91 additions and 6 deletions

View File

@@ -28,7 +28,7 @@ limitations under the License.
#include <c10/cuda/CUDAGuard.h>
#include "device_utils.cuh"
#include "device_utils.cuh"
#include <torch/extension.h>
namespace xllm::kernel::cuda {

View File

@@ -24,6 +24,7 @@ limitations under the License.
// expert_offsets = exclusive prefix sum of counts (scratch, reused)
#include <c10/cuda/CUDAGuard.h>
#include <torch/extension.h>
#include <cub/block/block_scan.cuh>
@@ -115,7 +116,7 @@ std::tuple<torch::Tensor, torch::Tensor, torch::Tensor> moe_compute_index(
auto stream = at::cuda::getCurrentCUDAStream();
int64_t N = expert_id.numel();
int32_t E = static_cast<int32_t>(num_experts);
CHECK_LE(E, kMoeIndexBlock) << "num_experts cannot exceed " << kMoeIndexBlock;
TORCH_CHECK(E <= kMoeIndexBlock, "num_experts cannot exceed ", kMoeIndexBlock);
auto expert_id_i32 = expert_id.to(torch::kInt32).contiguous();
auto opt_i32 = expert_id_i32.options();

View File

@@ -16,6 +16,7 @@ limitations under the License.
#include "kernels/dcu/dcu_ops_api.h"
#else
#include "device_utils.cuh"
#include <torch/extension.h>
#endif
#include "moe_topk_sigmoid_kernels.cuh"
#include "moe_topk_softmax_kernels.cuh"
@@ -49,8 +50,7 @@ std::tuple<torch::Tensor, torch::Tensor> moe_fused_topk(
topk_sigmoid(
topk_weights, topk_ids, gating_output, renormalize, correction_bias);
} else {
LOG(FATAL) << "Unsupported scoring function for moe topk: " << scoring_func
<< "only softmax and sigmoid are supported";
TORCH_CHECK(false, "Unsupported scoring function: ", scoring_func);
}
return std::make_tuple(topk_weights, topk_ids);