o
    ZhY                    @  s.  U d 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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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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!m"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/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZFmGZGmHZH ddlImJZJ ddlKmLZLmMZM ddlNmOZO ddlPmQZQmRZRmSZSmTZT ddlUmVZV ddlWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZw ddlmxZxmyZymzZzm{Z{m|Z|m}Z} ddl~mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dZded< zddlZW n ey   dZY nw erddlmZ eeZejedZejedZejedZejed ZG d!d" d"Zd#d$ Zedd%d& Zdad'ed(< d)d* Zdd/d0ZejjjZdd4d5Zdd9d:Zd;d< Zd=d> Zd?d@ ZdAdB ZejdCdDG dEdF dFZdGdH ZdIdJ ZejG dKdL dLZG dMdN dNejZedddQdRZG dSdT dTe<ZG dUdV dVZdWdX ZG dYdZ dZeZG d[d\ d\Zdd^d_Zd`da Zdbdc Zddde Zdfdg ZddidjZddqdrZddsdtZddwdxZdydz ZdddZeVeǃ dd Zdd ZddddZdS )a  
Core guard system for Dynamo that detects when compiled code needs to be recompiled due to
changes in program state. Guards are conditions that must remain true for previously-compiled
code to be valid for reuse.

This module provides the infrastructure for creating, managing and checking guards, including:
- Guard creation and composition
- Guard state management and invalidation
- Guard checking and failure handling
- Utilities for guard optimization and debugging
- Integration with Dynamo's compilation caching

The guard system is critical for Dynamo's ability to efficiently reuse compiled code while
maintaining correctness by detecting when recompilation is necessary due to changes in
program state, tensor properties, or control flow.
    )annotationsN)contextmanager)deepcopy)currentframe)AnyCallableOptionalTYPE_CHECKINGUnion)ReferenceType)code_framelocals_names)
check_obj_idcheck_type_iddict_versionDictGuardManager install_no_tensor_aliasing_guardinstall_object_aliasing_guard!install_storage_overlapping_guardinstall_symbolic_shape_guardprofile_guard_managerRootGuardManager)IndexedSource$is_from_flatten_script_object_sourceis_from_local_sourceis_from_optimizer_sourceTensorPropertyTensorPropertySource)CompileEventLogger)	CompileContext	CompileIdDuplicateInputsGuardGuardBuilderBaseGuardEnvExprGuardSourceSourceStorageOverlap)
structured)justknobs_check)EqualityConstraintis_symbolicSYMPY_INTERP)
OrderedSet)format_framereport_compile_source_on_error)TensorWeakRef   )configconvert_frameexcmutation_guard)set_guard_error_hook) AttrProxySource
AttrSourceCallFunctionNoArgsSourceCallMethodItemSourceChainedSourceConstantSourceConstDictKeySourceDefaultsSourceDictGetItemSourceFlattenScriptObjectSourceFloatTensorSourceFSDPNNModuleSourceGetItemSourceGlobalSourceGlobalStateSourceGlobalWeakRefSource
GradSourceListGetItemSourceLocalSourceNNModuleSourceNumpyTensorSourceOptimizerSourceScriptObjectQualifiedNameSourceShapeEnvSourceSubclassAttrListSourceTorchFunctionModeStackSourceTupleIteratorGetItemSource
TypeSource"UnspecializedBuiltinNNModuleSourceUnspecializedNNModuleSourceUnspecializedParamBufferSourceWeakRefCallSource)
CacheEntryDynamoFrameType
ExtraStateGuardedCode	GuardFailGuardFn)builtin_dict_keyscommon_constant_types	dict_keysget_custom_getattrget_torch_function_mode_stack get_torch_function_mode_stack_atguard_failuresistype	key_is_id	key_to_idnormalize_range_iterorig_code_maptensor_always_has_static_shapetuple_iterator_getitemtuple_iterator_lenunpatched_nn_module_getattrverify_guard_fn_signaturez#Optional[Callable[[Any, Any], Any]]guard_manager_testing_hook_fn)Symbolguards
recompilesrecompiles_verboseZverbose_guardsc                   @  s   e Zd ZdZdddZedd Zdd Zd	d
 Zdd Z	dd Z
dd ZdddZdd Zdd Zdd Zdd Zdd Zdd ZdS ) GuardManagerWrappera  
    A helper class that contains the root guard manager. An instance of this
    class is stored in the Dynamo cache entry, so that the cache entry can
    access the RootGuardManager stored in the "root" attribute and directly call
    the check_nopybind from C++.
    Nc                 C  sl   |d u r	t  | _n|| _d | _d | _d | _g | _d | _d | _d | _d | _	d | _
i | _g | _d| _t | _d S NT)r   rootdiff_guard_rootclosure_varsargs
code_partsverbose_code_partsglobal_scopeguard_fail_fncache_entryextra_stateid_matched_objsno_tensor_aliasing_sourcesprint_no_tensor_aliasing_guardr,   diff_guard_sources)selfrt    r   C/var/www/auris/lib/python3.10/site-packages/torch/_dynamo/guards.py__init__   s    
zGuardManagerWrapper.__init__c                 c  s$    d| _ z	d V  W d| _ d S d| _ w rs   )r   r   r   r   r   '_preserve_print_no_tensor_aliasing_flag   s
   z;GuardManagerWrapper._preserve_print_no_tensor_aliasing_flagc                   s:    fdd fddfdd j   jS )Nc                   sd   |    jv p|  dk}t|   D ]\}\}}|||B O }q|r0 j|    |S Nr   )
get_sourcer   
fail_countsortedget_key_value_managersitemsadd)nodeis_diff_guard_nodeidxkey_mgrval_mgrr   visitr   r   visit_dict_manager   s   
zJGuardManagerWrapper.collect_diff_guard_sources.<locals>.visit_dict_managerc                   sZ   t | trJ |   jv p|  dk}|  D ]}||O }q|r+ j|   |S r   )
isinstancer   r   r   r   get_child_managersr   )r   r   	child_mgrr   r   r   visit_manager   s   zEGuardManagerWrapper.collect_diff_guard_sources.<locals>.visit_managerc                   s&   | d u rdS t | tr | S | S )NF)r   r   r   )r   r   r   r   r      s
   
z=GuardManagerWrapper.collect_diff_guard_sources.<locals>.visit)rt   r   r   r   )r   r   r   r   r   collect_diff_guard_sources   s
   

z.GuardManagerWrapper.collect_diff_guard_sourcesc                 C  s   |    |   d S N)r   populate_diff_guard_managerr   r   r   r   finalize  s   zGuardManagerWrapper.finalizec                 C  s&   |  | j| _| jr| j  d S d S r   )clone_with_chosen_sourcesr   ru   r|   Zupdate_diff_guard_root_managerr   r   r   r   r     s   	z/GuardManagerWrapper.populate_diff_guard_managerc                   s    fdd}| j |S )Nc                   s   |    v S r   )r   )Znode_mgrchosen_sourcesr   r   	filter_fn     z@GuardManagerWrapper.clone_with_chosen_sources.<locals>.filter_fn)rt   Zclone_manager)r   r   r   r   r   r   r     s   z-GuardManagerWrapper.clone_with_chosen_sourcesc                   s&   |j j | } fdd|D }|S )Nc                   s   g | ]} d  | qS ): r   ).0partZ
guard_namer   r   
<listcomp>!  s    z7GuardManagerWrapper.get_guard_lines.<locals>.<listcomp>)	__class____name__ry   )r   guardpartsr   r   r   get_guard_lines  s   z#GuardManagerWrapper.get_guard_linesc                 C  s0   |  }|jj}|d | }|r|d| 7 }|S )Nz	: source=, )r   r   r   )r   guard_managerZaccessor_strsourcetsr   r   r   get_manager_line$  s   z$GuardManagerWrapper.get_manager_linec              	   C  s   t |  D ]H\}\}}|d|  | . |r.|d| |  | || |rA|d| |  | || W d    n1 sKw   Y  qd S )NzKeyValueManager pair at index=zKeyManager: zValueManager: )r   r   r   	writelineindentr   construct_manager_string)r   mgrbodyr   r   r   r   r   r   construct_dict_manager_string,  s   
z1GuardManagerWrapper.construct_dict_manager_stringc              
   C  s   |  f | D ]+}t|tjjjjr,| jr#d| _|	| 
| q	|	|jjg q	|	| 
| q	t|tr@| || t| | D ]\}}|| |d|   | || qIW d    d S 1 smw   Y  d S )NFzaccessed_by=)r   get_leaf_guardsr   torch_C_dynamoro   NO_TENSOR_ALIASINGr   
writelinesr   r   r   r   r   zipZget_accessorsr   r   r   reprr   )r   r   r   r   accessorr   r   r   r   r   8  s,   

