
    JTh                     	   % S SK r S SKrS SKrS SKrS SKJrJr  S SKJrJ	r	J
r
JrJrJr  S SKJr  S SKrS SKrS SKr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Jr  S S	KJr  S S
KJ r   \(       a&  S SK!r!S SK"r"S SK#J$r%  S SK&rS SK'rS SK(rS SK)rS SK*rS SK+rSq,\\-   \.S'   / SQr/S\-4S jr00 r1\2\3\44   \.S'    SPS\5S\\Rl                  Rn                     S\34S jjr8S\\3   4S jr9S\34S jr:S r;S\34S jr<\Rz                  " \>5      r? " S S\5      r@S\Rl                  Rn                  SS4S jrAS\B\3S4   4S jrCS\Rl                  Rn                  S\B\S4   4S jrDS\Rl                  Rn                  S\4S  jrES\Rl                  Rn                  S\B\3S4   4S! jrFS"\B\3S4   S\B\3S4   4S# jrGS$\B\\R                  \R                  \4\R                  \K\R                  \-4   S4   S\BS%   4S& jrMS'\B\R                  S4   S(\BS%   S\B\R                  S4   4S) jrNS*\S\R                  4S+ jrOS,\\R                  \4\K\-4   S-S.S\R                  4S/ jrPS*\R                  S0\\R                  \R                  \4\R                  \K\R                  \-4   S\\R                  \4\K\-4   4S1 jrQS2S3S4\B\3S4   S5\B\R                  S4   S6\BS%   S7\B\3S4   S8\B\R                  S4   S9\BS%   S:\-S;\BS<   S=\B\\R                  \R                  \4\R                  \K\R                  \-4   S4   S\B\\R                  \4\K\-4   S4   4S> jrRS2S3S4\B\3S4   S5\B\R                  S4   S6\BS%   S7\B\3S4   S8\B\R                  S4   S9\BS%   S:\-S;\BS<   S=\B\\R                  \R                  \4\R                  \K\R                  \-4   S4   S\B\\R                  \4\K\-4   S4   4S? jrS " S@ SA5      rT\ R                   " SB SC5      5       rV\\3\B\3\\3\4   4   4   rW\\.SD'    \ R                  " SESF9\" SGSH9 " SI SJ5      5       5       rX\" SGSH9 " SK SL5      5       rY\" SGSH9SSM.S\Rl                  Rn                  SN\\\X\\3\4   4      4SO jj5       rZg)Q    N)MappingSequence)AnyCallableFinalOptionalTYPE_CHECKINGUnion)	TypeAlias)
FakeTensor)compatibility)FakeTensorProp)OperatorSupport)CALLABLE_NODE_OPS)_pytree_pybind_state_SUPPORT_ONNXRT)is_onnxrt_backend_supportedtorch_compile_backendOrtExecutionProviderOrtBackendOptions
OrtBackendreturnc                     [         cg   [        R                  " S5        [        R                  " S5        [        R                  " S5        SSKn SSKn SSKn SSKn SSKJnJ	nJ
nJn  Sq [         $ [         $ ! [         a
    Sq  [         $ f = f)	a  Returns ``True`` if ONNX Runtime dependencies are installed and usable
to support TorchDynamo backend integration; ``False`` otherwise.

Example::

    # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ONNX)
    >>> import torch
    >>> if torch.onnx.is_onnxrt_backend_supported():
    ...     @torch.compile(backend="onnxrt")
    ...     def f(x):
    ...             return x * x
    ...     print(f(torch.randn(10)))
    ... else:
    ...     print("pip install onnx onnxscript onnxruntime")
    ...
Nonnxruntimezonnxruntime.capi._pybind_state
onnxscriptr   )decomposition_tablefx_onnx_interpreterpasses
type_utilsTF)r   	importlibimport_module
torch.onnxtorch.onnx._internal%torch.onnx._internal._exporter_legacy torch.onnx._internal.diagnosticstorch.onnx._internal.fxr   r   r    r!   ImportError)torchr   r   r    r!   s        X/var/www/auris/envauris/lib/python3.13/site-packages/torch/onnx/_internal/onnxruntime.pyr   r   /   s    & 	$##M2##$DE ##L1'83  #O ?  	$#O	$s   A A5 5B	B	_dumped_onnx_modelmodel_stringgraph_modulec                    [         R                  R                  SS5      nU(       d  g[        R                  US5      S-   nU U S3n[	        US5       nUR                  U 5        SSS5        U[        U'   UbA  U U S3n[	        US	S
S9 nUR                  [        UR                  5      5        SSS5        U$ U$ ! , (       d  f       N]= f! , (       d  f       U$ = f)zStores the onnx model into a file.
The name is "{ONNXRT_DUMP_PATH}{N}.onnx"
where *N* is the number of files already stored with
this prefix.
If graph_module is not None, the graph is stored as a string with
the same filename except the extension (.txt).
ONNXRT_DUMP_PATHN    z.onnxwbz.txtwzutf-8)encoding)osenvirongetr,   openwritestrgraph)r-   r.   prefixnfilenameffilename_txts          r+   _dump_onnx_modelrC   c   s     ZZ^^.5Fvr*Q.A!E"H	h		 
!"v !D),g6!GGC**+, 7O8O 
	
 76Os   B>%C>
C
Cc                      S/$ )NCPUExecutionProvider rF       r+   _infer_default_epsrH   |   s     ###rG   namec                     [         R                  R                  5       (       a*  [         R                  R                  R	                  U 5        gg)zIf PyTorch is installed with CUDA support, this starts NVTX range.

