o
    e&                     @   sJ   d dl Z d dlZd dlZd dlZd dlmZmZm	Z	m
Z
 G dd dZdS )    N)deserialize_bf16_tensordeserialize_bytes_tensorraise_errortriton_to_np_dtypec                   @   s@   e Zd ZdZdd Ze	dddZdd	 Zd
d Zdd Z	dS )InferResultak  An object of InferResult class holds the response of
    an inference request and provide methods to retrieve
    inference results.

    Parameters
    ----------
    response : geventhttpclient.response.HTTPSocketPoolResponse
        The inference response from the server
    verbose : bool
        If True generate verbose output. Default value is False.
    c              
   C   sR  | d}G dd d}| d}|d ur0|dkr#|t| }n|dkr0|t| }|d u rb| }|r>t| z	t|| _W d S  t	ya } zt
d|  W Y d }~d S d }~ww t|}|j|d}|rrt| t|| _i | _| | _d	}| jd
 D ]}	|	 d}
|
d ur|
 d}|d ur|| j|	d < || }qd S )NInference-Header-Content-Lengthc                   @   s   e Zd Zdd ZdddZdS )z2InferResult.__init__.<locals>.DecompressedResponsec                 S   s   || _ d| _d S )Nr   Zdecompressed_data_offset_)selfZdecompressed_data r   OD:\Projects\ConvertPro\env\Lib\site-packages\tritonclient/http/_infer_result.py__init__;   s   
z;InferResult.__init__.<locals>.DecompressedResponse.__init__c                 S   <   |dkr| j | jd  S | j}|  j|7  _| j || j S Nr   r   r
   lengthZprev_offsetr   r   r   read?   
   z7InferResult.__init__.<locals>.DecompressedResponse.readNr   )__name__
__module____qualname__r   r   r   r   r   r   DecompressedResponse:   s    r   Content-EncodinggzipdeflatezkFailed to encode using UTF-8. Please use binary_data=True, if you want to pass a byte array. UnicodeError: )r   r   outputs
parametersbinary_data_sizename)getr   
decompressr   zlibprintjsonloads_resultUnicodeDecodeErrorr   int_output_name_to_buffer_map_buffer)r
   responseverboseheader_lengthr   content_encodingcontentebuffer_indexoutputr   this_data_sizer   r   r   r   6   sN   




zInferResult.__init__FNc                 C   s    G dd d}| |||||S )a  A class method to construct InferResult object
        from a given 'response_body'.

        Parameters
        ----------
        response_body : bytes
            The inference response from the server
        verbose : bool
            If True generate verbose output. Default value is False.
        header_length : int
            The length of the inference header if the header does not occupy
            the whole response body. Default value is None.
        content_encoding : string
            The encoding of the response body if it is compressed.
            Default value is None.

        Returns
        -------
        InferResult
            The InferResult object generated from the response body
        c                   @   s&   e Zd Zdd Zdd Zd	ddZdS )
z0InferResult.from_response_body.<locals>.Responsec                 S   s   || _ d| _||d| _d S )Nr   )r   r   )response_body_r	   parameters_)r
   response_bodyr.   r/   r   r   r   r      s
   z9InferResult.from_response_body.<locals>.Response.__init__c                 S   s   | j |S )N)r6   r!   )r
   keyr   r   r   r!      s   z4InferResult.from_response_body.<locals>.Response.getr   c                 S   r   r   )r5   r	   r   r   r   r   r      r   z5InferResult.from_response_body.<locals>.Response.readNr   )r   r   r   r   r!   r   r   r   r   r   Response   s    r9   r   )clsr7   r-   r.   r/   r9   r   r   r   from_response_bodyl   s   zInferResult.from_response_bodyc           
      C   s  | j ddur| j d D ]w}|d |kr|d }d}|d}|durl|d}|durld}|d	krg| j| }|| }|d
krJt| j|| }	n"|dkrXt| j|| }	ntj| j|| t|d}	nt	d	}	|sytj
|d t|d}	|	|d }	|	  S qdS )a  Get the tensor data for output associated with this object
        in numpy format

        Parameters
        ----------
        name : str
            The name of the output tensor whose result is to be retrieved.

        Returns
        -------
        numpy array
            The numpy array containing the response data for the tensor or
            None if the data for specified tensor name is not found.
        r   Nr    datatypeFr   r   Tr   ZBYTESZBF16)Zdtypedatashape)r'   r!   r*   r   r+   r   npZ
frombufferr   emptyarrayZreshape)
r
   r    r3   r<   Zhas_binary_datar   r4   Zstart_indexZ	end_indexZnp_arrayr   r   r   as_numpy   sD   



$zInferResult.as_numpyc                 C   s(   | j d D ]}|d |kr|  S qdS )a  Retrieves the output tensor corresponding to the named output.

        Parameters
        ----------
        name : str
            The name of the tensor for which Output is to be
            retrieved.

        Returns
        -------
        Dict
            If an output tensor with specified name is present in
            the infer response then returns it as a json dict,
            otherwise returns None.
        r   r    Nr'   )r
   r    r3   r   r   r   
get_output   s
   zInferResult.get_outputc                 C   s   | j S )zRetrieves the complete response

        Returns
        -------
        dict
            The underlying response dict.
        rC   )r
   r   r   r   get_response   s   zInferResult.get_response)FNN)
r   r   r   __doc__r   classmethodr;   rB   rD   rE   r   r   r   r   r   )   s    607r   )r   r#   numpyr?   Z	rapidjsonr%   Ztritonclient.utilsr   r   r   r   r   r   r   r   r   <module>   s   