Skip to content

[MoE Refactor] Move expert map related code into ExpertMapManager class#41046

Merged
robertgshaw2-redhat merged 29 commits into
vllm-project:mainfrom
neuralmagic:expert-map-manager
May 12, 2026
Merged

[MoE Refactor] Move expert map related code into ExpertMapManager class#41046
robertgshaw2-redhat merged 29 commits into
vllm-project:mainfrom
neuralmagic:expert-map-manager

Conversation

@bnellnm
Copy link
Copy Markdown
Collaborator

@bnellnm bnellnm commented Apr 27, 2026

Purpose

Create ExpertMapManager class to handle all expert map related functionality in the FusedMoE layer.

Test Plan

CI

Test Result

cc @yzong-rh


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

bnellnm added 4 commits April 27, 2026 20:01
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the ExpertMapManager class to centralize and manage expert ID mappings, placement strategies, and routing tables for MoE layers, refactoring this logic out of layer.py. The feedback identifies critical state management issues in the update and update_expert_map methods, specifically regarding the synchronization of local expert counts when shared experts are present and the need to keep configuration objects in sync during dynamic updates. Additionally, the round-robin routing table logic requires correction to include shared experts to prevent potential out-of-bounds errors in kernels.

Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py
Comment thread vllm/model_executor/layers/fused_moe/layer.py
Signed-off-by: Bill Nell <bnell@redhat.com>
bnellnm and others added 2 commits April 29, 2026 19:53
Signed-off-by: Bill Nell <bnell@redhat.com>
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/layer.py
Comment thread vllm/model_executor/layers/fused_moe/layer.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/layer.py
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
bnellnm added 3 commits May 6, 2026 19:01
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
@bnellnm bnellnm requested a review from tjtanaa as a code owner May 6, 2026 19:53
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 6, 2026

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @bnellnm.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label May 6, 2026
bnellnm added 2 commits May 6, 2026 19:54
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
@mergify mergify Bot removed the needs-rebase label May 6, 2026
Copy link
Copy Markdown
Contributor

@yzong-rh yzong-rh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three remaining concerns with ExpertMapManager

  1. self.device might throw an error if expert_maps are not initialized (when ep_size == 1 for example).
  2. Usage of self.device in _ensure_routing_tables_initialized is redundant thanks to with self.device:.
  3. In order to ensure idempotency _ensure_routing_tables_initialized never override already set routing_tables. This could be problematic in update.

Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Comment thread vllm/model_executor/layers/fused_moe/layer.py Outdated
Signed-off-by: Bill Nell <bnell@redhat.com>
Copy link
Copy Markdown
Contributor

@yzong-rh yzong-rh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than that LGTM. Thanks!

Comment thread vllm/model_executor/layers/fused_moe/expert_map_manager.py Outdated
Signed-off-by: Bill Nell <bnell@redhat.com>
Comment thread vllm/model_executor/layers/fused_moe/layer.py Outdated
return expert_placement_strategy


class ExpertMapManager:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what is confusing about ExpertMap is that it is used ** sometimes **

It would help a lot for this class to explain when and when it is not used

@varun-sundar-rabindranath might have the best view of this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC it's used for EP to map from global<->local expert ids. Not all backends/experts require it though. Some all2alls handle it internally and pass that along to the experts.

Signed-off-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 11, 2026

Hi @bnellnm, the pre-commit checks have failed. Please run:

uv pip install pre-commit>=4.5.1
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy failing?
mypy is run differently in CI. If the failure is related to this check, please use the following command to run it locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10

@robertgshaw2-redhat robertgshaw2-redhat added the ready ONLY add when PR is ready to merge/full CI is needed label May 11, 2026
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 11, 2026

Hi @bnellnm, the pre-commit checks have failed. Please run:

uv pip install pre-commit>=4.5.1
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy failing?
mypy is run differently in CI. If the failure is related to this check, please use the following command to run it locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10