Check torch.cuda.nvtx.range_push's document for more details.
N)r*   cudais_availablenvtx
range_pushrI   s    r+   _nvtx_range_pushrP      s2    
 zz  

""4( !rG   c                      [         R                  R                  5       (       a)  [         R                  R                  R	                  5         gg)zIf PyTorch is installed with CUDA support, this terminates NVTX range.

Check torch.cuda.nvtx.range_pop's document for more details.
N)r*   rK   rL   rM   	range_poprF   rG   r+   _nvtx_range_poprS      s0    
 zz  

!!# !rG   device_typec                     SSK Jn  U S:X  a  UR                  R                  5       $ U S:X  a  UR                  R	                  5       $ U S:X  a  UR                  R                  5       $ [        SU -   5      e)Nr   r   rK   cpumaiazUnsupported device type: )onnxruntime.capir   	OrtDevicerK   rV   npu
ValueError)rT   ORTCs     r+   _get_ort_device_typer]      sg    6f~~""$$e~~!!##f~~!!##
0;>
??rG   c                      ^  \ rS rSrSrS\\   S\\\4   4U 4S jjr	S\
\\R                  R                  4   S\R                  R                  S\4U 4S	 jjrS
rU =r$ )OrtOperatorSupport   a   Operator support for ONNXRuntime backend.

It has two-level of support decision. One is via support_dict and the other one
is via extra_support_dict. The logic of using support_dict is implemented in
OrtOperatorSupport and extra_support_dict is used by OperatorSupport.is_node_supported.
support_dictextra_support_dictc                 0   > [         TU ]  U5        Xl        g N)super__init___onnx_support_dict)selfra   rb   	__class__s      r+   rf   OrtOperatorSupport.__init__   s    
 	+,".rG   
