o
    I&i#o                     @   s  U 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
mZmZmZmZ zd dlZW n eyF   dZY nw d dlZd dlZd dlmZmZmZmZ d dlmZmZ d dlmZmZ d dlm Z! d dl"m#Z# dd	l$m%Z%m&Z& dd
l'm(Z(m)Z) ddl*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z= ddl&m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE ddlFmGZGmHZHmIZI ddlJmKZK ddlLmMZM ddlNmOZO ddlPmQZQmRZR ddlmSZS ddlTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZe efegZhejijegdZkejijegdZlejmjnjFjoZoG dd dZpep Zqep Zrdasete!dd Zudd Zve=jwd d! ZxdDd#d$Zyd aze { Z|ej{e} e~d%< 	&	"	dEd'e+d(ed)efd*d+Zd,d- Zd.d/ Zd d0l mZ d d1lmZ e Zee}eSf e~d2< d3eSd4efd5d6Ze			dFd7ejd8eeef d9eeef d:eeef d'e+d(ed)ed;eKd<e6d=eej d4eeI fd>d?Zd'e+d;eKfd@dAZdBdC ZdS )G    N)AnyCallableDictListOptionalSet)compile_contextCompileContext	CompileIdtracing)log_compilation_eventsignpost_event)ConstraintViolationErrorGuardOnDataDependentSymNode)_forward_from_src)format_traceback_short   )configexc)
is_allowedis_numpy)
CompilerFn)remove_dead_coderemove_pointless_jumps) check_inst_exn_tab_entries_validInstructionis_generator propagate_inst_exn_table_entriestransform_code_object)CacheSizeRelevantForFramecompute_cache_sizeexceeds_cache_size_limitis_recompilation)always_optimize_code_objects	skip_codeTorchPatcher)augment_exc_messageBackendCompilerFailedformat_error_msgInternalTorchDynamoErrorTorchRuntimeErrorUncapturedHigherOrderOpErrorunimplementedUnsupported)CheckFunctionManager&get_and_maybe_log_recompilation_reasonGuardedCode)Hooks)OutputGraph)ExecutionRecord)InstructionTranslatorSpeculationLog)BytecodeHook)CleanupManagerCompilationMetricscounterscprofile_wrapperdynamo_timedformat_bytecodeframe_phase_timinggen_record_file_nameincrement_frameis_namedtupleistype
LazyStringorig_code_mapreset_graph_break_dup_checkersetup_compile_debugtroubleshooting_urlwrite_record_to_filebytecodeZ
recompilesc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Trackerc                 C   s   g | _ t | _d S N)seensetseen_idsself rP   FC:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/convert_frame.py__init__]   s   zTracker.__init__c                    sH   t |  jvr"t| fdd}j| j  d S d S )Nc                    s   j  S rJ   )rM   remove)_idxrO   rP   rQ   <lambda>d   s    zTracker.add.<locals>.<lambda>)idrM   weakrefrefrK   appendadd)rO   Z
strong_objobjrP   rU   rQ   r\   a   s   
zTracker.addc                 C   s   t || jv S rJ   )rX   rM   )rO   itemrP   rP   rQ   __contains__h      zTracker.__contains__c                 C   s   | j   | j  d S rJ   )rK   clearrM   rN   rP   rP   rQ   ra   k   s   
zTracker.clearN)__name__
__module____qualname__rR   r\   r_   ra   rP   rP   rP   rQ   rI   \   s
    rI   c                  O   s   t | i |}t|j |S rJ   )original_forward_from_srcr$   __code__)argskwargsresultrP   rP   rQ   fx_forward_from_src_skip_resultv   s   
rj   c                    s    t   fdd} |_|S )z
    Context manager to:
        1) Save/restore torch.is_grad_enabled() state
        2) Save/restore python random state
        3) Save/restore torch random state
        4) Monkey patch torch.fx.graph_module._forward_from_src
    c               
      sj  t  }t }t }t }t }t }tj }tj	
 r&tj	 }	tjjj}
ttjj_t }zD | i |W |  tj| tjjj| tj||d t| tj| tj	
 rjtj	|	 |
tjj_| swJ dS |  tj| tjjj| tj||d t| tj| tj	
 rtj	|	 |
tjj_| sJ dw )N)Z	warn_onlyz>Global state changed while dynamo tracing, please report a bug)GlobalStateGuardtorchZis_grad_enabledZis_inference_mode_enabledZ$are_deterministic_algorithms_enabledZ-is_deterministic_algorithms_warn_only_enabledrandomgetstateZget_rng_statecudaZis_availableZfxZgraph_moduler   rj   rE   close_CZ_set_grad_enabledZautogradZ	grad_modeZ_enter_inference_modeZuse_deterministic_algorithmssetstateZset_rng_statecheck)rg   rh   guardsZprior_grad_modeZprior_inference_modeZprior_deterministicZprior_warn_onlyZpy_rng_stateZtorch_rng_stateZcuda_rng_stateZprior_fwd_from_srccleanupfnrP   rQ   _fn   sX   










