o
    ߥi                     @   s6  d dl Z d dlmZmZ d dlZd dlZd dlmZ d dl	m  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 d dlmZmZ ddlm Z  dgZ!ej"ej#ej$dG dd deZ%dd Z&dddZ'G dd dej(Z)G dd dej(Z*G dd dej(Z+dS )    N)DictUnion)xavier_uniform_)	BertModel)Models)
TorchModel)Tensor)MODELS)-DialogueUserSatisfactionEstimationModelOutput)compatible_position_ids)	ModelFileTasks   )TransformerEncoderUserSatisfactionEstimation)module_namec                       sZ   e Zd Z		ddededef fddZdd Zd	ed
eee	eef f fddZ
  ZS )r   N	model_dir	bert_namedevicec                    s   t  j|fi | || _|dur|nd| _d| _|dur%tj r%|| _|  | _	t
j|tj}tj|tdd}t|d | j	| dS )a|  initialize the user satisfaction estimation model from the `model_dir` path. The default preprocessor
        for this task is DialogueClassificationUsePreprocessor.

        Args:
            model_dir: The model dir containing the model.
            bert_name: The pretrained model, default bert-base-chinese
            device: The device of running model, default cpu
        Nbert-base-chinesecpu)map_locationz$private.bert.embeddings.position_ids)super__init__r   r   r   torchcudais_available
init_modelmodelospathjoinr   TORCH_MODEL_FILEloadr   load_state_dict)selfr   r   r   kwargs
model_ckpt
stats_dict	__class__ j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/use/user_satisfaction_estimation.pyr      s   
z#UserSatisfactionEstimation.__init__c                 C   s   | j | jdd}t|}|S )N皙?)r   	cache_dirdropout)r   r   USE)r%   configsr   r+   r+   r,   r   7   s   z%UserSatisfactionEstimation.init_model	input_idsreturnc                 C   s   |  |}t|dS )ap  Compute the logits of satisfaction polarities for a dialogue.

        Args:
           input_ids (Tensor): the preprocessed dialogue input
        Returns:
           output (Dict[str, Any] or DialogueUserSatisfactionEstimationModelOutput): The results of user satisfaction.

        Example:
            >>> {'logits': tensor([[-2.1795,  1.1323,  1.8605]])}
        )logits)r   r
   )r%   r2   r4   r+   r+   r,   forward@   s   

z"UserSatisfactionEstimation.forward)NN)__name__
__module____qualname__strr   r   r   r   r
   r   r5   __classcell__r+   r+   r)   r,   r      s$    	c                 C   s2   |   D ]\}}|j dkrt|j q	 qd S )Nr   )named_parametersdatadimr   )r   nameparamr+   r+   r,   init_paramsR   s
   r@   Tc                 C   sV   t | ddd| dd}|jdddd }|r%| }|| }|S )Nr      r   r=   )	r   bmmpermutefloat	unsqueezesqueezesumviewsqrt)	sentencesmaskrK   sentence_sumsdivisorr+   r+   r,   universal_sentence_embeddingZ   s   rP   c                       $   e Zd Z fddZdd Z  ZS )BERTBackbonec                    s:   t    |dd}|d}tj||d| _d| _d S )Nr   r   r.   )r.   i   )r   r   getr   from_pretrainedbertd_model)r%   configr   r.   r)   r+   r,   r   g   s
   


zBERTBackbone.__init__c                 C   sF   | d }| ||}t|d |}|d }tj||gdd}|S )Nr   r   rB   rC   )nedetachrU   rP   r   cat)r%   r2   attention_maskoutputshclsoutr+   r+   r,   r5   n   s   zBERTBackbone.forwardr6   r7   r8   r   r5   r:   r+   r+   r)   r,   rR   e   s    rR   c                       rQ   )MLPc                    s*   t    t||| _t||| _d S N)r   r   nnLinearfc1fc2)r%   
input_sizeoutput_sizehidden_sizer)   r+   r,   r   y   s   
zMLP.__init__c                 C   s$   t | |}t | |}|S rb   )Frelure   rf   )r%   dindoutr+   r+   r,   r5   ~   s   zMLP.forwardr`   r+   r+   r)   r,   ra   w   s    ra   c                       rQ   )r0   c                    s   t    t|d | _t|d |d d| _| jj}t||d ddd| _	tj
||dd	d
d| _t|d| _t||| _tj|dd	d| _t| j	 t| j t| j t| j d S )Nr/   r   r.   )r   r.   rA      r-   r   FT)
num_layersbidirectionalbatch_first   )bias)r   r   rc   Dropoutdrop_outrR   privaterV   r   encoderGRUcontent_grurd   sat_classifierU_cw_cr@   )r%   argsrV   r)   r+   r,   r      s*   



zUSE.__init__c                 C   s   | j   | \}}}|d d d d df dd }|d|}| j|d}|||d}| ||}| 	|}|  |\}}| 
t| |d}	tj|	j| tj ddd}	t|ddd|	dd}
| |
}|S )Nr   rB   )r2   )rM   valuer   rC   rA   )ry   flatten_parameterssizerH   rX   rY   rJ   rv   rw   ru   r|   r   tanhr{   rj   softmaxmasked_fillnpinfrD   rE   rG   rz   )r%   r2   
batch_size
dialog_lenutt_lenr[   private_outH_att_chiddensat_resr+   r+   r,   r5      s    
&
"
zUSE.forwardr`   r+   r+   r)   r,   r0      s    r0   )T),r   typingr   r   numpyr   r   torch.nnrc   torch.nn.functional
functionalrj   torch.nn.initr   transformersr   modelscope.metainfor   modelscope.modelsr   modelscope.models.baser   modelscope.models.builderr	   modelscope.outputsr
   -modelscope.utils.compatible_with_transformersr   modelscope.utils.constantr   r   transformerr   __all__register_moduletext_classificationuser   r@   rP   ModulerR   ra   r0   r+   r+   r+   r,   <module>   s0   7
