a
    d                     @   sl  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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mZ d dlmZmZmZ zd dlZdZ W n e!y   dZdZ Y n0 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+ d dl,m-Z-m.Z. ddl/m0Z0mZ1 e 2e j3Z4dZ5e6e7Z8e 9 Z:e; a<dd Z=dd Z>e 9 Z?d a@dd ZAdd ZBd aCdd ZDdd ZEdddZFdddZGe#jHe#jIe#jJfe#jKe#jLe#jMfe#jNe#jOe#jPfe#jQe#jRfe#jSe#jTfe#jUe#jVfe#jWe#jXe#jYfe#jZe#j[e#j\fe#j]e#j^e#j_fe#j`e#jafi
ZbG d d! d!Zcec Zdd"d# Zed$d% Zfd&d' Zgd(d) Zhd*d+ Zie%jjd,d-d.Zkd/d0 Zld1d2 ZmG d3d4 d4Znd5d6 Zod7d8 Zpd9d: Zqd;d< Zrd=d> Zsd?d@ ZtdAdB ZuedCdDdE ZvddFdGZwdHdI ZxejyG dJdK dKZzG dLdM dMenZ{e{ e{_|dNdO Z}dPdQ Z~dRdS ZedTdU ZdVdW ZddXdYZdZd[ Zd\d] Zd^d_ Zedd`da Zdbdc ZddddeZdfdg Zedhdidj Zdkdl Zdmdn Zdodp Zdqdr Zee  Zee 9  Zeee ZejZejZdsdt Zdudv Zdwdx Zdydz Zd{d| Zd}d~ Zdd Zdd Zd dlmZmZ dd Zdd Zdd ZdddZdd Zejdd Zen Ze 2eZG dd dZeddd Zdd Zdd Zdd Zdd Zdd Zee dddZedddZdd ZejdddZdS )    N)contextmanager)	lru_cachewraps)AnyDictListTF)fx)enable_python_dispatcher)
FakeTensor)LazyModuleMixin)tree_flattentree_map   )configloggingz;https://pytorch.org/docs/master/dynamo/troubleshooting.htmlc              
   C   sN   zdd l }|j | |dW S  tyH   ddd t|g| D  Y S 0 d S )Nr   headers
c                 s   s   | ]}d  tt|V  qdS ), N)joinmapstr).0row r   \/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/_dynamo/utils.py	<genexpr>A   s   ztabulate.<locals>.<genexpr>)tabulateImportErrorr   	itertoolschain)rowsr   r   r   r   r   r   ;   s    
r   c                    s   t   fdd}|S )Nc                     s    j tt d }t }|  |j g| R i |}|  td j  dtt d t	
|}|t	jjd |t	jjd || |S )Nz.profilez### Cprofile for z iter z ###   )__name__nexttimer_countercProfileZProfileenableZruncalldisableprintpstatsZStatsZ
sort_statsZSortKeyZTIMEZprint_statsZ
CUMULATIVEZ
dump_stats)argskwargsZdatafnZprofretvalZpsfuncr   r   profile_wrapperG   s    

z(dynamo_profiled.<locals>.profile_wrapperr   )r/   r0   r   r.   r   dynamo_profiledF   s    r2   c                   C   s   t d a d S Nr   )
curr_framer   r   r   r   increment_frame`   s    r5   c                   C   s   t   dad S )Nr   )frame_phase_timingclearr4   r   r   r   r   reset_frame_countf   s    r8   c                 C   s   t | 7 a d S N)op_count)Zcntr   r   r   increment_op_counto   s    r;   c                  C   s   d} i }t  D ]D\}}| D ]2\}}| |7 } ||vrB|||< q ||  |7  < q qd}| D ]"\}}| d| dt|d }qbt| d S )Nr   zTIMING: :   )r6   itemsroundr)   )totalZtotal_by_keyframeZtimingskeyZtimingoutvaluer   r   r   print_time_reporty   s    
rF   c                    s    fdd}| r|| S |S )Nc                    s   t   fdd}|S )Nc                     s    j }|tvrg t|< t } | i |}t | }t| | rtt}|tvrbi t|< t| vsJ d d| |t| < |S )NzDuplicate phase name z for frame )__qualname__compilation_metricstimeappendr   r4   r6   )r+   r,   rC   t0rZ
time_spentZ	frame_key)r/   
phase_namer   r   time_wrapper   s"    
z>dynamo_timed.<locals>.dynamo_timed_inner.<locals>.time_wrapperr1   )r/   rN   rM   r.   r   dynamo_timed_inner   s    z(dynamo_timed.<locals>.dynamo_timed_innerr   )Zoriginal_functionrM   rP   r   rO   r   dynamo_timed   s    rQ   r   c                    s|   dd f fdd	| dkrFfddt D }d}|t|d	d
7 }|S | dkrxfddt  D }tt  }||fS dS )a  
    Get metrics about torchdynamo frontend/backend compilation times.

    Accumulates information from functions tagged with `@dynamo_timed`.

    repr='str' returns a printable string for user interaction, and 'csv'
    returns headers, rows which can be logged for output

    aggregate causes values from multiple compilations (e.g. split graphs)
    to be accumulated into one value.  If false, expect more than one value
    per metric.
    c                 S   s   | S r9   r   xr   r   r   <lambda>       zcompile_times.<locals>.<lambda>c                    s     r|t | S dt|| S )Nr   )sumr   r   )valuesitem_fn)	aggregater   r   fmt_fn   s    zcompile_times.<locals>.fmt_fnr   c                    s$   g | ]}| t | d d dfqS )c                 S   s   | dS )Nz.4fr   rR   r   r   r   rT      rU   *compile_times.<locals>.<listcomp>.<lambda>rX   )rH   r   krZ   r   r   
<listcomp>   s   z!compile_times.<locals>.<listcomp>z!TorchDynamo compilation metrics:
)FunctionzRuntimes (s)r   csvc                    s   g | ]} |d d dqS )c                 S   s   | dS )Nz.6fr   rR   r   r   r   rT      rU   r[   r\   r   )r   vr_   r   r   r`      s   N)rH   r   rW   listkeys)reprrY   r!   rD   rW   r   r   )rY   rZ   r   compile_times   s    

