Token selection and sampling
When a language model generates text, it builds the continuation token by token. In the Nucleus Sampling study by Holtzman and colleagues, the preceding sequence of tokens is used as context for each new token. The probabilities the model assigns to possible continuations affect which tokens can be selected during sampling.
The researchers define open-ended text generation as the task of producing a coherent continuation of a given context. In this type of generation, the context constrains the range of suitable continuations; however, there may still be multiple plausible options for what comes next. The token selection examined in the study is part of the process of producing that continuation.
Google Cloud states that parameter values in calls sent to the model affect how the response is generated. Different values can lead to different results. The available parameters can also vary by model.
Temperature and top-p affect different aspects of token selection. According to Google Cloud's explanation, temperature controls the degree of randomness in selection. Top-p changes how the candidate tokens for selection are determined. Both settings are related to sampling during response generation.
What does temperature change?
According to Google Cloud, low temperature values are suitable for prompts that require a less open-ended or less creative response. Higher values can lead to more varied or creative results. This does not mean that a high value will produce a particular result in every response.
Google Cloud states that when the temperature value is 0, the highest-probability tokens are always selected. Responses to the same prompt are largely deterministic in this case. Small variations may still occur.
Holtzman and colleagues describe temperature as a method for shaping the probability distribution. In the study, the calculation recomputes the softmax distribution using the model's logits values and the temperature value. This process changes the distribution of probabilities used for sampling.
According to the study, temperature values between zero and one shift the distribution toward high-probability events. This process also reduces the total probability in the tail of the distribution made up of low-probability tokens. The researchers also refer to studies in which the distribution is shaped with low temperature before top-k sampling.
Google Cloud recommends increasing the temperature value when the response is too generic or short, or when the model gives a fallback response. Raising the temperature value to at least 0.1 when the model enters infinite generation may also improve results. These are recommendations specific to the situations described by Google Cloud.
How does top-p determine candidates?
In the method Holtzman and colleagues call Nucleus Sampling, the set of tokens eligible for selection is determined by the shape of the probability distribution. The top-p set is the smallest set of tokens whose cumulative probability reaches the specified p threshold. The process builds this set starting with the highest-probability tokens.
In the study, after the set is determined, the original distribution is rescaled and the next token is sampled from this new distribution. Tokens excluded from the set have zero probability in the new distribution. The threshold determines which tokens participate in sampling based on cumulative probability.
In Google Cloud's example, the probabilities of tokens A, B, and C are given as 0.3, 0.2, and 0.1, respectively. When the top-p value is 0.5, the combined probability of A and B reaches that value. The next token is selected from A or B using temperature; C is not included in the candidate set.
In this example, top-p does not mean that the selected token has a probability of 0.5 on its own. The threshold is met by the sum of the probabilities of A and B. In the process described by Google Cloud, whether the next token is A or B is determined by selection from this candidate set using temperature.
Google Cloud states that a lower top-p value should be used for less random responses and a higher value for more random responses. Top-p concerns the cumulative probability of the candidates to be selected at each step. In Google Cloud's example, temperature is used during sampling from these candidates.