How does pipeline("text-generation", model=<a Llama checkpoint>) build a pipeline, and what happens when you call it on a prompt, down to the model's forward pass?

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.

MAIN PROCESSa loop around arrows a20..a20a loop around arrows a22..a24a loop around arrows a26..a28resolves modelrequests configreads configinitializes pipelinerequests tokenizerselects tokenizerinitializes backendloads tokenizerprocesses promptstarts generationprepares loopruns forwardpro…computes maskcreates maskrun…selects tokenrun…continues loopruns forwardpro…Pipeline LogicAuto SetupTokenizerGenerationModel Layers
1Resolve model and configuration — The 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.
2Load model configuration — 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.
3Initialize pipeline and tokenizer — 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.
4Select tokenizer type — 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.
5Initialize tokenizer backend — 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.
6Process prompt and start generation — 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.
7Prepare generation loop — 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.
8Run model forward pass — 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.
9Compute attention masks — 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.
10Process through decoder layers — 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.
11Select next token — 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.
12Continue generation loop — 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.
13Run subsequent forward passes — 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.
14Finalize and return output — 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.
◇ 1 of 2
  • This is the normal path where the model generates text based on the provided prompt without modifying the input tokens.
    Shown
  • This path is for a specific mode that fixes potential issues with how words are split into tokens at the end of a prompt.
    Not built
◇ 1 of 2
  • This path prepares for text generation by setting up the necessary configuration for a model that can produce output.
    Shown
  • This path is taken for models that only process input, like those used for understanding text rather than creating it.
    Not built
◇ 1 of 2
  • 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.
    Shown
  • This path runs when the user provides an already loaded model object instead of a string identifier.
    Not built
◇ 1 of 2
  • This path runs when the model type is known, which is typical for standard models.
    Shown
  • This path runs when the model type cannot be determined.
    Not built
◇ 1 of 2
  • This is the standard path for loading most tokenizers, including those for Llama models.
    Shown
  • This path runs only for specific model families that use a special backend format.
    Not built
◇ 1 of 2
  • This path runs when the system finds a registered tokenizer class for the model type.
    Shown
  • This path runs when the system cannot find a registered tokenizer and must use a fallback.
    Not built
◇ 1 of 2
  • This path runs when a tokenizer class is successfully found for the model type.
    Shown
  • This path runs when no specific tokenizer class is found.
    Not built
◇ 1 of 2
  • This path runs when the system successfully identifies the tokenizer type from the model configuration.
    Shown
  • This path runs when the system cannot determine the tokenizer type and must fall back to other methods.
    Not built
STEP · hover for full text