o
    }oiN                     @   sl   d dl mZmZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ dgZG dd de	eZdS )	    )DictOptionalN)nntransformer_weights_init)
ExportableNeuralModule)ChannelType
NeuralType
Classifierc                       s   e Zd ZdZedeeeef  fddZ	dde
deddf fd	d
ZdefddZdddZdefddZedefddZ  ZS )r   zJ
    A baseclass for modules to perform various classification tasks.
    returnc                 C   s   dt dt iS )z
        Returns definitions of module input ports.
        We implement it here since all NLP classifiers have the same inputs
        hidden_states)BTD)r
   r	   )self r   d/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/submodules/classifier.pyinput_types    s   zClassifier.input_types        hidden_sizedropoutNc                    s    t    || _t|| _dS )z
        Initializes the Classifier base module.
        Args:
            hidden_size: the size of the hidden dimension
            dropout: dropout to apply to the input hidden states
        N)super__init___hidden_sizer   Dropoutr   )r   r   r   	__class__r   r   r   (   s   
zClassifier.__init__use_transformer_initc                 C   s   |r|  dd  dS dS )z
        Common post-processing to be called at the end of concrete Classifiers init methods
        Args:
          use_transformer_init : whether or not to apply transformer_weights_init
        c                 S   s   t | ddS )NF)xavierr   )moduler   r   r   <lambda>:   s    z&Classifier.post_init.<locals>.<lambda>N)apply)r   r   r   r   r   	post_init3   s   zClassifier.post_init      c                 C   s6   t |  }t||| j|j|j}t|gS )zs
        Generates input examples for tracing etc.
        Returns:
            A tuple of input examples.
        )	next
parameterstorchrandnr   todevicedtypetuple)r   	max_batchmax_dimsampleexampler   r   r   input_example<   s    
zClassifier.input_example	save_pathc                 C      dS )z
        Saves the module to the specified path.
        Args:
            save_path: Path to where to save the module.
        Nr   )r   r3   r   r   r   save_toF   s   zClassifier.save_torestore_pathc                 C   r4   )z
        Restores the module from the specified path.
        Args:
            restore_path: Path to restore the module from.
        Nr   )clsr6   r   r   r   restore_fromN   s   zClassifier.restore_from)r   )r$   r%   )__name__
__module____qualname____doc__propertyr   r   strr
   r   intfloatr   boolr#   r2   r5   classmethodr8   __classcell__r   r   r   r   r      s    
	
)typingr   r   r(   r   nemo.collections.common.partsr   nemo.core.classesr   r   nemo.core.neural_typesr	   r
   __all__r   r   r   r   r   <module>   s   