
    sg                     J   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZ dd	lmZmZmZ  e       rdd
lmZ  ej8                  e      ZdZdZ dZ!dZ"dZ#d Z$ G d d      Z% G d de%      Z&ddZ'ddZ(d Z)d Z*d Z+ G d de      Z,y)z-Factory function to build auto-model classes.    N)OrderedDict   )PretrainedConfig)get_class_from_dynamic_moduleresolve_trust_remote_code)	CONFIG_NAMEcached_file	copy_funcextract_commit_hashfind_adapter_config_fileis_peft_availableis_torch_availableloggingrequires_backends   )
AutoConfigmodel_type_to_module_name!replace_list_option_in_docstrings)GenerationMixinaJ  
    This is a generic model class that will be instantiated as one of the model classes of the library when created
    with the [`~BaseAutoModelClass.from_pretrained`] class method or the [`~BaseAutoModelClass.from_config`] class
    method.

    This class cannot be instantiated directly using `__init__()` (throws an error).
ax  
        Instantiates one of the model classes of the library from a configuration.

        Note:
            Loading a model from its configuration file does **not** load the model weights. It only affects the
            model's configuration. Use [`~BaseAutoModelClass.from_pretrained`] to load the model weights.

        Args:
            config ([`PretrainedConfig`]):
                The model class to instantiate is selected based on the configuration class:

                List options
            attn_implementation (`str`, *optional*):
                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.

        Examples:

        ```python
        >>> from transformers import AutoConfig, BaseAutoModelClass

        >>> # Download configuration from huggingface.co and cache.
        >>> config = AutoConfig.from_pretrained("checkpoint_placeholder")
        >>> model = BaseAutoModelClass.from_config(config)
        ```
ac  
        Instantiate one of the model classes of the library from a pretrained model.

        The model class to instantiate is selected based on the `model_type` property of the config object (either
        passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's missing, by
        falling back to using pattern matching on `pretrained_model_name_or_path`:

        List options

        The model is set in evaluation mode by default using `model.eval()` (so for instance, dropout modules are
        deactivated). To train the model, you should first set it back in training mode with `model.train()`

        Args:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                Can be either:

                    - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
                    - A path to a *directory* containing model weights saved using
                      [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
                    - A path or url to a *tensorflow index checkpoint file* (e.g, `./tf_model/model.ckpt.index`). In
                      this case, `from_tf` should be set to `True` and a configuration object should be provided as
                      `config` argument. This loading path is slower than converting the TensorFlow checkpoint in a
                      PyTorch model using the provided conversion scripts and loading the PyTorch model afterwards.
            model_args (additional positional arguments, *optional*):
                Will be passed along to the underlying model `__init__()` method.
            config ([`PretrainedConfig`], *optional*):
                Configuration for the model to use instead of an automatically loaded configuration. Configuration can
                be automatically loaded when:

                    - The model is a model provided by the library (loaded with the *model id* string of a pretrained
                      model).
                    - The model was saved using [`~PreTrainedModel.save_pretrained`] and is reloaded by supplying the
                      save directory.
                    - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a
                      configuration JSON file named *config.json* is found in the directory.
            state_dict (*Dict[str, torch.Tensor]*, *optional*):
                A state dictionary to use instead of a state dictionary loaded from saved weights file.

                This option can be used if you want to create a model from a pretrained configuration but load your own
                weights. In this case though, you should check if using [`~PreTrainedModel.save_pretrained`] and
                [`~PreTrainedModel.from_pretrained`] is not a simpler option.
            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.
            from_tf (`bool`, *optional*, defaults to `False`):
                Load the model weights from a TensorFlow checkpoint save file (see docstring of
                `pretrained_model_name_or_path` argument).
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force the (re-)download of the model weights and configuration files, overriding the
                cached versions if they exist.
            resume_download:
                Deprecated and ignored. All downloads are now resumed by default when possible.
                Will be removed in v5 of Transformers.
            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.
            output_loading_info(`bool`, *optional*, defaults to `False`):
                Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
            local_files_only(`bool`, *optional*, defaults to `False`):
                Whether or not to only look at local files (e.g., not try downloading the model).
            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.
            trust_remote_code (`bool`, *optional*, defaults to `False`):
                Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
                should only be set to `True` for repositories you trust and in which you have read the code, as it will
                execute code present on the Hub on your local machine.
            code_revision (`str`, *optional*, defaults to `"main"`):
                The specific revision to use for the code on the Hub, if the code leaves in a different repository than
                the rest of the model. 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.
            kwargs (additional keyword arguments, *optional*):
                Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
                `output_attentions=True`). Behaves differently depending on whether a `config` is provided or
                automatically loaded:

                    - If a configuration is provided with `config`, `**kwargs` will be directly passed to the
                      underlying model's `__init__` method (we assume all relevant updates to the configuration have
                      already been done)
                    - If a configuration is not provided, `kwargs` will be first passed to the configuration class
                      initialization function ([`~PretrainedConfig.from_pretrained`]). Each key of `kwargs` that
                      corresponds to a configuration attribute will be used to override said attribute with the
                      supplied `kwargs` value. Remaining keys that do not correspond to any configuration attribute
                      will be passed to the underlying model's `__init__` function.

        Examples:

        ```python
        >>> from transformers import AutoConfig, BaseAutoModelClass

        >>> # Download model and configuration from huggingface.co and cache.
        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder")

        >>> # Update configuration during loading
        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder", output_attentions=True)
        >>> model.config.output_attentions
        True

        >>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
        >>> config = AutoConfig.from_pretrained("./tf_model/shortcut_placeholder_tf_model_config.json")
        >>> model = BaseAutoModelClass.from_pretrained(
        ...     "./tf_model/shortcut_placeholder_tf_checkpoint.ckpt.index", from_tf=True, config=config
        ... )
        ```
