Skip to content

clear runtime state in sleep - #4729

Merged
lvhan028 merged 3 commits into
InternLM:mainfrom
grimoire:fix-sleep-runtime-state-reset
Jul 10, 2026
Merged

clear runtime state in sleep#4729
lvhan028 merged 3 commits into
InternLM:mainfrom
grimoire:fix-sleep-runtime-state-reset

Conversation

@grimoire

@grimoire grimoire commented Jul 3, 2026

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.

@grimoire
grimoire marked this pull request as ready for review July 7, 2026 08:18
Copilot AI review requested due to automatic review settings July 7, 2026 08:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 ensures PyTorch engine “sleep” properly clears request-local runtime state (decode/chunk carry and scheduling state) so canceled sessions don’t leave stale state that could affect subsequent work after wakeup.

Changes:

  • Add reset_runtime_state() APIs across ModelAgent, InputsMaker, EngineLoop, and speculative decoding agents to discard request-local state.
  • Invoke runtime-state resets during sleep flows (engine-level sleep and model-agent sleep), and prevent prefetching additional work once sleep is requested.
  • Add unit tests to validate runtime-state clearing and that sleep drain avoids prefetch while still releasing prefix-cache save refs.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/pytorch/engine/test_model_agent.py Adds tests asserting BaseModelAgent.reset_runtime_state() and spec agent reset discard chunk/decode carry.
tests/pytorch/engine/test_inputs_maker.py Adds tests for sleep-prefetch suppression, EngineLoop reset delegation, and InputsMaker runtime-state reset behavior.
tests/pytorch/engine/test_engine_sleep.py Extends sleep test to assert drain/sleep/reset ordering.
lmdeploy/pytorch/spec_decode/spec_agent.py Initializes and clears speculative draft carry state via _init_runtime_state() / reset_runtime_state().
lmdeploy/pytorch/spec_decode/base.py Adds a base reset_runtime_state() hook for spec agents (no-op by default).
lmdeploy/pytorch/engine/model_agent/agent.py Adds runtime-state init/reset for decode + chunk carry; calls reset during model-agent sleep.
lmdeploy/pytorch/engine/inputs_maker.py Refactors request-local scheduling state into _init_runtime_state() and adds reset_runtime_state().
lmdeploy/pytorch/engine/engine.py Resets EngineLoop runtime state after sleeping (post session cancellation + executor sleep).
lmdeploy/pytorch/engine/engine_loop.py Adds loop-level reset delegation and skips prefetch once sleep is requested.

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

@RunningLeon RunningLeon left a comment

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.

LGTM

@lvhan028
lvhan028 merged commit 3af6788 into InternLM:main Jul 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants