o
    ez                     @   s.   d dl Zd dlmZ d dlT G dd dZdS )    N)MessageToJson)*c                   @   s4   e Zd ZdZdd Zdd ZdddZdd	d
ZdS )InferResultzAn object of InferResult class holds the response of
    an inference request and provide methods to retrieve
    inference results.

    Parameters
    ----------
    result : protobuf message
        The ModelInferResponse returned by the server
    c                 C   s
   || _ d S )N)_result)selfresult r   OD:\Projects\ConvertPro\env\Lib\site-packages\tritonclient/grpc/_infer_result.py__init__-   s   
zInferResult.__init__c                 C   s   d}| j jD ]k}|j|krmg }|jD ]}|| q|j}|t| j jk rM|dkr2t| j j| }n2|dkr?t	| j j| }n%t
j| j j| t|d}nt|jjdkr_t
j|jjdd}nt
d}||}|  S |d7 }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   ZBYTESZBF16)ZdtypeF)copy   N)r   outputsnameshapeappenddatatypelenZraw_output_contentsZdeserialize_bytes_tensorZdeserialize_bf16_tensornpZ
frombufferZtriton_to_np_dtypecontentsZbytes_contentsarrayemptyZreshape)r   r   indexoutputr   valuer   Znp_arrayr   r   r	   as_numpy0   s6   







zInferResult.as_numpyFc                 C   s6   | j jD ]}|j|kr|rt|dd q|  S qdS )a  Retrieves the InferOutputTensor corresponding to the
        named output.

        Parameters
        ----------
        name : str
            The name of the tensor for which Output is to be
            retrieved.
        as_json : bool
            If True then returns response as a json dict, otherwise
            as a protobuf message. Default value is False.
            The returned json is generated from the protobuf message
            using MessageToJson and as a result int64 values are
            represented as string. It is the caller's responsibility
            to convert these strings back to int64 values as
            necessary.

        Returns
        -------
        protobuf message or dict
            If a InferOutputTensor with specified name is present in
            ModelInferResponse then returns it as a protobuf message
            or dict, otherwise returns None.
        TZpreserving_proto_field_nameN)r   r   r   r   )r   r   as_jsonr   r   r   r	   
get_outputc   s   
zInferResult.get_outputc                 C   s   |rt t| jddS | jS )a  Retrieves the complete ModelInferResponse as a
        json dict object or protobuf message

        Parameters
        ----------
        as_json : bool
            If True then returns response as a json dict, otherwise
            as a protobuf message. Default value is False.
            The returned json is generated from the protobuf message
            using MessageToJson and as a result int64 values are
            represented as string. It is the caller's responsibility
            to convert these strings back to int64 values as
            necessary.

        Returns
        -------
        protobuf message or dict
            The underlying ModelInferResponse as a protobuf message or dict.
        Tr   )jsonloadsr   r   )r   r   r   r   r	   get_response   s
   zInferResult.get_responseN)F)__name__
__module____qualname____doc__r
   r   r   r    r   r   r   r	   r   "   s    

3"r   )Z	rapidjsonr   Zgoogle.protobuf.json_formatr   Ztritonclient.utilsr   r   r   r   r	   <module>   s   