a  
        Instantiate one of the model classes of the library from a pretrained model.

        The model class to instantiate is selected based on the `model_type` property of the config object (either
        passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's missing, by
        falling back to using pattern matching on `pretrained_model_name_or_path`:

        List options

        Args:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                Can be either:

                    - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
                    - A path to a *directory* containing model weights saved using
                      [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
                    - A path or url to a *PyTorch state_dict save file* (e.g, `./pt_model/pytorch_model.bin`). In this
                      case, `from_pt` should be set to `True` and a configuration object should be provided as `config`
                      argument. This loading path is slower than converting the PyTorch model in a TensorFlow model
                      using the provided conversion scripts and loading the TensorFlow model afterwards.
            model_args (additional positional arguments, *optional*):
                Will be passed along to the underlying model `__init__()` method.
            config ([`PretrainedConfig`], *optional*):
                Configuration for the model to use instead of an automatically loaded configuration. Configuration can
                be automatically loaded when:

                    - The model is a model provided by the library (loaded with the *model id* string of a pretrained
                      model).
                    - The model was saved using [`~PreTrainedModel.save_pretrained`] and is reloaded by supplying the
                      save directory.
                    - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a
                      configuration JSON file named *config.json* is found in the directory.
            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.
            from_pt (`bool`, *optional*, defaults to `False`):
                Load the model weights from a PyTorch checkpoint save file (see docstring of
                `pretrained_model_name_or_path` argument).
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force the (re-)download of the model weights and configuration files, overriding the
                cached versions if they exist.
            resume_download:
                Deprecated and ignored. All downloads are now resumed by default when possible.
                Will be removed in v5 of Transformers.
            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.
            output_loading_info(`bool`, *optional*, defaults to `False`):
                Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
            local_files_only(`bool`, *optional*, defaults to `False`):
                Whether or not to only look at local files (e.g., not try downloading the model).
            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.
            trust_remote_code (`bool`, *optional*, defaults to `False`):
                Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
                should only be set to `True` for repositories you trust and in which you have read the code, as it will
                execute code present on the Hub on your local machine.
            code_revision (`str`, *optional*, defaults to `"main"`):
                The specific revision to use for the code on the Hub, if the code leaves in a different repository than
                the rest of the model. 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.
            kwargs (additional keyword arguments, *optional*):
                Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
                `output_attentions=True`). Behaves differently depending on whether a `config` is provided or
                automatically loaded:

                    - If a configuration is provided with `config`, `**kwargs` will be directly passed to the
                      underlying model's `__init__` method (we assume all relevant updates to the configuration have
                      already been done)
                    - If a configuration is not provided, `kwargs` will be first passed to the configuration class
                      initialization function ([`~PretrainedConfig.from_pretrained`]). Each key of `kwargs` that
                      corresponds to a configuration attribute will be used to override said attribute with the
                      supplied `kwargs` value. Remaining keys that do not correspond to any configuration attribute
                      will be passed to the underlying model's `__init__` function.

        Examples:

        ```python
        >>> from transformers import AutoConfig, BaseAutoModelClass

        >>> # Download model and configuration from huggingface.co and cache.
        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder")

        >>> # Update configuration during loading
        >>> model = BaseAutoModelClass.from_pretrained("checkpoint_placeholder", output_attentions=True)
        >>> model.config.output_attentions
        True

        >>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
        >>> config = AutoConfig.from_pretrained("./pt_model/shortcut_placeholder_pt_model_config.json")
        >>> model = BaseAutoModelClass.from_pretrained(
        ...     "./pt_model/shortcut_placeholder_pytorch_model.bin", from_pt=True, config=config
        ... )
        ```
