o
    ߥi!                  	   @   s   d dl Z d dlmZ d dlZ d dlmZ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mZ e Ze
jejejd
e
jejejd
e
jejejd
e
jejejd
e
jejejd
G dd deZdS )    N)BCEWithLogitsLossCrossEntropyLossMSELoss)Models)MODELS)&AttentionTextClassificationModelOutput)logger)Tasks   )MGeoMGeoPreTrainedModel)module_namec                       sF   e Zd Zddef fddZ												d	ddZ  ZS )
MGeoForSequenceClassificationsingle-modalfinetune_modec                    sr   t  | |j| _|| _t| | jt||dd |jd ur!|jn|j}t	
|| _t	|j|j| _|   d S )NT)add_pooling_layer)super__init__
num_labelsconfigsetattrbase_model_prefixr   classifier_dropouthidden_dropout_probnnDropoutdropoutLinearhidden_size
classifier	post_init)selfr   r   kwargsr   	__class__ j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/multi_modal/mgeo/text_classification.pyr   *   s   z&MGeoForSequenceClassification.__init__Nc                 C   sz  |
dur|
n| j j}
| jj||||||||	|
||dd}|d }| |}| |}d}|dur| j jdu rZ| jdkr@d| j _n| jdkrV|jt	j
ksQ|jt	jkrVd| j _nd| j _| j jdkrxt }| jdkrr|| | }n+|||}n%| j jdkrt }||d| j|d}n| j jdkrt }|||}|
s|f|d	d  }|dur|f| S |S t|||j|jd
S )a<  
        Args:
        input_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using :class:`~modelscope.models.nlp.structbert.SbertTokenizer`. See
            :meth:`transformers.PreTrainedTokenizer.encode` and :meth:`transformers.PreTrainedTokenizer.__call__` for
            details.

        attention_mask (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
            Mask to avoid performing attention on padding token indices. Mask values selected in ``[0, 1]``:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

        token_type_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in ``[0,
            1]``:

            - 0 corresponds to a `sentence A` token,
            - 1 corresponds to a `sentence B` token.

        position_ids (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0,
            config.max_position_embeddings - 1]``.

        head_mask (:obj:`torch.FloatTensor` of shape :obj:`(num_heads,)` or :obj:`(num_layers, num_heads)`, `optional`):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in ``[0, 1]``:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`):
            Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation.
            This is useful if you want more control over how to convert :obj:`input_ids` indices into associated
            vectors than the model's internal embedding lookup matrix.
        output_attentions (:obj:`bool`, `optional`):
            Whether or not to return the attentions tensors of all attention layers. See ``attentions`` under returned
            tensors for more detail.
        output_hidden_states (:obj:`bool`, `optional`):
            Whether or not to return the hidden states of all layers. See ``hidden_states`` under returned tensors for
            more detail.
        return_dict (:obj:`bool`, `optional`):
            Whether or not to return a :class:`~transformers.ModelOutput` instead of a plain tuple.
        labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
            Labels for computing the sequence classification/regression loss. Indices should be in :obj:`[0, ...,
            config.num_labels - 1]`. If :obj:`config.num_labels == 1` a regression loss is computed (Mean-Square loss),
            If :obj:`config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Returns:
            Returns `modelscope.outputs.AttentionTextClassificationModelOutput`

        Examples:
            >>> from modelscope.models import Model
            >>> from modelscope.preprocessors import Preprocessor
            >>> model = Model.from_pretrained('damo/nlp_structbert_sentence-similarity_chinese-base')
            >>> preprocessor = Preprocessor.from_pretrained('damo/nlp_structbert_sentence-similarity_chinese-base')
            >>> print(model(**preprocessor(('This is a test', 'This is also a test'))))
        NT)attention_masktoken_type_idsposition_ids	head_maskinputs_embedsoutput_attentionsoutput_hidden_statesreturn_dictgis_listgis_tpuse_token_typer
   
regressionsingle_label_classificationmulti_label_classification   )losslogitshidden_states
attentions)r   use_return_dict
base_modelforwardr   r   problem_typer   dtypetorchlongintr   squeezer   viewr   r   r9   r:   )r!   	input_idsr'   r(   r)   r*   r+   labelsr,   r-   r.   r/   r0   outputspooled_outputr8   r7   loss_fctoutputr%   r%   r&   r=   :   sb   J






z%MGeoForSequenceClassification.forward)r   )NNNNNNNNNNNN)__name__
__module____qualname__strr   r=   __classcell__r%   r%   r#   r&   r   !   s    	r   )r@   torch.nnr   torch.utils.checkpointr   r   r   modelscope.metainfor   modelscope.models.builderr   modelscope.outputsr   modelscope.utilsr   loggingmodelscope.utils.constantr	   backboner   r   
get_loggerregister_moduletext_classificationmgeonlisentiment_classificationsentence_similarityzero_shot_classificationr   r%   r%   r%   r&   <module>   s*   