logoalt Hacker News

NooneAtAll3today at 1:31 AM4 repliesview on HN

where can one learn what top_k and top_p mean?


Replies

kraphttoday at 1:58 AM

ironically, any frontier LLM will easily generate a tutorial at any detail you like explaining what these are.

if don't have time for that, just know that these are technical parameters that affect how likely it is an llm will produce the same result after being asked the same question.

show 1 reply
nodjatoday at 5:55 AM

The posted answers are either behind a paywall or very obtuse so I'll just explain. I'll assume you know what tokens are.

A models output is not a single token, but a list with the probability for all the tokens that it knows, so we need to use a sampler to select the token that it's going to be the next token in the sentence. For example a simple greedy sampler will choose the token with the highest probability, but samplers normally pick a random token weighted by probability. A model usually knows about ~250 thousand tokens and the probability of some of these tokens are gonna be high, but the vast majority is close to but not actually 0% so there's a chance the sampler might pick some random token that doesn't make much sense, so we filter tokens.

top_k filters the tokens so that only the k top tokens are selected. So top_k=50 will filter those 250k tokens to only 50. This is assuming the list of tokens is sorted by probability.

top_p filters the top tokens until a percentage is accumulated. So if for example if you set the top_p to 0.6 and the model gave the top token a 0.5 (50%) probability and the second top token a 0.2, those 2 token accumulated to 0.7 which is greater than what you set it to (0.6) so no more tokens are selected. If this ran after top_k=50 it'll turn the list of 50 tokens into one of 2.

After each filter parameter is processed, the probability of the tokens is adjusted to sum to 1 (100%), Also note that order of operation here matters, i.e. top_p could be applied before top_k, but most providers follow what's on huggingface, I think I've only seen different implementation in certain local model hosting frameworks.

show 1 reply