a
    d/t                     @   s8  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	 d dl
mZmZmZmZmZmZmZmZ d dlZd dlmZ d dlmZmZmZmZmZ d dlmZ ddlmZmZ m!Z! dd	l"m#Z#m$Z$ dd
l%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZE ddlFmGZGmHZHmIZImJZJ ddlKmLZL ddlMmNZNmOZOmPZP eQeRZSG dd deZTeUddd ZVe	G dd dZWdd ZXG dd dejYjZZ[G d d! d!Z\G d"d# d#ej]eeT Z^dS )$    N)	dataclass)AnyDictList
NamedTupleOptionalOrderedDictSetUnion)fx)CheckpointableGuardGuardsCheckpointStatetracingTracingContext)ShapeEnv   )configlogging	variables)
CompiledFn
CompilerFn)create_instructionInstruction	unique_id)	PyCodegen)BackendCompilerFailedunimplemented)GuardBuilder)is_dynamic_nn_module)SideEffects)ConstantSourceis_constant_sourceLocalInputSourceLocalSourceShapeEnvSource)	 assert_no_fake_params_or_bufferscheckpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedformat_graph_tabularsame)VariableTracker)GraphArgTrackedFakeVariableBuilderwrap_fx_proxy)NNModuleVariable)SymNodeVariableTensorVariableUnspecializedPythonVariablec                   @   s~   e Zd ZU ee ed< ee ed< eed< ee	e
ejjf  ed< eed< eed< ddd e
ee
 d	d
dZedd ZdS )OutputGraphState	graphargstracked_fakesguard_state
nn_modulesside_effects	timestamp )prefix)otherr@   returnc                C   s   | j D ]}|dkr4| j|j}|d ur|  S qn(|dkr\| j|j}|d ur|  S qt| |}t||}||kr| | d| d|   S qd S )Nr;   r=   z mismatch: z != )_fieldsr;   diffr=   getattr)selfrA   r@   krsvov rK   c/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/_dynamo/output_graph.pyrD   E   s     


zOutputGraphState.diffc                 C   s   | j jS N)r;   dynamo_guardsrF   rK   rK   rL   guardsY   s    zOutputGraphState.guardsN)__name__
__module____qualname__r   r0   __annotations__r1   r   r   r   strtorchnnModuler    intrD   propertyrP   rK   rK   rK   rL   r8   =   s   
r8   c                   C   s
   t tS rM   )torchdynamo_loggingZget_step_loggerlogrK   rK   rK   rL   _step_logger^   s    r]   c                   @   s(   e Zd ZU dZeed< eej ed< dS )GraphCompileReasonzOStores why a given output graph was compiled; i.e. what caused the graph break.reasonZ
user_stackN)	rQ   rR   rS   __doc__rU   rT   r   	tracebackFrameSummaryrK   rK   rK   rL   r^   c   s   
r^   c                    s    fdd}|S )Nc                      s   dd  D S )Nc                 S   s    g | ]\}}}||i |qS rK   rK   ).0fnargskwargsrK   rK   rL   
<listcomp>m       zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>rK   rK   random_callsrK   rL   _gen_rand_valuesl   s    z1_get_gen_rand_values_fn.<locals>._gen_rand_valuesrK   )rj   rk   rK   ri   rL   _get_gen_rand_values_fnk   s    rl   c                       s:   e Zd ZdZeeejjf d fddZ	dd Z
  ZS )FakeRootModulez'Trick the constructor of fx.GraphModule)r<   c                    s,   t    | D ]\}}t| || qd S rM   )super__init__itemssetattr)rF   r<   rG   v	__class__rK   rL   ro   u   s    