c                    |t        |          }t        |t        t        f      s|S |D ci c]  }|j                  | }}t        | dg       }|D ]/  }||v r||   c S d| |v r
|d|    c S d| |v s'|d|    c S  |d   S c c}w )NarchitecturesTFFlaxr   )type
isinstancelisttuple__name__getattr)configmodel_mappingsupported_modelsmodelname_to_modelr   archs          X/var/www/html/venv/lib/python3.12/site-packages/transformers/models/auto/auto_factory.py_get_model_classr'     s    $T&\2&u68HIuU^^U*IMIFOR8M 0=  &&$[M) 2dV--D6]m+ 4v//0 A Js   Bc                   H    e Zd ZdZd Zed        Zed        Zedd       Zy)_BaseAutoModelClassNc                     t        | j                  j                   d| j                  j                   d| j                  j                   d      )Nz+ is designed to be instantiated using the `z5.from_pretrained(pretrained_model_name_or_path)` or `z.from_config(config)` methods.)EnvironmentError	__class__r   )selfargskwargss      r&   __init__z_BaseAutoModelClass.__init__  sR    ~~&&' (..112 3''((FH
 	
    c                 p   |j                  dd       }t        |d      xr | j                  |j                  v }t	        |      | j
                  j                         v }t        ||j                  ||      }|r|r|j                  | j                     }d|v r|j                  d      \  }}n|j                  }t        ||fi |}| j                  |j                  |d       |j                  dd       }	t        |      } |j                  |fi |S t	        |      | j
                  j                         v r)t!        || j
                        } |j                  |fi |S t#        d|j                   d| j                   d	d
j%                  d | j
                  j                         D               d      )Ntrust_remote_codeauto_mapz--Texist_okcode_revision!Unrecognized configuration class  for this kind of AutoModel: .
Model type should be one of , c              3   4   K   | ]  }|j                     y wNr   .0cs     r&   	<genexpr>z2_BaseAutoModelClass.from_config.<locals>.<genexpr>       4cAQZZ4c   .)pophasattrr   r4   r   _model_mappingkeysr   _name_or_pathsplitname_or_pathr   registerr,   $add_generation_mixin_to_remote_model_from_configr'   
ValueErrorjoin)
clsr    r/   r3   has_remote_codehas_local_code	class_refrepo_idmodel_class_s
             r&   from_configz_BaseAutoModelClass.from_config  s   "JJ':DA!&*5Y#,,&//:Yf););)@)@)BB5v33^_
 05Iy %.__T%:" --7	7UfUKLL));LF

