MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / sample_top_p

Method sample_top_p

accessory/model/meta.py:550–565  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 2

generateMethod · 0.95
stream_generateMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected