o
    ߥi!                     @   s   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ e Zdd ZejejejdG dd de	eZdS )zPyTorch BERT model.     )
AutoConfig	AutoModel)PreTrainedModel)Models)
TorchModel)MODELS)AttentionBackboneModelOutput)Tasks)
get_logger)parse_labels_in_orderc                 C   s   |t |  }t|ttfs|S dd |D }t| dg }|D ],}||v r*||   S d| |v r:|d|    S d| |v rJ|d|    S q|d S )Nc                 S   s   i | ]}|j |qS  __name__).0modelr   r   b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/hf_transformers/backbone.py
<dictcomp>%   s    z$_get_model_class.<locals>.<dictcomp>architecturesTFFlaxr   )type
isinstancelisttuplegetattr)configmodel_mappingsupported_modelsname_to_modelr   archr   r   r   _get_model_class    s   r    )	group_keymodule_namec                   @   s   e Zd ZdZedddZdS )TransformersModela  The Bert Model transformer outputting raw hidden-states without any
    specific head on top.

    This model inherits from [`PreTrainedModel`]. Check the superclass
    documentation for the generic methods the library implements for all its
    model (such as downloading or saving, resizing the input embeddings, pruning
    heads etc.)

    This model is also a PyTorch
    [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)
    subclass. Use it as a regular PyTorch Module and refer to the PyTorch
    documentation for all matter related to general usage and behavior.

    Parameters:
        config ([`BertConfig`]): Model configuration class with all the
        parameters of the model.
            Initializing with a config file does not load the weights associated
            with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model
            weights.

    The model can behave as an encoder (with only self-attention) as well as a
    decoder, in which case a layer of cross-attention is added between the
    self-attention layers, following the architecture described in [Attention is
    all you need](https://arxiv.org/abs/1706.03762) by Ashish Vaswani, Noam
    Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz
    Kaiser and Illia Polosukhin.

    To behave as an decoder the model needs to be initialized with the
    `is_decoder` argument of the configuration set to `True`. To be used in a
    Seq2Seq model, the model needs to initialized with both `is_decoder`
    argument and `add_cross_attention` set to `True`; an `encoder_hidden_states`
    is then expected as an input to the forward pass.


    Nc              	   K   s   | dd}|rt|}|S tj|fddd|\}}tj}t|| v r6t||}||}||_|S t	d|j
 dddd	 | D  d
)Ninit_backboneFT)return_unused_kwargstrust_remote_codez!Unrecognized configuration class z. for the AutoModelModel type should be one of z, c                 s   s    | ]}|j V  qd S Nr   )r   cr   r   r   	<genexpr>u   s    z1TransformersModel._instantiate.<locals>.<genexpr>.)popr   from_pretrainedr   _model_mappingr   keysr    	model_dir
ValueError	__class__join)clsr/   r   r$   r   kwargsr   model_classr   r   r   _instantiate\   s.   



zTransformersModel._instantiater'   )r   
__module____qualname____doc__classmethodr6   r   r   r   r   r#   4   s    %r#   N)r9   transformersr   r   transformers.modeling_utilsr   modelscope.metainfor   modelscope.modelsr   modelscope.models.builderr   modelscope.outputsr   modelscope.utils.constantr	   modelscope.utils.loggerr
   modelscope.utils.nlp.utilsr   loggerr    register_modulebackboner#   r   r   r   r   <module>   s    