o
    }oi                     @   s~  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 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 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)m*Z* eG dd dZ+de,dej-fddZ.d$dedee de+fddZ/d$dedee fddZ0d$dedee fd d!Z1d$dedee fd"d#Z2dS )%    )	dataclass)Path)Optional)
DictConfig	OmegaConf)	lightning)AutoTokenizer)_setupfinetune)AudioToTextDataModule)SpeechToTextLLMSpeechToTextLLMConfig)ASRModuleConfig)ModalityAdapterConfig)SpeechToTextLLMPEFTget_object_list_from_config) prepare_pretrained_llm_dist_ckpt)Serialization	typecheck)
AutoResume)PreemptionCallback)logging)StatelessTimerTimingCallbackc                   @   s   e Zd ZU dZee ed< dZee ed< dZ	ee
j ed< dZee
j ed< dZee ed< dZee ed< dZee
j ed< dZee ed	< dS )
PipelineComponentsNmodeldatatraineroptimpeftresumeloggercfg)__name__
__module____qualname__r   r   r   __annotations__r   r   r   nlTrainerr   MegatronOptimizerModuler   r   r    r   r!   
NeMoLoggerr"   dict r,   r,   ^/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/speechlm/recipes/pipeline.pyr   %   s   
 r   r"   r!   c                 C   s   |j r|j n|j}|d u rtt d }|jr|jnd}t|| }|jddd t|d d}|t	
|  W d    d S 1 sEw   Y  d S )Nnemo_experimentsdefaultT)parentsexist_okzconfig.yamlw)explicit_log_dirlog_dirstrr   cwdnamemkdiropenwriter   to_yaml)r"   r!   r4   r7   
output_dirfr,   r,   r-   dump_config1   s   "r>   N	tokenizerreturnc                 C   s  t jdd tj| dd} tdt|   |d ur%td|  n&d| d d	 v rFtd
| d d	 d   t| d d	 d }ntddt	| d d	 d t
| d d	 d td-i | d d td-i | d d | d d	 dd | d d | d d | d d | d d | d dd | d dd d}|jrt| t||d}t| d |d}t
| d }t| d }| dd r| d ddrtd d!| d d  d"td#| d  d$ |t| d  n|t  |t  tjd-t
| d t| d% |d&| d' }d }| d d(d r+tt
| d d( d)}t
| d* }	t
| d+ }
t||||||	|
| d,S ).NF)enabledT)resolvezHydra config: zUsing provided tokenizer: pretrained_modelr   llmz'Using tokenizer from pretrained model: zPTokenizer is not provided, please pass `tokenizer` to `speech_to_text_llm_train`z,or specify `pretrained_model` in the config._target_configspeech_encodermodality_adapterfreeze_language_modelfreeze_speech_modelfreeze_modality_adapterr   commonresume_speech_model_from_path!resume_modality_adapter_from_path)language_model_classlanguage_model_configspeech_model_configmodality_adapter_configlanguage_model_from_pretrainedrI   rJ   rK   data_configrM   rN   )rF   r?   )r?   r   	callbacksmax_time_per_runstrategyckpt_async_savezW`strategy.ckpt_async_save` must be `False` to save ckpt when `max_time_per_run` is set,zgot z0. `max_time_per_run` will not work in this case!zSetting max_time_per_run=z for the training job.plugins)rW   rY   rU   r   r   )r   r    r!   )r   r   r   r   r   r    r!   r"   r,   )r   set_typecheck_enabledr   to_containerr   infor;   r   
ValueErrorr   r   from_config_dictr   r   getrS   r   r   r   r   appendr   r   r   r'   r(   r   r   )r"   r?   model_configr   r   r   rU   r   r   r    r!   r,   r,   r-   build_components=   s~   




rb   c              	   C   sD   t | |}t|j|j t|j|j|j|j|j|j	|j
d |jjS )z&Train the model using provided config.)r   r   r   r   logr   r    )rb   r>   r"   r!   r
   r   r   r   r   r   r    r4   )r"   r?   
componentsr,   r,   r-   speech_to_text_llm_train   s   

re   c              
   C   sJ   t | |}t|j|j|j|j|j|j||jd}|jj	|j|jd |j
S )z
    Validate the model using provided config, groundtruth required.

    NOTE: Can use dummy groundtruth (e.g., answer='-') for inference
    when speech_to_text_llm_generate is not implemented yet.
    )r   r   r   rc   r    r   r?   model_transform)
datamodule)rb   r	   r   r   r   r!   r    r   r   validater4   )r"   r?   rd   	app_stater,   r,   r-   speech_to_text_llm_validate   s   
rj   c                 C   s   t d)z<Running inference using provided config without groundtruth.z%This function is not implemented yet.)NotImplementedError)r"   r?   r,   r,   r-   speech_to_text_llm_generate   s   rl   )N)3dataclassesr   pathlibr   typingr   	omegaconfr   r   nemor   r'   "nemo.collections.common.tokenizersr   nemo.collections.speechlm.apir	   r
   3nemo.collections.speechlm.data.audio_to_text_moduler   9nemo.collections.speechlm.models.speech_to_text_llm_modelr   r   ,nemo.collections.speechlm.modules.asr_moduler   2nemo.collections.speechlm.modules.modality_adapterr   nemo.collections.speechlm.utilsr   r   "nemo.collections.speechlm.utils.ior   nemo.core.classes.commonr   r   nemo.lightningr    nemo.lightning.pytorch.callbacksr   
nemo.utilsr   nemo.utils.exp_managerr   r   r   r+   r*   r>   rb   re   rj   rl   r,   r,   r,   r-   <module>   s2   W