|
| 1 | +import random |
| 2 | +from typing import List, Union |
| 3 | +from lightllm.server.router.req_queue.base_queue import BaseQueue |
| 4 | +from lightllm.server.router.batch import Batch, Req |
| 5 | +from lightllm.utils.log_utils import init_logger |
| 6 | +from .base import DpBalancer |
| 7 | + |
| 8 | +logger = init_logger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class DpBsBalancer(DpBalancer): |
| 12 | + """ |
| 13 | + This balancer is main to balance the batch size of each dp rank. |
| 14 | + Because, for dp mode, if it exists a dp rank without any request, it will |
| 15 | + padding a request and cause the waste of GPU compute resource. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, dp_size_in_node: int, inner_queues: List[BaseQueue]): |
| 19 | + super().__init__(dp_size_in_node, inner_queues) |
| 20 | + |
| 21 | + def assign_reqs_to_dp(self, current_batch: Batch, reqs_waiting_for_dp_index: List[List[Req]]) -> None: |
| 22 | + if len(reqs_waiting_for_dp_index) == 0: |
| 23 | + return |
| 24 | + # calculate the total load of each dp rank |
| 25 | + all_dp_req_num = [0 for _ in range(self.dp_size_in_node)] |
| 26 | + if current_batch is not None: |
| 27 | + all_dp_req_num = current_batch.get_all_dp_req_num() |
| 28 | + total_load_per_dp = [ |
| 29 | + all_dp_req_num[i] + len(self.inner_queues[i].waiting_req_list) for i in range(self.dp_size_in_node) |
| 30 | + ] |
| 31 | + for req_group in reqs_waiting_for_dp_index: |
| 32 | + # find the dp rank with minimum load |
| 33 | + min_load = min(total_load_per_dp) |
| 34 | + select_dp_indexes = [i for i in range(self.dp_size_in_node) if total_load_per_dp[i] == min_load] |
| 35 | + suggested_dp_index = random.choice(select_dp_indexes) |
| 36 | + |
| 37 | + # assign the request to the dp rank and update the load count |
| 38 | + for req in req_group: |
| 39 | + req.sample_params.suggested_dp_index = suggested_dp_index |
| 40 | + self.inner_queues[suggested_dp_index].extend(req_group) |
| 41 | + # update the load count for this dp rank |
| 42 | + total_load_per_dp[suggested_dp_index] += len(req_group) |
| 43 | + |
| 44 | + reqs_waiting_for_dp_index.clear() |
| 45 | + return |
0 commit comments