U
    ªÊ¹jŸ)  ã                   @   s  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 d dlmZ d dlZd dlmZ d dlmZ ddlmZmZmZmZ ddlmZmZmZmZ dd	lmZmZ dd
l m!Z! ej"Z"dZ#e $e%¡Z&dd„ Z'dd„ Z(dd„ Z)dd„ Z*dd„ Z+dd„ Z,dd„ Z-dd„ Z.dd„ Z/dJej0dœd d!„Z1d"d#„ Z2G d$d%„ d%ƒZ3G d&d'„ d'ƒZ4G d(d)„ d)ƒZ5d*d+„ Z6d,d-„ Z7d.d/„ Z8dKd0d1„Z9ej:d2œd3d4„Z;dLd7d8„Z<ej=d9d fee> ee> ej?ee@ejAf e>d:œd;d<„ZBd=d>„ ZCdd?œd@dA„ZDdBdC„ ZEdDdE„ ZFdFdG„ ZGdHdI„ ZHdS )Mé    N)ÚSequenceÚUnion)Úpatch)Úfx)ÚOutputGraphé   )ÚconfigÚ
eval_frameÚoptimize_assertÚreset)Úcreate_instructionÚdebug_checksÚis_generatorÚtransform_code_object)ÚCheckFunctionManagerÚGuardedCode)Úsameé   c                 C   s    | d krd S |   ¡  ¡  | j¡S ©N)ÚdetachÚcloneZrequires_grad_Úrequires_grad)Úx© r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/_dynamo/testing.pyÚclone_me!   s    r   c                    s   t  ˆ ¡‡ fdd„ƒ}|S )Nc                     s   dt jkrt d¡‚ˆ | |ŽS )NZPYTEST_CURRENT_TESTzdoes not work under pytest)ÚosÚenvironÚunittestZSkipTest)ÚargsÚkwargs©Úfnr   r   Úwrapped(   s    

zskip_if_pytest.<locals>.wrapped©Ú	functoolsÚwraps)r"   r#   r   r!   r   Úskip_if_pytest'   s    r'   c                 C   s   t | tjƒst‚| jjS r   )Ú
isinstancer	   ÚOptimizedModuleÚAssertionErrorÚ	_orig_modÚnamed_parameters©Úmodr   r   r   Ú%named_parameters_for_optimized_module1   s    r/   c                 C   s   t | tjƒst‚| jjS r   )r(   r	   r)   r*   r+   Únamed_buffersr-   r   r   r   Ú"named_buffers_for_optimized_module6   s    r1   c                 C   s   t  dd| ¡S )Nz^_orig_mod[.]Ú )ÚreÚsub)Únamer   r   r   Úremove_optimized_module_prefix;   s    r6   c                 C   s(  g }|  |¡ |  |¡ tƒ }tƒ }|  ¡ D ]N\}}t| tjƒrHt|ƒ}|}	|j}
|jd krft 	|¡}
|
||d < |	||< q,|  |¡ |  |¡ tƒ }|  
¡ D ]$\}}t| tjƒrºt|ƒ}|||< qž|  |¡ |D ]P}t|ttfƒ�r
|D ]}t|tjƒrê|  |j¡ qêqÒt|tjƒrÒ|  |j¡ qÒ|S )Nz.grad)ÚappendÚdictr,   r(   r	   r)   r6   ÚgradÚtorchZ
zeros_liker0   ÚtupleÚlistÚTensor)ÚmodelZ
predictionZlossÚexample_inputsÚresultsZgradsÚparamsr5   ÚparamZ
param_copyr9   ÚbuffersÚbufferÚexampleZinpr   r   r   Úcollect_results?   s<    








