Skip to content

[WIP]: Support reuse routed experts on eviction#4599

Open
RunningLeon wants to merge 1 commit into
InternLM:mainfrom
RunningLeon:reuse-experts
Open

[WIP]: Support reuse routed experts on eviction#4599
RunningLeon wants to merge 1 commit into
InternLM:mainfrom
RunningLeon:reuse-experts

Conversation

@RunningLeon
Copy link
Copy Markdown
Collaborator

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily receiving feedbacks. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Please describe the motivation of this PR and the goal you want to achieve through this PR.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  3. If the modification has a dependency on downstream projects of a newer version, this PR should be tested with all supported versions of downstream projects.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

Copilot AI review requested due to automatic review settings May 19, 2026 13:24
@RunningLeon RunningLeon changed the title [WIP]: Ssupport reuse routed experts on sequnece [WIP]: Support reuse routed experts on eviction May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR reworks how routed-expert traces are accumulated on a sequence so that experts can be reused across re-prefill / chunked prefill rather than dropped. append_routed_experts now takes an absolute start_pos and deduplicates rows that the cache already covers; the previous truncation in set_step is removed; callers in AR / AR-spec / chunked engine loop pass the appropriate absolute position; and tests are reorganized to cover the new behaviour. The PR title is marked WIP.

Changes:

  • append_routed_experts requires start_pos and deduplicates overlapping rows; set_step no longer truncates all_routed_experts.
  • AR / AR-spec sequence updates (update_token_ids, _update_token_ids_prefill/_decode) and the chunk-handling branch in EngineLoop._make_infer_outputs now compute and pass start_pos based on num_history_ids / chunk history_lengths.
  • Tests rewritten/added: _make_seq_with_experts-style helpers, new TestRoutedExpertsReuse and TestRoutedExpertsLongContext cases verifying preservation across set_step and chunked prefill.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
lmdeploy/pytorch/messages.py Adds start_pos parameter and dedup logic to append_routed_experts.
lmdeploy/pytorch/strategies/ar/sequence.py Passes start_pos based on num_history_ids; removes expert truncation from set_step.
lmdeploy/pytorch/strategies/ar_spec/sequence.py Same treatment for spec-decode prefill/decode paths and set_step.
lmdeploy/pytorch/engine/engine_loop.py Non-final chunk path now passes start_pos=history_lengths[0].
tests/pytorch/spec_decode/test_strategies.py Updates expectations to reflect preservation; adds new reuse / long-context coverage.
.gitignore Adds workdir*/ and wscripts*/ to ignore list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +741 to 762
def append_routed_experts(self, routed_experts: Tensor | np.ndarray, start_pos: int):
"""Append routed experts, deduplicating by absolute token position.

Args:
routed_experts (Tensor | np.ndarray): Expert rows returned by the
model. The first dimension is aligned with token positions.
start_pos (int): Absolute token position of the first expert row
in ``routed_experts``. Rows already covered by the contiguous
cached expert history are skipped.
"""
if not self.return_routed_experts:
return
if routed_experts is None:
return
if isinstance(routed_experts, Tensor):
routed_experts = routed_experts.cpu().numpy()
cur_len = len(self.all_routed_experts)
assert cur_len >= start_pos, f'cached length must >= start_pos, but given: {cur_len} vs {start_pos}'
offset = cur_len - start_pos
if offset > 0:
routed_experts = routed_experts[offset:]
self.all_routed_experts.append(routed_experts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants