o
    ,i                     @   s  U d dl Z 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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" z?d dl#Z#d dl$Z$d d
l%m&Z' e(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/m0Z0 d dl1m2Z2m3Z3m4Z4 dZ5W n e6y   dZ5Y nw g dZ7de8fddZ9i Z:ee;e<f e=d< 	dede>deej?j@ de;fddZAdee; fddZBde;fddZCdd ZDd e;fd!d"ZEeFeGZHG d#d$ d$eZIdej?j@ddfd%d&ZJdee;d'f fd(d)ZKdej?j@deed'f fd*d+ZLdej?j@defd,d-ZMdej?j@dee;d'f fd.d/ZNd0ee;d'f dee;d'f fd1d2ZOd3eeejPejQe<ejReSejTe8f d'f ded4 fd5d6ZUd7eejPd'f d8ed4 deejPd'f fd9d:ZVd;edejPfd<d=ZWd>eejPe<eSe8f d?d@dejPfdAdBZXd;ejPdCeejPejQe<ejReSejTe8f deejPe<eSe8f fdDdEZYdFdGdHee;d'f dIeejPd'f dJed4 dKee;d'f dLeejPd'f dMed4 dNe8dOedP dQeeejPejQe<ejReSejTe8f d'f deeejPe<eSe8f d'f fdRdSZZdFdGdHee;d'f dIeejPd'f dJed4 dKee;d'f dLeejPd'f dMed4 dNe8dOedP dQeeejPejQe<ejReSejTe8f d'f deeejPe<eSe8f d'f fdTdUZ[G dVdW dWZ\e j]G dXdY dYZ^ee;ee;e
e;ef f f Z_ee=dZ< 	 e j]dd[edd\G d]d^ d^Z`edd\G d_d` d`Zaedd\ddadej?j@dbeee`e
e;ef f  fdcddZbdS )f    N)AnyCallable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_TYPE,from_python_type_to_onnx_tensor_element_typeTF)is_onnxrt_backend_supportedtorch_compile_backendOrtExecutionProviderOrtBackendOptions
OrtBackendreturnc                   C   s   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 r!   r!   ^/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/onnx/_internal/onnxruntime.pyr   F   s   r   _dumped_onnx_modelmodel_stringgraph_modulec                 C   s   t jdd}|sdS t|dd }| | d}t|d}||  W d   n1 s/w   Y  |t|< |durc| | d}t|d	d
d}|t|j W d   |S 1 s^w   Y  |S )a  Stores 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_txtr!   r!   r"   _dump_onnx_model]   s"   

r9   c                   C   s   dgS )NCPUExecutionProviderr!   r!   r!   r!   r"   _infer_default_epsv   s   r;   namec                 C   s    t j rt jj|  dS dS )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<   r!   r!   r"   _nvtx_range_push|   s   
rC   c                   C   s   t j rt jj  dS dS )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!   r"   _nvtx_range_pop   s   
rE   device_typec                 C   sB   | dkr	t j S | dkrt j S | dkrt j S td|  )Nr>   cpumaiazUnsupported device type: )ORTC	OrtDevicer>   rG   npu
ValueError)rF   r!   r!   r"   _get_ort_device_type   s   