zFakeRootModule.__init__c                 C   s   dS )NzFakeRootModule(...)rK   rO   rK   rK   rL   __repr__z   s    zFakeRootModule.__repr__)rQ   rR   rS   r`   r   rU   rV   rW   rX   ro   ru   __classcell__rK   rK   rs   rL   rm   r   s   rm   c                   @   s@   e Zd ZedddZedd Zejj	e
ej dddZd	S )
WrapperBackend)backendc                 C   s   || _ || _d S rM   )rx   original_example_inputs)rF   rx   ry   rK   rK   rL   ro      s    zWrapperBackend.__init__c                 C   s
   t | jS rM   )r)   ry   rO   rK   rK   rL   example_inputs   s    zWrapperBackend.example_inputs)gmrz   c                 C   s   t || _|| _t| j}| || j| _| jd u sD| j| jju rL| jjS t	j
sX| jS zzX| jj| j }| j| j }t||r| jW W |   S td|  | jjW W |   S  ty   td  Y n0 W |   n
|   0 d S )Nzincorrect results of backend zerror in verify_correctness)r'   restorer{   copydeepcopyrx   ry   	candidateforwardr   verify_correctnessrz   r.   RuntimeError	Exceptionr\   	exception)rF   r{   rz   Zcopy_gmcorrectresultrK   rK   rL   __call__   s.    

	


zWrapperBackend.__call__N)rQ   rR   rS   r   ro   rZ   rz   rV   r   GraphModuler   Tensorr   rK   rK   rK   rL   rw   ~   s   
rw   c                       s  e Zd ZdZeeef eeef ed fddZe	dd Z
e	dd Ze	d	d
 Ze	ee dddZdd Zdd Ze	dd ZedddZedddZedddZdd Zdd ZdId!d"ZdJd$d%Zd&d' Zeejj ej!ef d(d)d*Z"dKe#e$ d,d-d.Z%d/d0 Z&e'd1d2e(j)e*d3d4d5Z+e,ej! dd6d7Z-e,ej! dd8d9Z.d dd:d;Z/e,e0 d d<d=d>Z1d dd?d@Z2d ddAdBZ3dL fdCdD	Z4 fdEdFZ5dGdH Z6  Z7S )MOutputGraphze
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.
    )	f_globalscode_optionscompiler_fnc                    s   t    tj | _g | _tjjt	j
r.t nd d}t|| _t	j
r\| jt tj g | _| j| _t | _t | _t|| _g | _d| _i | _|| _|| _|| _ ddl!m"} g | _#g | _$d| _%d | _&d| _'i | _(i | _)t*+ | _,d S )N)	shape_envr   )InstructionTranslatorBaseFrK   )-rn   ro   rV   r   ZGraphgraphr9   Z_subclassesZFakeTensorModer   Zdynamic_shapesr   r   tracing_contextrP   addr%   
make_guardr   Z	SHAPE_ENVr:   Zorig_graphargsdictr<   r    r=   r   output_instructionsr>   real_value_cacher   root_globalsroot_txZtorch._dynamo.symbolic_convertr   _current_txcleanupsshould_exitrandom_values_varinitial_random_stateZunspec_variable_map
pos_to_argcollectionsr   name_to_input)rF   r   r   r   r   	fake_moder   rs   rK   rL   ro      s<    


zOutputGraph.__init__c                 C   s   | S rM   rK   rO   rK   rK   rL   output   s    zOutputGraph.outputc                 C   s   | j jS rM   )r   r   rO   rK   rK   rL   r      s    zOutputGraph.fake_modec                 C   s
   | j jjS rM   )r   r   r   rO   rK   rK   rL   r      s    zOutputGraph.shape_env)rB   c                 C   s
   | j jjS rM   )r   guards_contextrN   rO   rK   rK   rL   rP      s    zOutputGraph.guardsc                 C   s   | j | d S rM   )r   append)rF   txrK   rK   rL   push_tx   s    zOutputGraph.push_txc                 C   s
   | j  S rM   )r   poprO   rK   rK   rL   pop_tx   s    zOutputGraph.pop_txc                 C   s   | j s| jS | j d S )N)r   r   rO   rK   rK   rL   
current_tx  s    zOutputGraph.current_txc                 C   sX   | j dusJ | jj }tt| jt| j|t| j | j	
 | j}|  jd7  _|S )z>Create a checkpoint of the current state by copying everythingNr   )r<   r   r   copy_graphstater8   listr9   r:   r   r=   cloner>   )rF   Zguards_graph_statestaterK   rK   rL   r     s    zOutputGraph.copy_graphstate)r   c                 C   s   |\| _ | _}| _| _| _| jj| d}tt	| j
jD ]F}|jd | jkr<d|jv rb|jd= | | | j|d |d7 }q<td| d dS )z6Restore a checkpoint created by self.copy_graphstate()r   creation_timestampexample_valueNr   zrestore_graphstate: removed z nodes)r9   r:   r<   r=   r>   r   r   restore_graphstatereversedr   r   nodesmetaremove_noder   r   r\   debug)rF   r   Zguards_stateZremoved_nodesnoderK   rK   rL   r     s$    	


zOutputGraph.restore_graphstate)argc                 C   s4   t | j}| j| t|jtr0|| j|jj< d S rM   )lenr9   r   
isinstancesourcer#   r   pos)rF   r   curr_posrK   rK   rL   add_grapharg.  s    
zOutputGraph.add_graphargc                 C   s
   t | jS rM   )r*   r   rO   rK   rK   rL   r*   4  s    zOutputGraph.count_callsc                 C   s@   |sJ | j }|dD ]"}t|tr0|| }qt||}q|S )N.)r<   splitr   r   rE   )rF   keysobjrG   rK   rK   rL   get_submodule7  s    

zOutputGraph.get_submoduleNc                 C   s   || j v r>t D ]*}| d| | j vr| d| } q>q| j rftt| j }| j| j | }n| jd }|4 | jd|di |d}|j	| j |< |W  d    S 1 s0    Y  d S )N_placeholderrK   )	type_expr)
r   	itertoolscountnextr   r   Zinserting_afterZinserting_beforecreate_proxyr   )rF   namer   iZ	prev_namectxproxyrK   rK   rL   create_graph_inputA  s    
zOutputGraph.create_graph_inputtmpc                 C   sV   t | jd }t D ]:}d| d| }||vr| jd |f | jd< |  S qd S )Nco_varnamesZ___r   )setr   r   r   )rF   r   existingr   varrK   rK   rL   new_varS  s    zOutputGraph.new_varc                 C   s,   || j d vr(t| j d |f | j d< dS )z/Ensure self.code_options.co_names contains nameco_namesN)r   tuple)rF   r   rK   rK   rL   update_co_names]  s    zOutputGraph.update_co_names)targetc                    s  t rtjfi  S t  t dg  d< d v sBJ  d }ttjrt	|st d 
|tj  fdd}nxttjjrttjjsJ  d 
|tj  fdd}n2ttjtjfr fdd}nfdd}jd usJ j D ] \}}|u r||  S qdtt|}td	d
|}tdd|}|rz|d  sd| }|}	t D ]6}
|jvrj|< ||  S |	 d|
 }qtdd S )NrP   r   c                    s&   t jd| t i fdi S )Nget_attrr   )r3   r   r   r   Z
module_keyoptionsrF   r   rK   rL   	wrap_nameu  s    z6OutputGraph.register_attr_or_module.<locals>.wrap_namec                    s   t t| fi  S rM   )r4   typer   )r   r   rK   rL   r     s    c                    s&   t jd| t i fdi S )Nr   Zsym_num)r5   creater   r   r   r   rK   rL   r     s    c                    s*    j |   j| < t t| dS )N)Zsource_name)r   r   r   r2   r!   r   )rF   r   rK   rL   r     s
    
r   z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   subZunreachable)r   r   ZUnspecializedNNModuleVariabler   r   getr   rV   r   r"   r   r   r   ZTENSOR_MATCHrW   rX   Z	NN_MODULEZSymIntZSymFloatr<   rp   joinmaprU   rer   isalphar   r   AssertionError)rF   r   namesr   r   r   rG   rr   r   baser   rK   r   rL   register_attr_or_moduled  sB    

z#OutputGraph.register_attr_or_moduleF)r_   c                    sv  ddl m} || _|| _td|  tdd  jD sDtd t	 jD ]}|
  qN   t j}| jdus~J t| j}g }t }	|rt |	|d <  j D ]D\}
}t|jtr|j |
krq||	vrt |	|< |	| |
 q|	 D ]*}||	|  ||gt|	|   qt jd	krg }| d
| _td}|t  j}| !|| t" |}||j#ddd|$d|% j&j't(ddg ||)| |t(dd	|* j&jg | +| |r^tdd |D r^tdd |D r^tt,|t|kr^| j-. r^| +| / tt	||t(dt|g  n| d}t" ||}| j-0| |1| | j-2| t" ||dd |j3 D d}| j-0| |1| | j-2| g }t4| j5d	kst|j6d	krD|| / |7 | t|j6d	kr6||*| n|t(d | +||8   | + fddt	|D  dS )zw
        Generate a subgraph to continue execution on user code.
        Automatically restore live variables.
        r   disablezCOMPILING GRAPH due to c                 s   s   | ]}|  V  qd S rM   )Zcan_restore)rc   blockrK   rK   rL   	<genexpr>  rh   z/OutputGraph.compile_subgraph.<locals>.<genexpr>z&compile_subgraph with block_depth != 0Nr   r   Zrandom_valuesZ__gen_rand_valuesrandomT)r   setstateCALL_FUNCTIONc                 s   s   | ]}t |t V  qd S rM   )r   r7   )rc   rr   rK   rK   rL   r     s   c                 s   s   | ]}t |tV  qd S rM   )r   r6   rc   xrK   rK   rL   r     rh   UNPACK_SEQUENCEZ	graph_outc                 S   s   i | ]\}}|d kr|dqS )r   NrK   )rc   valr   rK   rK   rL   
<dictcomp>  rh   z0OutputGraph.compile_subgraph.<locals>.<dictcomp>)ZtempvarsPOP_TOPc                    s   g | ]}t  |qS rK   )r   create_store)rc   r   r   rK   rL   rg   (  rh   z0OutputGraph.compile_subgraph.<locals>.<listcomp>)9
eval_framer   partial_convertcompile_subgraph_reasonr\   r   allZblock_stackr   r   exitZprune_dead_localsr   stackr<   rm   r   r   Zsymbolic_localsrp   r   r   r$   r   r   r   extendr   rj   r   r   r   rl   install_globalr   Zcreate_load_globalZcreate_load_attrZcreate_load_constr   r   r   Zload_function_namer   add_output_instructionsr   r=   Zis_emptycompile_and_call_fx_graphZcodegen_save_tempvarsZforeachZcodegen_update_mutatedusesr*   r   Zgraph_outputsZgraph_output_varsget_instructions)rF   r   r   r_   r   r   Zstack_valuesrootZrestore_varsZval_to_namesrG   rr   Zrandom_calls_instructionsZrand_fn_nameZrand_fncodegenZgraph_output_varZpass1Zpass2r   rK   r   rL   compile_subgraph  s    







 zOutputGraph.compile_subgraphc              
   C   s  ddl m} t|tsJ t|ts(J |D ]}| j|j q,| dd| t	dd |D fi  | 
  t| j}td d  |7  < td d  |d 7  < | jjD ]}d	|jv r|jd	= q| j  t|| j}|  | j|_td
}	t| t| j | |}
W d   n1 s(0    Y  ||
}
td d  d7  < | |	|
 z\tjtjkrtj rtj!r|" nt#|j}t$$tjd|	 d|j%j&j' d| d W n t(y   t$)d Y n0 t*|}|+|	 |, S )zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r   r   c                 s   s   | ]}|  V  qd S rM   )Zas_proxyr   rK   rK   rL   r   8  rh   z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>statsZcalls_capturedZfusions_possibler   Z__compiled_fnNZunique_graphszTRACED GRAPH
  
zUnable to print graph: `format_graph_tabular` relies on the library `tabulate`, which could not be found on this machine. Run `pip install tabulate` to install the library.)-r   r   r   r   rm   rP   updatecreate_nodeZ
create_argr   remove_unused_graphargsr*   r   r+   r   r   r   clearr   r   Z	recompiler   r   r&   r   r   call_user_compilerr  r   Z	log_levelr   INFOZoutput_codeZoutput_graph_codeZprint_readabler-   r\   r   __code__co_filenameImportErrorwarningr   Zmake_call_generated_coder  )rF   r   rvr  r   r   Zncallsr   r{   r   compiled_fnZ	graph_strZcgrK   rK   rL   r  +  sV    



*

z%OutputGraph.compile_and_call_fx_graphZbackend_compile)Z
phase_name)r{   rB   c           	   
   C   s6  d}|j jD ]}|jdv r|d7 }qtjj| zt| jdrH| jj	nd}t
 tjd|  | j}tjrzt||  }tjd uotjdk}tjjjs|r|||  }n$tjr|||  }n|||  }t
 tjd|  t|sJ d	W n: ty0 } z |j}t| j||W Y d }~n
d }~0 0 |S )
Nr   )call_functioncall_methodZcall_moduler   rQ   r?   zcalling compiler function    zdone compiler function z#compiler_fn did not return callable)r   r   oprV   Z_dynamoutilsZincrement_op_counthasattrr   rQ   r]   r   r  r   r   rw   rz   Zrepro_afterZrepro_levelZdebug_utilsZMINIFIER_SPAWNEDZ)DO_NOT_USE_legacy_non_fake_example_inputsfake_example_inputscallabler   r   r   )	rF   r{   Ztotr   r   r   Zis_top_level_minifyingr  erK   rK   rL   r  i  s6    



$zOutputGraph.call_user_compilerc                 C   s>   g }| j D ].}| }|d ur*|| q
||  q
|S rM   )r9   Zget_fake_examplesr   get_examples)rF   r   r   ZexamplerK   rK   rL   r    s    
zOutputGraph.fake_example_inputsc                 C   s"   g }| j D ]}||  q
|S rM   )r9   r   r!  )rF   r   r   rK   rK   rL   rz     s    
zOutputGraph.example_inputsc                 C   s@  t t| jjD ]L}tt|jdkr|jdkr<| | q|jdkr|jt	j
u r| | qg }| jD ]}||gt|  d|_qht| jj|D ]*\}}|jdksJ | jt|j7  _qtt| jj|D ]T\}}|jdkrtd|j   d|jv r|jd= | | | j|d  qdd | jD | _d S )	Nr   r   r  r   zREMOVE UNUSED GRAPHARG r   c                 S   s   g | ]}|j d kr|qS )r   )r  )rc   r   rK   rK   rL   rg     rh   z7OutputGraph.remove_unused_graphargs.<locals>.<listcomp>)r   r   r   r   r   Zusersr  r   r   operatorgetitemr9   r   r  zipr\   r   r   r   r   r   r   )rF   r   Zexpanded_graphargsr   rK   rK   rL   r    s*    



z#OutputGraph.remove_unused_graphargs)r@   rB   c                 C   s   | j | d| _dS )zt
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        TN)r   r   r   )rF   r@   rK   rK   rL   r    s    z#OutputGraph.add_output_instructionsc                 C   s   | j t| j|| d S rM   )r   r   r(   r   r   )rF   r   valuerK   rK   rL   r    s    zOutputGraph.install_globalc                 C   s`   d | _ d | _| jD ]}|  q| jjD ]}d|jv r(|jd= q(| j  | j	  g | j
_d S )Nr   )r   r<   r9   Zeraser   r   r   r   r  r   r=   	keepalive)rF   Z	graph_argr   rK   rK   rL   cleanup  s    