z"preserve_global_state.<locals>._fn)	functoolswraps_torchdynamo_orig_callable)rw   rx   rP   rv   rQ   preserve_global_state   s   	r|   c                    s   | j tv rdS | j jD ]#}|| jv r.| j| }t|r dS tr.tjr.|tu s+t|r. dS qt	  fdd | j
 D ]	} |rG dS q>td| j j| j j| j j dS )z+Check if the frame has torch.* related bitsTc                    sP  t  }|v r| S d|< t tjtjjfs&t tr.t tjjr.d|< | S t	j
rGtrGt tjs?t tjrGd|< | S t ttfr_tfdd D |< | S t tr{t  }tfdd|D |< | S t ttttdtfrd|< | S t rt fdd jD |< | S dS )z)Recursively check if the obj has a tensorFTc                 3       | ]} |V  qd S rJ   rP   .0v
has_tensorrP   rQ   	<genexpr>       z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>c                 3   r}   rJ   rP   r~   r   rP   rQ   r      r   Nc                 3   s    | ]
} t |V  qd S rJ   )getattrr~   )r   r]   rP   rQ   r      s    )rX   
isinstancerl   ZTensornnModulerA   type
issubclassr   trace_numpynpZndarrayZgenericlisttupleanydictvaluesstrintfloatboolr@   _fields)r]   obj_idr   r   rM   )r]   rQ   r      sF   


z'has_tensor_in_frame.<locals>.has_tensorz0skipping because no torch.* %s             %s %sF)f_coder#   co_names	f_globalsr   r   r   r   r   r   f_localsr   logdebugco_nameco_filenameco_firstlineno)framer   r]   valuerP   r   rQ   has_tensor_in_frame   s.   


+r   Fc                 C   s:   d }t | drt| |}t|| j || _t| |d d S )Nexec_recordexport)hasattrr>   rG   r   record_filenamer&   )ecoder   r   r   rP   rP   rQ   exception_handler   s   

r   FRAME_COMPILE_COUNTERTcompiler_fn	one_graphr   c                    sD   t   dtjdtf fdd} |_fdd}||_|S )z&Fully convert a frame into an FX graphr   hooksc                    s  t   | j}t| |}d  t|rt||  t| |tv r!d S tj	
dr2tj	
d|jkr2d S |jdkr?|jdr?d S |jdkrFd S |jdkrY|jtjtjjrYd S |jdkre|jdkred S |jdkrv|jdkrvt| jsvd S t|r~td	 t|rd
d } fdd}tdtj||| t td t| sd S t a d|vrt!|d< t!d7 a!|d }t"| }	t"|  d7  < t#||	}
t$dd|j|j|j%|j&|j'd t(t)  t*| j| j+| j,| j||| ||
d}W d    |S 1 sw   Y  |S )NZTORCHDYNAMO_DEBUG_FUNCTIONz	<genexpr>)ztransformers/file_utils.pyztransformers/utils/generic.pyzdiffusers/utils/outputs.py__setattr__rR   z<module>z<string>z<lambda>	generatorc                 S   s   d| j  d| j d| j dS )N'z' (:))r   r   r   )r   rP   rP   rQ   format_func_infoM  s   zMconvert_frame_assert.<locals>._convert_frame_assert.<locals>.format_func_infoc                      s    sJ d d S )Nz&TODO(whc) any other recompile reasons?rP   rP   Zrecompile_reasonsrP   rQ   format_guard_failuresP  s   zRconvert_frame_assert.<locals>._convert_frame_assert.<locals>.format_guard_failuresztorch._dynamo hit config.cache_size_limit (%s)
   function: %s
   last reason: %s
To log all recompilation reasons, use TORCH_LOGS="recompiles".
To diagnose recompilation issues, see %s.zcache_size_limit reachedZ_idr   Zdynamoz_convert_frame_assert._compile)r   r   r   
cache_sizeZaccumulated_cache_size)frame_state
compile_id)-r?   r   r    r"   r/   input_codesr\   output_codesosenvirongetr   r   endswith
startswithpathdirnamerl   Zoptim__file__r   
f_builtinsr   r,   r!   r   warningr   cache_size_limitrF   r   rk   initial_global_stateFRAME_COUNTERr   r
   r   r   num_cache_entries_in_bucketnum_cache_entriespatch_patch_config_if_changed_compiler   r   )r   cache_entryr   r   r   r   r   r   Zframe_idZframe_compile_idr   Zcompiled_productr   r   export_constraintsr   r   rQ   _convert_frame_assert  s   







