-
Notifications
You must be signed in to change notification settings - Fork 273
Dp balancer #991
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
shihaobai
wants to merge
7
commits into
main
Choose a base branch
from
dp_balancer
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
Dp balancer #991
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8eda8ab
dp balancer abstract
shihaobai 86df27c
add dp balancer for dp
shihaobai 54cd9ac
fix test
shihaobai 74cfa55
Merge branch 'main' into dp_balancer
shihaobai ea0ada4
update router
shihaobai d3f12d0
rename
shihaobai dc1e2f0
fix
shihaobai 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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from .dp_base_balancer import RoundRobinDpBalancer | ||
from typing import List | ||
from lightllm.server.router.req_queue.base_queue import BaseQueue | ||
from .dp_bs_balancer import DpBsBalancer | ||
|
||
|
||
def get_dp_balancer(args, dp_size_in_node: int, inner_queues: List[BaseQueue]): | ||
if args.dp_balancer == "round_robin": | ||
return RoundRobinDpBalancer(dp_size_in_node, inner_queues) | ||
elif args.dp_balancer == "bs_balancer": | ||
return DpBsBalancer(dp_size_in_node, inner_queues) | ||
else: | ||
raise ValueError(f"Invalid dp balancer: {args.dp_balancer}") |
65 changes: 65 additions & 0 deletions
65
lightllm/server/router/req_queue/dp_balancer/dp_base_balancer.py
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,65 @@ | ||
import random | ||
from abc import ABC, abstractmethod | ||
from typing import List, Union | ||
from lightllm.server.router.req_queue.base_queue import BaseQueue | ||
from lightllm.server.router.batch import Batch, Req | ||
from lightllm.utils.log_utils import init_logger | ||
|
||
logger = init_logger(__name__) | ||
|
||
|
||
class DpBalancer(ABC): | ||
""" | ||
DP负载均衡器基类 | ||
定义了负载均衡策略的接口,子类可以实现不同的负载均衡算法 | ||
""" | ||
|
||
def __init__(self, dp_size_in_node: int, inner_queues: List[BaseQueue]): | ||
self.dp_size_in_node = dp_size_in_node | ||
self.inner_queues = inner_queues | ||
self.pre_select_dp_index = self.dp_size_in_node - 1 | ||
|
||
@abstractmethod | ||
def assign_reqs_to_dp(self, current_batch: Batch, reqs_waiting_for_dp_index: List[Union[Req, List[Req]]]) -> None: | ||
pass | ||
|
||
|
||
class RoundRobinDpBalancer(DpBalancer): | ||
""" | ||
轮询负载均衡器 | ||
在队列长度最小的DP中进行轮询选择 | ||
""" | ||
|
||
def get_suggest_dp_index( | ||
self, | ||
) -> int: | ||
min_length = min(len(queue.waiting_req_list) for queue in self.inner_queues) | ||
select_dp_indexes = [ | ||
i for i, queue in enumerate(self.inner_queues) if len(queue.waiting_req_list) == min_length | ||
] | ||
|
||
# 如果没有可选择的索引,随机选择一个 | ||
if not select_dp_indexes: | ||
self.pre_select_dp_index = random.randint(0, self.dp_size_in_node - 1) | ||
return self.pre_select_dp_index | ||
|
||
# 轮询选择 | ||
for i in range(self.dp_size_in_node): | ||
next_dp_index = (self.pre_select_dp_index + i + 1) % self.dp_size_in_node | ||
if next_dp_index in select_dp_indexes: | ||
self.pre_select_dp_index = next_dp_index | ||
return self.pre_select_dp_index | ||
|
||
self.pre_select_dp_index = random.choice(select_dp_indexes) | ||
return self.pre_select_dp_index | ||
|
||
def assign_reqs_to_dp(self, current_batch: Batch, reqs_waiting_for_dp_index: List[Union[Req, List[Req]]]) -> None: | ||
for req_group in reqs_waiting_for_dp_index: | ||
suggested_dp_index = self.get_suggest_dp_index() | ||
if not isinstance(req_group, list): | ||
req_group = [req_group] | ||
for req in req_group: | ||
req.sample_params.suggested_dp_index = suggested_dp_index | ||
self.inner_queues[suggested_dp_index].append(req) | ||
reqs_waiting_for_dp_index.clear() | ||
return |
63 changes: 63 additions & 0 deletions
63
lightllm/server/router/req_queue/dp_balancer/dp_bs_balancer.py
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,63 @@ | ||
from typing import List, Union | ||
from lightllm.server.router.req_queue.base_queue import BaseQueue | ||
from lightllm.server.router.batch import Batch, Req | ||
from lightllm.utils.log_utils import init_logger | ||
from .dp_base_balancer import DpBalancer | ||
|
||
logger = init_logger(__name__) | ||
|
||
|
||
class DpBsBalancer(DpBalancer): | ||
""" | ||
This balancer is main to balance the batch size of each dp rank. | ||
Because, for dp mode, if it exists a dp rank without any request, it will | ||
padding a request and cause the waste of GPU compute resource. | ||
""" | ||
|
||
def __init__(self, dp_size_in_node: int, inner_queues: List[BaseQueue]): | ||
super().__init__(dp_size_in_node, inner_queues) | ||
|
||
def assign_reqs_to_dp(self, current_batch: Batch, reqs_waiting_for_dp_index: List[Union[Req, List[Req]]]) -> None: | ||
if len(reqs_waiting_for_dp_index) == 0: | ||
return | ||
# calculate the total load of each dp rank | ||
if current_batch is not None: | ||
all_dp_req_num = current_batch.get_all_dp_req_num() | ||
total_load_per_dp = [ | ||
all_dp_req_num[i] + len(self.inner_queues[i].waiting_req_list) for i in range(self.dp_size_in_node) | ||
] | ||
else: | ||
total_load_per_dp = [len(self.inner_queues[i].waiting_req_list) for i in range(self.dp_size_in_node)] | ||
for req_group in reqs_waiting_for_dp_index: | ||
# calculate the length of this request group | ||
if isinstance(req_group, list): | ||
req_length = len(req_group) | ||
else: | ||
req_length = 1 | ||
|
||
# find the dp rank with minimum load | ||
min_load = min(total_load_per_dp) | ||
select_dp_indexes = [i for i in range(self.dp_size_in_node) if total_load_per_dp[i] == min_load] | ||
|
||
# select the dp rank with the minimum load | ||
if len(select_dp_indexes) == 1: | ||
suggested_dp_index = select_dp_indexes[0] | ||
else: | ||
# if multiple dp ranks have the same minimum load, randomly select one | ||
import random | ||
|
||
suggested_dp_index = random.choice(select_dp_indexes) | ||
|
||
# assign the request to the dp rank and update the load count | ||
if not isinstance(req_group, list): | ||
req_group = [req_group] | ||
|
||
for req in req_group: | ||
req.sample_params.suggested_dp_index = suggested_dp_index | ||
self.inner_queues[suggested_dp_index].append(req) | ||
|
||
# update the load count for this dp rank | ||
total_load_per_dp[suggested_dp_index] += req_length | ||
|
||
reqs_waiting_for_dp_index.clear() | ||
return |
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.
When an exception occurs during batch generation, using
raise e
can obscure the original stack trace. Using a bareraise
will preserve the original traceback, making debugging easier.