?D1A>{KK+;++F=f==&\S//4466*633E3EFK+;++F=f==/0@0@/AA^_b_k_k^l m++/994cI[I[I`I`Ib4c+c*ddeg
 	
r1   c                    |j                  dd       }|j                  dd       }d|d<   g d}|D ci c]  }||v s||j                  |       }}|j                  dd       }	|j                  dd       }
|j                  dd       }|j                  d	d       }|j                  d
d       }|)t        j                  dt               |t	        d      |}|||d	<   |
?t        |t              s"t        |t        fdddd|}t        ||
      }
nt        |dd       }
t               rQ|	i }|||d	<   t        |fd|
i|}|6t        |dd      5 }t        j                  |      }||d<   |d   }d d d        t        |t              st!        j"                  |      }|j%                  dd       dk(  r|j                  d      }|j%                  dd       |j                  d      }t'        j(                  |fd||	|
d||\  }}|j%                  dd       dk(  rd|d<   |j%                  dd       |d   |d<   t+        |d      xr | j,                  |j.                  v }t1        |      | j2                  j5                         v }t7        ||||      }||d<   |r|r|j.                  | j,                     }t9        ||fd|	i||}|j                  dd       }| j;                  |j<                  |d       t?        |      } |j(                  |g|d|i||S t1        |      | j2                  j5                         v r0tA        || j2                        } |j(                  |g|d|i||S t	        d|j<                   d| j,                   ddjC                  d | j2                  j5                         D               d      c c}w # 1 sw Y   ^xY w) Nr    r3   T
_from_auto)		cache_dirforce_downloadlocal_files_onlyproxiesresume_downloadrevision	subfolderuse_auth_tokentokenr7   _commit_hashadapter_kwargsrd   rc   zrThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.zV`token` and `use_auth_token` are both specified. Please set only the argument `token`.F) _raise_exceptions_for_gated_repo%_raise_exceptions_for_missing_entries'_raise_exceptions_for_connection_errorsrzutf-8)encoding_adapter_model_pathbase_model_name_or_pathtorch_dtypeautoquantization_config)return_unused_kwargsr3   r7   re   r4   r5   r8   r9   r:   r;   c              3   4   K   | ]  }|j                     y wr=   r>   r?   s     r&   rB   z6_BaseAutoModelClass.from_pretrained.<locals>.<genexpr>9  rC   rD   rE   )"rF   warningswarnFutureWarningrP   r   r   r	   r   r   r   r   r   openjsonloadcopydeepcopygetr   from_pretrainedrG   r   r4   r   rH   rI   r   r   rM   r,   rN   r'   rQ   )rR   pretrained_model_name_or_path
model_argsr/   r    r3   hub_kwargs_namesname
hub_kwargsr7   commit_hashrf   rd   rc   resolved_config_filemaybe_adapter_pathfadapter_configkwargs_origrX   rS   rT   rU   rW   s                           r&   r|   z#_BaseAutoModelClass.from_pretrained  s   Hd+"JJ':DA#|

 :J\TU[^dFJJt,,\
\

?D9jj6$4d;w-#(8$?%MM E   l  #E"'Jwf&67'21( 6;:?<A( !($ 22FT%fndC%!#$.3N7+!9-"<G"KY" "-,cGD ^%)YYq\N<YN#894BC\4]1	^ &"23--/K zz-.&8JJ}-zz/6BJJ45'77-%)"3+(  NFF }d3v=(.}%4d;G0;<Q0R,-!&*5Y#,,&//:Yf););)@)@)BB5<no

 $2 05I78HUYcgmK 5ALL));LF>{KK.;..-0:CIMW[a  &\S//4466*633E3EFK.;..-0:CIMW[a  /0@0@/AA^_b_k_k^l m++/994cI[I[I`I`Ib4c+c*ddeg
 	
Q ]^^ ^s   	O	O	 OOc                     t        |d      r=t        |j                        t        |      k7  rt        d|j                   d| d      | j                  j                  |||       y)a  
        Register a new model for this class.

        Args:
            config_class ([`PretrainedConfig`]):
                The configuration corresponding to the model to register.
            model_class ([`PreTrainedModel`]):
                The model to register.
        config_classzThe model class you are passing has a `config_class` attribute that is not consistent with the config class you passed (model has z and you passed z!. Fix one of those so they match!r5   N)rG   strr   rP   rH   rM   )rR   r   rW   r6   s       r&   rM   z_BaseAutoModelClass.register<  sv     ;/C8P8P4QUXYeUf4f66A6N6N5OO_`l_m n.. 
 	##L+#Qr1   F)	r   
__module____qualname__rH   r0   classmethodrY   r|   rM    r1   r&   r)   r)     sM    N
 
 
8 z
 z
x R Rr1   r)   c                   @     e Zd ZdZe fd       Ze fd       Z xZS )_BaseAutoBackboneClassNc                    t        | ddg       ddlm} |j                  d |             }|j	                  dd       t        d      |j	                  dd	      rt        d
      |j                  d|j                        }|j                  d|j                        }|j                  d|j                        }|j                  d|j                        }	 ||||||	      }t        
| ,  |fi |S )Nvisiontimmr   )TimmBackboneConfigr    out_featuresz0Cannot specify `out_features` for timm backbonesoutput_loading_infoFz@Cannot specify `output_loading_info=True` when loading from timmnum_channelsfeatures_onlyuse_pretrained_backboneout_indices)backboner   r   r   r   )r   models.timm_backboner   rF   r{   rP   r   r   r   r   superrY   )rR   r}   r~   r/   r   r    r   r   r   r   r,   s             r&   #_load_timm_backbone_from_pretrainedz:_BaseAutoBackboneClass._load_timm_backbone_from_pretrainedT  s    #&12>H&8&:;::nd+7OPP::+U3_``zz.&2E2EF

?F4H4HI"(**-FHfHf"gjj0B0BC#2%'$;#
 w"64V44r1   c                 ~    |j                  dd      }|r | j                  |g|i |S t        |   |g|i |S )Nuse_timm_backboneF)rF   r   r   r|   )rR   r}   r~   r/   r   r,   s        r&   r|   z&_BaseAutoBackboneClass.from_pretrainedn  sT    "JJ':EB:3::;Xp[epioppw&'D\z\U[\\r1   )r   r   r   rH   r   r   r|   __classcell__)r,   s   @r&   r   r   P  s0    N5 52 ] ]r1   r   c                 n    t        |      dkD  r| j                  dd| d      S | j                  dd      S )Nr   z(one of the model classes of the library z0one of the model classes of the library (with a z head) z-one of the base model classes of the library )lenreplace)	docstringhead_docs     r&   insert_head_docr   w  sK    
8}q  6>xjP
 	
 24c r1   c                 ^   | j                   }| j                  }t        t        |      }|j	                  d|      | _        t        t        j                        }t        t        |      }|j	                  d|      }|j	                  d|      }||_         t        |j                   d      |      }t        |      | _        |j                  d      rt        }n|j                  d      rt        }nt        }t        t        j                         }	t        ||      }|j	                  d|      }|j	                  d|      }|j#                  d      d	   j#                  d
      d   }
|j	                  d|
      }||	_         t        |j                         |	      }	t        |	      | _        | S )N)r   BaseAutoModelClasscheckpoint_placeholderF)use_model_typesr   r   /-r   shortcut_placeholder)rH   r   r   CLASS_DOCSTRINGr   __doc__r
   r)   rY   FROM_CONFIG_DOCSTRINGr   r   
startswithFROM_PRETRAINED_TF_DOCSTRINGFROM_PRETRAINED_FLAX_DOCSTRINGFROM_PRETRAINED_TORCH_DOCSTRINGr|   rK   )rR   checkpoint_for_exampler   r!   r   class_docstringrY   from_config_docstringfrom_pretrained_docstringr|   shortcuts              r&   auto_class_updater     s   &&M<<D%oIO!))*>ECK /;;<K+,AHU199:NPTU199:RTjk/Kh3M4P4PbghituK!+.COt$@!		 $B!$C! 3 C CDO /0IT\ ] 9 A ABVX\ ] 9 A ABZ\r s%++C04::3?BH 9 A ABXZb c7OU78T8TUVefO%o6CJr1   c                     g }| j                         D ]8  }t        |t        t        f      r|t        |      z  }(|j	                  |       : |S r=   )valuesr   r   r   append)r!   resultr#   s      r&   
get_valuesr     sN    F%%' !edE]+d5k!FMM% 	! Mr1   c           
      8    |y t        |t              rt         fd|D              S t         |      rt         |      S t	        j
                  d      } |k7  r	 t        ||      S t        d| d| d      # t        $ r t        d| d  d| d      w xY w)Nc              3   6   K   | ]  }t        |        y wr=   )getattribute_from_module)r@   amodules     r&   rB   z+getattribute_from_module.<locals>.<genexpr>  s     GQ-fa8Gs   transformerszCould not find z neither in z nor in !z in )r   r   rG   r   	importlibimport_moduler   rP   )r   attrtransformers_modules   `  r&   r   r     s    |$G$GGGvtvt$$ $11.A$$	i+,?FF ?4&5H4IKLL  	itfLQdPeefghh	is   A: :Bc                 $   dt        | j                        vr| S dt        | j                        v r| S dt        t        | d            v}dt        t        | d            v}|s|r+t	        | j
                  | t        fi | j                        }|S | S )a  
    Adds `GenerationMixin` to the inheritance of `model_class`, if `model_class` is a PyTorch model.

    This function is used for backwards compatibility purposes: in v4.45, we've started a deprecation cycle to make
    `PreTrainedModel` stop inheriting from `GenerationMixin`. Without this function, older models dynamically loaded
    from the Hub may not have the `generate` method after we remove the inheritance.
    ztorch.nn.modules.module.Moduler   generateprepare_inputs_for_generation)r   __mro__	__bases__r   r   r   r   __dict__)rW   has_custom_generatehas_custom_prepare_inputs!model_class_with_generation_mixins       r&   rN   rN     s     (s;3F3F/GG C 5 566 ,3w{J7W3XX 1W[Rq=r9s s7,0  ;"@BZ[EYEYBZ-
) 10r1   c                   Z    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZy)_LazyAutoMappinga  
    " A mapping config to object (model or tokenizer for instance) that will load keys and values when it is accessed.

    Args:
        - config_mapping: The map model type to config class
        - model_mapping: The map model type to model (or tokenizer) class
    c                     || _         |j                         D ci c]  \  }}||
 c}}| _        || _        | | j                  _        i | _        i | _        y c c}}w r=   )_config_mappingitems_reverse_config_mappingrH   _extra_content_modules)r-   config_mappingr!   kvs        r&   r0   z_LazyAutoMapping.__init__  sY    -9G9M9M9O'PA1'P$+-1* 	 (Qs   Ac                     t        | j                  j                               j                  | j                  j                               }t        |      t        | j                        z   S r=   )setr   rI   intersectionrH   r   r   )r-   common_keyss     r&   __len__z_LazyAutoMapping.__len__  sP    $..3356CCDDWDWD\D\D^_;#d&9&9":::r1   c                    || j                   v r| j                   |   S | j                  |j                     }|| j                  v r!| j                  |   }| j	                  ||      S | j
                  j                         D cg c]  \  }}||j                  k(  s| }}}|D ]3  }|| j                  v s| j                  |   }| j	                  ||      c S  t        |      c c}}w r=   )r   r   r   rH   _load_attr_from_moduler   r   KeyError)r-   key