rg   c                   @   s&   e Zd Zd	ddZdd Zdd ZdS )
DuplicateWarningChecker   c                 C   s   || _ |   d S r9   )maxsizereset)selfrj   r   r   r   __init__   s    z DuplicateWarningChecker.__init__c                 C   s   t  | _d S r9   )collectionsOrderedDictsetrl   r   r   r   rk      s    zDuplicateWarningChecker.resetc                 C   sT   || j v r&| j j|dd tjsPdS n*d | j |< t| j | jkrP| j jdd q0dS )NT)lastF)rp   move_to_endr   verboselenrj   popitemrl   rC   r   r   r   add   s    

zDuplicateWarningChecker.addN)ri   )r#   
__module__rG   rm   rk   rx   r   r   r   r   rh      s   
rh   c                   C   s   t jtjtjd t  d S )N)log_file_name)torchdynamo_logginginit_loggingr   Z	log_levelrz   graph_break_dup_warning_checkerrk   r   r   r   r   r|     s    r|   c                 C   s    dd | j D }t|g ddS )Nc                 S   s$   g | ]}|j |j|j|j|jgqS r   )opnametargetr+   r,   )r   nr   r   r   r`   
  rU   z(format_graph_tabular.<locals>.<listcomp>)opcoder   r   r+   r,   r   )nodesr   )graphZ
node_specsr   r   r   format_graph_tabular	  s    r   c                 C   s,   |  d| d| d| dt |   d
S )Nr<   z line z 
z
 )disBytecode)prefixr   filenameline_nocoder   r   r   format_bytecode  s
    r   c                 C   s(   t   d|j dt| j d|j dS )Nz/error_recordings/_z.rec)get_debug_dirco_nametyper#   co_firstlineno)excr   r   r   r   gen_record_file_name  s    r   c                 C   s   znt j| r"td|  d nJt jt j| dd t| d}|| W d    n1 sb0    Y  W n& t	y   tj
d|  dd Y n0 d S )Nz!Unable to write execution record z; file already exists.T)exist_okwbr   )exc_info)ospathexistslogwarningmakedirsdirnameopendump	Exceptionerror)r   Zexec_recordfr   r   r   write_record_to_file  s    
,r   )gc                 C   s&   d}| j D ]}d|jv r
|d7 }q
|S )Nr   callr   )r   r~   )r   cr   r   r   r   count_calls&  s
    


r   c                 C   s   | S r9   r   rR   r   r   r   identity.  s    r   c                  O   s   d S r9   r   r+   r,   r   r   r   nothing2  s    r   c                   @   sJ   e Zd Z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S )ExactWeakKeyDictionaryz\Similar to weakref.WeakKeyDictionary, but use `is`/`id` rather than `==` to compare equalityc                 C   s   t  | _t  | _d S r9   )dictrW   refsrq   r   r   r   rm   9  s    zExactWeakKeyDictionary.__init__c                 C   s   | j t| S r9   )rW   idrw   r   r   r   __getitem__=  s    z"ExactWeakKeyDictionary.__getitem__Nc                 C   s   | j t||S r9   )rW   getr   )rl   rC   defaultr   r   r   r   @  s    zExactWeakKeyDictionary.getc                 C   s   t || jv S r9   )r   rW   rw   r   r   r   __contains__C  s    z#ExactWeakKeyDictionary.__contains__c                    s<   t |  jvr.t| fddj < |j < d S )Nc                    s
     S r9   )
_remove_id)refidxrl   r   r   rT   I  rU   z4ExactWeakKeyDictionary.__setitem__.<locals>.<lambda>)r   r   weakrefr   rW   )rl   rC   rE   r   r   r   __setitem__F  s    
z"ExactWeakKeyDictionary.__setitem__c                 C   s(   || j v r| j |= || jv r$| j|= d S r9   )rW   r   )rl   r   r   r   r   r   L  s    

z!ExactWeakKeyDictionary._remove_idc                 C   s   | j   | j  d S r9   )r   r7   rW   rq   r   r   r   r7   R  s    
zExactWeakKeyDictionary.clear)N)r#   ry   rG   __doc__rm   r   r   r   r   r   r7   r   r   r   r   r   6  s   
r   c                 C   s(   t |tttfrt| |v S t| |u S )zisinstance() without subclasses)
isinstancetuplerd   rp   r   )objZallowed_typesr   r   r   istypeW  s    r   c                 C   s&   t jdk rt| tjS t| tjS d S )N)   	   )sysversion_infor   typing_GenericAlias_SpecialGenericAliasrE   r   r   r   	is_typing^  s    
r   c              
   C   s6   t r.t| tjtjtjtjtjtjtj	tj
fS dS d S NF)	HAS_NUMPYr   npint8int16int32int64uint8Zuint16Zuint32Zuint64r   r   r   r   is_numpy_int_typee  s    r   c                 C   s"   t rt| tjtjtjfS dS d S r   )r   r   r   float16float32float64r   r   r   r   is_numpy_float_typex  s    	r   c                 C   s   t rt| tjS dS d S r   )r   r   r   Zndarrayr   r   r   r   is_numpy_ndarray  s    r   c                 C   s.   t jt jjgtjR }|t jjf }t| |S )zCheck of obj is a tensor)	torchTensornn	Parameterr   Ztraceable_tensor_subclasses_subclassesr
   r   )r   Ztensor_listr   r   r   istensor  s    r   c                 C   s
   t | tS r9   )r   r   modr   r   r   is_lazy_module  s    r   ri   c                  G   s   t |   d S r9   )r)   )r+   r   r   r   
print_once  s    r   c                    s,   |   fdd}t |jdks"J |jd S )zNSome black magic to create a cell object that usually only exists in a closurec                      s    S r9   r   r   rR   r   r   r     s    zmake_cell.<locals>.fr   r   )ru   __closure__)valr   r   rR   r   	make_cell  s    r   c                 C   s   z.t dd | D }dd | D }||fW S  ty } zJddlm} ddlm} |d||   d	|t|   |W Y d }~n
d }~0 0 d S )
Nc                 s   s   | ]}|  V  qd S r9   Zas_proxy)r   argr   r   r   r     rU   z$proxy_args_kwargs.<locals>.<genexpr>c                 S   s   i | ]\}}||  qS r   r   )r   rC   r   r   r   r   
<dictcomp>  rU   z%proxy_args_kwargs.<locals>.<dictcomp>r   unimplemented)typestrzcall_function args: r<   )	r   r?   NotImplementedErrorr   r   Zvariables.baser   rd   rW   )r+   r,   Z
proxy_argsproxy_kwargser   r   r   r   r   proxy_args_kwargs  s    
r   c                   @   s>   e Zd ZU dZeeef ed< eed< dd Ze	dd Z
dS )	CleanupHookz,Remove a global variable when hook is calledscoper   c                 G   s   t  jd8  _| j| j= d S r3   )CleanupManagercountr   r   )rl   r+   r   r   r   __call__  s    zCleanupHook.__call__c                 C   s,   || vsJ t  jd7  _|| |< t| |S r3   )r   r   r   )r   r   r   r   r   r   create  s    zCleanupHook.createN)r#   ry   rG   r   r   r   r   __annotations__r   staticmethodr   r   r   r   r   r     s   
r   c                       s    e Zd ZdZ fddZ  ZS )r   r   c                    s&   | j | D ]
}|  q
t | d S r9   )rW   superr   )rl   r   hook	__class__r   r   r     s    zCleanupManager._remove_id)r#   ry   rG   r   r   __classcell__r   r   r   r   r     s   r   c                 C   s0   |   | j}| jr,| jdur,| j  |_|S )z!Clone the tensor and its gradientN)clonerequires_grad_requires_gradis_leafgradrS   yr   r   r   clone_tensor  s    r   c              	   C   sd  t | tjjr| S dd }t * | jjdkrH|| W  d   S tdd t| 	 | 
 D }| jrt|d f| }ntj|d | j| jd}|  |  d |   }|| 	 | 
 | zD||   | jr|| j | jr| jdurt| j|_W n* ty:   ||  Y W  d   S 0 |W  d   S 1 sV0    Y  dS )	zcopy while preserving stridesc                 S   s<   t | }| jr|| j | jr8| jd ur8t| j|_|S r9   )r   r   r   r   r   r   clone_inputr   r   r   r   torch_clone  s    
z clone_input.<locals>.torch_cloneZxlaNc                 s   s   | ]\}}|d  | V  qdS )r   Nr   )r   shapestrider   r   r   r     s   zclone_input.<locals>.<genexpr>    )dtypedevice)r   r   r   r
   no_gradr  r   rV   zipsizer  Zis_quantizedZempty_quantizedemptyr  Zdata_ptrZelement_sizeZas_strided_copy_r   r   r   r   r   r  RuntimeError)rS   r  Zneeded_sizeresultZcache_line_offsetr   r   r   r    s2    r  c                 C   s   t | trDt| }| D ]$\}}t |tjs2J t|||< q|S t| }tt|D ]$}t || tjrXt|| ||< qX|S r9   )	r   r   r?   r   r   r  rd   rangeru   )example_inputsresrC   rE   ir   r   r   clone_inputs  s    
r  c               	   c   s   t t j } t j r*t t j }z,d V  W t j|  t j r|t j| n$t j|  t j rzt j| 0 d S r9   )r   r   randomget_rng_statecudais_availableset_rng_state)rngZcuda_rngr   r   r   preserve_rng_state  s    


r  c                 C   s&   t | tjjjtjjjtjjtjjfS r9   )	r   r   jitZ_traceZTopLevelTracedModule_scriptZRecursiveScriptModuleZScriptFunctionZScriptModule)Zmodel0r   r   r   is_jit_model*  s    r  c                 C   st   t | r| S ztj| |W S  tyn   ztj| W  Y S  tyh   |rZtd n
td Y n0 Y n0 d S )Nz	jit errorz0Both torch.jit.trace and torch.jit.script failed)	r  r   r  tracer   scriptr   	exceptionr   )modelr  rt   r   r   r   torchscript6  s    r"  c                 C   s&   zt | W S  ty    Y d S 0 d S r9   )inspectgetfile	TypeErrorr   r   r   r   r$  H  s    r$  c                 C   s   t t| S )LTest if an object is a namedtuple or a torch.return_types.* quasi-namedtuple)is_namedtuple_clsr   r&  r   r   r   is_namedtupleO  s    r)  c                 C   sn   zVt | trTt| dg pdg}t| dd}|dkpP|d tu oPt| doPt| dW S W n tyh   Y n0 dS )	r'  	__bases__Nry   torch.return_typesr   _make_fieldsF)
issubclassr   getattrhasattrr%  )clsbasesmoduler   r   r   r(  T  s    
r(  c                 C   s   | t u rg dS t| tsJ t| dr.| jS tjG dd d}| jdksPJ | t|t	| j
}dg| j
 }t|D ]0}|d dkrxtt|||rx||t||j< qx|S )	zIGet the fields of a namedtuple or a torch.return_types.* quasi-namedtuple)startstopstepr-  c                   @   s   e Zd ZU eed< dS )z!namedtuple_fields.<locals>.MarkerindexN)r#   ry   rG   intr   r   r   r   r   Markerm  s   
r9  r+  Nr   r   )slicer.  r   r0  r-  dataclasses	dataclassry   r   r  n_fieldsdirr   r/  r7  )r1  r9  r   fieldsr   r   r   r   namedtuple_fieldsb  s    
r@  c              	      s   t  p t t j t j r4t t j  g t| 	 | 
 D ]}||jt |f qLW d    n1 s~0    Y   fdd}|S )Nc                     sr   t  V t j t j r,t j  D ]\} }}| j|kr0| | q0W d    n1 sd0    Y  d S r9   )r   r  r  r  r  r  _versionr  )paramversionoriginal_valueZcuda_rng_stateZ	rng_statesaved_stater   r   restore  s    


z"checkpoint_params.<locals>.restore)r   r  r   r  r  r  r  r   r    
parametersbuffersrJ   rA  )gmrB  rG  r   rE  r   checkpoint_params{  s    

8	rK  c                 C   sh   t j rt jj}nt}|  t  t d t	 }t
|D ]}| | }|  q@t	 }||| fS )Ni9  )r   r  r  synchronizer   gcZcollectZmanual_seedrI   perf_counterr  )r!  r  timesrL  rK   r   r  t1r   r   r   timed  s    


rQ  c                 C   s    t dd t|| dD S )Nc                 s   s   | ]}|j V  qd S r9   )Zis_cudar   rS   r   r   r   r     rU   z check_is_cuda.<locals>.<genexpr>T)allr   r    rH  )rJ  r  r   r   r   check_is_cuda  s    rT  r  c                 C   sn   | dksJ dd t | D }t|dd  |d d  }tdd| dd| d}d	|  d
|_|S )Nr   c                 S   s   g | ]}d | qS rc   r   )r   r  r   r   r   r`     rU   z rot_n_helper.<locals>.<listcomp>zlambda ,z: ()Zrot_Z_helper)r  reversedevalr   r#   )r   varsZrotatedfnr   r   r   rot_n_helper  s    "r]  c                 C   sV   t | ttfrttt| S t | tjtt	t
tttd ttttjtjfpTt| tjS r9   )r   r   	frozensetrS  r   is_safe_constanttypesCodeTyper8  floatboolr   bytesr   r:  r   r  r  r   enumEnumrU  r   r   r   r_    s&    
r_  c                 C   s   t dd t| | D S )Nc                 s   s   | ]}|  V  qd S r9   )Zis_python_constantrR  r   r   r   r     rU   z&check_constant_args.<locals>.<genexpr>)rS  r   r    rW   r   r   r   r   check_constant_args  s    rg  c                 C   sb   ddl m} ddlm} d}t| | D ],}t||rD|d7 }q,t|||fs, dS q,|dkS )Nr   )ConstantVariable)UnspecializedPythonVariabler   F)Zvariables.constantrh  Zvariables.tensorri  r   r    rW   r   )r+   r,   rh  ri  Zunspec_countrS   r   r   r   check_unspec_python_args  s    

