policy.py 965 Bytes
Newer Older
1
2
from collections import deque
from typing import Deque
3

Woosuk Kwon's avatar
Woosuk Kwon committed
4
from vllm.sequence import SequenceGroup
5
6
7
8
9
10
11
12
13
14
15
16
17
18


class Policy:

    def get_priority(
        self,
        now: float,
        seq_group: SequenceGroup,
    ) -> float:
        raise NotImplementedError

    def sort_by_priority(
        self,
        now: float,
19
20
21
22
23
24
25
26
        seq_groups: Deque[SequenceGroup],
    ) -> Deque[SequenceGroup]:
        return deque(
            sorted(
                seq_groups,
                key=lambda seq_group: self.get_priority(now, seq_group),
                reverse=True,
            ))
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


class FCFS(Policy):

    def get_priority(
        self,
        now: float,
        seq_group: SequenceGroup,
    ) -> float:
        return now - seq_group.arrival_time


class PolicyFactory:

    _POLICY_REGISTRY = {
        'fcfs': FCFS,
    }

    @classmethod
    def get_policy(cls, policy_name: str, **kwargs) -> Policy:
        return cls._POLICY_REGISTRY[policy_name](**kwargs)