o
    ei-H                     @   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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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%m&Z& e'e(Z)dej*dedede+e,ef fddZ-eG dd dZ.G dd dej/Z0G dd dej1Z2eG dd dZ3eG dd dZ4	d.ded e5e5ed!f e+e,ef f d"e!d#eej6j7e8ej9 gef d$e:dB de4fd%d&Z;eG d'd( d(Z<eG d)d* d*Z=dej>j?d+e8e< d"e!d#eej6j7e8ej9 gef de=f
d,d-Z@dS )/    N)CallableSequence)AbstractContextManager	ExitStacknullcontext)	dataclass)AnyOptionalTYPE_CHECKING)GraphRuntimeEnv)_graph_device_type)
SystemInfo   )convert_frame)&BundledAOTAutogradSerializableCallableSerializableCallable)Hooks)GuardManagerWrapper)SerializedCode
SourceInfo	signatureargskwargsreturnc                 O   s   | j |i |}|  |jS N)bindapply_defaults	arguments)r   r   r   bound_arguments r   c/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_dynamo/aot_compile.pybind_locals#   s   r!   c                   @   s   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dZeed< dddZdS )CompileArtifactsr   r   guard_managerguards_state
backend_idcompiled_fnoriginal_coderuntime_envr   source_infodevice_typebackend_name)default_factorysystem_infor   Nc                 C   s   t  }|| j| j d S r   )r   currentcheck_compatibilityr-   r*   )selfcurrent_systemr   r   r    r/   9   s   z$CompileArtifacts.check_compatibilityr   N)__name__
__module____qualname__inspect	Signature__annotations__r	   bytesstrr   typesCodeTyper   dataclassesfieldr   r.   r-   r/   r   r   r   r    r"   +   s   
 

