Skip to content

vllm.model_executor.layers.fused_moe.experts.marlin_moe

Fused MoE utilities for GPTQ.

Classes:

Functions:

BatchedMarlinExperts

Bases: MarlinExpertsBase

Batched Marlin-based fused MoE expert implementation.

MarlinExperts

Bases: LoRAExpertsMixin, MarlinExpertsBase

Marlin-based fused MoE expert implementation.

batched_fused_marlin_moe(hidden_states, expert_num_tokens, w1, w2, bias1, bias2, w1_scale, w2_scale, quant_type_id, apply_router_weight_on_input=False, global_num_experts=-1, activation=MoEActivation.SILU, expert_map=None, global_scale1=None, global_scale2=None, w1_zeros=None, w2_zeros=None, workspace=None, intermediate_cache13=None, intermediate_cache2=None, output=None, activation_func=None, activation_config=None)

This function massages the inputs so the batched hidden_states can be presented as a 2D contiguous tensor that could be used with _fused_marlin_moe.

Note that both batched_fused_marlin_moe and fused_marlin_moe ultimately use ops.moe_wna16_marlin_gemm for the gemm operation and ops.moe_mna16_marlin_gemm supports only 2D contiguous hidden_states. Note that the moe_align_block_size function indicates, - What rows of the A matrix (hidden_states) to access during the matmul, via sorted_ids output. - What expert_id to use for each block matmul, via expert_ids output.

In the batched version, the tokens are already grouped/batched by experts they subscribe to. Due to this, we can represent the batched hidden_states tensor of shape [B, MAX_TOKENS_PER_BATCH, K] as a 2D tensor of shape, [B * MAX_TOKENS_PER_BATCH, K]. We may treat this a 2D contiguous tensor with topk=1 as each token (row in the tensor) subscribes to exactly one expert_id (which is the batch_id). With the expert_num_tokens tensor, that indicates how many tokens are actually valid in each batch, the batched_moe_align_block_size function constructs the sorted_ids and expert_ids tensors, so only relevant/valid rows of A (hidden_states) are accessed and are processed with the correct expert_ids.

fused_marlin_moe(hidden_states, w1, w2, bias1, bias2, w1_scale, w2_scale, topk_weights, topk_ids, quant_type_id, apply_router_weight_on_input=False, global_num_experts=-1, activation=MoEActivation.SILU, activation_func=None, moe_sum=None, expert_map=None, global_scale1=None, global_scale2=None, w1_zeros=None, w2_zeros=None, workspace=None, intermediate_cache13=None, intermediate_cache2=None, output=None, activation_config=None, mul_topk_weights_in_gemm2=True)

This function computes a Mixture of Experts (MoE) layer using two sets of weights, w1 and w2, and top-k gating mechanism.

Parameters:

  • hidden_states

    (Tensor) –

    The input tensor to the MoE layer.

  • w1

    (Tensor) –

    The first set of expert weights.

  • w2

    (Tensor) –

    The second set of expert weights.

  • bias1

    (Tensor | None) –

    Optional bias for the first GEMM.

  • bias2

    (Tensor | None) –

    Optional bias for the second GEMM.

  • w1_scale

    (Tensor) –

    Scale to be used for w1.

  • w2_scale

    (Tensor) –

    Scale to be used for w2.

  • topk_weights

    (Tensor) –

    Top-k weights.

  • topk_ids

    (Tensor) –

    Indices of topk-k elements.

  • quant_type_id

    (int) –

    Id of the ScalarType the expert weights are quantized to. Determines the 4- or 8-bit Marlin kernel path.

  • apply_router_weight_on_input

    (bool, default: False ) –

    Apply the topk weights to the input rather than the output. Only valid when topk is 1.

  • global_num_experts

    (int, default: -1 ) –

    Total number of experts across all expert parallel shards. -1 means the local expert count.

  • activation

    (MoEActivation, default: SILU ) –

    Activation applied between the two GEMMs.

  • activation_func

    (Callable | None, default: None ) –

    Override for the activation implementation.

  • moe_sum

    (Callable | None, default: None ) –

    Override for the final reduction over the topk dimension. Defaults to ops.moe_sum / torch.sum.

  • expert_map

    (Tensor | None, default: None ) –

    Maps global expert indices to the local expert space of this expert parallel shard.

  • global_scale1

    (Tensor | None, default: None ) –

    NVFP4 global weight scale for w1.

  • global_scale2

    (Tensor | None, default: None ) –

    NVFP4 global weight scale for w2.

  • w1_zeros

    (Tensor | None, default: None ) –

    Optional zero points to be used for w1.

  • w2_zeros

    (Tensor | None, default: None ) –

    Optional zero points to be used for w2.

  • workspace

    (Tensor | None, default: None ) –

    Scratch buffer for the Marlin kernels.

  • intermediate_cache13

    (Tensor | None, default: None ) –

    Scratch buffer for the first and third intermediate activations.

  • intermediate_cache2

    (Tensor | None, default: None ) –

    Scratch buffer for the second intermediate activation.

  • output

    (Tensor | None, default: None ) –

    Tensor to write the result into. A new tensor is allocated when omitted.

  • activation_config

    (ApplyMoEActivationConfig | None, default: None ) –

    Extra configuration forwarded to the activation.

  • mul_topk_weights_in_gemm2

    (bool, default: True ) –

    Apply top-k weights in GEMM2.

Returns:

  • Tensor –

    torch.Tensor: The output tensor after applying the MoE layer.