o
    }oi@                     @   s|   d dl Z d dlZ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mZ d dlmZ dgZG dd dee	ZdS )	    N)DictOptional)NeuralModule)
Exportable)ChannelTypeMaskType
NeuralType)logging
BertModulec                   @   s`   e Zd Zedeeeef  fddZedeeeef  fddZ	defddZ
dddZdS )r
   returnc                 C   s,   t dt t dt ddt dt dddS )N)BTT)optional	input_idsattention_masktoken_type_ids)r   r   r   self r   c/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/nlp/modules/common/bert_module.pyinput_types   s   
zBertModule.input_typesc                 C   s   dt dt iS )Nlast_hidden_states)r   r   D)r   r   r   r   r   r   output_types&   s   zBertModule.output_typesrestore_pathc                    s  t d|  tj|st d| d dS t|}d| v r(|d }t	
dt| d }|rF|dfdd	| D }|    fd
d	| D }d v rcd|vrc d |d< t|t ksmJ  | |   t dt| j d|  dS )zRestores module/model's weightszRestoring weights from zPath z
 not foundN
state_dictz
^bert.*?\.r   c                    s"   i | ]\}}|t  d  |qS )N)len.0kv)prefixr   r   
<dictcomp><   s   " z.BertModule.restore_weights.<locals>.<dictcomp>c                    s   i | ]\}}| v r||qS r   r   r   )
model_dictr   r   r#   >   s    zembeddings.position_idszWeights for z restored from )r	   infoospathexistswarningtorchloadkeysrematchlistgroupitemsr   r   updateload_state_dicttype__name__)r   r   pretrained_dictmr   )r$   r"   r   restore_weights*   s&   



 zBertModule.restore_weights      c           	      C   sj   t |  }||f}tjd|d ||jd}tjdd||jd}tjdd||jd}|||d}t|gS )zs
        Generates input examples for tracing etc.
        Returns:
            A tuple of input examples.
        r   r9   )lowhighsizedevicer   )next
parametersr*   randintr>   tuple)	r   	max_batchmax_dimsampleszr   r   r   
input_dictr   r   r   input_exampleJ   s   
zBertModule.input_exampleN)r9   r:   )r5   
__module____qualname__propertyr   r   strr   r   r   r8   rH   r   r   r   r   r
      s     )r&   r-   typingr   r   r*   nemo.core.classesr   nemo.core.classes.exportabler   nemo.core.neural_typesr   r   r   
nemo.utilsr	   __all__r
   r   r   r   r   <module>   s   