o
    8wiE                     @  sJ  U d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlZd dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d d
lmZ G dd deZeeZded< eddd d7ddZeG dd dZeG dd dZ ejG dd dZ!d8d!d"Z"d9d*d+Z#d:d-d.Z$d;d1d2Z%d<d5d6Z&dS )=    )annotationsN)defaultdict)EnumSimpleNamespace)Any)define)grpc)http)aio)model_config_pb2c                   @  s   e Zd ZdZdZdS )TritonProtocolr	   r
   N)__name__
__module____qualname__r	   r
    r   r   L/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/tritony/helpers.pyr      s    r   zdict[str, int]COMPRESSION_ALGORITHM_MAP      )deflategzipobjdict[str, Any]returnr   c                 C  s   t jt | dd dS )z|
    Convert dict to attr like object as SimpleNamespace
    Only for grpc client's output
    :param obj:
    :return:
    c                 S  s   t di | S )Nr   r   )dr   r   r   <lambda>#   s    zdict_to_attr.<locals>.<lambda>)object_hook)jsonloadsdumps)r   r   r   r   dict_to_attr   s   r!   c                   @  sF   e Zd ZU dZded< ded< dZded< g Zded	< d
Zded< dS )TritonModelInputz
    Most of the fields are mapped to model_config_pb2.ModelInput(https://github.com/triton-inference-server/common/blob/a2de06f4c80b2c7b15469fa4d36e5f6445382bad/protobuf/model_config.proto#L317)

    Commented fields are not used.
    strnamedtyper   intformatz	list[int]dimsFbooloptionalN)r   r   r   __doc____annotations__r'   r(   r*   r   r   r   r   r"   &   s   
 r"   c                   @  s:   e Zd ZU ded< ded< ded< ded< d	Zded
< dS )TritonModelSpecr#   r$   r&   max_batch_sizezlist[TritonModelInput]model_inputz	list[str]output_name1model_versionN)r   r   r   r,   r2   r   r   r   r   r-   :   s   
 r-   c                   @  s   e Zd ZU dZded< ded< dZded< ejeej	dZ
ded	< d
Zded< dZded< dZded< d
Zded< dZded< dZded< d
Zded< d
Zded< dS )TritonClientFlagzp
    run_async=True,
    concurrency=6,
    streaming=False,
    compression_algorithm=None,
    ssl=False,
    r#   url
model_namer1   r2   )	converterdefaultr   protocolFr)   	streamingT	async_set   r&   concurrencyverboser   
input_dimsNz
str | Nonecompression_algorithmssluse_aio_tritonclient)r   r   r   r+   r,   r2   attrsfieldr   r	   r8   r9   r:   r<   r=   r>   r?   r@   rA   r   r   r   r   r3   F   s   
 r3   flaggrpcclient.InferenceServerClient | httpclient.InferenceServerClient | aio_grpcclient.InferenceServerClient | aio_httpclient.InferenceServerClientc                 C  s   | j r| jtjurJ d| js7| jtju r#tj| j| j| j	d}|S | j
r)| jnd}tj| j| j|d}|S | jtju rJtj| j| j| j	d}|S tj| j| jd}|S )Nz,Streaming is only allowed with gRPC protocol)r4   r=   r@   r   )r4   r=   r<   )r4   r=   )r9   r8   r   r	   rA   
grpcclientInferenceServerClientr4   r=   r@   r:   r<   
httpclientaio_grpcclientaio_httpclient)rD   triton_clientr<   r   r   r   init_triton_client_   s&   	
	rL   rK   Cgrpcclient.InferenceServerClient | httpclient.InferenceServerClientr5   r#   r2   r8   -tuple[int, list[TritonModelInput], list[str]]c           	      C  sX   t ||d}| jdi |}|tju rt|}n|tju r |j}t|\}}}|||fS )z
    (required in)
    :param triton_client:
    :param flag:
    - protocol
    - streaming
    - async_set
    - model_name
    - model_version

    :return:
    r5   r2   Nr   dictget_model_configr   r
   r!   r	   configparse_model	rK   r5   r2   r8   argsmodel_configr.   
input_listoutput_name_listr   r   r   get_triton_client   s   



rZ   Kaio_grpcclient.InferenceServerClient | aio_httpclient.InferenceServerClientc           	        s`   t ||d}| jdi |I d H }|tju rt|}n|tju r$|j}t|\}}}|||fS )NrO   r   rP   rU   r   r   r   async_get_triton_client   s   



r\   r/   -model_config_pb2.ModelInput | SimpleNamespacec                 C  sJ   | j }t| j trtj|}|d}|dkrd}t| j| j	|| j
dS )zm
    https://github.com/triton-inference-server/common/blob/r23.08/protobuf/model_config.proto#L317-L412
    TYPE_STRINGBYTES)r$   r(   r%   r*   )	data_type
isinstancer&   r   DataTypeNamestripr"   r$   r(   r*   )r/   	RAW_DTYPEr   r   r   parse_model_input   s   
rg   rW   .model_config_pb2.ModelConfig | SimpleNamespacec                 C  s$   | j dd | jD dd | jD fS )Nc                 S  s   g | ]}t |qS r   )rg   ).0model_config_inputr   r   r   
<listcomp>   s    zparse_model.<locals>.<listcomp>c                 S  s   g | ]}|j qS r   )r$   )ri   model_config_outputr   r   r   rk      s    )r.   inputoutput)rW   r   r   r   rT      s   rT   )r   r   r   r   )rD   r3   r   rE   )
rK   rM   r5   r#   r2   r#   r8   r   r   rN   )
rK   r[   r5   r#   r2   r#   r8   r   r   rN   )r/   r]   r   r"   )rW   rh   r   rN   )'
__future__r   r   collectionsr   enumr   typesr   typingr   rB   r   tritonclientr	   rF   r
   rH   tritonclient.grpcr   rI   r   tritonclient.grpc.aiotritonclient.httprJ   r   r&   r   r,   updater!   r"   r-   r3   rL   rZ   r\   rg   rT   r   r   r   r   <module>   s:    



 
 
