U
    E‹ÑhKt  ã                   @  sš  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m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mZ d dlmZ d dlmZ d dl m!Z! e "e#¡Z$erâd dl%Z%d dl&Z&G d	d
„ d
eƒZ'G dd„ deƒZ(G dd„ dej)ƒZ*G dd„ dƒZ+G dd„ deƒZ,ej-G dd„ dƒƒZ.edƒZ/ej-G dd„ dƒƒZ0ej-G dd„ de0ƒƒZ1G dd„ dee/ ƒZ2G dd„ dƒZ3G dd„ dƒZ4G d d!„ d!e2e4 ƒZ5G d"d#„ d#ƒZ6G d$d%„ d%e2e6 ƒZ7G d&d'„ d'ƒZ8G d(d)„ d)e2e3 ƒZ9e :¡ Z;G d*d+„ d+ƒZ<G d,d-„ d-ƒZ=ed.d/œd0d1„ƒZ>ed2d/œd3d4„ƒZ?ej-d5d6�G d7d8„ d8ƒƒZ@ej-d5d6�G d9d:„ d:e@ƒƒZAdAd;d<œd=d>„ZBd?d@„ ZCdS )Bé    )ÚannotationsN)Úabstractmethod)Úcontextmanager)ÚAnyÚCallableÚDictÚGenericÚListÚ
NamedTupleÚOptionalÚSetÚTupleÚTYPE_CHECKINGÚTypeVar)Ú_pytree)ÚCapturedTraceback)ÚWeakTensorKeyDictionaryc                   @  s&   e Zd ZU ded< ded< dd„ ZdS )Ú	CompileIdÚintÚframe_idÚframe_compile_idc                 C  s   | j › d| j› �S )Nú/)r   r   ©Úself© r   ú;/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/_guards.pyÚ__str__@   s    zCompileId.__str__N©Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r   r   r   r   r   r   7   s   
r   c                   @  s&   e Zd ZU ded< ded< dd„ ZdS )ÚTraceIdr   Ú
compile_idr   Úattemptc                 C  s*   | j dkrt| jƒS | j› d| j › �S d S )Nr   Ú_)r$   Ústrr#   r   r   r   r   r   J   s    

zTraceId.__str__Nr   r   r   r   r   r"   D   s   
r"   c                   @  s`   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZddœdd„Zddœdd„Zdd„ ZdS )ÚGuardSourcer   é   é   é   é   é   é   é   é   é	   é
   é   Úbool©Úreturnc                 C  s   | t jt jfkS ©N)r'   ÚGLOBAL_FSDP_MODULEÚLOCAL_FSDP_MODULEr   r   r   r   Úis_fsdp_module_   s    zGuardSource.is_fsdp_modulec                 C  s   | t jt jfkp|  ¡ S r6   )r'   ÚGLOBAL_NN_MODULEÚLOCAL_NN_MODULEr9   r   r   r   r   Úis_nn_moduleb   s    þÿúzGuardSource.is_nn_modulec                 C  s   | t jt jt jfkS r6   )r'   ÚLOCALr;   r8   r   r   r   r   Úis_locall   s
    ýzGuardSource.is_localN)r   r   r    r=   ÚGLOBALr;   r:   ÚCONSTANTZRANDOM_VALUEZ	SHAPE_ENVr8   r7   ZBACKWARD_STATEZ	EPHEMERALÚSYNTHETIC_LOCALr9   r<   r>   r   r   r   r   r'   Q   s   