r"   c                       s  e Zd Zdeeef dejddf fddZdede	eB dB fdd	Z
ed
edefddZedededejfddZededejfddZedddejfddZedejdededeedf dB deejdf dB dejfddZdedefd d!Z  ZS )"AOTCompilePicklerexternal_databufr   Nc                    s0   t  | || _dd | D | _i | _d S )Nc                 S   s   i | ]	\}}t ||qS r   )id).0keyvaluer   r   r    
<dictcomp>B   s    z.AOTCompilePickler.__init__.<locals>.<dictcomp>)super__init__r@   itemsid_maperrors)r0   r@   rA   	__class__r   r    rH   ?   s   
zAOTCompilePickler.__init__objc                 C   sD   t || jv r| jt | S t|tjjr || jt |< t |S d S r   )rB   rJ   
isinstancetorchnnModulerK   )r0   rN   r   r   r    persistent_idG   s   zAOTCompilePickler.persistent_idvalc                    s*   dt f fdd}|jd usJ |jd S )Nr   c                          S r   r   r   rT   r   r    _R   s   z+AOTCompilePickler._unpickle_cell.<locals>._r   )object__closure__)clsrT   rW   r   rV   r    _unpickle_cellP   s   
z AOTCompilePickler._unpickle_cellfuncbasec                 C   s   t ||S r   )r;   
MethodType)rZ   r\   r]   r   r   r    _unpickle_bound_methodX   s   z(AOTCompilePickler._unpickle_bound_methodnamec                 C   s
   t |S r   )	importlibimport_module)rZ   r`   r   r   r    _unpickle_module]   s   
z"AOTCompilePickler._unpickle_moduleserialized_coder   c                 C   s   ddl m} ||S )Nr   r   )torch._dynamo.packager   to_code_object)rZ   rd   r   r   r   r    _unpickle_codea   s   
z AOTCompilePickler._unpickle_codecodemodulequalnameargdefs.closurec                 C   s   t |j}t|||||S r   )ra   rb   __dict__r;   FunctionType)rZ   ri   rj   rk   rl   rm   	f_globalsr   r   r    _unpickle_nested_functiong   s   	z+AOTCompilePickler._unpickle_nested_functionc                 C   s  t |tdd djd rt| j|jffS t|r-ddlm} t| j	|
|ffS t|r;t| j|jffS t|rd	 |j}|j}t||j}t|rU|j}||urbt| j||ffS tS t|rd|jv rt| j|j|j|j|j|jffS tS )Nc                    s    fddS )Nc                      rU   r   r   r   xr   r    <lambda>u   s    zFAOTCompilePickler.reducer_override.<locals>.<lambda>.<locals>.<lambda>r   rr   r   rr   r    rt   u   s    z4AOTCompilePickler.reducer_override.<locals>.<lambda>r   re   z<locals>)rO   typerY   r[   cell_contentsr6   iscoderf   r   rh   from_code_objectismodulerc   r3   ismethod__func____self__getattrr_   
isfunctionr5   rq   __code__r4   __defaults__NotImplemented)r0   rN   r   r\   method_self
inner_funcr   r   r    reducer_overridet   s6   





z"AOTCompilePickler.reducer_override)r3   r4   r5   dictr:   rX   ioBytesIOrH   intrS   classmethodr[   r   r;   r^   r_   
ModuleTyperc   r<   rh   tupleCellTypero   rq   r   r   __classcell__r   r   rL   r    r?   >   s4    $	r?   c                       sF   e Zd Zdeeef dejdef fddZdedefddZ	  Z
S )	AOTCompileUnpicklerr@   filer   c                    s   t  | || _d S r   )rG   rH   r@   )r0   r@   r   rL   r   r    rH      s   
zAOTCompileUnpickler.__init__rD   c                 C   s*   || j vrtd| d| j  | j | S )Nz-Missing required external reference to data: zS. Please load AOT compiled function with `external_data=<external data dictionary>`)r@   RuntimeError)r0   rD   r   r   r    persistent_load   s   

z#AOTCompileUnpickler.persistent_load)r3   r4   r5   r   r:   rX   r   r   rH   r   r   r   r   rL   r    r      s    $r   c                   @   s   e Zd ZU eed< dS )AOTCompileSaveResultserialized_dataN)r3   r4   r5   r9   r8   r   r   r   r    r      s   
 r   c                
   @   s.  e Zd ZU eed< dZeed< dZee	e
f dB ed< de
de
dee	e
f fd	d
ZdededefddZd"ddZdededefddZd#ddZ	d$de	dee	ef dB defddZe	d$dd dee	ef dB defddZe		d%dedee	e
f dB dee	ef dB dd fddZd"d d!ZdS )&AOTCompiledFunction
_artifactsT_guard_check_enabledN_extra_globalsr   r   r   c                 O   sr   i }| j j}|jr'|jjrt|jt|jjksJ dd t|jj|jD }|t| j j	g|R i | |S )Nc                 S   s   i | ]\}}||j qS r   )rv   )rC   r`   cellr   r   r    rF      s    z8AOTCompiledFunction.prepare_f_locals.<locals>.<dictcomp>)
r   r(   rm   bytecodeco_freevarslenzipupdater!   r   )r0   r   r   f_localsenvr   r   r    prepare_f_locals   s   
 z$AOTCompiledFunction.prepare_f_localsc                 O   s.   | j |i |}| jjd usJ | jj|S r   )r   r   r#   check)r0   r   r   r   r   r   r    guard_check   s   zAOTCompiledFunction.guard_checkc                 C   sr   ddl m}m} | j  | jjj| jj| jj| j	d| _