submodulesnoder   c                   > UR                   [        ;  a  gUR                   S:X  aO  UR                  U R                  ;   a5  [        R                  SUR                  [        UR                  5      5        g[        TU ]!  X5      (       a5  [        R                  SUR                  [        UR                  5      5        g[        R                  SUR                  [        UR                  5      5        g)NFcall_functionz0support_dict supports node.target: %s (type: %s)Tz6extra_support_dict supports node.target: %s (type: %s)zLsupport_dict and extra_support_dict don't support node.target: %s (type: %s))
opr   targetrg   loggerinfotypere   is_node_supportedwarning)rh   rk   rl   ri   s      r+   rt   $OrtOperatorSupport.is_node_supported   s     77++77o%$++9P9P*PKKBT[[!
  7$Z66KKHT[[!
 ZKK	

 rG   )rg   )__name__
__module____qualname____firstlineno____doc__setr   dictr<   rf   r   r*   nnModulefxNodeboolrt   __static_attributes____classcell__)ri   s   @r+   r_   r_      sc    /SX /4S> /!#uxx"67?Dxx}}	 rG   r_   c                     U R                   n/ nSnUR                   H=  nUR                  S:X  a  UR                  U5        Ub  M)  UR                  S:w  d  M;  UnM?     Uc  gU H  nUR	                  U5        M     g)z
In torch.fx.Graph, placeholder is a special assignment node. If it's not
executed in the beginning, it could overwrite values computed by upstream
nodes.
Nplaceholder)r=   nodesro   appendprepend)r.   r=   placeholdersfirst_not_placeholderrl   r   s         r+   _move_placeholder_to_frontr      s|     EL 77m#% (TWW-E$(!	 
 $#%%k2 $rG   .c                      / nU  Hh  n[        US5      (       d  M  UR                  nUR                  S:X  a  UR                  S5        ME  UR                  S:X  d  MW  UR                  S5        Mj     [	        U5      $ )zBReturn the first valid device (i.e., GPU or CPU) in argument list.devicerK   CUDAExecutionProviderrV   rE   )hasattrr   rs   r   tuple)argsepsargr   s       r+   _infer_ep_from_devicer      se    
C3!!ZZF{{f$

23%

12  :rG   c                 :   / nU R                   R                   Hu  nUR                  S:X  d  M  [        US5      (       a>  SUR                  ;   a.  [        UR                  S   [        R                  5      (       d   eUR                  U5        Mw     [        U5      $ )Nr   metaval)
r=   r   ro   r   r   
isinstancer*   Tensorr   r   )r.   r   rl   s      r+   _extract_graph_module_inputsr      s|    L""((77m#tV$$$)));!$))E"2ELLAAAA%	 )
 rG   c                     U R                   R                   H$  nUR                  S:X  d  M  UR                  S   s  $    [	        S5      e)zHCollect "val" fields from outputs metadata in this torch.fx.GraphModule.outputr   z2No output node found in this torch.fx.GraphModule.)r=   r   ro   r   r[   )r.   rl   s     r+   _extract_graph_module_outputsr     sB    ""((77h 99Q<	 )
 I
JJrG   c                     [         R                  " [        U 5      5      u  pU Vs/ s H7  n[        US5      (       d  M  SUR                  ;   d  M(  UR                  S   PM9     nn[        U6 $ s  snf )z[Return the all valid devices (i.e., GPU or CPU) among outputs of this torch.fx.GraphModule.r   r   )r   tree_flattenr   r   r   r   )r.   flattened_output_args_
output_argselected_output_argss        r+   _infer_ep_from_graph_moduler     s}    &33%l3  0/J J'	 	 -2Z__,D	 	
/   !"677s   A.A.A.r   c                 `    S[         S[        4S jn[        U 5      n[        [	        X!SS95      $ )z:Sort execution providers in eps based on pre-set priority.epr   c                      U S:X  a  gU S:X  a  gg)NrE      r   r3   r   rF   )r   s    r+   get_execution_provider_priority2_sort_eps.<locals>.get_execution_provider_priority"  s    ''(( rG   T)keyreverse)r<   intr|   r   sorted)r   r   
unique_epss      r+   	_sort_epsr     s3    	C 	C 	 SJ
QUVWWrG   valueszORTC.OrtDevice.c           	      Z  ^^^ SSK Jm  S[        S[        4S jmS[        [        R
                  [        R                  [        [        R                  [        [        R                  [        4   S[        4UU4S jjm[        U 5      S:  a  [        U4S jU  5       5      nU$ T" S	5      4$ )
Nr   r   	device_idr   c                     U =(       d    S$ )Nr   rF   )r   s    r+   _device_id_or_zero-_get_onnx_devices.<locals>._device_id_or_zero;  s    ~ArG   valuec           	      L  > [        U [        R                  5      (       ac  TR                  " [	        U R
                  R                  5      TR                  R                  5       T" U R
                  R                  5      5      $ [        U [        R                  [        [        R                  [        [        R                  [        45      (       a5  TR                  " [	        S5      TR                  R                  5       S5      $ [        S[!        [        U 5      5      -   5      e)NrV   r   zUnsupported value type: )r   r*   r   rY   r]   r   rs   default_memoryindexSymIntr   SymFloatfloatSymBoolr   r[   r<   )r   r\   r   s    r+   _map_tensor_or_sym_to_device7_get_onnx_devices.<locals>._map_tensor_or_sym_to_device>  s    
 eU\\**>>$U\\%6%67--/"5<<#5#56 
 ELL#u~~uemmTR
 
 >>$U+T^^-J-J-La  7#d5k:JJKKrG   c              3   4   >#    U  H  nT" U5      v   M     g 7frd   rF   ).0r   r   s     r+   	<genexpr>$_get_onnx_devices.<locals>.<genexpr>S  s     TVE8??Vs   r3   )rX   r   r   r
   r*   r   r   r   r   r   r   lenr   )r   ort_devicesr\   r   r   s     @@@r+   _get_onnx_devicesr   1  s     7c c LLL%,,U^^UEMMSWW
L 
	L L( 6{QTVTT,Q/11rG   tensorsdevicesc                 n   SSK Jn  SSKJn  UR                  " 5       nUR                  [        U 5      5        / n/ n/ nU  H^  nUR                  X8R                     5        UR                  UR                  5       5        UR                  UR                  5       5        M`     UR                  XXVU5        U$ )Nr   r   )_TORCH_DTYPE_TO_NUMPY_DTYPE)rX   r   "torch.onnx._internal.fx.type_utilsr   OrtValueVectorreserver   r   dtypesizedata_ptrpush_back_batch)	r   r   r\   r   	ortvaluesdtypesshapes	data_ptrstensors	            r+   !_get_ortvalues_from_torch_tensorsr   Y  s     7N##%Ic'l#FFI1,,?@fkkm$*+  g&'JrG   r   c                     U R                   (       a  [        S5      e[        R                  " U R	                  5       U R
                  U R                  S9nU$ )Nz#sparse tensor is not yet supported.)r   r   )	is_sparser[   r*   emptyr   r   r   )r   outs     r+   _to_real_tensorr   n  s<    >??
++fkkm6<<
NCJrG   dynamo_value
value_infoonnx.ValueInfoProtoc                    [        U [        R                  5      (       a]  [        UR                  R
                  R                  R                  5      S:X  a&  U R                  S:X  a  [        R                  " U 5      $ [        U [        5      (       a#  [        R                  " U [        R                  S9$ [        U [        5      (       a#  [        R                  " U [        R                  S9$ [        U [        5      (       a#  [        R                  " U [        R                  S9$ [        U [        R                  5      (       d   eU R                  5       $ )z9Helper function to wrap PyTorch variables as torch.Tensorr   )r3   )r   )r   r*   r   r   rs   tensor_typeshapedimsqueezer   r   int64r   float32r   
contiguous)r   r   s     r+   _adjust_scalar_from_fx_to_onnxr   u  s     	<..
++11556!;$& }}\**	L#	&	&||L<<	L%	(	(||L>>	L$	'	'||L

;;,5555&&((rG   
prim_valuec           	         [        U [        R                  5      (       d   S5       e[        U[        R                  [        [        R
                  [        [        R                  [        45      (       a  U R                  5       $ U $ )zFHelper function to wrap ORT-produced torch.Tensor as PyTorch variableszORT's output must be tensor.)
r   r*   r   r   r   r   r   r   r   item)r   r   s     r+   _adjust_scalar_from_onnx_to_fxr     s`    $ fell++K-KK+	sENNE5==$G 
 {{}MrG   sessonnxruntime.InferenceSessioninput_namesinputsinput_devicesoutput_namesoutputsoutput_devicespreallocate_outputinput_value_infosr   .normalized_prim_outputsc
                    SS K n
SSKJn  [        S5        [	        S [        X(5       5       5      n[        5         [        S5        [        X#5      nU(       a  [	        S U 5       5      n[        X5      nOUR                  " 5       n[        5         [        S5        U
R                  " 5       nUR                  SS	5        U R                  XXX5        [        5         U(       a3  [        S
5        [	        S [        WU	5       5       5      n[        5         U$ [        S
5        U
R                  R                  R                  R                  U5      n[	        S [        X5       5       5      n[        5         U$ )Nr   r   r   c              3   <   #    U  H  u  p[        X5      v   M     g 7frd   r   r   r   r   s      r+   r   8_run_onnx_session_with_ortvaluevector.<locals>.<genexpr>  !      =OC 	's77=   r   c              3   f   #    U  H'  n[        U[        5      (       a  [        U5      OUv   M)     g 7frd   )r   r   r   )r   ts     r+   r   r     s)      
LSq*Q
";";OABG   /1run_with_ortvaluevector'disable_synchronize_execution_providers1zafter run_with_ortvaluevectorc              3   <   #    U  H  u  p[        X5      v   M     g 7frd   r   r   onnx_outputprim_outputs      r+   r   r     "      
,U( +;DD,Ur  c              3   <   #    U  H  u  p[        X5      v   M     g 7frd   r
  r  s      r+   r   r     r  r  )r   rX   r   rP   r   ziprS   r   r   
RunOptionsadd_run_config_entryr  training	ortmodule_utils_ortvalues_to_torch_tensor)r   r   r   r   r   r   r   r   r   r   r   r\   
ort_inputspth_outputsort_outputsrun_optionss                   r+   %_run_onnx_session_with_ortvaluevectorr    sd   " 6\" "6= F &'26IJ
  
LS
 
 8T))+./((*K$$%NPST  *K  89  
,/=T,U
 
 	 	89!**44;;VV
  
,/,U
 
 	rG   c
           	      P   SS K n
[        S [        X(5       5       5      n[        X5       VVs0 s H<  u  pXR                  R	                  UR                  5       R                  5       5      _M>     nnnU R                  XM5      n[        S [        X5       5       5      nU$ s  snnf )Nr   c              3   <   #    U  H  u  p[        X5      v   M     g 7frd   r   r   s      r+   r   /_run_onnx_session_with_fetch.<locals>.<genexpr>  r  r  c              3   f   #    U  H'  u  p[        [        R                  " U5      U5      v   M)     g 7frd   )r   r*   
from_numpy)r   r   r  s      r+   r   r    s;      
 #LE	 	'U#	
 	
 #Lr  )r   r   r  OrtValueortvalue_from_numpyrV   numpyrun)r   r   r   r   r   r   r   r   r   r   r   rI   r   feedr  r  s                   r+   _run_onnx_session_with_fetchr&    s    "  "6= F  44LD 	""66vzz|7I7I7KLL4 	  ((<.K 
 #&k"K K s   AB"c                       \ rS rSrSrSSS\\S4   S\S   S	\\S4   S
\S   S\S   S\S   S\\\R                  S4   \R                  4   4S jr
S rSrg)OrtExecutionInfoPerSessioni(  zWInformation required to execute torch.fx.GraphModule using onnxruntime.InferenceSessionsessionr   r   .r   r   r   output_value_infosr   r   r   example_outputsc	                 f    Xl         X l        X0l        X@l        XPl        X`l        Xpl        UU l        g rd   r)  r   r   r   r*  r   r   r+  )	rh   r)  r   r   r   r*  r   r   r+  s	            r+   rf   #OrtExecutionInfoPerSession.__init__+  s?     6= -8BS-9CU :G:H  	rG   c                    SSK JnJn  [        U5      [        U R                  5      :w  a  g[        XR                  5       GH  u  pE[        U[        R                  [        [        45      (       d    g[        U[        [        [        45      (       aq  U" [        U5      5      nXeR                  R                  R                  :w  a    g[        UR                  R                  R                  R                   5      S:w  a    gM  X$R"                     nXeR                  R                  R                  :w  a    g[        UR                  UR                  R                  R                  R                   5       Hp  u  px[        U[        5      (       a#  UR$                  U:X  d  UR&                  (       a  M=  [        U[        R(                  5      (       a  UR&                  (       a  Mo      g   GM     g)Nr   )(_TORCH_DTYPE_TO_ONNX_TENSOR_ELEMENT_TYPE,from_python_type_to_onnx_tensor_element_typeFT)r   r0  r1  r   r   r  r   r*   r   r   r   r   rs   r   	elem_typer   r   r   	dim_value	dim_paramr   )	rh   r   r0  r1  r   r   
onnx_dtyper   onnx_dims	            r+   is_supported'OrtExecutionInfoPerSession.is_supportedL  s\   	
 t9D2233"4)?)?@OCcELL%#=>> #UD122I$s)T
!<!<!F!FF z2288<<=B  B))LJ__88BBB!$SYY
0K0K0Q0Q0U0U!Vc3''&&#-1C1CU\\22x7I7I  "W%  A6 rG   )r+  r   r   r   r   r   r*  r)  N)rw   rx   ry   rz   r{   r   r<   r
   r*   r   rf   r7  r   rF   rG   r+   r(  r(  (  s    a
/
 38_
 !!;<	

 CHo
 ""<=
 23
 34
 uU\\3%67EF
B%rG   r(  c                       \ rS rSrS	S jrS\R                  R                  4S jrS\R                  R                  S\	4S jr
Srg)
"OrtExecutionInfoForAllGraphModulesit  Nc                     0 U l         g rd   execution_info_per_graph_module)rh   s    r+   rf   +OrtExecutionInfoForAllGraphModules.__init__v  s    
  	,rG   r.   c                     XR                   ;  a  g U R                   U   nU H  nUR                  " U6 (       d  M  Us  $    g rd   )r=  r7  )rh   r.   r   
candidates	candidates        r+   &search_reusable_session_execution_infoIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_info}  sK     CCC 99,G
#I%%t,,   $
 rG   rr   c                     XR                   ;  a  U/U R                   U'   g U R                   U   R                  U5        g rd   )r=  r   )rh   r.   rr   s      r+   cache_session_execution_info?OrtExecutionInfoForAllGraphModules.cache_session_execution_info  s;     CCCBFD00>00>EEdKrG   r<  )r   N)rw   rx   ry   rz   rf   r*   r   GraphModulerB  r(  rE  r   rF   rG   r+   r:  r:  t  s@    !HH00 L!HH00L8RLrG   r:  r   T)frozenF)is_backward_compatiblec                       \ rS rSr% SrSr\\\      \	S'    Sr
\\	S'    Sr\\\      \	S'    Sr\\	S	'    Sr\\	S
'    Sr\S   \	S'    Sr\S   \	S'    Sr\\\S/S4         \	S'   Srg)r   i  a&  Options for constructing an ``OrtBackend``, the ONNX Runtime
backend (``"onnxrt"``) for ``torch.compile``.

Example::

    >>> @torch.compile(
    ...     backend="onnxrt",
    ...     options=torch.onnx._OrtBackendOptions(...),
    ... )
    ... def ort_function(x):
    ...     return x ** x
Npreferred_execution_providersTinfer_execution_providersdefault_execution_providersFr   use_aot_autogradztorch.onnx.ExportOptionsexport_optionszonnxruntime.SessionOptionsort_session_optionszonnx.ModelProtopre_ort_model_transformsrF   )rw   rx   ry   rz   r{   rK  r   r   r   __annotations__rL  r   rM  r   rN  rO  rP  rQ  r   r   rF   rG   r+   r   r     s     OS!8H5I,J#KR '+t*mLP(3G*H!IP  %$V!d! <@NH78?UBF">?FV 	 h,-t345 1rG   r   c            	          \ rS rSr% SrSS\\   4S jjrS\R                  R                  S\\\\\\4   4      4S jrS\R                  R                  4S	 jrS\R                  R                  S\R                  R                  4S
 jrS\R                  R                  S\R                  R                  4S jrSr\\S'   / r\\S       \S'   \ SS\\\\\\4   4      SS 4S jj5       r\S 5       r\S 5       rSrg)r   i  a  A backend compiles (sub-)graphs in torch.fx.GraphModule to onnxruntime.InferenceSession calls.

The compiler entry point is OrtBackend.compile, which
    1. partitions the original graph into supported sub-graphs (type: torch.fx.GraphModule) and unsupported
       sub-graphs.
    2. For each supported sub-graph, it replaces its _wrapped_call function with _ort_accelerated_call.
    3. Inside _ort_accelerated_call, it creates onnxruntime.InferenceSession and calls it to execute the sub-graph.
Noptionsc                    SSK Jn  SS KnSS KnSS KnUc
  [        5       OUU l        UR                  R                  R                  R                  U R                  R                  c  UR                  R                  5       OU R                  R                  5      U l        UR                  R                  R                  R                  R!                  U R                  R"                  5      nS S S S S S.n[%        XE5      U l        0 U l        [+        5       U l        SU l        SU l        [3        UR4                  S5      (       a  [6        U l        g [8        U l        g )Nr   r   )getattrz_operator.getitemz_operator.mulz_operator.addz_operator.subFr   )rX   r   r$   r&   +torch.onnx._internal.fx.decomposition_tabler   _optionsonnx	_internal_exporter_legacyResolvedExportOptionsrO  ExportOptions_resolved_onnx_exporter_optionsr   r   '_create_onnx_supports_op_overload_tableonnx_registryr_   _supported_ops_partitioner_cacher:  _all_ort_execution_info_assert_allclose_to_baselineexecution_countr   r   r  r&  r$  )rh   rT  r\   r*   ra   rb   s         r+   rf   OrtBackend.__init__  s1   :4:6=o027  JJ  11GG==//7 

((*]]11 	,& zz++..BBjj00>>

  "& "!!
.
 1R UW (J'K$,1) 
 t**,=>> 2 	 . 	rG   r.   r   c                    SnU R                   R                  (       a&  [        U6 =n(       a  UnO[        U5      =n(       a  Un/ n/ U R                   R                  =(       d    / Q[        U5      QU R                   R                  =(       d
    [        5       Q7 H\  n[        U[        5      (       a  U0 4nO"[        U[        5      (       a  US   c  US   0 4nUc  MD  Xv;  d  MK  UR                  U5        M^     U$ )NrF   r3   r   )rX  rL  r   r   rK  r   rM  rH   r   r<   r   r   )rh   r.   r   inferred_epseps_from_argseps_from_graph_moduleselected_epsr   s           r+   _select_epsOrtBackend._select_epsG  s     )+==22 5t <<}<
  -*El*SS&S  5
mm99?R
|$
 mm77O;M;O
B
 "c"""XB&&2a5=eR[~""8##B'
 rG   c                 	   SSK nSSKJnJn  U R                  R
                  " U/UQ76 nU(       ab  UR                  nUR                  n	UR                  n
UR                  nUR                  nUR                  nUR                  nUR                  nGOUR                  U R                  R                   U5      R#                  5       nU R                  R$                  (       a-  SU l        [)        U5      nS n[*        R,                  " UU5      nO [/        U5      R0                  " U0 UD6nUR9                  U R                  R                   S9nUR;                  U R                  R                   U5      R#                  5       nUR#                  UU R                  R<                  S9nUR?                  U R                  R@                  RB                  S	9nU RD                  RF                  (       a%  U RD                  RF                   H  nU" U5        M     URI                  5       n[J        RL                  RO                  S
S5      (       a
  [Q        UUS9  URR                  " UU RD                  RT                  U RV                  " U/UQ76 S9n[Y        S URZ                  R\                   5       5      n	[Y        S URZ                  R^                   5       5      n
[a        U5      n[c        U[X        5      (       a  [a        U5      nO[a        U45      n[Y        S URZ                  R\                   5       5      n[Y        S URZ                  R^                   5       5      n[e        UU	UU
UUUUS9nU R                  Rg                  UU5        U =Rh                  S-  sl4        [c        U[j        Rl                  5      nU(       a  U4OUn[c        U[X        5      (       d   e[o        S U 5       5      (       d   e[q        S5        U R#                  UU	UUU
UUU RD                  R&                  UU5
      n[s        5         U Rt                  (       ap  [j        Rv                  Rx                  Rz                  " U/UQ7SS06nU(       a  U4OUn[}        UU5       H&  u  nn[j        R~                  R                  UU5        M(     U(       a  US   $ U$ ! [2         a    [4        R7                  SU5        SU l        e f = f)zThis function replaces GraphModule._wrapped_call in compiled model.

The _wrapped_call is the underlying implementation of forward method. Replacing
it means we delegate the computation to _ort_acclerated_call and therefore
onnxruntime.InferenceSession.
r   N)r   r    Fc                 f    [        U S5      (       a  SU R                  ;   a  U R                  S   $ U $ )Nr   r   )r   r   )r   s    r+   maybe_map_to_meta_val>OrtBackend._ort_acclerated_call.<locals>.maybe_map_to_meta_val  s0    uf--%5::2E  %zz%00$rG   zFakeTensorProb failed for %s)diagnostic_context)fx_graph_moduleonnxfunction_dispatcher)opset_versionr0   )r.   )path_or_bytessess_options	providersc              3   8   #    U  H  oR                   v   M     g 7frd   rO   r   inputs     r+   r   2OrtBackend._ort_acclerated_call.<locals>.<genexpr>  s     O8Nu

8N   c              3   8   #    U  H  oR                   v   M     g 7frd   rO   r   r   s     r+   r   r|    s      S;R;Rr}  c              3   $   #    U  H  ov   M     g 7frd   rF   rz  s     r+   r   r|    s     %P9Oe9O   c              3   $   #    U  H  ov   M     g 7frd   rF   r  s     r+   r   r|    s     &T<S&v<Sr  r-  r3   c              3      #    U  H4  n[        U[        R                  [        R                  [        45      v   M6     g 7frd   )r   r*   r   r   r   )r   elems     r+   r   r|    s0      
/ tellELL#>??/s   <>$run_onnx_session_with_ortvaluevectorexecutoraten)Ar   r(   r   r    rc  rB  r)  r   r   r   r*  r   r   r+  MovePlaceholderToFrontr^  rr  r$  dynamic_shapesr   r   r   tree_mapr   	propagate	Exceptionrq   ru   FxOnnxInterpreterInsertTypePromotionrt  to_model_protor`  ru  rX  rQ  SerializeToStringr7   r8   r9   rC   InferenceSessionrP  rl  r   r=   r{  r   r   r   r(  rE  re  r*   r   allrP   rS   rd  _primsr  executer  testingassert_close)rh   r.   r   kwargsr   r   r    !cached_execution_info_per_sessiononnx_sessionr   r   r   r*  r   r   prim_outputsextracted_outputsrp  fx_interpreterexported
onnx_model	transformonnx_model_bytesexecution_info_per_sessionis_single_tensor_outputr   onnx_outputsbaseline_outputsnormalized_baseline_ouptutsr  baseline_outputs                                  r+   _ort_acclerated_callOrtBackend._ort_acclerated_callh  s    	G ((OO# 	*
 -<DDL;GGK<IIL A S S!B!U!U=KKM>MMN<LLL
 "8844GG ce  33BB*/'$A,$O!%  '//)+< #1,#?#I#I$!'$L 1BB#'#G#G#Z#Z C N "5544GGce 
 &)) ,(,(L(L(d(d * H
 "00"BBPP^^ 1 J }}55!%!G!GIj) "H  *;;=zz~~0$77 !!1M '77.!]]>>**<?$?L  O
8H8H8N8NOOK  S:;K;K;R;R SSL-d3M ,..!2<!@!2L?!C %%P9I9I9O9O%P P!&&TJ<L<L<S<S&T!T)C$'"3)#5+- ,	*& ((EE8 	!
 #-\5<<"H6\OL 	  159999 
/
 
 
 	
 

 	?@xx#MM,,#
 	,,$||44<< # .4  (?!#DT ( 1491,_ **;H1 #:|AK|KW ! NN#A<P /4D+ s   S )S+c                    SSK Jn  XR                  ;   a  U R                  U   nU$ UnU" UU R                  SS9nUR	                  5       nX@R                  U'   UR
                  R                   HM  nUR                  S:X  d  M  SUR                  ;   d  M'  [        XGR                  5      nU R                  Ul        MO     U$ )Nr   )CapabilityBasedPartitionerT)allows_single_node_partitioncall_modulefused_)!torch.fx.passes.infra.partitionerr  rb  ra  partition_and_fuser=   r   ro   rI   rV  r  _wrapped_call)	rh   r.   r   r  partitioned_prim_graph_moduleprim_graph_modulepartitionerrl   fused_modules	            r+   compileOrtBackend.compile1  s    
 	Q, 222,0,C,CL,Q)0 -,- !-4!##-1K
 -8,J,J,L)4Q##L1 6;;AA 77m+DII0E#*+H))#TL 261J1JL. B -,rG   c                     U R                   R                  (       a8  SSKJn  SSKJn  U" U R                  UU R                  R                  S9" X5      $ U R                  X5      $ )zIf ``OrtBackendOptions.use_aot_autograd`` is ``True``, the `auto_autograd` compiler
will be invoked, wrapping this ``OrtBackend`` instance's ``compile`` method. Otherwise,
the ``compile`` method is invoked directly.r   )#min_cut_rematerialization_partition)aot_autograd)fw_compilerpartition_fndecompositions)	rX  rN  functorch.compiler  torch._dynamo.backends.commonr  r  r^  r   )rh   r.   r   r  r  s        r+   __call__OrtBackend.__call__g  sX     ==))MB LL@#CCWW 	" " ||L//rG      %_OrtBackend__instance_cache_max_count_OrtBackend__instance_cachec                   ^ ^ S[         S[         4S jm[        T [         5      (       d  [        S
0 T =(       d    0 D6m [        U U4S j[        R                   5       S5      nUc|  [        [        R                  5      [        R                  :  d'   S[        R                   S[         S[         S	35       e[        R                  R                  [        T 5      =n5        U$ )a  Returns a possibly cached instance of an ``OrtBackend``. If an existing
backend was created previously through this function with the same options,
it will be returned. Otherwise a new backend will be created, cached, and
returned.

Note: if ``options`` sets ``ort_session_options``, a new ``OrtBackend``
will always be returned, since ``onnxruntime.SessionOptions`` cannot
participate in caching.abc                 j   U R                   UR                   :w  d  U R                  UR                  :w  dh  U R                  UR                  :w  dN  U R                  UR                  :w  d4  U R                  UR                  :w  d  U R
                  UR
                  :w  a  gU R                  c  UR                  b  gU R                  UR                  L a  gU R                  b  UR                  b  U R                  R                  UR                  R                  :H  =(       a    U R                  R                  UR                  R                  :H  =(       a_    U R                  R                  UR                  R                  L =(       a,    U R                  R                  UR                  R                  L $ g)NFT)rK  rL  rM  r   rN  rQ  rP  rO  r  diagnostic_optionsr`  fake_context)r  r  s     r+   reusable<OrtBackend.get_cached_instance_for_options.<locals>.reusable  sl   //13R3RR..!2M2MM00A4Q4QQ''1+?+??%%););;--1K1KK $$0A4I4I4U1#3#33 +0@0@0L$$33q7G7G7V7VV W((;;''::;W ((66!:J:J:X:XXW ((559I9I9V9VV rG   c              3   \   >#    U  H!  nT" UR                   T5      (       d  M  Uv   M#     g 7frd   )rX  )r   r  rT  r  s     r+   r   =OrtBackend.get_cached_instance_for_options.<locals>.<genexpr>  s"     U31x

G7TQQ3s   ,	,NzNo more than z instances of z allowed. Please instantiate `z` explicitly to pass to `torch.compile`. See https://github.com/pytorch/pytorch/pull/107973#discussion_r1306144795 for discussion.rF   )r   r   nextr   r  r   r  r   )rT  backendr  s   ` @r+   get_cached_instance_for_options*OrtBackend.get_cached_instance_for_options|  s    !	) !	.? !	F '#455':7=b:GU
33U

 ?J//0:3X3XX  
 E EFn,<ZL I""X ''..*W:M/MwNrG   c                  @    [         R                  R                  5         g rd   )r   r  clearrF   rG   r+   clear_cached_instances!OrtBackend.clear_cached_instances  s    ##))+rG   c                  4    [        [        R                  5      $ rd   )r   r   r  rF   rG   r+   get_cached_instancesOrtBackend.get_cached_instances  s    Z0011rG   )	rc  rd  rX  rb  r^  ra  re  r   r$  rd   )rw   rx   ry   rz   r{   r   r   rf   r*   r   rG  r   r   r<   r   r   rl  r  r  r  r  r   rR  r  liststaticmethodr
   r  r  r  r   rF   rG   r+   r   r     sM   V
): ; V
p!HH00	%WS#X../	0BGL1E1E GLR4-EHH$8$8 4-588CWCW 4-l0!HH000			0$ )*)24eD./4IMC% 17383D DEFC	C CJ , , 2 2rG   r   )rT  rT  c                8    [         R                  U5      " X5      $ rd   )r   r  )r.   r   rT  s      r+   r   r     s     55g>|RRrG   rd   )[dataclassesr"   loggingr7   collections.abcr   r   typingr   r   r   r   r	   r
   typing_extensionsr   r*   torch._C
torch._opstorch._prims.executortorch.fxtorch._subclasses.fake_tensorr   torch.fx._compatibilityr    torch.fx.passes.fake_tensor_propr    torch.fx.passes.operator_supportr   torch.fx.passes.tools_commonr   torch.utilsr   rY  r   rX   r   r\   r$   r%   r&   r'   rW  torch.onnx._internal.fx.passesr   r   rR  __all__r   r,   r}   r<   r   bytesr   rG  rC   rH   rP   rS   r]   	getLoggerrw   rq   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r&  r(  	dataclassr:  r   r   r   r   rF   rG   r+   <module>r     sU      	 - G G '      4 1 ; < :  60+6) #'$ &.T .b &( DcN ' IM'/0D0D'E2$HSM $)3 )$
@c 
@ 
		8	$/ /d3UXX-A-A 3d 3*
E#s(O 
uxx/C/C cSVh K0D0D K K8ehh.B.B 8uSRUX 8 X5c? XuS#X X$%2LL%,,U^^UEMMSWW	
 		%2  !%2P5<<$%056K0L
5<<*J 5<< )	) &) \\)@LL	 	LL	
8N
(NsCxN %,,#$N ./	N
 S/N 5<<$%N /0N N 78N #LL%,,U^^UEMMSWW	
 		N  5sE4/0#56!Nb#
(#sCx# %,,#$# ./	#
 S/# 5<<$%# /0# # 78# #LL%,,U^^UEMMSWW	
 		#  5sE4/0#56!#LI IX L L LB #(U3S8I3I-J(J"K i K	 d#e,<1 <1 - $<1~ e,c2 c2 -c2L e,
 FJ	S((&&S e-wsCx/@@AB	S -SrG   