z3convert_frame_assert.<locals>._convert_frame_assertc                    s   t |  S rJ   )convert_frame_assertbackend)r   r   r   rP   rQ   _clone_with_backend  r`   z1convert_frame_assert.<locals>._clone_with_backend)rD   types	FrameTyper1   r{   r   )r   r   r   r   r   r   rP   r   rQ   r     s    r   c                  C   s   i } t jj}|  |jjdu r| S |jj}|j|j}}t }|dus'J ||krq|} t	
tjrqt	dtjr=| n| dd tjrK| n| dd  t }| D ]}| | || krpt	d|| | ||  qZ| S )z
    Will return {} if the ambient config is the same as the compile-time.
    Else, returns the compile-time config saved on the code object.
    NzrCurrent config does not match config saved when compiling
Saved hash: %s, Current hash: %s
Restoring saved config.   z* %s=%s (prev: %s))rl   _dynamo
eval_frameZ _maybe_init_guarded_config_cacheZconfig_cacheZsaved_config_and_hashr   hashZget_hashrecompiles_logisEnabledForloggingDEBUGr   verbosehexZshallow_copy_dict)r   r   savedZsaved_configZsaved_config_hashZcurrent_config_hashZconfig_dict_refkeyrP   rP   rQ   r     s@   

r   c                 C   s   t jrt| S | S rJ   )r   Zcprofiler:   )funcrP   rP   rQ   maybe_cprofile  s   r   )OrderedDict)RemovableHandle_bytecode_hookshookreturnc                 C   s   t t}| t|j< |S )zRegister hooks for bytecode generated by Dynamo. The hook can do some
    logging, as well as return a new code object to be used. Please refer
    to `BytecodeHook` for the hook signature.
    )r   r   rX   )r   handlerP   rP   rQ   register_bytecode_hook  s   
r   r   globalslocalsbuiltinsr   r   r   c                    sj  ddl m m}mm} d t 	d }t t 	
fdd}tdddt	j
dtd	td
ttt tttf gtf dtt f
fdd}tt|S zz|
||}|W W ddlm} t|}|d u rd ur|tv rtj} }tjj}tj}t|  dd }t|  dd }dd j!D }dd j"D }nd }d }d }d }d }d }ti }ti }t#|j$j%j&|	j'|	j(|||||||||}t)| W  d    S  t*t+t,t-t.t/|t0|f	y } zt|}t1||
d  d }~w t2y+ } zt|}t1||
d t3t|4|j5d d }~ww ddlm} t|}|d u rzd urz|tv rztj} }tjj}tj}t|  dd }t|  dd }dd j!D }dd j"D }nd }d }d }d }d }d }ti }ti }t#|j$j%j&|	j'|	j(|||||||||}t)| w 1 sw   Y  d S )Nr   )bisectBisectValidationExceptiontranslation_validation_enabledValidationExceptionc                    s`     t| |
	d}z`z0t|jj  |  |  W d    n1 s1w   Y  W d    n1 s@w   Y  W n( tjyS   	    tj
tjfy^     tyn    rm |jj  w W |j  n|j  w |jd usJ jsJ j| d d < |j tjrt|  t|  tt| | d d < d S d S )N)r   speculation_log)Zrestartr4   r   outputZtracing_contextZset_current_txrunr   ZUnspecializeRestartAnalysisra   ZSpeculationRestartAnalysis	SkipFrame	ExceptionZ	shape_envZcall_cleanup_hooksZoutput_instructionsupdatecode_optionsr   Zdead_code_eliminationr   r   r   r   )Zinstructionsr   Ztracerr   r   r   r   r   r   r   r   r   Zmutated_closure_cell_contentsr   r   r   r   rP   rQ   	transform  sZ   
 
z_compile.<locals>.transformZentire_frame_compile)Z
phase_namer   r   r   r   r   c                    s  t  D ][}|t _zt| |}W  nL tjy8 } zt	dt
t|j |dkr.td W Y d }~qd }~w tjy_ } ztd|| j| j| j |rStd W Y d }~ d S d }~ww dd }|d| j| j| j|  |d	| j| j| j| t D ]}|| |}	|	d ur|	}q~| t|< t|  d usJ  jr  rd S  jd usJ  jtj|< t |r|jnd }
t ||
j!}  s|j"d ur|" j  j#$  |S )
NzRestarting analysis due to %sd   z100+ RestartAnalysis() callsz.Skipping frame %s %s                     %s %sz%No graph captured with one_graph=Truec                 S   s*   t tjrt t| |||| d S d S rJ   )bytecode_logr   r   r   r   r<   )prefixnamefilenameline_nor   rP   rP   rQ   log_bytecodeH  s
   z5_compile.<locals>.compile_inner.<locals>.log_bytecodezORIGINAL BYTECODEzMODIFIED BYTECODE)%	itertoolscountr	   r   attemptr   r   ZRestartAnalysisr   inforB   r   __traceback__r,   r   r   r   r   r   r   r   rC   r   r\   r   Zis_empty_graphrt   Zcleanupsr7   instancer.   Zguard_fail_fnr0   check_fnZguard_export_fnZlocal_scopera   )r   r   r   r   r	  Zout_coder   r  r   Zhook_outputr  guarded_code)r   rP   rQ   compile_inner'  s|   






z_compile.<locals>.compile_innerr   )
curr_frameZbackend_compilec                 S      h | ]}|j qS rP   rd   r   oprP   rP   rQ   	<setcomp>  s    z_compile.<locals>.<setcomp>c                 S   r  rP   r  r  rP   rP   rQ   r    s    r   )6Ztorch.fx.experimental.validatorr   r   r   r   rL   r5   r|   r;   r   CodeTyper   r1   r   r   r   r   r   r   r   r0   r   r	   utilsr  r=   lenrt   Zcount_callsgraphnodesZplaceholdersr   non_compliant_opscompliant_custom_opsr8   r   r   r   r   r   r   r-   r*   r'   AssertionErrorr   r   r+   r   r   r)   with_tracebackr  )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zfail_reasonr   r  r  r  Z	frame_keyZguard_countZgraph_op_countZgraph_node_countZgraph_input_countZentire_frame_compile_timeZbackend_compile_timer  r  Zmetricsr   rP   r   rQ   r     s  (-\







r   c                    s<   t | dddtjdtffdd}| |_ fdd|_|S )	zHTry to convert a frame into an FX graph, if error leave frame unmodifiedF)r   r   r   c           
   
      s   t d d  d7  < z | |||}t d d  d7  < |W S  tyj } z@t|tr, t|t}tjs7|s7 t|dd }| j}t	|||| }	|rQt
j|	dd nt
j|	dd W Y d }~d S W Y d }~d S d }~ww )Nframestotalr   okr   T)exc_info)r9   r   r   r+   r-   r   Zsuppress_errorsr   r   r(   r   r
  r   )
r   r   r   r   ri   r   Z	soft_failr   r   	error_msg)inner_convertrP   rQ   _convert_frame  s,   



z%convert_frame.<locals>._convert_framec                    s
   t |  S rJ   )convert_framer   )r   rP   rQ   rW     s   
 zconvert_frame.<locals>.<lambda>)r   r   r   r1   r{   r   )r   r   r%  rP   )r   r$  rQ   r&    s
   *r&  c                 C   s   ddl m} tj}dt_t| d}t|}W d    n1 s!w   Y  tt	|j
 t
  |_
z/zt|j|j
|j|j|ddd t tddd d W n	 tyX   Y nw W |t_d S W |t_d S |t_w )Nr   )eagerFrbr   )r   r   r   r   r   r   r   )Zbackends.debuggingr'  r   Zreplay_record_enabledopenr3   loadr   r  chainr   itemsr   r   r   r   r1   r   r   )r  r'  Zoriginal_replay_valin_filerecordrP   rP   rQ   replay  s8   

r/  )NF)TFN)NNN)collectionsry   r  r   r   rm   r   typingrY   r   r   r   r   r   r   numpyr   ModuleNotFoundErrorrl   Ztorch._loggingZtorch._guardsr   r	   r
   r   Ztorch._utils_internalr   r   Z%torch.fx.experimental.symbolic_shapesr   r   Ztorch.fx.graph_moduler   re   Ztorch.utils._tracebackr    r   r   Zallowed_functionsr   r   Zbackends.registryr   Zbytecode_analysisr   r   Zbytecode_transformationr   r   r   r   r   r   r   r    r!   r"   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   rt   r.   r/   r0   r   r1   Zoutput_graphr2   Zreplay_recordr3   Zsymbolic_convertr4   r5   r6   r  r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   	getLoggerrb   r   Z_loggingZgetArtifactLoggerr  r   rq   r   rk   rI   r   r   r   rz   rj   r|   Z#suppress_torch_distributed_warningsr   r   r   Counterr   r   __annotations__r   r   r   r   r   Ztorch.utils.hooksr   r   r   r  r   objectr   r   r&  r/  rP   rP   rP   rQ   <module>   s   
  (
L

-

L

 +



	
 t4