r'   c                   @  s   e Zd ZdS )ÚGuardBuilderBaseN©r   r   r    r   r   r   r   rB   ‚   s   rB   c                   @  s   e Zd ZU ded< ded< dS )Ú
ShapeGuardz
sympy.ExprÚexprr   ÚstackN)r   r   r    r!   r   r   r   r   rD   †   s   
rD   c                   @  s   e Zd ZU ded< ded< dZded< dZded< dZd	ed
< dZded< dZded< dZ	ded< dZ
ded< dd„ Zdd„ Zdd„ Zdd„ Zeddœdd„ƒZeddœd d!„ƒZed"d#„ ƒZd$d%„ Zd&d'„ Zd(d)œd*d+„Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ ZdS )4ÚGuardÚSourceÚoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]Ú	create_fnNzOptional[List[str]]Úguard_typesÚ	code_listzOptional[object]Úobj_weakrefzOptional[type]Úguarded_class_weakrefzOptional[CapturedTraceback]rF   z Optional[traceback.StackSummary]Ú
user_stackzOptional[int]Ú_hashc                 C  s*   | j d kr$t| j| jt| jƒfƒ| _ | j S r6   )rP   ÚhashÚnameÚsourceÚidrJ   r   r   r   r   Ú__hash__«   s    
zGuard.__hash__c                 C  sT   ddl m} t| jtjƒo&| jj|jk}|| jr8| jj	ndt
| jƒ| j|  ¡ jjfS )Nr(   )ÚGuardBuilderéÿÿÿÿ)Z_dynamo.guardsrV   Ú
isinstancerJ   Ú	functoolsÚpartialÚfuncZDUPLICATE_INPUTrS   ÚvalueÚlenrR   Úinner_create_fnÚ__code__Úco_firstlineno)r   rV   Zis_duplicate_inputr   r   r   Úsort_key°   s    þ
ûzGuard.sort_keyc                 C  s   |   ¡ |  ¡ k S r6   )ra   ©r   Úotherr   r   r   Ú__lt__Á   s    zGuard.__lt__c                 C  s    t | jtjƒr| jjS | jS d S r6   )rX   rJ   rY   rZ   r[   r   r   r   r   r^   Ä   s    zGuard.inner_create_fnr&   r4   c                 C  s
   | j  ¡ S r6   )rI   rR   r   r   r   r   rR   Ê   s    z
Guard.namer'   c                 C  s
   | j  ¡ S r6   )rI   Úguard_sourcer   r   r   r   rS   Î   s    zGuard.sourcec                 C  sh   t | tjƒr\| ƒ }|dk	rFdtt| ƒƒ› d|jj› dtt|ƒƒ› d�S dtt| ƒƒ› d�S nt| ƒS dS )aÔ  
        This is a workaround of a Python weakref bug.

        `obj_weakref` is instance returned by `weakref.ref`,
        `str(obj_weakref)` is buggy if the original obj overrides __getattr__, e.g:

            class MyConfig(dict):
                def __getattr__(self, x):
                    return self[x]

            obj = MyConfig(offset=5)
            obj_weakref = weakref.ref(obj)
            str(obj_weakref)  # raise error: KeyError: '__name__'
        Nz<weakref at z; to 'z' at ú>z; dead>)rX   ÚweakrefÚReferenceTypeÚhexrT   Ú	__class__r   r&   )rM   Úobjr   r   r   Úweakref_to_strÒ   s    ,zGuard.weakref_to_strc                 C  s`   d| j r| j j ¡ nd› dt| jƒ› d|  ¡ j› d| j› d| j› d|  | j	¡› d| j
› d�}|S )	Nz	
        Ú ú z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )rS   rR   ÚlowerÚreprr^   r   rK   rL   rl   rM   rN   )r   Úsr   r   r   Ú__repr__ë   s     ÿÿÿýü
ûú	zGuard.__repr__c                 C  s    dt | jƒ› d�}| jr$| jj ¡ nd}|d|› d�7 }|d|  ¡ j› d�7 }|d| j› d�7 }|d| j› d�7 }|d|  | j	¡› d�7 }|d	| j
› d�7 }|S )
NzName: Ú
rm   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )rp   rR   rS   ro   r^   r   rK   rL   rl   rM   rN   )r   ÚoutputrS   r   r   r   r   ÷   s    zGuard.__str__rB   )Úbuilderc                 C  sh   z|   || ¡W S  tk
rb   t dt| ƒ ¡ ¡ | jr\t dd | j 	¡ dd … ¡ ¡ ¡ ‚ Y nX d S )NzError while creating guard:
%szCreated at:
%srm   éüÿÿÿ)
rJ   Ú	ExceptionÚlogÚ	exceptionr&   ÚrstriprF   ÚerrorÚjoinÚformat)r   ru   r   r   r   Úcreate  s    $zGuard.createc                 C  s
   | j  ¡ S r6   )rS   r<   r   r   r   r   r<     s    zGuard.is_nn_modulec                 C  s
   | j  ¡ S r6   )rS   r9   r   r   r   r   r9     s    zGuard.is_fsdp_modulec                 C  s
   | j  ¡ S r6   )rS   r>   r   r   r   r   r>     s    zGuard.is_localc                 C  s†   | j stƒ | _ | j  |¡ | j|d fks0tdƒ‚|| _| jsD|| _n| j |¡ | j|d fks|t| jƒrt|  ¡ d ks|tdƒ‚|| _d S )Nz+Guarded class id must be identical, or NonezBGuarded object must be identical, None or ephemeral (dead weakref))	rK   ÚlistÚappendrN   ÚAssertionErrorrL   ÚextendrM   Úcallable)r   Z
guard_typeZguarded_classrL   rM   r   r   r   Úset_export_info  s4    þýþÿÿú
ùø	zGuard.set_export_info)r   r   r    r!   rK   rL   rM   rN   rF   rO   rP   rU   ra   rd   r^   ÚpropertyrR   rS   Ústaticmethodrl   rr   r   r~   r<   r9   r>   r„   r   r   r   r   rG   ‹   s4   

	rG   ÚTc                   @  s   e Zd ZdS )ÚGuardEnvExprNrC   r   r   r   r   rˆ   ?  s   rˆ   c                   @  s&   e Zd ZU ded< ded< dd„ ZdS )ÚDuplicateInputsrH   Úinput_source_aÚinput_source_bc                 C  s   | j | jkst‚d S r6   )rŠ   r‹   r�   r   r   r   r   Ú__post_init__O  s    zDuplicateInputs.__post_init__N)r   r   r    r!   rŒ   r   r   r   r   r‰   J  s   
r‰   c                   @  s0   e Zd Zeddœdd„ƒZeddœdd„ƒZdS )	ÚCheckpointabler‡   r4   c                 C  s   d S r6   r   r   r   r   r   Úcopy_graphstatea  s    zCheckpointable.copy_graphstate)Ústatec                 C  s   d S r6   r   ©r   r�   r   r   r   Úrestore_graphstatee  s    z!Checkpointable.restore_graphstateN)r   r   r    r   rŽ   r‘   r   r   r   r   r�   `  s   r�   c                   @  s8   e Zd ZU dZeƒ Zded< dd„ Zdd„ Zdd	„ Z	d
S )ÚGuardsCheckpointStatezW
    The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
    ú
