o
    }oi'                     @   sj  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
 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 dlmZ d dlmZmZ d dlmZmZ g dZdd Zd(de dee! fddZ"					d)dee# dee! dee! dee de
defddZ$								 d*de!d ee! d!e dee# d"ee! d#e d$e d%e%deeef fd&d'Z&dS )+    N)ListOptionalUnion)asdict)Trainer)
DictConfig)
BertModule)DecoderModule)EncoderModule)get_huggingface_lm_model)get_huggingface_pretrained_lm_models_list)#get_megatron_pretrained_bert_models)NeMoTransformerConfig)get_huggingface_transformerget_nemo_transformer)AppStatelogging)get_pretrained_lm_models_listget_lm_model	pad_batchc                 C   s^   g }t dd | D }| D ]}t|}||| k r%||g|| |   || q| |fS )Nc                 S   s   g | ]}t |qS  )len).0tokensr   r   `/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/nlp/modules/common/lm_utils.py
<listcomp>+   s    zpad_batch.<locals>.<listcomp>)maxr   extendappend)batchpad_idmax_lencontext_lengthsmax_context_lengthr   context_lengthr   r   r   r   )   s   r   Finclude_externalreturnc                 C   s
   t | dS )z
    Returns the list of supported pretrained model names

    Args:
        include_external if true includes all HuggingFace model names, not only those supported language models in NeMo.

    r%   )r   r'   r   r   r   r   4   s   
r   config_dictconfig_file
vocab_filetrainercfgc                    s  |j dr|j jr|j jtddvrt|j j d | r*|r*td| d d}|dr?|j ddr?|j dd}d	d
lm dtt	 ffdd}| }|j
durj|j
dddurjd|j
ddv sn||v rd	dl}d	d
lm G  fddd|jj |j drj|j j|d}	nj|j d|d}	  |	j_  |	j_  |	jj _|	S t| ||j jd}	|j drt }
|
jstj|j jst|j j d|	j|j jd |	S )a~  
    Helper function to instantiate a language model encoder, either from scratch or a pretrained model.
    If only pretrained_model_name are passed, a pretrained model is returned.
    If a configuration is passed, whether as a file or dictionary, the model is initialized with random weights.

    Args:
        config_dict: path to the model configuration dictionary
        config_file: path to the model configuration file
        vocab_file: path to vocab_file to be used with Megatron-LM
        trainer: an instance of a PyTorch Lightning trainer
        cfg: a model configuration
    Returns:
        Pretrained BertModule
    pretrained_model_nameFr'   zk is not in get_pretrained_lm_models_list(include_external=False), will be using AutoModel from HuggingFace.zLBoth config_dict and config_file were found, defaulting to use config_file: z will be used. language_modelr   MegatronBertModelr&   c                     s   dd    D } | S )Nc                 S   s   g | ]}|j qS r   )r-   )r   modelr   r   r   r   n   s    zMget_lm_model.<locals>.get_megatron_pretrained_bert_models.<locals>.<listcomp>)list_available_models)#all_pretrained_megatron_bert_modelsr0   r   r   r   l   s   z9get_lm_model.<locals>.get_megatron_pretrained_bert_modelsNtokenizer_namemegatronc                       s&   e Zd Z fddZdd Z  ZS )zget_lm_model.<locals>.Identityc                    s   t  |   d S N)super__init__)self)Identity	__class__r   r   r9   ~   s   z'get_lm_model.<locals>.Identity.__init__c                 W   s   |S r7   r   )r:   xargsr   r   r   forward   s   z&get_lm_model.<locals>.Identity.forward)__name__
__module____qualname__r9   r?   __classcell__r   )r;   )r<   r   r;   }   s    r;   lm_checkpoint)restore_pathr+   )r+   )r(   r)   r-   z
 not found)rE   )r/   getr-   r   r   warningAnemo.collections.nlp.models.language_modeling.megatron_bert_modelr1   r   str	tokenizertorchnnModulerestore_fromrD   from_pretrainedr2   lm_headbinary_headpoolerr   r   is_model_being_restoredospathexists
ValueErrorrestore_weights)r(   r)   r*   r+   r,   pretrain_model_namer   r4   rK   r2   	app_stater   )r;   r1   r   r   ?   sT   



r   nemoTlibrary
model_name
pretrainedcheckpoint_fileencoderpre_ln_final_layer_normpadding_idxc           	      C   s   d}| dkr)t |trt|}t||||||d}|dur'tj|r'td|S | dkr7t||||d}|S | dkr?tdtd	)
a  Gets Transformer based model to be used as an Encoder or Decoder in NeMo NLP.
       First choose the library to get the transformer from. This can be huggingface,
       megatron, or nemo. Use the model_name arg to get a named model architecture
       and use the pretrained arg to get the named model architecture with pretrained weights.

       If model_name is None, then we can pass in a custom configuration via the config_dict.
       For example, to instantiate a HuggingFace BERT model with custom configuration we would do:
       encoder = get_transformer(library='huggingface',
                                 config_dict={
                                     '_target_': 'transformers.BertConfig',
                                     'hidden_size': 1536
                                 })


    Args:
        library (str, optional): Can be 'nemo', 'huggingface', or 'megatron'. Defaults to 'nemo'.
        model_name (Optional[str], optional): Named model architecture from the chosen library. Defaults to None.
        pretrained (bool, optional): Use True to get pretrained weights.
                                     False will use the same architecture but with randomly initialized weights.
                                     Defaults to False.
        config_dict (Optional[dict], optional): Use for custom configuration of transformer. Defaults to None.
        checkpoint_file (Optional[str], optional): Provide weights for the transformer from a local checkpoint. Defaults to None.
        encoder (bool, optional): True returns an EncoderModule, False returns a DecoderModule. Defaults to True.

    Returns:
        Union[EncoderModule, DecoderModule]: Ensures that Encoder/Decoder will work in EncDecNLPModel
    Nr[   )r]   r^   r(   r`   ra   rb   zNLoading transformer weights from checkpoint file has not been implemented yet.huggingface)r]   r^   r(   r`   r6   z[megatron-lm bert support has been deprecated in NeMo 1.5+. Please use NeMo 1.4 for support.z2Libary must be 'nemo', 'huggingface' or 'megatron')	
isinstancer   r   r   rT   rU   isfilerW   r   )	r\   r]   r^   r(   r_   r`   ra   rb   r2   r   r   r   get_transformer   s4   &
	rf   )F)NNNNN)r[   NFNNTTr   )'rT   typingr   r   r   attrr   lightning.pytorchr   	omegaconfr   /nemo.collections.nlp.modules.common.bert_moduler   2nemo.collections.nlp.modules.common.decoder_moduler	   2nemo.collections.nlp.modules.common.encoder_moduler
   Anemo.collections.nlp.modules.common.huggingface.huggingface_utilsr   r   ;nemo.collections.nlp.modules.common.megatron.megatron_utilsr   ;nemo.collections.nlp.modules.common.transformer.transformerr   Anemo.collections.nlp.modules.common.transformer.transformer_utilsr   r   
nemo.utilsr   r   __all__r   boolrI   r   dictr   intrf   r   r   r   r   <module>   sz   
i
	