Skip to content

vllm.model_executor.layers.fused_moe.moe_permute_unpermute

Classes:

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

MoEPermuteScratch dataclass

Reused metadata buffers for repeated grouped-MoE permutes.

Capacity follows the largest input seen, like the shared workspace: it grows during warmup and is fixed once the workspace manager is locked.

Methods:

  • reserve –

    Ensure capacity for expanded_rows (num_tokens * topk) rows.

reserve(expanded_rows)

Ensure capacity for expanded_rows (num_tokens * topk) rows.

get_moe_permute_scratch(*, num_experts, num_local_experts, device)

Share scratch across sequential layers in the current ubatch and lane.

Without a workspace manager, allocate new scratch for each call.

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 reusable metadata buffers. Otherwise they 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.

moe_prepare_scatter(topk_ids, expert_map, scratch)

Generate expert offsets and shared scatter/unpermute destination indices.

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.