U
    5-ed                     @  sh  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 Z1G d"d# d#Z2G d$d% d%e,e- Z3e4 Z5G d&d' d'Z6G d(d) d)Z7ed'd*d+d,Z8ed)d*d-d.Z9ej'd/d0G d1d2 d2Z:ej'd/d0G d3d4 d4e:Z;d9d5d6d7d8Z<dS ):    )annotationsN)ABCabstractmethod)contextmanager)
AnyCallableDictGenericList
NamedTupleOptionalSetTupleTypeVar)CapturedTracebackc                   @  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   N/var/www/html/Darija-Ai-Train/env/lib/python3.8/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                   @  s\   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dZddddZdd ZdS )GuardSourcer                           c                 C  sH   | t jt jt jt jt jfkr |S | t jt jt jfkr8|S t	t
| d S 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boolreturnc                 C  s   | t jt jfkS r(   )r   r0   r+   r   r   r   r   is_fsdp_moduleZ   s    zGuardSource.is_fsdp_modulec                 C  s   | t jt jfkp|  S r(   )r   r/   r*   r8   r   r   r   r   is_nn_module]   s    zGuardSource.is_nn_modulec                 C  s   | t jt jt jfkS r(   )r   r)   r*   r+   r   r   r   r   is_localg   s
    zGuardSource.is_localN)r   r   r   r)   r.   r*   r/   CONSTANTr-   r,   r+   r0   r4   r8   r9   r:   r   r   r   r   r   8   s   
r   c                   @  s   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   r>      s   
r>   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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'd(Zd)d* Zd+d, Zd-d. Zd/d0 ZdS )1GuardSourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]	create_fnFr5   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idrE   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)rM   valuelenrL   inner_create_fn__code__co_firstlinenor   r   r   r   sort_key   s
    
zGuard.sort_keyc                 C  s   |   |  k S r(   )rV   r   otherr   r   r   __lt__   s    zGuard.__lt__c                 C  s    t | jtjr| jjS | jS d S r(   )
isinstancerE   	functoolspartialfuncr   r   r   r   rS      s    zGuard.inner_create_fnr2   r6   c                 C  s
   | j  S r(   )rD   rL   r   r   r   r   rL      s    z
Guard.namer   c                 C  s
   | j  S r(   )rD   guard_sourcer   r   r   r   rM      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>)rZ   weakrefReferenceTypehexrN   	__class__r   r2   )rI   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
        }
        )rM   rL   lowerreprrS   r   rG   rH   re   rI   rJ   )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: 
rf   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )ri   rL   rM   rh   rS   r   rG   rH   re   rI   rJ   )r   outputrM   r   r   r   r      s    zGuard.__str__r<   )local_builderglobal_builderc                 C  s   |  | j||| S r(   )rE   rM   r4   )r   rn   ro   r   r   r   create   s    zGuard.createc                 C  s
   | j  S r(   )rM   r9   r   r   r   r   r9      s    zGuard.is_nn_modulec                 C  s
   | j  S r(   )rM   r8   r   r   r   r   r8      s    zGuard.is_fsdp_modulec                 C  s
   | j  S r(   )rM   r:   r   r   r   r   r:      s    zGuard.is_localc                 C  sp   | j st | _ | j | | j|d fks0td|| _| jsD|| _n| j| | j|d fksftd|| _d S )Nz+Guarded class id must be identical, or Nonez)Guarded object must be identical, or None)rG   listappendrJ   AssertionErrorrH   extendrI   )r   Z
guard_typeZguarded_classrH   rI   r   r   r   set_export_info   s(    zGuard.set_export_info)r   r   r   r   rF   rG   rH   rI   rJ   r@   
user_stackrO   rV   rY   rS   propertyrL   rM   staticmethodre   rk   r   rp   r9   r8   r:   ru   r   r   r   r   rB      s4   

