
    <Цi(                     "	   % S SK r S SKrS SKrS SKrS SKJrJrJrJrJ	r	J
r
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/\\0   \1S'   / SQr2S\04S jr30 r4\\5\64   \1S'    SOS\7S\\Rp                  Rr                     S\54S jjr:S\\5   4S jr;S\54S jr<S r=S\54S jr>\R~                  " \@5      rA " S S\5      rBS\Rp                  Rr                  SS4S jrCS\\5S4   4S jrDS\Rp                  Rr                  S\\S4   4S jrES\Rp                  Rr                  S\4S jrFS\Rp                  Rr                  S\\5S4   4S  jrGS!\\5S4   S\\5S4   4S" jrHS#\\\R                  \R                  \6\R                  \L\R                  \04   S4   S\S$   4S% jrNS&\\R                  S4   S'\S$   S\\R                  S4   4S( jrOS)\S\R                  4S* jrPS+\\R                  \6\L\04   S,S-S\R                  4S. jrQS)\R                  S/\\R                  \R                  \6\R                  \L\R                  \04   S\\R                  \6\L\04   4S0 jrRS1S2S3\\5S4   S4\\R                  S4   S5\S$   S6\\5S4   S7\\R                  S4   S8\S$   S9\0S:\S;   S<\\\R                  \R                  \6\R                  \L\R                  \04   S4   S\\\R                  \6\L\04   S4   4S= jrSS1S2S3\\5S4   S4\\R                  S4   S5\S$   S6\\5S4   S7\\R                  S4   S8\S$   S9\0S:\S;   S<\\\R                  \R                  \6\R                  \L\R                  \04   S4   S\\\R                  \6\L\04   S4   4S> jrT " S? S@5      rU\ R                   " SA SB5      5       rW\\5\\5\
\5\4   4   4   rX\\1SC'    \ R                  " SDSE9\" SFSG9 " SH SI5      5       5       rY\" SFSG9 " SJ SK5      5       rZ\" SFSG9SSL.S\Rp                  Rr                  SM\\\Y\
\5\4   4      4SN jj5       r[g)P    N)AnyCallableDictFinalListMappingOptionalSequenceSetTuple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        _/var/www/html/ai-image-ml/venv/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getr0   openwritestrgraph)r1   r2   prefixnfilenameffilename_txts          r/   _dump_onnx_modelrG   o   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 rJ       r/   _infer_default_epsrL      s     ###rK   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rM   s    r/   _nvtx_range_pushrT      s2    
 zz  

""4( !rK   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.   rO   rP   rQ   	range_poprJ   rK   r/   _nvtx_range_poprW      s0    
 zz  

!!# !rK   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   rO   cpumaiazUnsupported device type: )onnxruntime.capir   	OrtDevicerO   rZ   npu
ValueError)rX   ORTCs     r/   _get_ort_device_typera      sg    6f~~""$$e~~!!##f~~!!##
0;>
??rK   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)selfre   rf   	__class__s      r/   rj   OrtOperatorSupport.__init__   s    
 	+,".rK   
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   targetrk   loggerinfotyperi   is_node_supportedwarning)rl   ro   rp   rm   s      r/   rx   $OrtOperatorSupport.is_node_supported   s     77++77o%$++9P9P*PKKBT[[!
  7$Z66KKHT[[!
 ZKK	

 rK   )rk   )__name__
__module____qualname____firstlineno____doc__r   r   r   r@   rj   r   r.   nnModulefxNodeboolrx   __static_attributes____classcell__)rm   s   @r/   rc   rc      sc    /SX /4S> /!#uxx"67?Dxx}}	 rK   rc   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)rA   nodesrs   appendprepend)r2   rA   placeholdersfirst_not_placeholderrp   r   s         r/   _move_placeholder_to_frontr      s|     EL 77m#% (TWW-E$(!	 
 $#%%k2 $rK   .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.devicerO   CUDAExecutionProviderrZ   rI   )hasattrr   rw   r   tuple)argsepsargr   s       r/   _infer_ep_from_devicer      se    
C3!!ZZF{{f$

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

12  :rK   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)
rA   r   rs   r   r   
isinstancer.   Tensorr   r   )r2   r   rp   s      r/   _extract_graph_module_inputsr     s|    L""((77m#tV$$$)));!$))E"2ELLAAAA%	 )
 rK   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.)rA   r   rs   r   r_   )r2   rp   s     r/   _extract_graph_module_outputsr     sB    ""((77h 99Q<	 )
 I
JJrK   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   )r2   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)NrI      r   r7   r   rJ   )r   s    r/   get_execution_provider_priority2_sort_eps.<locals>.get_execution_provider_priority.  s    ''(( rK   T)keyreverse)r@   intsetr   sorted)r   r   
unique_epss      r/   	_sort_epsr   +  s3    	C 	C 	 SJ
QUVWWrK   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   rJ   )r   s    r/   _device_id_or_zero-_get_onnx_devices.<locals>._device_id_or_zeroG  s    ~ArK   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)NrZ   r   zUnsupported value type: )r   r.   r   r]   ra   r   rw   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_deviceJ  s    
 eU\\**>>$U\\%6%67--/"5<<#5#56 
 ELL#u~~uemmTR
 
 >>$U+T^^-J-J-La  7#d5k:JJKKrK   c              3   4   >#    U  H  nT" U5      v   M     g 7frh   rJ   ).0r   r   s     r/   	<genexpr>$_get_onnx_devices.<locals>.<genexpr>_  s     TVE8??Vs   r7   )r\   r   r   r   r.   r   r   r   r   r   r   lenr   )r   ort_devicesr`   r   r   s     @@@r/   _get_onnx_devicesr   =  s     7c c LLL%,,U^^UEMMSWW
