-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Add endpoint load metrics #14906
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
Open
coolkp
wants to merge
4
commits into
vllm-project:main
Choose a base branch
from
coolkp:endpoint-load-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add endpoint load metrics #14906
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,12 +42,11 @@ | |
| from vllm.entrypoints.logger import RequestLogger | ||
| from vllm.entrypoints.openai.cli_args import (make_arg_parser, | ||
| validate_parsed_serve_args) | ||
| from vllm.entrypoints.openai.orca_metrics import metrics_header | ||
| # yapf conflicts with isort for this block | ||
| # yapf: disable | ||
| from vllm.entrypoints.openai.protocol import (ChatCompletionRequest, | ||
| ChatCompletionResponse, | ||
| CompletionRequest, | ||
| CompletionResponse, | ||
| DetokenizeRequest, | ||
| DetokenizeResponse, | ||
| EmbeddingChatRequest, | ||
|
|
@@ -99,6 +98,8 @@ | |
| # Cannot use __name__ (https://github.com/vllm-project/vllm/pull/4765) | ||
| logger = init_logger('vllm.entrypoints.openai.api_server') | ||
|
|
||
| ENDPOINT_LOAD_METRICS_FORMAT_HEADER_LABEL = "endpoint-load-metrics-format" | ||
|
|
||
| _running_tasks: set[asyncio.Task] = set() | ||
|
|
||
|
|
||
|
|
@@ -455,6 +456,8 @@ async def show_version(): | |
| @load_aware_call | ||
| async def create_chat_completion(request: ChatCompletionRequest, | ||
| raw_request: Request): | ||
| metrics_header_format = raw_request.headers.get( | ||
| ENDPOINT_LOAD_METRICS_FORMAT_HEADER_LABEL, "") | ||
| handler = chat(raw_request) | ||
| if handler is None: | ||
| return base(raw_request).create_error_response( | ||
|
|
@@ -466,8 +469,11 @@ async def create_chat_completion(request: ChatCompletionRequest, | |
| return JSONResponse(content=generator.model_dump(), | ||
| status_code=generator.code) | ||
|
|
||
| elif isinstance(generator, ChatCompletionResponse): | ||
| return JSONResponse(content=generator.model_dump()) | ||
| # Tuple[ChatCompletionResponse,Optional[InbandEngineStats]] | ||
| elif isinstance(generator, tuple): | ||
| return JSONResponse(content=generator[0].model_dump(), | ||
| headers=metrics_header(generator[1], | ||
| metrics_header_format)) | ||
|
|
||
| return StreamingResponse(content=generator, media_type="text/event-stream") | ||
|
|
||
|
|
@@ -476,6 +482,8 @@ async def create_chat_completion(request: ChatCompletionRequest, | |
| @with_cancellation | ||
| @load_aware_call | ||
| async def create_completion(request: CompletionRequest, raw_request: Request): | ||
| metrics_header_format = raw_request.headers.get( | ||
| ENDPOINT_LOAD_METRICS_FORMAT_HEADER_LABEL, "") | ||
| handler = completion(raw_request) | ||
| if handler is None: | ||
| return base(raw_request).create_error_response( | ||
|
|
@@ -485,8 +493,12 @@ async def create_completion(request: CompletionRequest, raw_request: Request): | |
| if isinstance(generator, ErrorResponse): | ||
| return JSONResponse(content=generator.model_dump(), | ||
| status_code=generator.code) | ||
| elif isinstance(generator, CompletionResponse): | ||
| return JSONResponse(content=generator.model_dump()) | ||
|
|
||
| # Tuple[ChatCompletionResponse,Optional[InbandEngineStats]] | ||
| elif isinstance(generator, tuple): | ||
| return JSONResponse(content=generator[0].model_dump(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also check len(generator) == 2, and the correpsonding type? Maybe create a helper function. |
||
| headers=metrics_header(generator[1], | ||
| metrics_header_format)) | ||
|
|
||
| return StreamingResponse(content=generator, media_type="text/event-stream") | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some unittest? |
||
| """ | ||
| This file contains the command line arguments for the vLLM's | ||
| OpenAI-compatible server. It is kept in a separate file for documentation | ||
| purposes. | ||
| """ | ||
|
|
||
| import json | ||
| from collections.abc import Mapping | ||
| from typing import Optional | ||
|
|
||
| from vllm.logger import init_logger | ||
| from vllm.sequence import InbandEngineStats | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| def create_orca_header(metrics_format: str, | ||
| named_metrics: list[tuple[str, float]], | ||
| metadata_fields=None) -> Optional[Mapping[str, str]]: | ||
| """ | ||
| Creates ORCA headers named 'endpoint-load-metrics' in the specified format | ||
| and adds custom metrics to named_metrics. | ||
| ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0 | ||
| ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto | ||
| Parameters: | ||
| - format (str): The format of the header ('BIN', 'TEXT', 'JSON'). | ||
| - named_metrics (List[Tuple[str, float]]): List of tuples with metric names | ||
| and their corresponding double values. | ||
| - metadata_fields (list): List of additional metadata fields | ||
| (currently unsupported). | ||
| Returns: | ||
| - Optional[Mapping[str,str]]: A dictionary with header key as | ||
| 'endpoint-load-metrics' and values as the ORCA header strings with | ||
| format prefix and data in with named_metrics in. | ||
| """ | ||
|
|
||
| if metadata_fields: | ||
| logger.warning("Warning: `metadata_fields` are not supported in the" | ||
| "ORCA response header yet.") | ||
|
|
||
| if metrics_format.lower() not in ["text", "json"]: | ||
| logger.warning( | ||
| "Warning: `%s` format is not supported in the ORCA response header", | ||
| format, | ||
| ) | ||
| return None | ||
|
|
||
| header = {} | ||
| orca_report = { | ||
| "named_metrics": { | ||
| metric_name: value | ||
| for metric_name, value in named_metrics | ||
| if isinstance(metric_name, str) and isinstance(value, float) | ||
| } | ||
| } | ||
| # output example: | ||
| # endpoint-load-metrics: TEXT named_metrics.kv_cache_utilization=0.4 | ||
| if metrics_format.lower() == "text": | ||
| native_http_header = ", ".join([ | ||
| f"named_metrics.{metric_name}={value}" | ||
| for metric_name, value in named_metrics | ||
| if isinstance(metric_name, str) and isinstance(value, float) | ||
| ]) | ||
| header["endpoint-load-metrics"] = f"TEXT {native_http_header}" | ||
|
|
||
| # output example: | ||
| # endpoint-load-metrics: JSON “named_metrics”: {“custom-metric-util”: 0.4} | ||
| elif metrics_format.lower() == "json": | ||
| header["endpoint-load-metrics"] = f"JSON {json.dumps(orca_report)}" | ||
|
|
||
| return header | ||
|
|
||
|
|
||
| def metrics_header(m: Optional[InbandEngineStats], | ||
| metrics_format: str) -> Optional[Mapping[str, str]]: | ||
| if not m or not metrics_format: | ||
| return None | ||
| named_metrics: list[tuple[str, float]] = [] | ||
| for metric, val in vars(m).items(): | ||
| if isinstance(val, float) and metric != "now": | ||
| named_metrics.append((str(metric), float(val))) | ||
| return create_orca_header(metrics_format, named_metrics) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: create a function to include get_stats then set inband_stats logic, since it repeats.