rj  c                 C   sP   g }i }|D ]}| ||  q| D ]\}}|||| i q*||fS r9   )rJ   Zas_specializedr?   update)txr+   r,   Zspecialized_argsZspecialized_kwargsrS   r^   rc   r   r   r   specialize_args_kwargs  s    rm  c                 C   s   t tj| dS r3   )	functoolsreduceoperatormul)itr   r   r   product  s    rs  c                 C   s   |   \}\}}|||  S r9   )
__reduce__)rr  r7  r   r   r4  r   r   r   tuple_iterator_getitem  s    ru  c                 C   s   t jdk rt| S t| S d S )N)r      )r   r   r   rf   r   r   r   r   	enum_repr  s    
rw  c                 C   s   dd |   D S )Nc                 S   s"   h | ]}t |tjjrt|qS r   )r   r   r   r   r   r]   r   r   r   	<setcomp>  rU   z%dict_param_key_ids.<locals>.<setcomp>re   r   r   r   r   dict_param_key_ids  s    rz  c                 C   s   dd |   D S )Nc                 S   s   h | ]}t |tjjs|qS r   )r   r   r   r   r]   r   r   r   rx     rU   z"dict_const_keys.<locals>.<setcomp>ry  r   r   r   r   dict_const_keys  s    r{  c                 C   s6   t dd | D r,dd | D  dd}n| }|S )Nc                 s   s   | ]}t |tjV  qd S r9   )r   re  rf  r]   r   r   r   r     rU   z'dict_const_keys_repr.<locals>.<genexpr>c                 S   s(   h | ] }t |tjrt|nt|qS r   )r   re  rf  rw  rf   r]   r   r   r   rx    rU   z'dict_const_keys_repr.<locals>.<setcomp>' )anyreplace)Z
const_keysZconst_keys_strr   r   r   dict_const_keys_repr  s    r  c                 C   s   dt |  S )NZ__dict_key_)r   )rC   r   r   r   global_key_name  s    r  c                 C   s:   t d| }|r6| dks&J d|  d|d S | S )z
    Usage of inline comprehensions generates a implicit ".0" variable that
    trips up guard generation.  This renames these variables in guards.
    z
