Skip to content

vllm.transformers_utils.dynamic_module

Functions:

get_transformers_model_class(architecture, model_config)

The Transformers class for architecture, from Transformers itself or from the model's remote code. As with AutoModel, trusted remote code takes precedence.

Source code in vllm/transformers_utils/dynamic_module.py
def get_transformers_model_class(
    architecture: str, model_config: "ModelConfig"
) -> type["PreTrainedModel"] | None:
    """The Transformers class for `architecture`, from Transformers itself or from
    the model's remote code. As with `AutoModel`, trusted remote code takes precedence.
    """
    if model_config.trust_remote_code:
        auto_map = getattr(model_config.hf_config, "auto_map", None) or {}
        for name, ref in auto_map.items():
            if name.startswith("AutoModel") and ref.endswith(f".{architecture}"):
                return try_get_class_from_dynamic_module(
                    ref,
                    model_config.model,
                    trust_remote_code=model_config.trust_remote_code,
                    revision=model_config.revision,
                    code_revision=model_config.code_revision,
                )
    return getattr(transformers, architecture, None)

try_get_class_from_dynamic_module(class_reference, pretrained_model_name_or_path, trust_remote_code, cache_dir=None, force_download=False, resume_download=None, proxies=None, token=None, revision=None, local_files_only=False, repo_type=None, code_revision=None, warn_on_fail=True, **kwargs)

As transformers.dynamic_module_utils.get_class_from_dynamic_module, but ignoring any errors.

Source code in vllm/transformers_utils/dynamic_module.py
def try_get_class_from_dynamic_module(
    class_reference: str,
    pretrained_model_name_or_path: str,
    trust_remote_code: bool,
    cache_dir: str | os.PathLike | None = None,
    force_download: bool = False,
    resume_download: bool | None = None,
    proxies: dict[str, str] | None = None,
    token: bool | str | None = None,
    revision: str | None = None,
    local_files_only: bool = False,
    repo_type: str | None = None,
    code_revision: str | None = None,
    warn_on_fail: bool = True,
    **kwargs,
) -> type | None:
    """As `transformers.dynamic_module_utils.get_class_from_dynamic_module`,
    but ignoring any errors.
    """
    try:
        resolve_trust_remote_code(
            trust_remote_code,
            pretrained_model_name_or_path,
            has_local_code=False,
            has_remote_code=True,
        )

        return get_class_from_dynamic_module(
            class_reference,
            pretrained_model_name_or_path,
            cache_dir=cache_dir,
            force_download=force_download,
            resume_download=resume_download,
            proxies=proxies,
            token=token,
            revision=revision,
            local_files_only=local_files_only,
            repo_type=repo_type,
            code_revision=code_revision,
            **kwargs,
        )
    except Exception:
        location = "ModelScope" if envs.VLLM_USE_MODELSCOPE else "HF Hub"

        if warn_on_fail:
            logger.warning(
                "Unable to load %s from %s on %s.",
                class_reference,
                pretrained_model_name_or_path,
                location,
                exc_info=True,
            )

        return None