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.
I used to try to train models ages ago (that is, a couple of years), and I never found out how they got good results from top_k in the first place.
My problem was that as soon as the model had generated an unlikely token, it was quickly steering towards spaces it had never seen during its training. It found itself in unmapped territory, where anything might happen. With high temperatures, it would then generate noisy garbage, with low temperature it would generate repetitive garbage. We tried to fight this with rollout strategies and backtracking, i.e. if you find yourself in a space where all tokens seem equally likely, you're probably in junk land.
Our tricks didn't work very well. But I didn't manage to keep up well enough to learn what worked.
> So they are essentially a protection against spurious errors
Only coincidentally. Sampling nonsense tokens will certainly degrade its performance and/or brick it, but it's also there to encourage diversity.
For example, imagine we have the following sentence:
> The color of this ball is ____
Now, what should the model predict for "____"? There isn't really a "correct" answer here. It can be "red", it can be "blue", it can be "green", or any other color. But it's definitely not going to be "ব্যথাя". LLMs output a probability distribution for the next token, so imagine this is the probability distribution that it outputs:
red -> 60%
green -> 19%
blue -> 19%
ব্যথাя -> 2%
So how do we decide which token to pick? Simplest way is to always pick the most probable one (in this case: "red"). In this case we'd ideally want it to be able to output "red", "green" or "blue" (since all of those are reasonable), but never "ব্যথাя" (whose 2% is most certainly noise). So a sampler is essentially an algorithm which lets the inference engine pick the exact token to output from this list.
> I don't quite understand the point about order of operations - does it do normalization after every such filter pass? why not leave it to the end?
Because you can technically compose multiple samplers at the same time in a pipeline, and in some cases their order can matter and give you a different result (or take less/more time to execute). To give you a generic example: imagine you have a list with numbers in random order. You can execute one of two operations on it: (1) sort it, (2) take the leading 10 numbers. If you first sort it and then take 10 leading numbers you'll get a different result than if you'd first took 10 leading numbers and then sorted them.
Yes, the whole list will always sum to 1 (100%) because there's lots of more sampling parameters. top_p, top_k and temperature are just the ones that affect output the most. Most parameters do math around assuming the list sums to 1 and order is not always the same, some software even lets you change the order around.
top_a is not very common and is better explained if I explain how the much more common min_p works. min_p filters out tokens below a certain threshold. The formula is <filter threshold> = <min_p> * <top token probability>. So if the top token has 0.5 probability, min_p = 0.1 would cut out tokens below 0.05. This is a tunable that lets you filter out other tokens depending on how confident the model is.
top_a is almost the same formula but you just square the <top token probability>. So <filter threshold> = <top_a> * <top token probability> ^ 2. This makes the filtering ramp up faster (cut out more tokens) if the model has a much more confident top choice, but keep more choices if the model is not so confident.
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.
An LLM is stateless so every time you send a message a command is run with the parameters, the prompts, and a copy of the chat history when using one of those chat interfaces, and then it returns text. If the output includes explaining it to an LLM that would then be included in the copy of the chat history placed in the new command.
It would be a bit like opening a text editor and typing in that you want to increase the font size. Someone external to the text would have to come along and click the font options.
Yes but it implies it's doing its own "brain surgery" so if it got params wrong it could break itself. In that case you would have another AI behind the scenes doing the tweaking so it can understand you at a "subconscious" level.
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.