2024-08-02 16:51:58 -04:00
|
|
|
#include "core/registration.h"
|
2024-06-09 16:23:30 -04:00
|
|
|
#include "moe_ops.h"
|
|
|
|
|
|
|
|
TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, m) {
|
|
|
|
// Apply topk softmax to the gating outputs.
|
|
|
|
m.def(
|
|
|
|
"topk_softmax(Tensor! topk_weights, Tensor! topk_indices, Tensor! "
|
|
|
|
"token_expert_indices, Tensor gating_output) -> ()");
|
|
|
|
m.impl("topk_softmax", torch::kCUDA, &topk_softmax);
|
2024-08-27 18:07:09 -04:00
|
|
|
|
2024-10-24 17:37:52 -05:00
|
|
|
// Calculate the result of moe by summing up the partial results
|
|
|
|
// from all selected experts.
|
|
|
|
m.def("moe_sum(Tensor! input, Tensor output) -> ()");
|
|
|
|
m.impl("moe_sum", torch::kCUDA, &moe_sum);
|
|
|
|
|
|
|
|
// Aligning the number of tokens to be processed by each expert such
|
|
|
|
// that it is divisible by the block size.
|
|
|
|
m.def(
|
|
|
|
"moe_align_block_size(Tensor topk_ids, int num_experts,"
|
|
|
|
" int block_size, Tensor! sorted_token_ids,"
|
|
|
|
" Tensor! experts_ids,"
|
|
|
|
" Tensor! num_tokens_post_pad) -> ()");
|
|
|
|
m.impl("moe_align_block_size", torch::kCUDA, &moe_align_block_size);
|
|
|
|
|
2025-02-02 21:09:50 -08:00
|
|
|
// temporarily adapted from
|
|
|
|
// https://github.com/sgl-project/sglang/commit/ded9fcd09a43d5e7d5bb31a2bc3e9fc21bf65d2a
|
|
|
|
m.def(
|
|
|
|
"sgl_moe_align_block_size(Tensor topk_ids, int num_experts,"
|
|
|
|
" int block_size, Tensor! sorted_token_ids,"
|
|
|
|
" Tensor! experts_ids,"
|
|
|
|
" Tensor! num_tokens_post_pad) -> ()");
|
|
|
|
m.impl("sgl_moe_align_block_size", torch::kCUDA, &sgl_moe_align_block_size);
|
|
|
|
|
2025-03-12 05:00:28 -07:00
|
|
|
#ifndef USE_ROCM
|
2025-03-11 08:12:40 +08:00
|
|
|
m.def(
|
|
|
|
"moe_wna16_gemm(Tensor input, Tensor! output, Tensor b_qweight, "
|
|
|
|
"Tensor b_scales, Tensor? b_qzeros, "
|
|
|
|
"Tensor? topk_weights, Tensor sorted_token_ids, "
|
|
|
|
"Tensor expert_ids, Tensor num_tokens_post_pad, "
|
|
|
|
"int top_k, int BLOCK_SIZE_M, int BLOCK_SIZE_N, int BLOCK_SIZE_K, "
|
|
|
|
"int bit) -> Tensor");
|
|
|
|
|
|
|
|
m.impl("moe_wna16_gemm", torch::kCUDA, &moe_wna16_gemm);
|
|
|
|
|
2024-08-27 18:07:09 -04:00
|
|
|
m.def(
|
|
|
|
"marlin_gemm_moe(Tensor! a, Tensor! b_q_weights, Tensor! sorted_ids, "
|
|
|
|
"Tensor! topk_weights, Tensor! topk_ids, Tensor! b_scales, Tensor! "
|
2024-10-04 20:34:44 +02:00
|
|
|
"b_zeros, Tensor! g_idx, Tensor! perm, Tensor! workspace, "
|
2024-10-17 15:08:34 -04:00
|
|
|
"int b_q_type, SymInt size_m, "
|
|
|
|
"SymInt size_n, SymInt size_k, bool is_k_full, int num_experts, int "
|
|
|
|
"topk, "
|
2024-09-16 17:47:19 +02:00
|
|
|
"int moe_block_size, bool replicate_input, bool apply_weights)"
|
|
|
|
" -> Tensor");
|
2024-10-03 22:55:25 -04:00
|
|
|
// conditionally compiled so impl registration is in source file
|
2024-08-27 18:07:09 -04:00
|
|
|
#endif
|
2024-06-09 16:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_EXTENSION(TORCH_EXTENSION_NAME)
|