Sample a token based on the provided probability distribution using top-p sampling. :param probs: The probability distribution for the next token. :param p: The cumulative probability threshold for top-p sampling. :return: The sampled next token.
(self, probs, p)
| 548 | yield {"text": generated, "end_of_content": True} |
| 549 | |
| 550 | def sample_top_p(self, probs, p): |
| 551 | """ |
| 552 | Sample a token based on the provided probability distribution using top-p sampling. |
| 553 | |
| 554 | :param probs: The probability distribution for the next token. |
| 555 | :param p: The cumulative probability threshold for top-p sampling. |
| 556 | :return: The sampled next token. |
| 557 | """ |
| 558 | probs_sort, probs_idx = torch.sort(probs, dim=-1, descending=True) |
| 559 | probs_sum = torch.cumsum(probs_sort, dim=-1) |
| 560 | mask = probs_sum - probs_sort > p |
| 561 | probs_sort[mask] = 0.0 |
| 562 | probs_sort.div_(probs_sort.sum(dim=-1, keepdim=True)) |
| 563 | next_token = torch.multinomial(probs_sort, num_samples=1) |
| 564 | next_token = torch.gather(probs_idx, -1, next_token) |
| 565 | return next_token |
| 566 | |
| 567 | def get_image_words(self): |
| 568 | return self.llma.image_words |
no outgoing calls
no test coverage detected