
    Th                     (	   % 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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q,\\-   \.S'   / SQr/S\-4S jr00 r1\2\3\44   \.S'    SRS\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rSS@\T4SA jrU " SB SC5      rV\ R                   " SD SE5      5       rX\\3\B\3\\3\4   4   4   rY\\.SF'    \ R                  " SGSH9\" SISJ9 " SK SL5      5       5       rZ\" SISJ9 " SM SN5      5       r[\" SISJ9SSO.S\Rl                  Rn                  SP\\\Z\\3\4   4      4SQ jj5       r\g)S    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                     [         cc   [        R                  " S5        [        R                  " S5        [        R                  " S5        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.fxr   r   r    r!   ImportError)torchr   r   r    r!   s        Z/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/onnx/_internal/onnxruntime.pyr   r   /   s    & 	$##M2##$DE ##L1'8  #O ?  	$#O	$s   AA1 1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_modelrB   b   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 rE       r*   _infer_default_epsrG   {   s     ###rF   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rH   s    r*   _nvtx_range_pushrO      s2    
 zz  

""4( !rF   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)   rJ   rK   rL   	range_poprE   rF   r*   _nvtx_range_poprR      s0    
 zz  

!!# !rF   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   rJ   cpumaiazUnsupported device type: )onnxruntime.capir   	OrtDevicerJ   rU   npu
ValueError)rS   ORTCs     r*   _get_ort_device_typer\      sg    6f~~""$$e~~!!##f~~!!##
0;>
??rF   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)selfr`   ra   	__class__s      r*   re   OrtOperatorSupport.__init__   s    
 	+,".rF   
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   targetrf   loggerinfotyperd   is_node_supportedwarning)rg   rj   rk   rh   s      r*   rs   $OrtOperatorSupport.is_node_supported   s     77++77o%$++9P9P*PKKBT[[!
  7$Z66KKHT[[!
 ZKK	

 rF   )rf   )__name__
__module____qualname____firstlineno____doc__setr   dictr;   re   r   r)   nnModulefxNodeboolrs   __static_attributes____classcell__)rh   s   @r*   r^   r^      sc    /SX /4S> /!#uxx"67?Dxx}}	 rF   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<   nodesrn   appendprepend)r-   r<   placeholdersfirst_not_placeholderrk   r   s         r*   _move_placeholder_to_frontr      s|     EL 77m#% (TWW-E$(!	 
 $#%%k2 $rF   .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.devicerJ   CUDAExecutionProviderrU   rD   )hasattrr   rr   r   tuple)argsepsargr   s       r*   _infer_ep_from_devicer      se    
C3!!ZZF{{f$

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

12  :rF   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   rn   r   r   
isinstancer)   Tensorr   r   )r-   r   rk   s      r*   _extract_graph_module_inputsr      s|    L""((77m#tV$$$)));!$))E"2ELLAAAA%	 )
 rF   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   rn   r   rZ   )r-   rk   s     r*   _extract_graph_module_outputsr     sB    ""((77h 99Q<	 )
 I
JJrF   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)NrD      r   r2   r   rE   )r   s    r*   get_execution_provider_priority2_sort_eps.<locals>.get_execution_provider_priority!  s    ''(( rF   T)keyreverse)r;   intr{   r   sorted)r   r   
unique_epss      r*   	_sort_epsr     s3    	C 	C 	 SJ
QUVWWrF   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   rE   )r   s    r*   _device_id_or_zero-_get_onnx_devices.<locals>._device_id_or_zero:  s    ~ArF   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)NrU   r   zUnsupported value type: )r   r)   r   rX   r\   r   rr   default_memoryindexSymIntr   SymFloatfloatSymBoolr   rZ   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KrF   c              3   4   >#    U  H  nT" U5      v   M     g 7frc   rE   ).0r   r   s     r*   	<genexpr>$_get_onnx_devices.<locals>.<genexpr>R  s     TVE8??Vs   r2   )rW   r   r   r
   r)   r   r   r   r   r   r   lenr   )r   ort_devicesr[   r   r   s     @@@r*   _get_onnx_devicesr   0  s     7c c LLL%,,U^^UEMMSWW
L 
	L L( 6{QTVTT,Q/11rF   tensorsdevicesc                 B   SS K nSSKJn  [        R                  UR                  [        R
                  UR
                  [        R                  UR                  [        R                  UR                  [        R                  UR                  [        R                  UR                  [        R                  UR                  [        R                  UR                  [        R                  UR                  0	nUR                  " 5       nUR!                  [#        U 5      5        / n/ n/ nU  H^  n	UR%                  XIR&                     5        UR%                  U	R)                  5       5        UR%                  U	R+                  5       5        M`     UR-                  XXgU5        U$ )Nr   r   )numpyrW   r   r)   float16float32float64uint8int8int16int32int64longlongr   bool_OrtValueVectorreserver   r   dtypesizedata_ptrpush_back_batch)
