U
    •Ê¹j°  ã                   @   s2  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  zzd 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-m.Z. d dl/m0Z0m1Z1 dZ2W n e3k
�rZ   dZ2Y nX dddddgZ4e5dœdd„Z6e
e7 dœdd„Z8e7dœdd„Z9dd„ Z:e7dœdd „Z;e <e=¡Z>G d!d"„ d"eƒZ?ej@jAdd#œd$d%„ZBej@jAdd&œd'd(„ZCee7d)f dœd*d+„ZDej@jAeed)f d#œd,d-„ZEej@jAed#œd.d/„ZFej@jAee7d)f d#œd0d1„ZGee7d)f ee7d)f d2œd3d4„ZHeejId)f ed5 d6œd7d8„ZJeejId)f ed5 eejId)f d9œd:d;„ZKeejId<œd=d>„ZLd?ee7d)f eejId)f ed5 ee7d)f eejId)f ed5 e5eejId)f d@œ	dAdB„ZMd?ee7d)f eejId)f ed5 ee7d)f eejId)f ed5 e5eejId)f d@œ	dCdD„ZNG dEdF„ dFƒZOe jPG dGdH„ dHƒƒZQee7ee7ee7ef f f ZReeSd< e jPddI�eddJ�G dKd„ dƒƒƒZTeddJ�G dLd„ dƒƒZUeddJ�ddMœej@jAe	eeTee7ef f  dNœdOd„ƒZVdS )Pé    N)
ÚAnyÚDictÚFinalÚListÚMappingÚOptionalÚSequenceÚSetÚTupleÚUnion)Ú	TypeAlias)Ú
FakeTensor)Úcompatibility)ÚFakeTensorProp)ÚOperatorSupport)ÚCALLABLE_NODE_OPS)Ú_pytree)Ú_pybind_stateZ
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                   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-preview onnxruntime")
        ...
    )Ú_SUPPORT_ONNXRT© r   r   ú_/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/onnx/_internal/onnxruntime.pyr   C   s    c                   C   s   dgS )NÚCPUExecutionProviderr   r   r   r   r   Ú_infer_default_epsW   s    r!   ©Únamec                 C   s   t j ¡ rt jj | ¡ dS )zŠIf 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ÚnvtxZ
range_pushr"   r   r   r   Ú_nvtx_range_push]   s    
r(   c                   C   s   t j ¡ rt jj ¡  dS )z�If 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'   Z	range_popr   r   r   r   Ú_nvtx_range_popf   s    
r)   ©Zdevice_typec                 C   sF   | dkrt j ¡ S | dkr$t j ¡ S | dkr6t j ¡ S td|  ƒ‚d S )Nr%   ÚcpuZortzUnsupported device type: )ÚORTCÚ	OrtDevicer%   r+   ZnpuÚ
ValueErrorr*   r   r   r   Ú_get_ort_device_typeo   s    


r/   c                       sZ   e Zd ZdZee eeef dœ‡ fdd„Ze	ee
jjf e
jjedœ‡ 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)Úselfr1   r2   ©Ú	__class__r   r   r5   ˆ   s    zOrtOperatorSupport.__init__)Ú
submodulesÚnoder   c                    s–   |j tkrdS |j dkr>|j| jkr>t d|jt|jƒ¡ dS t d|jt|jƒ¡ tƒ  ||¡r|t d|jt|jƒ¡ dS t d|jt|jƒ¡ dS )NFZ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   Útargetr6   ÚloggerÚwarningÚtyper4   Úis_node_supported)r7   r:   r;   r8   r   r   rA   �   s6    
ýýýýz$OrtOperatorSupport.is_node_supported)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   Ústrr5   r   r$   ÚnnÚModuleÚfxÚNodeÚboolrA   Ú__classcell__r   r   r8   r   r0   €   s    	 þr0   ©Úgraph_moduler   c                 C   sh   | j }g }d}|jD ].}|jdkr,| |¡ |dkr|jdkr|}q|dkrPdS |D ]}| |¡ qTdS )zª
    In torch.fx.Graph, placehoder is a special assignment node. If it's not
    executed in the beginning, it could overwrite values computed by upstream
    nodes.
    NÚplaceholder)ÚgraphÚnodesr<   ÚappendÚprepend)rN   rP   ÚplaceholdersZfirst_not_placeholderr;   rO   r   r   r   Ú_move_placeholder_to_front·   s    