rF   c                 C   s^   t | tjƒr| jS t | ttfƒr2tdd„ | D ƒƒS | d kr>dS t | tƒrLdS tdt	| ƒƒ‚d S )Nc                 s   s   | ]}t |ƒV  qd S r   )Úrequires_bwd_pass©Ú.0r   r   r   r   Ú	<genexpr>k   s     z$requires_bwd_pass.<locals>.<genexpr>FúDon't know how to reduce)
r(   r:   r=   r   r<   r;   ÚanyÚintÚNotImplementedErrorÚtype©Úoutr   r   r   rG   g   s    
rG   c                 C   s²   t | tjƒr|  ¡ |  ¡  S t | ttfƒrDtdd„ | D ƒƒt| ƒ S t| ƒj	dkr\t
| jƒS t| ƒj	dkrt| j ¡ S t | tƒr tdd„ |  ¡ D ƒƒt|  ¡ ƒ S tdt| ƒƒ‚dS )z/Reduce the output of a model to get scalar lossc                 S   s   g | ]}t |ƒ‘qS r   ©Úreduce_to_scalar_lossrH   r   r   r   Ú
<listcomp>y   s     z)reduce_to_scalar_loss.<locals>.<listcomp>)ZMaskedLMOutputZSeq2SeqLMOutputZ!CausalLMOutputWithCrossAttentionsZSquashedNormalc                 S   s   g | ]}t |ƒ‘qS r   rR   )rI   Úvaluer   r   r   rT   ƒ   s     rK   N)r(   r:   r=   ÚsumZnumelr<   r;   ÚlenrO   Ú__name__rS   ZlogitsZmeanr8   ÚvaluesÚkeysrN   rP   r   r   r   rS   s   s    


ÿrS   c                  C   s0   t j t j t¡d¡} t j | ¡s,t  | ¡ | S )Nz../debug)r   ÚpathÚjoinÚdirnameÚ__file__ÚexistsÚmkdir)r[   r   r   r   Ú	debug_dir‰   s    
ra   r2   ©Úcodec              	   C   sV   t tj tƒ | ¡dƒ�6}| t |¡ ¡ › dt |¡ ¡ › d|› d�¡ W 5 Q R X d S )NÚwz

