o
    wi+                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ eee	f Z
dddZdee
 d	ee	 d
ee
 dededededededee	 dedefddZdS )    N)Path)OptionalUnion)loggingreturnc                  C   s^   t d dd } g }dD ]}| |}|| q|r(t dd|  dS t d dS )	z
    SLURM_, PMI_, PMIX_ Variables are needed to be unset for trtllm export to work
    on clusters. This method takes care of unsetting these env variables
    z+Unsetting all SLURM_, PMI_, PMIX_ Variablesc                 S   s\   g }d|  d}t j|dddd}|j d}|D ]}|r+tj|d  || q|S )Nzenv | grep ^z | cut -d= -f1T)shellcapture_outputtext
)	
subprocessrunstdoutstripsplitosenvironpopappend)prefix
unset_varscmdresultvars_to_unsetvar r   ]/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/llm/deploy/base.pyunset_vars_with_prefix!   s   
z;unset_environment_variables.<locals>.unset_vars_with_prefix)SLURM_PMI_PMIX_zUnset env variables: z, zNo env variables were unset.N)r   infoextendjoin)r   all_unset_varsr   r   r   r   r   unset_environment_variables   s   
r$   nemo_checkpoint
model_typetriton_model_repositorynum_gpustensor_parallelism_sizepipeline_parallelism_sizemax_input_lenmax_output_lenmax_batch_sizedtypeoutput_context_logitsoutput_generation_logitsc                 C   s   ddl m} |du rd}t|jddd n|}| du r$|du r$td| du r2tj|s2td||| du d	}| durkzt	d
 |j
| ||||||||	|
|d W |S  tyj } ztdt| d}~ww |S )zp
    Exports the nemo checkpoint to trtllm and returns trt_llm_exporter that is used to deploy on PyTriton.
    r   )TensorRTLLMNz/tmp/trt_llm_model_dir/T)parentsexist_okzThe provided model repository is not a valid TensorRT-LLM model directory. Please provide a --nemo_checkpoint or a TensorRT-LLM engine.zThe provided model repository is not a valid TensorRT-LLM model directory. Please provide a --nemo_checkpoint or a valid TensorRT-LLM engine.)	model_dir
load_modelzOExport operation will be started to export the nemo checkpoint to TensorRT-LLM.)nemo_checkpoint_pathr&   gpus_per_noder)   r*   r+   r,   r-   r.   gather_context_logitsgather_generation_logitsz>An error has occurred during the model export. Error message: )nemo.export.tensorrt_llmr1   r   mkdir
ValueErrorr   pathisdirr   r    export	ExceptionRuntimeErrorstr)r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   trt_llm_pathtrt_llm_exportererrorr   r   r   get_trtllm_deployable:   sN   
rF   )r   N)r   r   pathlibr   typingr   r   
nemo.utilsr   rB   AnyPathr$   intboolrF   r   r   r   r   <module>   s@   
!	
