o
    ;ήcv                     @   sb   d dl Z d dlmZmZmZ d dlZd dlZd dlmZ e 	e
ZG dd dZG dd dZdS )    N)DictListUnion)InferenceSessionc                   @   s   e Zd ZedededefddZededefddZedefd	d
ZedefddZ	ede
jfddZedejfddZededeee
jf fddZdS )
TypeHelperort_sessionnamereturnc                 C   :   t |  D ]\}}|j|kr|j  S qtd| d)Nzinput name 
 not found)	enumerate
get_inputsr   type
ValueError)r   r   iinput r   Q/tmp/pip-target-vg8gfxp4/lib/python/onnxruntime/transformers/io_binding_helper.pyget_input_type   s
   

zTypeHelper.get_input_typec                 C   r
   )Nzoutput name r   )r   get_outputsr   r   r   )r   r   r   outputr   r   r   get_output_type   s
   

zTypeHelper.get_output_typeort_typec                 C   8   t jt jt jt jt jd}| |vrt|  d||  S N)ztensor(int64)ztensor(int32)ztensor(float)ztensor(float16)ztensor(bool) not found in map)numpylonglongintcfloat32float16boolr   )r   ort_type_to_numpy_type_mapr   r   r   ort_type_to_numpy_type      z!TypeHelper.ort_type_to_numpy_typec                 C   r   r   )torchint64int32r   r    r!   r   )r   ort_type_to_torch_type_mapr   r   r   ort_type_to_torch_type*   r$   z!TypeHelper.ort_type_to_torch_type
numpy_typec                 C   sR   t jtjt jtjt jtjt jtjt jtjt jtji}| |vr%t	|  d||  S Nr   )
r   r   r%   r&   r   r'   r   r    r!   r   )r*   numpy_type_to_torch_type_mapr   r   r   numpy_type_to_torch_type8   s   z#TypeHelper.numpy_type_to_torch_type
torch_typec              
   C   sJ   t jtjt jtjt jtjt jtjt jtji}| |vr!t	|  d||  S r+   )
r%   r&   r   r   r'   r   r   r    r!   r   )r.   torch_type_to_numpy_type_mapr   r   r   torch_type_to_numpy_typeG   s   z#TypeHelper.torch_type_to_numpy_typec                 C   sH   i }|   D ]}t|j||j< q|  D ]}t|j||j< q|S )z:Create a mapping from input/output name to numpy data type)r   r   r#   r   r   r   )r   name_to_numpy_typer   r   r   r   r   get_io_numpy_type_mapU   s   z TypeHelper.get_io_numpy_type_mapN)__name__
__module____qualname__staticmethodr   strr   r   r#   r)   r   dtyper-   r%   r0   r   r2   r   r   r   r   r      s    "r   c                   @   sZ   e Zd ZedefddZe	ddejdejdejdeej fd	d
Z	edddZ
dS )IOBindingHelperr   c                 C   sJ   i }|  D ]\}}t| |}t|}tjt|||d||< q|S )zpReturns a dictionary of output name as key, and 1D tensor as value. The tensor has enough space for given shape.)r8   device)itemsr   r   r)   r%   emptyr   prod)r   output_shapesr:   output_buffersr   shaper   r.   r   r   r   get_output_buffersb   s   
z"IOBindingHelper.get_output_buffersN	input_idsposition_idsattention_maskpastc              
   C   s  |du r	t | }|  }| sJ |d|jjd|d t| |	  |dur^t
|D ].\}	}
|
 s9J |
	 }|dkrE|	 }|d|	 |
jjd|d|	  t|
 | q/|dur|| shJ |d|jjd|d t| |	  |dur| sJ |d|jjd|d t| |	  |  D ].}|j}|| }t| d|jj dt|   |||jjd|| || |	  q|S )	z)Returnas IO binding object for a session.NrB   r   past_rD   rC   z device type=z shape=)r   r2   
io_bindingis_contiguous
bind_inputr:   r   listsizedata_ptrr   r   r   loggerdebugbind_output)r   rB   rC   rD   rE   r?   r>   name_to_np_typerG   r   past_irL   r   output_nameoutput_bufferr   r   r   prepare_io_bindingl   sv   

	
	
	

&	z"IOBindingHelper.prepare_io_bindingTc           
      C   sn   g }|   D ].}|j}|| }|| }|dt| |  }	|r/||	   q||	 q|S )z3Copy results to cpu. Returns a list of numpy array.r   )	r   r   r   r=   reshapeclonedetachappendcpu)
r   r?   r>   return_numpyort_outputsr   rR   bufferr@   copy_tensorr   r   r   "get_outputs_from_io_binding_buffer   s    z2IOBindingHelper.get_outputs_from_io_binding_buffer)N)T)r3   r4   r5   r6   r   rA   r%   Tensorr   rT   r^   r   r   r   r   r9   a   s     		Ur9   )loggingtypingr   r   r   r   r%   onnxruntimer   	getLoggerr3   rM   r   r9   r   r   r   r   <module>   s    
U