rU   )Ú	fx_moduler   c                 C   s  | j jD �]}t|jtjjƒr|jjtjj	j
krd}d}d}d}d|jkr^|jd tjkr^d}d|jkrˆ|jd |jd jd jkrˆd}d|jkr–d}|jD ]}|dkrœd}qœt|jƒd	krì|rì|rì|rì|rìd|jd i|_tjj	jj|_qtd
dd„ |jD ƒ› d|j› �ƒ‚q|  ¡  d S )NTÚlayoutFÚdevicer   ÚvalÚdtype)rW   rX   rZ   é   zaaten._to_copy must be replaced with other ONNX-supported aten ops.                          args=c                 S   s   g | ]
}|j ‘qS r   )Úmeta)Ú.0Úargr   r   r   Ú
<listcomp>ô   s     z,_replace_to_copy_with_to.<locals>.<listcomp>z	, kwargs=)rP   rQ   Ú
isinstancer=   r$   Z_opsZ
OpOverloadZoverloadpacketZopsÚatenZ_to_copyÚkwargsZstridedÚargsr\   rX   ÚlenÚtorZ   ÚRuntimeErrorZ	recompile)rV   r;   Zis_default_layoutZis_on_same_deviceZis_castZare_kwargs_supportedÚkwargr   r   r   Ú_replace_to_copy_with_toÌ   sR    ÿþÿþ

ÿþýüû
ÿÿÿrh   .c                  G   sP   g }| D ]>}t |dƒr|j}|jdkr2| d¡ q|jdkr| d¡ qt|ƒS )zBReturn the first valid device (i.e., GPU or CPU) in argument list.rX   r%   ÚCUDAExecutionProviderr+   r    )ÚhasattrrX   r@   rR   Útuple)rc   Úepsr^   rX   r   r   r   Ú_infer_ep_from_deviceù   s    


