
    |h"                     8    d dl mZ d dlmZ d dlZ G d d      Zy)    )List)urlsplitNc                   d    e Zd ZdZd
dededefdZdej                  deej                     fdZ	y	)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endpointschemec                 J   |sS|sQt        |      }|j                  j                  d      j                  dd      d   }|j                  }|j
                  }|| _        || _        |dk(  rEddlm	} |j                  | j                  dd      | _        | j                  j                  |      }nIddlm} |j                  | j                  dd      | _        | j                  j                  |d	      d
   }t        |d   d       |d<   t         j"                  t         j$                  t         j&                  d}|j(                  | _        |j*                  | _        |d   D cg c]  }|d   	 c}| _        | j,                  D cg c]  }||   	 c}| _        |d   D cg c]  }|d   	 c}| _        |d   D cg c]  }|d   	 c}| _        t5        |j7                  di       j7                  di       j7                  dd            | _        yc c}w c c}w c c}w c c}w )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').

        Examples:
            >>> model = TritonRemoteModel(url="localhost:8000", endpoint="yolov8", scheme="http")
            >>> model = TritonRemoteModel(url="http://localhost:8000/yolov8")
        /   r   httpNF)r   verbosesslT)as_jsonconfigoutputc                 $    | j                  d      S )Nname)get)xs    W/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/utils/triton.py<lambda>z,TritonRemoteModel.__init__.<locals>.<lambda>M   s    !%%-     )key)	TYPE_FP32	TYPE_FP16
TYPE_UINT8input	data_typer   
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evalr   r!   )	selfr   r   r	   splitsclientr   type_mapr   s	            r   __init__z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FanF6:6H6H I! I/5g?!AfI?06x0@A1QvYAVZZb9==j"MQQR`bhij	 G I?As   $HH!H<H inputsreturnc           	         g }|d   j                   }t        |      D ]  \  }}|j                   | j                  |   k7  r|j                  | j                  |         }| j	                  | j
                  |   g |j                  | j                  |   j                  dd            }|j                  |       |j                  |        | j                  D cg c]  }| j                  |       }}| j                  j                  | j                  ||      }	| j                  D cg c]"  }|	j!                  |      j                  |      $ c}S c c}w c c}w )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 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_namer?   outputs)dtype	enumerater6   astyper4   r7   shaper5   replaceset_data_from_numpyappendr8   r3   r*   inferr   as_numpy)
r:   r?   infer_inputsinput_formatir   infer_inputoutput_nameinfer_outputsrE   s
             r   __call__zTritonRemoteModel.__call__Y   s=     ayf% 	-DAqww$//22HHT22156//$*:*:1*=zz4K]K]^_K`KhKhiprtKuvK++A.,	- TXSdSdeK22;?ee$$**dmmLbo*pVZVgVgh{  -44\Bhh f is   E('EN)rC   rC   )
__name__
__module____qualname____doc__strr>   r/   ndarrayr   rU    r   r   r   r   	   sI    :0kC 0k3 0kS 0kdi

 itBJJ/? ir   r   )typingr   urllib.parser   numpyr/   r   r\   r   r   <module>r`      s     ! li lir   