model_type
model_namer   r   model_typesmtypes           r&   __getitem__z_LazyAutoMapping.__getitem__  s    $%%%&&s++11#,,?
,,,,,Z8J..z:FF &*%9%9%?%?%AWTQQ#,,EVqWW  	FE+++!007
225*EE	F sm Xs   C%C%c                     t        |      }|| j                  vr&t        j                  d| d      | j                  |<   t	        | j                  |   |      S )NrE   ztransformers.models)r   r   r   r   r   )r-   r   r   module_names       r&   r   z'_LazyAutoMapping._load_attr_from_module  sQ    /
;dmm+)2)@)@1[MARTi)jDMM+&'k(BDIIr1   c                 
   | j                   j                         D cg c]3  \  }}|| j                  j                         v r| j	                  ||      5 }}}|t        | j                  j                               z   S c c}}w r=   )r   r   rH   rI   r   r   r   )r-   r   r   mapping_keyss       r&   rI   z_LazyAutoMapping.keys  s~     "11779
Td))..00 ''T2
 

 d4#6#6#;#;#=>>>
   8A?c                 H    	 | j                  |      S # t        $ r |cY S w xY wr=   )r   r   )r-   r   defaults      r&   r{   z_LazyAutoMapping.get  s,    	##C(( 	N	s    !!c                 4    t        | j                               S r=   )boolrI   r-   s    r&   __bool__z_LazyAutoMapping.__bool__      DIIK  r1   c                 
   | j                   j                         D cg c]3  \  }}|| j                  j                         v r| j	                  ||      5 }}}|t        | j                  j                               z   S c c}}w r=   )rH   r   r   rI   r   r   r   r   )r-   r   r   mapping_valuess       r&   r   z_LazyAutoMapping.values  s~     "00668
