
    Ph                        U d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lm Z  	 d dl!Z!d dl"Z"d d
l#m$Z%  ejL                  d       d dl'Zd dl(Zd dl)Zd dl*Zd dl+Zd dl,Zd dl-m.Z. d dl/m0Z0m1Z1 dZ2g dZ4de5fdZ6de
e7   fdZ8de7fdZ9d Z:de7fdZ; ejx                  e=      Z> G d de      Z?dej                  j                  ddfdZBdej                  j                  ddfdZCdee7df   fd ZDdej                  j                  deedf   fd!ZEdej                  j                  defd"ZFdej                  j                  dee7df   fd#ZGd$ee7df   dee7df   fd%ZHd&eej                  df   ded'   fd(ZJd)eej                  df   d*ed'   deej                  df   fd+ZKd,edej                  fd-ZLd.d/d0ee7df   d1eej                  df   d2ed'   d3ee7df   d4eej                  df   d5ed'   d6e5deej                  df   fd7ZMd.d/d0ee7df   d1eej                  df   d2ed'   d3ee7df   d4eej                  df   d5ed'   d6e5deej                  df   fd8ZN G d9 d:      ZOe j                   G d; d<             ZQee7ee7ee7ef   f   f   ZReeSd=<   	  e j                  d>       ed?       G d@ dA                    ZT ed?       G dB dC             ZU ed?      ddDdej                  j                  dEe	eeTee7ef   f      fdF       ZVy# e3$ r dZ2Y w xY w)G    N)
AnyDictFinalListMappingOptionalSequenceSetTupleUnion)	TypeAlias)
FakeTensor)compatibility)FakeTensorProp)OperatorSupport)CALLABLE_NODE_OPS)_pytree)_pybind_state
onnxscript)fx_onnx_interpreter)_TORCH_DTYPE_TO_NUMPY_DTYPE(_TORCH_DTYPE_TO_ONNX_TENSOR_ELEMENT_TYPETF)is_onnxrt_backend_supportedtorch_compile_backendOrtExecutionProviderOrtBackendOptions
OrtBackendreturnc                      t         S )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")
        ...
    )_SUPPORT_ONNXRT     kC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/onnx/_internal/onnxruntime.pyr   r   C   s
    " r"   c                      dgS )NCPUExecutionProviderr!   r!   r"   r#   _infer_default_epsr&   W   s     ###r"   namec                     t         j                  j                         r*t         j                  j                  j	                  |        yy)zIf PyTorch is installed with CUDA support, this starts NVTX range.

    Check torch.cuda.nvtx.range_push's document for more details.
    N)torchcudais_availablenvtx
range_pushr'   s    r#   _nvtx_range_pushr/   ]   s/    
 zz 

""4( !r"   c                      t         j                  j                         r)t         j                  j                  j	                          yy)zIf PyTorch is installed with CUDA support, this terminates NVTX range.

    Check torch.cuda.nvtx.range_pop's document for more details.
    N)r)   r*   r+   r,   	range_popr!   r"   r#   _nvtx_range_popr2   f   s-    
 zz 

!!# !r"   device_typec                     | dk(  rt         j                  j                         S | dk(  rt         j                  j                         S | dk(  rt         j                  j	                         S t        d| z         )Nr*   cpuortzUnsupported device type: )ORTC	OrtDevicer*   r5   npu
ValueError)r3   s    r#   _get_ort_device_typer;   o   sd    f~~""$$e~~!!##e~~!!##
0;>
??r"   c                        e Zd ZdZdee   deeef   f fdZde	ee
j                  j                  f   de
j                  j                  def fdZ xZS )	OrtOperatorSupporta0  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                 2    t         |   |       || _        y N)super__init___onnx_support_dict)selfr>   r?   	__class__s      r#   rC   zOrtOperatorSupport.__init__   s    
 	+,".r"   
