Skip to content

Conversation

@noooop
Copy link
Collaborator

@noooop noooop commented Aug 26, 2024

SUMMARY:

vllm 0.5.4 enable_chunked_prefill throughput is slightly lower than 0.5.3~0.5.0.

Prioritizing prefill causes and aggravate system thrashing.

FILL IN THE PR DESCRIPTION HERE

FIX #7592

by definition

By default, vLLM scheduler prioritizes prefills ...
Once chunked prefill is enabled, the policy is changed to prioritize decode requests.

The easiest fix is sort the running queue.

Keeping chunked prefill performance the untouched, everyone is happy.

BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE


PR Checklist (Click to Expand)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!

@github-actions
Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which consists a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of default ones by unblocking the steps in your fast-check build on Buildkite UI.

Once the PR is approved and ready to go, please make sure to run full CI as it is required to merge (or just use auto-merge).

To run full CI, you can do one of these:

  • Comment /ready on the PR
  • Add ready label to the PR
  • Enable auto-merge.

🚀

@noooop
Copy link
Collaborator Author

noooop commented Aug 26, 2024

@youkaichao

@youkaichao
Copy link
Member

thanks for the contribution! please fix the format issue.

@youkaichao
Copy link
Member

I don't get it though, why this would affect chunked prefill so much 👀

@comaniac
Copy link
Collaborator

Thanks for the fix! I have the same question as Kaichao. Why sorting running requests by their arrival time impacts the throughput significantly?

@noooop
Copy link
Collaborator Author

noooop commented Aug 27, 2024

Putting definitions and conventions aside first, let's discuss the pros and cons of chunked_prefill prioritizing scheduling prefill and prioritizing decoding.

  1. GPU memory limitations (gpu cache block limitations)
    When the GPU memory is sufficient, or max_num_batched_tokens and max_num_seqs are within a reasonable range, priority scheduling prefill can allow as many tasks as possible to enter decode mode, and even the entire batch is in decode mode, triggering CUDA graph optimization to improve throughput, but This (CUDA graph) is particularly effective for small models and when using tensor parallelism., and when the batch is less than 256 (_BATCH_SIZES_TO_CAPTURE[-1]).

So.Scenarios that favor priority scheduling of prefill are difficult to satisfy.

In reality, when llm is deployed, the GPU memory is often limited, or max_num_batched_tokens and max_num_seqs are set too large, and preemption inevitably occurs. Priority scheduling decode can finish running tasks as soon as possible and release GPU memory, while priority scheduling prefill increases the number of tasks that are running at the same time, increasing the possibility of preemption.
When preemption occurs, scheduling decode first means that tasks in the prefill phase are preempted and the cost is relatively small. When scheduling prefill first, tasks in the decode phase are preempted and the cost is relatively high.

In short, when the GPU memory is limited, scheduling prefill first is Disaster, this is what I encountered.

  1. User satisfaction

Prioritize scheduling decode, As mentioned in the documentation, "It improves ITL and generation decode because decode requests are prioritized."

Why sorting matters?

Give an example
max_num_seqs = max_num_batched_tokens= 256
input_len = output_len = 511

init
request 0: num_computed_tokens: 0, num_uncomputed_tokens 511
request 1: num_computed_tokens: 0, num_uncomputed_tokens 511

step 1:
Scheduled [0]

request 0: num_computed_tokens: 256, num_uncomputed_tokens 255
request 1: num_computed_tokens: 0, num_uncomputed_tokens 511

step 2:
Scheduled [0, 1]
request 0: num_computed_tokens: 511, num_uncomputed_tokens 1, (to enter decode mode,)
request 1: num_computed_tokens: 1, num_uncomputed_tokens 510

