U
    9%e_                     @   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 Zkd4d!d"Zld#d$ Zmd%d& Znd aoe p Zqejper esd'< d5e'etetd)d*d+Zud6ejveewexf eewexf eewexf e'etete@ereejy ee> d,d-d.Zze'e@d/d0d1Z{d2d3 Z|dS )7    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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 rH   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py__init__L   s    zTracker.__init__c                    sD   t |  jkr@t| fdd}j| j  d S )Nc                    s   j  S rB   )rE   remove)_idxrG   rH   rI   <lambda>S       zTracker.add.<locals>.<lambda>)idrE   weakrefrefrC   appendadd)rG   Z
strong_objobjrH   rM   rI   rU   P   s
    
zTracker.addc                 C   s   t || jkS rB   )rQ   rE   )rG   itemrH   rH   rI   __contains__W   s    zTracker.__contains__c                 C   s   | j   | j  d S rB   )rC   clearrE   rF   rH   rH   rI   rY   Z   s    
zTracker.clearN)__name__
__module____qualname__rJ   rU   rX   rY   rH   rH   rH   rI   rA   K   s   rA   c                  O   s   t | |}t|j |S rB   )original_forward_from_srcr   __code__)argskwargsresultrH   rH   rI   fx_forward_from_src_skip_resulth   s    

rb   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 | |W S |  t j| t| t j| t j rt j| |t jj_	X d S rB   )torchis_grad_enabledrandomgetstateZget_rng_statecudaZis_availableZfxZgraph_moduler   rb   r=   close_CZ_set_grad_enabledsetstateZset_rng_state)r_   r`   Zprior_grad_modeZpy_rng_stateZtorch_rng_stateZcuda_rng_stateZprior_fwd_from_srccleanupfnrH   rI   _fnz   s"    






z!wrap_convert_context.<locals>._fn)	functoolswraps_torchdynamo_orig_callable)rm   rn   rH   rl   rI   wrap_convert_contextq   s    	rr   c                    s   | j tkrdS | j jD ]"}|| jkrt| j| r dS qt  fdd | j D ]} |rX dS qXt	d| j j
| j j| j j dS )z+Check if the frame has torch.* related bitsTc                    s  t  }|kr| S d|< t tjtjjfrDd|< | S t ttfrtt	fdd D |< | S t t
rt  }t	fdd|D |< | S t ttttdtfrd|< | S t rt	 fdd jD |< | S dS dS )z)Recursively check if the obj has a tensorFTc                 3   s   | ]} |V  qd S rB   rH   .0v
has_tensorrH   rI   	<genexpr>   s     z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>c                 3   s   | ]} |V  qd S rB   rH   rs   rv   rH   rI   rx      s     Nc                 3   s   | ]} t |V  qd S rB   )getattrrs   )rw   rV   rH   rI   rx      s     )rQ   
isinstancerc   ZTensornnModuler9   listtupleanydictvaluesstrintfloattypeboolr8   _fields)rV   obj_idr   rw   rE   )rV   rI   rw      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   valuerH   r   rI   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   rH   rH   rI   exception_handler   s    

r   c                 C   s   | dkS )Nr   rH   )
cache_sizerH   rH   rI   is_recompilation   s    r   c                 C   s   d}| r|d7 }| j } q|S )Nr   r   )next)cache_entryZrunning_cache_sizerH   rH   rI   compute_cache_size   s
    r   FRAME_COMPILE_COUNTERT)compiler_fn	one_graphr   c                    sF   t   tjtd fdd} |_fdd}||_t|S )z&Fully convert a frame into an FX graph)r   hooksc                    s  t   | j}t|}t|rttjs.tj	rt
 rhd|j d|j d|j dtt| d  f}n d|j d|j d|j df}ttjrtj|dd tj	rt|t| |tkrd S tjd	rtjd	|jkrd S |jd
kr|jdrd S |jdkrd S |jdkrB|jtjtjjrBd S |jdkr^|jdkr^d S |jdkr|jdkrt | j!sd S t"|rt#d |tj$kr
dd }dd }tj%r|tkst&dt'(dtj$||||t) nt'(dtj$||t) t#d t*| sd S t+ a,t- a.tj/0 a1d|krLt2|d< t2d7 a2|d }	t3|	 }
t3|	  d7  < t4|	|
}t5dd|j|j|j|d t6| j| j7| j8| 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_infoZTORCHDYNAMO_DEBUG_FUNCTIONz	<genexpr>)ztransformers/file_utils.pyztransformers/utils/generic.py__setattr__rJ   z<module>z<string>z<lambda>	generatorc                 S   s   d| j  d| j d| j dS )N'z' (r   ))r   r   r   r   rH   rH   rI   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   rH   rH   rI   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 reachedZ_idr   Zdynamoz_convert_frame_assert._compile)r   r   r   r   )frame_state
compile_id)9r6   r   r   r   recompiles_logisEnabledForloggingDEBUGr   Zerror_on_recompiler7   r   r   r   r   r5   r   r   ZRecompileErrorinput_codesrU   output_codesosenvirongetendswith
startswithpathdirnamerc   Zoptim__file__r   
f_builtinsr   r&   cache_size_limitZreport_guard_failuresAssertionErrorr   warningr>   r   rd   initial_grad_stateZ$are_deterministic_algorithms_enabled&initial_deterministic_algorithms_stateri   Z_is_torch_function_enabledinitial_torch_function_stateFRAME_COUNTERr   r
   r   _compiler   r   )r   r   r   r   r   r   messager   r   Zframe_idZframe_compile_idr   r   r   export_constraintsr   rH   rI   _convert_frame_assert   s    




	



z3convert_frame_assert.<locals>._convert_frame_assertc                    s   t |  S rB   )convert_frame_assertbackend)r   r   r   rH   rI   _clone_with_backend  s    z1convert_frame_assert.<locals>._clone_with_backend)r<   types	FrameTyper*   rq   r   rr   )r   r   r   r   r   r   rH   r   rI   r      s      r   )r   globalslocalsbuiltinsr   r   r   r   r   r   returnc                    s*  ddl mm} d 
t d } 	
fdd}tddtjttt	t
t tttf gtf tt d
fdd	}tt| zz$|	||}|W W W  5 Q R  S  t"t#t$t%t&t'|fk
r } zt|}t(||
d  W 5 d }~X Y nR t)k
rT } z2t|}t(||
d t*t|+|j,d W 5 d }~X Y nX W 5 d
dlm} t|}|d krֈ
d k	r|tk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!| X W 5 Q R X d S )Nr   )translation_validation_enabledValidationExceptionc                    s  t |  |	d}z$t|jj |  W 5 Q R X W nd tjtjfk
rb    Y nH tk
r    r|jj	}|jj
dd |D dd |D   Y nX |j

d k	st
jst
j| d d < |
j tjrt|  t|  tt| | d d < d S )N)r   c                 S   s   g | ]
}|j qS rH   )Zfakert   arH   rH   rI   
<listcomp>  s     z/_compile.<locals>.transform.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rH   )sourcer   rH   rH   rI   r     s     )r-   r   outputZtracing_contextrunr   RestartAnalysis	SkipFrame	ExceptionZtracked_fakesZ	shape_envZproduce_guardsr   Zoutput_instructionsupdatecode_optionsr   Zdead_code_eliminationr   r   r   r   )Zinstructionsr   ZtracerZfakesr   r   r   r   r   r   r   r   Zmutated_closure_cell_contentsr   r   r   rH   rI   	transform  sH    
z_compile.<locals>.transformZentire_frame_compile)Z
phase_name)r   r   r   r   r   c           
         s  t  D ]}zt| |}| t|< W  qW q tjk
rp } z(tdtt	|j
 |dkr`td W 5 d }~X Y q tjk
