
    hC                         S SK r S SKrS SKJr  S SKJr  S SKJr  S SKrS SK	r	S SK
JrJr  \\\\\   -  4   r\R$                  " \5      r " S S5      r " S S	5      r " S
 S5      r " S S\5      r " S S5      rg)    N)OrderedDict)Mapping)Any)InferenceSession
RunOptionsc                      \ rS rSr\S\S\S\4S j5       r\S\S\4S j5       r\S\4S j5       r	\S\4S	 j5       r
\S
\R                  4S j5       r\S\R                  4S j5       r\S\S\\\R                  4   4S j5       rSrg)
TypeHelper   ort_sessionnamereturnc                     [        U R                  5       5       H#  u  p#UR                  U:X  d  M  UR                  s  $    [	        SU S35      e)Nzinput name 
 not found)	enumerate
get_inputsr   type
ValueError)r   r   _iinputs       d/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/io_binding_helper.pyget_input_typeTypeHelper.get_input_type   sH    ";#9#9#;<IBzzT!zz! = ;tfJ788    c                     [        U R                  5       5       H#  u  p#UR                  U:X  d  M  UR                  s  $    [	        SU S35      e)Nzoutput name r   )r   get_outputsr   r   r   )r   r   r   outputs       r   get_output_typeTypeHelper.get_output_type   sH    #K$;$;$=>JB{{d"{{" ? <vZ899r   ort_typec                     [         R                  [         R                  [         R                  [         R                  [
        [         R                  S.nX;  a  [        U  S35      eX   $ )N)tensor(int64)tensor(int32)tensor(float)tensor(float16)tensor(bool)tensor(uint8) not found in map)numpylonglongintcfloat32float16booluint8r   )r   ort_type_to_numpy_type_maps     r   ort_type_to_numpy_type!TypeHelper.ort_type_to_numpy_type"   sV     #^^"ZZ"]]$}} "[[&
" 5z):;<<)33r   c                    [         R                  [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  S.nX;  a  [        U  S35      eX   $ )N)r!   r"   r#   r$   ztensor(bfloat16)r%   r&   r'   )	torchint64int32r+   r,   bfloat16r-   r.   r   )r   ort_type_to_torch_type_maps     r   ort_type_to_torch_type!TypeHelper.ort_type_to_torch_type1   sa     #[["[["]]$}} %!JJ"[[&
" 5z):;<<)33r   
numpy_typec                    [         R                  [        R                  [         R                  [        R
                  [         R
                  [        R
                  [         R                  [        R                  [         R                  [        R                  [        [        R                  [         R                  [        R                  0nX;  a  [        U  S35      eX   $ Nr'   )r(   r)   r3   r4   r*   r5   r+   r,   r-   r.   r   )r:   numpy_type_to_torch_type_maps     r   numpy_type_to_torch_type#TypeHelper.numpy_type_to_torch_typeA   s     NNEKKJJKKMM5==MM5==%**KK(
$ 9
|+<=>>+77r   
torch_typec                    [         R                  [        R                  [         R                  [        R
                  [         R                  [        R                  [         R                  [        R                  [         R                  [        [         R                  [        R                  0nX;  a  [        U  S35      eX   $ r<   )r3   r4   r(   r)   r5   r*   r+   r,   r-   r.   r   )r@   torch_type_to_numpy_type_maps     r   torch_type_to_numpy_type#TypeHelper.torch_type_to_numpy_typeQ   sz     KKKKMM5==MM5==JJKK(
$ 9
|+<=>>+77r   c                    0 nU R                  5        H.  n[        R                  UR                  5      XR                  '   M0     U R                  5        H.  n[        R                  UR                  5      XR                  '   M0     U$ )z:Create a mapping from input/output name to numpy data type)r   r	   r0   r   r   r   )r   name_to_numpy_typer   r   s       r   get_io_numpy_type_map TypeHelper.get_io_numpy_type_map`   ss       ++-E-7-N-Nuzz-Zzz* . "--/F.8.O.OPVP[P[.\{{+ 0!!r    N)__name__
__module____qualname____firstlineno__staticmethodr   strr   r   r0   r8   r(   dtyper>   r3   rC   dictrG   __static_attributes__rI   r   r   r	   r	      s    9$4 9C 9C 9 9 :3 :3 : : 4 4 4 4 4 4 8U[[ 8 8 8U[[ 8 8 "+; "S%++EU@V " "r   r	   c                       \ rS rSr\S\4S j5       r\ SS\R                  S\R                  S\R                  S\	\R                     4S	 jj5       r
\SS
 j5       rSrg)IOBindingHelperl   r   c                     0 nUR                  5        HZ  u  pE[        R                  X5      n[        R                  U5      n[        R
                  " [        R                  " U5      XrS9X4'   M\     U$ )zpReturns a dictionary of output name as key, and 1D tensor as value. The tensor has enough space for given shape.)rP   device)itemsr	   r   r8   r3   emptyr(   prod)r   output_shapesrW   output_buffersr   shaper   r@   s           r   get_output_buffers"IOBindingHelper.get_output_buffersm   sb     (..0KD!11+DH#::8DJ#(;;uzz%/@
#bN  1 r   N	input_idsposition_idsattention_maskpastc                 j   Uc  [         R                  U 5      nU R                  5       nUR                  5       (       d   eUR	                  SUR
                  R                  SUS   [        UR                  5       5      UR                  5       5        Ub  [        U5       H  u  pU
R                  5       (       d   eU
R                  5       nUS:X  a  UR                  5       nUR	                  SU	 3U
R
                  R                  SUSU	 3   [        U
R                  5       5      U5        M     Ubi  UR                  5       (       d   eUR	                  SUR
                  R                  SUS   [        UR                  5       5      UR                  5       5        Ubi  UR                  5       (       d   eUR	                  SUR
                  R                  SUS   [        UR                  5       5      UR                  5       5        U R                  5        H  nUR                  nX]   n[        R                  U SUR
                  R                   S[        UR                  5       5       35        UR                  UUR
                  R                  SX}   Xm   UR                  5       5        M     U$ )z)Returnas IO binding object for a session.r`   r   past_rb   ra   z device type=z shape=)r	   rG   
io_bindingis_contiguous
bind_inputrW   r   listsizedata_ptrr   r   r   loggerdebugbind_output)r   r`   ra   rb   rc   r\   r[   name_to_np_typerf   ipast_irk   r   output_nameoutput_buffers                  r   prepare_io_binding"IOBindingHelper.prepare_io_bindingw   s    "(>>{KO !++-
 &&((((!!K(!" 	
 &t_	++----!??,q=  )113H%%A3KMM&&#eA3K0' -$ %!//1111!! %%** 01^((*+'') #--////!!##((/\&&()%%' "--/F ++K*7MLLK=m6J6J6O6O5PPWX\]j]o]o]qXrWstu""$$)),*&&(	 0 r   c                 v   / nU R                  5        H  nUR                  nX   nX&   nUS[        R                  " U5       R	                  U5      R                  5       R                  5       n	U(       a/  UR                  U	R                  5       R                  5       5        M  UR                  U	5        M     U$ )z3Copy results to cpu. Returns a list of numpy array.r   )	r   r   r(   rZ   reshapeclonedetachappendcpu)
r   r\   r[   return_numpyort_outputsr   rr   bufferr]   copy_tensors
             r   "get_outputs_from_io_binding_buffer2IOBindingHelper.get_outputs_from_io_binding_buffer   s     !--/F ++K#0F!.E UZZ%67??FLLNUUWK"";??#4#:#:#<="";/ 0 r   rI   N)T)rJ   rK   rL   rM   rN   r   r^   r3   Tensorri   rt   r   rR   rI   r   r   rT   rT   l   s    (8    S<<S llS 	S
 5<< S Sj  r   rT   c                       \ rS rSrSrSS\S\R                  4S jjrS\	S\	4S jr
S	 rS
\	S\R                  4S jrS\4S jrSS\\	\R                  4   S\S\4S jjr\SS\S\S\S\\	\4   4S jj5       rSrg)CudaSession   zLInference Session with IO Binding for ONNX Runtime CUDA or TensorRT providerr   rW   c                    Xl         U R                   R                  5        Vs/ s H  oDR                  PM     snU l        U R                   R	                  5        Vs/ s H  oUR                  PM     snU l        [        R                  U R                   5      U l        U R                   R                  5       U l	        X0l
        [        5       U l        [        5       U l        X l        0 U l        g s  snf s  snf r   )r   r   r   input_namesr   output_namesr	   rG   io_name_to_numpy_typerf   enable_cuda_graphr   input_tensorsoutput_tensorsrW   buffer_sharing)selfr   rW   r   r   r   s         r   __init__CudaSession.__init__   s    &484D4D4O4O4QR4Q5JJ4QR7;7G7G7S7S7UV7UV[[7UV%/%E%EdFVFV%W"**557!2(])m /1 SVs   C.C3
input_namerr   c                     XR                   ;   d   eX R                  ;   d   eX R                  U'   XR                  U'   g r   )r   r   r   )r   r   rr   s      r   set_buffer_sharingCudaSession.set_buffer_sharing   sB    -----/////*5J'+5K(r   c                     U ? U ?U ?g r   )r   r   rf   )r   s    r   __del__CudaSession.__del__   s    Or   r   tensorc           	      p   UR                   R                  b  UR                   R                  OSn[        UR                  5      S:X  a  S/O[	        UR                  5      nU R
                  R                  UUR                   R                  UU R                  U   UUR                  5       5        XR                  ;   ax  U R
                  R                  U R                  U   UR                   R                  UU R                  U   UUR                  5       5        X R                  U R                  U   '   g g )Nr      )rW   indexlenr]   ri   rf   rh   r   r   rk   r   rn   r   )r   r   r   	device_idtensor_shapes        r   bind_input_and_buffer_sharing)CudaSession.bind_input_and_buffer_sharing   s    +1==+>+>+JFMM''PQ	!&,,/14s$v||:L""MM&&t,OO	
 &&&OO''##D)""**40! >D 3 3D 9: 'r   
shape_dictc                    U R                   (       a  UR                  5        H  u  p#X R                  ;   d  M  X R                  ;   a<  [	        U R                  U   R
                  5      [	        U5      :X  a  MV  [        S5      eU R                  U   n[        R                  " [	        U5      [        R                  U5      S9R                  U R                  S9nXPR                  U'   U R                  X%5        M     UR                  5        GHS  u  p#X R                  ;   d  M  X R                   ;   a1  [	        U R                   U   R
                  5      [	        U5      :X  a  MW  X R"                  ;   a  Mh  U R                  U   n[        R                  " [	        U5      [        R                  U5      S9R                  U R                  S9nXPR                   U'   U R$                  R'                  UUR                  R(                  UR                  R*                  b  UR                  R*                  OSU[-        UR/                  5       5      UR1                  5       5        GMV     g)z Allocate tensors for I/O Bindingz(Expect static input shape for cuda graph)rP   )rW   Nr   )r   rX   r   r   tupler]   RuntimeErrorr   r3   rY   r	   r>   torW   r   r   r   r   rf   rn   r   r   ri   rj   rk   )r   r   r   r]   numpy_dtyper   s         r   allocate_buffersCudaSession.allocate_buffers  s   !!)//1+++111 !3!3D!9!?!?@E%LP$*+UVV"&"<"<T"BK"[[uZ=`=`al=mnqq#{{ r F 06&&t,66tD  2 &++-KD(((...59L9LT9R9X9X3Y]bch]i3i..."88>U5\9\9\]h9ijmm;; n  -3##D)++MM&&+1==+>+>+JFMM''PQ'OO% .r   N	feed_dictrun_optionssynchronizec                 t   UR                  5        GH  u  pE[        U[        R                  5      (       a  UR	                  5       (       d   eX@R
                  ;   d  MM  U R                  (       a  U R                  U   R                  5       UR                  5       :X  d   eU R                  U   R                  UR                  :X  d   eUR                  R                  S:X  d   eU R                  U   R                  U5        M  U R                  XE5        GM     U(       af  U R                  R                  5         U R                   R#                  U R                  U5        U R                  R%                  5         U R&                  $ U R                   R#                  U R                  U5        U R&                  $ )z$Bind input tensors and run inferencecuda)rX   
isinstancer3   r   rg   r   r   r   nelementrP   rW   r   copy_r   rf   synchronize_inputsr   run_with_iobindingsynchronize_outputsr   )r   r   r   r   r   r   s         r   inferCudaSession.infer<  sO   %OO-LDfell338L8L8N8NNN'''))--d3<<>&//BSSSS--d399V\\III!==--777&&t,226:66tD . OO..0//MOO//1 """ //M"""r   r   r   streamr   c                 :    U SUS.nUS:w  a  [        U5      US'   U$ )NkSameAsRequested)r   arena_extend_strategyr   r   user_compute_stream)rO   )r   r   r   optionss       r   get_cuda_provider_options%CudaSession.get_cuda_provider_optionsR  s1     #%7!2
 Q;-0[G)*r   )
r   rW   r   r   r   rf   r   r   r   r   F)NT)r   )rJ   rK   rL   rM   __doc__r   r3   rW   r   rO   r   r   r   r   	ShapeDictr   rQ   r   r-   r   rN   intr   r   rR   rI   r   r   r   r      s    V1$4 1ell 16S 6s 6
D# Du|| D0(9 (T#tC$56 #Z #ei #, S T SV _cdgildl_m  r   r   c                      ^  \ rS rSr    SS\S\R                  S\S\S\	S\	S	\
\\4   S-  4U 4S
 jjjrSS\S\4S jjrSS\
\\R                  4   S\4U 4S jjjrSrU =r$ )
GpuBindingia  Nr   rW   r   enable_gpu_graphgpu_graph_idr   r   c                   > [         T
U ]  XU5        U(       a*  UR                  5        H  u  pU R                  X5        M     U R	                  U5        XPl        U(       a  [        R                  " U5      OS U l        X`l	        S U l
        g r   )superr   rX   r   r   r   copydeepcopyr   r   last_run_gpu_graph_id)r   r   rW   r   r   r   r   r   r   rr   	__class__s             r   r   GpuBinding.__init__b  st     	.>?+9+?+?+A'
''
@ ,B 	j)(7G$--
3T%)"r   disable_cuda_graph_in_runr   c                     [        5       nU(       a  SOU R                  nUR                  S[        U5      5        X0l        U$ )Nr   )r   r   add_run_config_entryrO   r   )r   r   r   r   s       r   get_run_optionsGpuBinding.get_run_optionsy  s8    ,6rD<M<M$$^S5FG%1"r   r   c                    > U R                  U5      nU R                  (       a  UR                  SS5        [        TU ]  X5      $ )N'disable_synchronize_execution_providers1)r   r   r   r   r   )r   r   r   r   r   s       r   r   GpuBinding.infer  s<    **+DE;;,,-VX[\w}Y44r   )r   r   r   r   )Fr   r   Nr   )rJ   rK   rL   rM   r   r3   rW   r   r-   r   rQ   rO   r   r   r   r   r   rR   __classcell__)r   s   @r   r   r   a  s     "'04*%* * 	*
 * * * S#X-* *.	 	* 	5tC$56 5SW 5 5r   r   c            	       |    \ rS rSrSrSS\S\R                  S\S\4S jjr	  SS	\
S
\S\\\4   S-  S\4S jjrSrg)GpuBindingManageri  zA manager for I/O bindings that support multiple CUDA Graphs.
One cuda graph is reused for same input shape. Automatically add a new cuda graph for new input shape.
r   rW   r   max_cuda_graphsc                 P    Xl         X l        / U l        S U l        X0l        X@l        g r   )r   rW   graph_bindingsno_graph_bindingr   r   )r   r   rW   r   r   s        r   r   GpuBindingManager.__init__  s,    & ! !%.r   Nr   use_cuda_graphr   r   c           
      <   U R                    H  nUR                  U:X  d  M  Us  $    [        U R                   5      U R                  :  d  U(       do  U R                  c;  [        U R                  U R                  XR                  US9U l        U R                  $ U R                  R                  U5        U R                  $ [        U R                  U R                  US[        U R                   5      U R                  US9nU R                   R                  U5        U$ )N)r   r   T)r   r   r   r   )r   r   r   r   r   r   r   rW   r   r   rz   )r   r   r   r   gpu_graph_bindings        r   get_bindingGpuBindingManager.get_binding  s     "&!4!4 ++z9(( "5 t""#t';';;N$$,(2$$dkk:kkbp)%
 ((( %%66zB((( 'KK!T001;;)
 	""#45  r   )rW   r   r   r   r   r   )r   r   )FN)rJ   rK   rL   rM   r   r   r3   rW   r   r   r   r-   rQ   rO   r   r   rR   rI   r   r   r   r     st    /$4 /ell /TW /nq /"  %04	 ! !  ! S#X-	 !
 
 !  !r   r   )r   loggingcollectionsr   collections.abcr   typingr   r(   r3   onnxruntimer   r   rO   r   ri   r   r   	getLoggerrJ   rl   r	   rT   r   r   r   rI   r   r   <module>r      s      # #    4 Cc**+				8	$W" W"tn nbA AH)5 )5X3! 3!r   