Make a Logprob dictionary for a position. Args: logprobs: list of log probabilities logprob_token_ids: list of top token ids decoded_tokens: list of decoded top tokens rank: rank of the sampled token num_logprobs: number of logprobs requested
(
logprobs: list[float],
logprob_token_ids: list[int],
decoded_tokens: Iterable[str | None],
rank: int,
num_logprobs: int,
)
| 482 | |
| 483 | @staticmethod |
| 484 | def _make_logprob_dict( |
| 485 | logprobs: list[float], |
| 486 | logprob_token_ids: list[int], |
| 487 | decoded_tokens: Iterable[str | None], |
| 488 | rank: int, |
| 489 | num_logprobs: int, |
| 490 | ) -> dict[int, Logprob]: |
| 491 | """Make a Logprob dictionary for a position. |
| 492 | Args: |
| 493 | logprobs: list of log probabilities |
| 494 | logprob_token_ids: list of top token ids |
| 495 | decoded_tokens: list of decoded top tokens |
| 496 | rank: rank of the sampled token |
| 497 | num_logprobs: number of logprobs requested |
| 498 | by the user (in addition to sampled logprob) |
| 499 | Returns: |
| 500 | dict[token id, Logprob] |
| 501 | """ |
| 502 | if num_logprobs == -1: |
| 503 | num_logprobs = len(logprobs) |
| 504 | # We do not need a special case for the sampled token |
| 505 | # being in the topk, since inserting duplicated data |
| 506 | # into a dictionary twice is the same as doing it once. |
| 507 | topk_ranks = range(1, num_logprobs + 1) |
| 508 | ranks = itertools.chain((rank,), topk_ranks) |
| 509 | |
| 510 | return { |
| 511 | token_id: Logprob( |
| 512 | logprob=logprob, |
| 513 | rank=rank, |
| 514 | decoded_token=token, |
| 515 | ) |
| 516 | for token_id, logprob, rank, token in zip(logprob_token_ids, logprobs, ranks, decoded_tokens) |
| 517 | } |
| 518 | |
| 519 | def _run_engine( |
| 520 | self, |