mirror of
https://git.datalinker.icu/vllm-project/vllm.git
synced 2026-05-24 07:11:18 +08:00
linting
Signed-off-by: Pr0Wh1teGivee <calvin_zhu0210@outlook.com> Signed-off-by: weichen <calvin_zhu0210@outlook.com>
This commit is contained in:
parent
e14d347982
commit
379eabac7f
@ -1,31 +1,36 @@
|
|||||||
from typing import List
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||||
|
import math
|
||||||
|
|
||||||
from vllm.logger import init_logger
|
from vllm.logger import init_logger
|
||||||
|
|
||||||
import math
|
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ScoreDim:
|
class ScoreDim:
|
||||||
"""
|
"""
|
||||||
Normalized scoring dimension.
|
Normalized scoring dimension.
|
||||||
"""
|
"""
|
||||||
def __init__(self, name: str, median: float, norm_scale=0.0, weight=0.5, reverse=False):
|
|
||||||
|
def __init__(
|
||||||
|
self, name: str, median: float, norm_scale=0.0, weight=0.5, reverse=False
|
||||||
|
):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.median = median
|
self.median = median
|
||||||
if norm_scale != 0.0:
|
if norm_scale != 0.0:
|
||||||
self.norm_scale = norm_scale
|
self.norm_scale = norm_scale
|
||||||
else:
|
else:
|
||||||
self.norm_scale = 1/median
|
self.norm_scale = 1 / median
|
||||||
self.weight = weight
|
self.weight = weight
|
||||||
self.reverse = reverse
|
self.reverse = reverse
|
||||||
|
|
||||||
|
|
||||||
class NormalizedScorer:
|
class NormalizedScorer:
|
||||||
"""
|
"""
|
||||||
Normalize unbounded N-dimensional values into a composite score using the Sigmoid function.
|
Normalize unbounded N-dimensional values into a composite score using the Sigmoid function.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dim_list: List[ScoreDim]) -> None:
|
def __init__(self, dim_list: list[ScoreDim]) -> None:
|
||||||
"""
|
"""
|
||||||
:param dim_list: Scoring dimensions; each dimension must define a median reference point, scaling factor, and weight.
|
:param dim_list: Scoring dimensions; each dimension must define a median reference point, scaling factor, and weight.
|
||||||
"""
|
"""
|
||||||
@ -50,32 +55,48 @@ class NormalizedScorer:
|
|||||||
Smaller value → higher score → use inverse Sigmoid.
|
Smaller value → higher score → use inverse Sigmoid.
|
||||||
"""
|
"""
|
||||||
if len(dims) > self.dim_count:
|
if len(dims) > self.dim_count:
|
||||||
raise ValueError(f"Dim num({len(dims)}) exceeds max num dim({self.dim_count})")
|
raise ValueError(
|
||||||
|
f"Dim num({len(dims)}) exceeds max num dim({self.dim_count})"
|
||||||
|
)
|
||||||
|
|
||||||
final_score = 0.0
|
final_score = 0.0
|
||||||
for idx, dim_value in enumerate(dims):
|
for idx, dim_value in enumerate(dims):
|
||||||
dim_info = self.dim_list[idx]
|
dim_info = self.dim_list[idx]
|
||||||
if dim_info.reverse:
|
if dim_info.reverse:
|
||||||
score = self._inv_sigmoid_normalize(dim_value, dim_info.median, dim_info.norm_scale)
|
score = self._inv_sigmoid_normalize(
|
||||||
|
dim_value, dim_info.median, dim_info.norm_scale
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
score = self._sigmoid_normalize(dim_value, dim_info.median, dim_info.norm_scale)
|
score = self._sigmoid_normalize(
|
||||||
|
dim_value, dim_info.median, dim_info.norm_scale
|
||||||
|
)
|
||||||
logger.debug(f"{dim_info.name}({dim_info.reverse}) : {score:.10f}")
|
logger.debug(f"{dim_info.name}({dim_info.reverse}) : {score:.10f}")
|
||||||
|
|
||||||
# Weighted summation.
|
# Weighted summation.
|
||||||
final_score += score * dim_info.weight
|
final_score += score * dim_info.weight
|
||||||
return max(0.0, min(1.0, final_score)) # Clamp to [0, 1].
|
return max(0.0, min(1.0, final_score)) # Clamp to [0, 1].
|
||||||
|
|
||||||
|
|
||||||
class TimeAndLengthScorer(NormalizedScorer):
|
class TimeAndLengthScorer(NormalizedScorer):
|
||||||
"""
|
"""
|
||||||
Scorer for time and length dimensions; defaults to forward scoring with equal weights (0.5 each).
|
Scorer for time and length dimensions; defaults to forward scoring with equal weights (0.5 each).
|
||||||
"""
|
"""
|
||||||
def __init__(self,
|
|
||||||
time_median, length_median,
|
def __init__(
|
||||||
time_scale=0.0, length_scale=0.0,
|
self,
|
||||||
time_weight=0.5, length_weight=0.5,
|
time_median,
|
||||||
reverse_time=False, reverse_len=False) -> None:
|
length_median,
|
||||||
dim_list = [ScoreDim("time", time_median, time_scale, time_weight, reverse_time),
|
time_scale=0.0,
|
||||||
ScoreDim("length", length_median, length_scale, length_weight, reverse_len)]
|
length_scale=0.0,
|
||||||
|
time_weight=0.5,
|
||||||
|
length_weight=0.5,
|
||||||
|
reverse_time=False,
|
||||||
|
reverse_len=False,
|
||||||
|
) -> None:
|
||||||
|
dim_list = [
|
||||||
|
ScoreDim("time", time_median, time_scale, time_weight, reverse_time),
|
||||||
|
ScoreDim("length", length_median, length_scale, length_weight, reverse_len),
|
||||||
|
]
|
||||||
super().__init__(dim_list)
|
super().__init__(dim_list)
|
||||||
|
|
||||||
def score(self, time: float, length: float) -> float:
|
def score(self, time: float, length: float) -> float:
|
||||||
|
|||||||
@ -1,28 +1,44 @@
|
|||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||||
|
import time
|
||||||
from functools import total_ordering
|
from functools import total_ordering
|
||||||
|
|
||||||
from vllm.v1.core.sched.policy.normalized_scorer import TimeAndLengthScorer
|
from vllm.v1.core.sched.policy.normalized_scorer import TimeAndLengthScorer
|
||||||
import time
|
|
||||||
|
|
||||||
TimeAndLengthScorer_Instance = None
|
TimeAndLengthScorer_Instance = None
|
||||||
|
|
||||||
if TimeAndLengthScorer_Instance == None:
|
if TimeAndLengthScorer_Instance == None:
|
||||||
TimeAndLengthScorer_Instance = TimeAndLengthScorer(time_median=5, time_weight=0.5, length_median=32 * 1024,
|
TimeAndLengthScorer_Instance = TimeAndLengthScorer(
|
||||||
length_weight=0.5, reverse_len=True)
|
time_median=5,
|
||||||
|
time_weight=0.5,
|
||||||
|
length_median=32 * 1024,
|
||||||
|
length_weight=0.5,
|
||||||
|
reverse_len=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@total_ordering
|
@total_ordering
|
||||||
class WeightedScoreSorter:
|
class WeightedScoreSorter:
|
||||||
def __init__(self, request_length: int, request_arrival_time: float, request_slo_requirement: list = None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
request_length: int,
|
||||||
|
request_arrival_time: float,
|
||||||
|
request_slo_requirement: list = None,
|
||||||
|
):
|
||||||
self.request_length = request_length
|
self.request_length = request_length
|
||||||
self.request_arrival_time = request_arrival_time
|
self.request_arrival_time = request_arrival_time
|
||||||
self.request_slo_requirement = request_slo_requirement
|
self.request_slo_requirement = request_slo_requirement
|
||||||
self.__update_stats()
|
self.__update_stats()
|
||||||
|
|
||||||
def __lt__(self, other_request_weighted_score: 'WeightedScoreSorter') -> bool:
|
def __lt__(self, other_request_weighted_score: "WeightedScoreSorter") -> bool:
|
||||||
self.__update_stats()
|
self.__update_stats()
|
||||||
return self.weighted_score > other_request_weighted_score.weighted_score
|
return self.weighted_score > other_request_weighted_score.weighted_score
|
||||||
|
|
||||||
def __eq__(self, other_request_weighted_score: 'WeightedScoreSorter') -> bool:
|
def __eq__(self, other_request_weighted_score: "WeightedScoreSorter") -> bool:
|
||||||
return self.weighted_score == other_request_weighted_score.weighted_score
|
return self.weighted_score == other_request_weighted_score.weighted_score
|
||||||
|
|
||||||
def __update_stats(self):
|
def __update_stats(self):
|
||||||
self.wait_time = time.time() - self.request_arrival_time
|
self.wait_time = time.time() - self.request_arrival_time
|
||||||
self.weighted_score = TimeAndLengthScorer_Instance.score(self.wait_time, self.request_length)
|
self.weighted_score = TimeAndLengthScorer_Instance.score(
|
||||||
|
self.wait_time, self.request_length
|
||||||
|
)
|
||||||
|
|||||||
@ -7,8 +7,8 @@ from collections import deque
|
|||||||
from collections.abc import Iterable, Iterator
|
from collections.abc import Iterable, Iterator
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from vllm.v1.request import Request
|
|
||||||
from vllm.v1.core.sched.policy.weighted_score_softer import WeightedScoreSorter
|
from vllm.v1.core.sched.policy.weighted_score_softer import WeightedScoreSorter
|
||||||
|
from vllm.v1.request import Request
|
||||||
|
|
||||||
|
|
||||||
class SchedulingPolicy(Enum):
|
class SchedulingPolicy(Enum):
|
||||||
@ -212,7 +212,7 @@ class PriorityRequestQueue(RequestQueue):
|
|||||||
class SJFRequestQueue(RequestQueue):
|
class SJFRequestQueue(RequestQueue):
|
||||||
"""
|
"""
|
||||||
A SJF queue that supports heap operations.
|
A SJF queue that supports heap operations.
|
||||||
|
|
||||||
Requests with a larger value of weighted score value are processed first.
|
Requests with a larger value of weighted score value are processed first.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -221,8 +221,15 @@ class SJFRequestQueue(RequestQueue):
|
|||||||
|
|
||||||
def add_request(self, request: Request) -> None:
|
def add_request(self, request: Request) -> None:
|
||||||
"""Add a request to the queue according to SJF policy."""
|
"""Add a request to the queue according to SJF policy."""
|
||||||
heapq.heappush(self._heap,
|
heapq.heappush(
|
||||||
(WeightedScoreSorter(len(request.prompt_token_ids), request.arrival_time), request))
|
self._heap,
|
||||||
|
(
|
||||||
|
WeightedScoreSorter(
|
||||||
|
len(request.prompt_token_ids), request.arrival_time
|
||||||
|
),
|
||||||
|
request,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
def pop_request(self) -> Request:
|
def pop_request(self) -> Request:
|
||||||
"""Pop a request from the queue according to SJF policy."""
|
"""Pop a request from the queue according to SJF policy."""
|
||||||
@ -240,14 +247,14 @@ class SJFRequestQueue(RequestQueue):
|
|||||||
|
|
||||||
def prepend_request(self, request: Request) -> None:
|
def prepend_request(self, request: Request) -> None:
|
||||||
"""Add a request to the queue according to SJF policy.
|
"""Add a request to the queue according to SJF policy.
|
||||||
|
|
||||||
Note: In a SJF queue, there is no concept of prepending to the
|
Note: In a SJF queue, there is no concept of prepending to the
|
||||||
front. Requests are ordered by (priority, arrival_time)."""
|
front. Requests are ordered by (priority, arrival_time)."""
|
||||||
self.add_request(request)
|
self.add_request(request)
|
||||||
|
|
||||||
def prepend_requests(self, requests: RequestQueue) -> None:
|
def prepend_requests(self, requests: RequestQueue) -> None:
|
||||||
"""Add all requests from another queue according to SJF policy.
|
"""Add all requests from another queue according to SJF policy.
|
||||||
|
|
||||||
Note: In a SJF queue, there is no concept of prepending to the
|
Note: In a SJF queue, there is no concept of prepending to the
|
||||||
front. Requests are ordered by weighted score."""
|
front. Requests are ordered by weighted score."""
|
||||||
for request in requests:
|
for request in requests:
|
||||||
@ -261,8 +268,7 @@ class SJFRequestQueue(RequestQueue):
|
|||||||
def remove_requests(self, requests: Iterable[Request]) -> None:
|
def remove_requests(self, requests: Iterable[Request]) -> None:
|
||||||
"""Remove multiple specific requests from the queue."""
|
"""Remove multiple specific requests from the queue."""
|
||||||
requests_to_remove = set(requests)
|
requests_to_remove = set(requests)
|
||||||
self._heap = [(ws, r) for ws , r in self._heap
|
self._heap = [(ws, r) for ws, r in self._heap if r not in requests_to_remove]
|
||||||
if r not in requests_to_remove]
|
|
||||||
heapq.heapify(self._heap)
|
heapq.heapify(self._heap)
|
||||||
|
|
||||||
def __bool__(self) -> bool:
|
def __bool__(self) -> bool:
|
||||||
@ -282,7 +288,7 @@ class SJFRequestQueue(RequestQueue):
|
|||||||
|
|
||||||
def __reversed__(self) -> Iterator[Request]:
|
def __reversed__(self) -> Iterator[Request]:
|
||||||
"""Iterate over the queue in reverse SJF order."""
|
"""Iterate over the queue in reverse SJF order."""
|
||||||
return reversed(list(self))
|
return reversed(list(self))
|
||||||
|
|
||||||
|
|
||||||
def create_request_queue(policy: SchedulingPolicy) -> RequestQueue:
|
def create_request_queue(policy: SchedulingPolicy) -> RequestQueue:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user