rB   Tc                   @  s   e Zd ZdS )GuardEnvExprNr=   r   r   r   r   rz      s   rz   c                   @  s&   e Zd ZU ded< ded< dd ZdS )DuplicateInputsrC   input_source_ainput_source_bc                 C  s   | j | jkstd S r(   )r|   r}   rs   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                   @  s0   e Zd ZeddddZeddddZdS )	Checkpointablery   r6   c                 C  s   d S r(   r   r   r   r   r   copy_graphstateB  s    zCheckpointable.copy_graphstate)statec                 C  s   d S r(   r   r   r   r   r   r   restore_graphstateF  s    z!Checkpointable.restore_graphstateN)r   r   r   r   r   r   r   r   r   r   r   A  s   r   c                   @  s4   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  s
   || _ d S r(   )r   )r   r   r   r   r   __init__S  s    zGuardsCheckpointState.__init__c                 C  s"   | j |j }t|dkrd S |S Nr   )r   
differencerR   r   rX   rr   r   r   diff]  s    zGuardsCheckpointState.diffc                 C  s   |  |d kS r(   r   rW   r   r   r   __eq__c  s    zGuardsCheckpointState.__eq__N)	r   r   r   setr   r   r   r   r   r   r   r   r   r   P  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 r(   r   )r   r   r   r   r   r   j  s    z%ModuleContextCheckpointState.__init__c                 C  s2   t | j t |j }t|dkr.d S |S r   )r   r   keysr   rR   r   r   r   r   r   t  s    z!ModuleContextCheckpointState.diffc                 C  s   |  |d kS r(   r   rW   r   r   r   r   z  s    z#ModuleContextCheckpointState.__eq__N)r   r   r   r   r   r   r   r   r   r   r   r   r   g  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 r(   r   r   r   r   r   r     s    zModuleContext.__init__c                 C  s   t t| jS r(   )r   dictr   r   r   r   r   r     s    zModuleContext.copy_graphstatec                 C  s   t |tst|j| _d S r(   )rZ   r   rs   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 r(   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 r   )r   r   r   r   rR   r   r   r   r   r     s    z!GlobalContextCheckpointState.diffc                 C  s   |  |d kS r(   r   rW   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 r(   r   r   r   r   r   r     s    zGlobalContext.__init__c                 C  s   t t| jS r(   )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)	rZ   r   rs   r   rR   _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                   @  sX   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ZddddZ	dS )	GuardsSetNc                 C  s   |d krt  }|| _d S r(   )r   inner)r   r   r   r   r   r     s    zGuardsSet.__init__c                 C  s
   t | jS r(   )iterr   r   r   r   r   __iter__  s    zGuardsSet.__iter__c                 C  s
   t | jS r(   )rR   r   r   r   r   r   __len__  s    zGuardsSet.__len__c                 C  s   t | j|j S r(   )r   r   rW   r   r   r   __sub__  s    zGuardsSet.__sub__c                 C  s
   t | jS r(   )r5   r   r   r   r   r   __bool__  s    zGuardsSet.__bool__r   skiprB   )guardc                C  sN   || j krd S |jd kr*tjd| d|_|jd kr>t |_| j | d S Nr    r   )r   r@   r   extractrv   TracingContextextract_stackadd)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.update)N)
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 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     s    zGuardsContext.copy_graphstatec                 C  s   t |tstt|j| _d S r(   )rZ   r   rs   r   r   r   r   r   r   r     s    z GuardsContext.restore_graphstateNr   r   r   r   r   r     s   r   c                   @  s2   e Zd ZeddddZdd Zedd Zd	S )
CompileContextzOptional[CompileContext]r6   c                   C  s   t tdd S Ncompile_contextgetattr_TLSr   r   r   r   get  s    zCompileContext.getc                 C  s    |d kst |tst|| _d S r(   )rZ   r   rs   
compile_id)r   r   r   r   r   r     s    zCompileContext.__init__c                  C  s   t  } | d krd S | jS r(   )r   r   r   r   r   r   r   current_compile_id  s    z!CompileContext.current_compile_idN)r   r   r   rx   r   r   r   r   r   r   r   r     s
   r   c                   @  sx   e Zd ZdZed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.
    zOptional[TracingContext]r6   c                   C  s   t tdd S )Ntracing_contextr   r   r   r   r   r   +  s    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 krt S t| j}| jd k	r8|| j tj|S r(   )	r   r   	tracebackStackSummaryrq   r   r   rr   	from_list)r   r@   r   r   r   r   J  s    

