§
    l¬¸j_  ã                  óH   — d dl mZ d dlZd dlmZ d dlZ G d„ d¦  «        ZdS )é    )ÚannotationsN)Úurlsplitc                  ó$   — e Zd ZdZddd„Z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. 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ÚstrÚendpointÚschemec                ó  ‡— |sS|sQt          |¦  «        }|j                             d¦  «                             dd¦  «        d         }|j        }|j        }|| _        || _        |dk    rCddlm	} | 
                    | j        dd¬¦  «        | _        | j                             |¦  «        }nJddlm} | 
                    | j        dd¬¦  «        | _        | j                             |d¬	¦  «        d
         }t          |d         d„ ¬¦  «        |d<   t           j        t           j        t           j        dœŠ|j        | _        |j        | _        d„ |d         D ¦   «         | _        ˆfd„| j        D ¦   «         | _        d„ |d         D ¦   «         | _        d„ |d         D ¦   «         | _        t5          j        |                     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, optional): The name of the model on the Triton server.
            scheme (str, optional): The communication scheme ('http' or 'grpc').
        ú/é   r   ÚhttpNF)r   ÚverboseÚsslT)Úas_jsonÚconfigÚoutputc                ó,   — |                       d¦  «        S )NÚname)Úget)Úxs    úh/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/utils/triton.pyú<lambda>z,TritonRemoteModel.__init__.<locals>.<lambda>I   s   € À!Ç%Â%ÈÁ-Ä-€ ó    )Úkey)Ú	TYPE_FP32Ú	TYPE_FP16Ú
TYPE_UINT8c                ó   — g | ]
}|d          ‘ŒS )Ú	data_type© ©Ú.0r   s     r   ú
<listcomp>z.TritonRemoteModel.__init__.<locals>.<listcomp>O   s   € ÐFÐFÐF°˜a œnÐFÐFÐFr   Úinputc                ó    •— g | ]
}‰|         ‘ŒS r"   r"   )r$   r   Útype_maps     €r   r%   z.TritonRemoteModel.__init__.<locals>.<listcomp>P   s   ø€ Ð IÐ IÐ I° ¨!¤Ð IÐ IÐ Ir   c                ó   — g | ]
}|d          ‘ŒS ©r   r"   r#   s     r   r%   z.TritonRemoteModel.__init__.<locals>.<listcomp>Q   s   € Ð?Ð?Ð?¨!˜A˜fœIÐ?Ð?Ð?r   c                ó   — g | ]
}|d          ‘ŒS r*   r"   r#   s     r   r%   z.TritonRemoteModel.__init__.<locals>.<listcomp>R   s   € ÐAÐAÐA¨1˜Q˜vœYÐAÐAÐAr   Ú
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ÚastÚliteral_evalr   r-   )Úselfr   r
   r   ÚsplitsÚclientr   r(   s          @r   Ú__init__zTritonRemoteModel.__init__(   s  ø€ ð ð 	  ð 	 Ý˜c‘]”]ˆFØ”{×(Ò(¨Ñ-Ô-×3Ò3°C¸Ñ;Ô;¸AÔ>ˆHØ”]ˆFØ”-ˆCà ˆŒØˆŒð �VÒÐØ.Ð.Ð.Ð.Ð.Ð.à!'×!=Ò!=À$Ä(ÐTYÐ_dÐ!=Ñ!eÔ!eˆDÔØÔ'×8Ò8¸ÑBÔBˆFˆFà.Ð.Ð.Ð.Ð.Ð.à!'×!=Ò!=À$Ä(ÐTYÐ_dÐ!=Ñ!eÔ!eˆDÔØÔ'×8Ò8¸È4Ð8ÑPÔPÐQYÔZˆFõ " &¨Ô"2Ð8OÐ8OÐPÑPÔPˆˆxÑõ "$¤½"¼*ÕTVÔT\Ð]Ð]ˆØ$*Ô$?ˆÔ!Ø Ô+ˆŒØFÐF°f¸W´oÐFÑFÔFˆÔØ IÐ IÐ IÐ I°dÔ6HÐ IÑ IÔ IˆÔØ?Ð?¨v°g¬Ð?Ñ?Ô?ˆÔØAÐA°°xÔ0@ÐAÑAÔAˆÔÝÔ(¨¯ª°LÀ"Ñ)EÔ)E×)IÒ)IÈ*ÐVXÑ)YÔ)Y×)]Ò)]Ð^lÐntÑ)uÔ)uÑvÔvˆŒˆˆr   Úinputsú
np.ndarrayÚreturnúlist[np.ndarray]c           	     ó2  ‡ ‡‡— g }|d         j         Št          |¦  «        D ]­\  }}|j         ‰ j        |         k    r |                     ‰ j        |         ¦  «        }‰                      ‰ j        |         g |j        ¢‰ j        |                              dd¦  «        ¦  «        }| 	                    |¦  «         | 
                    |¦  «         Œ®ˆ fd„‰ j        D ¦   «         }‰ j                             ‰ j        ||¬¦  «        Šˆˆfd„‰ j        D ¦   «         S )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 cast to the dtype of the first 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                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r"   )r?   )r$   Úoutput_namerG   s     €r   r%   z.TritonRemoteModel.__call__.<locals>.<listcomp>m   s'   ø€ ÐeÐeÐeÀK˜×2Ò2°;Ñ?Ô?ÐeÐeÐer   )Ú
model_namerK   Úoutputsc                ó`   •— g | ]*}‰                      |¦  «                             ‰¦  «        ‘Œ+S r"   )Úas_numpyÚastype)r$   rR   Úinput_formatrT   s     €€r   r%   z.TritonRemoteModel.__call__.<locals>.<listcomp>p   s5   ø€ ÐhÐhÐhÀ{�× Ò  Ñ-Ô-×4Ò4°\ÑBÔBÐhÐhÐhr   )ÚdtypeÚ	enumeraterB   rW   r@   rC   ÚshaperA   ÚreplaceÚset_data_from_numpyÚappendrD   r6   Úinferr
   )	rG   rK   Úinfer_inputsÚir   Úinfer_inputÚinfer_outputsrX   rT   s	   `      @@r   Ú__call__zTritonRemoteModel.__call__U   s'  øøø€ ð ˆØ˜a”y”ˆÝ˜fÑ%Ô%ð 	-ð 	-‰DˆAˆqØŒw˜$Ô/°Ô2Ò2Ð2Ø—H’H˜TÔ2°1Ô5Ñ6Ô6�ØŸ/š/¨$Ô*:¸1Ô*=¸zÀÄ¸zÈ4ÔK]Ð^_ÔK`×KhÒKhÐipÐrtÑKuÔKuÑvÔvˆKØ×+Ò+¨AÑ.Ô.Ð.Ø×Ò Ñ,Ô,Ð,Ð,àeÐeÐeÐeÐSWÔSdÐeÑeÔeˆØÔ$×*Ò*°d´mÈLÐboÐ*ÑpÔpˆàhÐhÐhÐhÐhÐVZÔVgÐhÑhÔhÐhr   N)r   r   )r   r	   r
   r	   r   r	   )rK   rL   rM   rN   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rJ   rd   r"   r   r   r   r      sW   € € € € € ðð ð8+wð +wð +wð +wð +wðZið ið ið ið ið ir   r   )Ú
__future__r   rE   Úurllib.parser   Únumpyr;   r   r"   r   r   ú<module>rl      s†   ðð #Ð "Ð "Ð "Ð "Ð "à 
€
€
€
Ø !Ð !Ð !Ð !Ð !Ð !à Ð Ð Ð ðeið eið eið eið eiñ eiô eið eið eið eir   