rm   c                 C   sX   g }| j jD ]B}|jdkrt|dƒrDd|jkrDt|jd tjƒsDt‚| 	|¡ qt
|ƒS )NrO   r\   rY   )rP   rQ   r<   rj   r\   r`   r$   ÚTensorÚAssertionErrorrR   rk   )rN   rT   r;   r   r   r   Ú_extract_graph_module_inputs  s    
rp   c                 C   s2   | j jD ]}|jdkr|jd   S qtdƒ‚dS )zHCollect "val" fields from outputs metadata in this torch.fx.GraphModule.Úoutputr   z2No output node found in this torch.fx.GraphModule.N)rP   rQ   r<   rc   r.   )rN   r;   r   r   r   Ú_extract_graph_module_outputs  s    
rr   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kr|jd ‘qS )r\   rY   ©rj   r\   )r]   Z
output_argr   r   r   r_      s   
 
üz/_infer_ep_from_graph_module.<locals>.<listcomp>)r   Ztree_flattenrr   rm   )rN   Zflattened_output_argsÚ_Zselected_output_argsr   r   r   Ú_infer_ep_from_graph_module  s    ÿþru   )rl   r   c                 C   s*   t tdœ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    é   ri   r[   r   r   )rv   r   r   r   Úget_execution_provider_priority-  s
    z2_sort_eps.<locals>.get_execution_provider_priorityT)ÚkeyÚreverse)rF   ÚintÚsetrk   Úsorted)rl   rx   Z
unique_epsr   r   r   Ú	_sort_eps*  s    r~   ©zORTC.OrtDevice.)Úvaluesr   c                    sH   t ‡fdd„ˆD ƒƒstdƒ‚ttdœdd„‰ t‡ fdd„ˆD ƒƒ}|S )Nc                 3   s   | ]}|j ˆ d  j kV  qdS )r   N)rX   ©r]   Úvalue)r€   r   r   Ú	<genexpr>=  s    z$_get_onnx_devices.<locals>.<genexpr>z&All values must be on the same device.)Ú	device_idr   c                 S   s   | pdS )Nr   r   )r„   r   r   r   Ú_device_id_or_zeroA  s    z-_get_onnx_devices.<locals>._device_id_or_zeroc                 3   s2   | ]*}t  t|jjƒt j ¡ ˆ |jjƒ¡V  qd S r3   )r,   r-   r/   rX   r@   Zdefault_memoryÚindexr�   )r…   r   r   rƒ   D  s   û

ý)Úallro   r{   rk   )r€   Údevicesr   )r…   r€   r   Ú_get_onnx_devices<  s    ÿ
þúr‰   )Útensorsrˆ   r   c                 C   sn   t  ¡ }| t| ƒ¡ g }g }g }| D ]0}| t|j ¡ | | ¡ ¡ | | ¡ ¡ q&| 	| ||||¡ |S r3   )
r,   ÚOrtValueVectorZreserverd   rR   r   rZ   ÚsizeZdata_ptrÚpush_back_batch)rŠ   rˆ   Z	ortvaluesZdtypesZshapesZ	data_ptrsÚtensorr   r   r   Ú!_get_ortvalues_from_torch_tensorsO  s    r�   )rŽ   r   c                 C   s*   | j rtdƒ‚tj|  ¡ | j| jd�}|S )Nz#sparse tensor is not yet supported.)rZ   rX   )Z	is_sparser.   r$   ÚemptyrŒ   rZ   rX   )rŽ   Úoutr   r   r   Ú_to_real_tensor`  s    r’   úonnxruntime.InferenceSession)	ÚsessÚinput_namesÚinputsÚinput_devicesÚoutput_namesÚoutputsÚoutput_devicesÚpreallocate_outputr   c                 C   sÆ   t dƒ tdd„ |D ƒƒ}tƒ  t dƒ t||ƒ}|rTtdd„ |D ƒƒ}	t|	|ƒ}
nt ¡ }
tƒ  t dƒ t ¡ }| dd¡ |  	|||||
|¡ tƒ  |r |	S t d	ƒ tj
jj |
¡}	tƒ  |	S d S )
NÚ
contiguousc                 s   s   | ]}|  ¡ V  qd S r3   )rœ   )r]   Úar   r   r   rƒ   r  s     z8_run_onnx_session_with_ortvaluevector.<locals>.<genexpr>r�   c                 s   s$   | ]}t |tƒrt|ƒn|V  qd S r3   )r`   r   r’   )r]   Útr   r   r   rƒ   }  s    Úrun_with_ortvaluevectorZ'disable_synchronize_execution_providersÚ1zafter run_with_ortvaluevector)r(   rk   r)   r�   r,   r‹   ÚonnxruntimeZ
RunOptionsZadd_run_config_entryrŸ   ZtrainingZ	ortmoduleZ_utilsZ_ortvalues_to_torch_tensor)r”   r•   r–   r—   r˜   r™   rš   r›   Z
ort_inputsÚpth_outputsÚort_outputsZrun_optionsr   r   r   Ú%_run_onnx_session_with_ortvaluevectorg  s@    

ÿ     ÿ
ÿr¤   c                 C   s<   dd„ t ||ƒD ƒ}|  ||¡}	tdd„ t |	|ƒD ƒƒ}
|
S )Nc                 S   s&   i | ]\}}|t j | ¡  ¡ ¡“qS r   )r¡   ZOrtValueZortvalue_from_numpyr+   Únumpy)r]   r#   rŽ   r   r   r   Ú
<dictcomp>¢  s   ÿ z0_run_onnx_session_with_fetch.<locals>.<dictcomp>c                 s   s$   | ]\}}t  |¡ |j¡V  qd S r3   )r$   Z
from_numpyre   rX   )r]   r‚   rŽ   r   r   r   rƒ   §  s   ÿz/_run_onnx_session_with_fetch.<locals>.<genexpr>)ÚzipÚrunrk   )r”   r•   r–   r—   r˜   r™   rš   r›   Úfeedr£   r¢   r   r   r   Ú_run_onnx_session_with_fetch˜  s    
þþrª   c                   @   sh   e Zd ZdZdeedf ed eedf ed ed ed eeejdf ejf dœdd„Z	d	d
„ Z
dS )ÚOrtExecutionInfoPerSessionzWInformation required to execute torch.fx.GraphModule using onnxruntime.InferenceSessionr“   .)zonnx.ValueInfoProto.r   ©Úsessionr•   Úinput_value_infosr˜   Úoutput_value_infosr—   rš   Úexample_outputsc	           	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r3   r¬   )	r7   r­   r•   r®   r˜   r¯   r—   rš   r°   r   r   r   r5   ±  s    þz#OrtExecutionInfoPerSession.__init__c                 G   sº   t |ƒt | jƒkrdS t|| jƒD ]’\}}t|tjƒs< dS t|j }||jj	j
krZ dS t|j|jj	jjƒD ]D\}}t|tƒr”|j|ksn|jr”qnqnt|tjƒrª|jrªqnqn  dS qnq"dS ©NFT)rd   r®   r§   r`   r$   rn   r   rZ   r@   Ztensor_typeZ	elem_typeÚshapeÚdimr{   Z	dim_valueZ	dim_paramZSymInt)r7   rc   r^   Z
value_infoZ
onnx_dtyper³   Zonnx_dimr   r   r   Úis_supportedÒ  s&    