Set[Guard]Údynamo_guardsc                 C  s
   || _ d S r6   )r”   )r   r”   r   r   r   Ú__init__q  s    zGuardsCheckpointState.__init__c                 C  s"   | j  |j ¡}t|ƒdkrdS |S )z¹
        Produces a delta against another GuardsCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        Guard type objects.
        r   N)r”   Ú
differencer]   ©r   rc   Úrr   r   r   Údifft  s    zGuardsCheckpointState.diffc                 C  s   |   |¡d kS r6   ©r™   rb   r   r   r   Ú__eq__€  s    zGuardsCheckpointState.__eq__N)
r   r   r    Ú__doc__Úsetr”   r!   r•   r™   r›   r   r   r   r   r’   j  s
   
r’   c                   @  s2   e Zd ZU i Zded< dd„ Zdd„ Zdd„ Zd	S )
ÚModuleContextCheckpointStatezDict[str, torch.nn.Module]Ú
nn_modulesc                 C  s
   || _ d S r6   ©rŸ   )r   rŸ   r   r   r   r•   ‡  s    z%ModuleContextCheckpointState.__init__c                 C  s2   t | j ¡ ƒ t |j ¡ ƒ¡}t|ƒdkr.dS |S )z¾
        Produces a delta against another ModuleContextCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        module key names.
        r   N)r�   rŸ   Úkeysr–   r]   r—   r   r   r   r™   Š  s    z!ModuleContextCheckpointState.diffc                 C  s   |   |¡d kS r6   rš   rb   r   r   r   r›   –  s    z#ModuleContextCheckpointState.__eq__N)r   r   r    rŸ   r!   r•   r™   r›   r   r   r   r   rž   „  s   
