
    i24                        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
 d dl mZ d dlmZmZmZ d dlZd dlZd dlmZ d dlmZ d dlmZ d	d
lmZ d	dlmZmZ d	dlmZ erd	dlm Z  d	dl!m"Z"  ejF                  e$      Z%dejL                  dedede'e(ef   fdZ)e G d d             Z* G d dejV                        Z,e G d d             Z-dede.e.edf   e'e(ef   f   dedeej^                  j`                  e1ejd                     gef   de-f
dZ3e G d  d!             Z4e G d" d#             Z5dejl                  jn                  d$e1e4   dedeej^                  j`                  e1ejd                     gef   de5f
d%Z8y)&    N)Callable)AbstractContextManager	ExitStack)	dataclass)AnyOptionalTYPE_CHECKING)GraphRuntimeEnv)_graph_device_type)
SystemInfo   )convert_frame)&BundledAOTAutogradSerializableCallableSerializableCallable)Hooks)GuardManagerWrapper
SourceInfo	signatureargskwargsreturnc                 ^     | j                   |i |}|j                          |j                  S N)bindapply_defaults	arguments)r   r   r   bound_argumentss       S/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/aot_compile.pybind_localsr    "   s1     %innd5f5O""$$$$    c                       e Zd ZU ej                  ed<   ed   ed<   eed<   eed<   e	ed<   e
j                  ed<   eed<   d	ed
<   eed<   eed<    ej                  ej                         Zeed<   ddZy)CompileArtifactsr   r   guard_managerguards_state
backend_idcompiled_fnoriginal_coderuntime_envr   source_infodevice_typebackend_name)default_factorysystem_infoNc                 x    t        j                         }|j                  | j                  | j                         y r   )r   currentcheck_compatibilityr.   r+   )selfcurrent_systems     r   r1   z$CompileArtifacts.check_compatibility8   s,    #++-**4+;+;T=M=MNr!   r   N)__name__
__module____qualname__inspect	Signature__annotations__r   bytesstrr   typesCodeTyper
   dataclassesfieldr   r0   r.   r1    r!   r   r#   r#   *   sp       122O%%>>!  /k//
