o
    ߥiE                  	   @   s   d dl 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mZ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)CrossEntropyLossMSELoss)Models)MODELS)&AttentionTextClassificationModelOutput)logger)Tasks   )PeerClassificationHead	PeerModelPeerPreTrainedModelPeerTopModel)module_namec                       s>   e Zd Z fddZddddddddde dfddZ  ZS )PeerForSequenceClassificationc                    s   t  | |j| _|| _t|}t|}|jdks J d|j|_|j|j |_t|| _	t
|| _t|| _|   d S )Nr   z9config.num_hidden_layers_shared should be greater than 0!)super__init__
num_labelsconfigcopydeepcopynum_hidden_layers_sharednum_hidden_layersr   teams1_shared_bottomr   teams1_discr_topr
   
classifierinit_weights)selfr   kwargsconfig_discr_topconfig_shared_bottom	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/peer/text_classification.pyr   *   s   




z&PeerForSequenceClassification.__init__Nc                 C   s   |dur|n| j j}| ||||||||	|
|
}| |d ||||||||	|
|}|}|d }| |}d}|dur^| jdkrNt }||d|d}nt }||d| j|d}|st|f|dd  }|durr|f| S |S t	|||j
|jdS )a  
        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).
        Nr   r	   )losslogitshidden_states
attentions)r   use_return_dictr   r   r   r   r   viewr   r   r'   r(   )r   	input_idsattention_masktoken_type_idsposition_ids	head_maskinputs_embedslabelsoutput_attentionsoutput_hidden_statesside_info_setsreturn_dicthidden_states_discr_bottomhidden_states_discr_topdiscriminator_hidden_statessequence_outputr&   r%   loss_fctoutputr"   r"   r#   forward?   sB   



z%PeerForSequenceClassification.forward)__name__
__module____qualname__r   dictr<   __classcell__r"   r"   r    r#   r   !   s    	r   )r   torch.nnr   r   modelscope.metainfor   modelscope.models.builderr   modelscope.outputsr   modelscope.utilsr   loggingmodelscope.utils.constantr   backboner
   r   r   r   
get_loggerregister_moduletext_classificationpeernlisentiment_classificationsentence_similarityzero_shot_classificationr   r"   r"   r"   r#   <module>   s&   