rM   c                       s`   e Zd ZdZdee deeef f fddZde	ee
jjf de
jjdef fd	d
Z  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                    s   t  | || _d S N)super__init___onnx_support_dict)selfrO   rP   	__class__r!   r"   rS      s   
zOrtOperatorSupport.__init__
submodulesnoder   c                    s   |j tvrdS |j dkr|j| jv rtd|jt|j dS t ||r3td|jt|j dS t	d|jt|j dS )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   targetrT   loggerinfotyperR   is_node_supportedwarning)rU   rX   rY   rV   r!   r"   r`      s,   
z$OrtOperatorSupport.is_node_supported)__name__
__module____qualname____doc__r
   r   r   r2   rS   r   r=   nnModulefxNodeboolr`   __classcell__r!   r!   rV   r"   rN      s    "rN   c                 C   sh   | j }g }d}|jD ]}|jdkr|| |du r!|jdkr!|}q
|du r(dS |D ]}|| q*dS )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)r3   nodesr[   appendprepend)r%   r3   placeholdersfirst_not_placeholderrY   rl   r!   r!   r"   _move_placeholder_to_front   s   


rr   .c                  G   sP   g }| D ]}t |dr#|j}|jdkr|d q|jdkr#|d qt|S )zBReturn the first valid device (i.e., GPU or CPU) in argument list.devicer>   CUDAExecutionProviderrG   r:   )hasattrrs   r_   rn   tuple)argsepsargrs   r!   r!   r"   _infer_ep_from_device   s   



rz   c                 C   sX   g }| j jD ]!}|jdkr't|dr"d|jv r"t|jd tjs"J || qt	|S )Nrl   metaval)
r3   rm   r[   ru   r{   
isinstancer=   Tensorrn   rv   )r%   rp   rY   r!   r!   r"   _extract_graph_module_inputs   s   

r   c                 C   s.   | j jD ]}|jdkr|jd   S qtd)zHCollect "val" fields from outputs metadata in this torch.fx.GraphModule.outputr   z2No output node found in this torch.fx.GraphModule.)r3   rm   r[   rw   rL   )r%   rY   r!   r!   r"   _extract_graph_module_outputs   s
   
r   c                 C   s(   t t| \}}dd |D }t| S )z[Return the all valid devices (i.e., GPU or CPU) among outputs of this torch.fx.GraphModule.c                 S   s*   g | ]}t |d rd|jv r|jd qS )r{   r|   ru   r{   ).0
output_argr!   r!   r"   
<listcomp>  s    
z/_infer_ep_from_graph_module.<locals>.<listcomp>)r   tree_flattenr   rz   )r%   flattened_output_args_selected_output_argsr!   r!   r"   _infer_ep_from_graph_module  s   r   rx   c                 C   s,   dt dtfdd}t| }tt||ddS )z:Sort execution providers in eps based on pre-set priority.epr   c                 S   s   | dkrdS | dkrdS dS )Nr:      rt   r)   r   r!   )r   r!   r!   r"   get_execution_provider_priority  s
   z2_sort_eps.<locals>.get_execution_provider_priorityT)keyreverse)r2   intsetrv   sorted)rx   r   
unique_epsr!   r!   r"   	_sort_eps  s   r   valueszORTC.OrtDevice.c              	      sr   dt dt fdd dttjtjt tjttjtf dt f fddt	| dkr4t
fd	d
| D }|S dfS )N	device_idr   c                 S   s   | pdS )Nr   r!   )r   r!   r!   r"   _device_id_or_zero1  s   z-_get_onnx_devices.<locals>._device_id_or_zerovaluec                    sx   t | tjrtt| jjtj  | jj	S t | tj
ttjttjtfr2ttdtj dS tdtt|  )NrG   r   zUnsupported value type: )r}   r=   r~   rI   rJ   rM   rs   r_   default_memoryindexSymIntr   SymFloatfloatSymBoolrj   rL   r2   r   )r   r!   r"   _map_tensor_or_sym_to_device4  s   

z7_get_onnx_devices.<locals>._map_tensor_or_sym_to_devicer   c                 3   s    | ]} |V  qd S rQ   r!   )r   r   )r   r!   r"   	<genexpr>I  s    z$_get_onnx_devices.<locals>.<genexpr>r)   )r   r   r=   r~   r   r   r   r   rj   lenrv   )r   ort_devicesr!   )r   r   r"   _get_onnx_devices)  s   
r   tensorsdevicesc                 C   sn   t  }|t|  g }g }g }| D ]}|t|j  ||  ||  q|	| |||| |S rQ   )
rI   OrtValueVectorreserver   rn   r   dtypesizedata_ptrpush_back_batch)r   r   	ortvaluesdtypesshapes	data_ptrstensorr!   r!   r"   !_get_ortvalues_from_torch_tensorsO  s   r   r   c                 C   s*   | j rtdtj|  | j| jd}|S )Nz#sparse tensor is not yet supported.)r   rs   )	is_sparserL   r=   emptyr   r   rs   )r   outr!   r!   r"   _to_real_tensor`  s   r   dynamo_value
value_infoonnx.ValueInfoProtoc                 C   s   t | tjrt|jjjjdkr| jdkrt| S t | t	r'tj
| tjdS t | tr4tj
| tjdS t | trAtj
| tjdS t | tjsIJ |  S )z9Helper function to wrap PyTorch variables as torch.Tensorr   )r)   )r   )r}   r=   r~   r   r_   tensor_typeshapedimsqueezer   r   int64r   float32rj   
contiguous)r   r   r!   r!   r"   _adjust_scalar_from_fx_to_onnxg  s   