ÿÿz'OrtExecutionInfoPerSession.is_supportedN)rB   rC   rD   rE   r
   rF   r   r$   rn   r5   r´   r   r   r   r   r«   ®  s   

÷!r«   c                   @   s:   e Zd Zdd„ Zejjdœdd„Zejjedœdd„Z	d	S )
Ú"OrtExecutionInfoForAllGraphModulesc                 C   s
   i | _ d S r3   )Úexecution_info_per_graph_module)r7   r   r   r   r5   ë  s    þz+OrtExecutionInfoForAllGraphModules.__init__©rN   c                 G   s8   || j krd S | j | }|D ]}|j|Ž r|  S qd S r3   )r¶   r´   )r7   rN   rc   Ú
candidatesÚ	candidater   r   r   Ú&search_reusable_session_execution_infoò  s    



zIOrtExecutionInfoForAllGraphModules.search_reusable_session_execution_info)rN   Úinfoc                 C   s,   || j kr|g| j |< n| j |  |¡ d S r3   )r¶   rR   )r7   rN   r»   r   r   r   Úcache_session_execution_info  s    
z?OrtExecutionInfoForAllGraphModules.cache_session_execution_infoN)
rB   rC   rD   r5   r$   rI   ÚGraphModulerº   r«   r¼   r   r   r   r   rµ   é  s   ÿ ÿrµ   )Úfrozen)Z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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_options)rB   rC   rD   rE   r¿   r   r   r   Ú__annotations__rÀ   rK   rÁ   r›   rÂ   rÃ   rÄ   r   r   r   r   r     s   

c                   @   sò   e Zd ZU dZdee dœdd„Zejj	e
eeeeef f  dœdd„Zejj	d	œd
d„Zejj	ejj	dœdd„Zejj	ejj	dœdd„ZdZeed< g Zeed   ed< edeeeeeef f  d dœ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.GrpahModule) 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 krt ƒ n|| _tjjj | jjd kr4tj ¡ n| jj¡| _	tjjj
j | j	j¡}d d dœ}t||ƒ| _i | _tƒ | _d| _d| _ttdƒr”tnt| _d S )N)Úgetattrz_operator.getitemFr   r�   )r   Ú_optionsr$   ÚonnxÚ	_internalZexporterZResolvedExportOptionsrÃ   ZExportOptionsÚ_resolved_onnx_exporter_optionsrI   Údecomposition_tableZ'_create_onnx_supports_op_overload_tableÚonnx_registryr0   Ú_supported_opsÚ_partitioner_cacherµ   Ú_all_ort_execution_infoÚ_assert_allclose_to_baselineÚexecution_countrj   r,   r¤   rª   r¨   )r7   rÇ   r1   r2   r   r   r   r5   ^  s,    

ÿýÿÿþÿýzOrtBackend.__init__rM   c                 G   s®   t ƒ }| jjr0t|Ž  }r |}nt|ƒ }r0|}g }| jjp>g t|ƒ| jjpPtƒ ˜D ]T}t	|t
ƒrl|i f}n"t	|t ƒrŽ|d d krŽ|d i f}|d k	rT||krT| |¡ qT|S )Nr[   r   )rk   rÉ   rÀ   rm   ru   r¿   r~   rÁ   r!   r`   rF   rR   )r7   rN   rc   Zinferred_epsZeps_from_argsZeps_from_graph_moduleZselected_epsrv   r   r   r   Ú_select_eps©  s$    
ý

