o
    it)                     @   sP  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 d dlZd dlZd dlmZ ddlmZ ddlmZ eeZG dd	 d	e jZd
ejde
de
deee
f fddZeG dd dZeG dd dZG dd deZ de
de!e!e
df eee
f f dedeej"j#e$ej% gef def
ddZ&dS )    N)	dataclass)AnyCallableOptional)PrecompileContext   )convert_frame)Hooksc                   @   sD   e Zd ZeejdedefddZeejdedefddZ	dS )	SerializableCallablefnreturnc                 C      d S N clsr   r   r   V/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/torch/_dynamo/aot_compile.pyserialize_compile_artifacts      z0SerializableCallable.serialize_compile_artifactsdatac                 C   r   r   r   )r   r   r   r   r   deserialize_compile_artifacts   r   z2SerializableCallable.deserialize_compile_artifactsN)
__name__
__module____qualname__classmethodabcabstractmethodr   bytesr   r   r   r   r   r   r
      s    r
   	signatureargskwargsr   c                 O   s   | j |i |}|  |jS r   )bindapply_defaults	arguments)r   r   r    bound_argumentsr   r   r   bind_locals"   s   r%   c                   @   sz   e Zd ZU ejed< ejed< ee	j
jj ed< eed< eeef ed< eed< eed< ejed< eeed	f  ed
< dS )CompileArtifactsr   bytecodeguard_managerguards_stateimport_sources
backend_idcompiled_fnoriginal_code.closureN)r   r   r   inspect	Signature__annotations__typesCodeTyper   torch_dynamoguardsGuardManagerWrapperr   dictstrr
   tupler   r   r   r   r   r&   *   s   
 


r&   c                   @   s   e Zd ZU eed< 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e
ddfddZedd defddZededd fddZdS )AOTCompiledFunction
_artifactsr   r    r   c                 O   s8   t | jjg|R i |}| jjd usJ | jj|S r   )r%   r<   r   r(   check)selfr   r    f_localsr   r   r   guard_check;   s   zAOTCompiledFunction.guard_checkNc                 C   s   dd | j j D }i || j j| j ji}tj| j j|| j jd| _	| j j
d u rEt| j j}tjjj| j j|j|j|dj
| j _
d S d S )Nc                 S   s   i | ]
\}}|t |qS r   )	importlibimport_module).0aliasmodule_namer   r   r   
<dictcomp>A   s    
z5AOTCompiledFunction.__post_init__.<locals>.<dictcomp>r.   )shape_code_partsruntime_global_scope)r<   r*   itemsr+   r,   r2   FunctionTyper'   r.   r   r(   pickleloadsr)   r4   r5   r6   CheckFunctionManagerr-   output_graphrH   )r>   r*   	f_globalsr)   r   r   r   __post_init__@   s,   

z!AOTCompiledFunction.__post_init__c                 O   sj   | j jd usJ | j|i |s-t| j jg|R i |}t| j j|}td| | j|i |S )Nz#GuardManager check failed, reason: )	r<   r(   r@   r%   r   r9   check_verboseRuntimeErrorr   )r>   r   r    r?   reasonr   r   r   __call__V   s   zAOTCompiledFunction.__call__pathc                 C   sD   t |d}|t| |  W d    d S 1 sw   Y  d S )Nwb)openwritetype	serialize)r>   rV   fr   r   r   save_compiled_function^   s   "z*AOTCompiledFunction.save_compiled_functionr   c                 C   sr   ddl m} |jj }d |d< ||d |d< |d }t|jt||f|d< ||d |d< t	
|S )Nr   SerializedCoder(   r'   r,   r-   )torch._dynamo.packager_   r<   __dict__copyfrom_code_objectrZ   r   r   rL   dumps)r   r   r_   stater,   r   r   r   r[   b   s   
zAOTCompiledFunction.serializer   c                 C   sh   ddl m} t|}||d |d< |d \}}|||d< ||d |d< tdi |}| |S )Nr   r^   r'   r,   r-   r   )r`   r_   rL   rM   to_code_objectr&   )r   r   r_   re   deserializercompiled_fn_state	artifactsr   r   r   deserializeq   s   
zAOTCompiledFunction.deserialize)r   N)r   r   r   r&   r1   r   boolr@   rQ   rU   r9   r]   r   r   r[   rj   r   r   r   r   r;   7   s   
 
r;   c                       s   e Zd ZdZdeddfddZdedef fdd	Zed
edd fddZ	edd de
fddZede
defddZdededefddZ  ZS )&BundledAOTAutogradSerializableCallableaL  
    Represents a serializable callable generated by compile_fx.
    This class wraps around the compiled function generated by AOTAutograd.

    TODO: Instead of using PrecompileContext to grab it from AOTAutograd,
    this object should be what's *returned* by aot_module_simplified.
    We'll do that refactor in a later PR.
    artifactr   Nc                 C   s   |  | _|j| _dS )z
        Takes in a BundledAOTAutogradCacheArtifact, which is the serialized form
        of a compiled function generated by AOTAutograd.
        N)after_deserializationr,   contentr   )r>   rm   r   r   r   __init__   s   
