Skip to content

Commit

Permalink
Do not use longest prefix matching when #queue-req is large (#1896)
Browse files Browse the repository at this point in the history
  • Loading branch information
merrymercy authored Nov 3, 2024
1 parent 6aed044 commit efbc116
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions python/sglang/srt/managers/schedule_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ def __init__(self, policy: str, tree_cache: BasePrefixCache):
self.tree_cache = tree_cache

def calc_priority(self, waiting_queue: List[Req]):
if len(waiting_queue) > 128 and self.policy == "lpm":
# Turn off the expensive prefix matching and sorting when the #queue is large.
policy = "fcfs"
else:
policy = self.policy

# Compute matched prefix length
prefix_computed = False
if self.policy == "lpm" or self.policy == "dfs-weight":
if policy == "lpm" or policy == "dfs-weight":
for r in waiting_queue:
# NOTE: the prefix_indices must always be aligned with last_node
r.prefix_indices, r.last_node = self.tree_cache.match_prefix(
Expand All @@ -56,18 +62,18 @@ def calc_priority(self, waiting_queue: List[Req]):

prefix_computed = True

if self.policy == "lpm":
if policy == "lpm":
# Longest Prefix Match
waiting_queue.sort(key=lambda x: -len(x.prefix_indices))
elif self.policy == "fcfs":
elif policy == "fcfs":
# first come first serve
pass
elif self.policy == "lof":
elif policy == "lof":
# longest output first
waiting_queue.sort(key=lambda x: -x.sampling_params.max_new_tokens)
elif self.policy == "random":
elif policy == "random":
random.shuffle(waiting_queue)
elif self.policy == "dfs-weight":
elif policy == "dfs-weight":
last_node_to_reqs = defaultdict(list)
for req in waiting_queue:
last_node_to_reqs[req.last_node].append(req)
Expand All @@ -85,7 +91,7 @@ def calc_priority(self, waiting_queue: List[Req]):
waiting_queue,
)
else:
raise ValueError(f"Unknown schedule_policy: {self.policy}")
raise ValueError(f"Unknown schedule_policy: {policy=}")

return prefix_computed

Expand Down

0 comments on commit efbc116

Please sign in to comment.