a
    yf`                     @   s2   d dl mZ d dlmZ d dlZG dd dZdS )    )List)urlsplitNc                   @   s<   e Zd ZdZd
eeedddZejeej dddZ	d	S )TritonRemoteModela  
    Client for interacting with a remote Triton Inference Server model.

    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.
     )urlendpointschemec                    s8  |s2|s2t |}|jddd }|j}|j}|| _|| _|dkrtddlm	} |j
| jddd| _| j|}n4d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 | _dS )a  
        Initialize the TritonRemoteModel.

        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').
        /r   httpNF)r   verbosesslT)Zas_jsonconfigoutputc                 S   s
   |  dS )Nname)get)x r   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/triton.py<lambda>;       z,TritonRemoteModel.__init__.<locals>.<lambda>)key)Z	TYPE_FP32Z	TYPE_FP16Z
TYPE_UINT8c                 S   s   g | ]}|d  qS )Z	data_typer   .0r   r   r   r   
<listcomp>A   r   z.TritonRemoteModel.__init__.<locals>.<listcomp>inputc                    s   g | ]} | qS r   r   r   Ztype_mapr   r   r   B   r   c                 S   s   g | ]}|d  qS r   r   r   r   r   r   r   C   r   c                 S   s   g | ]}|d  qS r   r   r   r   r   r   r   D   r   )r   pathstripsplitr   netlocr   r   Ztritonclient.httpr
   ZInferenceServerClienttriton_clientZget_model_configZtritonclient.grpcZgrpcsortednpZfloat32Zfloat16Zuint8InferRequestedOutput
InferInputinput_formatsnp_input_formatsinput_namesoutput_names)selfr   r   r   Zsplitsclientr   r   r   r   __init__   s,    zTritonRemoteModel.__init__)inputsreturnc              	      s   g }|d j  t|D ]d\}}|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 )z
        Call the model with the given inputs.

        Args:
            *inputs (List[np.ndarray]): Input data to the model.

        Returns:
            (List[np.ndarray]): Model outputs.
        r   ZTYPE_r   c                    s   g | ]}  |qS r   )r$   r   Zoutput_name)r*   r   r   r   Y   r   z.TritonRemoteModel.__call__.<locals>.<listcomp>)Z
model_namer-   outputsc                    s   g | ]} | qS r   )Zas_numpyastyper/   )input_formatr0   r   r   r   \   r   )Zdtype	enumerater'   r1   r%   r(   shaper&   replaceZset_data_from_numpyappendr)   r!   Zinferr   )r*   r-   Zinfer_inputsir   Zinfer_inputZinfer_outputsr   )r2   r0   r*   r   __call__F   s    

(
zTritonRemoteModel.__call__N)r   r   )
__name__
__module____qualname____doc__strr,   r#   Zndarrayr   r8   r   r   r   r   r   	   s   -r   )typingr   urllib.parser   numpyr#   r   r   r   r   r   <module>   s   