z/BundledAOTAutogradSerializableCallable.__init__attrc                    s"   t | |rtt |S t| j|S r   )hasattrgetattrsuperr,   )r>   rq   	__class__r   r   __getattr__   s   
z2BundledAOTAutogradSerializableCallable.__getattr__r+   c                 C   s&   t |}|du rtd| | |S )z
        Takes in a backend_id, and returns a BundledAOTAutogradSerializableCallable
        that wraps around the compiled function generated by AOTAutograd.
        Nz"No artifact found for backend_id: )r   serialize_artifact_by_keyrS   )r   r+   rm   r   r   r   from_backend_id   s   
z6BundledAOTAutogradSerializableCallable.from_backend_idr   c                 C   s   |j S r   )r   r   r   r   r   r      s   zBBundledAOTAutogradSerializableCallable.serialize_compile_artifactsr   c                 C   s   ddl m} |d|}| |S )Nr   )BundledAOTAutogradCacheArtifact )-torch._functorch._aot_autograd.autograd_cacherz   )r   r   rz   rm   r   r   r   r      s   
zDBundledAOTAutogradSerializableCallable.deserialize_compile_artifactsr   r    c                 O   s   | j |i |S r   )r,   )r>   r   r    r   r   r   rU      s   z/BundledAOTAutogradSerializableCallable.__call__)r   r   r   __doc__r   rp   rw   r   r9   ry   r   r   r   rU   __classcell__r   r   ru   r   rl      s&    		
rl   modelexample_inputs.hooksbackendc                    s  ddl m  ddlm}m} ddlm}m}m} |\}	}
t	| dr+| j
}| jf|	 }	nt| r3| }ntd|  t|}t|g|	R i |
}|jjsQ|jrnt|jt|jjks^J |dd t|jj|jD  ||ti q | ^ |d	J ttj|j|j|tj|jpd
d}|j}|jsddl m!} dt"| dt"t# f fdd}||_|j$|j|ddd}|j%d usJ W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  |j&}|j'|j(_)|j*j+}|d usJ |j,}t-j./||j0( t-j1j23dd ||j(|j4}W d    n	1 s+w   Y  W d    n	1 s;w   Y  t5|t-j6rMt78|j'}t5|t9sot	|dr]|j:}n|}tdt;| dd| d t<||j=|j>|j%||j'||j|jd	}t?|d}|S )Nr   rN   )dynamo_timedget_metrics_context)compile_contextCompileContextTracingContext__self__zUnsupported model code type c                 S   s   i | ]\}}||j qS r   )cell_contents)rC   namecellr   r   r   rF      s    z)aot_compile_fullgraph.<locals>.<dictcomp>fullgraph_capturer   rG   )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>   s    zFaot_compile_fullgraph.<locals>.new_guard_filter_fn.<locals>.<listcomp>r   )r   r   r   r   new_guard_filter_fn   s   
z2aot_compile_fullgraph.<locals>.new_guard_filter_fnT)r   savestrict_errorbundled_autograd_cachecompiler_fnzCompiled function type z (produced zfrom backend z*) does not implement SerializableCallable.)	r   r'   r(   r)   r*   r+   r,   r-   r.   )r<   )@torch._dynamo.guardsrN   torch._dynamo.utilsr   r   torch._guardsr   r   r   rr   __func__r   r/   
isfunctionrS   r   r%   __code__co_freevars__closure__lenupdatezipr   get_compile_idr   	FrameInfo__globals__builtinsra   dynamo_outputguard_filter_fntorch._dynamo.typesr   listrk   build_guardsr)   backend_inputr+   graph_module_backend_idtracer_outputrO   r*   r4   _guardstracing	fake_mode
_functorchconfigpatchr   
isinstance_TorchCompileInductorWrapperrl   ry   r
   r   rZ   r&   r'   r(   r;   )r   r   r   r   r   r   r   r   r   r   r    r   r   r?   capture_outputr   r   r   check_fnr   rO   r*   r,   r   ri   aot_compiled_fnr   r   r   aot_compile_fullgraph   s   


	
  )
 

r   )'r   r   rA   r/   loggingrL   r2   dataclassesr   typingr   r   r   r4   torch.fx torch._dynamo.precompile_contextr   r{   r   r   r	   	getLoggerr   logABCr
   r0   r8   r9   r%   r&   r;   rl   r:   fxGraphModuler   Tensorr   r   r   r   r   <module>   sR    


G;