r   r   npr[   torch_dtype_to_numpy_dtype	ortvaluesdtypesshapes	data_ptrstensors
             r*   !_get_ortvalues_from_torch_tensorsr   X  s    6 	rzzrzzrzzRXX

BGGRXXRXXR[[

BHH
" ##%Ic'l#FFI0>?fkkm$*+  g&'JrF   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_sparserZ   r)   emptyr   r   r   )r   outs     r*   _to_real_tensorr   x  s<    >??
++fkkm6<<
NCJrF   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   )r2   )r   )r   r)   r   r   rr   tensor_typeshapedimsqueezer   r   r   r   r   r   
contiguous)r   r   s     r*   _adjust_scalar_from_fx_to_onnxr     s     	<..
++11556!;$& }}\**	L#	&	&||L<<	L%	(	(||L>>	L$	'	'||L

;;,5555&&((rF   
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 
 {{}MrF   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S Kn
[        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rc   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rc   )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rc   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rc   r  r  s      r*   r   r    r  r
  )r   rW   r   rO   r   ziprR   r   r   
RunOptionsadd_run_config_entryr  onnxruntime.training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$    sg   " 6\" "6= F &'26IJ
  
LS
 
 8T))+./((*K$$%NPST  *K  89  
,/=T,U
 
 	# 	89!**44;;VV
  
,/,U
 
 	rF   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rc   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rc   )r   r)   
from_numpy)r   r   r  s      r*   r   r'  *  s;      
 #LE	 	'U#	
 	
 #Lr  )r   r   r  OrtValueortvalue_from_numpyrU   r   run)r   r   r   r   r   r   r   r   r  r  r   rH   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"rr   c                     SSK n[        UR                  R                  [        UR                  R
                  [        UR                  R                  0nUR                  U 5      $ )a  
Converts a Python type to the corresponding ONNX tensor element type.
For example, `_from_python_type_to_onnx_tensor_element_type(float)` returns
`onnx.TensorProto.FLOAT`.

Args:
  type (type): The Python type to convert.

Returns:
  int: The corresponding ONNX tensor element type.

