vllm.model_executor.layers.fused_moe.expert_map_manager
¶
Expert Map Manager for MoE layers.
This module contains the ExpertMapManager class which manages expert ID mappings and placement strategies for Expert Parallelism in MoE models.
Classes:
-
ExpertMapManager–Manages expert ID mappings and placement for Expert Parallelism.
Functions:
-
determine_expert_map–Calculates how many experts should be assigned to each rank for EP and
ExpertMapManager
¶
Manages expert ID mappings and placement for Expert Parallelism.
Responsibilities: - Calculate local vs global expert counts - Map between global, local, and physical expert IDs - Manage placement strategies (linear, round_robin) - Maintain routing tables for round-robin placement - Support dynamic reconfiguration of EP topology
When expert_map is required: - Expert Parallelism (EP) is enabled, i.e., when ep_size > 1 - EP disabled (ep_size == 1): expert_map is None * All experts are local to the current rank * No mapping is needed - EP enabled (ep_size > 1): expert_map is created * Maps global expert IDs to local expert IDs * Shape: (global_num_experts,) * Contains the local expert index for experts on this rank, -1 for experts on other ranks * Used by kernels to handle distributed expert execution - Kernel support varies: * Supports expert_map: fused_moe, fused_marlin_moe, fused_humming_moe, rocm_aiter_fused_moe, deep_gemm_moe, xpu_moe, gpt_oss_triton_kernels_moe * Does not support: flashinfer_cutlass_moe, fused_batched_moe, most cutlass_moe variants, trtllm_* kernels * When kernel doesn't support expert_map: The modular kernel method sets expert_map=None even if EP is enabled
Methods:
-
__init__–Initialize expert map manager.
-
get_compressed_map_string–Get compressed string representation of expert map for logging.
-
is_local_expert–Check if expert is assigned to this rank.
-
map_global_to_local–Map global expert ID to local expert ID.
-
update–Update expert mappings for new EP configuration.
Attributes:
-
expert_map(Tensor | None) –Mapping from global expert ID to local expert ID.
-
expert_mask(Tensor | None) –Expert mask for AITER fusion (ROCm-specific).
-
placement_strategy(ExpertPlacementStrategy) –Expert placement strategy ('linear' or 'round_robin').
-
routing_tables(tuple[Tensor, Tensor, Tensor] | None) –Routing tables for round-robin placement.
expert_map
property
¶
Mapping from global expert ID to local expert ID.
Returns tensor of shape (global_num_experts,) where: - expert_map[global_id] = local_id if expert is on this rank - expert_map[global_id] = -1 if expert is not on this rank
Returns None if EP is not enabled (ep_size == 1).
expert_mask
property
¶
Expert mask for AITER fusion (ROCm-specific).
Returns tensor of shape (global_num_experts + num_fused_shared + 1,) where 1 indicates expert is on this rank, 0 otherwise.
placement_strategy
property
¶
Expert placement strategy ('linear' or 'round_robin').
routing_tables
property
¶
Routing tables for round-robin placement.
Returns (global_to_physical, physical_to_global, local_to_global) or None if not using round-robin or tables not needed.
__init__(max_num_batched_tokens, top_k, global_num_experts, num_redundant_experts, num_expert_group, moe_parallel_config, placement_strategy, enable_eplb, num_fused_shared_experts=0, rocm_aiter_enabled=False)
¶
Initialize expert map manager.
Parameters:
-
(max_num_batched_tokens¶int) –Maximum number of batched tokens per step
-
(top_k¶int) –Number of experts each token is routed to
-
(global_num_experts¶int) –Total number of experts across all ranks
-
(num_redundant_experts¶int) –Number of redundant expert replicas (EPLB)
-
(num_expert_group¶int | None) –Number of expert groups, when grouped routing is used
-
(moe_parallel_config¶FusedMoEParallelConfig) –MoE parallel configuration (contains ep_size, ep_rank, backend flags)
-
(placement_strategy¶ExpertPlacementStrategy) –Strategy for placing experts ('linear' or 'round_robin')
-
(num_fused_shared_experts¶int, default:0) –Number of fused shared experts (for AITER)
-
(rocm_aiter_enabled¶bool, default:False) –Whether ROCm AITER fusion is enabled
-
(enable_eplb¶bool) –Whether expert-parallel load balancing is enabled
_calculate_expert_maps()
¶
Calculate expert mappings based on placement strategy.
_determine_placement_strategy(requested_strategy)
¶
Determine effective placement strategy based on config.
_init_round_robin_expert_routing_tables()
¶
Build routing tables for round-robin placement.
_init_routing_tables()
¶
Ensure routing tables are initialized if needed for round-robin.
This is a public method that can be called to explicitly initialize routing tables. It's safe to call multiple times (idempotent).
get_compressed_map_string()
¶
Get compressed string representation of expert map for logging.
Returns string mapping local to global expert IDs.
is_local_expert(global_id)
¶
Check if expert is assigned to this rank.
map_global_to_local(global_id)
¶
Map global expert ID to local expert ID.
Parameters:
Returns:
-
int–Local expert ID (0 to local_num_experts - 1)
Raises:
-
ValueError–If expert is not on this rank
update(moe_parallel_config, global_num_experts)
¶
Update expert mappings for new EP configuration.
Used during dynamic reconfiguration (e.g., elastic scaling).
Parameters:
-
(global_num_experts¶int) –New total number of experts across all ranks
-
(moe_parallel_config¶FusedMoEParallelConfig) –New MoE parallel configuration (contains ep_size, ep_rank, backend flags)
determine_expert_map(ep_size, ep_rank, global_num_experts, expert_placement_strategy='linear', num_fused_shared_experts=0, return_expert_mask=False)
¶
Calculates how many experts should be assigned to each rank for EP and creates a mapping from global to local expert index. Experts are distributed evenly across ranks. Any remaining are assigned to the last rank.
Parameters:
-
(ep_size¶int) –The size of the expert parallel group
-
(ep_rank¶int) –The rank of the current process in the expert parallel group
-
(global_num_experts¶int) –The total number of experts in the model.
-
(expert_placement_strategy¶ExpertPlacementStrategy, default:'linear') –The expert placement strategy.
-
(num_fused_shared_experts¶int, default:0) –Number of fused shared experts (for AITER)
-
(return_expert_mask¶bool, default:False) –Whether to return expert mask for AITER
Returns:
-
tuple[int, Tensor | None, Tensor | None]–tuple[int, Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple containing: - local_num_experts (int): The number of experts assigned to the current rank. - expert_map (Optional[torch.Tensor]): A tensor of shape (global_num_experts,) mapping from global to local index. Contains -1 for experts not assigned to the current rank. Returns None if ep_size is 1. - expert_mask (Optional[torch.Tensor]): A tensor of shape (global_num_experts + num_fused_shared_experts + 1,) containing 1 for experts assigned to the current rank and 0 for sentinel. Returns None if ep_size is 1. Used only when AITER MOE is enabled.