This part manages the overall pipeline setup, runs the generation process, and returns the final text results.
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/pipelines/__init__.pytransformers/pipelines/base.pytransformers/pipelines/text_generation.pyThe system begins by resolving the model identifier, configuration, and task type for the pipeline. It loads the configuration from the model checkpoint, infers the task if not specified, and sets up default parameters like using fast tokenizers and automatic data types.
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.load_model transformers/pipelines/base.py — Returns an existing model or loads a string identifier using the first successful candidate model class.The system loads the model weights from the checkpoint, trying candidate model classes sequentially until one succeeds. If loading fails with the original data type, it retries with 32-bit floating point, stopping at the first successful load.
load_model transformers/pipelines/base.py — Returns an existing model or loads a string identifier using the first successful candidate model class.The system resolves and loads the tokenizer required for text generation, using the same checkpoint as the model. It ensures the tokenizer matches the model's vocabulary and special tokens.
_resolve_tokenizer transformers/pipelines/__init__.py — Resolve and optionally load the tokenizer required by the pipeline class.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.TextGenerationPipeline.__init__ transformers/pipelines/text_generation.py — Initializes the pipeline, checks its model type, and configures tokenizer padding and default prefix parameters.The pipeline initializes itself and configures the tokenizer to use left padding instead of right padding for text generation. It also sets up default prefix parameters for certain model types.
TextGenerationPipeline.__init__ transformers/pipelines/text_generation.py — Initializes the pipeline, checks its model type, and configures tokenizer padding and default prefix parameters.When you call the pipeline, it processes your input prompt and determines whether to return results immediately or lazily for datasets. It sanitizes your parameters and merges them with the pipeline's default settings.
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.TextGenerationPipeline.preprocess transformers/pipelines/text_generation.py — Returns tokenized prompt inputs with the original prompt attached and optional room reserved for generation.The system converts your text prompt into token IDs that the model can understand, adding any special tokens and preparing the input tensors. It reserves room for new tokens based on generation parameters and truncates if the input exceeds the model's maximum length.
TextGenerationPipeline.preprocess transformers/pipelines/text_generation.py — Returns tokenized prompt inputs with the original prompt attached and optional room reserved for generation.The system moves your input tensors to the model's device and runs the forward pass within an inference context. After generation completes, it moves the output tensors back to the CPU for post-processing.
Pipeline.forward transformers/pipelines/base.py — Returns model outputs moved to CPU after running the pipeline's forward implementation in its inference context.TextGenerationPipeline._forward transformers/pipelines/text_generation.py — Runs model generation and returns generated sequences grouped by input, together with prompt data and eligible auxiliary tensors.Pipeline._ensure_tensor_on_device transformers/pipelines/base.py — _ensure_tensor_on_deviceThe model generates new tokens based on your prompt, producing sequences of token IDs. It groups the generated sequences by input and extracts auxiliary outputs like attention masks that match the output batch size.
TextGenerationPipeline._forward transformers/pipelines/text_generation.py — Runs model generation and returns generated sequences grouped by input, together with prompt data and eligible auxiliary tensors.The system converts the generated token IDs back into text by decoding only the new tokens after your input. It creates one record per generated sequence, optionally prepending your original prompt to the generated text.
TextGenerationPipeline.postprocess transformers/pipelines/text_generation.py — Returns one record per generated sequence containing token IDs or formatted generated text.Pipeline.run_single transformers/pipelines/base.py — Returns the postprocessed prediction for one input.The pipeline returns your results as a list if you provided a single input, or as an iterator for datasets and generators. It logs warnings if you call it many times on GPU without using a dataset.
Pipeline.__call__ transformers/pipelines/base.py — Processes a single input, eagerly returns list results, or returns an iterator for datasets and generators.