[WIP]: Support reuse routed experts on eviction#4599
Open
RunningLeon wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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_expertsrequiresstart_posand deduplicates overlapping rows;set_stepno longer truncatesall_routed_experts.- AR / AR-spec sequence updates (
update_token_ids,_update_token_ids_prefill/_decode) and the chunk-handling branch inEngineLoop._make_infer_outputsnow compute and passstart_posbased onnum_history_ids/ chunkhistory_lengths. - Tests rewritten/added:
_make_seq_with_experts-style helpers, newTestRoutedExpertsReuseandTestRoutedExpertsLongContextcases verifying preservation acrossset_stepand 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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