Implementation:Ggml org Ggml Gpt sample top k top p
Appearance
Summary
gpt_sample_top_k_top_p is a C++ function that samples a single token from the model's output logits using combined top-k and top-p (nucleus) sampling with temperature scaling.
API
gpt_vocab::id gpt_sample_top_k_top_p(
const gpt_vocab & vocab,
const float * logits,
int top_k,
double top_p,
double temp,
std::mt19937 & rng
)
Source
- File:
examples/common.cpp, lines 401-479 - Repository: https://github.com/ggml-org/ggml
Parameters
| Parameter | Type | Description |
|---|---|---|
vocab |
const gpt_vocab & |
Vocabulary object; used to determine n_vocab (vocabulary size)
|
logits |
const float * |
Raw model output logits, array of size n_vocab
|
top_k |
int |
Number of highest-probability tokens to retain for top-k filtering; 0 disables top-k |
top_p |
double |
Cumulative probability threshold for nucleus sampling; 1.0 disables top-p |
temp |
double |
Temperature for softmax scaling; 1.0 is neutral (no scaling) |
rng |
std::mt19937 & |
Mersenne Twister random number generator for stochastic sampling |
Returns
- Type:
gpt_vocab::id - Description: The sampled token ID from the filtered and renormalized distribution
Implementation
The function applies the following pipeline in order:
- Temperature scaling — Divide all logits by
temp - Sort logits — Sort token-logit pairs in descending order by logit value
- Top-k filter — If
top_k > 0, truncate the sorted list to the firsttop_kentries - Softmax — Convert the filtered logits into a probability distribution
- Top-p filter — If
top_p < 1.0, walk the sorted probabilities and keep tokens until the cumulative probability reachestop_p, then truncate - Sample — Draw a token from the remaining candidates using
std::discrete_distribution
Dependencies
Standard C++ headers only:
<algorithm>— for sorting logits<random>— forstd::mt19937andstd::discrete_distribution<numeric>— for accumulation during softmax and cumulative probability computation
Related
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment