U
    ªÊ¹j _  ã                   @   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/bwave/python/venv/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 ¡ r˜t 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
ƒr¬tˆ  ¡ ƒ}t	‡fdd„|D ƒƒˆ|< ˆ| S tˆ ttttdƒtfƒrÔdˆ|< ˆ| 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|ƒr²t tj¡s.tj	r²t
ƒ rhd|j› d|j› d|j› �dtt| d ƒ› �f}n d|j› d|j› d|j› �df}t tj¡r¢tj|dd� tj	r²t |¡‚t |¡ |tkrÈd S tj d	¡rêtj d	¡|jkrêd 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�r†t | j!ƒ�s†d S t"|ƒ�r˜t#dƒ |tj$k�r
dd„ }dd„ }tj%�rì|tk�sÎt&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.z¦torch._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&   Z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|ƒƒ��� zÆz$|ˆˆ	||ƒ}|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�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!|ƒ 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	s¼t‚ˆ
jsÆt‚ˆ
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  qÄW 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¡ |r¨t 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 %séd   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|||| ƒ}	|r˜t	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