
    Vh                        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j(                  e      Z G d	 d
      ZdefdZd Zd Zdej6                  fdZdej:                  fdZy)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                   H    e Zd ZddZdej
                  j                  fdZy)AotAutogradNc                      d| _         || _        y )Ncompiler_fn)__name__kwargs)selfr   s     M/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/backends/common.py__init__zAotAutograd.__init__)   s    %    gmc                    |rt         j                  d|       t        d |D              rt        |||       S t	        | j
                  j                  d            r! | j
                  d          | j
                  d<   t        d   dxx   dz  cc<   d}|r+t         j                  d       t        d   d	xx   dz  cc<   |S d
 }| j
                  j                  d      xs | j
                  d   }t        |t              r ||j                        |_        n ||      }|| j
                  d<   | j
                  j                  d      xs | j
                  d   | j
                  d<   ddlm} ddlm} | j
                  j                  dd       |k(  rt!        dd      }	nt#        j$                         }		  |       5  |	5  t'        ||fi | j
                  }
t        d   dxx   dz  cc<   t)        |
      cd d d        cd d d        S # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w# t*        $ r  t,        $ r t        d   d	xx   dz  cc<    w xY w)Nz3aot_autograd-based backend ignoring extra kwargs %sc              3   R   K   | ]  }t        |t        t        t        f       ! y wN)
isinstancelisttupledict).0xs     r   	<genexpr>z'AotAutograd.__call__.<locals>.<genexpr>1   s     Jaz!dE401Js   %'decompositionsaot_autogradtotal   Fz5Unable to use AOT Autograd because graph has mutationnot_okc                       fd}|S )Nc                  8    t         t              | i |      S r   r   )argsr   bw_compiler_fns     r   _wrapped_bw_compilerzLAotAutograd.__call__.<locals>.wrap_bw_compiler.<locals>._wrapped_bw_compilerF   s     6w~6GGHHr    )r*   r+   s   ` r   wrap_bw_compilerz.AotAutograd.__call__.<locals>.wrap_bw_compilerE   s    I ('r   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.compiler1   torch._inductor.debugr2   r   
contextlibnullcontextr
   r   r   	Exception)r   r   example_inputsr   use_fallbackr-   r.   r1   r2   patch_configcgs              r   __call__zAotAutograd.__call__-   s   KKMvVJ>JJ'  DKKOO$456,IDKK8H,I,KDKK() 	 )Q.)IIMN^$X.!3.I	( kkoom4RM8Rk#BC&6{7N7N&OK#*;7K%0M"KKOO01OT[[5O 	() 	*< ;;??=$/36 !H$OL%113L
	#% #| #*2~MM(.!3.r{# # # # # # # . 	 	^$X.!3.	sH   4H( ;H>6H4	H=	H( H	HH( H%!H( %H( ((I)returnN)r   
__module____qualname__r   torchfxGraphModulerC   r,   r   r   r   r   (   s    ?588// ?r   r   rD   c                      t        di | S )Nr,   )r   )r   s    r   r#   r#   o   s       r   c                 4    ddl m}m}m} |||d}| r||d<   |S )Nr   )default_decompositions#min_cut_rematerialization_partition
ts_compile)r/   r.   partition_fnr"   )r:   rL   rM   rN   )use_decompsrL   rM   rN   r   s        r   mem_efficient_fusion_kwargsrQ   s   s3      "!;	F #9 Mr   c                 B     t        j                          fd       }|S )zg
    Decorator for backends that need real inputs.  We swap out fake
    tensors for zero tensors.
    c                     t               5  t        t        t        |            } | |fi |cd d d        S # 1 sw Y   y xY wr   )r   r   mapr   )modelinputsr   fns      r   wrapperz(fake_tensor_unsupported.<locals>.wrapper   s>    #% 	/#ff-.FeV.v.	/ 	/ 	/s	   #9A)	functoolswraps)rW   rX   s   ` r   fake_tensor_unsupportedr[      s'     __R/ /
 Nr   c                 F    | D ]  }t        |d      s|j                  c S  y )Ndevice)hasattrr]   r?   r    s     r   device_from_inputsr`      s$     1h88Or   c                 F    | D ]  }t        |d      s|j                  c S  y )Ndtype)r^   rb   r_   s     r   dtype_from_inputsrc      s$     1g77Nr   )__doc__r<   rY   loggingunittest.mockr   rG   torch._dynamor   torch._dynamo.excr   torch._dynamo.utilsr   r   r	   torch._functorch.aot_autogradr
   r   torch.utils._python_dispatchr   	getLoggerr   r4   r   r#   rQ   r[   r]   r`   rb   rc   r,   r   r   <module>rm      s   $      ! < F F @ g!D DN!k !(%,,  r   