[Improve]: Drain queues when sleep engine#4577
Open
RunningLeon wants to merge 1 commit intoInternLM:mainfrom
Open
[Improve]: Drain queues when sleep engine#4577RunningLeon wants to merge 1 commit intoInternLM:mainfrom
RunningLeon wants to merge 1 commit intoInternLM:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses stale forward data persisting across engine sleep/wakeup by introducing a queue-draining step in the PyTorch BaseModelAgent sleep path, and adds unit tests to validate the draining behavior.
Changes:
- Add
BaseModelAgent._drain_queues()to discard pending items from internal forward queues. - Invoke
_drain_queues()duringBaseModelAgent.sleep()to prevent stale outputs after wakeup. - Add a new test module covering queue-draining scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
lmdeploy/pytorch/engine/model_agent/agent.py |
Adds _drain_queues() and drains internal queues during sleep() to avoid stale forward data. |
tests/pytorch/engine/test_model_agent.py |
Adds unit tests intended to validate _drain_queues() behavior and stale-output prevention. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+9
to
+16
| old_loop = asyncio.get_event_loop() | ||
| new_loop = asyncio.new_event_loop() | ||
| try: | ||
| asyncio.set_event_loop(new_loop) | ||
| yield new_loop | ||
| finally: | ||
| new_loop.stop() | ||
| asyncio.set_event_loop(old_loop) |
Comment on lines
+19
to
+27
| def _make_agent_with_queues(): | ||
| """Create a minimal BaseModelAgent-like object with internal queues.""" | ||
| from lmdeploy.pytorch.engine.model_agent.agent import BaseModelAgent | ||
|
|
||
| # Bypass __init__ — we only need the queues. | ||
| agent = BaseModelAgent.__new__(BaseModelAgent) | ||
| agent._pre_in_que = asyncio.Queue() | ||
| agent._in_que = asyncio.Queue() | ||
| agent._out_que = asyncio.Queue() |
Comment on lines
+941
to
+950
| def _drain_queues(self): | ||
| """Drain all internal queues to discard stale forward data.""" | ||
| for q in (self._pre_in_que, self._in_que, self._out_que): | ||
| if q is None: | ||
| continue | ||
| while not q.empty(): | ||
| try: | ||
| q.get_nowait() | ||
| except asyncio.QueueEmpty: | ||
| break |
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.
Motivation
Drain queues when sleep engine to avoid stale data queues.
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