"z,GuardManagerWrapper.construct_manager_stringc                 C  s   ddl m} G dd d|}|  B | }d|_|jddd |jd	dd |d
 | | j| t| jdrI| j D ]
}|	| 
| q>| W  d    S 1 sWw   Y  d S )Nr   IndentedBufferc                      s&   e Zd Zdd Zd fdd	Z  ZS )z=GuardManagerWrapper.__str__.<locals>.IndentedBufferWithPrefixc                 S  s   d| j | j  S )Nz| )_indenttabwidthr   r   r   r   prefixY     zDGuardManagerWrapper.__str__.<locals>.IndentedBufferWithPrefix.prefixFc                   s(   |r
t  | d S t  d|  d S )Nz+- )superr   )r   lineskip_prefixr   r   r   r   \  s   zGGuardManagerWrapper.__str__.<locals>.IndentedBufferWithPrefix.writelineF)r   
__module____qualname__r   r   __classcell__r   r   r   r   IndentedBufferWithPrefixX  s    r   r0    T)r   zTREE_GUARD_MANAGER:r   get_epilogue_lambda_guards)torch._inductor.utilsr   r   r   r   r   rt   hasattrr   r   r   getvalue)r   r   r   r   r   r   r   r   __str__U  s   


$zGuardManagerWrapper.__str__c                 C     | j |S r   )rt   checkr   xr   r   r   r   n     zGuardManagerWrapper.checkc                 C  r   r   )rt   check_verboser   r   r   r   r   r  r   z!GuardManagerWrapper.check_verbosec                   s,   ddd   fddj  d S )NFc                 S  s2   g }|   D ]}|dd  }|| q|S )N#r   )ry   splitrstripappend)Z
leaf_guardrx   verbose_code_part	code_partr   r   r   get_code_partsz  s
   zMGuardManagerWrapper.populate_code_parts_for_debugging.<locals>.get_code_partsc                   sd   |   D ] }t|tjjjjrsj | dqj | q| 	 D ]}| q)d S rs   )
r   r   r   r   r   ro   r   rx   extendr   )r   r   r   r   r   Ztensor_aliasing_guard_seenr   r   r   r     s   
zDGuardManagerWrapper.populate_code_parts_for_debugging.<locals>.visit)rt   r   r   r   r   !populate_code_parts_for_debuggingv  s   z5GuardManagerWrapper.populate_code_parts_for_debuggingr   )r   r   r   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rr      s"    

1
rr   c                 C  sN   t j  t| tjtjfrt | n| W  d    S 1 s w   Y  d S r   )r   Z	overridesZ_enable_torch_functionr   npZgenericZndarrayZ	as_tensor)ar   r   r   
from_numpy  s   $r   c                  C  sD   dd l } dd l} | jj| jjg}ddlm} || dd |D S )Nr   )POLYFILLED_MODULESc                 S  s   h | ]}t |qS r   )inspectgetfile)r   mr   r   r   	<setcomp>      z&uninteresting_files.<locals>.<setcomp>)Ztorch._dynamo.external_utilsZtorch._dynamo.polyfillsr   Zexternal_utilsZ	polyfillsZtorch._dynamo.polyfills.loaderr   r   )r   modsr   r   r   r   uninteresting_files  s   
r   zOptional[dict[str, object]]_CLOSURE_VARSc                   C  s   t d u rRi dtdtdtjjdtdtddd d	td
t	dt
dtdtjdtd u r2d ntjdtddtjdtjjdtjdttjttda t S )NZ___check_type_idZ___check_obj_idZ___odict_getitemZ___key_to_idZ___dict_versionZ___dict_containsc                 S  s   t || S r   )dict__contains__)r   br   r   r   <lambda>      z#_get_closure_vars.<locals>.<lambda>Z___tuple_iterator_lenZ___normalize_range_iterZ___tuple_iterator_getitemZ#___get_torch_function_mode_stack_at__math_isnan__numpy_isnaninfZ__load_moduleZutils_devicedeviceZ___from_numpy)Z___as_tensorr   r   )r   r   r   collectionsOrderedDict__getitem__re   r   rj   rf   ri   ra   mathisnanr   float	importlibimport_moduler   utils_devicer   r   _as_tensor_fullprecr   r   r   r   r   _get_closure_vars  sR   	
r
  r   ast.ASTreturnstrc                 C  s   t | ddS )N
r   )astunparsereplacer   r   r   r   _ast_unparse     r  r   r   r!   c                 C  sr   d}|d ur2|j r#t|j D ]}|jt vr!dt|dd } nqn|jr2dt|j d  }| d| S )Nr   z  # T)r   z<60)
user_stackreversedfilenamer   r-   stacksummary)r   r   extrafsr   r   r   get_verbose_code_part  s   r  rx   Union[str | list[str]]	list[str]c                   s"   t | ts| g}  fdd| D S )Nc                   s   g | ]}t | qS r   )r  )r   r   r   r   r   r     r   z*get_verbose_code_parts.<locals>.<listcomp>)r   list)rx   r   r   r  r   get_verbose_code_parts  s   
r!  c                 C  sF   g }| D ]}t |s|| qt|tjsJ ||j  q|S r   )r*   r   r   r   ZSymIntr   Zmaybe_as_int)Zsize_or_strideZ	converteddimr   r   r   convert_to_concrete_values  s   r#  c           
      C  sx   t | }tj| tj B tj  }| j}| jj}| j	}d| d|j
 d| d| d| d| d| d| d}	|	S )Nzcheck_tensor(r   z	, device=z, requires_grad=z, size=z	, stride=))typer   r   Z_dispatch_keysZ_dispatch_tls_local_include_setZ_dispatch_tls_local_exclude_setdtyper   indexrequires_gradr   )
valuenamesizesstridesZpytypeZdispatch_keyr&  Zdevice_indexr(  Z	guard_strr   r   r   get_tensor_guard_code_part  s$   r-  c                 C  s   t t| |S r   )r   r\   r'  )dctkeyr   r   r   get_key_index  s   r0  c                 C  s   d|  d| dS )Nlist(dict.keys())[]r   )r   r'  r   r   r   get_key_index_source  r  r4  T)frozenc                   @  s2   e Zd ZU dZded< dZded< dZded< dS )NNModuleAttrAccessorInfoFboolpresent_in_generic_dictNzOptional[str]l1_keyl2_key)r   r   r   r8  __annotations__r9  r:  r   r   r   r   r6  	  s   
 r6  c           
      C  s   | j  }t| jtr| jj}nt|tsJ t|| j}t||}tt	|| }t|t
tfr:| d|d}	n| d| d}	t| jts^|j||| jtjd| j| d|g |j||	||dS )N[r3  r'  r   example_valueguard_manager_enum == )baser*  r   r'  r<   r   r0  r4  r   r\   intr  get_key_managerGuardManagerTypeGUARD_MANAGERadd_equals_match_guardget_value_manager)
r   base_guard_managerbase_example_valuer>  r?  base_source_namer'  
key_sourceZkey_example_valuevalue_sourcer   r   r   getitem_on_dict_manager  s4   


rM  c                 C  s4   | j }t|tr
dS |  rdS | ot|t S NFT)originating_sourcer   rJ   is_specialized_nn_moduleZis_dict_keyrF   )r   r   r   r   r   match_on_id_for_tensorA  s   
rQ  c                   @     e Zd ZU ded< ded< dS )GuardCodeListr  	code_listr!   r   Nr   r   r   r;  r   r   r   r   rS  P     
 rS  c                   @  s   e Zd ZdZdZdS )rD  r0      N)r   r   r   rE  DICT_GUARD_MANAGERr   r   r   r   rD  V  s    rD  codetypes.CodeTypec                 C  s   t tt| S r   )r   r  r   )rY  r   r   r   &code_framelocals_names_reversed_cached[  s   r[  c                   @  s  e Zd ZdddZdd Zdd Zedd Zdd Zdd Z	dd Z
dd Zd d! Zd"d# Zdd&d'Z	(	)dd*d+Zddd2d3Zdd5d6Zdd8d9Zdd:d;Zddd=d>Zdd?d@ZddAdBZddFdGZddHdIZdddJdKZddLdMZddNdOZddPdQZddRdSZddTdUZddVdWZddXdYZ ddZd[Z!dd\d]Z"dd^d_Z#dd`daZ$ddbdcZ%ddddeZ&dfdg Z'dhdi Z(djdk Z)dldm Z*dndo Z+dpdq Z,drds Z-dtdu Z.ddvdwZ/ddxdyZ0ddzd{Z1dd|d}Z2dd~dZ3dddZ4dddZ5ddddZ6dddZ7d(S )GuardBuilderf_coderZ  id_refCallable[[Any, str], str]
source_refCallable[[Source], str]lookup_weakrefs)Callable[[object], ReferenceType[object]]local_scopedict[str, object]rz   r   rr   check_fn_managerCheckFunctionManagerc	                 C  s   || _ || _|| _|| _||d| _tj | jd< tj	j
j D ]\}	}
|	dddddd}	|
| jd |	< |
| j|	< q!|| _g | _g | _g | _g | _g | _|| _t | _| jjjD ]}| jt| | q^i | _i | _t | _d S )NLG__builtins__>_<.Z_dot_)r]  r^  r`  rb  scopebuiltins__dict__copyr   packageZpackage_importerZ_package_imported_modulesr   r  r   argnamesrY  shape_env_codeno_tensor_aliasing_names!no_tensor_aliasing_guard_managersrf  setkey_order_guarded_dict_idsoutput_graphZguard_on_key_orderr   idgetr~   _cached_guard_managers_cached_duplicate_input_guards)r   r]  r^  r`  rb  rd  rz   r   rf  r*  Zpackage_modulesource_namer   r   r   r   a  s6   zGuardBuilder.__init__c           	      C  s   |  |}t|trtd|j |j }t|D ]"}|| }t|j|d}| ||}|j	|| d|d||d qd S )NzsNot expecting a DictGuardManager. Seems like Dynamo incorrectly added the dict to tx.output.guard_on_key_order for )r'  r<  r3  r/  r   r>  r?  )
get_guard_managerr   r   NotImplementedErrorr*  rO  r\   r>   get_guard_manager_typedict_getitem_manager)	r   r>  r   dict_mgrZdict_sourcer/  r)  rL  r?  r   r   r   #guard_on_dict_keys_and_ignore_order  s*   


