0
0
Prompt Engineering / GenAIml~12 mins

Top-p and top-k sampling in Prompt Engineering / GenAI - Model Pipeline Trace

Choose your learning style9 modes available
Model Pipeline - Top-p and top-k sampling

This pipeline shows how a language model generates text using two popular sampling methods: top-k and top-p. These methods help the model pick the next word by focusing on the most likely options, making the output more natural and creative.

Data Flow - 6 Stages
1Input prompt
1 sequence of tokensUser provides a starting text prompt1 sequence of tokens
"The weather today is"
2Model prediction
1 sequence of tokensModel predicts probabilities for next token over vocabulary1 probability distribution over 50,000 tokens
{"the": 0.1, "sunny": 0.05, "rainy": 0.03, "cloudy": 0.02, "windy": 0.01, ...}
3Top-k filtering
1 probability distribution over 50,000 tokensKeep only top k=5 tokens with highest probabilities, set others to zero1 filtered probability distribution over 5 tokens
{"the": 0.1, "sunny": 0.05, "rainy": 0.03, "cloudy": 0.02, "windy": 0.01}
4Top-p (nucleus) filtering
1 probability distribution over 50,000 tokensKeep smallest set of tokens whose cumulative probability ≥ p=0.9, set others to zero1 filtered probability distribution over ~10 tokens
{"the": 0.1, "sunny": 0.05, "rainy": 0.03, "cloudy": 0.02, "windy": 0.01, "storm": 0.01, ...}
5Sampling
Filtered probability distributionRandomly pick next token based on filtered probabilities1 chosen token
"sunny"
6Output generation
1 sequence of tokens + 1 chosen tokenAppend chosen token to sequence and repeat for next tokenLonger sequence of tokens
"The weather today is sunny"
Training Trace - Epoch by Epoch

Epoch: 1 | Loss: 3.2  ***************
Epoch: 2 | Loss: 2.5  ***********
Epoch: 3 | Loss: 2.0  ********
Epoch: 4 | Loss: 1.7  *******
Epoch: 5 | Loss: 1.5  ******
EpochLoss ↓Accuracy ↑Observation
13.20.25Model starts learning basic word patterns
22.50.40Loss decreases as model predicts common words better
32.00.52Model improves on grammar and context
41.70.60Model starts capturing longer dependencies
51.50.65Training converges with steady improvement
Prediction Trace - 5 Layers
Layer 1: Model prediction
Layer 2: Top-k filtering (k=5)
Layer 3: Top-p filtering (p=0.9)
Layer 4: Sampling
Layer 5: Output generation
Model Quiz - 3 Questions
Test your understanding
What does top-k sampling do to the model's predicted probabilities?
ARandomly shuffles all tokens before sampling
BKeeps only the top k tokens with highest probabilities and ignores the rest
CKeeps tokens until their cumulative probability reaches p
DAlways picks the token with highest probability
Key Insight
Top-k and top-p sampling help language models generate more natural and diverse text by limiting the next word choices to the most likely tokens. This balances creativity and coherence in generated text.