vllm.model_executor.layers.fused_moe.moe_permute_unpermute
¶
Functions:
-
get_moe_permute_scratch–Share scratch across sequential layers in the current ubatch and lane.
-
moe_permute–This function expands and permutes activation to gather uncontinuous tokens
-
moe_prepare_scatter–Generate expert offsets and shared scatter/unpermute destination indices.
-
moe_unpermute–This function expands and permutes activation to gathering uncontinuous
get_moe_permute_scratch(*, max_num_tokens, topk, num_experts, num_local_experts, device, hidden_size=None, hidden_dtype=None)
¶
Share scratch across sequential layers in the current ubatch and lane.
Without a workspace manager, allocate new scratch for each call.
Source code in vllm/model_executor/layers/fused_moe/moe_permute_unpermute.py
moe_permute(hidden_states, a1q_scale, topk_ids, n_expert, n_local_expert=-1, expert_map=None, permuted_hidden_states=None, scratch=None)
¶
This function expands and permutes activation to gather uncontinuous tokens for each expert.
Parameters:
-
(hidden_states¶Tensor) –The input tensor to the MoE layer.
-
(a1q_scale¶Optional[Tensor]) –quant scale for hidden_states
-
(topk_ids¶Tensor) –topk expert route id for each token.
-
(n_expert¶int) –The number of expert.
-
(n_local_expert¶int, default:-1) –The number of expert in current EP rank.
-
(expert_map¶Optional[Tensor], default:None) –A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard.
-
(permuted_hidden_states¶Optional[Tensor], default:None) –Optional output tensor. If None, the output tensor will be created in this function.
-
(scratch¶Optional[MoEPermuteScratch], default:None) –Optional preallocated scratch buffers. Validated against hidden_states and topk_ids when given, otherwise the buffers are allocated in this function.
Returns: - permuted_hidden_states (torch.Tensor): permuted activation. - a1q_scale (Optional[torch.Tensor]): permuted quant scale for hidden_states if original scale not per-tensor scaling - expert_first_token_offset (torch.Tensor): offset of the first token of each expert for standard grouped gemm. - inv_permuted_idx (torch.Tensor): idx map for moe_unpermute. - permuted_idx (torch.Tensor): idx map from hidden to permuted_hidden.
Source code in vllm/model_executor/layers/fused_moe/moe_permute_unpermute.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
moe_prepare_scatter(topk_ids, expert_map, scratch)
¶
Generate expert offsets and shared scatter/unpermute destination indices.
Source code in vllm/model_executor/layers/fused_moe/moe_permute_unpermute.py
moe_unpermute(out, permuted_hidden_states, topk_weights, inv_permuted_idx, expert_first_token_offset=None)
¶
This function expands and permutes activation to gathering uncontinuous tokens for each expert.
Parameters:
-
(out¶Tensor) –output tensor
-
(permuted_hidden_states¶Tensor) –permuted activation.
-
(topk_weights¶Tensor) –topk expert route weight for each token.
-
(inv_permuted_idx¶Tensor) –row idx map for moe_unpermute.
-
(expert_first_token_offset¶Optional[Tensor], default:None) –offset of the first token of each expert for grouped gemm.
- hidden_states (torch.Tensor): The reduced and unpermuted activation tensor.