^[.](\d+)$z.0zcurrently only .0 supported: Z___implicitr   )rematchgroup)rc   mr   r   r   rename_implicit  s
    r  )FakeTensorModeUnsupportedFakeTensorExceptionc              
   C   s`   z|  W S  t yZ } z:ddlm} d|j d}t| |||W Y d }~n
d }~0 0 d S )Nr   r   zUnsupported: z with fake tensor propagation.)r  r   r   reasonr   r   )r\  r   r   msgr   r   r   wrap_fake_exception&  s    
r  c                    sB   t jj|  t fddW  d    S 1 s40    Y  d S )Nc                      s
   t  S r9   )copydeepcopyr   r&  r   r   rT   3  rU   z)deepcopy_to_fake_tensor.<locals>.<lambda>)r   r   fake_tensorZFakeCopyModer  )r   	fake_moder   r&  r   deepcopy_to_fake_tensor1  s    r  c                 C   s   t t t | | S )z+
    Calculate root mean squared error
    )r   sqrtmeanZsquare)r   r  r   r   r   rmse6  s    r  -C6?c                    s  du rt tttjjtjfrt ttfsNJ dt dt ttkot	 fddt
D S t tr&t tsJ t t ksJ dt  dt   D ]>}t| | |  dstd	|   d
S qdS t tjrt tjjrHJ t tjjr\J jrjspJ   t tjsJ dt dt r&jjkrtdj dj  d
S jtjkr&tjjtjdjtjdd}	|	s"td |	S  r tj tjtjddrfdS tjjjddd}
|
dk rtd|
  ! "   |
dkS sjtjdrdS jtj#krt$" }t$" }d}% dk sNj&dkrDj'd j'd   krBdksNn dkrRd}||| d  k}|std|d d!|d d"(   |S td#  d
S n\t t)t*tdttj+frk}	|	std$t d% d&  |	S t t,r,t-j.d'}	|	s(td( |	S t/s@t0r|rN" ttu ofk}	|	sxtd) |	S t1rttu ok	 S tj2d*v rttu sJ t	 fd+dj3 D S t4d,tj2 dS )-z-Check correctness to see if ref and res matchNztype mismatch r<   c              
   3   s*   | ]"\}}}t ||| V  qd S r9   )same)r   ZaiZbiZ	fp64_refi)cos_similarity	equal_nanexact_dtyperelax_numpy_equalitytolr   r   r   L  s   zsame.<locals>.<genexpr>zkeys mismatch z == r  r  r  r  r  zAccuracy failed for key name FTzdtype mismatch r   )r  )ZatolZrtolr  z+Accuracy failed: uint8 tensor did not matchr   gư>)ZdimZepsgGz?zSimilarity score=g       @i     rV  r   g{Gz?g      @g      $@zRMSE (res-fp64): z.5fz, (ref-fp64): z and shape=z)Accuracy failed: allclose not within tol=zAccuracy failed (z): z != )Zrel_tolZabs_tolz:Accuracy failed (float): {ref} != {res} (within tol={tol})z'Accuracy failed (numpy): {ref} != {res})ZMaskedLMOutputZSeq2SeqLMOutputZ!CausalLMOutputWithCrossAttentionsZLongformerMaskedLMOutputZ	InstancesZSquashedNormalZBoxesZNormalZTanhTransformZFooVariablec                 3   s8   | ]0}t t|t|t| d V  qdS )r  N)r  r/  )r   rC   r  r  r  fp64_refr   r  r  r  r   r   r     s   zunsupported type: )5r   rd   r   r   r   ZParameterListSizer   ru   rS  r	  r   rp   re   r  r   r   r   r   r
   Z	is_sparseZto_denser  rc  Zallclosetor   flattenr   Z
functionalZcosine_similarityr   cpudetachitemr   r  Znumelndimr  r
  r   r8  r  rb  mathiscloser   r   r   r#   __dict__r  )r   r  r  r  r  r  r  r  r^   rL   ZscoreZ	ref_errorZ	res_errorZ
multiplierZpasses_testr   r  r   r  =  s    *$
*




	 


r  c                 C   s,   | j dd }d| j d| d| j dS )N/rV  r|  z' (r=   rX  )co_filenamesplitr   r   )r   Zshort_filenamer   r   r   format_func_info  s    r  c                  c   s,   t j} tjt _zd V  W | t _n| t _0 d S r9   )r   cache_size_limitr   rj   )Zpriorr   r   r   disable_cache_limit  s
    r  c                   @   s:   e Zd ZdZdd ZejjdddZdd Z	d	d
 Z
dS )CompileProfilerzUtility for profiling how and what dynamo would compile.

    Can be used for
     * diagnosing recompilation issues
     * determining an appropriate compile cache limit
     * (TODO)confirming which functions got compiled/skipped
    c                 C   s   d| _ d| _dd | _d S )Nr   c                   S   s   t  S r9   )r  r   r   r   r   rT     rU   z*CompileProfiler.__init__.<locals>.<lambda>)frame_countr:   Zbackend_ctx_ctorrq   r   r   r   rm     s    zCompileProfiler.__init__)rJ  c                 C   s:   |  j d7  _ |jjD ]}d|jv r|  jd7  _q|jS )Nr   r   )r  r   r   r~   r:   Zforward)rl   rJ  r  noder   r   r   r     s
    
zCompileProfiler.__call__c                 C   s   dt iS )Nguard_failures)r  rq   r   r   r   get_metrics  s    zCompileProfiler.get_metricsc                    s   |   }|d   fdd fddfdd D }d}d	tv r|d
7 }|d7 }|d7 }td	 |tfddD ddgd7 }t rtfdd D }|d
7 }|d7 }|d7 }|t|g dd7 }|d
7 }|d| d7 }n|d7 }|S )Nr  c                    s   t  |  S r9   )ru   r   gfr   r   num_recompiles  s    z.CompileProfiler.report.<locals>.num_recompilesc                    s   d dd  |  D S )Nr   c                 S   s   g | ]}t |qS r   )r   rR  r   r   r   r`     rU   zECompileProfiler.report.<locals>.recompile_reasons.<locals>.<listcomp>)r   r  r  r   r   recompile_reasons  s    z1CompileProfiler.report.<locals>.recompile_reasonsc                    s"   g | ]}t | ||gqS r   )r  r   r   )r  r  r   r   r`   !  s   z*CompileProfiler.report.<locals>.<listcomp>zTorchdynamo Profiler Report
Zgraph_breakr   z]The following conditions caused torchdynamo to break out of tracing and fall back to python.
zwYou may gain additional insight by passing `nopython=True` to torch._dynamo.optimize, to break on the first condition.
c                    s   g | ]}| | gqS r   r   )r   r  )graph_breaksr   r   r`   /  rU   zGraph Break ReasonZCountr   c                    s   g | ]} |qS r   r   r  )r  r   r   r`   4  rU   zEThese subgraphs were recompiled more than once due to guard failures.zGuard failures indicate some condition assumed to be static by the tracer changed, making it unsafe to reuse the compiled program.)ra   zNum RecompileszRecompile Reasonsz-Set torch._dynamo.config.cache_size_limit to z to avoid being cache limited.
z*No cache-limited recompilations detected.
)r  countersr   ru   max)rl   ZmetricsZsummarized_gfrptZmax_recompilesr   )r  r  r  r  r   report  sP    zCompileProfiler.reportN)r#   ry   rG   r   rm   r   r   ZGraphModuler   r  r  r   r   r   r   r    s
   r  c                 C   s2   dt j  d d tt  }tj| |S )NZrun_z%Y_%m_%d_%H_%M_%S_%fz-pid_)datetimenowstrftimer   r   getpidr   r   )root_dirdir_namer   r   r   _get_debug_dirM  s    
