
    j_                    H    d dl mZ d dlZd dlmZ d dlZ G d d          ZdS )    )annotationsN)urlsplitc                  $    e Zd ZdZdddZddZdS )TritonRemoteModela  Client for interacting with a remote Triton Inference Server model.

    This class provides a convenient interface for sending inference requests to a Triton Inference Server and
    processing the responses. Supports both HTTP and gRPC communication protocols.

    Attributes:
        endpoint (str): The name of the model on the Triton server.
        url (str): The URL of the Triton server.
        triton_client: The Triton client (either HTTP or gRPC).
        InferInput: The input class for the Triton client.
        InferRequestedOutput: The output request class for the Triton client.
        input_formats (list[str]): The data types of the model inputs.
        np_input_formats (list[type]): The numpy data types of the model inputs.
        input_names (list[str]): The names of the model inputs.
        output_names (list[str]): The names of the model outputs.
        metadata: The metadata associated with the model.

    Methods:
        __call__: Call the model with the given inputs and return the outputs.

    Examples:
        Initialize a Triton client with HTTP
        >>> model = TritonRemoteModel(url="localhost:8000", endpoint="yolov8", scheme="http")

        Make inference with numpy arrays
        >>> outputs = model(np.random.rand(1, 3, 640, 640).astype(np.float32))
     urlstrendpointschemec                   |sS|sQt          |          }|j                            d                              dd          d         }|j        }|j        }|| _        || _        |dk    rCddlm	} |
                    | j        dd          | _        | j                            |          }nJddlm} |
                    | j        dd          | _        | j                            |d	          d
         }t          |d         d           |d<   t           j        t           j        t           j        d|j        | _        |j        | _        d |d         D             | _        fd| j        D             | _        d |d         D             | _        d |d         D             | _        t5          j        |                    di                               di                               dd                    | _        dS )a  Initialize the TritonRemoteModel for interacting with a remote Triton Inference Server.

        Arguments may be provided individually or parsed from a collective 'url' argument of the form
        <scheme>://<netloc>/<endpoint>/<task_name>

        Args:
            url (str): The URL of the Triton server.
            endpoint (str, optional): The name of the model on the Triton server.
            scheme (str, optional): The communication scheme ('http' or 'grpc').
        /   r   httpNF)r   verbosesslT)as_jsonconfigoutputc                ,    |                      d          S )Nname)get)xs    ]/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/triton.py<lambda>z,TritonRemoteModel.__init__.<locals>.<lambda>I   s    !%%--     )key)	TYPE_FP32	TYPE_FP16
TYPE_UINT8c                    g | ]
}|d          S )	data_type .0r   s     r   
<listcomp>z.TritonRemoteModel.__init__.<locals>.<listcomp>O   s    FFFanFFFr   inputc                     g | ]
}|         S r"   r"   )r$   r   type_maps     r   r%   z.TritonRemoteModel.__init__.<locals>.<listcomp>P   s     I I I! I I Ir   c                    g | ]
}|d          S r   r"   r#   s     r   r%   z.TritonRemoteModel.__init__.<locals>.<listcomp>Q   s    ???!AfI???r   c                    g | ]
}|d          S r*   r"   r#   s     r   r%   z.TritonRemoteModel.__init__.<locals>.<listcomp>R   s    AAA1QvYAAAr   
parametersmetadatastring_valueNone)r   pathstripsplitr   netlocr
   r   tritonclient.httpr   InferenceServerClienttriton_clientget_model_configtritonclient.grpcgrpcsortednpfloat32float16uint8InferRequestedOutput
InferInputinput_formatsnp_input_formatsinput_namesoutput_namesastliteral_evalr   r-   )selfr   r
   r   splitsclientr   r(   s          @r   __init__zTritonRemoteModel.__init__(   s     	  	 c]]F{((--33C;;A>H]F-C  V......!'!=!=$(TY_d!=!e!eD'88BBFF......!'!=!=$(TY_d!=!e!eD'8848PPQYZF "&"28O8OPPPx "$"*TVT\]]$*$?! +FFfWoFFF I I I Id6H I I I??vg???AAx0@AAA(L")E)E)I)I*VX)Y)Y)])]^lnt)u)uvvr   inputs
np.ndarrayreturnlist[np.ndarray]c           	     2    g }|d         j         t          |          D ]\  }}|j          j        |         k    r |                     j        |                   }                      j        |         g |j         j        |                             dd                    }|	                    |           |
                    |            fd j        D             } j                             j        ||          fd j        D             S )a  Call the model with the given inputs and return inference results.

        Args:
            *inputs (np.ndarray): Input data to the model. Each array should match the expected shape and type for the
                corresponding model input.

        Returns:
            (list[np.ndarray]): Model outputs cast to the dtype of the first input. Each element in the list corresponds
                to one of the model's output tensors.

        Examples:
            >>> model = TritonRemoteModel(url="localhost:8000", endpoint="yolov8", scheme="http")
            >>> outputs = model(np.random.rand(1, 3, 640, 640).astype(np.float32))
        r   TYPE_r   c                :    g | ]}                     |          S r"   )r?   )r$   output_namerG   s     r   r%   z.TritonRemoteModel.__call__.<locals>.<listcomp>m   s'    eeeK22;??eeer   )
model_namerK   outputsc                `    g | ]*}                     |                                        +S r"   )as_numpyastype)r$   rR   input_formatrT   s     r   r%   z.TritonRemoteModel.__call__.<locals>.<listcomp>p   s5    hhh{  --44\BBhhhr   )dtype	enumeraterB   rW   r@   rC   shaperA   replaceset_data_from_numpyappendrD   r6   inferr
   )	rG   rK   infer_inputsir   infer_inputinfer_outputsrX   rT   s	   `      @@r   __call__zTritonRemoteModel.__call__U   s'    ayf%% 	- 	-DAqw$/222HHT21566//$*:1*=zz4K]^_K`KhKhiprtKuKuvvK++A...,,,,eeeeSWSdeee$**dmLbo*pphhhhhVZVghhhhr   N)r   r   )r   r	   r
   r	   r   r	   )rK   rL   rM   rN   )__name__
__module____qualname____doc__rJ   rd   r"   r   r   r   r      sW         8+w +w +w +w +wZi i i i i ir   r   )
__future__r   rE   urllib.parser   numpyr;   r   r"   r   r   <module>rl      s    # " " " " " 



 ! ! ! ! ! !    ei ei ei ei ei ei ei ei ei eir   