o
    h_                     @   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 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% ddl&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4 ddl#m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF ddlGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY eZe[Z\ej]^e[dZ_ej]^e[dZ`G dd dZaea Zbea Zcdaddaedafegedd Zhdd Zie4jjdd Zkd<d!d"Zld#d$ Zmd%d& Znd aoe p Zqejper esd'< 	(	 	d=d)e'd*etd+etfd,d-Zu			d>d.ejvd/eewexf d0eewexf d1eewexf d)e'd*etd+etd2e@d3erd4eejy d5ee> fd6d7Zzd)e'd2e@fd8d9Z{d:d; Z|dS )?    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)
CompilerFn)remove_dead_coderemove_pointless_jumps) check_inst_exn_tab_entries_validInstructionis_generator propagate_inst_exn_table_entriestransform_code_object)always_optimize_code_objects	skip_codeTorchPatcher)augment_exc_messageBackendCompilerFailedformat_error_msgInternalTorchDynamoErrorTorchRuntimeErrorunimplementedUnsupported)CheckFunctionManagerGuardedCode)Hooks)OutputGraph)ExecutionRecord)InstructionTranslator)CleanupManagerCompilationMetricscountersdynamo_timedformat_bytecodeframe_phase_timinggen_record_file_nameguard_failuresincrement_frame"is_guard_failure_reporting_enabledis_namedtupleistype
LazyStringorig_code_mapreset_graph_break_dup_checkersetup_compile_debugtroubleshooting_urlwrite_record_to_filebytecode
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 rI   Q/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py__init__L   s   zTracker.__init__c                    sH   t |  jvr"t| fdd}j| j  d S d S )Nc                    s   j  S rC   )rF   remove)_idxrH   rI   rJ   <lambda>S   s    zTracker.add.<locals>.<lambda>)idrF   weakrefrefrD   appendadd)rH   
strong_objobjrI   rN   rJ   rU   P   s   
zTracker.addc                 C   s   t || jv S rC   )rQ   rF   )rH   itemrI   rI   rJ   __contains__W      zTracker.__contains__c                 C   s   | j   | j  d S rC   )rD   clearrF   rG   rI   rI   rJ   r[   Z   s   
zTracker.clearN)__name__
__module____qualname__rK   rU   rY   r[   rI   rI   rI   rJ   rB   K   s
    rB   c                  O   s   t | i |}t|j |S rC   )original_forward_from_srcr   __code__)argskwargsresultrI   rI   rJ   fx_forward_from_src_skip_resulth   s   
rd   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               	      s   t  }t }t j }t j rt j }t jjj	}t
t jj_	t }z- | i |W |  t j| t| t j| t j rLt j| |t jj_	S |  t j| t| t j| t j rrt j| |t jj_	w rC   )torchis_grad_enabledrandomgetstateget_rng_statecudais_availablefxgraph_moduler   rd   r=   close_C_set_grad_enabledsetstateset_rng_state)ra   rb   prior_grad_modepy_rng_statetorch_rng_statecuda_rng_stateprior_fwd_from_srccleanupfnrI   rJ   _fnz   s0   








z!wrap_convert_context.<locals>._fn)	functoolswraps_torchdynamo_orig_callable)rz   r{   rI   ry   rJ   wrap_convert_contextq   s   	r   c                    s   | j tv rdS | j jD ]}|| jv rt| j| r dS qt  fdd | j D ]	} |r5 dS q,t	d| j j
| j j| j j dS )z+Check if the frame has torch.* related bitsTc                    s  t  }|v r| S d|< t tjtjjfr"d|< | S t ttfr:t	fdd D |< | S t t
rVt  }t	fdd|D |< | S t ttttdtfrjd|< | 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 rC   rI   .0v
has_tensorrI   rJ   	<genexpr>       z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>c                 3   r   rC   rI   r   r   rI   rJ   r      r   Nc                 3   s    | ]
} t |V  qd S rC   )getattrr   )r   rW   rI   rJ   r      s    )rQ   
isinstancere   TensornnModuler9   listtupleanydictvaluesstrintfloattypeboolr8   _fields)rW   obj_idr   r   rF   )rW   rJ   r      s*   
z'has_tensor_in_frame.<locals>.has_tensorz0skipping because no torch.* %s             %s %sF)f_coder   co_names	f_globalsr   r   f_localsr   logdebugco_nameco_filenameco_firstlineno)framer   valuerI   r   rJ   has_tensor_in_frame   s(   

"r   Fc                 C   s:   d }t | drt| |}t|| j || _t| |d d S )Nexec_recordexport)hasattrr4   r?   r   record_filenamer!   )ecoder   r   r   rI   rI   rJ   exception_handler   s   