Td**//11 ''T2
 

 T%8%8%?%?%A BBB
r   c           	      V   | j                   j                         D cg c]\  }|| j                  j                         v r>| j                  || j                  |         | j                  || j                   |         f^ }}|t	        | j
                  j                               z   S c c}w r=   )rH   rI   r   r   r   r   r   )r-   r   mapping_itemss      r&   r   z_LazyAutoMapping.items$  s     **//1

 d**//11	 ++C1E1Ec1JK++C1D1DS1IJ
 
 tD$7$7$=$=$?@@@
s   A!B&c                 4    t        | j                               S r=   )iterrI   r   s    r&   __iter__z_LazyAutoMapping.__iter__/  r   r1   c                     || j                   v ryt        |d      r|j                  | j                  vry| j                  |j                     }|| j                  v S )NTr   F)r   rG   r   r   rH   )r-   itemr   s      r&   __contains__z_LazyAutoMapping.__contains__2  sV    4&&&tZ(DMMA]A],]11$--@
T0000r1   c                     t        |d      r^|j                  | j                  v rF| j                  |j                     }|| j                  j	                         v r|st        d| d      || j                  |<   y)z7
        Register a new model in this mapping.
        r   'z*' is already used by a Transformers model.N)rG   r   r   rH   rI   rP   r   )r-   r   valuer6   r   s        r&   rM   z_LazyAutoMapping.register:  sq     3
#8T8T(T55cllCJT005577 1SE)S!TUU#(C r1   Nr   )r   r   r   r   r0   r   r   r   rI   r{   r   r   r   r  r  rM   r   r1   r&   r   r     sF    ; J?!C	A!1	)r1   r   ) )zgoogle-bert/bert-base-casedr	  )-r   ry   r   rw   rs   collectionsr   configuration_utilsr   dynamic_module_utilsr   r   utilsr   r	   r
   r   r   r   r   r   r   configuration_autor   r   r   
generationr   
get_loggerr   loggerr   r   r   r   r   r'   r)   r   r   r   r   r   rN   r   r   r1   r&   <module>r     s    4     # 3 \
 
 
 i h - 
		H	% 4j# Xa  Fa" H(vR vRr$]0 $]N FM(8c){ c)r1   