o
    "i)                     @   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/dNdej0fd d!Z1d"d# Z2G d$d% d%Z3G d&d' d'Z4G d(d) d)Z5d*d+ Z6d,d- Z7d.d/ Z8dOd0d1Z9d2ej:fd3d4Z;dPd7d8Z<ej=d9d fd:ee> d;ee> d<ej?d=ee@ejAf d>e>f
d?d@ZBdAdB ZCddCdDdEZDdFdG ZEdHdI ZFdJdK ZGdLdM ZHdS )Q    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 u rd S |    | jS N)detachcloneZrequires_grad_requires_grad)x r   d/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/_dynamo/testing.pyclone_me!   s   r   c                    s   t   fdd}|S )Nc                     s"   dt jv r
td | i |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      t | tjsJ | jjS r   )
isinstancer	   OptimizedModule	_orig_modnamed_parametersmodr   r   r   %named_parameters_for_optimized_module1      r/   c                 C   r(   r   )r)   r	   r*   r+   named_buffersr-   r   r   r   "named_buffers_for_optimized_module6   r0   r2   c                 C   s   t dd| S )Nz^_orig_mod[.] )resub)namer   r   r   remove_optimized_module_prefix;   s   r7   c                 C   s&  g }| | | | t }t }|  D ]'\}}t| tjr$t|}|}	|j}
|jd u r3t	|}
|
||d < |	||< q| | | | t }| 
 D ]\}}t| tjr]t|}|||< qO| | |D ]'}t|ttfr|D ]}t|tjr| |j qtqit|tjr| |j qi|S )Nz.grad)appenddictr,   r)   r	   r*   r7   gradtorchZ
zeros_liker1   tuplelistTensor)modelZ
predictionZlossexample_inputsresultsZgradsparamsr6   paramZ
param_copyr:   buffersbufferZexampleinpr   r   r   collect_results?   sB   








rG   c                 C   sZ   t | tjr	| jS t | ttfrtdd | D S | d u rdS t | tr&dS tdt	| )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   rH   g   s   
rH   c                 C   s   t | tjr|  |   S t | ttfr"tdd | D t|  S t| j	dv r.t
| jS t| j	dkr:| j S t | trPtdd |  D t|   S tdt| )z/Reduce the output of a model to get scalar lossc                 S      g | ]}t |qS r   reduce_to_scalar_lossrI   r   r   r   
<listcomp>y       z)reduce_to_scalar_loss.<locals>.<listcomp>)ZMaskedLMOutputZSeq2SeqLMOutputZ!CausalLMOutputWithCrossAttentionsZSquashedNormalc                 S   rS   r   rT   )rJ   valuer   r   r   rV      rW   rL   )r)   r;   r>   sumZnumelr=   r<   lenrP   __name__rU   Zlogitsmeanr9   valueskeysrO   rQ   r   r   r   rU   s   s   


rU   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   
re   r3   codec              	   C   sl   t tjt | d!}|t|  dt|  d| d W d    d S 1 s/w   Y  d S )Nwz


)	openr   r_   r`   re   writedisBytecodeinfo)r6   rf   extrafdr   r   r   
debug_dump   s
   ("rp   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   )	rs   Zcompiler_fnZroot_txZexportZexport_constraintsZframe_stateZlocal_scopeZglobal_scopef_code)
r   ru   r   r   r   localsglobalsr   r   Zcheck_fn)frameZ
cache_sizehooks_rt   rf   graphr   r   r   debug_insert_nops   s"   

r|   c                   @   s.   e Zd Zdd ZdejjfddZdd ZdS )	CompileCounterc                 C      d| _ d| _d S Nr   frame_countop_countselfr   r   r   __init__      
zCompileCounter.__init__gmc                 C   s:   |  j d7  _ |jjD ]}d|jv r|  jd7  _q|jS )Nr   call)r   r{   nodesopr   forward)r   r   r@   noder   r   r   __call__   s   
zCompileCounter.__call__c                 C   r~   r   r   r   r   r   r   clear   r   zCompileCounter.clearN)	r[   
__module____qualname__r   r;   r   GraphModuler   r   r   r   r   r   r}      s    r}   c                   @   &   e Zd Zdd Zdejjf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v r|  jd7  _q| j| || j	||S )Nr   )lookup_backendr   )
Zbackends.registryr   r   r{   r   r   r   r   r8   r   )r   r   r@   r   r   r   r   r   r      s   
z"CompileCounterWithBackend.__call__Nr[   r   r   r   r;   r   r   r   r   r   r   r   r      s    r   c                   @   r   )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   r8   )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?r3   )strr4   r5   rf   r   r   r   strip_comment   s   r   c                 C   s   d dd | dD S )Nrh   c                 S   s   g | ]}|  qS r   )rstrip)rJ   liner   r   r   rV      rW   z)remove_trailing_space.<locals>.<listcomp>)r`   splitr   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 ur	|}t }|d u r5t }ztj|}|| td |j  |j	}W n	 t
y4   Y nw dd t|D }dd t|D }	|| }
||	 }t  t||}|| }||	 }|| }||	 }t  | t||
 | t||
 | t|| | t|| | |jd |d ur| |j	| d S d S )Nz
fx.symbolic_trace graph:c                 S      g | ]}t d d qS 
   r;   randnrJ   rz   r   r   r   rV          z!standard_test.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   rV     r   r   )r   Zassume_static_by_defaultr}   r;   r   Zsymbolic_traceprintr{   Zprint_tabularr   	Exceptionranger   r
   
assertTruer   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   sB   

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rtj|||d}n	tj|g||d}t|| |S )Nc                 s   s     | ]\}}|d  | V  qdS )r   Nr   )rJ   shaper   r   r   r   rK   (  s    zrand_strided.<locals>.<genexpr>r   )r   r   )r   r   r   )rY   zipZis_floating_pointr;   r   ZzerosZ
as_strided)r   r   r   r   r   Zneeded_sizerE   r   r   r   rand_strided   s   r   c                    s   t   fdd}|S )Nc               	      s^   t  !}D ]\}}}|t||| q | i |W  d    S 1 s(w   Y  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 ]6}|drNt| |}t|s)q| | }t|g|R  }	||	_ |d urHt||rHt	|	}	t
|||	 q|S )Nc                   @   s   e Zd ZdS )z2make_test_cls_with_patches.<locals>.DummyTestClassN)r[   r   r   r   r   r   r   DummyTestClass@  s    r   Ztest_)r[   r   dir
startswithgetattrcallabler   hasattrr   ZexpectedFailuresetattr)
clsZ
cls_prefixZ	fn_suffixr   r   r   r6   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   
   d| _ | S NT)Z_expected_failure_dynamicr!   r   r   r   expectedFailureDynamic^     r   c                 C   r   r   )Z!_expected_failure_codegen_dynamicr!   r   r   r   expectedFailureCodegenDynamicd  r   r   c                 C   r   r   )Z!_expected_failure_dynamic_wrapperr!   r   r   r   expectedFailureDynamicWrapperj  r   r   )r3   )NN)Tr   )Ir   rk   r%   loggingZos.pathr   r4   r   typesr   typingr   r   Zunittest.mockr   r;   r   Ztorch._dynamo.output_graphr   r3   r   r	   r
   r   Zbytecode_transformationr   r   r   r   Zguardsr   r   utilsr   unsupportedthree	getLoggerr[   logr   r'   r/   r2   r7   rG   rH   rU   re   CodeTyperp   r|   r}   r   r   r   r   r   r   r   r   r   Zfloat32rN   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sz    

(	
$