Ú
)	Úopenr   r[   r\   ra   ÚwriteÚdisÚBytecodeÚinfo)r5   rc   ÚextraÚfdr   r   r   Ú
debug_dump�   s    (ÿrm   c                 C   s`   dd„ }t | jƒrdS t| jƒ t| j|ƒ}ti dddddditƒ tƒ | jd�	}t|t|ƒj	ƒS )zused to debug jump updatesc                 S   s$   |   dtdƒ¡ |   dtdƒ¡ d S )Nr   ÚNOP)Úinsertr   )ZinstructionsÚcode_optionsr   r   r   Úinsert_nopsš   s    z&debug_insert_nops.<locals>.insert_nopsNFZ_idr   )	rp   Zcompiler_fnZroot_txZexportZexport_constraintsZframe_stateZlocal_scopeZglobal_scopeÚf_code)
r   rr   r   r   r   ÚlocalsÚglobalsr   r   Zcheck_fn)ÚframeZ
cache_sizeÚhooksÚ_rq   rc   Úgraphr   r   r   Údebug_insert_nops—   s"    

öry   c                   @   s.   e Zd Zdd„ Zejjdœdd„Zdd„ ZdS )	ÚCompileCounterc                 C   s   d| _ d| _d S ©Nr   ©Úframe_countÚop_count©Úselfr   r   r   Ú__init__´   s    zCompileCounter.__init__©Úgmc                 C   s:   |  j d7  _ |jjD ]}d|jkr|  jd7  _q|jS )Nr   Úcall)r}   rx   ÚnodesÚopr~   Úforward)r€   rƒ   r?   Únoder   r   r   Ú__call__¸   s
    
zCompileCounter.__call__c                 C   s   d| _ d| _d S r{   r|   r   r   r   r   Úclear¿   s    zCompileCounter.clearN)	rX   Ú
__module__Ú__qualname__r�   r:   r   ÚGraphModuler‰   rŠ   r   r   r   r   rz   ³   s   rz   c                   @   s&   e Zd Zdd„ Zejjdœdd„ZdS )ÚCompileCounterWithBackendc                 C   s   d| _ d| _|| _g | _d S r{   )r}   r~   ÚbackendÚgraphs)r€   r�   r   r   r   r�   Å   s    z"CompileCounterWithBackend.__init__r‚   c                 C   s\   ddl m} |  jd7  _|jjD ]}d|jkr"|  jd7  _q"| j |¡ || j	ƒ||ƒS )Nr   )Úlookup_backendr„   )
Zbackends.registryr‘   r}   rx   r…   r†   r~   r�   r7   r�   )r€   rƒ   r?   r‘   rˆ   r   r   r   r‰   Ë   s    
z"CompileCounterWithBackend.__call__N©rX   r‹   rŒ   r�   r:   r   r�   r‰   r   r   r   r   rŽ   Ä   s   rŽ   c                   @   s&   e Zd Zdd„ Zejjdœdd„ZdS )ÚEagerAndRecordGraphsc                 C   s
   g | _ d S r   )r�   r   r   r   r   r�   Ù   s    zEagerAndRecordGraphs.__init__r‚   c                 C   s   | j  |¡ |S r   )r�   r7   )r€   rƒ   r?   r   r   r   r‰   Ü   s    zEagerAndRecordGraphs.__call__Nr’   r   r   r   r   r“   Ø   s   r“   c                 C   s   t | ƒ} t dd| ¡S )Nz(?m)^ *#.*\n?r2   )Ústrr3   r4   rb   r   r   r   Ústrip_commentá   s    r•   c                 C   s   d  dd„ |  d¡D ƒ¡S )Nre   c                 S   s   g | ]}|  ¡ ‘qS r   )Úrstrip)rI   Úliner   r   r   rT   ç   s     z)remove_trailing_space.<locals>.<listcomp>)r\   Úsplitrb   r   r   r   Úremove_trailing_spaceæ   s    r™   c                 C   s   t t| ƒƒS r   )r™   r•   )Zgm_strr   r   r   Únormalize_gmê   s    rš   c                 C   sB  t js|d k	r|}tƒ }|d krltƒ }z0tj |¡}||ƒ tdƒ |j ¡  |j	}W n t
k
rj   Y nX dd„ t|ƒD ƒ}dd„ t|ƒD ƒ}	||Ž }
||	Ž }tƒ  t|ƒ|ƒ}||Ž }||	Ž }||Ž }||	Ž }tƒ  |  t||
ƒ¡ |  t||
ƒ¡ |  t||ƒ¡ |  t||ƒ¡ |  |jd¡ |d k	�r>|  |j	|¡ d S )Nz
fx.symbolic_trace graph:c                 S   s   g | ]}t  d d ¡‘qS ©é
   ©r:   Úrandn©rI   rw   r   r   r   rT      s     z!standard_test.<locals>.<listcomp>c                 S   s   g | ]}t  d d ¡‘qS r›   r�   rŸ   r   r   r   rT     s     r   )r   Zassume_static_by_defaultrz   r:   r   Zsymbolic_traceÚprintrx   Zprint_tabularr~   Ú	ExceptionÚranger   r
   Z
assertTruer   ZassertEqualr}   )r€   r"   ÚnargsZexpected_opsZexpected_ops_dynamicÚactualÚexpectedrƒ   Zargs1Zargs2Zcorrect1Zcorrect2Zopt_fnZval1aZval2aZval1bZval2br   r   r   Ústandard_testð   s>    


r¦   r‚   c                 C   s   | j S r   )r‡   )rƒ   r?   r   r   r   Údummy_fx_compile  s    r§   Tçš™™™™™¹?c                 C   s.   |sdS ||kr| d›d�S | d›d|d›�S )NÚERRORz.3fzx SAMEzx p=z.2fr   )ZspeedupZpvalueZ
is_correctZpvalue_thresholdr   r   r   Úformat_speedup  s
    rª   Úcpu)ÚsizeÚstrideÚdtypeÚdeviceÚ
extra_sizec                 C   sX   t dd„ t| |ƒD ƒƒd | }|jr8tj|||d�}ntj|g||d�}t || |¡S )Nc                 s   s   | ]\}}|d  | V  qdS )r   Nr   )rI   Úshaper­   r   r   r   rJ   (  s     zrand_strided.<locals>.<genexpr>r   )r®   r¯   )r¬   r®   r¯   )rV   ÚzipZis_floating_pointr:   rž   ZzerosZ
as_strided)r¬   r­   r®   r¯   r°   Zneeded_sizerD   r   r   r   Úrand_strided   s    ÿþÿr³   c                    s   t  ˆ ¡‡ ‡fdd„ƒ}|S )Nc               
      sN   t  ¡ �<}ˆD ]\}}}| t |||¡¡ qˆ | |ŽW  5 Q R £ S Q R X d S r   )Ú
contextlibÚ	ExitStackÚenter_contextr   Úobject)r   r    ÚstackÚmoduleÚattrÚval©r"   Úpatchesr   r   Ú_fn4  s    
z"_make_fn_with_patches.<locals>._fnr$   )r"   r½   r¾   r   r¼   r   Ú_make_fn_with_patches3  s    r¿   )Ú
xfail_propc          
      G   s    G dd„ d| ƒ}|› | j › �|_ |j |_t| ƒD ]j}| d¡r0t| |ƒ}t|ƒsRq0|› |› �}t|f|žŽ }	||	_ |d k	rŽt||ƒrŽt 	|	¡}	t
|||	ƒ q0|S )Nc                   @   s   e Zd ZdS )z2make_test_cls_with_patches.<locals>.DummyTestClassN)rX   r‹   rŒ   r   r   r   r   ÚDummyTestClass@  s   rÁ   Ztest_)rX   rŒ   ÚdirÚ
startswithÚgetattrÚcallabler¿   Úhasattrr   ZexpectedFailureÚsetattr)
ÚclsZ
cls_prefixZ	fn_suffixrÀ   r½   rÁ   r5   r"   Únew_nameZnew_fnr   r   r   Úmake_test_cls_with_patches?  s    


rÊ   c                 C   s   t jdkr| S t | ¡S )N)r   é   )ÚsysÚversion_infor   Úskipr!   r   r   r   ÚskipIfNotPy311V  s    
rÏ   c                 C   s
   d| _ | S ©NT)Z_expected_failure_dynamicr!   r   r   r   ÚexpectedFailureDynamic^  s    rÑ   c                 C   s
   d| _ | S rÐ   )Z!_expected_failure_codegen_dynamicr!   r   r   r   ÚexpectedFailureCodegenDynamicd  s    rÒ   c                 C   s
   d| _ | S rÐ   )Z!_expected_failure_dynamic_wrapperr!   r   r   r   ÚexpectedFailureDynamicWrapperj  s    rÓ   )r2   )NN)Tr¨   )Ir´   rh   r%   ÚloggingÚos.pathr   r3   rÌ   Útypesr   Útypingr   r   Zunittest.mockr   r:   r   Ztorch._dynamo.output_graphr   r2   r   r	   r
   r   Zbytecode_transformationr   r   r   r   Zguardsr   r   Úutilsr   ÚunsupportedÚthreeÚ	getLoggerrX   Úlogr   r'   r/   r1   r6   rF   rG   rS   ra   ÚCodeTyperm   ry   rz   rŽ   r“   r•   r™   rš   r¦   r�   r§   rª   Zfloat32rM   r®   r”   r¯   r³   r¿   rÊ   rÏ   rÑ   rÒ   rÓ   r   r   r   r   Ú<module>   sp   

(	
$
ûû