z0GuardBuilder.guard_on_dict_keys_and_ignore_orderc           	   
   C  s   |  |}t|tstd|j t|tsJ tt|D ]=\}}t|j|}|j|||t	j
d}t|rN| ||}||td| d| d| q||t| d|| qd S )Nz_Expecting a DictGuardManager. Seems like Dynamo forgot to set the right guard manager enum for r=  z__check_obj_id(r   r$  r@  )r  r   r   r  r*  	enumerater\   r4  rC  rD  rE  rd   r^  add_id_match_guardr!  rF  )	r   r)  r   r  r   r/  rK  Zkey_managerid_valr   r   r   guard_on_dict_keys_and_order  s8   

z)GuardBuilder.guard_on_dict_keys_and_orderc                 C  s.   t jrtjdkrtjdk rtdt d S | S )N)      )r  r  r0   zMGuards may run slower on Python 3.13.0. Consider upgrading to Python 3.13.1+.)r1   Zissue_3_13_0_warningsysversion_infowarningswarnRuntimeWarning)r>  r   r   r   '_get_generic_dict_manager_example_value  s   

z4GuardBuilder._get_generic_dict_manager_example_valuec                 C  s  dd }|j }	|j}
t }t|jD ]
}||j  qtddd}|	|
v r/td|	d}n6d|
v r@|	|
d v r@tdd|	}n%d|
v rQ|	|
d v rQtdd|	}n|	|vred|
v re|	|
d v retdd|	}|j	sr|j
|j |||d	S |jswJ |j}|j}| d
}d }}d }}d }}|rt|j|}| }|
| }| ||}|}|}| ||}n
|}|}| ||}|j|| |
tjd}|||||
||d}|r|||||||dS |S )a(  
        This tries to avoid calling the expensive nn module custom getattr method by
        checking if the attribute is accessible via __dict__. For attributes that
        are not accessible via __dict__ (like descriptors), we fallback to
        PyObject_GetAttr.

        There are two cases that we optimize for
        1) attributes present directly in __dict__, e.g training.
        2) parameters/buffers/modules - they can be accessed via _parameters,
        _buffers, _modules keys in __dict__. For example, mod.linear can be
        accessed as mod.__dict__["_parameters"]["linear"]

        The most common and expensive case for nn module guards is of type
        mod.submod1.submod2.submod3.training. We avoid the python getattr of nn
        modules by going through the __dict__.
        c                 S  sr   t | tr0t||}d| d|d}| j|||tjd|| d|g | j||||dS | j||||dS )Nr1  r2  r3  r=  r@  r  )	r   r   r0  rC  rD  rE  rF  rG  r  )r   r/  r  rI  r>  r?  r'  rK  r   r   r   getitem_on_dict_mgr  s,   

z>GuardBuilder.getattr_on_nn_module.<locals>.getitem_on_dict_mgrFNT_parameters_buffersZ_modulesattrr   r>  r?  	.__dict__r   r>  r?  )r   r/  r  rI  r>  r?  )memberrr  ry  r   getmror   updatekeysr6  r8  getattr_managerr9  r:  r7   rA  r*  r  get_generic_dict_managerr  rD  rE  )r   r   rH  rI  r>  rJ  r  r?  r  	attr_namemod_dictZall_class_attribute_namesr   Zaccessor_infor9  r:  mod_dict_sourceZl1_source_nameZl2_source_nameZl1_valueZl2_valueZl1_guard_manager_enumZl2_guard_manager_enumZ	l1_sourcemod_generic_dict_managerZl1_mgrr   r   r   getattr_on_nn_module  s    

	z!GuardBuilder.getattr_on_nn_modulec                 C  s,   |  }|dkr
dS t| |}|| jv S )Nr   F)r*  r|  r}  rz  )r   r   r  obj_idr   r   r   requires_key_order_guarding  s
   
z(GuardBuilder.requires_key_order_guardingc                 C  s<   t j}| |rt|trt j}|S t|tsJ t j}|S r   )rD  rE  r  r   r^   rX  r   )r   r   r>  r?  r   r   r   r    s   

z#GuardBuilder.get_guard_manager_typec                 C  s
   |t jkS r   )rD  rX  )r   Zmgr_enumr   r   r   manager_guards_on_keys     
z#GuardBuilder.manager_guards_on_keysc                 C  s$   | j jj| jd d| jd tjdS )Nrj  )	f_globalsr   r>  r?  )r   rt   Zglobals_dict_managerrp  rD  rE  r   r   r   r   get_global_guard_manager  s   z%GuardBuilder.get_global_guard_managerc              	     s  | j j}d }  }|dkr|| jv r| j| S |dkr!| |}|  |}d }d }d }tj}	t t	rL j
 }| |}|  j
}|  j
|}	t trtjrst| j}
t|
|
 j d }|j j|f|||d}n|j j|||d}nt tr|  j j|||d}nt tr|  j j|||d}nt tr|S t tr|S t tr|sJ |j|||d}nt t t!t"t#t$fr|sJ |}nt t%r|j& fdd|||d}nt t'r|sJ |j(|||d}nwt t)t*fr4|sJ t|t+j,j-r(t.|t/u r(| 0 ||||||}nN|j1 j2|||d	}nBt t3ry|s?J t|t4t5j6fsJJ t|t7rb| 8|	sXJ t9 ||||}nt jt:rmt;d
|j j|||d}nt t<rt=|d j>j?  d j@|||d}nt tAr|sJ |jB j@|||d}nt tCr|sJ |jD j|||d}nt tEr|sJ t|t4t5j6frJ dt|tFr jGs|jD j|||d}nt|tHr jGs|jI j|||d}no j} jGr J }|jK||||d}nYt tLrr|s(J tM|s/J  jNsG|jO||jPtjdjK jQ|||d}n/|jR}|d usQJ |d }|jS||tjd}t|t7rfJ |j jQ|||d}nt tTr|s}J |j&tU|||d}nt tVr|sJ |j&dd |||d}nt tWr|sJ |j&dd |||d}nt tXr|sJ |j&dd |||d}nt tYr|sJ |j&dd |||d}nt tZr|sJ |j&dd |||d}n|t t[r|sJ |j&dd |||d}net t\r'|sJ |j] j|||d}nOt t:rBt|t7s7t^d|j_ j|||d}n4t t`rV|sMJ |ja|||d}n t tbrj|saJ |jc|||d}nt^d  d   || j  < |S )Nr   r0   r  )global_namer   r>  r?  r  c                   s   t   S r   )ra   Z
_get_index)rm  r   r   r   r     s    z<GuardBuilder.get_guard_manager_from_source.<locals>.<lambda>)Zpython_lambdar   r>  r?  r  zUExpecting clean index here. Likely Dynamo forgot to mark a dict as guard_on_key_orderZtensor_property__manager)r   r   r>  r?  zUse DictGetItemSourcez.__kwdefaults__c                 S  s   |   d S r   __tensor_flatten__r   r   r   r   r     r   c                 S     |   S r   )Z__obj_flatten__r  r   r   r   r         c                 S  s   |    S r   )_typeZqualified_namer  r   r   r   r     r   c                 S  r  r   )Zget_baser  r   r   r   r     r  c                 S  r  r   )itemr  r   r   r   r     r  c                 S  s
   t | S r   )r   r	  r  r   r   r   r     s   
 r=  z4ConstDictKeySource can only work on DictGuardManagerzmissing guard manager builder z - )dr   rt   r*  r~  r}  r  rD  rE  r   r:   rA  get_guard_manager_from_sourcerc   rH   r1   Z!enable_cpp_framelocals_guard_evalr[  r]  lenr'  
