Skip to content

[Improve]: Drain queues when sleep engine#4577

Open
RunningLeon wants to merge 1 commit intoInternLM:mainfrom
RunningLeon:drain-queues
Open

[Improve]: Drain queues when sleep engine#4577
RunningLeon wants to merge 1 commit intoInternLM:mainfrom
RunningLeon:drain-queues

Conversation

@RunningLeon
Copy link
Copy Markdown
Collaborator

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

  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 9, 2026 13:26
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 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() during BaseModelAgent.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
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