This part processes the input through the model's layers to produce prediction scores.
Columns are parts of the code, grouped by process. Read top to bottom. Solid arrow: hands work over. Dashed: the reply. Purple: crosses between processes. Hook: work inside one part. Dashed boxes repeat. Hover a column name or a step for its full text.
transformers/generationtransformers/models/llama/modeling_llama.pyThe loop starts by processing the full prompt input in a single prefill pass. It then copies the resulting prediction scores to the main device and applies any configured processors. The system selects the first token using either multinomial sampling with softmax or a simple argmax for greedy selection.
GenerationMixin._sample transformers/generation/utils.py — The shown portion extends input sequences through multinomial sampling or greedy selection and begins removing steps produced beyond deferred stopping.GenerationMixin._prefill transformers/generation/utils.py — Runs full or chunked prompt prefill and returns the resulting model outputs, using the final chunk's outputs for chunked execution.LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.The forward pass receives the prepared inputs and calls the underlying model to process them. It takes the final hidden state and passes it through a slicing operation and a linear head to produce vocabulary logits. This step returns the prediction scores along with the updated cache and any auxiliary outputs.
LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.LlamaModel.forward transformers/models/llama/modeling_llama.py — Returns normalized decoder hidden states and the cache after processing the input batch through the configured layers.LlamaDecoderLayer.forward transformers/models/llama/modeling_llama.py — Returns hidden states transformed by attention and an MLP, each with normalization and a residual connection.LlamaAttention.forward transformers/models/llama/modeling_llama.py — Returns projected attention output and attention weights for the input hidden states.This step runs repeatedly for each new token after the initial prefill. It prepares a decoding request, often for just one token if a cache exists. It then samples the next token from the logits and appends it to the growing sequence.
LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.GenerationMixin._sample transformers/generation/utils.py — The shown portion extends input sequences through multinomial sampling or greedy selection and begins removing steps produced beyond deferred stopping.If beam search is active, this step initializes multiple sequence buffers to track candidate paths. It sets up the necessary data structures for ancestry and scores. The system then enters a loop that continues as long as unfinished sequences remain.
GenerationMixin._beam_search transformers/generation/utils.py — Begins beam-search generation and iteratively computes processed next-token log probabilities; the supplied excerpt omits completion and return behavior.LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.The forward pass evaluates the current candidates for all active beams. It computes the logits for the next potential tokens across these different paths. The resulting scores feed back into the beam search algorithm for ranking and selection.
LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.For assisted decoding, this step obtains candidate tokens from an assistant model. It adjusts the input positions and masks to align with these candidates. The system then requests the necessary logits to verify the speculative tokens.
GenerationMixin._assisted_decoding transformers/generation/utils.py — The shown portion sets up assisted decoding and repeatedly obtains and verifies candidate tokens, reaching speculative token selection when applicable.LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.The forward pass processes the candidate tokens to produce their prediction scores. It computes the logits needed to validate or reject the speculative choices. This output allows the generation loop to decide which candidates to accept.
LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.