o
    e(                     @   sp   d dl Z d dlZddlmZ dd Zdd ZG dd	 d	eZd
d Z	dd Z
dd Zdd Zdd Zdd ZdS )    N   )SharedMemoryTensorc                 C   s   t | dd)z/
    Raise error with the provided message
    msgN)InferenceServerExceptionr    r   KD:\Projects\ConvertPro\env\Lib\site-packages\tritonclient/utils/__init__.pyraise_error$   s   r	   c                 C   sR   | j tjkr
td | jdkr'd}tj| dgddD ]
}|t| 7 }q|S dS )a  
    Get the underlying number of bytes for a numpy ndarray.

    Parameters
    ----------
    tensor_value : numpy.ndarray
        Numpy array to calculate the number of bytes for.

    Returns
    -------
    int
        Number of bytes present in this tensor
    z)The tensor_value dtype must be np.object_r   refs_okCflagsorder)dtypenpobject_r	   sizenditerlenitem)Ztensor_valuetotal_bytesobjr   r   r   serialized_byte_size+   s   
r   c                       sB   e Zd ZdZdddZ fddZdd Zd	d
 Zdd Z  Z	S )r   zException indicating non-Success status.

    Parameters
    ----------
    msg : str
        A brief description of error

    status : str
        The error code

    debug_details : str
        The additional details on the error

    Nc                 C   s   || _ || _|| _d S )N)_msg_status_debug_details)selfr   statusdebug_detailsr   r   r   __init__W   s   
z!InferenceServerException.__init__c                    s:   | j d u r
t  n| j }| jd urd| j d | }|S )N[z] )r   super__str__r   )r   r   	__class__r   r   r"   \   s   
z InferenceServerException.__str__c                 C      | j S )zGet the exception message.

        Returns
        -------
        str
            The message associated with this exception, or None if no message.

        )r   r   r   r   r   messageb      	z InferenceServerException.messagec                 C   r%   )zGet the status of the exception.

        Returns
        -------
        str
            Returns the status of the exception

        )r   r&   r   r   r   r   m   r(   zInferenceServerException.statusc                 C   r%   )zGet the detailed information about the exception
        for debugging purposes

        Returns
        -------
        str
            Returns the exception details

        )r   r&   r   r   r   r   x   s   
z&InferenceServerException.debug_details)NN)
__name__
__module____qualname____doc__r   r"   r'   r   r   __classcell__r   r   r#   r   r   G   s    
r   c                 C   s   | t krdS | tjkrdS | tjkrdS | tjkrdS | tjkr"dS | tjkr)dS | tjkr0dS | tjkr7dS | tj	kr>d	S | tj
krEd
S | tjkrLdS | tjkrSdS | tjks^| jtjkr`dS d S )NBOOLINT8INT16INT32INT64UINT8UINT16UINT32UINT64FP16FP32FP64BYTES)boolr   int8int16int32int64uint8uint16uint32uint64float16float32float64r   typebytes_)Znp_dtyper   r   r   np_to_triton_dtype   s6   










rI   c                 C   s   | dkrt S | dkrtjS | dkrtjS | dkrtjS | dkr"tjS | dkr)tjS | dkr0tjS | dkr7tjS | d	kr>tj	S | d
krEtj
S | dksM| dkrPtjS | dkrWtjS | dkr^tjS d S )Nr.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   ZBF16r9   r:   )r;   r   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   r   r   r   r   r   triton_to_np_dtype   s6   rK   c                 C   s   | j dkrtjdgtjdS | jtjkr| jjtjkrtd g }tj| dgddD ]3}| jtjkrIt|	 t
kr?|	 }nt|	 d}n|	 }|tdt| || q*d	|}tj|tjd}|jd
 sxtj|tjd}|S )al  
    Serializes a bytes tensor into a flat numpy array of length prepended
    bytes. The numpy array should use dtype of np.object_. For np.bytes_,
    numpy will remove trailing zeros at the end of byte sequence and because
    of this it should be avoided.

    Parameters
    ----------
    input_tensor : np.array
        The bytes tensor to serialize.

    Returns
    -------
    serialized_bytes_tensor : np.array
        The 1-D numpy array of type uint8 containing the serialized bytes in row-major form.

    Raises
    ------
    InferenceServerException
        If unable to serialize the given tensor.
    r   rJ   z/cannot serialize bytes tensor: invalid datatyper
   r   r   zutf-8<I    C_CONTIGUOUS)r   r   emptyr   r   rG   rH   r	   r   r   bytesstrencodeappendstructpackr   joinasarrayr   ascontiguousarray)input_tensorflattened_lsr   s	flattenedflattened_arrayr   r   r   serialize_byte_tensor   s$   



r^   c                 C   sz   t  }d}| }|t|k r5td||d }|d7 }td|||d }||7 }|| |t|k stj|tjdS )a  
    Deserializes an encoded bytes tensor into a
    numpy array of dtype of python objects

    Parameters
    ----------
    encoded_tensor : bytes
        The encoded bytes tensor where each element
        has its length in first 4 bytes followed by
        the content
    Returns
    -------
    string_tensor : np.array
        The 1-D numpy array of type object containing the
        deserialized bytes in row-major form.

    r   rL      z<{}srJ   )	listr   rT   unpack_fromformatrS   r   arrayr   )encoded_tensorstrsoffsetval_buflsbr   r   r   deserialize_bytes_tensor   s   
rj   c                 C   s   | j dkrtjdgtjdS | jtjkrtd g }tj| dgddD ]}t	d|dd	 }|
| q#d
|}tj|tjd}|jd sOtj|tjd}|S )a  
    Serializes a bfloat16 tensor into a flat numpy array of bytes.
    The numpy array should use dtype of np.float32.

    Parameters
    ----------
    input_tensor : np.array
        The bfloat16 tensor to serialize.

    Returns
    -------
    serialized_bf16_tensor : np.array
        The 1-D numpy array of type uint8 containing the serialized bytes in row-major form.

    Raises
    ------
    InferenceServerException
        If unable to serialize the given tensor.
    r   rJ   z.cannot serialize bf16 tensor: invalid datatyper
   r   r   <f   r_   rM   rN   )r   r   rO   r   r   rE   r	   r   rT   rU   rS   rV   rW   r   rX   )rY   rZ   r   Z	obj_bytesr\   r]   r   r   r   serialize_bf16_tensor  s   


rm   c                 C   sf   t  }d}| }|t|k r+td||d }|d7 }|tdd|  |t|k stj|tjdS )a  
    Deserializes an encoded bf16 tensor into a
    numpy array of dtype of python objects

    Parameters
    ----------
    encoded_tensor : bytes
        The encoded bytes tensor where each element
        is 2 bytes (size of bfloat16)
    Returns
    -------
    string_tensor : np.array
        The 1-D numpy array of type float32 containing the
        deserialized bytes in row-major form.

    r   z<2srl   rk   s     rJ   )	r`   r   rT   ra   rS   unpackr   rc   rE   )rd   re   rf   rg   ri   r   r   r   deserialize_bf16_tensorC  s   ro   )rT   numpyr   Z_shared_memory_tensorr   r	   r   	Exceptionr   rI   rK   r^   rj   rm   ro   r   r   r   r   <module>   s   >8,