Skip to content

[Refactor / Style] Unify all event loops (except for PP)#12959

Merged
hnyls2002 merged 4 commits intomainfrom
lsyin/adjust-decode
Nov 10, 2025
Merged

[Refactor / Style] Unify all event loops (except for PP)#12959
hnyls2002 merged 4 commits intomainfrom
lsyin/adjust-decode

Conversation

@hnyls2002
Copy link
Copy Markdown
Collaborator

@hnyls2002 hnyls2002 commented Nov 10, 2025

Now all event loops follow the same logic:

Non-overlap loop:

while true:
    recv = recv_requests()
    process_input_requests(recv)
    pre_batch_work()
    batch = build_next_batch()
    cur_batch = batch
    if batch:
        result = run_batch(batch)
        consume_result(batch, result)
    else:
        self_check_during_idle()
    post_batch_work()
    last_batch = batch

Overlap loop:

result_queue = deque()
last_batch = None
while true:
    recv = recv_requests()
    process_input_requests(recv)
    pre_batch_work()
    batch = build_next_batch()
    cur_batch = batch
    if batch:
        result = run_batch(batch)
        result_queue.push((batch.copy(), result))
    if last_batch:
        prev_batch, prev_result = result_queue.popleft()
        consume_result(prev_batch, prev_result)
    elif batch is None:
        self_check_during_idle()
    post_batch_work()
    launch_batch_sample_if_needed(result)
    last_batch = batch

And all idle batch's preparation (MLP sync) happens inside build_next_batch()

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @hnyls2002, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on refactoring and improving the code structure within the SGLang scheduler, specifically targeting disaggregated prefill and decode operations. The changes aim to enhance code readability, maintainability, and reduce duplication by standardizing the interface for KV receivers and consolidating complex logic into dedicated helper methods. This streamlines the management of request states and KV cache during the lifecycle of a request, contributing to a more organized and efficient codebase.

Highlights

  • Standardized KV Receiver Interface: Added clear() and abort() methods to the base connection class (conn.py), ensuring a consistent interface for managing KV cache states across different implementations.
  • Refactored Decode Transfer Logic: Extracted the complex logic for committing transferred data to decode requests into a new private method _commit_transfer_to_req in decode.py, significantly improving modularity and readability within the pop_transferred method.
  • Consolidated Prefill Batch Preparation: Introduced get_next_disagg_prefill_batch_to_run in prefill.py to centralize the logic for preparing prefill batches, including handling chunked requests and KV transfers. This change simplifies the main event loop implementations by abstracting away batch creation details.
  • Simplified Abort and Clear Operations: Removed redundant hasattr checks before calling clear() and abort() on KV receivers in decode.py and scheduler.py. This change relies on the newly enforced interface in the base connection class, making the code cleaner and more robust.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several good refactorings that improve code clarity and reduce duplication across the event loops. Key changes include adding clear() and abort() methods to the BaseKVReceiver to avoid hasattr checks, and extracting common logic in decode.py and prefill.py into new, reusable methods. These changes make the code cleaner and more maintainable. The overall quality of the code is improved, and I have not identified any issues with this refactoring.

@hnyls2002 hnyls2002 changed the title Style adjust for all event loops [Refactor / Style] Unify all event loops (except for PP) Nov 10, 2025
@hnyls2002 hnyls2002 merged commit dc8a5a1 into main Nov 10, 2025
16 of 53 checks passed
@hnyls2002 hnyls2002 deleted the lsyin/adjust-decode branch November 10, 2025 06:49
ocss884 pushed a commit to ocss884/sglang that referenced this pull request Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants