o
    "id                     @  sl  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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 d dlZd dlmZ eeZ d dl!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Z*	 ej'G dd de*Z+	 G dd deee) Z,	 G dd dZ-G dd dZ.G dd de,e. Z/G dd dZ0G d d! d!e,e0 Z1	 G d"d# d#Z2G d$d% d%e,e- Z3e4 Z5	 G d&d' d'Z6G d(d) d)Z7ed9d+d,Z8ed:d-d.Z9ej'd/d0G d1d2 d2Z:ej'd/d0G d3d4 d4e:Z;d;d<d7d8Z<dS )=    )annotationsN)ABCabstractmethod)contextmanager)
AnyCallableDictGenericList
NamedTupleOptionalSetTupleTypeVar)CapturedTracebackc                   @  &   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   \/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/_guards.py__str__4   s   zCompileId.__str__N)__name__
__module____qualname____annotations__r   r   r   r   r   r   +   s   
 r   c                   @  sT   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
d ZdddZdddZdd ZdS )GuardSourcer                           c                 C  sD   | t jt jt jt jt jfv r|S | t jt jt jfv r|S t	t
| N)r    LOCALLOCAL_NN_MODULELOCAL_FSDP_MODULE	SHAPE_ENVRANDOM_VALUEGLOBALGLOBAL_NN_MODULEGLOBAL_FSDP_MODULENotImplementedErrorstr)r   Zlocals_globals_r   r   r   selectC   s   zGuardSource.selectreturnboolc                 C  s   | t jt jfv S r)   )r    r1   r,   r   r   r   r   is_fsdp_moduleZ      zGuardSource.is_fsdp_modulec                 C  s   | t jt jfv p|  S r)   )r    r0   r+   r8   r   r   r   r   is_nn_module]   s   zGuardSource.is_nn_modulec                 C  s   | t jt jt jfv S r)   )r    r*   r+   r,   r   r   r   r   is_localg   s
   zGuardSource.is_localNr6   r7   )r   r   r   r*   r/   r+   r0   CONSTANTr.   r-   r,   r1   r5   r8   r:   r;   r   r   r   r   r    8   s    


r    c                   @     e Zd ZdS )GuardBuilderBaseNr   r   r   r   r   r   r   r?   }   s    r?   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   rA      s   
 rA   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Z
dd Zdd Zdd Zdd Zed2ddZed3ddZedd  Zd!d" Zd#d$ Zd4d(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )5GuardSourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]	create_fnFr7   is_volatileNzOptional[List[str]]guard_types	code_listzOptional[object]obj_weakrefzOptional[type]guarded_class_weakrefc                 C  s   t | j| jt| jfS r)   )hashnamesourceidrH   r   r   r   r   __hash__   s   zGuard.__hash__c                 C  s*   | j r| j jndt| j| j|  jjfS )N)rP   valuelenrO   inner_create_fn__code__co_firstlinenor   r   r   r   sort_key   s
   
zGuard.sort_keyc                 C  s   |   |  k S r)   )rY   r   otherr   r   r   __lt__   r9   zGuard.__lt__c                 C  s   t | jtjr| jjS | jS r)   )
isinstancerH   	functoolspartialfuncr   r   r   r   rV      s   zGuard.inner_create_fnr6   r3   c                 C  
   | j  S r)   )rG   rO   r   r   r   r   rO         
z
Guard.namer    c                 C  ra   r)   )rG   guard_sourcer   r   r   r   rP      rb   zGuard.sourcec                 C  sb   t | tjr-|  }|dur#dtt|  d|jj dtt| dS dtt|  dS t| 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>)r]   weakrefReferenceTypehexrQ   	__class__r   r3   )rL   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
        }
        )rP   rO   lowerreprrV   r   rJ   rK   rj   rL   rM   )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: 
