|
| 1 | +import torch |
| 2 | +from .impl import ChunkedPrefillBackend |
| 3 | +from typing import List |
| 4 | +from lightllm.server.router.model_infer.infer_batch import InferReq |
| 5 | +from lightllm.server.router.model_infer.mode_backend.pre import prepare_prefill_inputs |
| 6 | +from lightllm.server.router.model_infer.mode_backend.generic_post_process import sample |
| 7 | +from lightllm.server.router.model_infer.mode_backend.overlap_events import OverlapEventPack |
| 8 | + |
| 9 | + |
| 10 | +class ReturnPromptLogProbBackend(ChunkedPrefillBackend): |
| 11 | + def __init__(self) -> None: |
| 12 | + super().__init__() |
| 13 | + self.prefill = self.return_all_prompt_logprobs_prefill |
| 14 | + return |
| 15 | + |
| 16 | + def return_all_prompt_logprobs_prefill( |
| 17 | + self, |
| 18 | + event_pack: OverlapEventPack, |
| 19 | + prefill_reqs: List[InferReq]): |
| 20 | + |
| 21 | + # 在 return all_prompt_logprobs 的模式下,不能启用 dynamic prompt cache |
| 22 | + assert self.radix_cache is None |
| 23 | + assert self.disable_chunked_prefill is True |
| 24 | + |
| 25 | + model_input, run_reqs = prepare_prefill_inputs( |
| 26 | + prefill_reqs, is_chuncked_mode=not self.disable_chunked_prefill, is_multimodal=self.is_multimodal |
| 27 | + ) |
| 28 | + |
| 29 | + model_output = self.model.forward(model_input) |
| 30 | + prompt_all_logits = model_output.logits |
| 31 | + |
| 32 | + input_ids = model_input.input_ids |
| 33 | + b_ready_cache_len = model_input.b_ready_cache_len |
| 34 | + b_seq_len = model_input.b_seq_len |
| 35 | + last_index = torch.cumsum(b_seq_len, dim=0, dtype=torch.long) - 1 |
| 36 | + logits = prompt_all_logits[last_index, :] |
| 37 | + |
| 38 | + b_q_seq_len = b_seq_len - b_ready_cache_len |
| 39 | + b_start_loc = torch.cumsum(b_q_seq_len, dim=0, dtype=torch.long) - b_q_seq_len |
| 40 | + b_start_loc = b_start_loc.cpu().numpy() |
| 41 | + b_q_seq_len = b_q_seq_len.cpu().numpy() |
| 42 | + |
| 43 | + for req_obj, start_loc, q_seq_len in zip(run_reqs, b_start_loc, b_q_seq_len): |
| 44 | + req_obj: InferReq = req_obj |
| 45 | + cur_ids: torch.Tensor = input_ids[start_loc : start_loc + q_seq_len] |
| 46 | + cur_logits = prompt_all_logits[start_loc : start_loc + q_seq_len] |
| 47 | + cur_logprobs = torch.log_softmax(cur_logits, dim=-1, dtype=torch.float)[0:-1, :] |
| 48 | + cur_logprobs = torch.gather(cur_logprobs, dim=1, index=cur_ids[1:].view(-1, 1)).detach().cpu().numpy() |
| 49 | + |
| 50 | + if req_obj.shm_req.input_len > 1: |
| 51 | + req_obj.shm_req.shm_logprobs.arr[1 : req_obj.shm_req.input_len] = cur_logprobs.flatten() |
| 52 | + |
| 53 | + if self.prefill_mask_func is not None: |
| 54 | + self.prefill_mask_func(run_reqs, logits) |
| 55 | + |
| 56 | + next_token_ids, next_token_probs = sample(logits, run_reqs, self.eos_id) |
| 57 | + next_token_ids = next_token_ids.detach().cpu().numpy() |
| 58 | + next_token_logprobs = torch.log(next_token_probs).detach().cpu().numpy() |
| 59 | + |
| 60 | + update_packs = self._pre_post_handle(run_reqs, is_chuncked_mode=not self.disable_chunked_prefill) |
| 61 | + self._post_handle( |
| 62 | + run_reqs=run_reqs, |
| 63 | + next_token_ids=next_token_ids, |
| 64 | + next_token_logprobs=next_token_logprobs, |
| 65 | + run_reqs_update_packs=update_packs, |
| 66 | + extra_post_req_handle_func=self.extra_post_req_handle_func, |
| 67 | + ) |
| 68 | + return |
0 commit comments