This part automatically selects and loads the correct configuration and tokenizer based on the model.
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/auto/configuration_auto.pytransformers/utilstransformerstransformers/models/auto/tokenization_auto.pyThe pipeline resolver starts resolving the model, configuration, and task for the pipeline. It sets defaults like fast tokenizer enabled and automatic data type, validates that a model is provided, and prepares to load the configuration. If a PEFT adapter is detected, it may switch to the base model repository.
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.Automatic configuration selects the correct configuration class based on the model type, checking for remote code and registered mappings. It sets internal flags indicating the configuration comes from an auto class and prepares to resolve the repository revision.
AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.replace_list_option_in_docstrings transformers/models/auto/configuration_auto.py — Returns a decorator that replaces a function's first 'List options' placeholder with generated options.resolve_revision transformers/utils/hub.py — Best-effort resolution of a (possibly mutable) revision into the immutable commit it currently points to.
Every public loading entry point (from_pretrained, pipeline, ...) calls this once, then passes the returned
value as revision for the rest of that call. Two benefits:
- all the files of a single load come from the same repository state, even if the repo is updated in the meantime;
- all subsequent lookups are done against an immutable commit, so they can be served from the local cache
(including the "this file does not exist" cache) without any call to the Hub.
The returned [~huggingface_hub.ResolvedRevision] is a str equal to revision, so URLs and error messages keep
mentioning what the user asked for.
This is best effort: local folders are returned as-is, and any failure to resolve the revision (unknown or gated
repo, unreachable Hub, ...) returns revision unchanged so that the regular loading path applies, with its own
error messages and its own fallbacks to the cache.
Args:
path_or_repo_id (str or os.PathLike, *optional*):
A repo id on the Hub, or a path to a local directory or None (both returned as-is).
revision (str, *optional*):
The revision to resolve. None means the default branch of the repo.
repo_type (str, *optional*):
The type of the repo ("model" if not provided).
token (str or bool, *optional*):
The token to use as HTTP bearer authorization for remote files.
local_files_only (bool, *optional*, defaults to False):
If True, resolve the revision from the local cache only, without contacting the Hub.
cache_dir (str or os.PathLike, *optional*):
The cache the caller downloads to, where the revision -> commit hash mapping is recorded so that a later
offline load can still resolve revision locally.
Returns:
Optional[str]: The resolved revision, or revision if it could not be resolved.The commit resolver converts the requested revision, like a branch name, into an immutable commit hash. This ensures all files loaded during this pipeline creation come from the same repository snapshot, even if the repository is updated mid-load.
resolve_revision transformers/utils/hub.py — Best-effort resolution of a (possibly mutable) revision into the immutable commit it currently points to.
Every public loading entry point (from_pretrained, pipeline, ...) calls this once, then passes the returned
value as revision for the rest of that call. Two benefits:
- all the files of a single load come from the same repository state, even if the repo is updated in the meantime;
- all subsequent lookups are done against an immutable commit, so they can be served from the local cache
(including the "this file does not exist" cache) without any call to the Hub.
The returned [~huggingface_hub.ResolvedRevision] is a str equal to revision, so URLs and error messages keep
mentioning what the user asked for.
This is best effort: local folders are returned as-is, and any failure to resolve the revision (unknown or gated
repo, unreachable Hub, ...) returns revision unchanged so that the regular loading path applies, with its own
error messages and its own fallbacks to the cache.
Args:
path_or_repo_id (str or os.PathLike, *optional*):
A repo id on the Hub, or a path to a local directory or None (both returned as-is).
revision (str, *optional*):
The revision to resolve. None means the default branch of the repo.
repo_type (str, *optional*):
The type of the repo ("model" if not provided).
token (str or bool, *optional*):
The token to use as HTTP bearer authorization for remote files.
local_files_only (bool, *optional*, defaults to False):
If True, resolve the revision from the local cache only, without contacting the Hub.
cache_dir (str or os.PathLike, *optional*):
The cache the caller downloads to, where the revision -> commit hash mapping is recorded so that a later
offline load can still resolve revision locally.
Returns:
Optional[str]: The resolved revision, or revision if it could not be resolved.Configuration instantiation downloads or reads the configuration file, following any redirect file if present. It returns the configuration dictionary and any remaining keyword arguments that were not consumed.
PreTrainedConfig.get_config_dict transformers/configuration_utils.py — Returns the resolved configuration dictionary and remaining keyword arguments, following a configuration-file redirect when present.AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.Configuration instantiation checks whether the user has opted in to loading remote code. If remote code exists and the user has not explicitly trusted it, an error is raised requiring user consent.
resolve_trust_remote_code transformers/dynamic_module_utils.py — Resolves the trust_remote_code argument. If there is remote code to be loaded, the user must opt-in to loading
it.
Args:
trust_remote_code (bool or None):
User-defined trust_remote_code value.
model_name (str):
The name of the model repository in huggingface.co.
has_local_code (bool):
Whether the model has local code.
has_remote_code (bool):
Whether the model has remote code.
error_message (str, *optional*):
Custom error message to display if there is remote code to load and the user didn't opt-in. If unset, the error
message will be regarding loading a model with custom code.
Returns:
The resolved trust_remote_code value.AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.Configuration instantiation loads a custom configuration class from a cached Python module in the local directory or repository. It splits the class reference, requests the module file, and retrieves the class for use.
get_class_from_dynamic_module transformers/dynamic_module_utils.py — Returns a class loaded from a cached Python module in a local directory or repository.AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.Configuration instantiation registers the loaded custom class with the auto configuration system. This allows the auto system to recognize and use the custom configuration for subsequent operations.
PreTrainedConfig.register_for_auto_class transformers/configuration_utils.py — Register this class with a given auto class. This should only be used for custom configurations as the ones in
the library are already mapped with AutoConfig.
Args:
auto_class (str or type, *optional*, defaults to "AutoConfig"):
The auto class to register this new configuration with.AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.Configuration instantiation creates the final configuration object from the pretrained model settings. It applies any user-provided overrides to the loaded values and returns the instantiated configuration.
PreTrainedConfig.from_pretrained transformers/configuration_utils.py — Instantiate a [PreTrainedConfig] (or a derived class) from a pretrained model configuration.
Args:
pretrained_model_name_or_path (str or os.PathLike):
This can be either:
- a string, the *model id* of a pretrained model configuration hosted inside a model repo on
huggingface.co.
- a path to a *directory* containing a configuration file saved using the
[~PreTrainedConfig.save_pretrained] method, e.g., ./my_model_directory/.
- a path to a saved configuration JSON *file*, e.g., ./my_model_directory/configuration.json.
cache_dir (str or os.PathLike, *optional*):
Path to a directory in which a downloaded pretrained model configuration should be cached if the
standard cache should not be used.
force_download (bool, *optional*, defaults to False):
Whether or not to force to (re-)download the configuration files and override the cached versions if
they exist.
proxies (dict[str, str], *optional*):
A dictionary of proxy servers to use by protocol or endpoint, e.g., {'http': 'foo.bar:3128',
'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
token (str or bool, *optional*):
The token to use as HTTP bearer authorization for remote files. If True, or not specified, will use
the token generated when running hf auth login (stored in ~/.huggingface).
revision (str, *optional*, defaults to "main"):
The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
git-based system for storing models and other artifacts on huggingface.co, so revision can be any
identifier allowed by git.
<Tip>
To test a pull request you made on the Hub, you can pass revision="refs/pr/<pr_number>".
</Tip>
return_unused_kwargs (bool, *optional*, defaults to False):
If False, then this function returns just the final configuration object.
If True, then this functions returns a Tuple(config, unused_kwargs) where *unused_kwargs* is a
dictionary consisting of the key/value pairs whose keys are not configuration attributes: i.e., the
part of kwargs which has not been used to update config and is otherwise ignored.
subfolder (str, *optional*, defaults to ""):
In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can
specify the folder name here.
kwargs (dict[str, Any], *optional*):
The values in kwargs of any keys which are configuration attributes will be used to override the loaded
values. Behavior concerning key/value pairs whose keys are *not* configuration attributes is controlled
by the return_unused_kwargs keyword parameter.
Returns:
[PreTrainedConfig]: The configuration object instantiated from this pretrained model.
Examples:
``python
# We can't instantiate directly the base class *PreTrainedConfig* so let's show the examples on a
# derived class: BertConfig
config = BertConfig.from_pretrained(
"google-bert/bert-base-uncased"
) # Download configuration from huggingface.co and cache.
config = BertConfig.from_pretrained(
"./test/saved_model/"
) # E.g. config (or model) was saved using *save_pretrained('./test/saved_model/')*
config = BertConfig.from_pretrained("./test/saved_model/my_configuration.json")
config = BertConfig.from_pretrained("google-bert/bert-base-uncased", output_attentions=True, foo=False)
assert config.output_attentions == True
config, unused_kwargs = BertConfig.from_pretrained(
"google-bert/bert-base-uncased", output_attentions=True, foo=False, return_unused_kwargs=True
)
assert config.output_attentions == True
assert unused_kwargs == {"foo": False}
``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 resolver checks if an assistant model is needed for speculative decoding. If provided as a string identifier, it loads the assistant model and tokenizer, moving them to the correct device and comparing vocabulary settings.
load_assistant_model transformers/pipelines/base.py — Returns an assistant model and any needed assistant tokenizer, or (None, None) when assistance is inapplicable.AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.Automatic configuration completes its selection process, having resolved the repository, loaded the configuration dictionary, handled remote code trust, and instantiated the configuration object. It prepares to return the final configuration.
AutoConfig.from_pretrained transformers/models/auto/configuration_auto.py — Returns a configuration instance selected from trusted remote code or the registered model-type mapping.Automatic tokenizer starts selecting the appropriate tokenizer through explicit choices, model metadata, and compatibility checks. It sets internal flags and determines whether to use a specialized backend for Mistral models.
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.load_assistant_model transformers/pipelines/base.py — Returns an assistant model and any needed assistant tokenizer, or (None, None) when assistance is inapplicable.replace_list_option_in_docstrings transformers/models/auto/configuration_auto.py — Returns a decorator that replaces a function's first 'List options' placeholder with generated options.The commit resolver converts the tokenizer's requested revision into an immutable commit hash. This ensures the tokenizer files come from the same repository state as the model configuration.
replace_list_option_in_docstrings transformers/models/auto/configuration_auto.py — Returns a decorator that replaces a function's first 'List options' placeholder with generated options.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.resolve_revision transformers/utils/hub.py — Best-effort resolution of a (possibly mutable) revision into the immutable commit it currently points to.
Every public loading entry point (from_pretrained, pipeline, ...) calls this once, then passes the returned
value as revision for the rest of that call. Two benefits:
- all the files of a single load come from the same repository state, even if the repo is updated in the meantime;
- all subsequent lookups are done against an immutable commit, so they can be served from the local cache
(including the "this file does not exist" cache) without any call to the Hub.
The returned [~huggingface_hub.ResolvedRevision] is a str equal to revision, so URLs and error messages keep
mentioning what the user asked for.
This is best effort: local folders are returned as-is, and any failure to resolve the revision (unknown or gated
repo, unreachable Hub, ...) returns revision unchanged so that the regular loading path applies, with its own
error messages and its own fallbacks to the cache.
Args:
path_or_repo_id (str or os.PathLike, *optional*):
A repo id on the Hub, or a path to a local directory or None (both returned as-is).
revision (str, *optional*):
The revision to resolve. None means the default branch of the repo.
repo_type (str, *optional*):
The type of the repo ("model" if not provided).
token (str or bool, *optional*):
The token to use as HTTP bearer authorization for remote files.
local_files_only (bool, *optional*, defaults to False):
If True, resolve the revision from the local cache only, without contacting the Hub.
cache_dir (str or os.PathLike, *optional*):
The cache the caller downloads to, where the revision -> commit hash mapping is recorded so that a later
offline load can still resolve revision locally.
Returns:
Optional[str]: The resolved revision, or revision if it could not be resolved.Configuration instantiation resolves the tokenizer class name to an actual class or backend, trying multiple lookup paths including registered aliases and module imports. It may install fast tokenizer aliases for successful lookups.
tokenizer_class_from_name transformers/models/auto/tokenization_auto.py — Resolves a tokenizer class name to a class or backend, returning None if all lookup paths fail.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.model_type_to_module_name transformers/models/auto/configuration_auto.py — Converts a model-type key into its module-name string.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.Configuration instantiation selects tokenizer assets and resolves their paths from a local source or model repository. It discovers vocabulary files, tokenizer configuration, and chat templates, handling both local directories and remote downloads.
PreTrainedTokenizerBase.from_pretrained transformers/tokenization_utils_base.py — Selects tokenizer assets and begins resolving their paths from a local source or model repository in the visible portion.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.Automatic tokenizer completes its selection process, having resolved the tokenizer class, repository revision, and asset paths. It prepares to return the final tokenizer for use in the pipeline.
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.