submodulesnoder   c                 >   |j                   t        vry|j                   dk(  rM|j                  | j                  v r5t        j                  d|j                  t        |j                               yt        j                  d|j                  t        |j                               t        | !  ||      r5t        j                  d|j                  t        |j                               yt        j                  d|j                  t        |j                               y)NFcall_functionz0support_dict supports node.target: %s (type: %s)Tz7support_dict doesn't support node.target: %s (type: %s)z6extra_support_dict supports node.target: %s (type: %s)z>extra_support_dict doesn't supports node.target: %s (type: %s))	opr   targetrD   loggerwarningtyperB   is_node_supported)rE   rG   rH   rF   s      r#   rP   z$OrtOperatorSupport.is_node_supported   s     77++77o%$++9P9P*PNNBT[[!
 EKK	
 7$Z6NNHT[[!
 LKK	

 r"   )__name__
__module____qualname____doc__r
   r   r   strrC   r   r)   nnModulefxNodeboolrP   __classcell__)rF   s   @r#   r=   r=      sc    /SX /4S> /$!#uxx"67$?Dxx}}$	$ $r"   r=   graph_modulec                     | j                   }g }d}|j                  D ]7  }|j                  dk(  r|j                  |       |&|j                  dk7  s6|}9 |y|D ]  }|j	                  |        y)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)graphnodesrK   appendprepend)r\   r_   placeholdersfirst_not_placeholderrH   r^   s         r#   _move_placeholder_to_frontre      s|     EL 77m#% (TWW-E$(!	 
 $#%%k2 $r"   	fx_modulec                    | j                   j                  D ]  }t        |j                  t        j
                  j                        s3|j                  j                  t        j                  j                  j                  k(  sod}d}d}d}d|j                  v r"|j                  d   t        j                  k7  rd}d|j                  v r8|j                  d   |j                  d   j                  d   j                  k7  rd}d|j                  vrd}|j                  D ]	  }|dvsd} t!        |j                        d	k(  rS|rQ|rO|rM|rKd|j                  d   i|_        t        j                  j                  j"                  j$                  |_        t'        d
|j                  D cg c]  }|j                   c} d|j                          | j)                          y c c}w )NTlayoutFdevicer   valdtype)rh   ri   rk      zaaten._to_copy must be replaced with other ONNX-supported aten ops.                          args=z	, kwargs=)r_   r`   
isinstancerL   r)   _ops
OpOverloadoverloadpacketopsaten_to_copykwargsstridedargsmetari   lentork   RuntimeError	recompile)rf   rH   is_default_layoutis_on_same_deviceis_castare_kwargs_supportedkwargargs           r#   _replace_to_copy_with_tor      s   %%t{{EJJ$9$9:**eiinn.E.EE $ $G#' 4;;&4;;x+@EMM+Q$)!DKK'KK)TYYq\->->u-E-L-LL$)!dkk) ==+0( %
 DII!#%%(
  'G(<=#iinn//55"48II>ISI>?yW I &P   ?s   +G&.c                      g }| D ]]  }t        |d      s|j                  }|j                  dk(  r|j                  d       =|j                  dk(  sM|j                  d       _ t	        |      S )zBReturn the first valid device (i.e., GPU or CPU) in argument list.ri   r*   CUDAExecutionProviderr5   r%   )hasattrri   rO   ra   tuple)rv   epsr   ri   s       r#   _infer_ep_from_devicer      sb    
C3!ZZF{{f$

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

12  :r"   c                    g }| j                   j                  D ]f  }|j                  dk(  st        |d      r7d|j                  v r)t        |j                  d   t        j                        sJ |j                  |       h t        |      S )Nr^   rw   rj   )
r_   r`   rK   r   rw   rm   r)   Tensorra   r   )r\   rc   rH   s      r#   _extract_graph_module_inputsr     sv    L""((77m#tV$$)));!$))E"2ELLAAA%	 )
 r"   c                     | j                   j                  D ]"  }|j                  dk(  s|j                  d   c S  t	        d      )zHCollect "val" fields from outputs metadata in this torch.fx.GraphModule.outputr   z2No output node found in this torch.fx.GraphModule.)r_   r`   rK   rv   r:   )r\   rH   s     r#   _extract_graph_module_outputsr     sB    ""((77h 99Q<	 )
 I
JJr"   c                     t        j                  t        |             \  }}|D cg c]+  }t        |d      rd|j                  v r|j                  d   - }}t        | S c c}w )z[Return the all valid devices (i.e., GPU or CPU) among outputs of this torch.fx.GraphModule.rw   rj   )r   tree_flattenr   r   rw   r   )r\   flattened_output_args_
output_argselected_output_argss        r#   _infer_ep_from_graph_moduler     su    &33%l3 1 0/J J'EZ__,D	 	/   !"677s   0A r   c                 f    dt         dt        fd}t        |       }t        t	        ||d            S )z:Sort execution providers in eps based on pre-set priority.epr   c                     | dk(  ry| dk(  ryy)Nr%      r   rl   r   r!   )r   s    r#   get_execution_provider_priorityz2_sort_eps.<locals>.get_execution_provider_priority-  s    ''(( r"   T)keyreverse)rU   intsetr   sorted)r   r   
unique_epss      r#   	_sort_epsr   *  s6    	C 	C 	 SJ
(GQUVWWr"   valueszORTC.OrtDevice.c                      t         fd D              sJ d       dt        dt        fdt        fd D              }|S )Nc              3   V   K   | ]   }|j                   d    j                   k(   " yw)r   N)ri   ).0valuer   s     r#   	<genexpr>z$_get_onnx_devices.<locals>.<genexpr>=  s(      6<Uq	(((fs   &)z&All values must be on the same device.	device_idr   c                     | xs dS )Nr   r!   )r   s    r#   _device_id_or_zeroz-_get_onnx_devices.<locals>._device_id_or_zeroA  s    ~Ar"   c              3      K   | ]n  }t        j                  t        |j                  j                        t         j                  j                          |j                  j                               p y wrA   )r7   r8   r;   ri   rO   default_memoryindex)r   r   r   s     r#   r   z$_get_onnx_devices.<locals>.<genexpr>D  sZ      2 E 	 !2!23NN))+u||112	

 s   A4A7)allr   r   )r   devicesr   s   ` @r#   _get_onnx_devicesr   <  s_     6<  0/0 c c  -2 2 2 -G Nr"   tensorsr   c                 f   t        j                         }|j                  t        |              g }g }g }| D ]b  }|j	                  t
        |j                            |j	                  |j                                |j	                  |j                                d |j                  | ||||       |S rA   )
