-
Notifications
You must be signed in to change notification settings - Fork 5k
[PD metrics] Add latency Histogram metrics of each stage for generate requests #8710
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
|
|
||
|
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. move this below copyright |
||
| # Copyright 2023-2024 SGLang Team | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
|
|
@@ -35,6 +37,7 @@ | |
| import dataclasses | ||
| import logging | ||
| import threading | ||
| import time | ||
| from enum import Enum, auto | ||
| from http import HTTPStatus | ||
| from itertools import chain | ||
|
|
@@ -61,7 +64,7 @@ | |
| from sglang.srt.mem_cache.lora_radix_cache import LoRAKey, LoRARadixCache | ||
| from sglang.srt.mem_cache.memory_pool import HybridReqToTokenPool, ReqToTokenPool | ||
| from sglang.srt.mem_cache.swa_radix_cache import SWARadixCache | ||
| from sglang.srt.metrics.collector import TimeStats | ||
| from sglang.srt.metrics.collector import SchedulerMetricsCollector, TimeStats | ||
| from sglang.srt.model_executor.forward_batch_info import CaptureHiddenMode, ForwardMode | ||
| from sglang.srt.sampling.sampling_batch_info import SamplingBatchInfo | ||
| from sglang.srt.sampling.sampling_params import SamplingParams | ||
|
|
@@ -408,6 +411,23 @@ def merge(self, other: MultimodalInputs): | |
| # other args would be kept intact | ||
|
|
||
|
|
||
| class RequestStage(str, enum.Enum): | ||
| # prefill | ||
| PREFILL_WAITING = "prefill_waiting" | ||
|
|
||
| # disaggregation prefill | ||
| PREFILL_PREPARE = "prefill_prepare" | ||
| PREFILL_BOOTSTRAP = "prefill_bootstrap" | ||
| PREFILL_FORWARD = "prefill_forward" | ||
| PREFILL_TRANSFER_KV_CACHE = "prefill_transfer_kv_cache" | ||
|
|
||
| # disaggregation decode | ||
| DECODE_PREPARE = "decode_prepare" | ||
| DECODE_BOOTSTRAP = "decode_bootstrap" | ||
| DECODE_WAITING = "decode_waiting" | ||
| DECODE_TRANSFERRED = "decode_transferred" | ||
|
|
||
|
|
||
| class Req: | ||
| """The input and output status of a request.""" | ||
|
|
||
|
|
@@ -434,6 +454,7 @@ def __init__( | |
| bootstrap_room: Optional[int] = None, | ||
| data_parallel_rank: Optional[int] = None, | ||
| vocab_size: Optional[int] = None, | ||
| metrics_collector: Optional[SchedulerMetricsCollector] = None, | ||
| ): | ||
| # Input and output info | ||
| self.rid = rid | ||
|
|
@@ -591,10 +612,12 @@ def __init__( | |
| self.spec_verify_ct = 0 | ||
|
|
||
| # For metrics | ||
| self.metrics_collector = metrics_collector | ||
| self.time_stats: TimeStats = TimeStats() | ||
| self.has_log_time_stats: bool = False | ||
| self.queue_time_start = None | ||
| self.queue_time_end = None | ||
| self.last_tic = time.monotonic() | ||
|
|
||
| # For disaggregation | ||
| self.bootstrap_host: str = bootstrap_host | ||
|
|
@@ -627,6 +650,16 @@ def is_prefill_only(self) -> bool: | |
| """Check if this request is prefill-only (no token generation needed).""" | ||
| return self.sampling_params.max_new_tokens == 0 | ||
|
|
||
| def add_latency(self, stage: RequestStage): | ||
| if self.metrics_collector is None: | ||
| return | ||
| assert stage.name in RequestStage.__members__, f"{stage=} is invalid" | ||
| now = time.monotonic() | ||
| self.metrics_collector.observe_request_latency_seconds( | ||
| stage.value, now - self.last_tic | ||
| ) | ||
| self.last_tic = now | ||
|
|
||
| def extend_image_inputs(self, image_inputs): | ||
| if self.multimodal_inputs is None: | ||
| self.multimodal_inputs = image_inputs | ||
|
|
||
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
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.
For the prepare stage of the request, would it be better to use create_time as last_tic? This way we can include the tokenizer time in the metrics.
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.
To fine grained stat the time cost in tokenizer, prefill, detokenizer stages, I think it's better to separate theses staged, the metrics added in this PR only care about the latencies in prefill and decode stages.