o
    }oi                     @   s  d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ dZdZ		ddejejeejf  d	ejejeejf  fd
dZdedefddZdeje dejfddZdejdeje fddZdejdejej fddZdd ZdddZdS )    N)Path)ImageTensor)TarPathzNEMO 2.0zNEMO 1.0overwrite_kwargsdefaultsc                    s>   dd fdd pi t  fddt|  D S )a  Converts a type dictionary class into a tuple of PyTriton Tensor objects.

    This function takes a class with type hints and converts each typed field into a PyTriton
    Tensor specification, handling nested list types and mapping Python types to numpy dtypes.

    Args:
        typedict_class: A class with type hints that will be converted to Tensor specs
        overwrite_kwargs: Optional dictionary of kwargs to override default Tensor parameters
        defaults: Optional dictionary of default values (unused)

    Returns:
        tuple: A tuple of PyTriton Tensor objects, one for each typed field in the input class

    Raises:
        Exception: If an unsupported type is encountered during type mapping
    c                 S   sD   | t u rtjS | tu rtjS | tu rtjS | tu rtS t	d|  )NzUnknown type )
intnpint32floatfloat32boolbool_strbytes	Exception)type_ r   E/home/ubuntu/.local/lib/python3.10/site-packages/nemo/deploy/utils.py	_map_type4   s   z"typedict2tensor.<locals>._map_typec                    s`   d}t | tu rt | d } |d7 }t | tu s	|d8 }|dkr'd| nd}| | dS )Nr      ))r   )shapedtype)typing
get_originlistget_args)r   countr   )r   r   r   _get_tensor_params@   s   z+typedict2tensor.<locals>._get_tensor_paramsc                 3   s.    | ]\}}t dd |i |V  qdS )nameNr   r   ).0r!   r   )r    r   r   r   	<genexpr>J   s
    
z"typedict2tensor.<locals>.<genexpr>)tupler   get_type_hintsitems)typedict_classr   r   r   )r    r   r   r   typedict2tensor   s   	r(   pathreturnc                 C   s>   t j| rt| } nt| } | d  r| d  rtS tS )a  Determines the version of a NeMo checkpoint from its file structure.

    Examines the provided checkpoint path to determine if it follows the NeMo 2.0
    or NeMo 1.0 format based on the presence of 'context' and 'weights' directories.

    Args:
        path (str): Path to the NeMo checkpoint file or directory

    Returns:
        str: Version string - either NEMO2 or NEMO1 constant indicating the checkpoint version
    contextweights)osr)   isdirr   r   existsNEMO2NEMO1)r)   r   r   r   nemo_checkpoint_versionP   s   
r2   str_listc                 C   s"   t | dt jf }t j|dS )a  Converts a list of strings to a numpy array of UTF-8 encoded bytes.

    Takes a list of strings and converts it to a numpy array with an additional
    dimension, then encodes the strings as UTF-8 bytes.

    Args:
        str_list (List[str]): List of strings to convert

    Returns:
        np.ndarray: Numpy array of UTF-8 encoded bytes with shape (N, 1) where N is
            the length of the input list
    .utf-8)r
   arraynewaxischarencode)r3   str_ndarrayr   r   r   str_list2numpyh   s   r:   r9   c                 C   s.   |  d} tjj| dd} | jdd} |  S )a  Converts a numpy array of UTF-8 encoded bytes back to a list of strings.

    Takes a numpy array of UTF-8 encoded bytes and decodes it back to strings,
    removing any extra dimensions, and returns the result as a Python list.

    Args:
        str_ndarray (np.ndarray): Numpy array of UTF-8 encoded bytes, typically
            with shape (N, 1) where N is the length of the resulting list

    Returns:
        List[str]: List of decoded strings
    r   r4   )encodingr   )axis)astyper
   r7   decodesqueezetolist)r9   r   r   r   str_ndarray2listy   s   
rA   img_ndarrayc                 C   s   dd | D }|S )a  Converts a numpy array of images to a list of PIL Image objects.

    Takes a numpy array containing one or more images and converts each image
    to a PIL Image object using Image.fromarray().

    Args:
        img_ndarray (np.ndarray): Numpy array of images, where each image is a 2D or 3D array
            representing pixel values

    Returns:
        List[Image.Image]: List of PIL Image objects created from the input array
    c                 S   s   g | ]}t |qS r   )r   	fromarray)r"   ir   r   r   
<listcomp>   s    zndarray2img.<locals>.<listcomp>r   )rB   img_listr   r   r   ndarray2img   s   rG   c                 C   sx   t | tjr|   } nt | tjs+t| } |ttj	t
tjfv }|r+tj| d} | jdk r7| dtjf } | |S )a  Casts input data to a numpy array with the required dtype.

    Takes input data that may be a torch.Tensor, numpy array, or other sequence type
    and converts it to a numpy array with the specified dtype. For string dtypes,
    the data is encoded as UTF-8 bytes. The output array is ensured to have at least
    2 dimensions.

    Args:
        data: Input data to cast. Can be a torch.Tensor, numpy array, or sequence type
            that can be converted to a numpy array.
        required_dtype: The desired numpy dtype for the output array.

    Returns:
        np.ndarray: A numpy array containing the input data cast to the required dtype,
            with at least 2 dimensions.
    r4      .)
isinstancetorchr   cpunumpyr
   ndarrayr5   objectobject_r   bytes_r7   r8   ndimr6   r=   )datarequired_dtypedata_is_strr   r   r   cast_output   s   


rU   c                 C   sF   t j s	tdt j |kr| gndg}t jj|||d |d S )a#  Broadcasts a list of text data to all processes.

    Args:
        data (list): List of strings to broadcast.
        src (int, optional): Source rank. Defaults to 0.
        group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used.
    z+Distributed environment is not initialized.N)srcgroupr   )rJ   distributedis_initializedRuntimeErrorget_rankbroadcast_object_list)rR   rV   rW   object_listr   r   r   broadcast_list   s
   
	r^   )NN)r   N)r-   r   pathlibr   rL   r
   rJ   PILr   pytriton.model_configr   nemo.export.tarutilsr   r0   r1   OptionalDictr   Anyr(   r2   ListrM   r:   rA   rG   rU   r^   r   r   r   r   <module>   s.   
2 