| jjd u r7|| jj}||| jj| j
j| j_d S d S )Nr   )load_guard_managerload_guards_state)extra_globals)packager   r   r   r/   r(   forward_callabler%   r&   r   fnr#   r$   r'   __globals__)r0   r   r   r$   r   r   r    __post_init__   s   
z!AOTCompiledFunction.__post_init__c                 O   sf   | j jd usJ | jr+| j|i |s+| j|i |}t| j j|}td| | j|i |S )Nz#GuardManager check failed, reason: )	r   r#   r   r   r   r:   check_verboser   r   )r0   r   r   r   reasonr   r   r    __call__   s   zAOTCompiledFunction.__call__r   c                 C   s   | j jS r   )r   r)   r0   r   r   r    r)      s   zAOTCompiledFunction.source_infopathr@   c                 C   sN   t |d}t| | |}||j |W  d    S 1 s w   Y  d S )Nwb)openru   	serializewriter   )r0   r   r@   fresultr   r   r    save_compiled_function   s
   $z*AOTCompiledFunction.save_compiled_functionr   c                 C   s   ddl m} |jj }d |d< tj|d ||d jd|d< |d }t	|j
t	||f|d< ||d |d< t }t|pDi |}|| |jr\tdt|j  d	t| d
S )Nr   re   r#   r(   r   r&   r'   z+Failed to serialize the following objects: zI
Please mark these as external data by using `external_data={'key': ...}`)r   )rf   r   r   rn   copyr=   replacerx   r   ru   deserialize_compile_artifactsserialize_compile_artifactsr   r   r?   dumprK   r   listvaluesr   getvalue)rZ   r   r@   r   stater&   rA   picklerr   r   r    r      s(   

zAOTCompiledFunction.serializedatarp   external_closure_datac                 C   s   ddl m} t|}|d t|pi |}| }|  tj	|d |
|d jd|d< |d \}}	tjjjdd ||	|d< W d    n1 sOw   Y  |
|d |d< td
i |}
| |
|d	S )Nr   re   r(   r   r&   T)enable_autograd_for_aotr'   )r   r   )rf   r   r   r   seekr   loadcloser=   r   rg   r   rP   	_inductorconfigpatchr"   )rZ   r   rp   r   r   r   	unpicklerr   deserializercompiled_fn_state	artifactsr   r   r    deserialize	  s"   


zAOTCompiledFunction.deserializec                 C   s
   d| _ d S )NF)r   r   r   r   r    disable_guard_check#  s   
z'AOTCompiledFunction.disable_guard_checkr2   )r   r   r   )NN)r3   r4   r5   r"   r8   r   boolr   r   r:   rX   r   r   r   r   r   r)   r   r   r   r   r9   r   r   r   r   r   r    r      sN   
 


r   modelexample_inputs.hooksbackenddynamicc                    sP  ddl m  ddlm} ddlm}m} ddlm} |\}	}
t	 }|d ur/ddl
m} ||}| j |dK tjjjdd	1 | t| |	|
}|j}|jd usYJ |jstdd
lm} dt| dtt f fdd}||_t| \}}|j}|d usJ |j|j_t|jj}|j j!|jj!u sJ ||j }|j"|_"tj#$|E tjjdddddd( ||j|j%}t&|tj'st(|drt&|j)tj*j+j,j-rt.|}W d    n1 sw   Y  W d    n1 sw   Y  t&|t/st(|dr|j)}n|}t0dt1| dd| d |j2|j3|ddd}|j4d us)J |t5 d}|j6D ]}|7| q2t8t9||j:|j4|j||j3|; ||t<|ddd
}t=||j>d}W d    n	1 shw   Y  W d    n	1 sxw   Y  W d    n1 sw   Y  W d    |S W d    |S 1 sw   Y  |S )Nr   CheckFunctionManager)r   )dynamo_timedget_metrics_context)TracingContext)set_enable_dynamicfullgraph_captureT)strict_autograd_cache)GuardFilterEntryguard_entriesr   c                    s    fdd| D S )Nc                    s    g | ]}|j p|j jv  qS r   )	is_global
guard_type%UNSUPPORTED_SERIALIZATION_GUARD_TYPES)rC   gr   r   r    
<listcomp>K  s    zFaot_compile_fullgraph.<locals>.new_guard_filter_fn.<locals>.<listcomp>r   )r   r   r   r    new_guard_filter_fnH  s   
z2aot_compile_fullgraph.<locals>.new_guard_filter_fn)r   bypass_autograd_cache_keybundled_autograd_cache force_non_lazy_backward_loweringforce_autograd_cache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+   )r   r   )?torch._dynamo.guardsr   rf   r   torch._dynamo.utilsr   r   torch._guardsr   r   torch._dynamo.eval_framer   rP   
_functorchr   r   r   r   graph_capture_outputoutput_graphguard_filter_fntorch._dynamo.typesr   r   r   get_traced_fnbackend_inputr%   graph_module_backend_idr   graph	fake_mode	shape_envtensor_to_context_guardstracingr   rO   _TorchCompileInductorWrapperhasattrr   _dynamobackendscommonAotAutogradr   r   r   ru   build_guardsr   r$   settraced_codeadd_coder"   _get_signaturer#   get_runtime_envr}   r   r   )r   r   r   r   r   r   r   r   r   r   r   dynamic_ctxr   capture_outputr   r   r   r   rW   r   r*   tracing_contextr&   r   check_fnr)   r  r   aot_compiled_fnr   r   r    aot_compile_fullgraph'  s   


 



 * fffr  c                   @   s>   e Zd ZU dZee ed< eeef ed< e	e
