LLM Learning Roadmap
✨ Awesome-Anything
🖼️ Digital Image Processing
🍃 LLM Components
🌱 LLM Pre-training
☘️ LLM Post-Training
🍀 LLM Popular Models
🪴 LLM Applications
🌿 LLM Optimization
🌾 LLM Compression
🌵 LLM Hands-on Practice
🌴 LLM Must-read Papers
🌳 LLM Q&A
🐝 VLM Image Encoders
📝 MISC.
1.6 Architecture & Decoding Policy
1.6 Architecture & Decoding Policy
1.6.1 LLM Architectures
Encoder, Decoder, and Hybrid

LLM architectures can be divided into four types: Decoder-only, Encoder-only, Encoder-Decoder, and Prefix LM (can be seen as a special case of Encoder-Decoder).
- Decoder-only: Uses unidirectional attention from left to right, typically autoregressive language models. The pre-training and training for downstream applications are exactly the same, leading to excellent text generation results. It has high training efficiency and strong zero-shot capabilities.
Representative models: GPT, LLaMA, BLOOM, OPT
- Encoder-only: Uses bidirectional attention. Mainly used for language understanding tasks, extracting representations, and a pre-training language model.
Representative model: BERT
- Encoder-Decoder: Uses bidirectional attention for input and unidirectional attention for output. Performs well in tasks requiring deep understanding (e.g., question answering), but training is less efficient, and text generation is less expressive.
Representative models: T5, Flan-T5, BART
- Prefix Decoder: Uses bidirectional attention for the prefix and unidirectional attention for output. A hybrid approach that combines the architecture of a decoder but uses the mask as in encoder-encoder.
Representative models: GLM, U-PaLM
Mixture of Experts (MoE) Architecture
Representative models: Deepseek V2/V3, Mistral
Typical LLMs are dense models, in contrast to MoE models. Each MoE layer is usually composed of N expert networks , paired with a gating network . This gating network is usually a linear network with a softmax activation function, whose purpose is to guide the input to the appropriate expert networks.
The placement of MoE layers is in the Transformer modules, replacing FFN, and is usually placed after the Self-Attention (SA) layer. This placement is reasonable because as the model scales up, the computational cost of FFN increases significantly. For example, in a model like PaLM with up to 540B parameters, 90% of the parameters are in the FFN layer.
The gating network (also known as the router or switch) is the foundation of all MoE frameworks. It coordinates which experts to use for computation and how to combine expert outputs. Based on how it processes inputs, gating can be divided into three types: sparse, dense, and soft. Among them, the sparse gating mechanism activates only a few experts, while dense activates all, and soft includes differentiable methods like token merging and expert fusion. More examples can be found in the [4.8 MOE Series].

<ins/>
1.6.2 Decoding/Sampling Policy
Several decoding strategies for large language models to output tokens
Greedy Search
Greedily selecting the token with the highest probability each time is simple and efficient, but it may lead to overly monotonous and repetitive text.

Beam Search
Maintain a set of candidate sequences of size . At each step, choose the candidate sequences for the next token prediction, leading to new candidate sequences. Then still choose top candidate sequences to keep, where some candidates may be from the same old beam and some old beams may not generate sequences that are in the new top . This method balances the quality and diversity of the generated output because it can pick up sequences with lower initial probability, but it may lead to the potentially optimal solution being discarded.

- Beam Search Code
Note that this implementation is a Breadth First Search (BFS) version, and there’s also Depth First Search (DFS) version out there. The outcomes of the two versions differ slightly from each other.
<ins/>
Top-K Sampling
Sampling from the top-k ranked tokens allows other less frequent or lower probability tokens a chance to be selected. Still, each time, only one token is selected, but instead of directly choosing the one with the highest probability, we sample one from the top K tokens according to their corresponding probabilities. It’s totally OK if you want to sample 2 or 3 each time from the top K, with which you are basically combining this method with the beam search.
In many cases, this kind of sampling brings randomness that helps improve generation quality. However, this may lead to the generation of gibberish when the distribution is sharp because some “not likely” tokens may be selected, and may limit the model's creativity when the distribution is relatively flat because more than one candidate token is similarly good to each other but we only sample one.

Above is an example of the .
Top-P Sampling
This one is similar to the Top-K sampling. But instead of sampling from the top-K tokens, we sample from the smallest set of tokens whose cumulative probability exceeds a predefined threshold .

Above is an example of the .
Temperature Sampling
Adding a temperature parameter to Softmax is used to adjust the flatness of the token probability distribution output by large language models. The higher the temperature, the flatter the probability distribution, and the more random the output. The lower the temperature, the more extreme the distribution, and the more stable the output. This method can be combined with other sampling methods to adjust the sampling distribution.
Where:
- is the probability of the token
- is the logit (unnormalized score) for the token
- is the temperature parameter
- When : this is the standard softmax
- When : the probability distribution becomes flatter, making the output more random
- When : the distribution becomes sharper, making the output more deterministic
You may notice some implementation likeChatOpenAI(model="gpt-4o-mini", temperature=0), but obviously the temperature cannot be 0. In this case, thetemperature=0indicates greedy decoding.
Best-of-N
By letting an LLM output N responses, and then using a Verifier, ORM, or PRM (PRM can provide step-by-step rewards and act as the overall response scorer) to assign a score to each response, the highest-scoring one is then selected as the final answer.

Majority Vote
Majority voting: output multiple responses, and select the one with the most agreement as the final answer. This is basically an ensemble of outputs and is commonly used in mathematics or fact questions.
Example:
You ask a language model the question five times independently: "What is the capital of France?". Then the model outputs: Paris, Paris, Lyon, Paris, Marseille. With a majority vote, you will get Paris as the final answer.
Self-consistency
First introduced in the paper Self-Consistency Improves Chain of Thought Reasoning in Language Models, it involves prompting the large language model to output multiple CoT reasoning paths to obtain multiple answers, and then selecting the final answer through a majority voting approach, similar to majority voting. More details can be found at [5.1 Prompt Engineering].
Prev
1.5 Positional Encoding
Next
2.1 Pre-training Definition
Loading...