Skip to content

Conversation

@CLFutureX
Copy link

issue

Problem

Excessive preemption occurs when preempting running requests to schedule new prefill requests.

Affected Path

python/sglang/srt/managers/schedule_policy.py

Relevant Code Snippet

min_tokens_to_remove = (
    req.extend_input_len
    + min(req.sampling_params.max_new_tokens, CLIP_MAX_NEW_TOKENS)
    - self.rem_total_tokens
)
for running_req in sorted_running_reqs:
    if running_req in self.preempt_list:
        continue
    # Priority difference needs to meet the threshold to be preemptible.
    priority_diff = req.priority - running_req.priority
    if server_args.schedule_low_priority_values_first:
        priority_diff *= -1
    if priority_diff > self.priority_scheduling_preemption_threshold:
        preemptible_reqs.append(running_req)
        min_tokens_to_remove -= self._get_running_request_total_token_offset(
            running_req
        )

Solution

Exit the loop and stop preemption once the current prefill request's resource requirements are met (i.e., when min_tokens_to_remove <= 0).

Additional Adjustments

  1. Filter running requests first before sorting them by priority.
  2. Avoid full for-loop traversal: Break the loop immediately when the priority difference is below the threshold.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @CLFutureX, 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 addresses an issue of excessive preemption in the scheduling policy by refining the logic within the preempt_to_schedule method. The changes aim to make the preemption process more efficient and targeted, ensuring that only necessary requests are preempted and that the system avoids redundant iterations. This leads to better resource utilization and potentially improved scheduling performance.

Highlights

  • Optimized Preemption Logic: Improved the efficiency of the preempt_to_schedule method by filtering out already preempted requests before sorting and introducing early exit conditions for the preemption loop.
  • Reduced Excessive Preemption: The preemption process now stops as soon as the resource requirements for the new prefill request are met, preventing unnecessary preemption of additional running requests.
  • Enhanced Loop Efficiency: The preemption loop now breaks early if a running request's priority difference does not meet the threshold, avoiding full traversal when further preemption is not possible or needed.
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.

@CLFutureX CLFutureX changed the title fix Excessive preemption and optimize loop fix: Excessive preemption occurs when preempting running requests to schedule new prefill requests. Nov 1, 2025
Copy link
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 optimizations to the request preemption logic in the scheduler. The main fix prevents excessive preemption by exiting the loop as soon as enough token space has been freed for the new request. Additionally, the code is optimized by filtering out non-preemptible requests before sorting, and by breaking the preemption loop early if a request does not meet the priority threshold, since subsequent requests in the sorted list also won't. These changes are well-implemented and should improve scheduling efficiency and prevent unnecessary preemptions.

Signed-off-by: CLFutureX <[email protected]>
@CLFutureX
Copy link
Author

@harrisonlimh Hey,PTAL

@harrisonlimh harrisonlimh self-assigned this Nov 4, 2025
@CLFutureX
Copy link
Author

@hnyls2002 @merrymercy @xiezhq-hermann @Ying1123 hey, PTAL , thanks

@CLFutureX CLFutureX requested a review from zhyncs as a code owner November 7, 2025 02:48
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