local_nameZframelocals_managerr  rC   r  r  rE   Zglobal_weakref_managerrD   rM   rQ   Ztype_managerrK   rI   rS   rR   rA   rO   Zlambda_managerrF   Zgrad_managerr7   rT   r   nnModuler_   rk   r  r  r  r>   r   r   r   r   r  rM  r<   RuntimeErrorr   getattrproplowerr   r   Zindexed_managerrG   Zlist_getitem_managerrB   r   Zindex_is_slicetupleZtuple_getitem_managerZunpack_sliceZgetitem_managerr=   callableZis_kwZfunc_defaults_manager__defaults__Zidx_key__kwdefaults__Zfunc_kwdefaults_managerrJ   r   rN   r?   rL   r6   r9   r@   rP   Ztuple_iterator_getitem_managerAssertionErrorrC  rU   Zweakref_call_managerr8   Zcall_function_no_args_manager)r   r   Zroot_guard_managerr>  r  r?  rJ  rI  rH  Zbase_guard_manager_enumZframelocals_names_reversedZframelocals_idxoutr'  
kwdefaultsZ	kw_sourcer  r   r  r   r    sx  




























	





















z*GuardBuilder.get_guard_manager_from_sourcer   r!   c                 C  s   |  |jS r   )r  rO  r   r   r   r   r   r    r   zGuardBuilder.get_guard_managerNTc                 C  s   |d u rt  }d| }t||\}}i }d| jd i}	td| t||	| |d |  }
|r>| j	j
|
| d S | j	j
|
| d S )Nr   rj  zPython shape guard function:
%sZ___make_guard_fn)r
  joinr  build_guard_functionrp  
guards_logdebugexecvaluesr   rt   Zadd_epilogue_lambda_guardadd_lambda_guard)r   rx   ry   rv   Zis_epilogueZmake_guard_fn_argsZ_guard_bodyZpycoder  globals_for_guard_fnguard_fnr   r   r   $add_python_lambda_leaf_guard_to_root  s   z1GuardBuilder.add_python_lambda_leaf_guard_to_rootr*  r  rv   Optional[dict[str, Any]]r  r   c                 C  s   |d u rt  }t|| j|S r   )r
  evalrp  )r   r*  rv   r   r   r   r}  *  s   zGuardBuilder.getUnion[str, Guard]c                 C  s^   t |tr|}n|j}t|}|| jvr-tjj|}|r-|dkr't	
d| | j| |S )NrW  zinvalid var name: %s)r   r  r*  strip_function_callru  r   r   r   Zis_valid_var_namelogwarningr   )r   r   r*  rA  Zis_validr   r   r   arg_ref4  s   

zGuardBuilder.arg_refr  c                 C  s.   t |j|}t|||j|jd}||  d S )N)r  r  )r7   rO  r!   r  r  create)r   r   r  r  Zattr_sourceZ	new_guardr   r   r   _guard_on_attributeD  s
   z GuardBuilder._guard_on_attributec              	   C  s8  |j }t|tr|j}t|tsJ d|j |j}| }|j}| |}t| 	||}d }|r=d| d|d}n	d| d|d}| j
||g| 	|d | |}	|r| 	| }
| 	|}| ||
}t|tjjrt|tu r| ||	||
|| |S |	j||j|
|d d S |	|t|| d S )Nzinvalid source hasattr(r   r$  znot hasattr()provided_guarded_objectr  )rO  r   rI   rA  r7   r*  r  r  r   r}  _set_guard_export_infor  r  r   r  r  r_   rk   r  r  add_no_hasattr_guardr!  )r   r   r   Zbase_sourcerA  r  refvalrY  base_managerr>  rI  r?  r   r   r   HASATTRM  sT   





zGuardBuilder.HASATTRNonec           	      C  s   |d usJ |  |}| |j}t|tjjsJ | |}|j d}|j|| 	|j
tjd}d|d| d}|d|t|| d S )Nr  r  znot ___dict_contains(r   z
.__dict__)F)r  r}  r*  r   r   r  r  r  r  r  rr  rD  rE  add_dict_contains_guardr!  )	r   r   r  r  r  r  r  r  rY  r   r   r   NOT_PRESENT_IN_GENERIC_DICT  s   


z(GuardBuilder.NOT_PRESENT_IN_GENERIC_DICTc                 C  sh   t | |j}| |d|j d}d| | d| d}| ||g | ||t|| d S )Ntype(r$  z___check_type_id(r   )	r%  r}  r*  r^  r  r  r  Zadd_type_match_guardr!  )r   r   r   r  rY  r   r   r   
TYPE_MATCH  s   

zGuardBuilder.TYPE_MATCHc                 C  s`   |  |}| |j}t| |j}d| d| }| ||g | ||t|| d S )Nz___dict_version() == )r  r}  r*  r   r  r  Zadd_dict_version_guardr!  )r   r   r  r  versionrY  r   r   r   DICT_VERSION  s   


zGuardBuilder.DICT_VERSIONr/  invertr7  c                 C  sZ   |  |}|r	dnd}| d|d| d}| ||g | || |t|| d S )Nnot r   z___dict_contains(r   r$  )r  r  r  r  r!  )r   r   r/  r  Zdict_refZ	maybe_notrY  r   r   r   DICT_CONTAINS  s   

zGuardBuilder.DICT_CONTAINSc                 C  s   t |jtr| t|jjtjS | |}| |j	}| 
||j	}d| d| d}| ||g | ||t|| t |jtr`t |tjjrb|jj}| |}|d urd|| j|< d S d S d S d S )Nz___check_obj_id(r   r$  )r   rO  rQ   r  r!   rA  r\  r  r}  r*  r^  r  r  r  r!  rH   r   r  r  r  rb  r~   )r   r   r  r  r  rY  r  Zweak_idr   r   r   ID_MATCH  s*   



zGuardBuilder.ID_MATCHc                 C  sX   |  |}| |j}t|tjsJ | d}| ||g | |t	|| d S Nz is not None)
r  r}  r*  r   r   Tensorr  r  add_not_none_guardr!  )r   r   r)  r  r  rY  r   r   r   NOT_NONE_MATCH  s   


zGuardBuilder.NOT_NONE_MATCHc                 C  sT   |  |}| |j}t|tjjsJ | d|d}| ||t	|| d S )Nz.raw_repr() == z.raw_repr())
r  r}  r*  r   r   r   ZDispatchKeySetr  Zadd_dispatch_key_set_guardr!  )r   r   r  r  rx   r   r   r   DISPATCH_KEY_SET_MATCH  s   


z#GuardBuilder.DISPATCH_KEY_SET_MATCHc                 C  s   |  |dtj d S )Nr   )r  r\  EQUALS_MATCHr  r   r   r   
NAME_MATCH  s   zGuardBuilder.NAME_MATCHc                 C  sN   |  |j}| | d|  }| ||g | ||t|| d S )Nz.data_ptr() == )r}  r*  r  Zdata_ptrr  r  Zadd_data_ptr_guardr!  )r   r   objrY  r   r   r   DATA_PTR_MATCH  s   

zGuardBuilder.DATA_PTR_MATCHc                   sT   t jjj d  g}| ||g t jj fdd}| jj|t|| d S )Nz,torch.autograd.forward_ad._current_level == c                   s
   j  kS r   )_current_levelr  Z
dual_level
forward_adr   r   fn  r  z#GuardBuilder.DUAL_LEVEL.<locals>.fn)	r   Zautogradr  r  r  r   rt   r  r!  )r   r   rY  r  r   r  r   
DUAL_LEVEL  s   

zGuardBuilder.DUAL_LEVELc                   sf   t jj }dd |D d dg}| || t jjj  fdd}| jj|t	|| d S )Nc                 S  s   g | ]}|  qS r   )Z	get_state)r   cir   r   r   r         z6GuardBuilder.FUNCTORCH_STACK_MATCH.<locals>.<listcomp>z5torch._functorch.pyfunctorch.compare_functorch_state(r$  c                   s    S r   r   r  Z
compare_fnZstatesr   r   r    s   z.GuardBuilder.FUNCTORCH_STACK_MATCH.<locals>.fn)
r   Z
_functorchZpyfunctorchZ#retrieve_all_functorch_interpretersr  Zcompare_functorch_stater   rt   r  r!  )r   r   ZcisrY  r  r   r  r   FUNCTORCH_STACK_MATCH  s   