r7   OrtValueVectorreserverx   ra   r   rk   sizedata_ptrpush_back_batch)r   r   	ortvaluesdtypesshapes	data_ptrstensors          r#   !_get_ortvalues_from_torch_tensorsr   O  s     ##%Ic'l#FFI1&,,?@fkkm$*+  gy&&'Jr"   r   c                     | j                   rt        d      t        j                  | j	                         | j
                  | j                        }|S )Nz#sparse tensor is not yet supported.)rk   ri   )	is_sparser:   r)   emptyr   rk   ri   )r   outs     r#   _to_real_tensorr   `  s<    >??
++fkkm6<<
NCJr"   sessonnxruntime.InferenceSessioninput_namesinputsinput_devicesoutput_namesoutputsoutput_devicespreallocate_outputc                 :   t        d       t        d |D              }t                t        d       t        ||      }|rt        d |D              }	t        |	|      }
nt	        j
                         }
t                t        d       t        j                         }|j                  dd       | j                  |||||
|       t                |r	S t        d       t        j                  j                  j                  j                  |
      }	t                |	S )	N
contiguousc              3   <   K   | ]  }|j                           y wrA   )r   )r   as     r#   r   z8_run_onnx_session_with_ortvaluevector.<locals>.<genexpr>r  s     26a1<<>6s   r   c              3   V   K   | ]!  }t        |t              rt        |      n| # y wrA   )rm   r   r   )r   ts     r#   r   z8_run_onnx_session_with_ortvaluevector.<locals>.<genexpr>}  s'      
LSq*Q
";OABGs   ')run_with_ortvaluevector'disable_synchronize_execution_providers1zafter run_with_ortvaluevector)r/   r   r2   r   r7   r   onnxruntime
RunOptionsadd_run_config_entryr   training	ortmodule_utils_ortvalues_to_torch_tensor)r   r   r   r   r   r   r   r   
ort_inputspth_outputsort_outputsrun_optionss               r#   %_run_onnx_session_with_ortvaluevectorr   g  s    \"2622F&'26=IJ
  