rž   c                   @  s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚModuleContextc                 C  s
   i | _ d S r6   r    r   r   r   r   r•   ›  s    zModuleContext.__init__c                 C  s   t t| jƒƒS r6   )rž   ÚdictrŸ   r   r   r   r   rŽ   ž  s    zModuleContext.copy_graphstatec                 C  s   t |tƒst‚|j| _d S r6   )rX   rž   r�   rŸ   r�   r   r   r   r‘   ¡  s    z ModuleContext.restore_graphstateN©r   r   r    r•   rŽ   r‘   r   r   r   r   r¢   š  s   r¢   c                   @  s2   e Zd ZU i Zded< dd„ Zdd„ Zdd„ Zd	S )
ÚGlobalContextCheckpointStatezDict[str, Tuple[Callable, ...]]Úglobal_statec                 C  s
   || _ d S r6   ©r¦   )r   Zglobal_statesr   r   r   r•   ©  s    z%GlobalContextCheckpointState.__init__c                 C  s2   t | j ¡ ƒ t |j ¡ ƒ¡}t|ƒdkr.dS |S )z¾
        Produces a delta against another GlobalContextCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        global key names.
        r   N)r�   r¦   r¡   r–   r]   r—   r   r   r   r™   ¬  s    z!GlobalContextCheckpointState.diffc                 C  s   |   |¡d kS r6   rš   rb   r   r   r   r›   ¸  s    z#GlobalContextCheckpointState.__eq__N)r   r   r    r¦   r!   r•   r™   r›   r   r   r   r   r¥   ¦  s   
r¥   c                   @  s:   e Zd ZdZdddddddhZd	d
„ Zdd„ Zdd„ ZdS )ÚGlobalContextzz
    This keeps track of the global torch state during tracing of a function.
    For example, torch.is_grad_enabled.
    Zgrad_enabledZtorch_function_enabledZautocast_enabledZautocast_cpu_enabledZautocast_gpu_dtypeZautocast_cpu_dtypeZautocast_cache_enabledc                 C  s
   i | _ d S r6   r§   r   r   r   r   r•   Ì  s    zGlobalContext.__init__c                 C  s   t t| jƒƒS r6   )r¥   r£   r¦   r   r   r   r   rŽ   Ï  s    zGlobalContext.copy_graphstatec                 C  sf   t |tƒst‚|j| _t| jƒt| jƒkr>t| j ¡ ƒ| jksFtdƒ‚| j ¡ D ]\}}||ƒ qPd S )NzGlobal state mismatch)	rX   r¥   r�   r¦   r]   Ú_supported_global_statesr�   r¡   Úvalues)r   r�   r[   Úargsr   r   r   r‘   Ò  s    ÿþýz GlobalContext.restore_graphstateN)r   r   r    rœ   r©   r•   rŽ   r‘   r   r   r   r   r¨   ¼  s   ù
r¨   c                   @  sb   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdddœddœdd„Zddœdd„Z	dd„ Z
dS )Ú	GuardsSetNc                 C  s   |d krt ƒ }|| _d S r6   )r�   Úinner)r   r­   r   r   r   r•   è  s    zGuardsSet.__init__c                 C  s
   t | jƒS r6   )Úiterr­   r   r   r   r   Ú__iter__í  s    zGuardsSet.__iter__c                 C  s
   t | jƒS r6   )r]   r­   r   r   r   r   Ú__len__ð  s    zGuardsSet.__len__c                 C  s   t | j|j ƒS r6   )r¬   r­   rb   r   r   r   Ú__sub__õ  s    zGuardsSet.__sub__c                 C  s
   t | jƒS r6   )r3   r­   r   r   r   r   Ú__bool__ø  s    zGuardsSet.__bool__Tr   )Úcollect_debug_stackÚskiprG   )Úguardc                C  sR   || j krd S |rB|jd kr.tjd| d�|_|jd krBt ¡ |_| j  |¡ d S ©Nr(   )r´   )r­   rF   r   ÚextractrO   ÚTracingContextÚextract_stackÚadd)r   rµ   r³   r´   r   r   r   rº   û  s    



