o
    eM&                     @   s6   d dl Zd dlmZmZmZmZmZ G dd dZdS )    N)np_to_triton_dtyperaise_errorserialize_bf16_tensorserialize_byte_tensortriton_to_np_dtypec                   @   s\   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dddZ
dd Zdd ZdS )
InferInputaR  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   s(   || _ || _|| _i | _d | _d | _d S )N)_name_shape	_datatype_parameters_data	_raw_data)selfnameshapedatatype r   ND:\Projects\ConvertPro\env\Lib\site-packages\tritonclient/http/_infer_input.py__init__4   s   
zInferInput.__init__c                 C      | j S )zGet the name of input associated with this object.

        Returns
        -------
        str
            The name of input
        )r   r   r   r   r   r   <      zInferInput.namec                 C   r   )zGet the datatype of input associated with this object.

        Returns
        -------
        str
            The datatype of input
        )r
   r   r   r   r   r   F   r   zInferInput.datatypec                 C   r   )zGet the shape of input associated with this object.

        Returns
        -------
        list
            The shape of input
        r	   r   r   r   r   r   P   r   zInferInput.shapec                 C   s
   || _ | S )zSet the shape of input.

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

        Returns
        -------
        InferInput
            The updated input
        r   )r   r   r   r   r   	set_shapeZ   s   zInferInput.set_shapeTc                 C   s  t |tjfstd | jdkr%|jt| jkr$td|jt| j nt|j}| j|kr8td|| j d}t	| j
t	|jkrGd}ntt	| j
D ]}| j
| |j| kr\d}qN|sutdt|jdd	 t| j
dd	  | jd
d | jdd | jdd |s| jdd d| _| jdkrtd | jdkrg | _zI|jdkrtj|dgddD ]7}|jtjkrt| tkr| jt| dd q| jt|  q| jt| dd qW | S W | S  ty   td|  d Y | S w dd | D | _| S d| _| jdkr1t|}|jdkr-| | _n#d| _n| jdkrKt|}|jdkrG| | _n	d| _n| | _t	| j| jd< | 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
        binary_data : bool
            Indicates whether to set data for the input in binary format
            or explicit tensor within JSON. The default value is True,
            which means the data will be delivered as binary data in the
            HTTP body after the JSON object.

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

        Raises
        ------
        InferenceServerException
            If failed to set data for the tensor.
        z"input_tensor must be a numpy arrayZBF16zFgot unexpected datatype {} from numpy array, expected {} for BF16 typez8got unexpected datatype {} from numpy array, expected {}TFz4got unexpected numpy array shape [{}], expected [{}]   shared_memory_regionNshared_memory_byte_sizeshared_memory_offsetbinary_data_sizezNBF16 inputs must be sent as binary data over HTTP. Please set binary_data=TrueZBYTESr   Zrefs_okC)flagsorderzutf-8)encodingzFailed to encode "zM" using UTF-8. Please use binary_data=True, if you want to pass a byte array.c                 S   s   g | ]}|  qS r   )item).0valr   r   r   
<listcomp>   s    z2InferInput.set_data_from_numpy.<locals>.<listcomp>    )
isinstancenpZndarrayr   r
   dtyper   formatr   lenr	   r   rangestrr   popr   r   sizeZnditerZobject_typer$   bytesappendUnicodeDecodeErrorflattenr   r   tobytes)r   Zinput_tensorZbinary_datar+   Zvalid_shapeiobjZserialized_outputr   r   r   set_data_from_numpyj   s   


 


&%
zInferInput.set_data_from_numpyr   c                 C   sD   d| _ d| _| jdd || jd< || jd< |dkr || 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
        Nr   r   r   r   r   )r   r   r   r0   )r   Zregion_nameZ	byte_sizeoffsetr   r   r   set_shared_memory   s   


zInferInput.set_shared_memoryc                 C   r   )zReturns the raw binary data if available

        Returns
        -------
        bytes
            The raw data for the input tensor
        )r   r   r   r   r   _get_binary_data   r   zInferInput._get_binary_datac                 C   sT   | j | j| jd}| jr| j|d< | jddu r(| jdu r(| jdur(| j|d< |S )zRetrieve the underlying input as json dict.

        Returns
        -------
        dict
            The underlying tensor specification as dict
        )r   r   r   
parametersr   Ndata)r   r	   r
   r   getr   r   )r   Ztensorr   r   r   _get_tensor   s   



zInferInput._get_tensorN)T)r   )__name__
__module____qualname____doc__r   r   r   r   r   r:   r<   r=   rA   r   r   r   r   r   &   s    




n
r   )	numpyr*   Ztritonclient.utilsr   r   r   r   r   r   r   r   r   r   <module>   s   	