Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core][Bugfix][Perf] Refactor Server to Avoid AsyncLLMEngine #8092

Closed
wants to merge 33 commits into from

Conversation

robertgshaw2-neuralmagic
Copy link
Collaborator

@robertgshaw2-neuralmagic robertgshaw2-neuralmagic commented Sep 2, 2024

SUMMARY:

  • @njhill and I removed almost all the overhead from the OpenAI server, but still saw significant slowdown running in AsyncLLMEngine rather than LLMEngine on H100, including when we ran "headless" (e.g. with no uvicorn server).
  • This caused us to believe the asyncio event loop in AsyncLLMEngine was the root cause of the slowdown
  • This PR replaces AsyncLLMEngine with MPLLMEngine. MPLLMEngine works similarly to AsyncLLMEngine (i.e. it runs a background loop, accepts new requests, and streams requests back to the clients). We use zeromq as the message passing protocol rather than pulling from queues and pushing to generators
  • This PR also fixes the number of sockets in use by the RPCClient, avoiding all issues with Too Many Open Files

Summary Performance vs Offline:

pr scenario offline serving slowdown %
main multistep 42.9 33.3 -22%
pr multistep 42.9 40.3 -6%
main single-step 34.8 14.9 -55%
pr single-step 34.8 31.6 -9%
  • NOTE: the multistep performance on main is "less-bad" because we currently only stream 1/8 tokens. Once we enable incremental streaming, the performance will be closer to -50% on main.

  • NOTE: there is still some remaining performance to get by switching the inner loop to using protobufs

Multistep Performance

1xH100 PERFORMANCE BASELINE:

MODEL="meta-llama/Meta-Llama-3.1-8B-Instruct"
python3 benchmarks/benchmark_throughput.py --model $MODEL --dataset benchmarks/ShareGPT_V3_unfiltered_cleaned_split.json --num-scheduler-steps 8

1xH100 SERVING PERFORMANCE

  • Client:
MODEL="meta-llama/Meta-Llama-3.1-8B-Instruct"
python3 benchmarks/benchmark_serving.py --model $MODEL --dataset-path benchmarks/ShareGPT_V3_unfiltered_cleaned_split.json 
  • Server:
MODEL="meta-llama/Meta-Llama-3.1-8B-Instruct"
vllm serve $MODEL --disable-log-requests --num-scheduler-steps 8 --max-model-len 8192

SUMMARY

branch setup throughput
main offline 42.9
main serving mp 33.3
main serving --disable-frontend-multiprocessing 28.9
pr serving mp 40.3
pr serving --disable-frontend-multiprocessing 27.9

Single-Step Performance

1xH100 PERFORMANCE BASELINE:

MODEL="meta-llama/Meta-Llama-3.1-8B-Instruct"
python3 benchmarks/benchmark_throughput.py --model $MODEL --dataset benchmarks/ShareGPT_V3_unfiltered_cleaned_split.json

1xH100 SERVING PERFORMANCE

  • Client:
MODEL="meta-llama/Meta-Llama-3.1-8B-Instruct"
python3 benchmarks/benchmark_serving.py --model $MODEL --dataset-path benchmarks/ShareGPT_V3_unfiltered_cleaned_split.json 
  • Server:
MODEL="meta-llama/Meta-Llama-3.1-8B-Instruct"
vllm serve $MODEL --disable-log-requests --max-model-len 8192

SUMMARY

branch setup throughput
main offline 34.8
main serving mp 14.9
pr serving mp 31.6

TODOS:

  • battle test with high load (abort | HWM?)
  • improve robustness (startup / teardown)
  • feature set (ray, non-gpu, pipeline parallel, profiler)
  • hardening / unit tests for MPLLMEngine
  • tests passing

co-authored by @njhill

FIX #7920

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!

Copy link

github-actions bot commented Sep 2, 2024

👋 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.

🚀