zGuardsSet.addr“   )Úothersc                 G  s&   |D ]}|D ]}| j |dd� qqd S r¶   )rº   )r   r»   ÚoÚgr   r   r   Úupdate  s    zGuardsSet.updatec                   s   ‡ fdd„| j D ƒ| _ dS )z%Delete all guards with a given sourcec                   s   h | ]}|j ˆ kr|’qS r   )rI   )Ú.0r½   ©rS   r   r   Ú	<setcomp>  s     
 z6GuardsSet.remove_guards_with_source.<locals>.<setcomp>N)r­   )r   rS   r   rÀ   r   Úremove_guards_with_source
  s    z#GuardsSet.remove_guards_with_source)N)r   r   r    r•   r¯   r°   r±   r²   rº   r¾   rÂ   r   r   r   r   r¬   ç  s   

r¬   c                   @  s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚGuardsContextc                 C  s   t ƒ | _g | _d S r6   )r¬   r”   Zaotautograd_guardsr   r   r   r   r•     s    zGuardsContext.__init__c                 C  s   t t| jjƒƒS r6   )r’   r�   r”   r­   r   r   r   r   rŽ     s    zGuardsContext.copy_graphstatec                 C  s   t |tƒst‚t|jƒ| _d S r6   )rX   r’   r�   r¬   r”   r�   r   r   r   r‘     s    z GuardsContext.restore_graphstateNr¤   r   r   r   r   rÃ     s   rÃ   c                   @  sP   e Zd Zed dœdd„ƒZeddœdd„ƒZdd„ Zed	d
„ ƒZedd„ ƒZdS )ÚCompileContextr4   c                   C  s   t jd k	st‚t jS r6   )Ú_TLSÚcompile_contextr�   r   r   r   r   Úget3  s    zCompileContext.getúOptional[CompileContext]c                   C  s   t tdd ƒS ©NrÆ   ©ÚgetattrrÅ   r   r   r   r   Útry_get8  s    zCompileContext.try_getc                 C  s&   |d kst |tƒst‚|| _d| _d S )Nr   )rX   r   r�   r#   r$   )r   r#   r   r   r   r•   <  s    zCompileContext.__init__c                  C  s   t  ¡ } | d krd S | jS r6   )rÄ   rÌ   r#   r   r   r   r   Úcurrent_compile_idA  s    z!CompileContext.current_compile_idc                  C  s0   t  ¡ } | d krd S | jd kr"d S t| j| jƒS r6   )rÄ   rÌ   r#   r"   r$   r   r   r   r   Úcurrent_trace_idH  s    
zCompileContext.current_trace_idN)	r   r   r    r†   rÇ   rÌ   r•   rÍ   rÎ   r   r   r   r   rÄ   2  s   
rÄ   c                   @  s¢   e Zd ZdZeddœdd„ƒZed dœdd„ƒZdd	„ Zd
d„ Zee	dd„ ƒƒZ
edd„ ƒZeej	dd„ ƒƒZeej	dd„ ƒƒZeej	dd„ ƒƒZedd„ ƒZdS )r¸   zÅ
    Provides the currently installed TracingContext, or None.

    Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but
    will return None.
    úOptional[TracingContext]r4   c                   C  s   t tdd ƒS )NÚtracing_contextrÊ   r   r   r   r   rÌ   Z  s    zTracingContext.try_getc                  C  s   t  ¡  } r| S tdƒ‚d S )Nz<TracingContext.get() must be called within an ongoing trace.)r¸   rÌ   ÚRuntimeError)Úctxr   r   r   rÇ   ^  s
    ÿzTracingContext.getc                 C  sZ   t ƒ | _tƒ | _tƒ | _|| _g | _d | _d | _	d | _
