o
    *Îj§  ã                   @   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)Zmap_locationz$private.bert.embeddings.position_ids)ÚsuperÚ__init__r   r   r   ÚtorchÚcudaZis_availableÚ
init_modelÚmodelÚosÚpathÚjoinr   ZTORCH_MODEL_FILEÚloadr   Zload_state_dict)Úselfr   r   r   ÚkwargsZ
model_ckptZ
stats_dict©Ú	__class__© úw/var/www/html/Deteccion_Ine/venv/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!   Z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!   r+   r-   r%   r%   r&   Úforward@   s   

z"UserSatisfactionEstimation.forward)NN)Ú__name__Ú
__module__Ú__qualname__Ústrr   r   r   r   r
   r   r.   Ú__classcell__r%   r%   r#   r&   r      s$    ýÿþý	ÿÿþc                 C   s2   |   ¡ D ]\}}|j ¡ dkrt|jƒ q	 qd S )Nr   )Znamed_parametersÚdataÚdimr   )r   ÚnameÚparamr%   r%   r&   Úinit_paramsR   s
   ür8   Tc                 C   sV   t  |  ddd¡| ¡  d¡¡ d¡}|jdd dd¡ ¡ }|r%| ¡ }|| }|S )Nr   é   r   éÿÿÿÿ©r5   )	r   ÚbmmÚpermuteÚfloatÚ	unsqueezeÚsqueezeÚsumÚviewÚsqrt)Z	sentencesÚmaskrC   Zsentence_sumsZdivisorr%   r%   r&   Úuniversal_sentence_embeddingZ   s   þþrE   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   Z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   r:   r;   )ÚneÚdetachrI   rE   r   Úcat)r!   r+   Úattention_maskZoutputsÚhÚclsÚoutr%   r%   r&   r.   n   s   zBERTBackbone.forward©r/   r0   r1   r   r.   r3   r%   r%   r#   r&   rG   e   s    rG   c                       rF   )ÚMLPc                    s*   t ƒ  ¡  t ||¡| _t ||¡| _d S ©N)r   r   ÚnnÚLinearÚfc1Úfc2)r!   Z
input_sizeZoutput_sizeZhidden_sizer#   r%   r&   r   y   s   
zMLP.__init__c                 C   s$   t  |  |¡¡}t  |  |¡¡}|S rU   )ÚFZrelurX   rY   )r!   ZdinZdoutr%   r%   r&   r.   ~   s   zMLP.forwardrS   r%   r%   r#   r&   rT   w   s    rT   c                       rF   )r*   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(   r9   é   r'   r   FT)Z
num_layersÚbidirectionalZbatch_firsté   )Zbias)r   r   rV   ZDropoutÚdrop_outrG   ÚprivaterJ   r   ÚencoderZGRUÚcontent_grurW   Úsat_classifierÚU_cÚw_cr8   )r!   ÚargsrJ   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   r:   )r+   )rD   Úvaluer   r;   r9   )ra   Zflatten_parametersÚsizer@   rL   rM   rB   r_   r`   r^   rd   r   Útanhrc   rZ   ZsoftmaxZmasked_fillÚnpÚinfr<   r=   r?   rb   )r!   r+   Z
batch_sizeZ
dialog_lenZutt_lenrO   Zprivate_outÚHÚ_Zatt_cZhiddenZsat_resr%   r%   r&   r.      s    
&
ÿ"
zUSE.forwardrS   r%   r%   r#   r&   r*   „   s    r*   )T),r   Útypingr   r   Únumpyri   r   Ztorch.nnrV   Ztorch.nn.functionalZ
functionalrZ   Ztorch.nn.initr   Ztransformersr   Zmodelscope.metainfor   Zmodelscope.modelsr   Zmodelscope.models.baser   Zmodelscope.models.builderr	   Zmodelscope.outputsr
   Z-modelscope.utils.compatible_with_transformersr   Zmodelscope.utils.constantr   r   Ztransformerr   Ú__all__Zregister_moduleZtext_classificationZuser   r8   rE   ÚModulerG   rT   r*   r%   r%   r%   r&   Ú<module>   s0   7
