
    h.                    8    S SK Jr  S SKJr  S SKr " S S5      rg)    )annotations)urlsplitNc                  0    \ rS rSrSrSSS jjrS	S jrSrg)
TritonRemoteModel
   a  
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))
c                T   U(       dX  U(       dQ  [        U5      nUR                  R                  S5      R                  SS5      S   nUR                  nUR
                  nX l        Xl        US:X  aB  SSKJ	n  UR                  U R                  SSS9U l        U R                  R                  U5      nOCSSKJn  UR                  U R                  SSS9U l        U R                  R                  USS	9S
   n[        US   S S9US'   [         R"                  [         R$                  [         R&                  S.nUR(                  U l        UR*                  U l        US    Vs/ s H  oS   PM	     snU l        U R,                   Vs/ s H  oU   PM	     snU l        US    Vs/ s H  oS   PM	     snU l        US    Vs/ s H  oS   PM	     snU l        [5        UR7                  S0 5      R7                  S0 5      R7                  SS5      5      U l        gs  snf s  snf s  snf s  snf )aU  
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').

Examples:
    >>> model = TritonRemoteModel(url="localhost:8000", endpoint="yolov8", scheme="http")
    >>> model = TritonRemoteModel(url="http://localhost:8000/yolov8")
/   r   httpNF)urlverbosesslT)as_jsonconfigoutputc                $    U R                  S5      $ )Nname)get)xs    R/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/utils/triton.py<lambda>,TritonRemoteModel.__init__.<locals>.<lambda>N   s    !%%-    )key)	TYPE_FP32	TYPE_FP16
TYPE_UINT8input	data_typer   
parametersmetadatastring_valueNone)r   pathstripsplitschemenetlocendpointr   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evalr   r!   )	selfr   r)   r'   splitsclientr   type_mapr   s	            r   __init__TritonRemoteModel.__init__(   s     c]F{{((-33C;A>H]]F--C  V.!'!=!=$((TY_d!=!eD''88BF.!'!=!=$((TY_d!=!eD''8848PQYZF "&"28OPx "$"**TVT\T\]$*$?$?! ++6<WoFonoF6:6H6H I6H!6H I/5g?!fI?06x0@A0@1vY0@AVZZb9==j"MQQR`bhij	 G I?As   !HH"H ?H%c           	        / nUS   R                   n[        U5       H  u  pEUR                   U R                  U   :w  a  UR                  U R                  U   5      nU R	                  U R
                  U   / UR                  QU R                  U   R                  SS5      5      nUR                  U5        UR                  U5        M     U R                   Vs/ s H  opR                  U5      PM     nnU R                  R                  U R                  X(S9n	U R                   Vs/ s H"  oyR!                  U5      R                  U5      PM$     sn$ s  snf s  snf )a2  
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 with the same dtype as the 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_ )
model_nameinputsoutputs)dtype	enumerater8   astyper6   r9   shaper7   replaceset_data_from_numpyappendr:   r5   r,   inferr)   as_numpy)
r<   rF   infer_inputsinput_formatir   infer_inputoutput_nameinfer_outputsrG   s
             r   __call__TritonRemoteModel.__call__Z   s8     ayf%DAww$//22HHT22156//$*:*:1*=zz4K]K]^_K`KhKhiprtKuvK++A., & TXSdSdeSdK22;?Sde$$**dmmL*pVZVgVghVg{  -44\BVghh f is   E()E)
r6   r5   r)   r7   r9   r!   r8   r:   r,   r   N)rD   rD   )r   strr)   rY   r'   rY   )rF   z
np.ndarrayreturnzlist[np.ndarray])__name__
__module____qualname____firstlineno____doc__r@   rW   __static_attributes__ r   r   r   r   
   s    :0kdir   r   )
__future__r   urllib.parser   numpyr1   r   ra   r   r   <module>re      s    # ! li lir   