o
    ߗiD                     @   s   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZmZ d dlmZ eeZG dd	 d	Zd
efddZdd Zdd Zd
ejfddZd
ejfddZdS )    N)patchdisable)TensorifyScalarRestartAnalysis)countersdefakeflatten_graph_inputs)aot_module_simplifiedSerializableAOTDispatchCompiler)_disable_current_modesc                   @   s(   e Zd ZdddZdejjfddZdS )	AotAutogradreturnNc                 K   s   d| _ || _d S )Ncompiler_fn)__name__kwargs)selfr    r   [/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/backends/common.py__init__   s   
zAotAutograd.__init__gmc              	   K   s  |rt d| tdd |D rt||| S t| jdr(| jd  | jd< td d  d7  < d}|rGt d	 td d
  d7  < |S dd }| jdpU| jd }t	|t
rb||j|_n||}|| jd< | jdpu| jd | jd< ddlm} ddlm} | jdd |krtdd}	nt }	zJ| < |	( t||fi | j}
td d  d7  < t|
W  d    W  d    W S 1 sw   Y  W d    W d S 1 sw   Y  W d S  ty     ty   td d
  d7  <  w )Nz3aot_autograd-based backend ignoring extra kwargs %sc                 s   s     | ]}t |tttfV  qd S N)
isinstancelisttupledict).0xr   r   r   	<genexpr>   s    z'AotAutograd.__call__.<locals>.<genexpr>decompositionsaot_autogradtotal   Fz5Unable to use AOT Autograd because graph has mutationnot_okc                    s    fdd}|S )Nc                     s   t t  | i |S r   r   )argsr   bw_compiler_fnr   r   _wrapped_bw_compiler4   s   zLAotAutograd.__call__.<locals>.wrap_bw_compiler.<locals>._wrapped_bw_compilerr   )r%   r&   r   r$   r   wrap_bw_compiler3   s   z.AotAutograd.__call__.<locals>.wrap_bw_compilerbw_compilerfw_compilerinference_compilerr   )nop)enable_aot_loggingz%functorch.compile.config.debug_assertTok)logwarninganyr   callabler   getr   debugr   r
   r   functorch.compiler+   torch._inductor.debugr,   r   
contextlibnullcontextr	   r   r   	Exception)r   r   example_inputsr   use_fallbackr'   r(   r+   r,   patch_configcgr   r   r   __call__   sR   


XzAotAutograd.__call__)r   N)r   
__module____qualname__r   torchfxGraphModuler=   r   r   r   r   r      s    
r   r   c                  K   s   t di | S )Nr   )r   )r   r   r   r   r   ]   s   r   c                 C   s0   ddl m}m}m} |||d}| r||d< |S )Nr   )default_decompositions#min_cut_rematerialization_partition
ts_compile)r)   r(   partition_fnr   )r4   rC   rD   rE   )use_decompsrC   rD   rE   r   r   r   r   mem_efficient_fusion_kwargsa   s   rH   c                    s   t   fdd}|S )zg
    Decorator for backends that need real inputs.  We swap out fake
    tensors for zero tensors.
    c                    sJ   t   ttt|} | |fi |W  d    S 1 sw   Y  d S r   )r   r   mapr   )modelinputsr   fnr   r   wrapper{   s   $z(fake_tensor_unsupported.<locals>.wrapper)	functoolswraps)rM   rN   r   rL   r   fake_tensor_unsupportedu   s   rQ   c                 C   "   | D ]}t |dr|j  S qd S )Ndevice)hasattrrS   r9   r   r   r   r   device_from_inputs   
   

rV   c                 C   rR   )Ndtype)rT   rX   rU   r   r   r   dtype_from_inputs   rW   rY   )r6   rO   loggingunittest.mockr   r@   torch._dynamor   torch._dynamo.excr   torch._dynamo.utilsr   r   r   torch._functorch.aot_autogradr	   r
   torch.utils._python_dispatchr   	getLoggerr   r.   r   r   rH   rQ   rS   rV   rX   rY   r   r   r   r   <module>   s"   
G