o
    闦i(                     @   sP  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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# erd dl$Z$d dl%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.Zda/ee0 e1d< g dZ2de0fddZ3i Z4ee5e6f e1d< 	dcde7deej8j9 de5fddZ:dee5 fddZ;de5fddZ<dd Z=de5fddZ>e?e@ZAG dd  d eZBdej8j9ddfd!d"ZCdee5d#f fd$d%ZDdej8j9deed#f fd&d'ZEdej8j9defd(d)ZFdej8j9dee5d#f fd*d+ZGd,ee5d#f dee5d#f fd-d.ZHd/eeejIejJe6ejKeLejMe0f d#f ded0 fd1d2ZNd3eejId#f d4ed0 deejId#f fd5d6ZOd7edejIfd8d9ZPd:eejIe6eLe0f d;d<dejIfd=d>ZQd7ejId?eejIejJe6ejKeLejMe0f deejIe6eLe0f fd@dAZRdBdCdDee5d#f dEeejId#f dFed0 dGee5d#f dHeejId#f dIed0 dJe0dKedL dMeeejIejJe6ejKeLejMe0f d#f deeejIe6eLe0f d#f fdNdOZSdBdCdDee5d#f dEeejId#f dFed0 dGee5d#f dHeejId#f dIed0 dJe0dKedL dMeeejIejJe6ejKeLejMe0f d#f deeejIe6eLe0f d#f fdPdQZTG dRdS dSZUe jVG dTdU dUZWee5ee5e
e5ef f f ZXee1dV< 	 e jVdWdXedYdZG d[d\ d\ZYedYdZG d]d^ d^ZZedYdZdd_dej8j9d`eeeYe
e5ef f  fdadbZ[dS )d    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                  C   s   t du rAz0td td td ddl} ddl} ddl} ddl} ddlm}m	}m
}m} da W t S  ty@   da Y t S w 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")
        ...
    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#    r-   ^/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/onnx/_internal/onnxruntime.pyr   ;   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)r0   r1   prefixnfilenameffilename_txtr-   r-   r.   _dump_onnx_modelo   s"   

rE   c                   C   s   dgS )NCPUExecutionProviderr-   r-   r-   r-   r.   _infer_default_eps   s   rG   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)r,   cudais_availablenvtx
range_pushrH   r-   r-   r.   _nvtx_range_push   s   
rN   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,   rI   rJ   rK   	range_popr-   r-   r-   r.   _nvtx_range_pop   s   
rP   device_typec                 C   sN   ddl m} | dkr|j S | dkr|j S | dkr!|j S td|  )Nr   r   rI   cpumaiazUnsupported device type: )onnxruntime.capir   	OrtDevicerI   rR   npu
ValueError)rQ   ORTCr-   r-   r.   _get_ort_device_type   s   


rY   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)selfr[   r\   	__class__r-   r.   r_      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   targetr`   loggerinfotyper^   is_node_supportedwarning)ra   rd   re   rb   r-   r.   rl      s,   
z$OrtOperatorSupport.is_node_supported)__name__
__module____qualname____doc__r
   r   r   r>   r_   r   r,   nnModulefxNodeboolrl   __classcell__r-   r-   rb   r.   rZ      s    "rZ   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)r?   nodesrg   appendprepend)r1   r?   placeholdersfirst_not_placeholderre   rx   r-   r-   r.   _move_placeholder_to_front   s   


r~   .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.devicerI   CUDAExecutionProviderrR   rF   )hasattrr   rk   rz   tuple)argsepsargr   r-   r-   r.   _infer_ep_from_device   s   



r   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 )Nrx   metaval)
r?   ry   rg   r   r   
isinstancer,   Tensorrz   r   )r1   r|   re   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.)r?   ry   rg   r   rW   )r1   re   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   r   r   ).0
output_argr-   r-   r.   
<listcomp>!  s    
z/_infer_ep_from_graph_module.<locals>.<listcomp>)r   tree_flattenr   r   )r1   flattened_output_args_selected_output_argsr-   r-   r.   _infer_ep_from_graph_module  s   r   r   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 )NrF      r   r5   r   r-   )r   r-   r-   r.   get_execution_provider_priority.  s
   z2_sort_eps.<locals>.get_execution_provider_priorityT)keyreverse)r>   intsetr   sorted)r   r   
unique_epsr-   r-   r.   	_sort_eps+  s   r   valueszORTC.OrtDevice.c              	      s   ddl m  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r;tfd
d| D }|S dfS )Nr   r   	device_idr   c                 S   s   | pdS )Nr   r-   )r   r-   r-   r.   _device_id_or_zeroG  s   z-_get_onnx_devices.<locals>._device_id_or_zerovaluec                    sx   t | tjr t| jj j | jjS t | tj	t
tjttjtfr2 td j dS tdtt|  )NrR   r   zUnsupported value type: )r   r,   r   rU   rY   r   rk   default_memoryindexSymIntr   SymFloatfloatSymBoolrv   rW   r>   r   )rX   r   r-   r.   _map_tensor_or_sym_to_deviceJ  s   

z7_get_onnx_devices.<locals>._map_tensor_or_sym_to_devicec                 3   s    | ]} |V  qd S r]   r-   )r   r   )r   r-   r.   	<genexpr>_  s    z$_get_onnx_devices.<locals>.<genexpr>r5   )rT   r   r   r   r,   r   r   r   r   r   rv   lenr   )r   ort_devicesr-   )rX   r   r   r.   _get_onnx_devices=  s   
r   tensorsdevicesc           	      C   s   ddl m} ddlm} | }|t|  g }g }g }| D ]}|||j  ||	  ||
  q|| |||| |S )Nr   r   )_TORCH_DTYPE_TO_NUMPY_DTYPE)rT   r   "torch.onnx._internal.fx.type_utilsr   OrtValueVectorreserver   rz   dtypesizedata_ptrpush_back_batch)	r   r   rX   r   	ortvaluesdtypesshapes	data_ptrstensorr-   r-   r.   !_get_ortvalues_from_torch_tensorse  s   r   r   c                 C   s*   | j rtdtj|  | j| jd}|S )Nz#sparse tensor is not yet supported.)r   r   )	is_sparserW   r,   emptyr   r   r   )r   outr-   r-   r.   _to_real_tensorz  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   )r5   )r   )r   r,   r   r   rk   tensor_typeshapedimsqueezer   r   int64r   float32rv   
contiguous)r   r   r-   r-   r.   _adjust_scalar_from_fx_to_onnx  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   rv   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  dd l }
ddlm} td tdd t||D }t  td t||}|r7tdd |D }t||}n| }t  td |
	 }|
d	d
 | |||||| t  |rptd tdd t||	D }t  |S td |
jjj|}tdd t||	D }t  |S )Nr   r   r   c                 s       | ]
\}}t ||V  qd S r]   r   r   r   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 r]   )r   r   r   )r   tr-   r-   r.   r     s    
run_with_ortvaluevector'disable_synchronize_execution_providers1zafter run_with_ortvaluevectorc                 s   r   r]   r   r   onnx_outputprim_outputr-   r-   r.   r     r   c                 s   r   r]   r   r   r-   r-   r.   r     r   )r   rT   r   rN   r   ziprP   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   rX   
ort_inputspth_outputsort_outputsrun_optionsr-   r-   r.   %_run_onnx_session_with_ortvaluevector  sN   