L 
	L L( 6{QTVTT,Q/11rK   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)r\   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   e  s     7N##%Ic'l#FFI1,,?@fkkm$*+  g&'JrK   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   z  s<    >??
++fkkm6<<
NCJrK   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   )r7   )r   )r   r.   r   r   rw   tensor_typeshapedimsqueezer   r   int64r   float32r   
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&&((rK   
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 
 {{}MrK   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rh   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rh   )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rh   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rh   r  r  s      r/   r   r    r  r  )r    r\   r   rT   r   ziprW   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
 
 	rK   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rh   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rh   )r   r.   
from_numpy)r   r   r  s      r/   r   r!  *  s;      
 #LE	 	'U#	
 	
 #Lr  )r    r   r  OrtValueortvalue_from_numpyrZ   numpyrun)r   r   r   r   r   r   r   r   r   r   r    rM   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)OrtExecutionInfoPerSessioni4  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 rh   r,  r   r   r   r-  r   r   r.  )	rl   r,  r   r   r   r-  r   r   r.  s	            r/   rj   #OrtExecutionInfoPerSession.__init__7  s?     6= -8BS-9CU :G:H  	rK   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   r3  r4  r   r   r  r   r.   r   r   r   r   rw   r   	elem_typer   r   r   	dim_value	dim_paramr   )	rl   r   r3  r4  r   r   
onnx_dtyper   onnx_dims	            r/   is_supported'OrtExecutionInfoPerSession.is_supportedX  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 rK   )r.  r   r   r   r   r   r-  r,  N)r{   r|   r}   r~   r   r   r@   r   r.   r   rj   r:  r   rJ   rK   r/   r+  r+  4  s    a
/
 38_
 !!;<	

 CHo
 ""<=
 23
 34
 uU\\3%67EF
B%rK   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)
"OrtExecutionInfoForAllGraphModulesi  Nc                     0 U l         g rh   execution_info_per_graph_module)rl   s    r/   rj   +OrtExecutionInfoForAllGraphModules.__init__  s    
  	,rK   r2   c                     XR                   ;  a  g U R                   U   nU H  nUR                  " U6 (       d  M  Us  $    g rh   )r@  r:  )rl   r2   r   