d | _d | _d| _tƒ | _d| _d S ©NF)rÃ   Zguards_contextr¢   Zmodule_contextr¨   Úglobal_contextÚ	fake_modeÚframe_summary_stackÚloc_in_frameZfw_metadataZaot_graph_nameZparams_flatÚoutput_stridesZ#force_unspec_int_unbacked_size_liker   Ztensor_to_contextZfakify_first_call)r   rÕ   r   r   r   r•   f  s    
zTracingContext.__init__c                 C  s   i | j _d S r6   )rÔ   r¦   r   r   r   r   Úclear�  s    zTracingContext.clearc                  k  sv   i }t  ¡ }|  ¡ D ]}t||ƒ||< q|  ¡ D ]\}}t|||ƒ q0z
d V  W 5 | ¡ D ]\}}t|||ƒ qZX d S r6   )r¸   rÇ   r¡   rË   ÚitemsÚsetattr)ÚkwargsZpriorrÒ   ÚkeyÚvalr   r   r   Úpatch•  s    
zTracingContext.patchc                  C  s@   t  ¡ } | d krt ¡ S | j}| jd k	r4|| jg }tj |¡S r6   )r¸   rÌ   Ú	tracebackÚStackSummaryrÖ   r×   Ú	from_list)r   rF   r   r   r   r¹   ¦  s    
zTracingContext.extract_stackc                  c  sˆ   t  ¡ } tjj | dg ¡�d tjj | dd ¡�F z
d V  W n4 tk
rn } zt|dƒs\d |_‚ W 5 d }~X Y nX W 5 Q R X W 5 Q R X d S )NrÖ   r×   Ú
real_stack)	r¸   rÇ   ÚunittestZmockrß   Úobjectrw   Úhasattrrã   )ÚtcÚer   r   r   Úclear_frame²  s      ÿþ

zTracingContext.clear_framec              
   c  s�   t  ¡ }| d k	r|j | ¡ |j}d |_zHz
d V  W n8 tk
rl } zt|dƒsZ| ¡ |_	‚ W 5 d }~X Y nX W 5 | d k	r„|j ¡  ||_X d S )Nrã   )
r¸   rÇ   rÖ   r€   r×   Úpoprw   ræ   r¹   rã   )Úframe_summaryrç   Úoldrè   r   r   r   Úcurrent_frameÑ  s    



zTracingContext.current_framec                  c  s@   t  ¡ } | d krd V  d S | j}g | _z| jV  W 5 || _X d S r6   )r¸   rÌ   rØ   )rç   Zold_output_stridesr   r   r   Úreport_output_stridesæ  s    z$TracingContext.report_output_stridesc                 C  s   t j| ||dd�t ¡ _d S )NF)Úlookup_line)rà   ÚFrameSummaryr¸   rÇ   r×   )ÚfilenameÚlinenoZ
frame_namer   r   r   Úset_current_locô  s       ÿzTracingContext.set_current_locN)r   r   r    rœ   r†   rÌ   rÇ   r•   rÙ   r   rß   r¹   Ú
contextlibré   rí   rî   ró   r   r   r   r   r¸   R  s.   *
r¸   rÈ   )Úcontextc                 c  s*   t tdd ƒ}| t_z
| V  W 5 |t_X d S rÉ   )rË   rÅ   rÆ   )rõ   Úold_contextr   r   r   rÆ   û  s
    
rÆ   rÏ   c              
   c  sš   t tddƒ}| t_zPz
| V  W n@ tk
r^ } z"t|dƒsL| dk	rL|  ¡ |_	‚ W 5 d}~X Y nX W 5 | dk	rŽ| jdk	rŽ| jjdk	rŽ| jj ¡  |t_X dS )zË
    This function installs the passed in tracing context as a dynamic scoped
    global variable.

    Calls to TracingContext.get() while not under a `with tracing()` context
    will return None.
    rÐ   Nrã   )
rË   rÅ   rÐ   rÕ   Z	shape_envÚcleanuprw   ræ   r¹   rã   )rõ   rö   rè   r   r   r   Útracing  s     	

