Skip to content

Instantly share code, notes, and snippets.

@imskull
imskull / top-k-top-p.py
Created September 1, 2023 02:58 — forked from thomwolf/top-k-top-p.py
Sample the next token from a probability distribution using top-k and/or nucleus (top-p) sampling
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
""" Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
Args:
logits: logits distribution shape (vocabulary size)
top_k >0: keep only top k tokens with highest probability (top-k filtering).
top_p >0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
"""
assert logits.dim() == 1 # batch size 1 for now - could be updated for more but the code would be less clear
top_k = min(top_k, logits.size(-1)) # Safety check
@imskull
imskull / test
Created June 21, 2014 09:55
Copy of StackeditTest
Blockquote
> Written with [StackEdit](https://stackedit.io/).
<blockquote>
<p>Written with <a href="https://stackedit.io/">StackEdit</a>. <br>
dsf <br>
sdf <br>
sfafsf\bbb</p>
</blockquote>