zOrtBackend._select_epsr·   c              
   O   sÀ  | j j|f|žŽ }|r>|j}|j}|j}|j}|j}	|j}
�nžtj	j
jj | jj|¡ ¡ }| jjr†d| _t|ƒ}dd„ }t ||¡}
n>zt|ƒj||Ž}
W n( tk
rÂ   t d|¡ d| _‚ Y nX tj| jjd�}tj	j
jj | jj|¡ ¡ }|j|| jj| jjd�}|j | jj!j"d�}t#j$| %¡ | j&j'| j(|f|žŽ d�}t)d	d
„ |j*j+D ƒƒ}t)dd
„ |j*j,D ƒƒ}t-|ƒ}t.|
t)ƒ�rˆt-|
ƒ}	n
t-|
fƒ}	t/||t)dd
„ |j*j+D ƒƒ|t)dd
„ |j*j,D ƒƒ||	|
d�}| j  0||¡ |  j1d7  _1t.|
tj2ƒ}|�r|
fn|
}t.|t)ƒ�st3‚t4dd
„ |D ƒƒ�s.t3‚t5dƒ |  |||||||	| j&j¡}t6ƒ  | j7�r®tj8j9j:|f|žddiŽ}|�r†|fn|}t;||ƒD ]\}}tj< =||¡ �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kr| jd S | S d S )Nr\   rY   rs   )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)Zfx_graph_moduleÚonnxfunction_dispatcherÚop_level_debug)Úopset_version)Zpath_or_bytesZsess_optionsÚ	providersc                 s   s   | ]}|j V  qd S r3   r"   ©r]   Úinputr   r   r   rƒ   1  s     z2OrtBackend._ort_acclerated_call.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r3   r"   ©r]   rq   r   r   r   rƒ   2  s     c                 s   s   | ]
}|V  qd S r3   r   rÛ   r   r   r   rƒ   ?  s     c                 s   s   | ]
}|V  qd S r3   r   rÝ   r   r   r   rƒ   A  s     r¬   r[   c                 s   s   | ]}t |tjƒV  qd S r3   )r`   r$   rn   )r]   Úelemr   r   r   rƒ   U  s     Z$run_onnx_session_with_ortvaluevectorÚexecutorra   r   )>rÑ   rº   r­   r•   r˜   r—   rš   r°   r$   rÊ   rË   rI   ZpassesZMovePlaceholderToFrontrÌ   rÖ   r¨   Údynamic_shapesr›   rr   r   Ztree_mapr   Ú	propagateÚ	Exceptionr>   r?   r   ZFxOnnxInterpreterZInsertTypePromotionr×   rØ   Zto_model_protorÎ   rÙ   r¡   ZInferenceSessionZSerializeToStringrÉ   rÄ   rÔ   rk   rP   rÜ   rq   r‰   r`   r«   r¼   rÓ   rn   ro   r‡   r(   r)   rÒ   Z_primsrß   Úexecuter§   ZtestingZassert_close)r7   rN   rc   rb   Z!cached_execution_info_per_sessionZonnx_sessionr•   r˜   r—   rš   Zprim_outputsZextracted_outputsrÕ   Zfx_interpreterZexportedZ
onnx_modelZexecution_info_per_sessionZis_single_tensor_outputZnormalized_prim_outputsZonnx_outputsZbaseline_outputsZnormalized_baseline_ouptutsZonnx_outputZbaseline_outputr   r   r   Ú_ort_acclerated_callÊ  sØ    ÿÿÿ
þ
 ÿ ÿÿ ÿýÿý

ø ÿÿø
ÿÿÿÿ ÿzOrtBackend._ort_acclerated_callc           	      C   sŠ   ddl m} || jkr"| j| }nd|}t|ƒ ||| jdd�}| ¡ }|| j|< |jjD ],}|jdkrXd|j	krXt
||j	ƒ}| j|_qX|S )Nr   )ÚCapabilityBasedPartitionerT)Zallows_single_node_partitionZcall_moduleZfused_)Z!torch.fx.passes.infra.partitionerrå   rÐ   rh   rÏ   Zpartition_and_fuserP   rQ   r<   r#   rÈ   rä   Z_wrapped_call)	r7   rN   rc   rå   Zpartitioned_prim_graph_moduleZprim_graph_moduleZpartitionerr;   Zfused_moduler   r   r   Úcompiler  s"    
ý