LS
 
 8^T))+./((*K$$%NPST  [*lK 89!**44;;VV
 	r"   c           	         t        ||      D 	ci c]A  \  }}	|t        j                  j                  |	j	                         j                               C }
}}	| j                  ||
      }t        d t        ||      D              }|S c c}	}w )Nc              3   ~   K   | ]5  \  }}t        j                  |      j                  |j                         7 y wrA   )r)   
from_numpyry   ri   )r   r   r   s      r#   r   z/_run_onnx_session_with_fetch.<locals>.<genexpr>  s6      6ME6 	""6==16s   ;=)zipr   OrtValueortvalue_from_numpyr5   numpyrunr   )r   r   r   r   r   r   r   r   r'   r   feedr   r   s                r#   _run_onnx_session_with_fetchr     s      V44LD& 	k""66vzz|7I7I7KLL4 	  ((<.K  g6 K s   AB	c                       e Zd ZdZdddeedf   ded   deedf   d	ed   d
ed   ded   deeej                  df   ej                  f   fdZ	d Z
y)OrtExecutionInfoPerSessionzWInformation required to execute torch.fx.GraphModule using onnxruntime.InferenceSessionsessionr   r   .input_value_infos)zonnx.ValueInfoProto.r   output_value_infosr   r   r   example_outputsc	                 t    || _         || _        || _        || _        || _        || _        || _        || _        y rA   r   r   r   r   r   r   r   r   )	rE   r   r   r   r   r   r   r   r   s	            r#   rC   z#OrtExecutionInfoPerSession.__init__  sM     6= -8BS-9CU <I<J
  	r"   c                 b   t        |      t        | j                        k7  ryt        || j                        D ]  \  }}t        |t        j
                        s yt        |j                     }||j                  j                  j                  k7  r yt        |j                  |j                  j                  j                  j                        D ]Z  \  }}t        |t              r|j                  |k(  s|j                  r2t        |t        j                         r|j                  rY  y  yNFT)rx   r   r   rm   r)   r   r   rk   rO   tensor_type	elem_typeshapedimr   	dim_value	dim_paramSymInt)rE   rv   r   
value_info
onnx_dtyper   onnx_dims          r#   is_supportedz'OrtExecutionInfoPerSession.is_supported  s     t9D2233"4)?)?@OCc5<<0A#))LJZ__88BBB!$SYY
0K0K0Q0Q0U0U!VXc3'&&#-1C1CU\\2x7I7I  "W  A r"   N)rQ   rR   rS   rT   r   rU   r   r)   r   rC   r   r!   r"   r#   r   r     s    a/ 38_ !!;<	
 CHo ""<= 23 34 uU\\3%67EFBr"   r   c                   ~    e Zd Zd Zdej
                  j                  fdZdej
                  j                  defdZ	y)"OrtExecutionInfoForAllGraphModulesc                     i | _         y rA   )execution_info_per_graph_module)rE   s    r#   rC   z+OrtExecutionInfoForAllGraphModules.__init__  s    
  	,r"   r\   c                 t    || j                   vry | j                   |   }|D ]  } |j                  | s|c S  y rA   )r  r   )rE   r\   rv   
candidates	candidates        r#   &search_reusable_session_execution_infozIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_info  sL     tCCC 99,G
#I%y%%t,   $
 r"   infoc                 ~    || j                   vr|g| j                   |<   y | j                   |   j                  |       y rA   )r  ra   )rE   r\   r	  s      r#   cache_session_execution_infoz?OrtExecutionInfoForAllGraphModules.cache_session_execution_info  s=     tCCCBFD00>00>EEdKr"   N)
rQ   rR   rS   rC   r)   rX   GraphModuler  r   r  r!   r"   r#   r  r    s@    !HH00 L!HH00L8RLr"   r  r   )frozen)is_backward_compatiblec                       e Zd ZU dZdZeee      ed<   	 dZ	e
ed<   	 dZeee      ed<   	 dZe
ed<   	 dZe
ed	<   	 dZed
   ed<   	 dZed   ed<   y)r   aJ  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_options)rQ   rR   rS   rT   r  r   r	   r   __annotations__r  rZ   r  r   r  r  r  r!   r"   r#   r   r     s     OS!8H5I,J#KR '+t*mLP(3G*H!IP  %$V!d! <@NH78?UBF">?FVr"   r   c            	          e Zd ZU dZddee   fdZdej                  j                  de
