o
    Zh                     @   s   d Z 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 )a  
This module provides common utilities and base classes for TorchDynamo backends.

Key components:
- AotAutograd: Base class for implementing AOT (Ahead-of-Time) autograd backends
- Backend utilities for handling:
  - Fake tensor conversion
  - Device/dtype detection from inputs
  - Memory efficient fusion
  - Graph flattening
  - Common compiler configurations

The utilities here are used by various backend implementations to handle
common operations and provide consistent behavior across different backends.
AOT autograd functionality is particularly important as it enables ahead-of-time
optimization of both forward and backward passes.
    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   L/var/www/auris/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>1   s    z'AotAutograd.__call__.<locals>.<genexpr>decompositionsaot_autogradtotal   Fz5Unable to use AOT Autograd because graph has mutationZnot_okc                    s    fdd}|S )Nc                     s   t t  | i |S r   r   )argsr   bw_compiler_fnr   r   _wrapped_bw_compilerF   s   zLAotAutograd.__call__.<locals>.wrap_bw_compiler.<locals>._wrapped_bw_compilerr   )r$   r%   r   r#   r   wrap_bw_compilerE   s   z.AotAutograd.__call__.<locals>.wrap_bw_compilerbw_compilerfw_compilerZinference_compilerr   )nop)enable_aot_loggingz%functorch.compile.config.debug_assertTok)logwarninganyr   callabler   getr   debugr   r
   r   functorch.compiler)   Ztorch._inductor.debugr*   r   
contextlibnullcontextr	   r   r   	Exception)r   r   example_inputsr   Zuse_fallbackr&   r'   r)   r*   Zpatch_configZcgr   r   r   __call__-   sR   


XzAotAutograd.__call__)r   N)r   
__module____qualname__r   torchZfxZGraphModuler7   r   r   r   r   r   (   s    
r   r   c                  K   s   t di | S )Nr   )r   )r   r   r   r   r   o   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'   Zpartition_fnr   )r2   r;   r<   r=   )Zuse_decompsr;   r<   r=   r   r   r   r   mem_efficient_fusion_kwargss   s   r>   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   )modelZinputsr   fnr   r   wrapper   s   $z(fake_tensor_unsupported.<locals>.wrapper)	functoolswraps)rB   rC   r   rA   r   fake_tensor_unsupported   s   rF   c                 C   "   | D ]}t |dr|j  S qd S )Ndevice)hasattrrH   r6   r   r   r   r   device_from_inputs   
   

rK   c                 C   rG   )Ndtype)rI   rM   rJ   r   r   r   dtype_from_inputs   rL   rN   )__doc__r3   rD   loggingZunittest.mockr   r:   Ztorch._dynamor   Ztorch._dynamo.excr   Ztorch._dynamo.utilsr   r   r   Ztorch._functorch.aot_autogradr	   r
   Ztorch.utils._python_dispatchr   	getLoggerr   r,   r   r   r>   rF   rH   rK   rM   rN   r   r   r   r   <module>   s$   
G