zOrtBackend.compilec                 C   sF   | j jr:ddlm} ddlm} || j|| jjd�||ƒS |  ||¡S )zçIf ``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)Zfw_compilerZpartition_fnZdecompositions)	rÉ   rÂ   Zfunctorch.compilerç   Ztorch._dynamo.backends.commonrè   ræ   rÌ   rÍ   )r7   rN   rc   rç   rè   r   r   r   Ú__call__«  s    ý üzOrtBackend.__call__é   Ú%_OrtBackend__instance_cache_max_countÚ_OrtBackend__instance_cache)rÇ   r   c                    s’   t t dœdd„‰tˆ t ƒs(t f ˆ p$i Ž‰ t‡ ‡fdd„tjD ƒdƒ}|dkrŽttjƒtjk szt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.©r�   Úbc                 S   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 k	sT|jd k	rXdS | j|jkrhdS | jd k	rÌ|jd k	rÌ| jj|jjkoÊ| jj|jjkoÊ| jj	|jj	koÊ| jj
|jj
koÊ| jj|jjkS dS r±   )r¿   rÀ   rÁ   r›   rÂ   rÄ   rÃ   rà   rØ   Zdiagnostic_optionsrÎ   Zfake_contextrí   r   r   r   ÚreusableÎ  s<    
ÿ
þ
ý
ü
ûÿÿÿýûùz<OrtBackend.get_cached_instance_for_options.<locals>.reusablec                 3   s   | ]}ˆ|j ˆ ƒr|V  qd S r3   )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`   Únextr   rì   rd   rë   ro   rR   )rÇ   Úbackendr   rð   r   Úget_cached_instance_for_optionsÁ  s    $
þÿý	z*OrtBackend.get_cached_instance_for_optionsc                   C   s   t j ¡  d S r3   )r   rì   Úclearr   r   r   r   Úclear_cached_instances  s    z!OrtBackend.clear_cached_instancesc                   C   s
   t tjƒS r3   )rk   r   rì   r   r   r   r   Úget_cached_instances  s    zOrtBackend.get_cached_instances)N)N)rB   rC   rD   rE   r   r   r5   r$   rI   r½   r   r
   rF   r   r   rÔ   rä   ræ   ré   rë   r   rÅ   rì   r   Ústaticmethodr   ró   rõ   rö   r   r   r   r   r   S  s.   
	Lþ! ):þ ÿþF
rÆ   )rN   rÇ   c                C   s   t  |¡| |ƒS r3   )r   ró   )rN   rc   rÇ   r   r   r   r     s    )WÚdataclassesÚ	importlibÚloggingÚtypingr   r   r   r   r   r   r   r	   r
   r   Útyping_extensionsr   r$   Ztorch._CZ
torch._opsZtorch._prims.executorZtorch.fxZtorch._subclasses.fake_tensorr   Ztorch.fx._compatibilityr   Z torch.fx.passes.fake_tensor_propr   Z torch.fx.passes.operator_supportr   Ztorch.fx.passes.tools_commonr   Ztorch.utilsr   rÊ   r¡   Zonnxruntime.capir   r,   Úimport_moduleZ
torch.onnxZtorch.onnx._internalZ torch.onnx._internal.diagnosticsZtorch.onnx._internal.exporterZ+torch.onnx._internal.fx.decomposition_tableZtorch.onnx._internal.fx.passesZtorch.onnx._internal.fxr   Z"torch.onnx._internal.fx.type_utilsr   r   r   ÚImportErrorÚ__all__rK   r   rF   r!   r(   r)   r/   Ú	getLoggerrB   r>   r0   rI   r½   rU   rh   rm   rp   rr   ru   r~   rn   r‰   r�   r’   r¤   rª   r«   Ú	dataclassrµ   r   rÅ   r   r   r   r   r   r   r   Ú<module>   sÀ    0

û			
7-

  þ

÷2

÷;!$
9   @üü