eeeeef   f      fdZdej                  j                  fdZdej                  j                  dej                  j                  fd	Zdej                  j                  dej                  j                  fd
ZdZeed<   g Zeed       ed<   e	 ddeeeeeef   f      dd fd       Zed        Zed        Zy)r   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                    |
t               n|| _        t        j                  j                  j
                  j                  | j                  j                  t        j                  j                         n| j                  j                        | _	        t        j                  j                  j                  j                  j                  | j                  j                        }d d d}t        ||      | _        i | _        t#               | _        d| _        d| _        t+        t,        d      rt.        | _        y t0        | _        y )N)getattrz_operator.getitemFr   r   )r   _optionsr)   onnx	_internalexporterResolvedExportOptionsr  ExportOptions_resolved_onnx_exporter_optionsrX   decomposition_table'_create_onnx_supports_op_overload_tableonnx_registryr=   _supported_ops_partitioner_cacher  _all_ort_execution_info_assert_allclose_to_baselineexecution_countr   r7   r   r   r   )rE   r  r>   r?   s       r#   rC   zOrtBackend.__init__^  s   6=o027  JJ  ))??==//7 

((*]]11 	,& zz++..BBjj00>>

 !%.

 1?QR UW (J'K$,1) 
 t./ 2 	 . 	r"   r\   r   c                    t               }| j                  j                  rt        | x}r|}nt	        |      x}r|}g }g | j                  j
                  xs g t        |      | j                  j                  xs
 t               D ]L  }t        |t              r|i f}nt        |t               r|d   |d   i f}|7||vs<|j                  |       N |S )Nrl   r   )r   r  r  r   r   r  r   r  r&   rm   rU   ra   )rE   r\   rv   inferred_epseps_from_argseps_from_graph_moduleselected_epsr   s           r#   _select_epszOrtBackend._select_eps  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[~"L"8##B'
 r"   c           
      H	    | j                   j                  |g| }|rJ|j                  }|j                  }|j                  }|j
                  }|j                  }	|j                  }
nt        j                  j                  j                  j                  j                  | j                  j                  |      j!                         }| j                  j"                  r,d| _        t'        |      }d }t)        j*                  ||      }
n	  t-        |      j.                  |i |}
t7        j8                  | j                  j                        }t        j                  j                  j                  j                  j;                  | j                  j                  |      j!                         }|j!                  || j                  j<                  | j                  j>                        }|jA                  | j                  jB                  jD                        }tG        jH                  |jK                         | jL                  jN                   | jP                  |g|       }tS        d |jT                  jV                  D              }tS        d	 |jT                  jX                  D              }t[        |      }t]        |
tR              rt[        |
      }	nt[        |
f      }	t_        ||tS        d
 |jT                  jV                  D              |tS        d |jT                  jX                  D              ||	|
      }| j                   ja                  ||       | xjb                  dz  c_1        t]        |
t        jd                        }|r|
fn|
}t]        |tR              sJ tg        d |D              sJ ti        d       | j!                  |||||||	| jL                  j$                        }tk                | jl                  rht        jn                  jp                  jr                  |g|ddi}|r|fn|}tu        ||      D ]%  \  }}t        jv                  jy                  ||       ' |r|d   S |S # t0        $ r t2        j5                  d|       d| _         w xY w)a  This 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.
        Fc                 X    t        | d      rd| j                  v r| j                  d   S | S )Nrw   rj   )r   rw   )r   s    r#   maybe_map_to_meta_valz>OrtBackend._ort_acclerated_call.<locals>.maybe_map_to_meta_val  s-    uf-%5::2E  %zz%00$r"   zFakeTensorProb failed for %s)diagnostic_context)fx_graph_moduleonnxfunction_dispatcherop_level_debug)opset_version)path_or_bytessess_options	providersc              3   4   K   | ]  }|j                     y wrA   r.   r   inputs     r#   r   z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>1  s     O8Nu

