|
| 1 | +import bisect |
| 2 | +from collections import deque |
| 3 | +import random |
| 4 | +from typing import List, Tuple |
| 5 | +import numpy as np |
| 6 | +from ...batch import Batch, Req |
| 7 | +from .impl import ChunkedPrefillQueue |
| 8 | + |
| 9 | + |
| 10 | +class PastFutureQueue(ChunkedPrefillQueue): |
| 11 | + WINDOW_SIZE = 200 |
| 12 | + MINIMUM_SAMPLES = 200 |
| 13 | + MAXIMUM_LISTS = 5 |
| 14 | + REVERSED = 0.05 |
| 15 | + COMPLIANCE_IS_BUSY_FLAG = False |
| 16 | + |
| 17 | + def __init__(self, args, router, dp_index, dp_size_in_node) -> None: |
| 18 | + super().__init__(args, router, dp_index, dp_size_in_node) |
| 19 | + initial_len = args.router_max_new_token_len |
| 20 | + self.history_output_len = deque([initial_len] * (self.WINDOW_SIZE // 2), maxlen=self.WINDOW_SIZE) |
| 21 | + |
| 22 | + def _sample_cache_list(self, reqs: List[Req], is_busy, samples=1) -> List[List[Tuple[int, int]]]: |
| 23 | + cache_len_lists = [[] for _ in range(samples)] |
| 24 | + his_Lo = sorted(self.history_output_len) |
| 25 | + for req in reqs: |
| 26 | + dl = req.shm_cur_output_len |
| 27 | + pos = bisect.bisect(his_Lo, dl) |
| 28 | + |
| 29 | + sample_range = [dl] + his_Lo[pos:] + [req.sample_params.max_new_tokens] # at least 2 value |
| 30 | + |
| 31 | + for i in range(samples): |
| 32 | + random_p = np.random.random() * (len(sample_range) - 1) |
| 33 | + l_pos = int(random_p) |
| 34 | + l_val, r_val = sample_range[l_pos : l_pos + 2] |
| 35 | + |
| 36 | + # Linear interpolation |
| 37 | + sampled = round(l_val + (r_val - l_val) * (random_p - l_pos)) |
| 38 | + cache_len_lists[i].append( |
| 39 | + req.get_tuple_tokens(is_busy and self.COMPLIANCE_IS_BUSY_FLAG, sampled, has_out_len_factor=1.0) |
| 40 | + ) |
| 41 | + |
| 42 | + return cache_len_lists |
| 43 | + |
| 44 | + def _calc_max_token_num_needed(self, cache_len_list: List[Tuple[int, int]]) -> int: |
| 45 | + cache_len_list.sort(key=lambda x: -x[1]) |
| 46 | + |
| 47 | + left_out_len_array = np.array([e[1] for e in cache_len_list]) |
| 48 | + has_run_len_array = np.array([e[0] for e in cache_len_list]) |
| 49 | + cum_run_len_array = np.cumsum(has_run_len_array) |
| 50 | + size_array = np.arange(1, len(cache_len_list) + 1, 1) |
| 51 | + |
| 52 | + need_max_token_num = (left_out_len_array * size_array + cum_run_len_array).max() |
| 53 | + return need_max_token_num |
| 54 | + |
| 55 | + def _init_cache_list(self, current_batch: Batch, is_busy): |
| 56 | + if current_batch is not None: |
| 57 | + n_lists = min(self.MAXIMUM_LISTS, int(self.MINIMUM_SAMPLES / len(current_batch.reqs)) + 1) |
| 58 | + local_reqs = [req for req in current_batch.reqs if req.sample_params.suggested_dp_index == self.dp_index] |
| 59 | + self._cache_len_lists = self._sample_cache_list(local_reqs, is_busy, samples=n_lists) |
| 60 | + else: |
| 61 | + self._cache_len_lists = [[]] |
| 62 | + self.cache_len_list = self._cache_len_lists[0] # keep compatibility |
| 63 | + |
| 64 | + def _update_cache_len_list(self, req: Req, is_busy): |
| 65 | + need_max_token_nums = [] |
| 66 | + for li in self._cache_len_lists: |
| 67 | + newreq_output_len_sample = random.choice(self.history_output_len) |
| 68 | + li.append( |
| 69 | + req.get_tuple_tokens( |
| 70 | + is_busy and self.COMPLIANCE_IS_BUSY_FLAG, newreq_output_len_sample, has_out_len_factor=1.0 |
| 71 | + ) |
| 72 | + ) |
| 73 | + need_max_token_nums.append(self._calc_max_token_num_needed(li)) |
| 74 | + need_max_token_num = np.max(need_max_token_nums) |
| 75 | + return need_max_token_num |
| 76 | + |
| 77 | + def record_finished_len_from_batch(self, batch: Batch): |
| 78 | + for req in batch.reqs: |
| 79 | + if req.shm_infer_released: |
| 80 | + self.history_output_len.append(req.shm_cur_output_len) |
0 commit comments