o
    }oix                  	   @   s   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	Z	de
defddZ	dd	ee
ef d
edee
 ddfddZde
defddZddeee
f dede	jfddZde	jjde
fddZdee
e	jf fddZdddZdS )     N)Counter)Path)DictOptionalUnioncheckpoint_pathreturnc                 C   s   t | }|d  S )z
    Checks if the checkpoint is in NeMo 2.0 format.
    Args:
        checkpoint_path (str): Path to a checkpoint.
    Returns:
        bool: True if the path points to a NeMo 2.0 checkpoint; otherwise false.
    context)r   is_dir)r   	ckpt_path r   K/home/ubuntu/.local/lib/python3.10/site-packages/nemo/export/utils/utils.pyis_nemo2_checkpoint   s   	r   	model_dirdelete_existing_filessubdirc                 C   s^   t | }| r|rt| nt| rtd| d|dur&|| }|jddd dS )a  
    Prepares model_dir path for the TensorRTT-LLM / vLLM export.
    Makes sure that the model_dir directory exists and is empty.

    Args:
        model_dir (str): Path to the target directory for the export.
        delete_existing_files (bool): Attempt to delete existing files if they exist.
        subdir (Optional[str]): Subdirectory to create inside the model_dir.

    Returns:
        None
    zThere are files in z0 folder: try setting delete_existing_files=True.NT)parentsexist_ok)r   existsshutilrmtreeanyiterdirRuntimeErrormkdir)r   r   r   
model_pathr   r   r   prepare_directory_for_export$   s   r   pathc                 C   s   t | }| o|jdkS )z
    Checks if the path exists and points to packed NeMo 1 checkpoint.

    Args:
        path (str): Path to possible checkpoint.
    Returns:
        bool: NeMo 1 checkpoint exists and is in '.nemo' format.
    z.nemo)r   r   suffix)r   r   r   r   r   is_nemo_tarfile@   s   	r   T	precisionmegatron_amp_O2c                 C   sD   |st jS | dv rt jS | dv rt jS | dv rt jS td|  d)ar  
    Mapping from PyTorch Lighthing (PTL) precision types to corresponding PyTorch parameter data type.

    Args:
        precision (Union[int, str]): The PTL precision type used.
        megatron_amp_O2 (bool): A flag indicating if Megatron AMP O2 is enabled.

    Returns:
        torch.dtype: The corresponding PyTorch data type based on the provided precision.
    )bf16z
bf16-mixed)   16z16-mixed)    32z32-truez"Could not parse the precision of 'z' to a valid torch.dtype)torchfloat32bfloat16float16
ValueError)r    r!   r   r   r   torch_dtype_from_precisionN   s   r,   modulec                 C   sZ   dd |   D }dd |  D }||}t|dkr%td| d|r+| S dS )zEFind the device type the model is assigned to and ensure consistency.c                 S      h | ]}|j jqS r   devicetype).0paramr   r   r   	<setcomp>i       z(get_model_device_type.<locals>.<setcomp>c                 S   r.   r   r/   )r2   bufferr   r   r   r4   j   r5      z;Model parameters and buffers are on multiple device types: z@. Ensure all parameters and buffers are on the same device type.cpu)
parametersbuffersunionlenr+   pop)r-   param_device_typesbuffer_device_typesall_device_typesr   r   r   get_model_device_typef   s   

rA   c                 C   s   t | ddgddgdd}|S )zvGets example data to feed to the model during ONNX export.

    Returns:
        Dictionary of tokenizer outputs.
    zexample query onezexample query twozexample passage onezexample passage twopt)return_tensors)dict)	tokenizerexample_inputsr   r   r   get_example_inputsw   s   rG   c                 C   st   ddl }g }| D ]}|j|jj|jjhv r|| q|s#d}t|tdd |D }|jj	|vr8d}t|dS )zChecks the network to ensure it's compatible with fp8 precison.

    Raises:
        ValueError if netowrk doesn't container Q/DQ FP8 layers
    r   Nz#No Quantize/Dequantize layers foundc                 s   s    | ]}|j V  qd S N)r    )r2   layerr   r   r   	<genexpr>   s    z'validate_fp8_network.<locals>.<genexpr>z>Found Quantize/Dequantize layers. But none with FP8 precision.)
tensorrtr1   	LayerTypeQUANTIZE
DEQUANTIZEappendr+   r   DataTypeFP8)networktrtquantize_dequantize_layersrI   	error_msg quantize_dequantize_layer_dtypesr   r   r   validate_fp8_network   s   
rW   rH   )T)r   N)r   collectionsr   pathlibr   typingr   r   r   r'   strboolr   r   r   intdtyper,   nnModulerA   TensorrG   rW   r   r   r   r   <module>   s*   

"