@R@RSKSOr!   r#   c                   6    e Zd Zededefd       ZdedefdZy)AOTCompilePicklervalr   c                 V    dt         ffd}|j                  J |j                  d   S )Nr   c                       S r   rA   )rD   s   r   _z+AOTCompilePickler._unpickle_cell.<locals>._@   s    Jr!   r   )r   __closure__)clsrD   rG   s    ` r   _unpickle_cellz AOTCompilePickler._unpickle_cell>   s0    	3 	 }}(((}}Qr!   objc                     t        |t         d d      j                  d               r"t        |       j                  |j                  ffS t
        S )Nc                       fdS )Nc                       S r   rA   xs   r   <lambda>zFAOTCompilePickler.reducer_override.<locals>.<lambda>.<locals>.<lambda>H   s    1 r!   rA   rO   s   `r   rQ   z4AOTCompilePickler.reducer_override.<locals>.<lambda>H   s    9 r!   r   )
isinstancetyperH   rJ   cell_contentsNotImplemented)r2   rK   s     r   reducer_overridez"AOTCompilePickler.reducer_overrideG   sF    c4!4a 8 D DQ GHI:,,s/@/@.BBBr!   N)r5   r6   r7   classmethodr   rJ   rV   rA   r!   r   rC   rC   =   s5           C C r!   rC   c            	           e Zd ZU eed<   dZeed<   dZee	e
ef      ed<   dededefd	Zdd
ZdededefdZddZde
ddfdZedd defd       Ze	 ddedee	e
ef      dd fd       ZddZy)AOTCompiledFunction
_artifactsT_guard_check_enabledN_extra_globalsr   r   r   c                 \   i }| j                   j                  }|j                  r|j                  j                  r5t        |j                        t        |j                  j                        k(  sJ t        |j                  j                  |j                        D ci c]  \  }}||j                   }}}|j                  t        | j                   j                  g|i |       | j                   j                  J | j                   j                  j                  |      S c c}}w r   )rZ   r)   closurebytecodeco_freevarslenziprT   updater    r   r$   check)r2   r   r   f_localsenvnamecells          r   guard_checkzAOTCompiledFunction.guard_checkS   s    #%oo));;<<++CKK0@C((E 1  
 #&cll&>&>"LD$ d(((H  	DOO$=$=OOOP,,888,,228<<s   D(c                    ddl m}m} | j                  j	                          | j                  j
                  j                  | j                  j                  | j                  j                  | j                        | _
        | j                  j                  ^ || j                  j                        } ||| j                  j                  | j                  j                        | j                  _        y y )Nr   )load_guard_managerload_guards_state)extra_globals)packagerk   rl   rZ   r1   r)   forward_callabler&   r'   r\   fnr$   r%   r(   __globals__)r2   rk   rl   r%   s       r   __post_init__z!AOTCompiledFunction.__post_init__b   s    B++- //-->>OO&&OO''-- ? 
 ??((0,T__-I-IJL,>--##-DOO) 1r!   c                 P   | j                   j                  J | j                  rq | j                  |i |s_t	        | j                   j
                  g|i |}t        | j                   j                  j                  |            }t        d|        | j                  |i |S )Nz#GuardManager check failed, reason: )
rZ   r$   r[   ri   r    r   r<   check_verboseRuntimeErrorrp   )r2   r   r   re   reasons        r   __call__zAOTCompiledFunction.__call__v   s    ,,888$$-=T-=-=t-Nv-N"4??#<#<NtNvNH66DDXNOF!DVHMNNtww'''r!   c                 .    | j                   j                  S r   )rZ   r*   r2   s    r   r*   zAOTCompiledFunction.source_info~   s    ***r!   pathc                     t        |d      5 }|j                  t        |       j                  |              d d d        y # 1 sw Y   y xY w)Nwb)openwriterS   	serialize)r2   rz   fs      r   save_compiled_functionz*AOTCompiledFunction.save_compiled_function   s>    $ 	0GGDJ((./	0 	0 	0s   *A  A	rp   c                    ddl m} |j                  j                  j	                         }d |d<   t        j                  |d   |j                  |d   j                              |d<   |d   }t        |      j                  t        |      j                  |      f|d<   |j                  |d         |d<   t        j                         }t        |      }|j                  |       |j!                         S )Nr   SerializedCoder$   r)   r_   r'   r(   )torch._dynamo.packager   rZ   __dict__copyr?   replacefrom_code_objectr_   rS   deserialize_compile_artifactsserialize_compile_artifactsioBytesIOrC   dumpgetvalue)rI   rp   r   stater'   bufpicklers          r   r   zAOTCompiledFunction.serialize   s    8&&++-!%o*22- #44U=5I5R5RS 
m M*;;99+F 
m "0!@!@AW!Xojjl#C(U||~r!   data	f_globalsc                    ddl m} t        j                  |      }t	        j
                  |d   |j                  |d   j                              |d<   |d   \  }}t        j                  j                  j                  d      5   ||      |d<   d d d        |j                  |d         |d<   t        d
i |} | ||	      S # 1 sw Y   5xY w)Nr   r   r)   r   r'   T)enable_autograd_for_aotr(   )r\   rA   )r   r   pickleloadsr?   r   to_code_objectr_   torch	_inductorconfigpatchr#   )rI   r   r   r   r   deserializercompiled_fn_state	artifactss           r   deserializezAOTCompiledFunction.deserialize   s     	9T"*22- #2253G3P3PQ 
m +0*>''__##))$)G 	C#/0A#BE- 	C!/!>!>u_?U!Vo$-u-	9Y77	C 	Cs   CCc                     d| _         y )NF)r[   ry   s    r   disable_guard_checkz'AOTCompiledFunction.disable_guard_check   s
    $)!r!   r4   )r   r   r   )r5   r6   r7   r#   r:   r[   boolr\   r   dictr<   objectr   ri   rr   rw   r*   r   rW   r;   r   r   r   rA   r!   r   rY   rY   M   s      !%$%26NHT#v+./6= = = =((c (S (S (+03 04 0 0 U  ( CG88%-d3;.?%@8	8 8$*r!   rY   modelexample_inputs.hooksbackendc                 0   ddl m ddlm} ddlm}m} ddlm} |\  }}	 |       5   |d      5  t        j                  j                  j                  d      5  t        j                  | ||	      }
|
j                  }|j                   J |j"                  s)dd	lm} d
t(        |   dt(        t*           ffd}||_        t        j,                  |       \  }}|
j.                  }|J |j0                  |j2                  _        t7        |j2                  j8                        }|j:                  j<                  |j                   j<                  u sJ  ||j:                        }|j>                  |_        t        j@                  jC                  |      5  t        j                  j                  j                  ddd      5   ||j2                  |jD                        }tG        |t        jH                        rtK        |      }d d d        d d d        tG        tL              s:tO        |d      r|jP                  }n|}tS        dtU        |       dd| dz         |jW                  |jX                  |dd      }|jZ                  J  |t]                     }|j^                  D ]  }|ja                  |        tc        t        jd                  |      |jf                  |jZ                  |j0                  ||jX                  |ji                         ||tk        |dd      
      }tm        ||jn                        }d d d        d d d        d d d        S # 1 sw Y   VxY w# 1 sw Y   [xY w# 1 sw Y   5xY w# 1 sw Y   9xY w# 1 sw Y   S xY w)Nr   )CheckFunctionManagerr   )dynamo_timedget_metrics_context)TracingContextfullgraph_captureT)strict_autograd_cache)GuardFilterEntryguard_entriesr   c                 x    | D cg c])  }|j                   xs |j                  j                  v  + c}S c c}w r   )	is_global
guard_type%UNSUPPORTED_SERIALIZATION_GUARD_TYPES)r   gr   s     r   new_guard_filter_fnz2aot_compile_fullgraph.<locals>.new_guard_filter_fn   sL     +	  KK Z ||3YY Z	 	 	s   .7)bundled_autograd_cache force_non_lazy_backward_loweringcompiler_fnzCompiled function type z (produced zfrom backend z*) does not implement SerializableCallable.)r   savestrict_error)inlined_sourcescompiler_nameunknown)
r   r$   r%   r&   r'   r(   r)   r*   r+   r,   )rZ   r\   )8torch._dynamo.guardsr   r   r   torch._dynamo.utilsr   r   torch._guardsr   r   
_functorchr   r   r   r   graph_capture_outputoutput_graphguard_filter_fntorch._dynamo.typesr   listr   get_traced_fnbackend_inputr&   graph_module_backend_idr   graph	fake_mode	shape_envtensor_to_context_guardstracingr   rR   _TorchCompileInductorWrapperr   r   hasattrr   ru   rS   build_guards__code__r%   settraced_codeadd_coder#   _get_signaturer$   get_runtime_envgetattrrY   rq   )r   r   r   r   r   r   r   r   r   r   capture_outputr   r   r   rp   rG   r   r+   tracing_contextr'   r   check_fnr*   r   r   aot_compiled_fnr   s                             @r   aot_compile_fullgraphr      s    :0E,!LD& 	[
()[
 	%%D%A[

 '88fM-BB#00<<<$$<#$45d %8E!++E2A&44(((1>1I1I"".()C)C)I)IJ##--#00::;	
; ))@)@A,9,K,K)MM!!/2	R##)).28<	R "**M,H,HK
 '5#E#EFD[Q	R 	R" +';<w.%11%)${*;)<KH!+.XYZ 
 (44KKu4d 5 
 $$000 7/;; 	.K  -	. %#2226"00!..$//#++,<<>## /9E
	 . 
s[
 [
 [
z k	R 	R 	R 	RQ[
 [
 [
 [
 [
z sm   	N+M?D9M3-M&	AMM&	D2M3?M?NM#M&	&M0+M33M<8M??N	NNc                   L    e Zd ZU dZee   ed<   eeef   ed<   e	e
e      ed<   y)
ModelInputa%  
    WIP type: represents a single model input
    Which consists of a tuple of arguments and a set of contexts in which to run the model.

    For each ModelInput, we'll compile one full graph of the model, and then use the guards generated
    to dispatch between the compiled graphs.


    r   r   contextsN)r5   r6   r7   __doc__tupler   r:   r   r<   r   r   rA   r!   r   r   r     s2     *cN)#.//r!   r   c                       e Zd ZU ej                  j
                  ed<   ee   ed<   de	de	de	fdZ
defdZedej                  j
                  dedd fd	       Zy
)AOTCompiledModelr   compiled_resultsr   r   r   c                     | j                   D ]:  } |j                  | j                  g|i |s$ || j                  g|i |c S   | j                   d   | j                  g|i |S )Nr   )r   ri   r   )r2   r   r   results       r   rw   zAOTCompiledModel.__call__6  sv    ++ 	;F!v!!$**>t>v>djj:4:6::	; (t$$Q'

DTDVDDr!   c                     g }| j                   D ]&  }|j                  t        j                  |             ( t	        j
                  |      S r   )r   appendrY   r   r   dumps)r2   r   r   s      r   r   zAOTCompiledModel.serialize=  sC    ++ 	?FKK+55f=>	?||D!!r!   r   c                 X   ddl m} ddlm}m} t        j                  |      }g }|D ]`  } | |t        j                  i                   5   |       5  |j                  t        j                  |             d d d        d d d        b  | ||      S # 1 sw Y   xY w# 1 sw Y   xY w)Nr   )r   )compile_contextCompileContext)r   r   r   r   r   r   r   r   get_compile_idr   rY   r   )	rI   r   r   r   r   r   resultsr   r   s	            r   r   zAOTCompiledModel.deserializeC  s    ;A%||D1 	QF}/K/KB/O PQQ#%Q !''(;(G(G(OP	Q Q Q	Q 5*++Q Q Q Qs$   B %B9B BB  B)	N)r5   r6   r7   r   nnModuler:   r   rY   r   rw   r;   r   rW   r   rA   r!   r   r   r   /  sz     88??.//Ec ES ES E"5 " , ,u ,AS , ,r!   r   inputsc                      dt         dt        f fd}g }|D ]/  }t        j                  d|       |j	                   ||             1 t        |      dkD  sJ t         |      S )zi
    Compiles a single nn.Module with any number of inputs, and returns a compiled forward function.
    model_inputr   c                     | j                   | j                  f}j                  }t               5 }| j                  D ]  }|j                  |        t        ||      cd d d        S # 1 sw Y   y xY w)N)r   r   )r   r   forwardr   r   enter_contextr   )r   r   orig_forwardstackctxr   r   r   s        r   compile_single_graphz0aot_compile_module.<locals>.compile_single_graph]  su    %**K,>,>?}}[ 	E"++ )##C()(		 	 	s   1A++A4zCompiling input %s..r   )r   rY   loginfor   ra   r   )r   r   r   r   r  r   r   s   ` ``   r   aot_compile_moduler  S  sw    * 9L   C'5 4[ ABC  1$$$E#344r!   )9r?   r8   r   loggingr   r=   collections.abcr   
contextlibr   r   r   typingr   r   r	   r   torch.fxtorch._dynamo.convert_framer
   torch._dynamo.graph_utilsr   r   r    r   aot_compile_typesr   r   r   r   guardsr   rn   r   	getLoggerr5   r  r9   r   r<   r    r#   PicklerrC   rY   r   fxGraphModuler   Tensorr   r   r   r   r   r  rA   r!   r   <module>r     s     	    $ 8 ! / /   7 8 ,   +# g!%  %),%8;%	#s(^% O O O$   `* `* `*Fjj%S/4S>9:j j uxx++T%,,-?@BVVW	j
 jZ 0 0 0   ,  ,  ,F588??55 5 uxx++T%,,-?@BVVW	5
 5r!   