o
    ߥi                      @   sL  d dl mZ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mZ d dlmZ d dlmZ d	d
gZ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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e	jejejde	jejejdG dd
 d
eZdS )    )AnyDictN)HeadsModels
TaskModels)MODELS)EncoderModel)'AttentionTokenClassificationModelOutput
OutputKeys)Tasks)parse_label_mappingModelForTokenClassification"ModelForTokenClassificationWithCRF)module_namec                       sh   e Zd ZejZejZdZdZ	de
f fddZ fddZ												d fd	d
	Z  ZS )r   encoderT	model_dirc                    sv   i | _ |d}|du r(t|}|durt|dkrt|}dd | D | _ ||d< t j|g|R i | dS )zinitialize the sequence classification model from the `model_dir` path.

        Args:
            model_dir (str): the model path.
        
num_labelsNr   c                 S   s   i | ]\}}||qS  r   ).0labelidr   r   j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/task_models/token_classification.py
<dictcomp>1   s    z8ModelForTokenClassification.__init__.<locals>.<dictcomp>)id2labelgetr   lenitemssuper__init__)selfr   argskwargsr   label2id	__class__r   r   r   "   s   
z$ModelForTokenClassification.__init__c                    sL   t   }t|dr|d d ur|jn|j|d< n|j|d< | jj|d< |S )Nclassifier_dropoutr   )r   parse_head_cfghasattrr%   hidden_dropout_probconfigr   )r   head_cfgr#   r   r   r&   5   s   



z*ModelForTokenClassification.parse_head_cfgNc              
      sD   ||d< ||d< t  j|||||||||	f	i |}||_||_|S )Noffset_mapping
label_mask)r   forwardr+   r,   )r   	input_idsattention_masktoken_type_idsposition_ids	head_maskinputs_embedslabelsoutput_attentionsoutput_hidden_statesreturn_dictr+   r,   r!   outputsr#   r   r   r-   @   s   z#ModelForTokenClassification.forward)NNNNNNNNNNNN)__name__
__module____qualname__r   token_classificationtaskr   	head_typebase_model_prefixoverride_base_model_prefixstrr   r&   r-   __classcell__r   r   r#   r   r      s(    c                   @   s   e Zd ZejZdZdd ZdS )r   r   c              	   K   s   |d }| j |d |d }|d }|d }t|}tt|D ]%}|| d  	 }	t
|| dd|	}
||  }|
|| |< q"|}td |d d |||dS )Nlogitsr,   r+   r   )lossrC   hidden_states
attentionsr,   r+   predictions)headdecodetorch
zeros_likeranger   sumlongcpuitemnarrowboolr	   )r   inputsr!   rC   predictsr+   maskmasked_predictimasked_lengthsselected_predictsmask_positionr   r   r   postprocessh   s.   

z.ModelForTokenClassificationWithCRF.postprocessN)r9   r:   r;   r   transformer_crfr>   r?   r\   r   r   r   r   r   [   s    
)typingr   r   rK   modelscope.metainfor   r   r   modelscope.models.builderr   ,modelscope.models.nlp.task_models.task_modelr   modelscope.outputsr	   r
   modelscope.utils.constantr   modelscope.utils.hubr   __all__register_moduler<   part_of_speechnamed_entity_recognitiontoken_classification_for_nerr   r]   tcrf	tcrf_wsegword_segmentationr   r   r   r   r   <module>   s@   C"