o
    wÖiŸ  ã                   @   s:   d dl Zd dlmZ d dlT ddlmZ G dd„ dƒZdS )é    N)Úservice_pb2)Ú*é   )Úraise_errorc                   @   sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
dd„ Zdd„ ZdS )Ú
InferInputaS  An object of InferInput class is used to describe
    input tensor for an inference request.

    Parameters
    ----------
    name : str
        The name of input whose data will be described by this object
    shape : list
        The shape of the associated input.
    datatype : str
        The datatype of the associated input.

    c                 C   sB   t  ¡  ¡ | _|| j_| j d¡ | jj |¡ || j_d | _	d S )NÚshape)
r   ÚModelInferRequestÚInferInputTensorÚ_inputÚnameÚ
ClearFieldr   ÚextendÚdatatypeÚ_raw_content)Úselfr   r   r   © r   ú[/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/tritonclient/grpc/_infer_input.pyÚ__init__3   s   
zInferInput.__init__c                 C   ó   | j jS )z†Get the name of input associated with this object.

        Returns
        -------
        str
            The name of input
        )r
   r   ©r   r   r   r   r   ;   ó   zInferInput.namec                 C   r   )zŽGet the datatype of input associated with this object.

        Returns
        -------
        str
            The datatype of input
        )r
   r   r   r   r   r   r   E   r   zInferInput.datatypec                 C   r   )z‰Get the shape of input associated with this object.

        Returns
        -------
        list
            The shape of input
        )r
   r   r   r   r   r   r   O   r   zInferInput.shapec                 C   s   | j  d¡ | j j |¡ | S )zÝSet the shape of input.

        Parameters
        ----------
        shape : list
            The shape of the associated input.

        Returns
        -------
        InferInput
            The updated input
        r   )r
   r   r   r   )r   r   r   r   r   Ú	set_shapeY   s   zInferInput.set_shapec                 C   s¤  t |tjfƒstdƒ | jjdkr(|jt| jjƒkr'td |jt| jjƒ¡ƒ nt	|jƒ}| jj|kr=td || jj¡ƒ d}t
| jjƒt
|jƒkrLd}tt
| jjƒƒD ]}| jj| |j| krcd}qT|s}td t|jƒdd	… t| jjƒdd	… ¡ƒ | jj d
d¡ | jj dd¡ | jj dd¡ | jjdkr°t|ƒ}|jdkr«| ¡ | _| S d| _| S | jjdkrËt|ƒ}|jdkrÆ| ¡ | _| S d| _| S | ¡ | _| S )aª  Set the tensor data from the specified numpy array for
        input associated with this object.

        Parameters
        ----------
        input_tensor : numpy array
            The tensor data in numpy array format

        Returns
        -------
        InferInput
            The updated input

        Raises
        ------
        InferenceServerException
            If failed to set data for the tensor.
        z"input_tensor must be a numpy arrayÚBF16zFgot unexpected datatype {} from numpy array, expected {} for BF16 typez8got unexpected datatype {} from numpy array, expected {}TFz4got unexpected numpy array shape [{}], expected [{}]r   éÿÿÿÿÚshared_memory_regionNÚshared_memory_byte_sizeÚshared_memory_offsetÚBYTESr   ó    )Ú
isinstanceÚnpÚndarrayr   r
   r   ÚdtypeÚtriton_to_np_dtypeÚformatÚnp_to_triton_dtypeÚlenr   ÚrangeÚstrÚ
parametersÚpopÚserialize_byte_tensorÚsizeÚitemr   Úserialize_bf16_tensorÚtobytes)r   Úinput_tensorr"   Úvalid_shapeÚiÚserialized_outputr   r   r   Úset_data_from_numpyj   sb   ÿÿ€
ÿÿ€"ÿÿ

÷	ø

ý
ÿzInferInput.set_data_from_numpyr   c                 C   sH   | j  d¡ d| _|| j jd _|| j jd _|dkr"|| j jd _| S )a  Set the tensor data from the specified shared memory region.

        Parameters
        ----------
        region_name : str
            The name of the shared memory region holding tensor data.
        byte_size : int
            The size of the shared memory region holding tensor data.
        offset : int
            The offset, in bytes, into the region where the data for
            the tensor starts. The default value is 0.

        Returns
        -------
        InferInput
            The updated input
        ÚcontentsNr   r   r   r   )r
   r   r   r)   Ústring_paramÚint64_param)r   Úregion_nameÚ	byte_sizeÚoffsetr   r   r   Úset_shared_memory°   s   zInferInput.set_shared_memoryc                 C   ó   | j S )z±Retrieve the underlying InferInputTensor message.
        Returns
        -------
        protobuf message
            The underlying InferInputTensor protobuf message.
        )r
   r   r   r   r   Ú_get_tensorË   ó   zInferInput._get_tensorc                 C   r<   )z¬Retrieve the contents for this tensor in raw bytes.
        Returns
        -------
        bytes
            The associated contents for this tensor in raw bytes.
        )r   r   r   r   r   Ú_get_contentÔ   r>   zInferInput._get_contentN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r4   r;   r=   r?   r   r   r   r   r   $   s    



F	r   )Únumpyr    Útritonclient.grpcr   Útritonclient.utilsÚ_utilsr   r   r   r   r   r   Ú<module>   s
   