@robertgshaw2-neuralmagic robertgshaw2-neuralmagic changed the title [Core] Reduce Asyncio Overhead Protoype [Core][Bugfix][Preformance] Asyncio-Free LLMEngine Sep 2, 2024
@robertgshaw2-neuralmagic robertgshaw2-neuralmagic changed the title [Core][Bugfix][Preformance] Asyncio-Free LLMEngine [Core][Bugfix][Performance] Remove AsyncLLMEngine Sep 2, 2024
@robertgshaw2-neuralmagic robertgshaw2-neuralmagic changed the title [Core][Bugfix][Performance] Remove AsyncLLMEngine [Core][Bugfix][Performance] Refactor OpenAI Server to Avoid Use of AsyncLLMEngine Sep 2, 2024
@robertgshaw2-neuralmagic robertgshaw2-neuralmagic changed the title [Core][Bugfix][Performance] Refactor OpenAI Server to Avoid Use of AsyncLLMEngine [Core][Bugfix][Performance] Refactor Server to avoid AsyncLLMEngine Sep 2, 2024
@robertgshaw2-neuralmagic robertgshaw2-neuralmagic changed the title [Core][Bugfix][Performance] Refactor Server to avoid AsyncLLMEngine [Core][Bugfix][Perf] Refactor Server to avoid AsyncLLMEngine Sep 2, 2024
@robertgshaw2-neuralmagic
Copy link
Collaborator Author

cc @KuntaiDu

@robertgshaw2-neuralmagic robertgshaw2-neuralmagic marked this pull request as ready for review September 3, 2024 02:46
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Sep 3, 2024
request=RPCUtilityRequest.DO_LOG_STATS,
socket=self.input_socket)

@property
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

todo: actually set these

await self._send_one_way_rpc_request(
request=RPCUtilityRequest.CHECK_HEALTH, socket=self.input_socket)

# Await acknowledgement from MPLLMEngine.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is a genuinely open question?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It indeed seems not ideal. Either of the following approaches make more sense to me (but we probably don't have to fix it in this PR).

  1. Make sure the response is corresponding to the right sender by matching request ID.
  2. Cache the health check result in an interval (e.g., 10 seconds) and decouple client health check requests and the actual health checks.

ray_utils.assert_ray_available()

# TODO: better abstraction?
executor_class = AsyncLLMEngine._get_executor_cls(engine_config)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO: make this generic


if not self.engine_use_ray:
engine_class = self._engine_class
elif self.worker_use_ray:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what is worker_use_ray?

Copy link
Member

Choose a reason for hiding this comment

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

it is the same as --distributed_executor_backend=ray

raise NotImplementedError("Not supported yet!")
return engine_class(*args, **kwargs)

def run_background_loop(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Inner loop?

How should we make this propogate exceptions and do things like have the BackgroundLoopDeadError

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it even worth pulling over the LoopDead logic from the async llm engine?

IIUC that's all there to prevent misconfiguration errors from putting the engine into a state where it only ever responds with an exception, but it looks like it's done pretty bluntly where any exception at all from the model executor will kill the loop. If we want to keep that behavior, we could simply raise from here and exit (after notifying the clients of the exception), and let the frontend die as well.

response = VLLM_RPC_SUCCESS_STR
elif request == RPCStartupRequest.CLIENT_IS_READY:
response = VLLM_RPC_SUCCESS_STR
# Breakout of loop once client is ready.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this should be a break ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't break here because we need to response the client in the next 2 lines? We'll get out of the while loop anyways due to client_is_ready = True

self.stream_outputs(request_outputs)

def wait_for_new_input(self):
while self.input_socket.poll(timeout=POLLING_TIMEOUT_MS) == 0:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should we do something else here?

Comment on lines +192 to +203
# Block until there is a new request.
if not self.engine.has_unfinished_requests():
self.wait_for_new_input()

# Handle any new input from the input socket.
self.maybe_handle_new_input()

# Engine step.
request_outputs = self.engine.step()

# Stream results to output socket.
self.stream_outputs(request_outputs)
Copy link
Member

Choose a reason for hiding this comment

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

to handle error propogatation, maybe we should add a try-except here, and send the exception back?

@alexm-neuralmagic
Copy link
Collaborator

@robertgshaw2-neuralmagic @njhill great find about the asyncio overheads being the root cause!

Copy link
Collaborator

@alexm-neuralmagic alexm-neuralmagic left a comment

Choose a reason for hiding this comment

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

Great work, some small comments

stream=stream,
logprobs=3)
max_tokens=100)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you need to change this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will revert before merge. I just use this as a simple test example for myself in dev