r   c                 C   s   | dkS )Nr   rI   )
cache_sizerI   rI   rJ   is_recompilation   s   r   c                 C   s   d}| r|d7 }| j } | s|S )Nr   r   )next)cache_entryrunning_cache_sizerI   rI   rJ   compute_cache_size   s   r   FRAME_COMPILE_COUNTERTcompiler_fn	one_graphr   c                    sH   t   dtjdtf fdd} |_fdd}||_t|S )z&Fully convert a frame into an FX graphr   hooksc                    s  t   | j}t|}t|rYttjstj	rYt
 r4d|j d|j d|j dtt| d  f}nd|j d|j d|j df}ttjrQtj|dd tj	rYt|t| |tv rdd S tjd	rutjd	|jkrud S |jd
kr|jdrd S |jdkrd S |jdkr|jtjtjjrd S |jdkr|jdkrd S |jdkr|jdkrt | j!sd S t"|rt#d |tj$krdd }dd }tj%r|tv sJ dt&'dtj$||||t( nt&'dtj$||t( t#d t)| sd S t* a+t, a-tj./ a0d|vrt1|d< t1d7 a1|d }	t2|	 }
t2|	  d7  < t3|	|
}t4dd|j|j|j|d t5| j| j6| j7| j! ||| ||dS ) NzRecompiling function z in :z*triggered by the following guard failure: z@set env var TORCHDYNAMO_REPORT_GUARD_FAILURES=1 to debug furtherT)
stack_infoTORCHDYNAMO_DEBUG_FUNCTIONz	<genexpr>)ztransformers/file_utils.pyztransformers/utils/generic.py__setattr__rK   z<module>z<string>z<lambda>	generatorc                 S   s   d| j  d| j d| j dS )N'z' (r   ))r   r   r   r   rI   rI   rJ   format_func_info@  s   zMconvert_frame_assert.<locals>._convert_frame_assert.<locals>.format_func_infoc                 S   s   dt t|  d  dS )Nz  reasons: r   
)r   r5   r   rI   rI   rJ   format_guard_failuresC  s   zRconvert_frame_assert.<locals>._convert_frame_assert.<locals>.format_guard_failuresz&TODO(whc) any other recompile reasons?zxtorch._dynamo hit config.cache_size_limit (%s)
   function: %s
   reasons:  %s
to diagnose recompilation issues, see %s.ztorch._dynamo hit config.cache_size_limit (%s)
   function: %s
to diagnose recompilation issues, set env variable TORCHDYNAMO_REPORT_GUARD_FAILURES=1 and also see %s.zcache_size_limit reached_idr   dynamoz_convert_frame_assert._compile)r   r   r   r   )frame_state
compile_id)8r6   r   r   r   recompiles_logisEnabledForloggingDEBUGr   error_on_recompiler7   r   r   r   r   r5   r   r   RecompileErrorinput_codesrU   output_codesosenvirongetendswith
startswithpathdirnamere   optim__file__r   
f_builtinsr   r&   cache_size_limitreport_guard_failuresr   warningr>   r   rf   initial_grad_state$are_deterministic_algorithms_enabled&initial_deterministic_algorithms_statero   _is_torch_function_enabledinitial_torch_function_stateFRAME_COUNTERr   r
   r   _compiler   r   )r   r   r   r   r   r   messager   r   frame_idframe_compile_idr   r   r   export_constraintsr   rI   rJ   _convert_frame_assert   s   







	


z3convert_frame_assert.<locals>._convert_frame_assertc                    s   t |  S rC   )convert_frame_assertbackend)r   r   r   rI   rJ   _clone_with_backend  rZ   z1convert_frame_assert.<locals>._clone_with_backend)r<   types	FrameTyper*   r~   r   r   )r   r   r   r   r   r   rI   r   rJ   r      s    r   r   globalslocalsbuiltinsr   r   r   returnc                    s  ddl mm} d 
t d } 	
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| zzo|	||}|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 }nd }d }d }d }d }d }t|jjj |	|||||||}t!| W  d    S  t"t#t$t%t&t'|fy } zt|}t(||
d  d }~w t)y } zt|}t(||
d t*t|+|j,d d }~ww ddlm} t|}|d u r>
d ur>|tv r>t
j}
 }t
jj}t
j}t| dd }t| dd }nd }d }d }d }d }d }t|jjj |	|||||||}t!| w 1 sew   Y  d S )Nr   )translation_validation_enabledValidationExceptionc                    s  t |  |	d}zt|jj |  W d    n1 s&w   Y  W n, tjtjfy8     tyX    rW|jj	}|jj
dd |D dd |D   w |j

