| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import os |
| 4 | |
| 5 | from transformers.dynamic_module_utils import ( |
| 6 | get_class_from_dynamic_module, |
| 7 | resolve_trust_remote_code, |
| 8 | ) |
| 9 | |
| 10 | import vllm.envs as envs |
| 11 | from vllm.logger import init_logger |
| 12 | |
| 13 | logger = init_logger(__name__) |
| 14 | |
| 15 | |
| 16 | def try_get_class_from_dynamic_module( |
| 17 | class_reference: str, |
| 18 | pretrained_model_name_or_path: str, |
| 19 | trust_remote_code: bool, |
| 20 | cache_dir: str | os.PathLike | None = None, |
| 21 | force_download: bool = False, |
| 22 | resume_download: bool | None = None, |
| 23 | proxies: dict[str, str] | None = None, |
| 24 | token: bool | str | None = None, |
| 25 | revision: str | None = None, |
| 26 | local_files_only: bool = False, |
| 27 | repo_type: str | None = None, |
| 28 | code_revision: str | None = None, |
| 29 | warn_on_fail: bool = True, |
| 30 | **kwargs, |
| 31 | ) -> type | None: |
| 32 | """As `transformers.dynamic_module_utils.get_class_from_dynamic_module`, |
| 33 | but ignoring any errors. |
| 34 | """ |
| 35 | try: |
| 36 | resolve_trust_remote_code( |
| 37 | trust_remote_code, |
| 38 | pretrained_model_name_or_path, |
| 39 | has_local_code=False, |
| 40 | has_remote_code=True, |
| 41 | ) |
| 42 | |
| 43 | return get_class_from_dynamic_module( |
| 44 | class_reference, |
| 45 | pretrained_model_name_or_path, |
| 46 | cache_dir=cache_dir, |
| 47 | force_download=force_download, |
| 48 | resume_download=resume_download, |
| 49 | proxies=proxies, |
| 50 | token=token, |
| 51 | revision=revision, |
| 52 | local_files_only=local_files_only, |
| 53 | repo_type=repo_type, |
| 54 | code_revision=code_revision, |
| 55 | **kwargs, |
| 56 | ) |
| 57 | except Exception: |
| 58 | location = "ModelScope" if envs.VLLM_USE_MODELSCOPE else "HF Hub" |
| 59 | |
| 60 | if warn_on_fail: |
| 61 | logger.warning( |
| 62 | "Unable to load %s from %s on %s.", |
| 63 | class_reference, |
| 64 | pretrained_model_name_or_path, |
| 65 | location, |
| 66 | exc_info=True, |
| 67 | ) |
| 68 | |
| 69 | return None |