r   
prim_valuec                 C   s<   t | tjs
J dt |tjttjttjtfr| 	 S | S )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   rj   item)r   r   r!   r!   r"   _adjust_scalar_from_onnx_to_fx  s   r   sessonnxruntime.InferenceSessioninput_namesinputsinput_devicesoutput_namesoutputsoutput_devicespreallocate_outputinput_value_infosr   .normalized_prim_outputsc
                 C   s  t d tdd t||D }t  t d t||}
|r-tdd |D }t||}nt }t  t d t }|	dd | 
|||
||| t  |rft d	 td
d t||	D }t  |S t d	 tjjj|}tdd t||	D }t  |S )Nr   c                 s       | ]
\}}t ||V  qd S rQ   r   r   ry   r   r!   r!   r"   r     
    
z8_run_onnx_session_with_ortvaluevector.<locals>.<genexpr>r   c                 s   s&    | ]}t |trt|n|V  qd S rQ   )r}   r   r   )r   tr!   r!   r"   r     s    
run_with_ortvaluevector'disable_synchronize_execution_providers1zafter run_with_ortvaluevectorc                 s   r   rQ   r   r   onnx_outputprim_outputr!   r!   r"   r     r   c                 s   r   rQ   r   r   r!   r!   r"   r     r   )rC   rv   ziprE   r   rI   r   onnxruntime
RunOptionsadd_run_config_entryr   training	ortmodule_utils_ortvalues_to_torch_tensor)r   r   r   r   r   r   r   r   r   r   
ort_inputspth_outputsort_outputsrun_optionsr!   r!   r"   %_run_onnx_session_with_ortvaluevector  sJ   

r   c
                 C   sT   t dd t||D }dd t||D }
| ||
}t dd t||	D }|S )Nc                 s   r   rQ   r   r   r!   r!   r"   r     r   z/_run_onnx_session_with_fetch.<locals>.<genexpr>c                 S   s&   i | ]\}}|t j|  qS r!   )r   OrtValueortvalue_from_numpyrG   numpy)r   r<   r   r!   r!   r"   
<dictcomp>  s    z0_run_onnx_session_with_fetch.<locals>.<dictcomp>c                 s   s$    | ]\}}t t||V  qd S rQ   )r   r=   
from_numpy)r   r   r   r!   r!   r"   r     s    
)rv   r   run)r   r   r   r   r   r   r   r   r   r   feedr   r   r!   r!   r"   _run_onnx_session_with_fetch  s   r   c                   @   sv   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dZ	dd Z
dS )OrtExecutionInfoPerSessionzWInformation required to execute torch.fx.GraphModule using onnxruntime.InferenceSessionsessionr   r   .r   r   r   output_value_infosr   r   r   example_outputsc	           	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S rQ   r   r   r   r   r   r   r   r   )	rU   r   r   r   r   r   r   r   r   r!   r!   r"   rS     s   z#OrtExecutionInfoPerSession.__init__c                 G   s  t |t | jkrdS t|| jD ]o\}}t|tjttfs! dS t|tttfrGt	t
|}||j
jjkr9 dS t |j
jjjdkrF dS qt|j }||j
jjkrV dS t|j|j
jjjD ]\}}t|trr|j|ksq|jrrq`t|tjr||jr|q`  dS qdS )NFr   T)r   r   r   r}   r=   r~   r   r   rj   r   r_   r   	elem_typer   r   r   r   	dim_value	dim_paramr   )rU   rw   ry   r   
onnx_dtyper   onnx_dimr!   r!   r"   is_supported4  s0   

	z'OrtExecutionInfoPerSession.is_supportedN)rb   rc   rd   re   r   r2   r   r=   r~   rS   r   r!   r!   r!   r"   r     s(    

	
!r   c                   @   s<   e Zd Zdd ZdejjfddZdejjdefddZ	d	S )
"OrtExecutionInfoForAllGraphModulesc                 C   s
   i | _ d S rQ   )execution_info_per_graph_module)rU   r!   r!   r"   rS   Y  s   z+OrtExecutionInfoForAllGraphModules.__init__r%   c                 G   s8   || j vrd S | j | }|D ]}|j| r|  S qd S rQ   )r   r   )rU   r%   rw   
candidates	candidater!   r!   r"   &search_reusable_session_execution_info`  s   


zIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_infor^   c                 C   s.   || j vr|g| j |< d S | j | | d S rQ   )r   rn   )rU   r%   r^   r!   r!   r"   cache_session_execution_infop  s   
z?OrtExecutionInfoForAllGraphModules.cache_session_execution_infoN)
rb   rc   rd   rS   r=   rh   GraphModuler  r   r  r!   r!   r!   r"   r   W  s    
r   r   )frozen)is_backward_compatiblec                   @   s   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< 	 dZeeedgdf   ed< dS )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_optionszonnx.ModelProtopre_ort_model_transforms)rb   rc   rd   re   r  r   r	   r   __annotations__r  rj   r	  r   r
  r  r  r  r   r!   r!   r!   r"   r     s*   
 	r   c                	   @   s   e Zd ZU dZddee fddZdejj	de
eeeeef f  fdd	Zdejj	fd
dZdejj	dejj	fddZdejj	dejj	fd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dZedd Zedd ZdS )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                 C   s   |d u rt  n|| _tjjj| jjd u rtj n| jj| _	tjjj
j| j	j}d d d d d d}t||| _i | _t | _d| _d| _ttjdrQt| _d S t| _d S )N)getattrz_operator.getitemz_operator.mulz_operator.addz_operator.subFr   r   )r   _optionsr=   onnx	_internalexporterResolvedExportOptionsr  ExportOptions_resolved_onnx_exporter_optionsrh   decomposition_table'_create_onnx_supports_op_overload_tableonnx_registryrN   _supported_ops_partitioner_cacher   _all_ort_execution_info_assert_allclose_to_baselineexecution_countru   rI   r   r   r   r   )rU   r  rO   rP   r!   r!   r"   rS     s4   


zOrtBackend.__init__r%   r   c                 G   s   t  }| jjrt|  }r|}nt| }r|}g }g | jjp g t|| jjp+t R D ]*}t	|t
r:|i f}nt	|t rK|d d u rK|d i f}|d urX||vrX|| q.|S )Nr)   r   )rv   r  r  rz   r   r  r   r	  r;   r}   r2   rn   )rU   r%   rw   inferred_epseps_from_argseps_from_graph_moduleselected_epsr   r!   r!   r"   _select_eps$  s,   




zOrtBackend._select_epsc                 O   sf  | j j|g|R  }|r&|j}|j}|j}|j}|j}	|j}
|j}|j	}nt
jjjj| jj| }| jjrJd| _t|}dd }t||}nzt|j|i |}W n tyg   td| d| _ w tj| jjd}t
jjjj| jj| }|j|| jj | jj!d}|j"| jj#j$d}zdd	l%m&} dd
l'm(} |)|}|*|}W n t+y   td Y nw | j,j-r| j,j-D ]}|| q|. }t/j01ddrt2||d t(j3|| j,j4| j5|g|R  d}t6dd |j7j8D }t6dd |j7j9D }t:|}
t;|t6rt:|}nt:|f}t6dd |j7j8D }t6dd |j7j9D }	t<|||||	|
||d}| j =|| |  j>d7  _>t;|t
j?}|rQ|fn|}t;|t6s[J t@dd |D sgJ tAd | ||||
|||| j,j||
}tB  | jCrt
jDjEjF|g|R ddi}|r|fn|}tG||D ]\}}t
jHI|| q|r|d S |S )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                 S   s"   t | drd| jv r| jd S | S )Nr{   r|   r   r   r!   r!   r"   maybe_map_to_meta_valm  s   
z>OrtBackend._ort_acclerated_call.<locals>.maybe_map_to_meta_valzFakeTensorProb failed for %s)diagnostic_context)fx_graph_moduleonnxfunction_dispatcherop_level_debug)opset_versionr   )	optimizer)r   zONNXScript optimizer is not available. Skipping optimization. Please `pip install onnxscript -U` to enable post-export optimization.r&   N)r%   )path_or_bytessess_options	providersc                 s       | ]}|j V  qd S rQ   rB   r   inputr!   r!   r"   r         z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>c                 s   r/  rQ   rB   r   r   r!   r!   r"   r     r2  c                 s       | ]}|V  qd S rQ   r!   r0  r!   r!   r"   r         c                 s   r4  rQ   r!   r3  r!   r!   r"   r     r5  r   r)   c                 s   s$    | ]}t |tjtjtfV  qd S rQ   )r}   r=   r~   r   r   )r   elemr!   r!   r"   r     s
    
