o
    Ďi2                     @   s2   d Z ddlZddlmZ ddlZG dd dZdS )z4 Utils to interact with the Triton Inference Server
    N)urlparsec                   @   sX   e Zd ZdZdefddZedd Zdej	e
jeje
jdf f fd	d
Zdd ZdS )TritonRemoteModelz A wrapper over a model served by the Triton Inference Server. It can
    be configured to communicate over GRPC or HTTP. It accepts Torch Tensors
    as input and returns them as outputs.
    urlc                    s   t |}|jdkr;ddlm}m  ||j_j }|jd j	_
jjj
dd_dtj  f fdd}n/ddlm}m  ||j_j }|d d	 _
jj
_dtj  f fd
d}|_dS )z
        Keyword arguments:
        url: Fully qualified address of the Triton server - for e.g. grpc://localhost:8000
        grpcr   )InferenceServerClient
InferInputT)as_jsonreturnc                          fddj d D S )Nc                    .   g | ]} |d  dd |d D |d qS )namec                 S      g | ]}t |qS  int.0sr   r   O/home/jeff/fluffinator/venv/lib/python3.10/site-packages/yolov5/utils/triton.py
<listcomp>"       \TritonRemoteModel.__init__.<locals>.create_input_placeholders.<locals>.<listcomp>.<listcomp>shapedatatyper   r   ir   r   r   r   !       "QTritonRemoteModel.__init__.<locals>.create_input_placeholders.<locals>.<listcomp>inputsmetadatar   r   selfr   r   create_input_placeholders       
z=TritonRemoteModel.__init__.<locals>.create_input_placeholdersr   c                      r
   )Nc                    r   )r   c                 S   r   r   r   r   r   r   r   r   .   r   r   r   r   r   r   r   r   r   r   -   r   r   r   r    r   r"   r   r   r$   ,   r%   N)r   schemetritonclient.grpcr   r   netlocclientget_model_repository_indexmodelsr   
model_nameget_model_metadatar!   typingListtritonclient.http_create_input_placeholders_fn)r#   r   
parsed_urlr   model_repositoryr$   r   r"   r   __init__   s   



zTritonRemoteModel.__init__c                 C   s   | j d| j dS )zReturns the model runtimebackendplatform)r!   get)r#   r   r   r   runtime2   s   zTritonRemoteModel.runtimer	   .c                 O   sl   | j |i |}| jj| j|d}g }| jd D ]}t||d }|| qt	|dkr4|d S |S )z Invokes the model. Parameters can be provided via args or kwargs.
        args, if provided, are assumed to match the order of inputs of the model.
        kwargs are matched with the model input names.
        )r,   r   outputsr      r   )
_create_inputsr)   inferr,   r!   torch	as_tensoras_numpyappendlen)r#   argskwargsr   responseresultoutputtensorr   r   r   __call__7   s   zTritonRemoteModel.__call__c                 O   s   t |t |}}|s|std|r|rtd|  }|rG|t |kr2tdt | d| dt||D ]\}}||   q7|S |D ]}||j }||   qI|S )NzNo inputs provided.z/Cannot specify args and kwargs at the same timez	Expected z inputs, got .)rA   RuntimeErrorr1   zipset_data_from_numpycpunumpyr   )r#   rB   rC   args_len
kwargs_lenplaceholdersinputvaluer   r   r   r;   D   s    
z TritonRemoteModel._create_inputsN)__name__
__module____qualname____doc__strr4   propertyr8   r.   Unionr=   TensorTuplerH   r;   r   r   r   r   r      s    !
&r   )rW   r.   urllib.parser   r=   r   r   r   r   r   <module>   s
   