rk   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )rn   rO   rP   rm   rV   r   rJ   rK   rj   rL   rM   )r   outputrP   r   r   r   r      s   zGuard.__str__local_builderr?   global_builderc                 C  s   |  | j||| S r)   )rH   rP   r5   )r   rs   rt   r   r   r   create   s   zGuard.createc                 C  ra   r)   )rP   r:   r   r   r   r   r:         
zGuard.is_nn_modulec                 C  ra   r)   )rP   r8   r   r   r   r   r8      rv   zGuard.is_fsdp_modulec                 C  ra   r)   )rP   r;   r   r   r   r   r;      rv   zGuard.is_localc                 C  sp   | j st | _ | j | | j|d fv sJ d|| _| js"|| _n| j| | j|d fv s3J d|| _d S )Nz+Guarded class id must be identical, or Nonez)Guarded object must be identical, or None)rJ   listappendrM   rK   extendrL   )r   Z
guard_typeZguarded_classrK   rL   r   r   r   set_export_info   s(   
zGuard.set_export_infor6   r3   r6   r    )rs   r?   rt   r?   )r   r   r   r   rI   rJ   rK   rL   rM   rC   
user_stackrR   rY   r\   rV   propertyrO   rP   staticmethodrj   rp   r   ru   r:   r8   r;   rz   r   r   r   r   rE      s6   
 

rE   Tc                   @  r>   )GuardEnvExprNr@   r   r   r   r   r      s    r   c                   @  r   )DuplicateInputsrF   input_source_ainput_source_bc                 C  s   | j | jksJ d S r)   )r   r   r   r   r   r   __post_init__0  s   zDuplicateInputs.__post_init__N)r   r   r   r   r   r   r   r   r   r   +  s   
 r   c                   @  s(   e Zd Zed	ddZed
ddZdS )Checkpointabler6   r   c                 C     d S r)   r   r   r   r   r   copy_graphstateB     zCheckpointable.copy_graphstatestatec                 C  r   r)   r   r   r   r   r   r   restore_graphstateF  r   z!Checkpointable.restore_graphstateN)r6   r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   A  s
    r   c                   @  s6   e Zd ZU e Zded< dd Z	 dd Zdd Zd	S )
GuardsCheckpointState
Set[Guard]dynamo_guardsc                 C  
   || _ d S r)   )r   )r   r   r   r   r   __init__S  rv   zGuardsCheckpointState.__init__c                 C  s"   | j |j }t|dkrd S |S Nr   )r   
differencerU   r   r[   rr   r   r   diff]  s   zGuardsCheckpointState.diffc                 C     |  |d u S r)   r   rZ   r   r   r   __eq__c     zGuardsCheckpointState.__eq__N)	r   r   r   setr   r   r   r   r   r   r   r   r   r   P  s   
 r   c                   @  4   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  r   r)   r   )r   r   r   r   r   r   j  rv   z%ModuleContextCheckpointState.__init__c                 C  2   t | j t |j }t|dkrd S |S r   )r   r   keysr   rU   r   r   r   r   r   t     z!ModuleContextCheckpointState.diffc                 C  r   r)   r   rZ   r   r   r   r   z  r   z#ModuleContextCheckpointState.__eq__N)r   r   r   r   r   r   r   r   r   r   r   r   r   g     
 r   c                   @  $   e Zd Zdd Zdd Zdd ZdS )ModuleContextc                 C  
   i | _ d S r)   r   r   r   r   r   r     rv   zModuleContext.__init__c                 C     t t| jS r)   )r   dictr   r   r   r   r   r     r   zModuleContext.copy_graphstatec                 C  s   t |tsJ |j| _d S r)   )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                   @  r   )