z"GuardBuilder.FUNCTORCH_STACK_MATCHc                   s   |  |jt|  |j d  tdr#t  fdd}n fdd}dt| dt  }| 	|
|t|| d S )Nr0   __metadata_guard__c                   s     |  d S Nr0   )r   r  r  original_metadatar)  r   r   metadata_checker#  s   zEGuardBuilder.TENSOR_SUBCLASS_METADATA_MATCH.<locals>.metadata_checkerc                   s   |   d  kS r  r  r  )r  r   r   r  *  r   Z___check_metadata_Z_c)r}  r*  r   r  r   rl   r|  r   current_compile_idr  r  r!  )r   r   r  r  r   r  r   TENSOR_SUBCLASS_METADATA_MATCH  s   


z+GuardBuilder.TENSOR_SUBCLASS_METADATA_MATCHc                 C  s   |  |}| |j}tr&tjtjtjtjtjtj	tj
tjtjtjtjf}nd}ttf}tttttttttjh||B }tj r[ddlm} ddlm}m }	m!}
 ||
|	||f }dd l"m#  m$} t%||sy|&t|syJ dt| t%|t'rt()|r| *| g }|+d| d | ,|| | -|.t/ d t0|| d S t%|t1rt)|r| *| g }|+d	| d | ,|| | -|.t/ d
 t0|| d S | d|g}t%||rt2|}| -|3|t0|| | ,|| d S )Nr   r   )
DeviceMesh)Partial	ReplicateShardzUnexpected type z__math_isnan(r$  r   z__numpy_isnan(r   r@  )4r  r}  r*  r   Zint8Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64Zfloat16Zfloat32Zfloat64r   ry  r  r]   r%  	frozensetsliceranger^   r   SizedistributedZis_availableZtorch.distributed.device_meshr  Z(torch.distributed.tensor.placement_typesr  r	  r
  Ztorch.utils._pytreer  _pytreerc   Zis_constant_classr  r  r  r  r   r  r  r  r
  r!  complexr   rF  )r   r   r  r  Znp_typesZok_mutable_typesZok_typesr  r  r	  r
  ZpytreerY  r   r   r   r  2  s   
	







zGuardBuilder.EQUALS_MATCHc                 C  s>   |  |j}t|ttd tjfr| | d S | | d S r   )	r}  r*  rc   r7  r%  typesCodeTyper  r  r   r   r  r   r   r   CONSTANT_MATCH  s   zGuardBuilder.CONSTANT_MATCHc                 C  sh   |  | | |j}t|dr"t|jtsJ | |dtj	 d S t
jdddt| ddgd d S )Ntrainingz-Attempted to guard on uninitialized nn.Moduler   zKAttempted to setup an NN_MODULE guard on uninitialized nn.Module subclass `z`.zIEnsure the `nn.Module` subclass instance has called `super().__init__()`.)Zgb_typecontextZexplanationhints)r  r}  r*  r   rc   r  r7  r  r\  r  r3   Zunimplemented_v2r%  r  r   r   r   	NN_MODULE  s   


zGuardBuilder.NN_MODULEc                 C  
   |  |S )z0things like torch.add and user defined functions)r  r  r   r   r   FUNCTION_MATCH  s   
zGuardBuilder.FUNCTION_MATCHc                 C  sV   |  |j}t|tjkr$t|dr$| |dtj | |dtj	 dS | 	| dS )z!matches a closure by __code__ id.__code__N)
r}  r*  r%  r  FunctionTyper   r  r\  r  r  r  r   r   r   CLOSURE_MATCH  s
   zGuardBuilder.CLOSURE_MATCHc                 C  r  r   r  r  r   r   r   BUILTIN_MATCH  r  zGuardBuilder.BUILTIN_MATCHc                 C  r  r   r  r  r   r   r   PYMODULE_MATCH  r  zGuardBuilder.PYMODULE_MATCHc                 C  s   |  |}| |j}t|ts| | g }t|dkr&|d|  n|d| dt|  | || t|trN| 	|
t|t|| d S | 	|t|t|| d S )Nr   r  zlen(r  )r  r}  r*  r   r   r  r  r   r  r  Zadd_dict_length_check_guardr!  Zadd_length_check_guardr   r   r  r)  rY  r   r   r   SEQUENCE_LENGTH  s    





zGuardBuilder.SEQUENCE_LENGTHc                 C  s   |  |}| |j}t|}g }|d| dt|  | || t|}| |d|j d}| |	t||t
|| d S )Nz___tuple_iterator_len(r  r  r$  )r  r}  r*  r%  r   rj   r  r^  r  Zadd_tuple_iterator_length_guardr!  )r   r   r  r)  r   rY  r  r   r   r   TUPLE_ITERATOR_LEN  s   

zGuardBuilder.TUPLE_ITERATOR_LENc              	   C  s   |  |}| |j}t|}g }t|}|d| d|  | || t|}| |d|j d}|\}}	}
| |	||	|
|t
|| d S )Nz___normalize_range_iter(r  r  r$  )r  r}  r*  r%  rf   r   r  r^  r  Zadd_range_iterator_match_guardr!  )r   r   r  r)  r   rY  Znormalized_range_iterr  startstopstepr   r   r   RANGE_ITERATOR_MATCH  s   


z!GuardBuilder.RANGE_ITERATOR_MATCHc                 C  s   |  |}|  | }t|jst|rd S ||f}|| jv r"d S | j||f | j||f | d| g}| || t| || 	|t
|| d S )N is )r  r*  r   rO  r  r   r  r   r  r  r!  )r   r   source_bZref_aZref_br/  rY  r   r   r   DUPLICATE_INPUT  s(   

zGuardBuilder.DUPLICATE_INPUTc                 C  s8   |  | dg}| || | |t|| d S r  )r  r  r  r  r!  )r   r   rY  r   r   r   WEAKREF_ALIVE  s
   
zGuardBuilder.WEAKREF_ALIVEc                 C  sZ   |  |}| |j}g }|d| dt|   | || | ||| dS )z7Guard on the key order of types.MappingProxyType objectzlist(z.keys()) == N)	r  r}  r*  r   r   r  r  r  Zadd_mapping_keys_guardr"  r   r   r   MAPPING_KEYS_CHECK  s   
zGuardBuilder.MAPPING_KEYS_CHECKc                 C  s   |  |}| |j}|tjjju r| | dS | | g }|	d| dt
t| | || | |jrC| || dS | || dS )z6Insert guard to check that the keys of a dict are sameNr1  z)) == )r  r}  r*  r   r  r  ZSUPPORTED_NODESr  r#  r   r   r\   r  r  rO  r  r  r"  r   r   r   DICT_KEYS_MATCH(  s   


