The pipeline orchestration first builds the pipeline by resolving the model identifier and loading the automatic configuration, which identifies the model type and initializes the appropriate tokenizer backend. Once the pipeline is constructed, calling it on a prompt triggers a preprocessing step that converts the text to numbers, after which the generation utilities manage a loop that repeatedly calls the model forward pass to predict the next token until a stopping condition is met. The final output is then post-processed back into readable text.
What this shows: This is the normal path where the model generates text based on the provided prompt without modifying the input tokens. (1 other option); This path prepares for text generation by setting up the necessary configuration for a model that can produce output. (1 other option); This path runs when the user provides a model identifier as a string, such as a model name from the Hub, which is the standard usage. (1 other option); This path runs when the model type is known, which is typical for standard models. (1 other option); This is the standard path for loading most tokenizers, including those for Llama models. (1 other option); This path runs when the system finds a registered tokenizer class for the model type. (1 other option); This path runs when a tokenizer class is successfully found for the model type. (1 other option); This path runs when the system successfully identifies the tokenizer type from the model configuration. (1 other option).
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/pipelinestransformers/models/autotransformerstransformers/generationtransformers/models/llamaThe pipeline entry point resolves the model identifier, such as a Hub name, and determines the task. It validates that a model is provided and sets defaults like using the fast tokenizer. It then requests the automatic configuration to load the model's specific settings.
pipeline transformers/pipelines/__init__.py — Begins resolving the model, configuration, task, and custom implementation for a pipeline; construction and return are beyond the supplied excerpt.AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.The automatic configuration part reads the model's configuration file, identifying the model type. It ensures the model type is known and registered, raising an error if it is missing. It returns a configuration instance with settings like hidden size and layer count.
AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.The pipeline orchestration initializes the pipeline components, setting up the device and generation defaults. It then calls the automatic configuration to select and load the correct tokenizer based on the model type. This step ensures padding is set to the left for text generation.
TextGenerationPipeline.__init__ transformers/pipelines/text_generation.py — Initializes the pipeline, checks its model type, and configures tokenizer padding and default prefix parameters.Pipeline.__init__ transformers/pipelines/base.py — Initializes pipeline components, execution device, generation defaults, and reusable processing parameters.load_assistant_model transformers/pipelines/base.py — Returns an assistant model and any needed assistant tokenizer, or (None, None) when assistance is inapplicable.AutoTokenizer.from_pretrained transformers/models/auto/tokenization_auto.py — Selects and sometimes loads a tokenizer through explicit choices, model metadata, and compatibility checks; the final selection path is truncated.The automatic configuration determines the appropriate tokenizer class, such as a standard Llama tokenizer. It checks for specific formats and resolves the necessary files from the repository. It then delegates the actual backend initialization.
AutoTokenizer.from_pretrained transformers/models/auto/tokenization_auto.py — Selects and sometimes loads a tokenizer through explicit choices, model metadata, and compatibility checks; the final selection path is truncated.MistralCommonBackend.from_pretrained transformers/tokenization_mistral_common.py — Returns a new Mistral backend using a tokenizer file selected from a local directory or downloaded from a repository.The tokenizer backend loads the tokenizer file, which contains the vocabulary and merge rules. It configures settings like the maximum sequence length and padding direction. The initialized tokenizer is then returned up the chain.
MistralCommonBackend.from_pretrained transformers/tokenization_mistral_common.py — Returns a new Mistral backend using a tokenizer file selected from a local directory or downloaded from a repository.MistralCommonBackend.__init__ transformers/tokenization_mistral_common.py — Initializes a Mistral tokenizer backend from a tokenizer file and configures its tokenization settings.When the pipeline is called, it preprocesses the input prompt, converting the text into numerical token IDs. It then invokes the generation utilities to handle the text generation process. This step manages the overall execution flow for a single input.
TextGenerationPipeline.__call__ transformers/pipelines/text_generation.py — Returns the parent pipeline's result for the supplied text inputs and keyword arguments.Pipeline.__call__ transformers/pipelines/base.py — Processes a single input, eagerly returns list results, or returns an iterator for datasets and generators.Pipeline.run_single transformers/pipelines/base.py — Returns the postprocessed prediction for one input.Pipeline.forward transformers/pipelines/base.py — Returns model outputs moved to CPU after running the pipeline's forward implementation in its inference context.The generation utilities prepare the generation configuration, setting parameters like the maximum length. They initiate the generation loop, which will repeatedly request predictions for the next token. This step manages the overall decoding strategy.
GenerationMixin.generate transformers/generation/utils.py — The shown portion dispatches generation to a custom Hub recipe, continuous batching, or a validated decoding strategy.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 model forward pass processes the input tokens through the model's layers. It computes the attention masks and passes the data through the decoder layers to produce raw prediction scores. This step forms the core computation of the model.
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.create_causal_mask transformers/masking_utils.py — Returns a backend-specific causal attention mask, or a bidirectional mask when the config disables causality.During the forward pass, the model computes the causal attention mask. This mask ensures that each token can only attend to previous tokens, maintaining the autoregressive property. The mask is applied within the attention layers.
create_causal_mask transformers/masking_utils.py — Returns a backend-specific causal attention mask, or a bidirectional mask when the config disables causality.The input is processed through each decoder layer, which applies self-attention and feed-forward networks. Each layer normalizes the input, applies the attention mechanism, and adds residual connections. The final hidden state is produced for output projection.
LlamaDecoderLayer.forward transformers/models/llama/modeling_llama.py — Returns hidden states transformed by attention and an MLP, each with normalization and a residual connection.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 generation utilities receive the prediction scores and apply a selection method, such as greedy search or sampling. The chosen token is appended to the generated sequence. The loop continues if stopping criteria are not met.
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.The generation loop continues, using the updated sequence to predict the next token. It checks if the maximum length is reached or an end-of-sequence token is generated. This process repeats for each new token.
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.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 model forward pass is called again with the newly appended token. This pass is more efficient as it can use cached key-value states from previous iterations. It produces new prediction scores for the next token.
LlamaForCausalLM.forward transformers/models/llama/modeling_llama.py — Returns language-model logits, optional label loss, and the underlying model's cache and auxiliary outputs.Once the stopping criteria are met, the generation utilities finalize the sequence of generated tokens. They return the full sequence to the pipeline orchestration. The pipeline then post-processes the tokens back into a text string.
GenerationMixin.generate transformers/generation/utils.py — The shown portion dispatches generation to a custom Hub recipe, continuous batching, or a validated decoding strategy.