candidates	candidates        r/   &search_reusable_session_execution_infoIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_info  sK     CCC 99,G
#I%%t,,   $
 rK   rv   c                     XR                   ;  a  U/U R                   U'   g U R                   U   R                  U5        g rh   )r@  r   )rl   r2   rv   s      r/   cache_session_execution_info?OrtExecutionInfoForAllGraphModules.cache_session_execution_info  s;     CCCBFD00>00>EEdKrK   r?  )r   N)r{   r|   r}   r~   rj   r.   r   GraphModulerE  r+  rH  r   rJ   rK   r/   r=  r=    s@    !HH00 L!HH00L8RLrK   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_transformsrJ   )r{   r|   r}   r~   r   rN  r	   r
   r   __annotations__rO  r   rP  r   rQ  rR  rS  rT  r   r   rJ   rK   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 1rK   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   )r\   r   r(   r*   +torch.onnx._internal.fx.decomposition_tabler   _optionsonnx	_internal_exporter_legacyResolvedExportOptionsrR  ExportOptions_resolved_onnx_exporter_optionsr   r"   '_create_onnx_supports_op_overload_tableonnx_registryrc   _supported_ops_partitioner_cacher=  _all_ort_execution_info_assert_allclose_to_baselineexecution_countr   r   r  r)  r'  )rl   rW  r`   r.   re   rf   s         r/   rj   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 	 . 	rK   r2   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$ )NrJ   r7   r   )r[  rO  r   r   rN  r   rP  rL   r   r@   r   r   )rl   r2   r   inferred_epseps_from_argseps_from_graph_moduleselected_epsr   s           r/   _select_epsOrtBackend._select_epsS  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'
 rK   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O!U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 SS
K"J#n  SSK$J n  URK                  U5      nURM                  U5      nU RP                  RR                  (       a%  U RP                  RR                   H  nU" U5        M     URU                  5       n[V        RX                  R[                  SS5      (       a
  []        UUS9  UR^                  " UU RP                  R`                  U Rb                  " U/UQ76 S9n[e        S URf                  Rh                   5       5      n	[e        S URf                  Rj                   5       5      n
[m        U5      n[o        U[d        5      (       a  [m        U5      nO[m        U45      n[e        S URf                  Rh                   5       5      n[e        S URf                  Rj                   5       5      n[q        UU	UU
UUUUS9nU R                  Rs                  UU5        U =Rt                  S-  sl:        [o        U[v        Rx                  5      nU(       a  U4OUn[o        U[d        5      (       d   e[{        S U 5       5      (       d   e[}        S5        U R#                  UU	UUU
UUU RP                  R&                  UU5
      n[        5         U R                  (       ap  [v        R                  R                  R                  " U/UQ7SS06nU(       a  U4OUn[        UU5       H&  u  nn [v        R                  R                  UU 5        M(     U(       a  US   $ U$ ! [2         a    [4        R7                  SU5        SU l        e f = f! [N         a    [4        R7                  S5         GN=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$rK   zFakeTensorProb failed for %s)diagnostic_context)fx_graph_moduleonnxfunction_dispatcher)opset_version)	optimizer)r    zONNXScript optimizer is not available. Skipping optimization. Please `pip install onnxscript -U` to enable post-export optimization.r4   )r2   )path_or_bytessess_options	providersc              3   8   #    U  H  oR                   v   M     g 7frh   rS   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rh   rS   r   r   s     r/   r   r    s      S;R;Rr  c              3   $   #    U  H  ov   M     g 7frh   rJ   r~  s     r/   r   r    s     %P9Oe9O   c              3   $   #    U  H  ov   M     g 7frh   rJ   r  s     r/   r   r    s     &T<S&v<Sr  r0  r7   c              3      #    U  H4  n[        U[        R                  [        R                  [        45      v   M6     g 7frh   )r   r.   r   r   r   )r   elems     r/   r   r  (  s0      
/ tellELL#>??/s   <>$run_onnx_session_with_ortvaluevectorexecutoraten)Gr    r,   r#   r$   rf  rE  r,  r   r   r   r-  r   r   r.  MovePlaceholderToFrontra  ru  r'  dynamic_shapesr   r   r   tree_mapr   	propagate	Exceptionru   ry   FxOnnxInterpreterInsertTypePromotionrw  to_model_protorc  rx  r!   ry  onnxscript.rewriteroptimizerewriter-   r[  rT  SerializeToStringr;   r<   r=   rG   InferenceSessionrS  ro  r   rA   r  r   r   r   r+  rH  rh  r.   r   allrT   rW   rg  _primsr  executer  testingassert_close)!rl   r2   r   kwargsr    r#   r$   !cached_execution_info_per_sessiononnx_sessionr   r   r   r-  r   r   prim_outputsextracted_outputsrs  fx_interpreterexported
onnx_modelry  ort_rewriter	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_callt  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0 '//
;
)11*=
 }}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|Ks ! NN#A<P /4D+ N  ]s   S1 .T 1)TU ?U 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  re  rd  partition_and_fuserA   r   rs   rM   rY  r  _wrapped_call)	rl   r2   r   r  partitioned_prim_graph_moduleprim_graph_modulepartitionerrp   fused_modules	            r/   compileOrtBackend.compileK  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 -,rK   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)	r[  rQ  functorch.compiler  torch._dynamo.backends.commonr  r  ra  r"   )rl   r2   r   r  r  s        r/   __call__OrtBackend.__call__  sX     ==))MB LL@#CCWW 	" " ||L//rK      %_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)rN  rO  rP  r   rQ  rT  rS  rR  r  diagnostic_optionsrc  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 rK   c              3   \   >#    U  H!  nT" UR                   T5      (       d  M  Uv   M#     g 7frh   )r[  )r   r  rW  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.rJ   )r   r   nextr   r  r   r  r   )rW  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rK   c                  @    [         R                  R                  5         g rh   )r   r  clearrJ   rK   r/   clear_cached_instances!OrtBackend.clear_cached_instances  s    ##))+rK   c                  4    [        [        R                  5      $ rh   )r   r   r  rJ   rK   r/   get_cached_instancesOrtBackend.get_cached_instances  s    Z0011rK   )	rf  rg  r[  re  ra  rd  rh  r   r'  rh   )r{   r|   r}   r~   r   r	   r   rj   r.   r   rJ  r
   r   r@   r   r   ro  r  r  r  r  r   rU  r  r   staticmethodr   r  r  r  r   rJ   rK   r/   r   r     sM   V
): ; V
p!HH00	%WS#X../	0BUL1E1E ULn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 2rK   r   )rW  rW  c                8    [         R                  U5      " X5      $ rh   )r   r  )r2   r   rW  s      r/   r   r     s     55g>|RRrK   rh   )\dataclassesr&   loggingr;   typingr   r   r   r   r   r   r	   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   r\  r    r\   r   r`   r(   r)   r*   r+   rZ  torch.onnx._internal.fx.passesr   r   rU  __all__r   r0   r@   r   bytesr   rJ  rG   rL   rT   rW   ra   	getLoggerr{   ru   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r)  r+  	dataclassr=  r   r   r   r   rJ   rK   r/   <module>r     sb      	    (      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,q2 q2 -q2h e,
 FJ	S((&&S e-wsCx/@@AB	S -SrK   