zTracingContext.extract_stackc                  c  s   t  } | d k	stdtjj| dg d tjj| dd F z
d V  W n4 tk
r~ } zt|dsld |_	 W 5 d }~X Y nX W 5 Q R X W 5 Q R X d S )N=Frame context manager must be called within an ongoing trace.r   r   
real_stack)
r   r   rs   unittestZmockpatchobject	Exceptionhasattrr   )tcer   r   r   clear_frameV  s$      

zTracingContext.clear_framec              
   c  s   t  }|d k	std| d k	r,|j|  |j}d |_zHz
d V  W n8 tk
r| } zt|dsj|	 |_
 W 5 d }~X Y nX W 5 | d k	r|j  ||_X d S )Nr   r   )r   r   rs   r   rr   r   popr   r   r   r   )frame_summaryr   oldr   r   r   r   current_framex  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 r(   )r   r   r   )r   Zold_output_stridesr   r   r   report_output_strides  s    z$TracingContext.report_output_stridesc                 C  s,   t  }|d k	stdt| |||_d S )Nz;Loc context manager must be called within an ongoing trace.)r   r   rs   r   FrameSummaryr   )filenamelinenoZ
frame_namer   r   r   r   set_current_loc  s    zTracingContext.set_current_locN)r   r   r   r   rx   r   r   r   
contextlibr   r   r   r   r   r   r   r   r   r   #  s"   
 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   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cleanupr   r   r   r   )r   r   r   r   r   r   tracing  s     	


r   T)frozenc                   @  sN   e Zd Zdd ZddddZdddd	ZdddddZddddZdS )rC   c                 C  s
   t  d S r(   r1   )r   Zcodegenr   r   r   reconstruct  s    zSource.reconstructr   r6   c                 C  s
   t  d S r(   r   r   r   r   r   r^     s    zSource.guard_sourcer2   c                 C  s
   t  d S r(   r   r   r   r   r   rL     s    zSource.nameFrB   c                 C  s    |   tjkrt t| ||S r(   )r^   r   r;   r1   rB   )r   fnrF   r   r   r   
make_guard  s    zSource.make_guardr5   c                 C  s   |    S r(   )r^   r9   r   r   r   r   r9     s    zSource.is_nn_moduleN)F)r   r   r   r   r^   rL   r   r9   r   r   r   r   rC     s
   rC   c                   @  s   e Zd ZU ded< dS )ChainedSourcerC   baseNrA   r   r   r   r   r     s   
r   r   )inputsc                 C  sZ  ddl m}m} ddlm} g }t }|dk	rN|j}|dk	rN||ddf ddl	m
} tt| D ]"\}}	t|	|rh||	d|f qh|| \}
}t|
D ]$\}}t||r||jd|f q|rR|d \}}}|d	d D ]f\}	}}||	kst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FakeTensorMode)tree_flattenNztracing context) _get_current_dispatch_mode_stackzactive fake modezfake tensor inputr    zfake mode (z) from rg   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   rr   Ztorch.utils._python_dispatchr   	enumeratereversedrZ   rs   r@   )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  s0    


Nr   )N)=
__future__r   r   dataclassesenumr[   logging	threadingr   Zunittest.mockr   r`   abcr   r   r   typingr   r   r   r	   r
   r   r   r   r   r   ZtorchZtorch.utils._tracebackr   	getLoggerr   logZsympyr   Enumr   r<   r>   	dataclassrB   ry   rz   r{   r   r   r   r   r   r   r   r   localr   r   r   r   r   rC   r   r   r   r   r   r   <module>   sb   0

E 

+# 	