8N   c              3   4   K   | ]  }|j                     y wrA   r.   r   r   s     r#   r   z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>2  s      S;R;Rr>  c              3       K   | ]  }|  y wrA   r!   r<  s     r#   r   z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>?  s     'R;Q%;Q   c              3       K   | ]  }|  y wrA   r!   r@  s     r#   r   z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>A  s     (V>UF>UrB  r   rl   c              3   P   K   | ]  }t        |t        j                           y wrA   )rm   r)   r   )r   elems     r#   r   z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>U  s     V>Ud:dELL1>Us   $&$run_onnx_session_with_ortvaluevectorexecutorrr   r   )=r'  r  r   r   r   r   r   r   r)   r  r  rX   passesMovePlaceholderToFrontr!  r3  r   dynamic_shapesr   r   r   tree_mapr   	propagate	ExceptionrM   rN   r   FxOnnxInterpreterInsertTypePromotionr5  r6  to_model_protor$  r7  r   InferenceSessionSerializeToStringr  r  r/  r   r_   r=  r   r   rm   r   r  r)  r   r   r/   r2   r(  _primsrG  executer   testingassert_close)rE   r\   rv   rt   !cached_execution_info_per_sessiononnx_sessionr   r   r   r   prim_outputsextracted_outputsr2  fx_interpreterexported
onnx_modelexecution_info_per_sessionis_single_tensor_outputnormalized_prim_outputsonnx_outputsbaseline_outputsnormalized_baseline_ouptutsonnx_outputbaseline_outputs                           r#   _ort_acclerated_callzOrtBackend._ort_acclerated_call  sW    PD((OO# 	*
 -<DDL;GGK<IIL=KKM>MMN<LLL
 !:://2299PP44GG ce  33BB*/'$A,$O!%  '//)+< #I>,#?#I#I$!'$L 1BB#'#G#G#Z#ZN !:://2299MM44GGce 
 &)) ,(,(L(L(d(d#CCRR * H "00"BBPP^^ 1 J '77(::<!]]>>*$**<?$?L  O
8H8H8N8NOOK  S:;K;K;R;R SSL-d3M ,.!2<!@!2L?!C)C$'"''R:;K;K;Q;Q'R"R)#((Vj>N>N>U>U(V#V+- ,	*& ((EE8 	!
 #-\5<<"H6\OL 	  15999V>UVVVV?@xx#MM,,	
 	,,$||44<< # .4  (?!#DT ( 1491,_ **;H1 #:|AK|Ka ! NN#A<P /4D+ s   Q9 9(R!c                    ddl m} || j                  v r| j                  |   }|S |}t        |        ||| j                  d      }|j                         }|| j                  |<   |j                  j                  D ]H  }|j                  dk(  sd|j                  v s"t        ||j                        }| j                  |_        J |S )Nr   )CapabilityBasedPartitionerT)allows_single_node_partitioncall_modulefused_)!torch.fx.passes.infra.partitionerrh  r&  r   r%  partition_and_fuser_   r`   rK   r'   r  rf  _wrapped_call)	rE   r\   rv   rh  partitioned_prim_graph_moduleprim_graph_modulepartitionerrH   fused_modules	            r#   compilezOrtBackend.compiler  s    
 	Q, 4222,0,C,CL,Q)6 -,3 !- %%674!##-1K
 -8,J,J,L)4QD##L1 6;;AA 77m+DII0E#*+H$))#TL 261J1JL. B -,r"   c                     | j                   j                  r<ddlm} ddlm}   || j                  || j                  j                        ||      S | j                  ||      S )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  r  functorch.compileru  torch._dynamo.backends.commonrv  rs  r!  r"  )rE   r\   rv   ru  rv  s        r#   __call__zOrtBackend.__call__  s`     ==))MB< LL@#CCWW D	" " ||L$//r"      %_OrtBackend__instance_cache_max_count_OrtBackend__instance_cachec                     dt         dt         fdt         t               st        d
i  xs i  t         fdt        j                  D        d      }|{t        t        j                        t        j                  k  s'J dt        j                   dt         dt         d	       t        j                  j                  t               x}       |S )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.r   bc                 b   | j                   |j                   k7  sd| j                  |j                  k7  sK| j                  |j                  k7  s2| j                  |j                  k7  s| j                  |j                  k7  ry| j
                  |j
                  y| j                  |j                  u ry| j                  |j                  | j                  j                  |j                  j                  k(  xr | j                  j                  |j                  j                  k(  xr | j                  j                  |j                  j                  k(  xrZ | j                  j                  |j                  j                  u xr, | j                  j                  |j                  j                  u S yr   )r  r  r  r   r  r  r  rJ  r6  diagnostic_optionsr$  fake_context)r   r  s     r#   reusablez<OrtBackend.get_cached_instance_for_options.<locals>.reusable  s   //13R3RR..!2M2MM00A4Q4QQ''1+?+??%%););; $$0A4I4I4U1#3#33 +0@0@0L$$33q7G7G7V7VV W((77''667W ((;;''::;W
 ((66!:J:J:X:XXW ((559I9I9V9VV r"   c              3   J   K   | ]  } |j                         s|  y wrA   )r  )r   r  r  r  s     r#   r   z=OrtBackend.get_cached_instance_for_options.<locals>.<genexpr>  s!     U31x

G7TQ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.r!   )r   rm   nextr   r  rx   r~  ra   )r  backendr  s   ` @r#   get_cached_instance_for_optionsz*OrtBackend.get_cached_instance_for_options  s    "	) "	.? "	H '#45':7=b:GU
33U

 ?J//0:3X3XX  
 E EFn,<ZL I""X ''..*W:M/MwNr"   c                  @    t         j                  j                          y rA   )r   r  clearr!   r"   r#   clear_cached_instancesz!OrtBackend.clear_cached_instances  s    ##))+r"   c                  4    t        t        j                        S rA   )r   r   r  r!   r"   r#   get_cached_instanceszOrtBackend.get_cached_instances  s    Z0011r"   rA   )rQ   rR   rS   rT   r   r   rC   r)   rX   r  r	   r   rU   r   r   r/  rf  rs  r|  r~  r   r  r  r   staticmethodr   r  r  r  r!   r"   r#   r   r   S  sM   I
): ; I
V!HH00	%WS#X../	0BfL1E1E fLP7-EHH$8$8 7-588CWCW 7-r0!HH000			0& )*)24eD./4IMD% 17383D DEFD	D DL , , 2 2r"   r   )r  r  c                8    t        j                  |      | |      S rA   )r   r  )r\   rv   r  s      r#   r   r     s     55g>|TRRr"   )Wdataclasses	importlibloggingtypingr   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   onnxruntime.capir   r7   import_module
torch.onnxtorch.onnx._internal torch.onnx._internal.diagnosticstorch.onnx._internal.exporter+torch.onnx._internal.fx.decomposition_tabletorch.onnx._internal.fx.passestorch.onnx._internal.fxr   "torch.onnx._internal.fx.type_utilsr   r   r    ImportError__all__rZ   r   rU   r&   r/   r2   r;   	getLoggerrQ   rM   r=   rX   r  re   r   r   r   r   r   r   r   r   r   r   r   r   r   	dataclassr  r   r  r   r   r   r!   r"   r#   <module>r     s        (      4 1 ; < : 6 IL)+(6);
 OT ($HSM $)3 )$@c @ 
		8	$4 4n3UXX-A-A 3d 3**(<(< * *Z
E#s(O 
uxx/C/C cSVh K0D0D K K8ehh.B.B 8uSRUX 8 X5c? XuS#X X$eELL#$56 5AV;W &5<<$%056K0L
5<<"J 5<< .
(.sCx. %,,#$. ./	.
 S/. 5<<$%. /0. . 5<<.b
(sCx %,,#$ ./	
 S/ 5<<$% /0  5<<,8 8v L L LB #(U3S8I3I-J(J"K i K	 d#e,6W 6W - $6Wr e,z2 z2 -z2z e,
 FJ	S((&&S e-wsCx/@@AB	S -Sw  Os   %AM MM