zGuardBuilder.DICT_KEYS_MATCHc                 C  s   t jrdS | | dS )z\Special guard to skip guards on empty hooks. This is controlled by skip_nnmodule_hook_guardsN)r1   skip_nnmodule_hook_guardsr#  r  r   r   r   EMPTY_NN_MODULE_HOOKS_DICTA  s   z'GuardBuilder.EMPTY_NN_MODULE_HOOKS_DICTc                 C  s   t | |j| j d S r   )r4   watchr}  r*  rf  r  r   r   r   OBJECT_MUTATIONH  s   zGuardBuilder.OBJECT_MUTATIONc                 C     d S r   r   r  r   r   r   	GRAD_MODEK     zGuardBuilder.GRAD_MODEc                 C  r3  r   r   r  r   r   r   DETERMINISTIC_ALGORITHMSN  r5  z%GuardBuilder.DETERMINISTIC_ALGORITHMSc                 C  r3  r   r   r  r   r   r   TORCH_FUNCTION_STATEQ  r5  z!GuardBuilder.TORCH_FUNCTION_STATEc                 C  r3  r   r   r  r   r   r   FSDP_TRAINING_STATET  r5  z GuardBuilder.FSDP_TRAINING_STATEc                 C  sV   |j tju sJ ddlm  m} d|jg}| || | |	t
|| dS )z/Guard on CURRENT_DEVICE per torch.utils._devicer   Nzutils_device.CURRENT_DEVICE == )r   r$   GLOBALtorch.utils._devicer  r  ZCURRENT_DEVICEr  r  Zadd_default_device_guardr!  )r   r   r   rY  r   r   r   DEFAULT_DEVICEW  s   
zGuardBuilder.DEFAULT_DEVICEc                   s2  |j dksJ jjjdd D fdd}jrXi }g }g }i }t }jD ]}|jjv rDtj	j
||j||||| q,td q,t||t| |dd nd   fd	d
}	tjro|	d\}
}}n|	d\}
}jjj	sj  |
jD ]	}||g qt  }r|j|j tjrdd l}ddlm} |sJ |j|j}}|sd S g }g }d}| D ]6\}}t |t!rd}qj"|  i t#t$ d}t |t%r|&||f qt |t'r|&||f qd}q|st(|| }zdfddt)|D }d*dd t)|D }d*dd t)|D }|r-d| d}|r6d| d}t+,d| d| dd*| d}t-.d||j |/|}|0|j1|j2j3}|scJ W n tj4j5j6yq   Y nw t7|t8|t8||||j d S |
jrj9|
j|ji t#t$ d d S d S )Nr   c                 S     g | ]}|j qS r   )Zsymbolic_contextr   r   r   r   r   r   k      z*GuardBuilder.SHAPE_ENV.<locals>.<listcomp>c                   s    fddj |  D S )Nc                   s   g | ]	}t |tj qS r   )r   r   ZSIZE)r   r   r"  r   r   r   p  s    z?GuardBuilder.SHAPE_ENV.<locals>.get_sources.<locals>.<listcomp>)tracked_fakes_id_to_source)t_idr"  )r{  r?  r   get_sourcesm  s   
z+GuardBuilder.SHAPE_ENV.<locals>.get_sourcesz+Untracked tensor used in export constraintsF)source_pairsderived_equalitiesphantom_symbolsrelaxed_sourcesZ	warn_onlyc              	     s8   j jdd D dd D  jjjj | dS )Nc                 S  r<  r   )Zfaker=  r   r   r   r     r>  zCGuardBuilder.SHAPE_ENV.<locals>._get_code_parts.<locals>.<listcomp>c                 S  r<  r   r  r=  r   r   r   r     r>  )input_contextsequalities_inputsr`  Zignore_staticlangs)	shape_envZproduce_guards_verboser`  rf  r{  export)rI  rH  r  rG  r{  r   r   r   _get_code_parts  s   
z/GuardBuilder.SHAPE_ENV.<locals>._get_code_parts)pythonverbose_pythoncpp)rN  rO  r   )CppCodeCacheT)rv   c                   s    g | ]\}}  t||qS r   )r  r   )r   ir   r   r   r   r     s    r   c                 s  (    | ]\}\}}| d | dV  qdS )z = int_values[r3  Nr   r   rR  rm  symbolr   r   r   	<genexpr>  
    

z)GuardBuilder.SHAPE_ENV.<locals>.<genexpr>c                 s  rS  )z = float_values[r3  Nr   rT  r   r   r   rV    rW  zint64_t ;zdouble z
                    #include <cstdint>
                    #include <cmath>
                    #include <c10/util/generic_math.h>

                    extern "C" int8_t guard(int64_t *int_values, double *float_values) {
                      z
                      z
                      return (z) && (z-);
                    }
                    zC++ shape guard function: %s %s):r*  rf  r{  Ztracked_fakesZexport_constraintsry  rA  r@  r   rK  Zdynamic_shapesZ_process_equalitiesrJ  r  r  r)   r   r  r1   Z enable_cpp_symbolic_shape_guardsfreezeexprsr  r   Ztry_getZshape_env_guardsr   ctypesZtorch._inductor.codecacherQ  source_to_symbolr   r   r;   r}  r+   r
  rB  r   r  r   r  r  textwrapdedentr  r  loadcastr   Zc_void_pr)  Z	_inductorr3   ZInvalidCxxCompilerr   r  r  )r   r   rB  namesrC  rD  rE  rF  
constraintrM  Zpython_code_partsry   Zcpp_code_partsrY  Zcompile_contextr[  rQ  rx   r\  Zint_source_to_symbolZfloat_source_to_symbolZpython_fallbackr   rU  r>  Zguard_managersZint_symbols_strZfloat_symbols_strZfunc_strZclibZcguardr   rL  r   	SHAPE_ENVc  s   












zGuardBuilder.SHAPE_ENVc              
   C  sp  t jr	| r	d S t|r| | d S t|tr| }|d ur"|n| |j}t|t	j
s0J | |}g }| jjjr{| | g d}|D ]2}| |d | }t|t	jt	jfrl|d| d| dt| qG|| d| d|  qGnZ| |}t jrt|t	jjst|jts| j| | j| | jj}	|	j|j }
t|
d }t|
d }tt |||||}|!||||| t|t	jjs| jj"j#$|j |j%d usJ t&|d|jd	\}}|s't'|d
r|j(}d| d| d| d}|| | |)|t|| nd| d}|| | |*d
t|| t+|dkr6| ,|| d S d S )N)r&  r   r(  zndimension()ro  zstr(r  r@  sizestrideT)Z	is_tensortensor_source_dynamo_dynamic_indicesz((z"._dynamo_dynamic_indices.issubset(z)) if hasattr(z', '_dynamo_dynamic_indices') else True)r  z%, '_dynamo_dynamic_indices') == Falser   )-r1   Z_unsafe_skip_fsdp_module_guardsZis_fsdp_modulerQ  r  r   r/   r}  r*  r   r  r  rf  r{  rK  r  rc   r   r&  r   r  r  Z,skip_no_tensor_aliasing_guards_on_parametersr  	ParameterrO  rJ   rw  rx  Zinput_source_to_sizes_stridesr#  r!  r-  Zadd_tensor_match_guardr   r   r   r   rh   r   rg  Zadd_dynamic_indices_guardr  r  r  )r   r   r)  Ztensor_namerY  Ztermsterm
real_valuer   r{  metadatard  re  ry   Zstatic_reasonZdynamic_indicesr   r   r   r   TENSOR_MATCH  s   



"



 






zGuardBuilder.TENSOR_MATCHc                 C  s   t  }|d us	J |j}~|d usJ |jj}~|| jjv s%J d| |d u r6|j}|s0d n| |}n|}|d urCt	t
|nd }	d }
t|jdddk}|r_t|tjtfs_t	|}
|||	||
 d S )NzH_produce_guard_code must be called from inside GuardedCode. Called from __weakrefoffset__r   )r   f_backr]  co_namer   rr  r*  r}  weakrefr  r%  r  r   enumEnumr  Zset_export_info)r   r   rT  r  Z	cur_frameZcaller	func_namer*  Zguarded_objectZguarded_object_typeZobj_refZsupports_weakrefr   r   r   r    s6   
z#GuardBuilder._set_guard_export_info)r]  rZ  r^  r_  r`  ra  rb  rc  rd  re  rz   re  r   rr   rf  rg  )r   r!   rs   r   )r*  r  rv   r  r  r   )r   r  r  r  )r   r!   r  r  )r   r!   r  r  )r   r!   r/  r  r  r7  )8r   r   r   r   r  r  staticmethodr  r  r  r  r  r  r  r  r  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r#  r$  r(  r+  r,  r-  r.  r0  r2  r4  r6  r7  r8  r;  rc  rm  r  r   r   r   r   r\  `  sz    
?&
   