robertgshaw2-redhat and others added 6 commits May 11, 2026 11:40
Signed-off-by: Robert Shaw <robertgshaw2@gmail.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Bill Nell <bnell@redhat.com>
@robertgshaw2-redhat robertgshaw2-redhat merged commit 206eaed into vllm-project:main May 12, 2026
84 of 85 checks passed
weifang231 pushed a commit to weifang231/eb-vllm that referenced this pull request May 13, 2026
…ss (vllm-project#41046)

Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Signed-off-by: Robert Shaw <robertgshaw2@gmail.com>
Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Co-authored-by: Robert Shaw <robertgshaw2@gmail.com>
wangxiyuan pushed a commit to vllm-project/vllm-ascend that referenced this pull request May 14, 2026
### What this PR does / why we need it?
1. fix vllm-project/vllm#33322
overwrite `gpu_modelrunner.sync_and_gather_intermediate_tensors`, for
the sceniro `pp+sp+tp`, skip scatter the residual for ascend

2. vllm-project/vllm#35520
Adapted to the modifications of `ModelRunner v2` for hybrid attn in
interface level, .
Todo: Added support for Mamba in ModelRunner in Ascend. any pull_request
is welcome

3. vllm-project/vllm#40711

4. vllm-project/vllm#42121

5. vllm-project/vllm#41706

6. vllm-project/vllm#39917
Disable `async_schedule` when `enable_return_routed_experts=True`
7. vllm-project/vllm#41046
8. vllm-project/vllm#41055
9. vllm-project/vllm#41035
10. vllm-project/vllm#42434
### Does this PR introduce _any_ user-facing change?

### How was this patch tested?

- vLLM version: v0.20.1
- vLLM main:
vllm-project/vllm@c7aa186

---------

Signed-off-by: wangli <wangli858794774@gmail.com>
Tian-Fantasea pushed a commit to Tian-Fantasea/vllm-ascend that referenced this pull request May 19, 2026
### What this PR does / why we need it?
1. fix vllm-project/vllm#33322
overwrite `gpu_modelrunner.sync_and_gather_intermediate_tensors`, for
the sceniro `pp+sp+tp`, skip scatter the residual for ascend

2. vllm-project/vllm#35520
Adapted to the modifications of `ModelRunner v2` for hybrid attn in
interface level, .
Todo: Added support for Mamba in ModelRunner in Ascend. any pull_request
is welcome

3. vllm-project/vllm#40711

4. vllm-project/vllm#42121

5. vllm-project/vllm#41706

6. vllm-project/vllm#39917
Disable `async_schedule` when `enable_return_routed_experts=True`
7. vllm-project/vllm#41046
8. vllm-project/vllm#41055
9. vllm-project/vllm#41035
10. vllm-project/vllm#42434
### Does this PR introduce _any_ user-facing change?

### How was this patch tested?

- vLLM version: v0.20.1
- vLLM main:
vllm-project/vllm@c7aa186

---------

Signed-off-by: wangli <wangli858794774@gmail.com>
shaopeng-666 pushed a commit to shaopeng-666/vllm-ascend that referenced this pull request May 19, 2026
### What this PR does / why we need it?
1. fix vllm-project/vllm#33322
overwrite `gpu_modelrunner.sync_and_gather_intermediate_tensors`, for
the sceniro `pp+sp+tp`, skip scatter the residual for ascend

2. vllm-project/vllm#35520
Adapted to the modifications of `ModelRunner v2` for hybrid attn in
interface level, .
Todo: Added support for Mamba in ModelRunner in Ascend. any pull_request
is welcome

3. vllm-project/vllm#40711

4. vllm-project/vllm#42121

5. vllm-project/vllm#41706

6. vllm-project/vllm#39917
Disable `async_schedule` when `enable_return_routed_experts=True`
7. vllm-project/vllm#41046
8. vllm-project/vllm#41055
9. vllm-project/vllm#41035
10. vllm-project/vllm#42434
### Does this PR introduce _any_ user-facing change?

### How was this patch tested?

- vLLM version: v0.20.1
- vLLM main:
vllm-project/vllm@c7aa186

---------

Signed-off-by: wangli <wangli858794774@gmail.com>
Signed-off-by: 李少鹏 <lishaopeng21@huawei.com>
mfylcek pushed a commit to mfylcek/vllm that referenced this pull request May 19, 2026
…ss (vllm-project#41046)

Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Signed-off-by: Robert Shaw <robertgshaw2@gmail.com>
Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Co-authored-by: Robert Shaw <robertgshaw2@gmail.com>
jhu960213 pushed a commit to jhu960213/vllm that referenced this pull request May 20, 2026
…ss (vllm-project#41046)

Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Signed-off-by: Robert Shaw <robertgshaw2@gmail.com>
Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Co-authored-by: Robert Shaw <robertgshaw2@gmail.com>
h1t35h pushed a commit to h1t35h/vllm that referenced this pull request May 21, 2026
…ss (vllm-project#41046)

Signed-off-by: Bill Nell <bnell@redhat.com>
Signed-off-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Signed-off-by: Robert Shaw <robertgshaw2@gmail.com>
Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Co-authored-by: Robert Shaw <robertgshaw2@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants