Tokenizer backend

This part initializes the tokenizer that converts text into numbers and creates attention masks.

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 PROCESSLoop through kernel loadingLoop through weight conversion in parallelBegin resolving modelRequest configurationSelect configuration classLoad configuration fileResolve configuration pathLocate configuration fileFind file locationInitialize componentsSelect tokenizerChoose tokenizer typeInitialize backendCreate tokenizer instanceResolve tokenizer assetsFind tokenizer filesProcess through layersCreate attention maskBuild SDPA maskReturn attention maskSwitch expert modeUpdate implementationValidate configurationLoad kernel moduleReg…Initialize candidat…Load MTP modelLoad checkpoint weightsDownload model shardsCache checkpoint filesConvert tensor weightsReport loading statusLog loading errorsInitialize dynamic cacheCreate cache layersInitialize static cache
Pipeline setup
Auto classes
Core modules
File utilities
Llama model
Generation
Integrations
1Resolve model configuration — The system resolves the model identifier and retrieves its configuration file. It checks for a valid model type and ensures the requested revision is available, defaulting to the main branch if not specified. The configuration dictionary is loaded from a local file, cache, or downloaded from the Hub.
2Load configuration dictionary — The configuration dictionary is loaded from a JSON file, either locally or from the remote repository. The system handles file resolution, checking for redirects and ensuring the file exists. Default values are applied for missing keys, and the model type is validated against known architectures.
3Resolve file path — The system locates the requested configuration file, first checking the local filesystem and cache. If not found locally, it downloads the file from the remote repository, respecting offline mode settings. The resolved file path is returned for loading.
4Initialize pipeline components — The pipeline initializes its core components, selecting the appropriate execution device such as a GPU or CPU. Generation defaults are configured, including maximum length and sampling parameters. The system prepares reusable processing parameters and sets up the tokenizer for text conversion.
5Initialize tokenizer backend — A new tokenizer backend is initialized using the selected tokenizer file. The system configures tokenization settings including maximum sequence length, padding direction, and truncation behavior. Special tokens are identified and mapped to their corresponding IDs for proper text processing.
6Select tokenizer assets — The system selects the appropriate tokenizer assets from the model repository, including vocabulary files and merge rules. It resolves paths for both fast and legacy tokenizer formats, preferring the fast version when available. Tokenizer configuration is read to determine the correct tokenizer class.
7Process input through model — The model processes the input batch through its decoder layers, computing hidden states and attention outputs. Each layer applies self-attention and feed-forward transformations to the input embeddings. The final layer produces logits that represent the probability distribution over the vocabulary.
8Create attention mask — A 4D boolean attention mask is created for use with scaled dot-product attention. The system checks if the mask can be skipped for causal attention with single query tokens, allowing optimized kernel dispatch. The mask indicates which positions each token can attend to.
9Switch expert implementation — The system temporarily switches grouped matrix multiplication experts to batched implementation for improved decoding performance. This optimization is applied around the decoding work and restored afterward. The switch only occurs on non-CPU devices where applicable.
10Update expert configuration — The model and submodel expert implementation configuration is updated after validation. The system checks top-level changes and iterates through modules to apply the requested implementation. Eligible descendants receive the resolved implementation setting.
11Load attention kernel — A kernel module is loaded and its attention function is registered for optimized computation. The system parses the kernel name, resolves the correct revision, and imports the necessary functions. Mask implementation is configured based on the kernel type.
12Load model weights — The model weights are loaded from the checkpoint files, either locally or downloaded from the repository. The system handles sharded checkpoints by collecting all shards and their metadata. Weight conversion is applied as needed to match the model architecture.
13Download checkpoint shards — All shards of a sharded checkpoint are downloaded and cached if the model is from a remote repository. The system returns the list of paths to all shards along with metadata. Local models use existing files without downloading.
14Convert checkpoint weights — Checkpoint tensors are prepared for model loading by visiting entries in order and applying weight conversions. The system handles device mapping, dtype selection, and quantization settings. Unmatched converter targets are recorded for error reporting.
15Log loading report — A loading report is logged when present, showing information about missing, unexpected, or mismatched keys. The system raises a runtime error for recorded errors or disallowed loading issues. Conversion errors are highlighted with guidance for resolution.
16Initialize model cache — A dynamic cache is initialized with configured layers for storing key and value states during generation. The cache supports lazy layer creation and optional offloading for memory efficiency. Static cache layers are constructed for decoder configurations with maximum cache length.
STEP · hover for full text