ÿþ
ýrø   T)Úfrozenc                   @  sd   e Zd Zdd„ Zdd„ Zdd„ Zddœd	d
„Zddœdd„Zddœdd„Zddœdd„Z	dd„ Z
dS )rH   c                 C  s   dS rÓ   r   r   r   r   r   Úis_dict_key$  s    zSource.is_dict_keyc                 C  s   dS rÓ   r   r   r   r   r   Úis_ephemeral'  s    zSource.is_ephemeralc                 C  s   t ‚d S r6   ©ÚNotImplementedError)r   Zcodegenr   r   r   Úreconstruct*  s    zSource.reconstructr'   r4   c                 C  s   t ‚d S r6   rü   r   r   r   r   re   -  s    zSource.guard_sourcer&   c                 C  s   t ‚d S r6   rü   r   r   r   r   rR   0  s    zSource.namerG   c                 C  s   |   ¡ tjkrt‚t| |ƒS r6   )re   r'   r@   rý   rG   )r   Úfnr   r   r   Ú
make_guard3  s    zSource.make_guardr3   c                 C  s   |   ¡  ¡ S r6   )re   r<   r   r   r   r   r<   8  s    zSource.is_nn_modulec                 C  s   |   ¡ tjkS )z+True if you can guard on attributes of this)re   r'   rA   r   r   r   r   Úsubguards_allowed;  s    zSource.subguards_allowedN)r   r   r    rú   rû   rþ   re   rR   r   r<   r  r   r   r   r   rH   "  s   rH   c                   @  s&   e Zd ZU ded< dd„ Zdd„ ZdS )ÚChainedSourcerH   Úbasec                 C  s
   | j  ¡ S r6   )r  rú   r   r   r   r   rú   E  s    zChainedSource.is_dict_keyc                 C  s
   | j  ¡ S r6   )r  rû   r   r   r   r   rû   I  s    zChainedSource.is_ephemeralN)r   r   r    r!   rú   rû   r   r   r   r   r  A  s   
r  r   )Úinputsc                 C  sH  ddl m}m} g }t ¡  }r>|j}|dk	r>| |ddf¡ ddlm} t	t
|ƒ ƒƒD ]"\}}t||ƒrX| |d|f¡ qXt | ¡}	t	|	ƒD ]$\}}
t|
|ƒrŽ| |
jd|f¡ qŽ|�r@|d \}}}|dd… D ]f\}}}||ksÔtd	|› d
|› d|› d|› d
|› d|› d|› d|› d|j› d|› d|› d|j› �ƒ‚qÔ|S dS dS )a˜  
    Attempts to "detect" what the current fake mode is.  If there is one ambiently
    available from TracingContext, we preferentially use that.  Otherwise, we
    heuristically detect the fake mode via the following sources, in order of
    priority:

        - Currently active fake mode on stack
        - Fake mode associated with passed in tensors (inputs does not
          have to be flattened)
    r   )Ú
FakeTensorÚFakeTensorModeNztracing context©Ú _get_current_dispatch_mode_stackzactive fake modezfake tensor inputr(   zfake mode (z) from rn   z doesn't match mode (z

fake mode from z allocated at:
z
fake mode from )Útorch._subclasses.fake_tensorr  r  r¸   rÌ   rÕ   r€   Útorch.utils._python_dispatchr  Ú	enumerateÚreversedrX   ÚpytreeZtree_leavesr�   rF   )r  r  r  Z
fake_modesrõ   rÕ   r  ÚiÚmZflat_inputsZ
flat_inputZdesc1Úi1Zdesc2Úi2r   r   r   Údetect_fake_modeM  s,    



Nÿr  c                  C  sF   ddl m}  ddlm} tt|ƒ ƒƒD ]\}}t|| ƒr&|  S q&dS )z~
    Inspects the dispatch mode stack for an active fake mode and returns it.
    Returns None if no fake mode is active.
    r   )r  r  N)r	  r  r
  r  r  r  rX   )r  r  r%   r  r   r   r   Úactive_fake_modey  s    

r  )N)DÚ
__future__r   rô   ZdataclassesÚenumrY   ÚloggingÚ	threadingrà   Zunittest.mockrä   rg   Úabcr   r   Útypingr   r   r   r   r	   r
   r   r   r   r   r   Ztorch.utilsr   r  Ztorch.utils._tracebackr   Ztorch.utils.weakr   Ú	getLoggerr   rx   ZsympyZtorchr   r"   ÚEnumr'   rB   rD   Z	dataclassrG   r‡   rˆ   r‰   r�   r’   rž   r¢   r¥   r¨   r¬   rÃ   ÚlocalrÅ   rÄ   r¸   rÆ   rø   rH   r  r  r  r   r   r   r   Ú<module>   sl   4
1 *


+(  *	

,