# Beam Search

Beam search is a <mark style="color:blue;">heuristic search algorithm</mark> used in sequence modeling, particularly relevant in language models like GPT for tasks such as text generation. It's a type of breadth-first search that expands and examines a set of most promising nodes at each level of the tree.

### <mark style="color:blue;">Detailed Explanation of Beam Search</mark>

1. **Basic Concept**: Beam search maintains a <mark style="color:blue;">fixed number of best candidates</mark> (or "beams") at each step of the model's output sequence. These candidates are the most promising sequences according to the model's scoring function.
2. **Selection of Beams**: At each step, it expands each candidate and keeps the top N expansions based on their likelihood, where N is the beam width. This process repeats for each step in the sequence generation.
3. **Beam Width**: This is a <mark style="color:purple;">crucial hyperparameter</mark>. A larger beam width increases the chances of finding a better output sequence but also increases computational expense. Conversely, a smaller beam width reduces the computational burden but might miss more optimal sequences.
4. **Sequence Scoring**: The scoring of sequences typically involves calculating the cumulative probability of the sequence. Longer sequences might need to be normalized to ensure a fair comparison with shorter sequences.
5. **Termination**: The process continues until a termination condition is met, which could be reaching a maximum sequence length or all candidates reaching an end-of-sequence token.

### <mark style="color:blue;">Integration with GPT's Attention Operator</mark>

1. **Context Phase**: In the initial phase, a <mark style="color:purple;">single beam is computed per input sequence</mark>, meaning it follows the most probable path according to the model's predictions.
2. **Generation Phase**: This is where beam search becomes particularly interesting. The attention mechanism (MHA/MQA/GQA) uses an additional tensor, called `cache_indirection`, to keep track of the paths in the beam search.
3. **Cache Indirection Tensor**: The shape of this tensor is `[batch_size, beam_width, max_seqlen]`. Each element of this tensor tells the model from which path in the beam (which previous token) to take the key (K) and value (V) elements for the current token. This mechanism allows the model to effectively manage multiple potential sequences in parallel.

### <mark style="color:blue;">Relevance to Large Language Models (LLMs)</mark>

1. **Handling Ambiguity and Complexity**: Beam search is crucial in LLMs for tasks like text generation, where there are often many possible valid continuations of a given text. It allows the model to explore multiple paths and find a more coherent and contextually appropriate output.
2. **Quality of Output**: By considering multiple paths simultaneously, beam search often produces more accurate and natural results compared to greedy approaches, which only consider the single most likely next step.
3. **Use in Diverse Applications**: This method is used in a variety of applications like machine translation, summarization, and conversational AI, where generating coherent and contextually relevant text is key.

In summary, beam search is a balance between exploring a variety of possible sequences and computational feasibility. In the context of LLMs like GPT, it is a fundamental technique for managing the complexities of natural language generation, ensuring that the output is not just probable but contextually coherent and diverse.

\