d usbJ 
jsgJ 
j| d d < |
j tjrt|  t|  tt| | d d < d S d S )N)r   c                 S      g | ]}|j qS rI   )faker   arI   rI   rJ   
<listcomp>      z/_compile.<locals>.transform.<locals>.<listcomp>c                 S   r   rI   )sourcer   rI   rI   rJ   r     r   )r-   r   outputtracing_contextrunr   RestartAnalysis	SkipFrame	Exceptiontracked_fakes	shape_envproduce_guardsoutput_instructionsupdatecode_optionsr   dead_code_eliminationr   r   r   r   )instructionsr  tracerfakesr   r   r   r   r   r   r   r   mutated_closure_cell_contentsr   r   r   rI   rJ   	transform  sP   
	
z_compile.<locals>.transformentire_frame_compile)
phase_namer   r   r   r  r   c           
         s  t  D ]Z}zt| |}| t|< W  nL tjy7 } 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 |rRtd W Y d }~ d S d }~ww t| dd }|d| j| j| j|  |d	| j| j| j|  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 rC   )bytecode_logr   r   r   r   r2   )prefixnamefilenameline_nor   rI   rI   rJ   log_bytecode  s
   z5_compile.<locals>.compile_inner.<locals>.log_bytecodezORIGINAL BYTECODEzMODIFIED BYTECODE) 	itertoolscountr   r;   r   r   r   infor:   r   __traceback__r&   r  r   r   r   r   r   rU   r   is_empty_graphguardscleanupsr.   instancer(   guard_fail_fnr)   check_fnguard_export_fnlocal_scoper[   )
r   r   r   r  attemptout_coder   r  r"  guarded_code)r   rI   rJ   compile_inner  sp   




z_compile.<locals>.compile_innerr   )
curr_framebackend_compiler   )-torch.fx.experimental.validatorr   r   rE   r1   r   CodeTyper   r*   r   r   r   r   r   r   r   r)   r   r	   utilsr)  r3   lenr  count_callsgraphnodesplaceholdersr   r/   r   r   r   r   r'   r%   r"   AssertionErrorr   r   r   r  r$   with_tracebackr  )r   r   r   r   r   r   r   r   r   r   r   r   r   r   fail_reasonr  r(  r'  r)  	frame_keyguard_countgraph_op_countgraph_node_countgraph_input_countentire_frame_compile_timebackend_compile_timemetricsr   rI   r  rJ   r     s   "*U

	





r   c                    s@   t | dddtjdt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   r   c           
   
      s   t d d  d7  < z | |||}t d d  d7  < |W S  tyd } z:t|t}tjs1|s1 t|dd }| j}t|||| }	|rKt	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)r0   r  r   r'   r   suppress_errorsr   r   r#   r   r  r   )
r   r   r   r   rc   r   	soft_failr   r   	error_msg)inner_convertrI   rJ   _convert_frame|  s(   


z%convert_frame.<locals>._convert_framec                    s
   t |  S rC   )convert_framer   )r   rI   rJ   rP     s   
 zconvert_frame.<locals>.<lambda>)r   r   r   r   r*   r~   r   )r   r   rF  rI   )r   rE  rJ   rG  x  s   'rG  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 dd d W n	 tyU   Y nw W |t_d S W |t_d S |t_w )Nr   )eagerFrbr   )r   r   r   r   r   r   r   )backends.debuggingrH  r   replay_record_enabledopenr,   loadr   r  chainr   itemsr   r   r   r   r*   r  )r  rH  original_replay_valin_filerecordrI   rI   rJ   replay  s8   

rS  )NF)TFN)NNN)}collectionsr|   r  r   r   rg   r   typingrR   r   r   r   r   r   r   re   torch._loggingtorch._guardsr   r	   r
   r   torch._utils_internalr   r   %torch.fx.experimental.symbolic_shapesr   r   torch.fx.graph_moduler   r_   torch.utils._tracebackr    r   r   allowed_functionsr   backends.registryr   bytecode_analysisr   r   bytecode_transformationr   r   r   r   r   
eval_framer   r   r    r!   r"   r#   r$   r%   r&   r'   r  r(   r)   r   r*   output_graphr+   replay_recordr,   symbolic_convertr-   r-  r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   	getLoggerr\   r   _logginggetArtifactLoggerr  r   rB   r   r   r   r   r   r}   rd   r   #suppress_torch_distributed_warningsr   r   r   r   r   Counterr   r   __annotations__r   r   r,  r   objectr   r   rG  rS  rI   rI   rI   rJ   <module>   s   
  $	P

"

?
	
 :


	

 [1