CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
auto_factory.py883 linesDownload Raw Back to auto
1# coding=utf-82# Copyright 2021 The HuggingFace Inc. team.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15"""Factory function to build auto-model classes."""16 17import copy18import importlib19import json20import os21import warnings22from collections import OrderedDict23from collections.abc import Iterator24from typing import Any, TypeVar, Union25 26from ...configuration_utils import PretrainedConfig27from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code28from ...utils import (29    CONFIG_NAME,30    cached_file,31    copy_func,32    extract_commit_hash,33    find_adapter_config_file,34    is_peft_available,35    is_torch_available,36    logging,37    requires_backends,38)39from .configuration_auto import AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings40 41 42if is_torch_available():43    from ...generation import GenerationMixin44 45 46logger = logging.get_logger(__name__)47 48_T = TypeVar("_T")49# Tokenizers will depend on packages installed, too much variance and there are no common base or Protocol50_LazyAutoMappingValue = tuple[Union[type[Any], None], Union[type[Any], None]]51 52CLASS_DOCSTRING = """53    This is a generic model class that will be instantiated as one of the model classes of the library when created54    with the [`~BaseAutoModelClass.from_pretrained`] class method or the [`~BaseAutoModelClass.from_config`] class55    method.56 57    This class cannot be instantiated directly using `__init__()` (throws an error).58"""59 60FROM_CONFIG_DOCSTRING = """61        Instantiates one of the model classes of the library from a configuration.62 63        Note:64            Loading a model from its configuration file does **not** load the model weights. It only affects the65            model's configuration. Use [`~BaseAutoModelClass.from_pretrained`] to load the model weights.66 67        Args:68            config ([`PretrainedConfig`]):69                The model class to instantiate is selected based on the configuration class:70 71                List options72            attn_implementation (`str`, *optional*):73                The attention implementation to use in the model (if relevant). Can be any of `"eager"` (manual implementation of the attention), `"sdpa"` (using [`F.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html)), or `"flash_attention_2"` (using [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention)). By default, if available, SDPA will be used for torch>=2.1.1. The default is otherwise the manual `"eager"` implementation.74 75        Examples:76 77        ```python78        >>> from transformers import AutoConfig, BaseAutoModelClass79 80        >>> # Download configuration from huggingface.co and cache.81        >>> config = AutoConfig.from_pretrained("checkpoint_placeholder")82        >>> model = BaseAutoModelClass.from_config(config)83        ```84"""85 86FROM_PRETRAINED_TORCH_DOCSTRING = """87        Instantiate one of the model classes of the library from a pretrained model.88 89        The model class to instantiate is selected based on the `model_type` property of the config object (either90        passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's missing, by91        falling back to using pattern matching on `pretrained_model_name_or_path`:92 93        List options94 95        The model is set in evaluation mode by default using `model.eval()` (so for instance, dropout modules are96        deactivated). To train the model, you should first set it back in training mode with `model.train()`97 98        Args:99            pretrained_model_name_or_path (`str` or `os.PathLike`):100                Can be either:101 102                    - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.103                    - A path to a *directory* containing model weights saved using104                      [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.105                    - A path or url to a *tensorflow index checkpoint file* (e.g, `./tf_model/model.ckpt.index`). In106                      this case, `from_tf` should be set to `True` and a configuration object should be provided as107                      `config` argument. This loading path is slower than converting the TensorFlow checkpoint in a108                      PyTorch model using the provided conversion scripts and loading the PyTorch model afterwards.109            model_args (additional positional arguments, *optional*):110                Will be passed along to the underlying model `__init__()` method.111            config ([`PretrainedConfig`], *optional*):112                Configuration for the model to use instead of an automatically loaded configuration. Configuration can113                be automatically loaded when:114 115                    - The model is a model provided by the library (loaded with the *model id* string of a pretrained116                      model).117                    - The model was saved using [`~PreTrainedModel.save_pretrained`] and is reloaded by supplying the118                      save directory.119                    - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a120                      configuration JSON file named *config.json* is found in the directory.121            state_dict (*dict[str, torch.Tensor]*, *optional*):122                A state dictionary to use instead of a state dictionary loaded from saved weights file.123 124                This option can be used if you want to create a model from a pretrained configuration but load your own125                weights. In this case though, you should check if using [`~PreTrainedModel.save_pretrained`] and126                [`~PreTrainedModel.from_pretrained`] is not a simpler option.127            cache_dir (`str` or `os.PathLike`, *optional*):128                Path to a directory in which a downloaded pretrained model configuration should be cached if the129                standard cache should not be used.130            from_tf (`bool`, *optional*, defaults to `False`):131                Load the model weights from a TensorFlow checkpoint save file (see docstring of132                `pretrained_model_name_or_path` argument).133            force_download (`bool`, *optional*, defaults to `False`):134                Whether or not to force the (re-)download of the model weights and configuration files, overriding the135                cached versions if they exist.136            resume_download:137                Deprecated and ignored. All downloads are now resumed by default when possible.138                Will be removed in v5 of Transformers.139            proxies (`dict[str, str]`, *optional*):140                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',141                'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.142            output_loading_info(`bool`, *optional*, defaults to `False`):143                Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.144            local_files_only(`bool`, *optional*, defaults to `False`):145                Whether or not to only look at local files (e.g., not try downloading the model).146            revision (`str`, *optional*, defaults to `"main"`):147                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a148                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any149                identifier allowed by git.150            trust_remote_code (`bool`, *optional*, defaults to `False`):151                Whether or not to allow for custom models defined on the Hub in their own modeling files. This option152                should only be set to `True` for repositories you trust and in which you have read the code, as it will153                execute code present on the Hub on your local machine.154            code_revision (`str`, *optional*, defaults to `"main"`):155                The specific revision to use for the code on the Hub, if the code leaves in a different repository than156                the rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-based157                system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier158                allowed by git.159            kwargs (additional keyword arguments, *optional*):160                Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,161                `output_attentions=True`). Behaves differently depending on whether a `config` is provided or162                automatically loaded:163 164                    - If a configuration is provided with `config`, `**kwargs` will be directly passed to the165                      underlying model's `__init__` method (we assume all relevant updates to the configuration have166                      already been done)167                    - If a configuration is not provided, `kwargs` will be first passed to the configuration class168                      initialization function ([`~PretrainedConfig.from_pretrained`]). Each key of `kwargs` that169                      corresponds to a configuration attribute will be used to override said attribute with the170                      supplied `kwargs` value. Remaining keys that do not correspond to any configuration attribute171                      will be passed to the underlying model's `__init__` function.172 173        Examples:174 175        ```python176        >>> from transformers import AutoConfig, BaseAutoModelClass177 178        >>> # Download model and configuration from huggingface.co and cache.179        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder")180 181        >>> # Update configuration during loading182        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder", output_attentions=True)183        >>> model.config.output_attentions184        True185 186        >>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)187        >>> config = AutoConfig.from_pretrained("./tf_model/shortcut_placeholder_tf_model_config.json")188        >>> model = BaseAutoModelClass.from_pretrained(189        ...     "./tf_model/shortcut_placeholder_tf_checkpoint.ckpt.index", from_tf=True, config=config190        ... )191        ```192"""193 194FROM_PRETRAINED_TF_DOCSTRING = """195        Instantiate one of the model classes of the library from a pretrained model.196 197        The model class to instantiate is selected based on the `model_type` property of the config object (either198        passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's missing, by199        falling back to using pattern matching on `pretrained_model_name_or_path`:200 201        List options202 203        Args:204            pretrained_model_name_or_path (`str` or `os.PathLike`):205                Can be either:206 207                    - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.208                    - A path to a *directory* containing model weights saved using209                      [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.210                    - A path or url to a *PyTorch state_dict save file* (e.g, `./pt_model/pytorch_model.bin`). In this211                      case, `from_pt` should be set to `True` and a configuration object should be provided as `config`212                      argument. This loading path is slower than converting the PyTorch model in a TensorFlow model213                      using the provided conversion scripts and loading the TensorFlow model afterwards.214            model_args (additional positional arguments, *optional*):215                Will be passed along to the underlying model `__init__()` method.216            config ([`PretrainedConfig`], *optional*):217                Configuration for the model to use instead of an automatically loaded configuration. Configuration can218                be automatically loaded when:219 220                    - The model is a model provided by the library (loaded with the *model id* string of a pretrained221                      model).222                    - The model was saved using [`~PreTrainedModel.save_pretrained`] and is reloaded by supplying the223                      save directory.224                    - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a225                      configuration JSON file named *config.json* is found in the directory.226            cache_dir (`str` or `os.PathLike`, *optional*):227                Path to a directory in which a downloaded pretrained model configuration should be cached if the228                standard cache should not be used.229            from_pt (`bool`, *optional*, defaults to `False`):230                Load the model weights from a PyTorch checkpoint save file (see docstring of231                `pretrained_model_name_or_path` argument).232            force_download (`bool`, *optional*, defaults to `False`):233                Whether or not to force the (re-)download of the model weights and configuration files, overriding the234                cached versions if they exist.235            resume_download:236                Deprecated and ignored. All downloads are now resumed by default when possible.237                Will be removed in v5 of Transformers.238            proxies (`dict[str, str]`, *optional*):239                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',240                'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.241            output_loading_info(`bool`, *optional*, defaults to `False`):242                Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.243            local_files_only(`bool`, *optional*, defaults to `False`):244                Whether or not to only look at local files (e.g., not try downloading the model).245            revision (`str`, *optional*, defaults to `"main"`):246                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a247                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any248                identifier allowed by git.249            trust_remote_code (`bool`, *optional*, defaults to `False`):250                Whether or not to allow for custom models defined on the Hub in their own modeling files. This option251                should only be set to `True` for repositories you trust and in which you have read the code, as it will252                execute code present on the Hub on your local machine.253            code_revision (`str`, *optional*, defaults to `"main"`):254                The specific revision to use for the code on the Hub, if the code leaves in a different repository than255                the rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-based256                system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier257                allowed by git.258            kwargs (additional keyword arguments, *optional*):259                Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,260                `output_attentions=True`). Behaves differently depending on whether a `config` is provided or261                automatically loaded:262 263                    - If a configuration is provided with `config`, `**kwargs` will be directly passed to the264                      underlying model's `__init__` method (we assume all relevant updates to the configuration have265                      already been done)266                    - If a configuration is not provided, `kwargs` will be first passed to the configuration class267                      initialization function ([`~PretrainedConfig.from_pretrained`]). Each key of `kwargs` that268                      corresponds to a configuration attribute will be used to override said attribute with the269                      supplied `kwargs` value. Remaining keys that do not correspond to any configuration attribute270                      will be passed to the underlying model's `__init__` function.271 272        Examples:273 274        ```python275        >>> from transformers import AutoConfig, BaseAutoModelClass276 277        >>> # Download model and configuration from huggingface.co and cache.278        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder")279 280        >>> # Update configuration during loading281        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder", output_attentions=True)282        >>> model.config.output_attentions283        True284 285        >>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)286        >>> config = AutoConfig.from_pretrained("./pt_model/shortcut_placeholder_pt_model_config.json")287        >>> model = BaseAutoModelClass.from_pretrained(288        ...     "./pt_model/shortcut_placeholder_pytorch_model.bin", from_pt=True, config=config289        ... )290        ```291"""292 293FROM_PRETRAINED_FLAX_DOCSTRING = """294        Instantiate one of the model classes of the library from a pretrained model.295 296        The model class to instantiate is selected based on the `model_type` property of the config object (either297        passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's missing, by298        falling back to using pattern matching on `pretrained_model_name_or_path`:299 300        List options301 302        Args:303            pretrained_model_name_or_path (`str` or `os.PathLike`):304                Can be either:305 306                    - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.307                    - A path to a *directory* containing model weights saved using308                      [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.309                    - A path or url to a *PyTorch state_dict save file* (e.g, `./pt_model/pytorch_model.bin`). In this310                      case, `from_pt` should be set to `True` and a configuration object should be provided as `config`311                      argument. This loading path is slower than converting the PyTorch model in a TensorFlow model312                      using the provided conversion scripts and loading the TensorFlow model afterwards.313            model_args (additional positional arguments, *optional*):314                Will be passed along to the underlying model `__init__()` method.315            config ([`PretrainedConfig`], *optional*):316                Configuration for the model to use instead of an automatically loaded configuration. Configuration can317                be automatically loaded when:318 319                    - The model is a model provided by the library (loaded with the *model id* string of a pretrained320                      model).321                    - The model was saved using [`~PreTrainedModel.save_pretrained`] and is reloaded by supplying the322                      save directory.323                    - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a324                      configuration JSON file named *config.json* is found in the directory.325            cache_dir (`str` or `os.PathLike`, *optional*):326                Path to a directory in which a downloaded pretrained model configuration should be cached if the327                standard cache should not be used.328            from_pt (`bool`, *optional*, defaults to `False`):329                Load the model weights from a PyTorch checkpoint save file (see docstring of330                `pretrained_model_name_or_path` argument).331            force_download (`bool`, *optional*, defaults to `False`):332                Whether or not to force the (re-)download of the model weights and configuration files, overriding the333                cached versions if they exist.334            resume_download:335                Deprecated and ignored. All downloads are now resumed by default when possible.336                Will be removed in v5 of Transformers.337            proxies (`dict[str, str]`, *optional*):338                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',339                'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.340            output_loading_info(`bool`, *optional*, defaults to `False`):341                Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.342            local_files_only(`bool`, *optional*, defaults to `False`):343                Whether or not to only look at local files (e.g., not try downloading the model).344            revision (`str`, *optional*, defaults to `"main"`):345                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a346                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any347                identifier allowed by git.348            trust_remote_code (`bool`, *optional*, defaults to `False`):349                Whether or not to allow for custom models defined on the Hub in their own modeling files. This option350                should only be set to `True` for repositories you trust and in which you have read the code, as it will351                execute code present on the Hub on your local machine.352            code_revision (`str`, *optional*, defaults to `"main"`):353                The specific revision to use for the code on the Hub, if the code leaves in a different repository than354                the rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-based355                system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier356                allowed by git.357            kwargs (additional keyword arguments, *optional*):358                Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,359                `output_attentions=True`). Behaves differently depending on whether a `config` is provided or360                automatically loaded:361 362                    - If a configuration is provided with `config`, `**kwargs` will be directly passed to the363                      underlying model's `__init__` method (we assume all relevant updates to the configuration have364                      already been done)365                    - If a configuration is not provided, `kwargs` will be first passed to the configuration class366                      initialization function ([`~PretrainedConfig.from_pretrained`]). Each key of `kwargs` that367                      corresponds to a configuration attribute will be used to override said attribute with the368                      supplied `kwargs` value. Remaining keys that do not correspond to any configuration attribute369                      will be passed to the underlying model's `__init__` function.370 371        Examples:372 373        ```python374        >>> from transformers import AutoConfig, BaseAutoModelClass375 376        >>> # Download model and configuration from huggingface.co and cache.377        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder")378 379        >>> # Update configuration during loading380        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder", output_attentions=True)381        >>> model.config.output_attentions382        True383 384        >>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)385        >>> config = AutoConfig.from_pretrained("./pt_model/shortcut_placeholder_pt_model_config.json")386        >>> model = BaseAutoModelClass.from_pretrained(387        ...     "./pt_model/shortcut_placeholder_pytorch_model.bin", from_pt=True, config=config388        ... )389        ```390"""391 392 393def _get_model_class(config, model_mapping):394    supported_models = model_mapping[type(config)]395    if not isinstance(supported_models, (list, tuple)):396        return supported_models397 398    name_to_model = {model.__name__: model for model in supported_models}399    architectures = getattr(config, "architectures", [])400    for arch in architectures:401        if arch in name_to_model:402            return name_to_model[arch]403        elif f"TF{arch}" in name_to_model:404            return name_to_model[f"TF{arch}"]405        elif f"Flax{arch}" in name_to_model:406            return name_to_model[f"Flax{arch}"]407 408    # If not architecture is set in the config or match the supported models, the first element of the tuple is the409    # defaults.410    return supported_models[0]411 412 413class _BaseAutoModelClass:414    # Base class for auto models.415    _model_mapping = None416 417    def __init__(self, *args, **kwargs) -> None:418        raise OSError(419            f"{self.__class__.__name__} is designed to be instantiated "420            f"using the `{self.__class__.__name__}.from_pretrained(pretrained_model_name_or_path)` or "421            f"`{self.__class__.__name__}.from_config(config)` methods."422        )423 424    @classmethod425    def from_config(cls, config, **kwargs):426        trust_remote_code = kwargs.pop("trust_remote_code", None)427        has_remote_code = hasattr(config, "auto_map") and cls.__name__ in config.auto_map428        has_local_code = type(config) in cls._model_mapping429        if has_remote_code:430            class_ref = config.auto_map[cls.__name__]431            if "--" in class_ref:432                upstream_repo = class_ref.split("--")[0]433            else:434                upstream_repo = None435            trust_remote_code = resolve_trust_remote_code(436                trust_remote_code, config._name_or_path, has_local_code, has_remote_code, upstream_repo=upstream_repo437            )438 439        if has_remote_code and trust_remote_code:440            if "--" in class_ref:441                repo_id, class_ref = class_ref.split("--")442            else:443                repo_id = config.name_or_path444            model_class = get_class_from_dynamic_module(class_ref, repo_id, **kwargs)445            # This block handles the case where the user is loading a model with `trust_remote_code=True`446            # but a library model exists with the same name. We don't want to override the autoclass447            # mappings in this case, or all future loads of that model will be the remote code model.448            if not has_local_code:449                cls.register(config.__class__, model_class, exist_ok=True)450                model_class.register_for_auto_class(auto_class=cls)451            _ = kwargs.pop("code_revision", None)452            model_class = add_generation_mixin_to_remote_model(model_class)453            return model_class._from_config(config, **kwargs)454        elif type(config) in cls._model_mapping:455            model_class = _get_model_class(config, cls._model_mapping)456            return model_class._from_config(config, **kwargs)457 458        raise ValueError(459            f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"460            f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping)}."461        )462 463    @classmethod464    def _prepare_config_for_auto_class(cls, config: PretrainedConfig) -> PretrainedConfig:465        """Additional autoclass-specific config post-loading manipulation. May be overridden in subclasses."""466        return config467 468    @classmethod469    def from_pretrained(cls, pretrained_model_name_or_path: Union[str, os.PathLike[str]], *model_args, **kwargs):470        config = kwargs.pop("config", None)471        trust_remote_code = kwargs.get("trust_remote_code")472        kwargs["_from_auto"] = True473        hub_kwargs_names = [474            "cache_dir",475            "force_download",476            "local_files_only",477            "proxies",478            "resume_download",479            "revision",480            "subfolder",481            "use_auth_token",482            "token",483        ]484        hub_kwargs = {name: kwargs.pop(name) for name in hub_kwargs_names if name in kwargs}485        code_revision = kwargs.pop("code_revision", None)486        commit_hash = kwargs.pop("_commit_hash", None)487        adapter_kwargs = kwargs.pop("adapter_kwargs", None)488 489        token = hub_kwargs.pop("token", None)490        use_auth_token = hub_kwargs.pop("use_auth_token", None)491        if use_auth_token is not None:492            warnings.warn(493                "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.",494                FutureWarning,495            )496            if token is not None:497                raise ValueError(498                    "`token` and `use_auth_token` are both specified. Please set only the argument `token`."499                )500            token = use_auth_token501 502        if token is not None:503            hub_kwargs["token"] = token504 505        if commit_hash is None:506            if not isinstance(config, PretrainedConfig):507                # We make a call to the config file first (which may be absent) to get the commit hash as soon as possible508                resolved_config_file = cached_file(509                    pretrained_model_name_or_path,510                    CONFIG_NAME,511                    _raise_exceptions_for_gated_repo=False,512                    _raise_exceptions_for_missing_entries=False,513                    _raise_exceptions_for_connection_errors=False,514                    **hub_kwargs,515                )516                commit_hash = extract_commit_hash(resolved_config_file, commit_hash)517            else:518                commit_hash = getattr(config, "_commit_hash", None)519 520        if is_peft_available():521            if adapter_kwargs is None:522                adapter_kwargs = {}523                if token is not None:524                    adapter_kwargs["token"] = token525 526            maybe_adapter_path = find_adapter_config_file(527                pretrained_model_name_or_path, _commit_hash=commit_hash, **adapter_kwargs528            )529 530            if maybe_adapter_path is not None:531                with open(maybe_adapter_path, "r", encoding="utf-8") as f:532                    adapter_config = json.load(f)533 534                    adapter_kwargs["_adapter_model_path"] = pretrained_model_name_or_path535                    pretrained_model_name_or_path = adapter_config["base_model_name_or_path"]536 537        if not isinstance(config, PretrainedConfig):538            kwargs_orig = copy.deepcopy(kwargs)539            # ensure not to pollute the config object with dtype="auto" - since it's540            # meaningless in the context of the config object - torch.dtype values are acceptable541            if kwargs.get("torch_dtype") == "auto":542                _ = kwargs.pop("torch_dtype")543            if kwargs.get("dtype") == "auto":544                _ = kwargs.pop("dtype")545            # to not overwrite the quantization_config if config has a quantization_config546            if kwargs.get("quantization_config") is not None:547                _ = kwargs.pop("quantization_config")548 549            config, kwargs = AutoConfig.from_pretrained(550                pretrained_model_name_or_path,551                return_unused_kwargs=True,552                code_revision=code_revision,553                _commit_hash=commit_hash,554                **hub_kwargs,555                **kwargs,556            )557 558            # if torch_dtype=auto was passed here, ensure to pass it on559            if kwargs_orig.get("torch_dtype", None) == "auto":560                kwargs["torch_dtype"] = "auto"561            if kwargs_orig.get("dtype", None) == "auto":562                kwargs["dtype"] = "auto"563            if kwargs_orig.get("quantization_config", None) is not None:564                kwargs["quantization_config"] = kwargs_orig["quantization_config"]565 566        has_remote_code = hasattr(config, "auto_map") and cls.__name__ in config.auto_map567        has_local_code = type(config) in cls._model_mapping568        upstream_repo = None569        if has_remote_code:570            class_ref = config.auto_map[cls.__name__]571            if "--" in class_ref:572                upstream_repo = class_ref.split("--")[0]573        trust_remote_code = resolve_trust_remote_code(574            trust_remote_code,575            pretrained_model_name_or_path,576            has_local_code,577            has_remote_code,578            upstream_repo=upstream_repo,579        )580        kwargs["trust_remote_code"] = trust_remote_code581 582        # Set the adapter kwargs583        kwargs["adapter_kwargs"] = adapter_kwargs584 585        if has_remote_code and trust_remote_code:586            model_class = get_class_from_dynamic_module(587                class_ref, pretrained_model_name_or_path, code_revision=code_revision, **hub_kwargs, **kwargs588            )589            _ = hub_kwargs.pop("code_revision", None)590            # This block handles the case where the user is loading a model with `trust_remote_code=True`591            # but a library model exists with the same name. We don't want to override the autoclass592            # mappings in this case, or all future loads of that model will be the remote code model.593            if not has_local_code:594                cls.register(config.__class__, model_class, exist_ok=True)595                model_class.register_for_auto_class(auto_class=cls)596            model_class = add_generation_mixin_to_remote_model(model_class)597            return model_class.from_pretrained(598                pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs599            )600        elif type(config) in cls._model_mapping:601            model_class = _get_model_class(config, cls._model_mapping)602            if model_class.config_class == config.sub_configs.get("text_config", None):603                config = config.get_text_config()604            return model_class.from_pretrained(605                pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs606            )607        raise ValueError(608            f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"609            f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping)}."610        )611 612    @classmethod613    def register(cls, config_class, model_class, exist_ok=False) -> None:614        """615        Register a new model for this class.616 617        Args:618            config_class ([`PretrainedConfig`]):619                The configuration corresponding to the model to register.620            model_class ([`PreTrainedModel`]):621                The model to register.622        """623        if hasattr(model_class, "config_class") and model_class.config_class.__name__ != config_class.__name__:624            raise ValueError(625                "The model class you are passing has a `config_class` attribute that is not consistent with the "626                f"config class you passed (model has {model_class.config_class} and you passed {config_class}. Fix "627                "one of those so they match!"628            )629        cls._model_mapping.register(config_class, model_class, exist_ok=exist_ok)630 631 632class _BaseAutoBackboneClass(_BaseAutoModelClass):633    # Base class for auto backbone models.634    _model_mapping = None635 636    @classmethod637    def _load_timm_backbone_from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):638        requires_backends(cls, ["vision", "timm"])639        from ...models.timm_backbone import TimmBackboneConfig640 641        config = kwargs.pop("config", TimmBackboneConfig())642 643        if kwargs.get("out_features") is not None:644            raise ValueError("Cannot specify `out_features` for timm backbones")645 646        if kwargs.get("output_loading_info", False):647            raise ValueError("Cannot specify `output_loading_info=True` when loading from timm")648 649        num_channels = kwargs.pop("num_channels", config.num_channels)650        features_only = kwargs.pop("features_only", config.features_only)651        use_pretrained_backbone = kwargs.pop("use_pretrained_backbone", config.use_pretrained_backbone)652        out_indices = kwargs.pop("out_indices", config.out_indices)653        config = TimmBackboneConfig(654            backbone=pretrained_model_name_or_path,655            num_channels=num_channels,656            features_only=features_only,657            use_pretrained_backbone=use_pretrained_backbone,658            out_indices=out_indices,659        )660        return super().from_config(config, **kwargs)661 662    @classmethod663    def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):664        use_timm_backbone = kwargs.pop("use_timm_backbone", False)665        if use_timm_backbone:666            return cls._load_timm_backbone_from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)667 668        return super().from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)669 670 671def insert_head_doc(docstring, head_doc: str = ""):672    if len(head_doc) > 0:673        return docstring.replace(674            "one of the model classes of the library ",675            f"one of the model classes of the library (with a {head_doc} head) ",676        )677    return docstring.replace(678        "one of the model classes of the library ", "one of the base model classes of the library "679    )680 681 682def auto_class_update(cls, checkpoint_for_example: str = "google-bert/bert-base-cased", head_doc: str = ""):683    # Create a new class with the right name from the base class684    model_mapping = cls._model_mapping685    name = cls.__name__686    class_docstring = insert_head_doc(CLASS_DOCSTRING, head_doc=head_doc)687    cls.__doc__ = class_docstring.replace("BaseAutoModelClass", name)688 689    # Now we need to copy and re-register `from_config` and `from_pretrained` as class methods otherwise we can't690    # have a specific docstrings for them.691    from_config = copy_func(_BaseAutoModelClass.from_config)692    from_config_docstring = insert_head_doc(FROM_CONFIG_DOCSTRING, head_doc=head_doc)693    from_config_docstring = from_config_docstring.replace("BaseAutoModelClass", name)694    from_config_docstring = from_config_docstring.replace("checkpoint_placeholder", checkpoint_for_example)695    from_config.__doc__ = from_config_docstring696    from_config = replace_list_option_in_docstrings(model_mapping._model_mapping, use_model_types=False)(from_config)697    cls.from_config = classmethod(from_config)698 699    if name.startswith("TF"):700        from_pretrained_docstring = FROM_PRETRAINED_TF_DOCSTRING701    elif name.startswith("Flax"):702        from_pretrained_docstring = FROM_PRETRAINED_FLAX_DOCSTRING703    else:704        from_pretrained_docstring = FROM_PRETRAINED_TORCH_DOCSTRING705    from_pretrained = copy_func(_BaseAutoModelClass.from_pretrained)706    from_pretrained_docstring = insert_head_doc(from_pretrained_docstring, head_doc=head_doc)707    from_pretrained_docstring = from_pretrained_docstring.replace("BaseAutoModelClass", name)708    from_pretrained_docstring = from_pretrained_docstring.replace("checkpoint_placeholder", checkpoint_for_example)709    shortcut = checkpoint_for_example.split("/")[-1].split("-")[0]710    from_pretrained_docstring = from_pretrained_docstring.replace("shortcut_placeholder", shortcut)711    from_pretrained.__doc__ = from_pretrained_docstring712    from_pretrained = replace_list_option_in_docstrings(model_mapping._model_mapping)(from_pretrained)713    cls.from_pretrained = classmethod(from_pretrained)714    return cls715 716 717def get_values(model_mapping):718    result = []719    for model in model_mapping.values():720        if isinstance(model, (list, tuple)):721            result += list(model)722        else:723            result.append(model)724 725    return result726 727 728def getattribute_from_module(module, attr):729    if attr is None:730        return None731    if isinstance(attr, tuple):732        return tuple(getattribute_from_module(module, a) for a in attr)733    if hasattr(module, attr):734        return getattr(module, attr)735    # Some of the mappings have entries model_type -> object of another model type. In that case we try to grab the736    # object at the top level.737    transformers_module = importlib.import_module("transformers")738 739    if module != transformers_module:740        try:741            return getattribute_from_module(transformers_module, attr)742        except ValueError:743            raise ValueError(f"Could not find {attr} neither in {module} nor in {transformers_module}!")744    else:745        raise ValueError(f"Could not find {attr} in {transformers_module}!")746 747 748def add_generation_mixin_to_remote_model(model_class):749    """750    Adds `GenerationMixin` to the inheritance of `model_class`, if `model_class` is a PyTorch model.751 752    This function is used for backwards compatibility purposes: in v4.45, we've started a deprecation cycle to make753    `PreTrainedModel` stop inheriting from `GenerationMixin`. Without this function, older models dynamically loaded754    from the Hub may not have the `generate` method after we remove the inheritance.755    """756    # 1. If it is not a PT model (i.e. doesn't inherit Module), do nothing757    if "torch.nn.modules.module.Module" not in str(model_class.__mro__):758        return model_class759 760    # 2. If it already **directly** inherits from GenerationMixin, do nothing761    if "GenerationMixin" in str(model_class.__bases__):762        return model_class763 764    # 3. Prior to v4.45, we could detect whether a model was `generate`-compatible if it had its own `generate` and/or765    # `prepare_inputs_for_generation` method.766    has_custom_generate_in_class = hasattr(model_class, "generate") and "GenerationMixin" not in str(767        getattr(model_class, "generate")768    )769    has_custom_prepare_inputs = hasattr(model_class, "prepare_inputs_for_generation") and "GenerationMixin" not in str(770        getattr(model_class, "prepare_inputs_for_generation")771    )772    if has_custom_generate_in_class or has_custom_prepare_inputs:773        model_class_with_generation_mixin = type(774            model_class.__name__, (model_class, GenerationMixin), {**model_class.__dict__}775        )776        return model_class_with_generation_mixin777    return model_class778 779 780class _LazyAutoMapping(OrderedDict[type[PretrainedConfig], _LazyAutoMappingValue]):781    """782    " A mapping config to object (model or tokenizer for instance) that will load keys and values when it is accessed.783 784    Args:785        - config_mapping: The map model type to config class786        - model_mapping: The map model type to model (or tokenizer) class787    """788 789    def __init__(self, config_mapping, model_mapping) -> None:790        self._config_mapping = config_mapping791        self._reverse_config_mapping = {v: k for k, v in config_mapping.items()}792        self._model_mapping = model_mapping793        self._model_mapping._model_mapping = self794        self._extra_content = {}795        self._modules = {}796 797    def __len__(self) -> int:798        common_keys = set(self._config_mapping.keys()).intersection(self._model_mapping.keys())799        return len(common_keys) + len(self._extra_content)800 801    def __getitem__(self, key: type[PretrainedConfig]) -> _LazyAutoMappingValue:802        if key in self._extra_content:803            return self._extra_content[key]804        model_type = self._reverse_config_mapping[key.__name__]805        if model_type in self._model_mapping:806            model_name = self._model_mapping[model_type]807            return self._load_attr_from_module(model_type, model_name)808 809        # Maybe there was several model types associated with this config.810        model_types = [k for k, v in self._config_mapping.items() if v == key.__name__]811        for mtype in model_types:812            if mtype in self._model_mapping:813                model_name = self._model_mapping[mtype]814                return self._load_attr_from_module(mtype, model_name)815        raise KeyError(key)816 817    def _load_attr_from_module(self, model_type, attr):818        module_name = model_type_to_module_name(model_type)819        if module_name not in self._modules:820            self._modules[module_name] = importlib.import_module(f".{module_name}", "transformers.models")821        return getattribute_from_module(self._modules[module_name], attr)822 823    def keys(self) -> list[type[PretrainedConfig]]:824        mapping_keys = [825            self._load_attr_from_module(key, name)826            for key, name in self._config_mapping.items()827            if key in self._model_mapping828        ]829        return mapping_keys + list(self._extra_content.keys())830 831    def get(self, key: type[PretrainedConfig], default: _T) -> Union[_LazyAutoMappingValue, _T]:832        try:833            return self.__getitem__(key)834        except KeyError:835            return default836 837    def __bool__(self) -> bool:838        return bool(self.keys())839 840    def values(self) -> list[_LazyAutoMappingValue]:841        mapping_values = [842            self._load_attr_from_module(key, name)843            for key, name in self._model_mapping.items()844            if key in self._config_mapping845        ]846        return mapping_values + list(self._extra_content.values())847 848    def items(self) -> list[tuple[type[PretrainedConfig], _LazyAutoMappingValue]]:849        mapping_items = [850            (851                self._load_attr_from_module(key, self._config_mapping[key]),852                self._load_attr_from_module(key, self._model_mapping[key]),853            )854            for key in self._model_mapping855            if key in self._config_mapping856        ]857        return mapping_items + list(self._extra_content.items())858 859    def __iter__(self) -> Iterator[type[PretrainedConfig]]:860        return iter(self.keys())861 862    def __contains__(self, item: type) -> bool:863        if item in self._extra_content:864            return True865        if not hasattr(item, "__name__") or item.__name__ not in self._reverse_config_mapping:866            return False867        model_type = self._reverse_config_mapping[item.__name__]868        return model_type in self._model_mapping869 870    def register(self, key: type[PretrainedConfig], value: _LazyAutoMappingValue, exist_ok=False) -> None:871        """872        Register a new model in this mapping.873        """874        if hasattr(key, "__name__") and key.__name__ in self._reverse_config_mapping:875            model_type = self._reverse_config_mapping[key.__name__]876            if model_type in self._model_mapping and not exist_ok:877                raise ValueError(f"'{key}' is already used by a Transformers model.")878 879        self._extra_content[key] = value880 881 882__all__ = ["get_values"]883 
Aluode/PerceptionLabPortable · CoolFace