Copy link
Collaborator

Choose a reason for hiding this comment

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

Reverted

def cleanup(self):
"""Cleanup zeromq state on shutdown."""
self.input_socket.close()
self.output_socket.close()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need also to close the health socket here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yup

Copy link
Collaborator

Choose a reason for hiding this comment

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

fixed :)

usage_context=usage_context,
ipc_path=ipc_path)

engine.run_background_loop()
Copy link
Collaborator

Choose a reason for hiding this comment

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

As I understand from the code, the loop that it is running here is not actually run in "the background".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mp_llm_engine runs in the background. I could change the name of the loop

vllm/entrypoints/openai/api_server.py Show resolved Hide resolved
vllm/engine/multiprocessing/mp_client.py Outdated Show resolved Hide resolved
await self._send_one_way_rpc_request(
request=RPCUtilityRequest.CHECK_HEALTH, socket=self.input_socket)

# Await acknowledgement from MPLLMEngine.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It indeed seems not ideal. Either of the following approaches make more sense to me (but we probably don't have to fix it in this PR).

  1. Make sure the response is corresponding to the right sender by matching request ID.
  2. Cache the health check result in an interval (e.g., 10 seconds) and decouple client health check requests and the actual health checks.

response = VLLM_RPC_SUCCESS_STR
elif request == RPCStartupRequest.CLIENT_IS_READY:
response = VLLM_RPC_SUCCESS_STR
# Breakout of loop once client is ready.
Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't break here because we need to response the client in the next 2 lines? We'll get out of the while loop anyways due to client_is_ready = True

vllm/engine/multiprocessing/mp_llm_engine.py Show resolved Hide resolved
socket.send_multipart((identity, pickle.dumps(e)),
copy=False)

def run_engine_loop(self) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible and beneficial to use async functions for all the function calls in this function?

Copy link
Contributor

Choose a reason for hiding this comment

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

What I gathered from this PR description is that this new engine implementation is running in a process without the use of any asyncio event loops, it's just a serial loop taking inputs over zmq and passing them to an LLMEngine

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, there is no asyncio at all in this design

Copy link
Collaborator

Choose a reason for hiding this comment

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

I know. I'm just wondering whether making the following functions async could further help with performance:

self.maybe_handle_new_input()
request_outputs = self.engine.step()
self.stream_outputs(request_outputs)

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a good point. I have a follow up PR #8125 that makes maybe_handle_new_input() and stream_outputs() async with GPU forward pass and it indeed helps. Maybe we will integrate the changes there immediately into this PR, but not sure yet.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have added the async behavior of sockets to this PR (and closed 8125)

benchmarks/benchmark_throughput_async.py Show resolved Hide resolved
# Conflicts:
#	vllm/entrypoints/openai/api_server.py
#	vllm/entrypoints/openai/rpc/client.py
@@ -21,6 +21,9 @@
from vllm.config import ModelConfig
from vllm.engine.arg_utils import AsyncEngineArgs
from vllm.engine.async_llm_engine import AsyncLLMEngine
# yapf: enable
Copy link
Member

Choose a reason for hiding this comment

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

You accidentally copied this line over.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good catch, thanks for pointing this.

@alexm-neuralmagic
Copy link
Collaborator

This PR is transferred to #8157

@alexm-neuralmagic
Copy link
Collaborator

We are addressing all review comments from here in here #8157

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.

[Bug]: OpenAI server errors out with "ZMQError Too many open files" under heavy load
7 participants