$run_onnx_session_with_ortvaluevectorexecutoraten)Jr  r  r   r   r   r   r   r   r   r   r=   r  r  rh   passesMovePlaceholderToFrontr  r&  r   dynamic_shapesr   r   r   tree_mapr   	propagate	Exceptionr]   ra   r   FxOnnxInterpreterInsertTypePromotionr(  r)  to_model_protor  r*  r   r+  onnxscript.rewriterr   optimizerewriteImportErrorr  r  SerializeToStringr-   r.   r/   r9   InferenceSessionr  r$  rv   r3   r1  r   r   r}   r   r  r  r~   allrC   rE   r  _primsr8  executer   testingassert_close)rU   r%   rw   kwargs!cached_execution_info_per_sessiononnx_sessionr   r   r   r   r   r   prim_outputsextracted_outputsr%  fx_interpreterexported
onnx_modelr+  ort_rewriter	transformonnx_model_bytesexecution_info_per_sessionis_single_tensor_outputr   onnx_outputsbaseline_outputsnormalized_baseline_ouptutsr   baseline_outputr!   r!   r"   _ort_acclerated_callE  s  



	
	

zOrtBackend._ort_acclerated_callc           	      C   s   ddl m} || jv r| j| }|S |}||| jdd}| }|| j|< |jjD ]}|jdkr?d|jv r?t	||j}| j
|_q)|S )Nr   )CapabilityBasedPartitionerT)allows_single_node_partitioncall_modulefused_)!torch.fx.passes.infra.partitionerr`  r  r  partition_and_fuser3   rm   r[   r<   r  r_  _wrapped_call)	rU   r%   rw   r`  partitioned_prim_graph_moduleprim_graph_modulepartitionerrY   fused_moduler!   r!   r"   compile  s$   


zOrtBackend.compilec                 C   sF   | j jrddlm} ddlm} || j|| jjd||S | ||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.compilerl  torch._dynamo.backends.commonrm  rk  r  r  )rU   r%   rw   rl  rm  r!   r!   r"   __call__O  s   zOrtBackend.__call__   %_OrtBackend__instance_cache_max_count_OrtBackend__instance_cachec                    s   dt dt fddt t st di  pi  t fddtjD d}|du rJttjtjk s@J dtj d	t d
t dtjt  } |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.abc                 S   s   | j |j ks$| j|jks$| j|jks$| j|jks$| j|jks$| j|jkr&dS | jd us0|jd ur2dS | j|ju r:dS | jd url|jd url| jj|jjkok| jj	|jj	kok| jj
|jj
kok| jj|jju ok| jj|jju S dS )NFT)r  r  r	  r   r
  r  r  r  r<  r)  diagnostic_optionsr  fake_context)rw  rx  r!   r!   r"   reusabler  s4   z<OrtBackend.get_cached_instance_for_options.<locals>.reusablec                 3   s     | ]}|j  r|V  qd S rQ   )r  )r   rx  r  r{  r!   r"   r     s    z=OrtBackend.get_cached_instance_for_options.<locals>.<genexpr>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   r}   nextr   rv  r   ru  rn   )r  backendr!   r|  r"   get_cached_instance_for_optionse  s"   
%
z*OrtBackend.get_cached_instance_for_optionsc                   C   s   t j  d S rQ   )r   rv  clearr!   r!   r!   r"   clear_cached_instances  s   z!OrtBackend.clear_cached_instancesc                   C   s
   t tjS rQ   )rv   r   rv  r!   r!   r!   r"   get_cached_instances  s   
zOrtBackend.get_cached_instancesrQ   )rb   rc   rd   re   r   r   rS   r=   rh   r  r	   r   r2   r   r   r$  r_  rk  rs  ru  r   r  rv  r   staticmethodr   r  r  r  r!   r!   r!   r"   r     s:   
 	R
! U6
G
r   )r  r  c                C   s   t || |S rQ   )r   r  )r%   rw   r  r!   r!   r"   r     s   r   rQ   )cdataclasses	importlibloggingr-   typingr   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   onnxruntime.capir   rI   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   r    rF  __all__rj   r   r#   r2   r   r  bytesrh   r  r9   r;   rC   rE   rM   	getLoggerrb   r]   rN   rr   rz   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   	dataclassr   r   r   r   r   r!   r!   r!   r"   <module>   sn  
4
	

		
2

"
&

 


	

N

	

$G$!
?   q