o
    }oi                     @   s   d dl m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mZmZ dd	gZejjejjd
ZeG dd dZG dd	 d	e
ZG dd de
ZdS )    )	dataclass)DictOptional)nn)MultiLayerPerceptron)
Classifier)	typecheck)
LogitsTypeLogprobsType
NeuralTypeBertPretrainingTokenClassifierTokenClassifier)gelureluc                   @   sJ   e Zd ZU dZeed< dZeed< dZe	ed< dZ
eed< dZe	ed	< d
S )TokenClassifierConfig   
num_layersr   
activationTlog_softmax        dropoutuse_transformer_initN)__name__
__module____qualname__r   int__annotations__r   strr   boolr   floatr    r    r    h/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/nlp/modules/common/token_classifier.pyr      s   
 r   c                       z   e Zd ZdZedeeeef  fddZ						dd	e
d
e
de
dededededdf fddZe dd Z  ZS )r   z`
    A module to perform token level classification tasks such as Named entity recognition.
    returnc                 C   &   | j sdtdt iS dtdt iS z=
        Returns definitions of module output ports.
        logits)BTC	log_probsr   r   r	   r
   selfr    r    r!   output_types,      zTokenClassifier.output_typesr   r   Tr   hidden_sizenum_classesr   r   r   r   r   Nc                    s:   t  j||d || _t|||||d| _| j|d dS )q  
        Initializes the Token Classifier module.

        Args:
            hidden_size: the size of the hidden dimension
            num_classes: number of classes
            num_layers: number of fully connected layers in the multilayer perceptron (MLP)
            activation: activation to usee between fully connected layers in the MLP
            log_softmax: whether to apply softmax to the output of the MLP
            dropout: dropout to apply to the input hidden states
            use_transformer_init: whether to initialize the weights of the classifier head with the same approach used in Transformer
        r0   r   r   r   r   r   N)super__init__r   r   mlp	post_initr-   r0   r1   r   r   r   r   r   	__class__r    r!   r7   6   s   
zTokenClassifier.__init__c                 C   s   |  |}| |}|S a3  
        Performs the forward step of the module.
        Args:
            hidden_states: batch of hidden states (for example, from the BERT encoder module)
                [BATCH_SIZE x SEQ_LENGTH x HIDDEN_SIZE]
        Returns: logits value for each class [BATCH_SIZE x SEQ_LENGTH x NUM_CLASSES]
        )r   r8   )r-   hidden_statesr&   r    r    r!   forwardT   s   
	
zTokenClassifier.forwardr   r   Tr   Tr   r   r   __doc__propertyr   r   r   r   r.   r   r   r   r7   r   r?   __classcell__r    r    r;   r!   r   '   s8    	c                       r"   )r   zT
    A module to perform token level classification tasks for Bert pretraining.
    r#   c                 C   r$   r%   r+   r,   r    r    r!   r.   g   r/   z+BertPretrainingTokenClassifier.output_typesr   r   Tr   r0   r1   r   r   r   r   r   Nc                    sz   t  j||d || _|tvrtd| dt||| _t| | _tj	|dd| _
t|||||d| _| j|d dS )	r2   r3   zactivation "z" not foundg-q=)epsr4   r5   N)r6   r7   r   ACT2FN
ValueErrorr   Lineardenseact	LayerNormnormr   r8   r9   r:   r;   r    r!   r7   q   s   

z'BertPretrainingTokenClassifier.__init__c                 C   s6   |  |}| |}| |}| |}| |}|S r=   )r   rI   rJ   rL   r8   )r-   r>   	transformr&   r    r    r!   r?      s   
	



z&BertPretrainingTokenClassifier.forwardr@   rA   r    r    r;   r!   r   b   s8    	%N)dataclassesr   typingr   r   torchr   nemo.collections.common.partsr   .nemo.collections.nlp.modules.common.classifierr   nemo.core.classesr   nemo.core.neural_typesr	   r
   r   __all__
functionalr   r   rF   r   r   r   r    r    r    r!   <module>   s   ;