step 3:
prioritizing scheduling prefill (0.5.4~0.5.5
Scheduled [1] (Why not let request 0 decode ???????
request 0: num_computed_tokens: 511, num_uncomputed_tokens 1
request 1: num_computed_tokens: 257, num_uncomputed_tokens 254

prioritizing scheduling decode (0.5.0~0.5.3
Scheduled [0, 1]
request 0: num_computed_tokens: 512, num_uncomputed_tokens 1
request 1: num_computed_tokens: 256, num_uncomputed_tokens 255

sorting matters

@noooop
Copy link
Collaborator Author

noooop commented Aug 27, 2024

by the way
prioritizing scheduling prefill and prioritizing decoding. the order of running_queue is exactly the opposite.

But you can't just reverse the running_queue, you need modify every self.running.extend or as i said 'The easiest fix is sort the running queue.'

@noooop
Copy link
Collaborator Author

noooop commented Aug 27, 2024

Add more

It is also a normal performance tuning behavior to set max_num_batched_tokens and max_num_seqs slightly larger (to slightly trigger preemption), increase parallelism, and improve throughput.

But prioritizing prefill causes and aggravate system thrashing.

@youkaichao
Copy link
Member

youkaichao commented Aug 27, 2024

LGTM to add the sorting to get back to the behavior of 0.5.3. Please fix the format.

@noooop
Copy link
Collaborator Author

noooop commented Aug 27, 2024

Submit code to vllm for the first time.

Is there anything else I need to do?

@youkaichao
Copy link
Member

as long as it does not break any tests, we can merge it.

@noooop
Copy link
Collaborator Author

noooop commented Aug 27, 2024

Thanks

@rkooo567
Copy link
Collaborator

rkooo567 commented Aug 27, 2024

@noooop I think the issue is that after the refactoring, we should've changed the order of these lines to guarantee the ordering. Before the refactoring, the order was guranteed because we always sorted. Now we should more carefully extend the queue to preserve the right order.

https://github.com/vllm-project/vllm/blob/ed6f002d3340888142cb67c13a37c060b51fa889/vllm/core/scheduler.py#L1029C1-L1029C72

I think if we change the order to be

extend(swapped_in.decode)
extend(swapped_in.prefill)
extend(running.decode)
extend(running.prefill)
extend(new_prefill)

The same behavior is preserved. can you test it?

Note: without sorting, it may be difficult to always guarantee the right ordering when preemption happens, but I think that's the tradeoff

@rkooo567
Copy link
Collaborator

rkooo567 commented Aug 27, 2024

more specifically, change these lines

        self.running.extend([s.seq_group for s in prefills.seq_groups])
        self.running.extend(
            [s.seq_group for s in running_scheduled.decode_seq_groups])
        self.running.extend(
            [s.seq_group for s in running_scheduled.prefill_seq_groups])
        self.running.extend(
            [s.seq_group for s in swapped_in.decode_seq_groups])
        self.running.extend(
            [s.seq_group for s in swapped_in.prefill_seq_groups])

to

        self.running.extend(
            [s.seq_group for s in swapped_in.decode_seq_groups])
        self.running.extend(
            [s.seq_group for s in swapped_in.prefill_seq_groups])
        self.running.extend(
            [s.seq_group for s in running_scheduled.decode_seq_groups])
        self.running.extend(
            [s.seq_group for s in running_scheduled.prefill_seq_groups])
        self.running.extend([s.seq_group for s in prefills.seq_groups])

can you try testing it and see if it works?

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

@rkooo567

We need to maintain the priority order of the queue. There are at least four methods to choose from. We can choose the best method from efficiency, readability, ease of use, scalability, and maybe minimal modification.

  1. Sorting when dequeue. Although slightly inefficient, no one can break it,ease to use,ease to read,and minimal modification.

  2. use PriorityQueue. Priority queue is very good option,we need priority queue, we use priority queue.

The following methods are not recommended

  1. Manually maintain queue order when inqueue,with online check. maybe efficient. code that maintains order is everywhere,
    difficult to use, difficult to read, difficult to modification.

  2. Manually maintain queue order when inqueue,without check. ????? No one can modify this code in the future

The performance bottleneck is in the GPU. I think there won't be much performance difference between Sorting and PriorityQueue, even manually maintaining queue order when inqueue.

@comaniac
Copy link
Collaborator

The performance bottleneck is in the GPU. I think there won't be much performance difference between Sorting and PriorityQueue, even manually maintaining queue order when inqueue.

This may not be true especially for online serving which we are talking about a few millisecond ITL. In fact, Python overheads like these are the main performance bottleneck. We now even need to pre-allocate and reuse Python objects, use array.array, or add a branch for edge cases (e.g., do not call sum, count when there's only one element in a list). The easiest way to verify whether this sort creates ineligible overhead is running a performance benchmark.

@rkooo567
Copy link
Collaborator

rkooo567 commented Aug 28, 2024

Sorting when dequeue. Although slightly inefficient, no one can break it,ease to use,ease to read,and minimal modification.

Also to be clear, we used this implementation originally for exactly this reason, but vLLM currently has python overhead, and that's why we removed the sorting logic that requires repetitive queue copy. Often times, model forward only takes 10-20ms overhead only, and having 2-3ms overhead in the scheduler is critical in this kind of scenario. (if we eventually support async scheduler, we can probably come back to this implementation)

I think manual sorting is the best workaround. I am not opposed to use priority queue as well if it turns out that it has no perf impact.

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

I understand that strict orderliness is not necessary.

I'm testing to see if certain queues may need to be reversed.

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

Actually I was implementing async scheduler and stumbled upon this bug

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

It works, in fact I love is tradeoff .

My own manual sorting method required too many changes, so I gave up.

Copy link
Collaborator

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

this manual sorting comparison with 0.5.3 and sorting on 1,000 requests,scheduling sequence exactly the same

@rkooo567
Copy link
Collaborator

Awesome to hear that!

btw I don't know if basic correctness test failure is related. can you try merging the latest master?

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

ok

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

By the way

I was implementing async scheduler.
During this process, I made a huge modularization and added dynamic workflow to vllm.
I don't know if you want to see it.

https://github.com/noooop/light-vllm

@noooop
Copy link
Collaborator Author

noooop commented Aug 28, 2024

I don't know why the test failed.

This pr is too simple to break anything.

Or the test is set up based on the wrong scheduling method

@noooop
Copy link
Collaborator Author

noooop commented Aug 30, 2024

merg to the latest master

Can anyone help me with the test?

@noooop
Copy link
Collaborator Author

noooop commented Aug 30, 2024

@jon-chuang

Can you give me some suggestions to pass the test.

Can I delete test7? How?

I can't find example.txt

@jon-chuang
Copy link
Contributor

For this test, try making NUM_LOGPROBS contingent on fp8 dtype and set to 8 if e5m2 and something higher (16 or 32) for e4m3.

@jon-chuang
Copy link
Contributor

jon-chuang commented Aug 30, 2024

If you can't fix it this way, you can mark that specific parameters for the test which fail (model type, dtype) as pytest.mark.skip("flakey test, see: #XXX") or create an issue and link to that and I can fix it in another PR.

@noooop noooop mentioned this pull request Aug 31, 2024
1 task
@noooop
Copy link
Collaborator Author

noooop commented Aug 31, 2024

# We use float32 for probabilities and log probabilities.

In Sampler float32 precise is is high enough.

Can NUM_LOGPROBS be enlarged to achieve the original testing purpose?

I choose to skip this test and let professionals solve it.

@jon-chuang #8051

@noooop
Copy link
Collaborator Author

noooop commented Aug 31, 2024

@youkaichao @rkooo567 @comaniac

Is it ready to launch?

@noooop
Copy link
Collaborator Author

noooop commented Aug 31, 2024

/ready

@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Aug 31, 2024
@youkaichao
Copy link
Member

thanks for the contribution! I triggered the test again, as long as the tests pass, we can merge it.

@youkaichao youkaichao merged commit 6e36f4f into vllm-project:main Sep 2, 2024
gongdao123 pushed a commit to bartsolutions/vllm that referenced this pull request Oct 18, 2024
[Bugfix] Fix vllm-project#7592 vllm 0.5.4 enable_chunked_prefill throughput is slightly lower than 0.5.3~0.5.0. (vllm-project#7874)
Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
[Bugfix] Fix vllm-project#7592 vllm 0.5.4 enable_chunked_prefill throughput is slightly lower than 0.5.3~0.5.0. (vllm-project#7874)

Signed-off-by: Alvant <[email protected]>
LeiWang1999 pushed a commit to LeiWang1999/vllm-bitblas that referenced this pull request Mar 26, 2025
[Bugfix] Fix vllm-project#7592 vllm 0.5.4 enable_chunked_prefill throughput is slightly lower than 0.5.3~0.5.0. (vllm-project#7874)

Signed-off-by: LeiWang1999 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: vllm 0.5.4 with enable_chunked_prefill =True, throughput is slightly lower than 0.5.3~0.5.0.

6 participants