r   c
                    s`   dd l  tdd t||D } fddt||D }
| ||
}tdd t||	D }|S )Nr   c                 s   r   r]   r   r   r-   r-   r.   r   !  r   z/_run_onnx_session_with_fetch.<locals>.<genexpr>c                    s&   i | ]\}}| j |  qS r-   )OrtValueortvalue_from_numpyrR   numpy)r   rH   r   r   r-   r.   
<dictcomp>%  s    z0_run_onnx_session_with_fetch.<locals>.<dictcomp>c                 s   s$    | ]\}}t t||V  qd S r]   )r   r,   
from_numpy)r   r   r   r-   r-   r.   r   *  s    
)r   r   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 r]   r  r   r   r   r  r   r   r  )	ra   r  r   r   r   r  r   r   r  r-   r-   r.   r_   7  s   z#OrtExecutionInfoPerSession.__init__c           	      G   s  ddl m}m} 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rO|t|}||jjjkrA dS t|jjjjdkrN dS q||j }||jjjkr^ dS t|j|jjjjD ]\}}t|t
rz|j|ksy|jrzqht|tjr|jrqh  dS qdS )Nr   )(_TORCH_DTYPE_TO_ONNX_TENSOR_ELEMENT_TYPE,from_python_type_to_onnx_tensor_element_typeFT)r   r  r  r   r   r   r   r,   r   r   r   rv   rk   r   	elem_typer   r   r   	dim_value	dim_paramr   )	ra   r   r  r  r   r   
onnx_dtyper   onnx_dimr-   r-   r.   is_supportedX  s2   

	z'OrtExecutionInfoPerSession.is_supportedN)rn   ro   rp   rq   r   r>   r   r,   r   r_   r  r-   r-   r-   r.   r  4  s(    

	
!r  c                   @   s>   e Zd ZdddZdejjfddZdejjdefd	d
Z	dS )"OrtExecutionInfoForAllGraphModulesr   Nc                 C   s
   i | _ d S r]   )execution_info_per_graph_module)ra   r-   r-   r.   r_     s   z+OrtExecutionInfoForAllGraphModules.__init__r1   c                 G   s8   || j vrd S | j | }|D ]}|j| r|  S qd S r]   )r  r  )ra   r1   r   
candidates	candidater-   r-   r.   &search_reusable_session_execution_info  s   


zIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_inforj   c                 C   s.   || j vr|g| j |< d S | j | | d S r]   )r  rz   )ra   r1   rj   r-   r-   r.   cache_session_execution_info  s   
z?OrtExecutionInfoForAllGraphModules.cache_session_execution_info)r   N)
rn   ro   rp   r_   r,   rt   GraphModuler  r  r  r-   r-   r-   r.   r    s    

r  r   T)frozenF)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)rn   ro   rp   rq   r  r   r	   r   __annotations__r  rv   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dl m} dd l}dd l}dd l}|d u rt n|| _|jjj	
| jjd u r,|j n| jj| _|jjjj| jj}d d d d d d}t||| _i | _t | _d| _d| _t|jdrct| _d S t| _d S )Nr   r   )getattrz_operator.getitemz_operator.mulz_operator.addz_operator.subFr   )rT   r   r&   r(   +torch.onnx._internal.fx.decomposition_tabler   _optionsonnx	_internal_exporter_legacyResolvedExportOptionsr  ExportOptions_resolved_onnx_exporter_optionsrt   r    '_create_onnx_supports_op_overload_tableonnx_registryrZ   _supported_ops_partitioner_cacher  _all_ort_execution_info_assert_allclose_to_baselineexecution_countr   r   r   r   r   )ra   r  rX   r,   r[   r\   r-   r-   r.   r_     s<   


zOrtBackend.__init__r1   r   c                 G   s   d}| j jrt|  }r|}nt| }r|}g }g | j jpg t|| j jp*t R D ]*}t|t	r9|i f}nt|t
rJ|d d u rJ|d i f}|d urW||vrW|| q-|S )Nr-   r5   r   )r"  r  r   r   r  r   r  rG   r   r>   r   rz   )ra   r1   r   inferred_epseps_from_argseps_from_graph_moduleselected_epsr   r-   r-   r.   _select_epsS  s,   




zOrtBackend._select_epsc           !      O   sh  ddl }ddlm}m} | jj|g|R  }|r2|j}|j}	|j}
|j	}|j
}|j}|j}|j}n|| jj| }| jjrRd| _t|}dd }t||}nzt|j|i |}W n tyo   td| d| _ w |j| jjd}|| 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+j,-ddrt.||d |j/|| j(j0| j1|g|R  d}t2dd |j3j4D }	t2dd |j3j5D }
t6|}t7|t2rt6|}nt6|f}t2dd |j3j4D }t2dd |j3j5D }t8||	||
||||d}| j9|| |  j:d7  _:t7|t;j<}|rR|fn|}t7|t2s\J t=dd |D shJ t>d | ||	|||
||| j(j||
}t?  | j@rt;jAjBjC|g|R ddi}|r|fn|}tD||D ]\}} t;jEF||  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.
        r   N)r!   r"   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_val  s   
z>OrtBackend._ort_acclerated_call.<locals>.maybe_map_to_meta_valzFakeTensorProb failed for %s)diagnostic_context)fx_graph_moduleonnxfunction_dispatcher)opset_version)	optimizerr   zONNXScript optimizer is not available. Skipping optimization. Please `pip install onnxscript -U` to enable post-export optimization.r2   )r1   )path_or_bytessess_options	providersc                 s       | ]}|j V  qd S r]   rM   r   inputr-   r-   r.   r         z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>c                 s   r>  r]   rM   r   r   r-   r-   r.   r     rA  c                 s       | ]}|V  qd S r]   r-   r?  r-   r-   r.   r         c                 s   rC  r]   r-   rB  r-   r-   r.   r     rD  r  r5   c                 s   s$    | ]}t |tjtjtfV  qd S r]   )r   r,   r   r   r   )r   elemr-   r-   r.   r   (  s
    
$run_onnx_session_with_ortvaluevectorexecutoraten)Gr   r*   r!   r"   r-  r  r  r   r   r   r  r   r   r  MovePlaceholderToFrontr(  r6  r   dynamic_shapesr   r   r   tree_mapr   	propagate	Exceptionri   rm   FxOnnxInterpreterInsertTypePromotionr8  to_model_protor*  r9  r   r:  onnxscript.rewriteroptimizerewriter+   r"  r  SerializeToStringr9   r:   r;   rE   InferenceSessionr  r4  r   r?   r@  r   r   r   r  r  r/  r,   r   allrN   rP   r.  _primsrG  executer   testingassert_close)!ra   r1   r   kwargsr   r!   r"   !cached_execution_info_per_sessiononnx_sessionr   r   r   r  r   r   prim_outputsextracted_outputsr5  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_callt  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.partitionerrm  r,  r+  partition_and_fuser?   ry   rg   rH   r   rl  _wrapped_call)	ra   r1   r   rm  partitioned_prim_graph_moduleprim_graph_modulepartitionerre   fused_moduler-   r-   r.   compileK  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.compilery  torch._dynamo.backends.commonrz  rx  r(  r    )ra   r1   r   ry  rz  r-   r-   r.   __call__  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rd|jd urd| jj|jjkoc| jj	|jj	koc| jj
|jj
u oc| jj|jju S dS )NFT)r  r  r  r   r  r  r  r  rJ  diagnostic_optionsr*  fake_context)r  r  r-   r-   r.   reusable  s,   	z<OrtBackend.get_cached_instance_for_options.<locals>.reusablec                 3   s     | ]}|j  r|V  qd S r]   )r"  )r   r  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   r  r   r  rz   )r  backendr-   r  r.   get_cached_instance_for_options  s"   
#
z*OrtBackend.get_cached_instance_for_optionsc                   C   s   t j  d S r]   )r   r  clearr-   r-   r-   r.   clear_cached_instances  s   z!OrtBackend.clear_cached_instancesc                   C   s
   t tjS r]   )r   r   r  r-   r-   r-   r.   get_cached_instances  s   
zOrtBackend.get_cached_instancesr]   )rn   ro   rp   rq   r   r   r_   r,   rt   r  r	   r   r>   r   r   r4  rl  rx  r  r  r   r  r  r   staticmethodr   r  r  r  r-   r-   r-   r.   r     s:   
 	X
! X6
E
r   )r  r  c                C   s   t || |S r]   )r   r  )r1   r   r  r-   r-   r.   r     s   r   r]   )\dataclassesr$   loggingr9   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   rT   r   rX   r&   r'   r(   r)   r!  torch.onnx._internal.fx.passesr   rv   r  __all__r   r/   r>   r   bytesrt   r  rE   rG   rN   rP   rY   	getLoggerrn   ri   rZ   r~   r   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  
8	1

		
2

"
(

 


	

Q

	

&L$!
?   w