`
#



	6













a













 : r\  c                   @  s   e Zd ZdZejejejfZe	j
G dd dZG dd dejZG dd dejZdd
dZddddZdddZdddZdS )PyExprCSEPassr0   c                   @  rR  )PyExprCSEPass.Configzdict[str, int]
expr_countzdict[str, str]expr_to_nameNrU  r   r   r   r   Config  rV  rz  c                      s(   e Zd ZdddZd fd
dZ  ZS )zPyExprCSEPass.ExprCounterr1   rw  r  r  c                 C  s
   || _ d S r   )_config)r   r1   r   r   r   r   	  r  z"PyExprCSEPass.ExprCounter.__init__r   r  r   c                   s4   t |tjr| jjt|  d7  < t | d S r  )r   rv  ALLOWED_NODE_TYPESr{  rx  r  r   r   )r   r   r   r   r   r   	  s   zPyExprCSEPass.ExprCounter.visit)r1   rw  r  r  r   r  r  r   r   r   r   r   r   r   r   r   r   r   ExprCounter	  s    
r  c                      s,   e Zd Zd fddZd fddZ  ZS )zPyExprCSEPass.Replacerr1   rw  gen_nameCallable[[], str]r  r  c                   s    t    || _|| _g | _d S r   )r   r   r{  	_gen_namepreface)r   r1   r  r   r   r   r   	  s   

zPyExprCSEPass.Replacer.__init__r   r  r   c                   s   t |tjrGt|}| jj| tjkrG|| jjvr9t 	|}t|}| 
 }| j| d|  || jj|< n| jj| }t|t S t 	|S )Nz = )r   rv  r|  r  r{  rx  USE_THRESHOLDry  r   r   r  r  r   r  NameLoad)r   r   exprZnode_Zexpr_var_namer   r   r   r   	  s   zPyExprCSEPass.Replacer.visit)r1   rw  r  r  r  r  r}  r~  r   r   r   r   Replacer	  s    
r  r  r  c                 C  s$   d| _ | jtdd i d| _d S )Nr   c                   S  s   dS r   r   r   r   r   r   r   4	      z(PyExprCSEPass.__init__.<locals>.<lambda>)rx  ry  )_counterrz  r   defaultdictr{  r   r   r   r   r   1	  s   zPyExprCSEPass.__init___varr   r  c                 C  s    | | j  }|  j d7  _ |S r  )r  )r   r   r*  r   r   r   _new_var7	  s   zPyExprCSEPass._new_varrZ  r  c                 C  sX   |  | j}|D ]!}z
|t| W q ty) } z	td|j|  d }~ww d S )Nz#Failed to visit expr at line %s.
%s)	r  r{  r   r  parseSyntaxErrorr  	exceptionlineno)r   rZ  countereexr   r   r   count<	  s   zPyExprCSEPass.countr  tuple[list[str], str]c                 C  s.   |  | j| j}|t|}|jt|fS r   )r  r{  r  r   r  r  r  r  )r   r  replacernew_noder   r   r   r  E	  s   zPyExprCSEPass.replaceN)r  r  )r  )r   r  r  r  )rZ  r  r  r  r  r  r  r  )r   r   r   r  r  	AttributeCall	Subscriptr|  dataclasses	dataclassrz  NodeVisitorr  NodeTransformerr  r   r  r  r  r   r   r   r   rv    s    	
%
	rv  c                 C  s   t | jtptjo| jtju S r   )r   rO  r=   r1   Z guard_nn_modules_using_dict_tagsZ	create_fnr\  r  r  r   r   r   must_add_nn_module_guardsK	  s   
r  c                      s$   e Zd Z fddZdd Z  ZS )DeletedGuardManagerWrapperc                   s   t    || _d S r   )r   r   invalidation_reason)r   reasonr   r   r   r   Z	  s   

z#DeletedGuardManagerWrapper.__init__c                 C  s
   d | _ d S r   )ru   r   r   r   r   r   ^	  r  z6DeletedGuardManagerWrapper.populate_diff_guard_manager)r   r   r   r   r   r   r   r   r   r   r  Y	  s    r  c                   @  s>   e Zd Z			ddddZdd Zdd	 Zd
d Zdd ZdS )rg  Nr{   %Optional[Callable[[GuardFail], None]]c              	     s  |r|j nd }i _t|}t _|j_|_d |r |jnd _fdd}t|j	|j
|j|jj}dd }	t||	tjoItd}
tdsStd t|pWdtjdD ] }|
sw| rwd	|jvrwd
|jvrwtjsvd|jvrwq\|| q\||| |jj_tdj |jj_d |jsֈj |jst!j|jt"# }t$d| t%d urt%j|j t&jj'|jd td d t()dt*  t+j,j-d fddd j.  d _d S )Nc                   sB   |   }|tju r|  S  sJ   }|d usJ ||  S r   )guard_sourcer$   ZCONSTANTr*  r  )r   r  Z	r_builder)	w_builderr   r   r`  	  s   
z1CheckFunctionManager.__init__.<locals>.source_refc                 S  s   |  }|r
d |_ d S d S r   )rp  )Zweak_br   r   r   r   cleanup_builder	  s   
z6CheckFunctionManager.__init__.<locals>.cleanup_builderz!pytorch/compiler:guard_nn_modulesz9guard_nn_modules is turned off using justknobs killswitchr   )r/  r  r  hooks%sg        zGuard check failed: 2   zGuard eval latency = %s us.2fZguard_latency_usZdynamo_cpp_guards_strc                     s   j  d ddS )Nz
Guard latency = r  z us)r   r   )latencyr   r   r   r   	  r  z/CheckFunctionManager.__init__.<locals>.<lambda>
payload_fn)/ro   	_weakrefs5update_diff_guard_managers_for_existing_cache_entriesrr   r   r   r{  torch_function_mode_stackr\  r^  rb  rd  rz   rq  r  r1   Zguard_nn_modulesr(   r  r  r   r!   sort_keyrP  r*  r/  r  compile_check_fnr~   r  r  rK  r   get_guard_fail_reason_helperr   r  r  rm   r   rt   r   Zincrement_toplevelrB  r   _loggingtrace_structuredclear)r   r]  r{  r|   r{   ro   Zexisting_diff_guard_sourcesr`  builderr  Zguard_on_nn_modulesr   reasonsr   )r  r   r  r   r   h	  s   










zCheckFunctionManager.__init__c                   s   j }|dg7 }td g g g t| j}| jjdg | jj| jdg d | _dfdd	}t	 } j
D ]}|jD ]}	|	|vrS||	|jd ||	 qAq< j}
d }d }t|
d	krqt j|
d
d|
 d g | jrz| jjjjng }|D ]t}t|tr|j}|j}|  d|  }t | ||g ||d d q~t|tr fdd|jD } fdd|j D }dddd |jD  dddd |j D  d}t!|||g ||d d q~t"d|  j#D ]}|jD ]	}	||	|jd qqrt$j%j&dfddd t'j(}|d u r!t') }|||j*|dt+t, }| j-  d j.d i}tdksCJ || j_/|| j_0| j1  | j_2|| j_3|| j_4d | j_5d | j_6|
| j_7d S ) Nz**___kwargs_ignoredzGUARDS:z___check_global_state()z$___check_torch_function_mode_stack()Fc                   s   t  }td|  fdd ttjrGd}d}d ur?jr1dd	j
  }jr?dd	j
  }td || |sU  | d S d S )Nr  c                     s@    rj rtj  nd rjrtjdS d dS )N)rY  r  r  )r  r'   from_tracebackr  r  r   r   r   r   r   r   
  s   
zNCheckFunctionManager.compile_check_fn.<locals>.add_code_part.<locals>.<lambda>r   z
Stack:
z
User stack:
zGuard: %s%s%s)r  r  r  r   verbose_guards_logisEnabledForloggingDEBUGr  r  formatr  )r   r   Zlog_onlyr   Zmaybe_stackZmaybe_user_stack)rx   structured_guard_fnsry   r  r   add_code_part
  s0   

z<CheckFunctionManager.compile_check_fn.<locals>.add_code_partTr0   zcheck_no_aliasing(r   r$  r)  c                      g | ]}  |qS r   r  r   r   r  r   r   r   e
      z9CheckFunctionManager.compile_check_fn.<locals>.<listcomp>c                   r  r   r  r  r  r   r   r   i
  r  zcheck_overlapping(overlapping=[c                 s      | ]}|  V  qd S r   r*  r  r   r   r   rV  o
      z8CheckFunctionManager.compile_check_fn.<locals>.<genexpr>z], non_overlapping=[c                 s  r  r   r  r  r   r   r   rV  p
  r  z])zUnknown GuardEnvExpr: dynamo_guardsc                     s   dd  D S )Nc                 S  s   g | ]}| qS r   r   )r   fr   r   r   r   
  r>  zKCheckFunctionManager.compile_check_fn.<locals>.<lambda>.<locals>.<listcomp>r   r   )r  r   r   r   
  s    z7CheckFunctionManager.compile_check_fn.<locals>.<lambda>r  )Z___check_tensorsZ___check_tensors_verboseZ___check_global_stateZ"___check_torch_function_mode_stackrj  r   r   )8ru  r  r  $make_torch_function_mode_stack_guardr  r   rt   Zadd_global_state_guardZ#add_torch_function_mode_stack_guardry  rY  rT  r   r   rw  r  r   rx  r  r{  Ztracing_contextguards_contextaotautograd_guardsr   r    Zinput_source_aZinput_source_br*  r   r  r&   Zoverlapping_sourcesZnon_overlapping_sourcesr   r  rv  r   r  r  r2   Zinitial_global_stateZGlobalStateGuardr   r+   r
  r   rp  rv   rw   r   ry   rz   r{   r|   r}   r   )r   r  Z
guards_outr{   largsZ"torch_function_mode_stack_check_fnr  seenZgclrY  rw  Zcheck_tensors_fnZcheck_tensors_verbose_fnr  r   Zsource_ar*  r   Zoverlapping_guard_managersZnon_overlapping_guard_managersZglobal_staterv   r  r   )r  rx   r  ry   r   r  	  s   

)


	







	
z%CheckFunctionManager.compile_check_fnc                 C  s   t | dr>t| jts@| jj }d urB| jj }d urDt|ts"J t|ts)J d| d}t|}||| || _d S d S d S d S d S )Nr   zCache line invalidated because z got deallocated)	r   r   r   r  r|   r}   rV   rX   
invalidate)r   obj_strr|   r}   r  Zdeleted_guard_managerr   r   r   r  
  s   

zCheckFunctionManager.invalidatec                 C  sh   z't || jvr#t|| jt |< t|tj| j|d W t |S W t |S  ty3   Y t |S w )zadd a weakref, return the id)r  )	r|  r  rq  r  r   	functoolspartialr  	TypeError)r   r  r  r   r   r   r^  
  s   
zCheckFunctionManager.id_refc                 C  s    t || jv r| jt | S dS )zFLookup the _weakrefs created in id_ref function for ID_MATCH'd objectsN)r|  r  )r   r  r   r   r   rb  
  s   z$CheckFunctionManager.lookup_weakrefs)NNN)r{   r  )r   r   r   r   r  r  r^  rb  r   r   r   r   rg  g	  s      3rg  tuple[str, str]c           	   	     sD  ddl m} t   |  d fdd}| }| D ]/}||\}}|| |d	| d
 |  |d W d    n1 sDw   Y  q| }|d |  || |d W d    n1 skw   Y  | }|d| d
 |  || |d W d    n1 sw   Y  | | fS )Nr   r   r  r  r  r  c                   s
     | S r   )r  )r  Zcsepassr   r   r  
  r  z%build_guard_function.<locals>.replacezif not (z):zreturn Falsezdef guard(L):zreturn Truezdef ___make_guard_fn(zreturn guardr  )	r   r   rv  r  r   r   r   splicer   )	rx   Zclosure_argsr   r  Z
guard_bodyr  r  r   Zmake_guard_fnr   r  r   r  
  s4   







r  c                   C     t jjjdS )Nrp   r   r  	_internalZ	log_stateZis_artifact_enabledr   r   r   r   is_recompiles_enabled
  r   r  c                   C  r  )Nrq   r  r   r   r   r   is_recompiles_verbose_enabled
  r   r  c                   s   dd | D   fdd}|S )Nc                 S  s   g | ]}t |qS r   )r%  )r   r   r   r   r   r      r  z8make_torch_function_mode_stack_guard.<locals>.<listcomp>c                    sD   t  } t| t krdS t | D ]\}}|t|kr dS qdS rN  )r`   r  r   r%  )Z	cur_stacktymoder  r   r   check_torch_function_mode_stack  s   zMmake_torch_function_mode_stack_guard.<locals>.check_torch_function_mode_stackr   )Zintial_stackr  r   r  r   r  
  s   r  c                   sn   t | j}tt | jD ]}||d< tt|||} | | q fdd D }d	|}d| gS )N__compile_source__c                   s&   g | ]}t  | d kr |  qS )r0   )r  )r   r/  Zids_to_sourcer   r   r     s     zErecompilation_reason_for_no_tensor_aliasing_guard.<locals>.<listcomp>r   zDuplicate tensors found: )