zOutputGraph.cleanupc              	      s   t  |||||||}| j}	|	j}
|
r8|
 |jjd< |dv rL||jjd< g }|	rp||	  t	|	dd }	qPt
j| }d||j_|S )Nnn_module_stack>   r  r  Z	source_fnparentz | )rn   r   r   r(  r}   r   r   r   frame_summaryrE   ra   StackSummary	from_listformatr   Zstack_trace)rF   kindr   re   rf   r   r   Zproxy_factory_fnr  r   r(  Zframe_summariesZmsgsrs   rK   rL   r     s     
zOutputGraph.create_proxyc                    s"   t  j|i |}| j|jd< |S )Nr   )rn   r  r>   r   )rF   re   rf   r   rs   rK   rL   r    s    zOutputGraph.create_nodec                 C   s    | j | | j|jd  d S rM   )r   Z
erase_noder   r   r   )rF   r   rK   rK   rL   r     s    zOutputGraph.remove_node)N)r   )FN)NNN)8rQ   rR   rS   r`   r   rU   r   r   ro   rZ   r   r   r   r	   r   rP   r   r   r   r8   r   r   r0   r   r*   r   r   r   r   r
   rV   rW   rX   r   r   r   r^   r  r  r,   r   r   r   r  r   r  rz   r  r   r  r  r'  r   r  r   rv   rK   rK   rs   rL   r      s\   

@







	S u><   #r   )_r   r}   	functoolsr   r   r"  r   ra   Zdataclassesr   typingr   r   r   r   r   r   r	   r
   Ztorch.nnrV   r   Ztorch._guardsr   r   r   r   r   Z%torch.fx.experimental.symbolic_shapesr   r?   r   r[   r   Zbackends.registryr   r   Zbytecode_transformationr   r   r   r  r   excr   r   rP   r   Zmutation_guardr   r=   r    r   r!   r"   r#   r$   r%   r  r&   r'   r(   r)   r*   r+   r,   r-   r.   Zvariables.baser/   Zvariables.builderr0   r1   r2   r3   Zvariables.nn_moduler4   Zvariables.tensorr5   r6   r7   	getLoggerrQ   r\   r8   	lru_cacher]   r^   rl   rW   rX   rm   rw   ZTracerr   rK   rK   rK   rL   <module>   sJ   (,
!
)