MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / _build_sample_logprobs

Method _build_sample_logprobs

fastdeploy/entrypoints/llm.py:396–437  ·  view source on GitHub ↗

Constructs a list of dictionaries mapping token IDs to Logprob objects, based on sliced LogprobsLists data (excluding the sampled token at index 0). Args: logprobs_lists (LogprobsLists): Contains top-k token IDs, logprobs, and sampled ranks. max_num

(self, logprobs_lists: LogprobsLists, topk_logprobs: int)

Source from the content-addressed store, hash-verified

394 return self.llm_engine.data_processor.process_logprob_response([token_id], clean_up_tokenization_spaces=False)
395
396 def _build_sample_logprobs(self, logprobs_lists: LogprobsLists, topk_logprobs: int) -> list[dict[int, Logprob]]:
397 """
398 Constructs a list of dictionaries mapping token IDs to Logprob objects,
399 based on sliced LogprobsLists data (excluding the sampled token at index 0).
400
401 Args:
402 logprobs_lists (LogprobsLists): Contains top-k token IDs, logprobs, and sampled ranks.
403 max_num (int): Maximum number of top logprobs to include (excluding sampled token at index 0).
404
405 Returns:
406 list[dict[int, Logprob]]: One dict per request, mapping token ID to Logprob.
407 """
408 try:
409 llm_logger.info(f"filter logprobs, topk_logprobs: {topk_logprobs}")
410 if not logprobs_lists.logprob_token_ids:
411 llm_logger.warning("Empty logprob_token_ids in LogprobsLists")
412 return None
413
414 available_topk = len(logprobs_lists.logprob_token_ids[0])
415 effective_topk_logprobs = min(topk_logprobs, available_topk)
416
417 if effective_topk_logprobs < 0:
418 llm_logger.warning(
419 f"Invalid effective_topk_logprobs={effective_topk_logprobs}, "
420 f"available_topk={available_topk}, topk_logprobs={topk_logprobs}; returning empty result."
421 )
422 return None
423
424 # sliced 0 ~ effective_topk_logprobs+1
425 sliced_logprobs_lists = logprobs_lists.slice_columns(0, effective_topk_logprobs + 1)
426 result = []
427 for token_ids, logprobs in zip(sliced_logprobs_lists.logprob_token_ids, sliced_logprobs_lists.logprobs):
428
429 logprob_dict = {
430 token_id: Logprob(logprob=logprob, rank=i + 1, decoded_token=self._decode_token(token_id))
431 for i, (token_id, logprob) in enumerate(zip(token_ids, logprobs))
432 }
433 result.append(logprob_dict)
434 return result
435
436 except Exception as e:
437 llm_logger.error(f"Error building sample logprobs from LogprobsLists: {e}, {str(traceback.format_exc())}")
438
439 def _build_prompt_logprobs(
440 self,

Calls 5

_decode_tokenMethod · 0.95
LogprobClass · 0.90
slice_columnsMethod · 0.80
infoMethod · 0.45
errorMethod · 0.45