r   rz   r   r  r   r   r|  r  r   r  )r   rp  rz   rf  Z	tensor_idZduplicate_tensorsr  r   r  r   1recompilation_reason_for_no_tensor_aliasing_guard  s   




r  r   c                 C  s   ddl }d}||d| S )z
    Replace occurrences of L[...] with just the inner content.
    Handles both single and double quotes.

    This is to generate user friendly recompilation messages.
    r   NzL\[\s*['\"](.*?)['\"]\s*\]z\1)resub)r   r  patternr   r   r   strip_local_scope!  s   r  r   r[   f_localsre  
compile_idr   c              
   C  s:  || j d d}|| j g }d}g }| |}|js2|j}t|dkr2d|d v r.d}n|}g }|r:t| |}nU|D ]R}t| j }	||	d< t	 $ zt
||	|}
W n tyf   t reY W d	   q< w W d	   n1 sqw   Y  t|
tr|
s|}
t|
tr||
 t s nq<| d
d| }t|S )z
    Return the reason why `guard_manager` failed.
    Updates `guard_failures` with the generated reason.
    Only the first failed check of guard_manager is reported.
    rj  rh  Fr0   zDuplicate tensor foundr   Tr  Nr   z; )rz   r  rv   r   resultry   r  r  r   r.   r  	Exceptionr  r   r7  r  r   r  r  )r   r  r  rp  r  Zno_tensor_aliasing_check_failedry   Zguard_debug_infor   rz   Zfail_reason
reason_strr   r   r   r  .  sP   




r  c                 C  s   t | tr| d| j S t| ||}tt|  | z| jd ur1| t|p(dt|  W |S W |S  t	yB   t
d Y |S w )Nr   zunknown reasonzVFailure in guard_fail_fn callback - raising here will cause a NULL Error on guard eval)r   r  r  r  rb   rg   r   r{   rZ   r  r  r  )r   rY  r  r  r  r   r   r   get_guard_fail_reasonq  s$   

	r  framerW   c                   s  g  | durt | j| j|j| j}|r | | j} | dus|j}t p't	 }|s-t
jrxt	 r=ddd t D }n	td d}d| }d|j d	|j d
|j dt|d }|rpt	 rkt| nt| t
jrxt|tjjddd  fddd  S )z
    Return the list of guard failure reasons using cache_entry.
    Logs the recompilation reason if `recompiles` logging is enabled.
    Raises a RecompileError if `config.error_on_recompile` is enabled.
    Nz

c                 s  s,    | ]\}}d | dt |d V  qdS )zguard z failures:
- N)r]  r   )r   rR  r  r   r   r   rV    s
    
z:get_and_maybe_log_recompilation_reasons.<locals>.<genexpr>r  r  z-triggered by the following guard failure(s):
zRecompiling function z in :z    artifactc                   S  s
   dddS )NZrecompile_reasonsjson)r*  encodingr   r   r   r   r   r     s   z9get_and_maybe_log_recompilation_reasons.<locals>.<lambda>c                     s    S r   r   r   r  r   r   r     r  )Zmetadata_fnr  )r  r   rY  r  r  r   nextr]  r  r  r1   Zerror_on_recompiler  r  r]  r   rp  co_filenameco_firstlinenorecompiles_verbose_logr  recompiles_logr3   ZRecompileErrorr   r  r  )r|   r  r  rY  Zdo_recompiles_logZfailuresZguard_failure_detailsmessager   r  r   'get_and_maybe_log_recompilation_reasons  sL   







	r  c                 C  s`   | }t  }| d ur|| j  | j} | d us	|} | d ur.|| j_| j  | j} | d us|S r   )ry  r  r   r   r  r   r   )r|   Zfirst_cache_entryZacc_diff_guard_sourcesr   r   r   r    s   
r  r'  rB  lastr7  c                 C  s   t d|j d|j d|j  t dd| j d  t dd| j t |  d|i| j}| jD ]}z	t|| j	| W q3   t d|  Y q3d S )	NzERROR RUNNING GUARDS  r  zlambda r   z and
  ri  zMalformed guard:
)
printrp  r  r  r  rw   rx   rv   r  rz   )r   rY  r  r'  r   rd  r   r   r   r   guard_error_hook  s   
r  c                 c  s.    t  }| D ]}||vr|V  || qd S r   )ry  r   )seqr  r   r   r   r   unique  s   
r  c                 C  sf   |r1|| kr1t |}t | }t|st| r%t|   d|  d||kr1tjtj|dS d S )Nz is alising zC. This is not supported. Please do a clone for corresponding input.)r*  )	r   r   r3   ZUnsafeScriptObjectErrorr*  r  r  r\  r+  )Z
obj_sourceZdupe_sourceZser_source_is_localZsource_is_localr   r   r   make_dupe_guard   s   	r  )skipc                 G  s`   ddl m} ttjpttj}| jj	j
}|D ]}t|ts$J |||| d d qdS )z
    Add dynamo guards to the current tracing context.

    Args:
        guards: guard(s) to add
        skip: number of stack frames to ignore for debug stack trace
    r   )TracingContextr0   )collect_debug_stackr  N)torch._guardsr  r  r  r  r  r  r}  r  r  r   r   r!   )r  ro   r  r	  r   r   r   r   r   install_guard"  s   
r  )r   r  r  r  )r   r  r   r!   r  r  )rx   r  r   r!   r  r  )rY  rZ  )r  r  )r   r  r  r  )r   r[   r  re  r  r   r  r  )
r   r[   rY  rZ  r  re  r  r   r  r  )r  rW   r  r  )
r   r[   rY  rZ  r  re  r'  rB  r   r7  )r   
__future__r   r  rq  r   r  rr  r  r  r   r  r  r  r]  r  r  rq  
contextlibr   rs  r   r   typingr   r   r   r	   r
   r   r   Ztorch.overridesr:  Ztorch._C._dynamo.eval_framer   Ztorch._C._dynamo.guardsr   r   r   r   r   r   r   r   r   r   Ztorch._dynamo.sourcer   r   r   r   r   r   Ztorch._dynamo.utilsr   r
  r   r   r    r!   r"   r#   r$   r%   r&   Ztorch._loggingr'   Ztorch._utils_internalr(   Z%torch.fx.experimental.symbolic_shapesr)   r*   r+   Ztorch.utils._ordered_setr,   Ztorch.utils._tracebackr-   r.   Ztorch.utils.weakr/   r   r1   r2   r3   r4   Z
eval_framer5   r   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r  r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   r;  numpyr   ModuleNotFoundErrorZsympyrn   	getLoggerr   r  r  ZgetArtifactLoggerr  r  r  r  rr   r   	lru_cacher   r   r
  r  r   r   r  r  r!  r#  r-  r0  r4  r  r6  rM  rQ  rS  rs  rD  r[  r\  rv  r  r  rg  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   <module>   s   0 , "L
 b	





)             #U  
l%


C
<
"