r } z2td|| j| j| j |rtd W Y  d S d }~X Y qX qt| dd }|d| j| j| j|  |d	| j| j| j|  d k	st jr&  r&d S  jd k	s6t jtj|< t |rR|jnd }t||j}	  s|jd k	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 rB   )bytecode_logr   r   r   r   r2   )prefixnamefilenameline_nor   rH   rH   rI   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   r   Zis_empty_graphguardsZcleanupsr.   instancer(   Zguard_fail_fnr)   check_fnZguard_export_fnZlocal_scoperY   )
r   r   r   r   attemptZout_coder   r   r   guarded_code)r   rH   rI   compile_inner  sj    





z_compile.<locals>.compile_innerr   )
curr_frameZbackend_compiler   )-Ztorch.fx.experimental.validatorr   r   rD   r1   r   CodeTyper   r*   r   r   r   r   r   r   r   r)   r   r	   utilsr   r3   lenr   Zcount_callsgraphZnodesZplaceholdersr   r/   r   r   r   r   r'   r%   r"   r   r   r   r   r   r$   with_tracebackr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zfail_reasonr   r   r   Z	frame_keyZguard_countZgraph_op_countZgraph_node_countZgraph_input_countZentire_frame_compile_timeZbackend_compile_timeZmetricsr   r   rH   r   rI   r     s    "*U	

  r   )r   r   c                    s<   t | ddtjttd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k
r } z\t|t}tjsd|sd t|dd }| j}t|||| }	|rt	j
|	dd nt	j|	dd W 5 d }~X Y nX d S )Nframestotalr   okr   T)exc_info)r0   r   rz   r'   r   Zsuppress_errorsry   r   r#   r   r   r   )
r   r   r   r   ra   r   Z	soft_failr   r   	error_msg)inner_convertrH   rI   _convert_frame|  s     

 z%convert_frame.<locals>._convert_framec                    s
   t |  S rB   )convert_framer   )r   rH   rI   rO     rP   zconvert_frame.<locals>.<lambda>)r   r   r   r   r*   rq   r   )r   r   r  rH   )r   r  rI   r  x  s      'r  c                 C   s   ddl m} tj}dt_t| d}t|}W 5 Q R X tt	|j
 t
  |_
zFz,t|j|j
|j|j|ddd t dd d W n tk
r   Y nX W 5 |t_X d S )Nr   )eagerFrbr   )r   r   r   r   r   r   r   )Zbackends.debuggingr  r   Zreplay_record_enabledopenr,   loadr   r   chainr   itemsr   r   r   r   r*   r   )r   r  Zoriginal_replay_valin_filerecordrH   rH   rI   replay  s.    

r  )NF)TFN)NNN)}collectionsro   r   r   r   re   r   typingrR   r   r   r   r   r   r   rc   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   r]   Ztorch.utils._tracebackr    r   r   Zallowed_functionsr   Zbackends.registryr   Zbytecode_analysisr   r   Zbytecode_transformationr   r   r   r   r   Z
eval_framer   r   r    r!   r"   r#   r$   r%   r&   r'   r   r(   r)   r   r*   Zoutput_graphr+   Zreplay_recordr,   Zsymbolic_convertr-   r   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   	getLoggerrZ   r   Z_loggingZgetArtifactLoggerr   r   rA   r   r   r   r   r   rp   rb   rr   Z#suppress_torch_distributed_warningsr   r   r   r   r   Counterr   r   __annotations__r   r   r   r   objectr   r   r  r  rH   rH   rH   rI   <module>   s     $	P

"
?

	    :   


 [1