e  ed< dS )
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)r3   r4   r5   __doc__r   r   r8   r   r:   r   r   r   r   r   r    r    s
   
 
r  c                   @   sh   e Zd ZU ejjed< ee ed< de	de	de	fddZ
defdd	Zedejjd
edd fddZdS )AOTCompiledModelr   compiled_resultsr   r   r   c                 O   s`   | j D ]}|j| jg|R i |r || jg|R i |  S q| j d | jg|R i |S )Nr   )r  r   r   )r0   r   r   r   r   r   r    r     s
   
zAOTCompiledModel.__call__c                 C   s,   g }| j D ]}|t|j qt|S r   )r  appendr   r   r   pickledumps)r0   r   r   r   r   r    r     s   

zAOTCompiledModel.serializer   c           	   
   C   s   ddl m} ddlm}m} t|}g }|D ]6}||ti # |  |	t
| W d    n1 s9w   Y  W d    n1 sHw   Y  q| ||S )Nr   )r   )compile_contextCompileContext)r   r   r   r  r  r  loadsr   get_compile_idr  r   r   )	rZ   r   r   r   r  r  resultsr  r   r   r   r    r     s   
 
zAOTCompiledModel.deserializeN)r3   r4   r5   rP   rQ   rR   r8   r   r   r   r   r9   r   r   r   r   r   r   r    r    s   
  r  inputsc                    s\   dt dtf fdd}g }|D ]}td| ||| qt|dks)J t|S )zi
    Compiles a single nn.Module with any number of inputs, and returns a compiled forward function.
    model_inputr   c                    sb   | j | jf}j}t }| jD ]}|| qt|| dW  d    S 1 s*w   Y  d S )N)r   r   )r   r   forwardr   r  enter_contextr  )r$  r   orig_forwardstackctxr   r   r   r   r    compile_single_graph  s   
$z0aot_compile_module.<locals>.compile_single_graphzCompiling input %s..r   )r  r   loginfor  r   r  )r   r#  r   r   r+  r  r$  r   r*  r    aot_compile_module  s   

r.  r   )Ar=   ra   r6   r   loggingr  r;   collections.abcr   r   
contextlibr   r   r   r   typingr   r	   r
   rP   torch.fxtorch._dynamo.convert_framer   torch._dynamo.graph_utilsr   rf   r    r   aot_compile_typesr   r   r   r   guardsr   r   r   r   	getLoggerr3   r,  r7   r   r:   r!   r"   Picklerr?   	Unpicklerr   r   r   r   fxGraphModuler   Tensorr   r  r  r  rQ   rR   r.  r   r   r   r    <module>   s    


[}
}#