GlobalContextCheckpointStatezDict[str, Tuple[Callable, ...]]global_statec                 C  r   r)   r   )r   Zglobal_statesr   r   r   r     rv   z%GlobalContextCheckpointState.__init__c                 C  r   r   )r   r   r   r   rU   r   r   r   r   r     r   z!GlobalContextCheckpointState.diffc                 C  r   r)   r   rZ   r   r   r   r     r   z#GlobalContextCheckpointState.__eq__N)r   r   r   r   r   r   r   r   r   r   r   r   r     r   r   c                   @  s0   e Zd ZdZh d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autocast_cpu_dtypeZautocast_gpu_dtypeZgrad_enabledZautocast_enabledZautocast_cpu_enabledZtorch_function_enabledZautocast_cache_enabledc                 C  r   r)   r   r   r   r   r   r     rv   zGlobalContext.__init__c                 C  r   r)   )r   r   r   r   r   r   r   r     r   zGlobalContext.copy_graphstatec                 C  sf   t |tsJ |j| _t| jt| jkrt| j | jks#J d| j D ]\}}|| q(d S )NzGlobal state mismatch)r]   r   r   rU   _supported_global_statesr   r   values)r   r   r`   argsr   r   r   r     s   
z GlobalContext.restore_graphstateN)r   r   r   __doc__r   r   r   r   r   r   r   r   r     s    
r   c                   @  sP   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ZdddZ	dS )	GuardsSetNc                 C  s   |d u rt  }|| _d S r)   )r   inner)r   r   r   r   r   r     s   
zGuardsSet.__init__c                 C  
   t | jS r)   )iterr   r   r   r   r   __iter__  rv   zGuardsSet.__iter__c                 C  r   r)   )rU   r   r   r   r   r   __len__  rv   zGuardsSet.__len__c                 C  s   t | j|j S r)   )r   r   rZ   r   r   r   __sub__  r9   zGuardsSet.__sub__c                 C  r   r)   )r7   r   r   r   r   r   __bool__  rv   zGuardsSet.__bool__r   skipguardrE   c                C  sN   || j v rd S |jd u rtjd| d|_|jd u rt |_| j | d S Nr!   r   )r   rC   r   extractr}   TracingContextextract_stackadd)r   r   r   r   r   r   r     s   



zGuardsSet.addothersr   c                 G  s&   |D ]}|D ]	}| j |dd qqd S r   )r   )r   r   ogr   r   r   update  s
   zGuardsSet.updater)   )r   rE   )r   r   )
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    
	r   c                   @  r   )GuardsContextc                 C  s   t  | _g | _d S r)   )r   r   Zaotautograd_guardsr   r   r   r   r     s   
zGuardsContext.__init__c                 C  s   t t| jjS r)   )r   r   r   r   r   r   r   r   r     r9   zGuardsContext.copy_graphstatec                 C  s   t |tsJ t|j| _d S r)   )r]   r   r   r   r   r   r   r   r     s   z GuardsContext.restore_graphstateNr   r   r   r   r   r     s    r   c                   @  s.   e Zd Zed
ddZdd Zedd Zd	S )CompileContextr6   Optional[CompileContext]c                   C     t tdd S Ncompile_contextgetattr_TLSr   r   r   r   get     zCompileContext.getc                 C  s    |d u st |tsJ || _d S r)   )r]   r   
compile_id)r   r   r   r   r   r     s   
zCompileContext.__init__c                  C  s   t  } | d u r
d S | jS r)   )r   r   r   r   r   r   r   current_compile_id  s   z!CompileContext.current_compile_idN)r6   r   )r   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @  st   e Zd ZdZedddZ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.
    r6   Optional[TracingContext]c                   C  r   )Ntracing_contextr   r   r   r   r   r   +  r   zTracingContext.getc                 C  s@   t  | _t | _t | _|| _g | _d | _d | _	d | _
d | _d S r)   )r   Zguards_contextr   Zmodule_contextr   Zglobal_context	fake_modeframe_summary_stackloc_in_frameZfw_metadataZparams_flatoutput_strides)r   r   r   r   r   r   /  s   

zTracingContext.__init__c                  C  sD   t  } | d u rt S t| j}| jd ur|| j tj|S r)   )	r   r   	tracebackStackSummaryrw   r   r   rx   	from_list)r   rC   r   r   r   r   J  s   

zTracingContext.extract_stackc                  c  s    t  } | d usJ dtjj| dg E tjj| dd # zd V  W n ty; } z	t|ds6d |_ d }~ww W d    n1 sFw   Y  W d    d S W d    d S 1 s^w   Y  d S )N=Frame context manager must be called within an ongoing trace.r   r   
real_stack)	r   r   unittestZmockpatchobject	Exceptionhasattrr   )tcer   r   r   clear_frameV  s(   


PzTracingContext.clear_framec              
   c  s    t  }|d usJ d| d ur|j|  |j}d |_z,zd V  W n ty: } zt|ds5| |_ d }~ww W | d urE|j	  ||_d S | d urS|j	  ||_w )Nr   r   )