r  c                  C   s   t j} t| S r9   )r   Zdebug_dir_rootr  )Z
debug_rootr   r   r   r   Y  s    r   c           
   
      s  ddl m}m}m} j}dd }tjjddd}tjj	j
jf|\ t|  t|d|d	krjjj tstj|d	krtrdusJ  i  z|j` t 8 t fd
dW  d   W  d   W S 1 s0    Y  W d   n1 s,0    Y  W n |yN    Y n ty } z|}	|jdurt|j}	t|	tjjjr|d|	j  n@t|	tjjjr|d|	j  nt|	tjjjjr|d | |W Y d}~n
d}~0 0 dS )z]
    Run the computation represented by `node` using fake tensors and return the result.
    r   )TorchRuntimeErrorr   Unsupportedc                 S   s   t | tjrt | tsJ | S r9   )r   r   r   r
   )r   r   r   r   fake_wrapperf  s    z$get_fake_value.<locals>.fake_wrapperr   c                 S   s
   | j d S )Nexample_value)metar  r   r   r   visitk  s    zget_fake_value.<locals>.visitNcall_modulec                      s   t j S r9   )run_nodeoutputr   r+   r,   nnmoduler  rl  r   r   rT     rU   z get_fake_value.<locals>.<lambda>zdata dependent operator: zdynamic shape operator: z*guard on data-dependent symbolic int/float) r   r  r   r  r~   r   r   Noder  map_argr+   r,   r   r  
nn_modulesr   r   r  r  r	   r  r  	__cause__r   r   r  ZDataDependentOutputExceptionr/   ZDynamicOutputShapeExceptionZexperimentalZsymbolic_shapesZGuardOnDataDependentSymNode)
r  rl  r  r   r  r~   r  r  r   causer   r  r   get_fake_value^  sN    

X

r  c                 C   s  |j }z|dkr"|j|i |W S |dkrNt|d |j|dd i |W S |dkrr|dusbJ ||i |W S |dkr| |jW S |dkrd	|jv sJ |jd	 W S W nR t y } z8td
| d|j d| d| d| d|W Y d}~n
d}~0 0 t|dS )a  
    Runs a given node, with the given args and kwargs.

    Behavior is dicatated by a node's op.

    run_node is useful for extracting real values out of nodes.
    See get_real_value for more info on common usage.

    Note: The output_graph arg is only used for 'get_attr' ops
    Note: The nnmodule arg is only used for 'call_module' ops

    Nodes that are not call_function, call_method, call_module, or get_attr will
    raise an AssertionError.
    call_functionZcall_methodr   r   Nr  Zget_attrplaceholderr  zFailed running r<   z(*z, **z):
z
(scroll up for backtrace))r~   r   r/  Zget_submoduler  r   r  AssertionError)output_graphr  r+   r,   r  r~   r   r   r   r   r    s*    $$r  c           	   
      s    j }| |v r||  S | j}tjj| j| jf fdd\}}|dkrx j| j	 }t
|sht|}q|||i | nd}zt | |||}||| < W n, ty } zt |W Y d}~n
d}~0 0 |S )z
    Run the actual computation represented by `node` and return the result.
    This will execute any dependent nodes in the graph as well.
    c                    s
   t |  S r9   )get_real_valuer  r  r   r   rT     rU   z get_real_value.<locals>.<lambda>r  N)Zreal_value_cacher~   r   r   r  r  r+   r,   r  r   r   r  r  r  r  r  )	r  r  cacher~   r+   r,   Z	nn_module
real_valuer   r   r  r   r    s(    

r  c                    s   ddl m   fdd}|  D ].\}}t|tjjr J d| d|| q |  D ].\}}t|tjjrXJ d| d|| qXd S )Nr   FakeTensorConfigc                    s(    j r dd l}d|| j S dS d S )Nr   z"FAKE TENSOR CREATION TRACEBACK: 
 zNEnable TORCH_FAKE_TENSOR_DEBUG=1 to get creation stack traces on fake tensors.)debug	tracebackformat_listZ_debug_trace)tr  r  r   r   stack_or_hint  s    z7assert_no_fake_params_or_buffers.<locals>.stack_or_hintzUnexpected fake buffer r<   zUnexpected fake param )torch._subclasses.fake_tensorr  Znamed_buffersr   r   r   r
   Znamed_parameters)rJ  r  r   bufferrB  r   r  r    assert_no_fake_params_or_buffers  s    r  )inputsc                 C   sJ   t | \}}d}|D ]0}t|tjjr|du r6|j}q||ju sJ q|S )z
    Takes a list of anything, unflattened is fine, returns a fake_mode
    if any are fake. All fake modes on all fake tensors must be identical.
    Returns None if no fake_mode is fine
    N)r   r   r   r   r
   r  )r  Zflat_inputsr   r  Z
flat_inputr   r   r   fake_mode_from_tensors  s    r  r&  c                 C   s   | j  d| j S )z9
    Returns the fully qualified name of the object.
    .)ry   rG   r&  r   r   r   fqn  s    r  c                 C   s   t jjjr| S |S d S r9   )r   Z_dynamor   Zdynamic_shapes)Zcount1Zcount2r   r   r   ifdyn  s    
r  r   c                 C   sV   t ttj| jD ]8}|dr|d dkrt| j	 d|dd   qdS )z@
    Ensure all the files in a given submodule are imported
    z.pyr   r   r  N)
sortedr   listdirr   r   __file__endswith	importlibimport_moduler#   )r   r   r   r   r   import_submodule  s    r  )NN)r   F)N)F)r   )NFr  FTF)rn   
contextlibr  r&   r;  r  r   re  rn  rM  r#  r   logging.configr   r  rp  r   r*   r  r   rI   r`  r   r   r   r   r   r   r   r   numpyr   r   ModuleNotFoundErrorr  r   Z%torch.fx.experimental.symbolic_shapesr   Ztorch._dispatch.pythonr	   r  r
   Ztorch.nn.modules.lazyr   Ztorch.utils._pytreer   r   r}  r   r{   defaultdictCounterr  Ztroubleshooting_url	getLoggerr#   r   ro   rH   r   r%   r   r2   r6   r4   r5   r8   r:   r;   rF   rQ   rg   ZFloatTensorr   rb  ZDoubleTensorr   doubleZ
HalfTensorr   ZhalfZBFloat16TensorZbfloat16Z
ByteTensorr   Z
CharTensorr   Z
LongTensorr   longZ	IntTensorr   r8  ZShortTensorr   shortZ
BoolTensorrc  Ztensortype_to_dtyperh   r}   r|   r   r   r   r   ZGraphr   r   r   r   r   r   r   r   r   r   r   r   r   r   r<  r   r   instancer   r  r  r  r  r"  r$  r)  r(  r@  rK  rQ  rT  r]  r_  rg  rj  rm  r   r   rW   dict_valuesZodict_valuesiterr   tuple_iterator__length_hint__Ztuple_iterator_lenobject__new__Z
object_newrs  ru  rw  rz  r{  r  r  r  Ztorch._subclassesr  r  r  r  r  r  r  r  Zorig_code_maprd   r  r  r  r   r  r  r  r  r  r  r  
ModuleTyper  r   r   r   r   <module>   s8  


"

&



!

	-




	
	
      
 +

N
:$"