r   N)	onnxr   TensorProtoFLOATr   INT64r   BOOLr8   )rr   r0  (_PYTHON_TYPE_TO_ONNX_TENSOR_ELEMENT_TYPEs      r*   -_from_python_type_to_onnx_tensor_element_typer6  4  sW      	t%%T##d##0,
 477==rF   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)OrtExecutionInfoPerSessioniK  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 rc   r9  r   r  r   r:  r   r   r;  )	rg   r9  r   r  r   r:  r   r   r;  s	            r*   re   #OrtExecutionInfoPerSession.__init__N  s?     6= -8BS-9CU :G:H  	rF   c                    SS K nUR                  R                  [        R                  UR                  R
                  [        R                  UR                  R                  [        R                  UR                  R                  [        R                  UR                  R                  [        R                  UR                  R                  [        R                  UR                  R                  [        R                   UR                  R"                  [        R$                  UR                  R&                  [        R(                  UR                  R*                  [        R,                  UR                  R.                  [        R0                  UR                  R2                  [        R4                  UR                  R6                  [        R8                  0nUR;                  5        VVs0 s H  u  pEXT_M	     nnn[=        U5      [=        U R>                  5      :w  a  g[A        XR>                  5       GH  u  px[C        U[        RD                  [F        [H        45      (       d    g[C        U[H        [F        [$        45      (       at  [K        [M        U5      5      n	XRL                  RN                  RP                  :w  a    g[=        URL                  RN                  RR                  RT                  5      S:w  a    gM  XgRV                     n	XRL                  RN                  RP                  :w  a    g[A        URR                  URL                  RN                  RR                  RT                  5       Hp  u  p[C        U
[H        5      (       a#  URX                  U
:X  d  URZ                  (       a  M=  [C        U
[        R\                  5      (       a  URZ                  (       a  Mo      g   GM     gs  snnf )Nr   FT)/r0  r1  r2  r)   r   FLOAT16r   
FLOAT8E5M2float8_e5m2FLOAT8E5M2FNUZfloat8_e5m2fnuzFLOAT8E4M3FNfloat8_e4m3fnFLOAT8E4M3FNUZfloat8_e4m3fnuzDOUBLEr   r4  r   UINT8r   INT8r   INT16r   INT32r   r3  r   itemsr   r  r  r   r   r   r   r6  rr   r   	elem_typer   r   r   	dim_value	dim_paramr   )rg   r   r0  (_onnx_tensor_element_type_to_torch_dtyper   r   (_torch_dtype_to_onnx_tensor_element_typer   r   
onnx_dtyper   onnx_dims               r*   is_supported'OrtExecutionInfoPerSession.is_supportedo  s    ""EMM$$emm''):):++U-B-B))5+>+>++U-B-B##U]]!!5::""EKK!!5::""EKK""EKK""EKK4
0" GLLN4
N
 JN 	1 4
 t9D2233"4)?)?@OCcELL%#=>> #UD122J4PS9U
!<!<!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 I4
s   .O()r;  r   r   r  r   r   r:  r9  N)rv   rw   rx   ry   rz   r   r;   r
   r)   r   re   rV  r   rE   rF   r*   r8  r8  K  s    a
/
 38_
 !!;<	

 CHo
 ""<=
 23
 34
 uU\\3%67EF
B7rF   r8  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)
"OrtExecutionInfoForAllGraphModulesi  Nc                     0 U l         g rc   execution_info_per_graph_module)rg   s    r*   re   +OrtExecutionInfoForAllGraphModules.__init__  s    
  	,rF   r-   c                     XR                   ;  a  g U R                   U   nU H  nUR                  " U6 (       d  M  Us  $    g rc   )r\  rV  )rg   r-   r   
candidates	candidates        r*   &search_reusable_session_execution_infoIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_info  sK     CCC 99,G
#I%%t,,   $
 rF   rq   c                     XR                   ;  a  U/U R                   U'   g U R                   U   R                  U5        g rc   )r\  r   )rg   r-   rq   s      r*   cache_session_execution_info?OrtExecutionInfoForAllGraphModules.cache_session_execution_info  s;     CCCBFD00>00>EEdKrF   r[  )r   N)rv   rw   rx   ry   re   r)   r   GraphModulera  r8  rd  r   rE   rF   r*   rY  rY    s@    !HH00 L!HH00L8RLrF   rY  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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onnxruntime.SessionOptionsort_session_optionszonnx.ModelProtopre_ort_model_transformsrE   )rv   rw   rx   ry   rz   rj  r   r   r   __annotations__rk  r   rl  r   rm  rn  ro  r   r   rE   rF   r*   r   r     s     OS!8H5I,J#KR '+t*mLP(3G*H!IP  %$V!d! CG">?FV 	 h,-t345 1rF   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                  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        [/        UR0                  S5      (       a  [2        U l        g [4        U l        g )Nr   r   )getattrz_operator.getitemz_operator.mulz_operator.addz_operator.subFr   )rW   r   r$   r&   +torch.onnx._internal.fx.decomposition_tabler   _optionsr0  	_internal_exporter_legacyResolvedExportOptions_resolved_onnx_exporter_optionsr   r   '_create_onnx_supports_op_overload_tableonnx_registryr^   _supported_ops_partitioner_cacherY  _all_ort_execution_info_assert_allclose_to_baselineexecution_countr   r   r$  r.  r,  )rg   rr  r[   r)   r`   ra   s         r*   re   OrtBackend.__init__!  s    :4:6=o027  JJ  11GGI 	, zz++..BBjj00>>

  "& "!!
.
 1R UW (J'K$,1) 
 t**,=>> 2 	 . 	rF   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$ )NrE   r2   r   )rv  rk  r   r   rj  r   rl  rG   r   r;   r   r   )rg   r-   r   inferred_epseps_from_argseps_from_graph_moduleselected_epsr   s           r*   _select_epsOrtBackend._select_epsu  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'
 rF   c                 b	   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5      R                  5       nU R                   R"                  (       a-  SU l        ['        U5      nS n[(        R*                  " UU5      nO [-        U5      R.                  " U0 UD6nUR7                  5       nUR9                  U5      R                  5       nUR                  UU R                   R:                  S9nUR=                  U R                   R>                  R@                  S9nU RB                  RD                  (       a%  U RB                  RD                   H  nU" U5        M     URG                  5       n[H        RJ                  RM                  S	S5      (       a
  [O        UUS
9  URP                  " UU RB                  RR                  U RT                  " U/UQ76 S9n[W        S URX                  RZ                   5       5      n	[W        S URX                  R\                   5       5      n
[_        U5      n[a        U[V        5      (       a  [_        U5      nO[_        U45      n[W        S URX                  RZ                   5       5      n[W        S URX                  R\                   5       5      n[c        UU	UU
UUUUS9nU R                  Re                  UU5        U =Rf                  S-  sl3        [a        U[h        Rj                  5      nU(       a  U4OUn[a        U[V        5      (       d   e[m        S U 5       5      (       d   e[o        S5        U R                  UU	UUU
UUU RB                  R$                  UU5
      n[q        5         U Rr                  (       ap  [h        Rt                  Rv                  Rx                  " U/UQ7SS06nU(       a  U4OUn[{        UU5       H&  u  nn[h        R|                  R                  UU5        M(     U(       a  US   $ U$ ! [0         a    [2        R5                  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$rF   zFakeTensorProb failed for %s)fx_graph_moduleonnxfunction_dispatcher)opset_versionr/   )r-   )path_or_bytessess_options	providersc              3   8   #    U  H  oR                   v   M     g 7frc   rN   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rc   rN   r   r   s     r*   r   r    s      S;R;Rr  c              3   $   #    U  H  ov   M     g 7frc   rE   r  s     r*   r   r    s     %P9Oe9O   c              3   $   #    U  H  ov   M     g 7frc   rE   r  s     r*   r   r    s     &T<S&v<Sr  r=  r2   c              3      #    U  H4  n[        U[        R                  [        R                  [        45      v   M6     g 7frc   )r   r)   r   r   r   )r   elems     r*   r   r  7  s0      
/ tellELL#>??/s   <>$run_onnx_session_with_ortvaluevectorexecutoraten)@r   r'   r   r    r  ra  r9  r   r   r  r:  r   r   r;  MovePlaceholderToFrontr,  rz  dynamic_shapesr   r   r   tree_mapr   	propagate	Exceptionrp   rt   FxOnnxInterpreterInsertTypePromotionr  to_model_protor|  r  rv  ro  SerializeToStringr6   r7   r8   rB   InferenceSessionrn  r  r   r<   r  r   r   r   r8  rd  r  r)   r   allrO   rR   r  _primsr  executer  testingassert_close)rg   r-   r   kwargsr   r   r    !cached_execution_info_per_sessiononnx_sessionr   r   r  r:  r   r   prim_outputsextracted_outputsr  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_call  s    	G ((OO# 	*
 -<DDL;GGK<IIL A S S!B!U!U=KKM>MMN<LLL
 "88ce  33BB*/'$A,$O!%  '//)+< #1,#?#I#I$!'$L 1BBDN "55lCGGIL &)) ,(,(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|KO ! NN#A<P /4D+ s   ;R )R.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  r~  r}  partition_and_fuser<   r   rn   rH   rt  r  _wrapped_call)	rg   r-   r   r  partitioned_prim_graph_moduleprim_graph_modulepartitionerrk   fused_modules	            r*   compileOrtBackend.compileZ  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 -,rF   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)	rv  rm  functorch.compiler  torch._dynamo.backends.commonr  r  rz  r   )rg   r-   r   r  r  s        r*   __call__OrtBackend.__call__  sX     ==))MB LL@#CCWW 	" " ||L//rF      %_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                 t   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g)NFT)rj  rk  rl  r   rm  ro  rn  )r  r  s     r*   reusable<OrtBackend.get_cached_instance_for_options.<locals>.reusable  s    //13R3RR..!2M2MM00A4Q4QQ''1+?+??%%););;--1K1KK $$0A4I4I4UrF   c              3   \   >#    U  H!  nT" UR                   T5      (       d  M  Uv   M#     g 7frc   )rv  )r   r  rr  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.rE   )r   r   nextr   r  r   r  r   )rr  backendr  s   ` @r*   get_cached_instance_for_options*OrtBackend.get_cached_instance_for_options  s    	) 	.? 	( '#455':7=b:GU
33U

 ?J//0:3X3XX  
 E EFn,<ZL I""X ''..*W:M/MwNrF   c                  @    [         R                  R                  5         g rc   )r   r  clearrE   rF   r*   clear_cached_instances!OrtBackend.clear_cached_instances  s    ##))+rF   c                  4    [        [        R                  5      $ rc   )r   r   r  rE   rF   r*   get_cached_instancesOrtBackend.get_cached_instances  s    Z0011rF   )	r  r  rv  r~  rz  r}  r  r   r,  rc   )rv   rw   rx   ry   rz   r   r   re   r)   r   rf  r   r   r;   r   r   r  r  r  r  r  r   rp  r  liststaticmethodr
   r  r  r  r   rE   rF   r*   r   r     sI   R
): ; R
h!HH00	%WS#X../	0BBL1E1E BLH4-EHH$8$8 4-588CWCW 4-l0!HH000			0$ )*)24eD./4IM4% 17383D DEF4	4 4l , , 2 2rF   r   )rr  rr  c                8    [         R                  U5      " X5      $ rc   )r   r  )r-   r   rr  s      r*   r   r     s     55g>|RRrF   rc   )]dataclassesr"   loggingr6   collections.abcr   r   typingr   r   r   r   r	   r
   typing_extensionsr   r)   torch._C
torch._opstorch._prims.executortorch.fx!torch.onnx._internal._lazy_import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   r0  r   rW   r   r[   r$   r%   r&   ru  torch.onnx._internal.fx.passesr   r   rp  __all__r   r+   r|   r;   r   bytesr   rf  rB   rG   rO   rR   r\   	getLoggerrv   rp   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$  r.  rr   r6  r8  	dataclassrY  r   r   r   r   rE   rF   r*   <module>r
     sb      	 - G G '      ( 4 1 ; < :  606) #'$ &-T -` &( 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	
8P
(PsCxP %,,#$P ./	P
 S/P 5<<$%P /0P P 78P #LL%,,U^^UEMMSWW	
 		P  5sE4/0#56!Pf#
(#sCx# %,,#$# ./	#
 S/# 5<<$%# /0# # 78# #LL%,,U^^UEMMSWW	
 		#  5sE4/0#56!#L> >.[ [| L L LB #(U3S8I3I-J(J"K i K	 d#e,91 91 - $91x e,K2 K2 -K2\ e,
 FJ	S((&&S e-wsCx/@@AB	S -SrF   