r   r   r   rx   r   r   r   r   r   pop)frame_summaryr   oldr   r   r   r   current_framex  s0   






zTracingContext.current_framec                  c  sF    t  } | d u rd V  d S | j}g | _z
| jV  W || _d S || _w r)   )r   r   r   )r   Zold_output_stridesr   r   r   report_output_strides  s   
z$TracingContext.report_output_stridesc                 C  s,   t  }|d usJ dt| |||_d S )Nz;Loc context manager must be called within an ongoing trace.)r   r   r   FrameSummaryr   )filenamelinenoZ
frame_namer   r   r   r   set_current_loc  s
   
zTracingContext.set_current_locN)r6   r   )r   r   r   r   r   r   r   r   
contextlibr   r   r   r   r   r   r   r   r   r   #  s$    
 r   contextc                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w r   )r   r   r   )r   old_contextr   r   r   r     s   r   c              
   c  s    t tdd}| t_z<z| V  W n ty+ } zt|ds&| dur&|  |_ d}~ww W | durB| jdurB| jjdurB| jj	  |t_dS | dur\| jdur\| jjdur\| jj	  |t_w )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   r   r   r   r   Z	shape_envcleanup)r   r   r   r   r   r   tracing  s,   	




r   T)frozenc                   @  s>   e Zd Zdd ZdddZddd	ZddddZdddZdS )rF   c                 C     t  r)   r2   )r   Zcodegenr   r   r   reconstruct     zSource.reconstructr6   r    c                 C  r   r)   r   r   r   r   r   rc     r   zSource.guard_sourcer3   c                 C  r   r)   r   r   r   r   r   rO     r   zSource.nameFrE   c                 C  s    |   tju r
t t| ||S r)   )rc   r    r=   r2   rE   )r   fnrI   r   r   r   
make_guard  s   zSource.make_guardr7   c                 C  s   |    S r)   )rc   r:   r   r   r   r   r:     s   zSource.is_nn_moduleNr|   r{   )F)r6   rE   r<   )r   r   r   r   rc   rO   r   r:   r   r   r   r   rF     s    

rF   c                   @  s   e Zd ZU ded< dS )ChainedSourcerF   baseNrD   r   r   r   r   r     s   
 r   inputsr   c                 C  sT  ddl m}m} ddlm} g }t }|dur'|j}|dur'||ddf ddl	m
} tt| D ]\}}	t|	|rE||	d|f q4|| \}
}t|
D ]\}}t||rb||jd|f qP|r|d \}}}|d	d D ]3\}	}}||	u sJ d
| d| d| d|	 d| d| d| d| d|j d| d| d|	j qr|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FakeTensorMode)tree_flattenNztracing context) _get_current_dispatch_mode_stackzactive fake modezfake tensor inputr!   zfake mode (z) from rl   z doesn't match mode (z

fake mode from z allocated at:
z
fake mode from )Ztorch._subclasses.fake_tensorr   r   Ztorch.utils._pytreer   r   r   r   rx   Ztorch.utils._python_dispatchr  	enumeratereversedr]   rC   )r   r   r   r   Z
fake_modesr   r   r  imZflat_inputs_Z
flat_inputZdesc1i1Zdesc2i2r   r   r   detect_fake_mode  sL   


&r	  )r   r   )r   r   r)   )r   r   )=
__future__r   r   dataclassesenumr^   logging	threadingr   Zunittest.mockr   re   abcr   r   r   typingr   r   r   r	   r
   r   r   r   r   r   ZtorchZtorch.utils._tracebackr   	getLoggerr   logZsympyr   Enumr    r?   rA   	dataclassrE   r   r   r   r   r   r   r   r   r   r   r   localr   r   r   r   r   rF   r   r	  r   r   r   r   <module>   st    0
7 
!
# 	

