|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from .dynamic_batching.io_struct import Batch, Req, RequestOutput |
| 4 | +from .manager import DynamicBatchManager |
| 5 | +from .tensor_parallel import TPInferEngine |
| 6 | + |
| 7 | + |
| 8 | +class Async_DynamicBatchManager(DynamicBatchManager): |
| 9 | + def __init__( |
| 10 | + self, |
| 11 | + tp_engine: TPInferEngine, |
| 12 | + max_total_token_num: int, |
| 13 | + batch_max_tokens: int, |
| 14 | + model: str, |
| 15 | + tokenizer=None, |
| 16 | + eos_id=None, |
| 17 | + log_stats=True, |
| 18 | + log_stats_interval=10, |
| 19 | + running_batch: Batch = None, |
| 20 | + waiting_req_list: List = [], |
| 21 | + ): |
| 22 | + """ |
| 23 | + Args: tp_engine : The tp engine that dynamic batch manager hold, defined before dynamic batch manager |
| 24 | + max_total_token_num : max_total_token_num for memory manager, default to: max batch size * (max input len + max output len) |
| 25 | + batch_max_tokens : max tokens of one batch, default to (max input + output len) * num_requests |
| 26 | + running_max_req_size : max request size of running batch, equals to MAX_BATCH_SIZE of tp engine |
| 27 | + eos_id : The end token of a seq |
| 28 | + model: the model weight dir path, the app will load config, weights and tokenizer from this dir |
| 29 | + log_stats : whether to log stats |
| 30 | + log_stats_interval : log stats interval |
| 31 | + running_batch : running batch |
| 32 | + waiting_req_list : list of waiting requests, initialized before dynamic batch manager |
| 33 | + """ |
| 34 | + super().__init__( |
| 35 | + tp_engine, |
| 36 | + max_total_token_num, |
| 37 | + batch_max_tokens, |
| 38 | + model, |
| 39 | + tokenizer, |
| 40 | + eos_id, |
| 41 | + log_stats, |
| 42 | + log_stats_interval, |
| 43 | + running_batch, |
| 44 | + waiting_req_list, |
| 45 | + ) |
| 46 | + |
| 47 | + def _step(self): |
| 48 | + """ |
| 49 | + Logic for handling requests |
| 50 | + """ |
| 51 | + has_new_finished = False |
| 52 | + if self.running_batch is None: |
| 53 | + new_batch = self.req_queue.generate_new_batch(self.running_batch) |
| 54 | + if new_batch is not None: |
| 55 | + self.stats_tool.count_prompt_tokens(new_batch) |
| 56 | + self.running_batch = new_batch |
| 57 | + has_new_finished, outputs = self._prefill_batch(self.running_batch) |
| 58 | + self._filter_runing_batch() |
| 59 | + self.has_wait_tokens = 0 |
| 60 | + |
| 61 | + else: |
| 62 | + if self.has_wait_tokens < self.max_wait_tokens: |
| 63 | + self.stats_tool.count_output_tokens(self.running_batch) |
| 64 | + has_new_finished, outputs = self._decode_batch(self.running_batch) |
| 65 | + self._filter_runing_batch() |
| 66 | + self.has_wait_tokens += 1 |
| 67 | + |
| 68 | + else: |
| 69 | + new_mini_batch = self.req_queue.generate_new_batch(self.running_batch) |
| 70 | + if new_mini_batch is not None: |
| 71 | + self.stats_tool.count_prompt_tokens(new_mini_batch) |
| 72 | + has_new_finished, outputs = self._prefill_batch(new_mini_batch) |
| 73 | + if not new_mini_batch.is_clear(): |
| 74 | + self._merge_batch(self.running_batch, new_mini_batch) |
| 75 | + self.running_batch.merge(new_mini_batch) |
| 76 | + self.has_wait_tokens = 0 |
| 77 | + |
| 78 | + else: |
| 79 | + self.stats_tool.count_output_tokens(self.running_batch) |
| 80 | + has_new_finished, outputs = self._decode_batch(self.running_batch) |
| 81 | + self._filter_runing_batch() |
| 82 | + self.has_wait_tokens += 1 |
| 83 | + |
| 84 | + if has_new_finished: |
| 85 | + return outputs |
| 86 | + return None |
| 87 | + |
| 88 | + def _prefill_batch(self, batch): |
| 89 | + """ |
| 90 | + For all batches, no matter it is a new batch or a mini batch, we need to do prefill first. |
| 91 | + """ |
| 92 | + self._init_batch(batch) |
| 93 | + |
| 94 | + # TODO: figure out if cache and batch id is needed |
| 95 | + ans = self.engine._prefill_batch(batch.batch_id) |
| 96 | + req_to_out_token_id = ans |
| 97 | + self._add_token_id_to_req(batch, req_to_out_token_id) |
| 98 | + has_new_finished_req = batch.mark_finished_req(self.eos_id, self.engine.max_output_len) |
| 99 | + outputs = self._handle_finish_req(batch, has_new_finished_req) |
| 100 | + return has_new_finished_req, outputs |
| 101 | + # delete finished reqs |
| 102 | + |
| 103 | + def _decode_batch(self, batch: Batch): |
| 104 | + """ |
| 105 | + Decoding process |
| 106 | + """ |
| 107 | + ans = self.engine._decode_batch(batch.batch_id) |
| 108 | + req_to_out_token_id = ans |
| 109 | + self._add_token_id_to_req(batch, req_to_out_token_id) |
| 110 | + has_new_finished_req = batch.mark_finished_req(self.eos_id, self.engine.max_output_len) |
| 111 | + outputs = self._handle_finish_req(batch, has_new_finished_req) |
| 112 | + return has_new_finished_req, outputs |
| 113 | + |
| 114 | + def _handle_finish_req(self, batch: Batch, has_new_finished_req): |
| 115 | + if has_new_finished_req: |
| 116 | + finished_reqs = batch.filter_finished() |
| 117 | + if batch.is_clear(): |
| 118 | + self._remove_batch(batch) |
| 119 | + else: |
| 120 | + self._filter_batch(batch) |
| 121 | + return self._output_process(finished_reqs) |
| 122 | + return None |
| 123 | + |
| 124 | + def _output_process(self, finished_reqs: List[Req]): |
| 125 | + """ |
| 126 | + Process the output of a batch. |
| 127 | + """ |
| 128 | + outputs = [] |
| 129 | + for req in finished_reqs: |
| 130 | + output = self.tokenizer.decode(req.output_ids) |
| 131 | + outputs.append(RequestOutput(req.request_id, req.prompts, req.prompt_ids, output)) |
| 132 | + return outputs |
| 133 | + |
| 134 | + |
| 135 | +def start_dynamic_batching(args, tp_engine, waiting_req_list): |
| 136 | + try: |
| 137 | + batch_manager = Async_DynamicBatchManager( |
| 138 | + tp_engine=tp_engine, |
| 139 | + max_total_token_num=args.max_total_token_num, |
| 140 | + batch_max_tokens=args.batch_max_tokens, |
| 141 | + eos_id=args.eos_id, |
| 142 | + model=args.model, |
| 143 | + log_stats=not args.disable_log_stats, |
| 144 | + log_stats_interval=args.log_stats_interval, |
| 145 | + waiting_req_list=waiting_req_list, |
| 146 | + ) |
| 147 | + |
| 148 | + except Exception: |
| 149 | + raise Exception |
| 150 | + |
| 151 | + return batch_manager |
0 commit comments