o
    Wh                     @   s2   d dl mZ d dlmZ d dlZG dd dZdS )    )List)urlsplitNc                   @   sB   e Zd ZdZddededefddZdejd	eej f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.

    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                    sZ  |s|st |}|jddd }|j}|j}|| _|| _|dkr:ddlm	} |j
| jddd| _| j|}nddlm} |j
| jddd| _| jj|ddd	 }t|d
 dd d|d
< tjtjtjd |j| _|j| _dd |d D | _ fdd| jD | _dd |d D | _dd |d
 D | _t|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): The name of the model on the Triton server.
            scheme (str): 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                 S   s
   |  dS )Nname)get)x r   L/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/utils/triton.py<lambda>L   s   
 z,TritonRemoteModel.__init__.<locals>.<lambda>)key)	TYPE_FP32	TYPE_FP16
TYPE_UINT8c                 S      g | ]}|d  qS )	data_typer   .0r   r   r   r   
<listcomp>R       z.TritonRemoteModel.__init__.<locals>.<listcomp>inputc                    s   g | ]} | qS r   r   r   type_mapr   r   r   S   r   c                 S   r   r   r   r   r   r   r   r   T   r   c                 S   r   r#   r   r   r   r   r   r   U   r   
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   r   r!   r   __init__&   s.   &zTritonRemoteModel.__init__inputsreturnc              	      s   g }|d j  t|D ]2\}}|j j| kr|j| }j| g |jj| dd}|	| |
| qfddjD }jjj||d fddjD S )am  
        Call the model with the given inputs.

        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_r   c                    s   g | ]}  |qS r   )r7   r   output_name)r>   r   r   r   q   s    z.TritonRemoteModel.__call__.<locals>.<listcomp>)
model_namerB   outputsc                    s   g | ]
} | qS r   )as_numpyastyperE   )input_formatrH   r   r   r   t   s    )dtype	enumerater:   rJ   r8   r;   shaper9   replaceset_data_from_numpyappendr<   r.   inferr   )r>   rB   infer_inputsir   infer_inputinfer_outputsr   )rK   rH   r>   r   __call__X   s   
(
zTritonRemoteModel.__call__N)r   r   )
__name__
__module____qualname____doc__strrA   r3   ndarrayr   rW   r   r   r   r   r   	   s    2r   )typingr   urllib.parser   numpyr3   r   r   r   r   r   <module>   s   