o
    wi(                     @   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   X/home/ubuntu/sommelier/.venv/lib/python3.10/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)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_)np_dtyper   r   r   np_to_triton_dtype   s6   










rK   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   BF16r:   r;   )r<   r   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r   r   r   r   r   triton_to_np_dtype   s6   rN   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 )aj  
    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   rM   z/cannot serialize bytes tensor: invalid datatyper
   r   r   zutf-8<I    C_CONTIGUOUS)r   r   emptyr   r   rH   rI   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$   



ra   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   rO      z<{}srM   )	listr   rW   unpack_fromformatrV   r   arrayr   )encoded_tensorstrsoffsetval_buflsbr   r   r   deserialize_bytes_tensor   s   
rm   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   rM   z.cannot serialize bf16 tensor: invalid datatyper
   r   r   <f   rb   rP   rQ   )r   r   rR   r   r   rF   r	   r   rW   rX   rV   rY   rZ   r   r[   )r\   r]   r   	obj_bytesr_   r`   r   r   r   serialize_bf16_tensor  s   


rq   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<2sro   rn   s     rM   )	rc   r   rW   rd   rV   unpackr   rf   rF   )rg   rh   ri   rj   rl   r   r   r   deserialize_bf16_tensorC  s   rs   )rW   numpyr   _shared_memory_tensorr   r	   r   	Exceptionr   rK   rN   ra   rm   rq   rs   r   r   r   r   <module>   s   >8,