main
cpp 166 lines 6.93 KB
Raw
1 #include "core/registration.h"
2 #include "moe_ops.h"
3
4 #include <torch/csrc/stable/library.h>
5
6 STABLE_TORCH_LIBRARY_FRAGMENT(_moe_C, m) {
7 // Apply topk softmax to the gating outputs.
8 m.def(
9 "topk_softmax(Tensor! topk_weights, Tensor! topk_indices, Tensor! "
10 "token_expert_indices, Tensor gating_output, bool renormalize, Tensor? "
11 "bias, Tensor? is_padding) -> ()");
12
13 // Apply topk sigmoid to the gating outputs.
14 m.def(
15 "topk_sigmoid(Tensor! topk_weights, Tensor! topk_indices, Tensor! "
16 "token_expert_indices, Tensor gating_output, bool renormalize, "
17 "Tensor? bias, float routed_scaling_factor, Tensor? is_padding) -> ()");
18
19 m.def(
20 "topk_softplus_sqrt(Tensor! topk_weights, Tensor! topk_indices, Tensor! "
21 "token_expert_indices, Tensor gating_output, bool renormalize, float "
22 "routed_scaling_factor, Tensor? "
23 "bias, Tensor? input_ids, Tensor? tid2eid, Tensor? is_padding, "
24 "Tensor? bias_vl=None, int image_sentinel_lo=0) -> ()");
25
26 // Calculate the result of moe by summing up the partial results
27 // from all selected experts. topk_ids/expert_map are optional and, when
28 // both given, enable pad-aware reduce that skips (token, expert)
29 // slots that were never actually computed (unrouted, or routed to an
30 // expert not owned by this rank under expert parallelism).
31 m.def(
32 "moe_sum(Tensor input, Tensor! output, Tensor? topk_ids=None, "
33 "Tensor? expert_map=None) -> ()");
34
35 // Aligning the number of tokens to be processed by each expert such
36 // that it is divisible by the block size.
37 m.def(
38 "moe_align_block_size(Tensor topk_ids, int num_experts,"
39 " int block_size, Tensor! sorted_token_ids,"
40 " Tensor! experts_ids,"
41 " Tensor! num_tokens_post_pad,"
42 " Tensor? maybe_expert_map,"
43 " Tensor(a!)? scatter_idx=None) -> ()");
44
45 // Aligning the number of tokens to be processed by each expert such
46 // that it is divisible by the block size, but for the batched case.
47 m.def(
48 "batched_moe_align_block_size(int max_tokens_per_batch,"
49 " int block_size, Tensor expert_num_tokens,"
50 " Tensor! sorted_token_ids,"
51 " Tensor! experts_ids,"
52 " Tensor! num_tokens_post_pad) -> ()");
53
54 // Aligning the number of tokens to be processed by each expert such
55 // that it is divisible by the block size.
56 m.def(
57 "moe_lora_align_block_size(Tensor topk_ids,"
58 " Tensor token_lora_mapping,"
59 " int num_experts,"
60 " int block_size, int max_loras, "
61 " int max_num_tokens_padded, "
62 " int max_num_m_blocks, "
63 " Tensor !sorted_token_ids,"
64 " Tensor !experts_ids,"
65 " Tensor !num_tokens_post_pad,"
66 " Tensor !adapter_enabled,"
67 " Tensor !lora_ids,"
68 " Tensor? maybe_expert_map) -> () ");
69 #ifndef USE_ROCM
70 m.def(
71 "moe_wna16_gemm(Tensor input, Tensor! output, Tensor b_qweight, "
72 "Tensor b_scales, Tensor? b_qzeros, "
73 "Tensor? topk_weights, Tensor sorted_token_ids, "
74 "Tensor expert_ids, Tensor num_tokens_post_pad, "
75 "int top_k, int BLOCK_SIZE_M, int BLOCK_SIZE_N, int BLOCK_SIZE_K, "
76 "int bit) -> Tensor");
77
78 m.def(
79 "moe_wna16_marlin_gemm(Tensor! a, Tensor? c_or_none,"
80 "Tensor! b_q_weight, Tensor? b_bias_or_none,"
81 "Tensor! b_scales, Tensor? a_scales, Tensor? global_scale, Tensor? "
82 "b_zeros_or_none,"
83 "Tensor! workspace,"
84 "Tensor sorted_token_ids,"
85 "Tensor! expert_ids, Tensor! num_tokens_past_padded,"
86 "Tensor! topk_weights, int moe_block_size, int top_k, "
87 "bool mul_topk_weights, int b_type_id,"
88 "int size_m, int size_n, int size_k, bool use_atomic_add,"
89 "bool use_fp32_reduce, bool is_zp_float,"
90 "int thread_k, int thread_n, int blocks_per_sm) -> Tensor");
91
92 m.def(
93 "moe_permute(Tensor input, Tensor topk_ids,"
94 "Tensor token_expert_indices, Tensor? expert_map, int n_expert,"
95 "int n_local_expert,"
96 "int topk, Tensor! permuted_input, Tensor! "
97 "expert_first_token_offset, Tensor! inv_permuted_idx, Tensor! "
98 "permuted_idx)->()");
99
100 m.def(
101 "moe_permute_with_scratch(Tensor input, Tensor topk_ids,"
102 "Tensor token_expert_indices, Tensor? expert_map, int n_expert,"
103 "int n_local_expert,"
104 "int topk, Tensor! permuted_input, Tensor! "
105 "expert_first_token_offset, Tensor! inv_permuted_idx, Tensor! "
106 "permuted_idx, Tensor! sort_workspace, Tensor! permuted_experts_id, "
107 "Tensor! sorted_row_idx, Tensor! topk_ids_for_sort)->()");
108
109 m.def(
110 "moe_prepare_scatter(Tensor topk_ids, Tensor token_expert_indices, "
111 "Tensor? expert_map, int n_expert, int n_local_expert, "
112 "Tensor! expert_first_token_offset, Tensor! scatter_idx, "
113 "Tensor! sort_workspace, Tensor! sorted_experts, Tensor! sorted_rows, "
114 "Tensor! topk_ids_for_sort)->()");
115
116 m.def(
117 "moe_unpermute(Tensor permuted_hidden_states, Tensor topk_weights,"
118 "Tensor inv_permuted_idx, Tensor? expert_first_token_offset, "
119 "int topk, Tensor! hidden_states)->()");
120
121 m.def("moe_permute_unpermute_supported() -> bool");
122 m.def(
123 "moe_permute_sort_workspace_size(int num_expanded_rows, int n_expert) -> "
124 "int");
125
126 // Row shuffle for MoE
127 m.def(
128 "shuffle_rows(Tensor input_tensor, Tensor dst2src_map, Tensor! "
129 "output_tensor) -> ()");
130
131 // Apply grouped topk routing to select experts.
132 m.def(
133 "grouped_topk(Tensor scores, int n_group, int "
134 "topk_group, int topk, bool renormalize, float "
135 "routed_scaling_factor, Tensor bias, int scoring_func) -> (Tensor, "
136 "Tensor)");
137 #endif
138 }
139
140 STABLE_TORCH_LIBRARY_IMPL(_moe_C, CUDA, m) {
141 m.impl("topk_softmax", TORCH_BOX(&topk_softmax));
142 m.impl("topk_sigmoid", TORCH_BOX(&topk_sigmoid));
143 m.impl("topk_softplus_sqrt", TORCH_BOX(&topk_softplus_sqrt));
144 m.impl("moe_sum", TORCH_BOX(&moe_sum));
145 m.impl("moe_align_block_size", TORCH_BOX(&moe_align_block_size));
146 m.impl("batched_moe_align_block_size",
147 TORCH_BOX(&batched_moe_align_block_size));
148 m.impl("moe_lora_align_block_size", TORCH_BOX(&moe_lora_align_block_size));
149 #ifndef USE_ROCM
150 m.impl("moe_wna16_gemm", TORCH_BOX(&moe_wna16_gemm));
151 m.impl("shuffle_rows", TORCH_BOX(&shuffle_rows));
152 m.impl("grouped_topk", TORCH_BOX(&grouped_topk));
153 #endif
154 }
155
156 #ifndef USE_ROCM
157 // Primitive-only ops have no tensor to dispatch on.
158 STABLE_TORCH_LIBRARY_IMPL(_moe_C, CompositeExplicitAutograd, m) {
159 m.impl("moe_permute_unpermute_supported",
160 TORCH_BOX(&moe_permute_unpermute_supported));
161 m.impl("moe_permute_sort_workspace_size",
162 TORCH_BOX(&moe_permute_sort_workspace_size));
163 }
164 #endif
165
166 REGISTER_EXTENSION(_moe_C_stable_libtorch)