
    9jH                       d dl m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 d dlmZmZm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 d dlmZmZ d dl m!Z! d dl"m#Z# ddl$m%Z% erd dl&m'Z'm(Z( d dl)m*Z* d dlm+Z+  ejX                  e-      Z. G d de	      Z/ G d de/      Z0 G d de/      Z1d dZ2d!dZ3ejh                  d"d       Z5ddd	 	 	 	 	 	 	 	 	 	 	 	 	 d#dZ6	 d$	 	 	 d%dZ7y)&    )annotationsN)ABCabstractmethod)AbstractContextManagernullcontext)AnyLiteralTYPE_CHECKING)&BundledAOTAutogradSerializableCallable)dynamo_timed)normalize_path_separator)BoxedDeviceIndex)temporary_cache_dir)	BoxedBool	InputType)FakeTensorMode)ShapeEnv   )config)CallableSequence)	CacheInfo)GraphModulec                  v    e Zd ZdZ	 	 	 	 d	dZed
d       Zedd	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Z	y)CompiledArtifacta  
    CompiledArtifact class represents the inductor cache artifacts that
    can be invoked in order to avoid repeated compilation.

    CompiledArtifact can be obtained by calling standalone_compile(gm, example_inputs)
    to create a fresh CompiledArtifact from a GraphModule and example inputs.

    Later this CompiledArtifact can be saved to disk, either as a binary or unpacked
    into the provided folder via the CompiledArtifact.save function.

    CompiledArtifact.load provides a way to create a CompiledArtifact from the
    binary or unpacked data.

    Finally, the CompiledArtifact can be invoked via the __call__ method
    to execute the cached artifact.
    c                     || _         || _        y N_compiled_fn
_artifactsselfcompiled_fn	artifactss      b/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_inductor/standalone_compile.py__init__zCompiledArtifact.__init__6       
 (#    c                     y r    r"   argss     r%   __call__zCompiledArtifact.__call__>   s    +.r(   binaryformatc                    y r   r*   )r"   pathr0   s      r%   savezCompiledArtifact.saveA   s     r(   c                @   |dk(  rt         j                  | |      S |dk(  sJ t        | d      5 }ddlm} ddlm} |j                         } ||      }|j                         }|t        j                  k(  rZ|j                          |       k(  sJ |j                         }|j                         sJ t        j                  |      cd d d        S |t         j                  k(  r|j                          |       k(  sJ |j                         }	|j                         }
|j                         sJ t        j                   j#                  |
       t         j%                  t'               |	      cd d d        S t)        d	|j+                  d
      z         # 1 sw Y   y xY w)Nunpackedr2   r0   r.   rbr   BytesReaderr   	torch_keyzLInvalid header, expected CacheCompiledArtifact or AOTCompiledArtifact, got: utf-8)CacheCompiledArtifactloadopen&torch.utils._appending_byte_serializerr9   	codecacher;   read
read_bytesAOTCompiledArtifact
AOT_HEADERis_finisheddeserializeCACHE_HEADERread_strtorchcompilerload_cache_artifacts
_load_implr   RuntimeErrordecode)r2   r0   filer9   r;   result_bytesreaderheaderartifactkeyartifact_bytess              r%   r>   zCompiledArtifact.loadF   s|    Z(--4-GG!!!$ 	J,99;L .F&&(F,777((*ik999!,,.))+++*66x@	 	 0===((*ik999oo'!'!2!2!4))+++33NC,77sK)	 	, #bmmG,- -	 	s   BFBF7FFNr#   Callable[..., Any]r$   ztuple[bytes, CacheInfo] | Noner,   r   returnr   r2   strr0   Literal['binary', 'unpacked']rZ   Noner2   r\   r0   r]   rZ   r   )
__name__
__module____qualname____doc__r&   r   r-   r3   staticmethodr>   r*   r(   r%   r   r   $   s    "$'$ 2$ . .DL$A	  >F!!;!	! !r(   r   c                      e Zd ZdZ ed d      Z	 	 	 	 ddZddZddZdd	 	 	 	 	 ddZ	e
	 	 	 	 	 	 dd	       Ze
dd	 	 	 	 	 dd
       Ze
dd	 	 	 	 	 dd       Zy)r=   zN
    CompiledArtifact that depends on torch.compiler.save_cache_artifacts
    r<   c                     || _         || _        y r   r   r!   s      r%   r&   zCacheCompiledArtifact.__init__r   r'   r(   c                      | j                   | S r   )r   r+   s     r%   r-   zCacheCompiledArtifact.__call__z   s     t  $''r(   c                j    | j                   y| j                   \  }}t        |j                        dk(  S )NFr   )r    lenaot_autograd_artifacts)r"   _
cache_infos      r%   is_saveablez!CacheCompiledArtifact.is_saveable}   s4    ??": :445::r(   r.   r/   c          
        t        d      5  | j                  t        d      | j                  \  }}t        |j                        dk(  rt        d|       t        |j                        dkD  rt        d|       |j                  d   }|dk(  rt        j                  j                  |      rJ ddl	m
} dd	lm}  |       }|j                  t        j                         |j                   |              |j!                  |       |j                  |       dd
lm}	  |	||j'                                np|dk(  sJ t        j                  j)                  |      r8t        j                  j                  |      sJ t+        j,                  |d       ddlm}
 t1        |      5  t2        j4                  j7                  |      }|J |j8                  D ]  }|
j;                  |      }t        j                  j)                  |      sJ t=        t        j>                  |            D ]p  }tA        t        j                  jC                  ||      d      5 }tE        jF                  |      }d d d        jI                         }tJ        jM                  d|       r  	 d d d        d d d        y # 1 sw Y   ExY w# 1 sw Y   xY w# 1 sw Y   y xY w)NzCompiledArtifact.savezFCompiledArtifact.save failed to save since there's no artifact to saver   a  CompiledArtifact.save failed to save due to no aot_autograd artifacts. This likely means there was something that was not serializable in the graph passed to standalone_compile. This can generally be fixed by ensuring that your model only uses constructs that are serializable. r   zhCompiledArtifact.save failed to save because there was more than one artifact but we only expected one. r.   BytesWriterr:   write_atomicr5   T)ignore_errors)FxGraphCacher7   zOutput code written to: %s)'r   r    rN   ri   rj   AssertionErrorosr2   isdirr@   rp   rA   r;   write_bytesr=   rH   	write_strtorch._inductor.codecacherr   to_bytesexistsshutilrmtreert   r   rJ   rK   rL   inductor_artifacts_get_tmp_dir_for_keysortedlistdirr?   joinpickler>   write_to_diskloginfo)r"   r2   r0   rV   rl   rU   rp   r;   writerrr   rt   loaded_cache_infosubdirfgraphoutput_files                   r%   r3   zCacheCompiledArtifact.save   s    12 =	P&"\  *.&NJ:445:"\ "l	$  :4459$::DG  33A6C!77==...N0$""#8#E#EF""9;/  %"">2BT6??#45+++77>>$'77==...MM$d;3(. P(-(K(K&)% -888  1CC P!-!B!B3!G!ww~~f555$*2::f+=$> PD!%bggll64&@$!G 71(.A7*/*=*=*?KHH%A;O	PPP]=	P =	Pt7 7P P]=	P =	Ps=   F K,B0KJ622K%K6J?;KK	KKc                  	 | 5  t        j                  d      5  t        j                  j                   j                  d      5  ddlm} |j                  |ddg i d       }d d d        J |\  }}dd	lm}  |t        d      t        d      
      }t        j                  j                  t        t                           }t        j                  j                  |      5  |j!                  g |j"                  |      	d d d        d d d        d d d        t%        	fdd       S # 1 sw Y   xY w# 1 sw Y   4xY w# 1 sw Y   8xY w# 1 sw Y   <xY w)NT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cacher   )AOTAutogradCacheF)localremoter,   rl   
aot_configr   )_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 &     t        |             S r   )list)r,   r#   s    r%   <lambda>z2CacheCompiledArtifact._load_impl.<locals>.<lambda>   s    ;tDz3J r(   )r   patchrJ   
_functorch-torch._functorch._aot_autograd.autograd_cacher   _lookup
compile_fxr   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configr=   )
cache_dir_ctxrU   r   resultentryrk   r   	fx_configcontextr#   s
            @r%   rM   z CacheCompiledArtifact._load_impl   sF   
  	LLE 	 !!((..T.J  *11 !# 2  %%%JUA4($U++;A+>I
 mm22>HJ3WXG&&w/ #5522I; 	  	B %%JDQQ; 2 ; 	  	  	  	sS   E+ED,$BE'D8EE,D51E8E=EE		EEc                   t        |       } t        d      5  |dk(  rt        j                  j	                  |       rJ t        | d      5 }|j                         }ddd       ddlm} ddl	m
}  |      }|j                          |       k(  sJ |j                         }|j                         }|j                         sJ t        j                  j!                  |       |t#               fcddd       S |d	k(  sJ t        j                  j	                  |       sJ t        j                  j%                  | d
      }	t        j                  j	                  |	      sJ t'        t        j(                  |	            }
t+        |
      dk(  sJ |
d   }t-        |       }||fcddd       S # 1 sw Y   RxY w# 1 sw Y   yxY w)zU
        Do format specific prep and loads, return a context manager and key
        zCompiledArtifact.loadr.   r7   Nr   r8   r   r:   r5   aotautograd)r   r   rv   r2   rw   r?   rB   r@   r9   rA   r;   rC   rI   rF   rJ   rK   rL   r   r   r   r   ri   r   )r2   r0   rP   r$   r9   r;   rR   rU   rV   autograd_cache_dirfilesr   s               r%   _prepare_loadz#CacheCompiledArtifact._prepare_load   s    (-12 	*!77==...$% , $		I,N0$Y/((*ik999oo'!'!2!2!4))+++33NCKM)#	* 	*& +++ww}}T***%'WW\\$%F"ww}}%7888RZZ(:;<5zQ&Ah 3D 9M)7	* 	*, ,		* 	*s+   3F8
F+BF87B*F8+F5	0F88Gc                b    t         j                  | |      \  }}t         j                  ||      S )Nr6   )r=   r   rM   )r2   r0   rU   r   s       r%   r>   zCacheCompiledArtifact.load  s9     3@@f A 
] %//sCCr(   NrW   rY   )rZ   boolr[   )r   zAbstractContextManager[Any]rU   r\   rZ   r   )r2   r\   r0   r]   rZ   z'tuple[str, AbstractContextManager[Any]]r_   )r`   ra   rb   rc   bytesrH   r&   r-   rm   r3   rd   rM   r   r>   r*   r(   r%   r=   r=   k   s     0':L$'$ 2$(; EM@P@P$A@P	@PD $R2$R9<$R	$R $RL >F"*"*;"*	0"* "*H >FDD;D	D Dr(   r=   c                      e Zd ZdZ ed d      Z	 	 ddZe	 	 	 	 dd       ZddZ	dd	 	 	 	 	 ddZ
dd	Zedd
       Zedd	 	 	 	 	 dd       Zy)rD   a9  
    Similar to CompiledArtifact, but the object is a single, bundled precompiled function.
    This object is always a serializable callable function.

    This object is essentially a wrapper for BundledAOTAutogradSerializableCallable, which
    is used by torch._dynamo.aot_compile for AOT Precompilation.
    r<   c                2    t        |      | _        d | _        y r   )r   inner_fnr    )r"   r#   s     r%   r&   zAOTCompiledArtifact.__init__)  s     ?{K 	r(   c                ,    t        | j                        S r   )rD   r#   )
bundled_fns    r%   from_bundled_callablez)AOTCompiledArtifact.from_bundled_callable2  s     #:#9#9::r(   c                      | j                   | S r   )r   r+   s     r%   r-   zAOTCompiledArtifact.__call__8  s    t}}d##r(   r.   r/   c               0   |dk(  rt        d      | j                         }ddlm} ddlm}  |       }|j                  t        j                         |j                   |              |j                  |       ddl	m
}  |||j                                y )Nr5   8AOTCompiledArtifact does not support unpacked format yetr   ro   r   r:   rq   )rN   	serializer@   rp   rA   r;   rx   rD   rE   rz   rr   r{   )r"   r2   r0   rQ   rp   r;   r   rr   s           r%   r3   zAOTCompiledArtifact.save;  s     ZJ  ~~'F(.99:9;'<(: 	T6??,-r(   c                @    t        j                  | j                        S r   )r   serialize_compile_artifactsr   )r"   s    r%   r   zAOTCompiledArtifact.serializeQ  s    5QQMM
 	
r(   c                z    t        j                  |       }t        |t               sJ t        j	                  |      S r   )r   deserialize_compile_artifacts
isinstancerD   r   )rQ   deserializeds     r%   rG   zAOTCompiledArtifact.deserializeV  s>     3PP 	
 ,(NOOO"88FFr(   c                   |dk(  rt        d      t        | d      5 }ddlm} ddlm} |j                         } ||      }|j                         }|t        j                  k(  sJ |j                          |       k(  sJ |j                         }|j                         sJ t        j                  |      cd d d        S # 1 sw Y   y xY w)Nr5   r   r7   r   r8   r   r:   )rN   r?   r@   r9   rA   r;   rB   rC   rD   rE   rF   rG   )	r2   r0   rP   r9   r;   rQ   rR   rS   rT   s	            r%   r>   zAOTCompiledArtifact.load`  s     ZJ  $ 	=J,99;L .F&&(F0;;;;;$$&)+555((*H%%'''&228<	= 	= 	=s   BCC
N)r#   rX   )r   r   rZ   rD   rY   r[   )rZ   r   )rQ   r   rZ   rD   r_   )r`   ra   rb   rc   r   rE   r&   rd   r   r-   r3   r   rG   r>   r*   r(   r%   rD   rD     s     ,g6J
'
 ;:;	; ;
$ EM..$A.	.,

 G G >F==;=	= =r(   rD   c                    | dk(  S )Nfrom_example_inputsr*   )dynamic_shapess    r%   _resolve_ignore_shape_envr   w  s     222r(   c                .   |dk(  rt        t                     S |dk(  rBt        j                  j                  j                         }|j                  J |j                  S |dk(  r!t        t        t        | j                  j                                    }|j                  dk(  sJ t        |j                        dk(  sJ t        |j                  d   t        j                   j"                        r|j                  d   gn|j                  d   }|D ]\  }d|j$                  v s|j$                  d   }t        |t        j&                  j(                  j*                        sP|j                  c S  t        t                     S t-        d	| d
      )Nr   r   from_tracing_context
from_graphoutputr   r   example_valuezJstandalone_compile got unsupported `dynamic_shapes` value: dynamic_shapes=.)r   r   rJ   r   r   get	fake_modenextiterreversedr   nodesopri   r,   r   fxNodemeta_subclassesfake_tensor
FakeTensor
ValueError)gmr   r   	last_noder   nodemaybe_tensors          r%   _resolve_fake_moder   }  se   ..
33	1	1 --..224  ,,,   	<	'
 hrxx~~678	||x'''9>>"a''' )..+UXX]]; ^^A" 	
  	2D$))+#yy9lE,=,=,I,I,T,TU'111		2 
33XYgXhhij
 	
r(   c           	   #    K   ddl m} t        | |      }t        j                  j                  |      }t        j                  j                  |      5  |j                         5  t        j                  dd      5  t        j                  j                  j                  d|      5  d  d d d        d d d        d d d        d d d        y # 1 sw Y   "xY w# 1 sw Y   &xY w# 1 sw Y   *xY w# 1 sw Y   y xY ww)Nr   )CacheArtifactManagerztriton.autotune_at_compile_timeTbundled_autograd_cache)torch.compiler._cacher   r   rJ   r   r   r   with_fresh_cacher   r   r   )r   r   aotr   r   tracing_contexts         r%   _standalone_contextr     s     :"2~6Imm229=Oo.--/ 	6= 	%%&>D	 	           sf   AC<C0$C$;+C&C	+C3C$;C0	C<CCC!C$$C-	)C00C95C<F)r   donate_graph_modulec                  ddl m } t        |      }t        | ||      5  |st        j                  |       }  || |fd|i|}t        |      sJ |r+t        |d      st        d      t        |      cddd       S t        j                  j                         }	|	t        j                  d       ddd       t        	      S # 1 sw Y   xY w)z=
    Implementation of torch.inductor.standalone_compile
    r   r   ignore_shape_envr   z<Compiled function should have serialize method when aot=TrueNzstandalone_compile artifact generation failed, cannot save. Run with TORCH_LOGS=+torch._inductor.codecache to identify the problem)r   r   r   copydeepcopycallablehasattrrN   rD   rJ   rK   save_cache_artifactsr   warningr=   )
r   example_inputsr   optionsr   r   r   r   r#   r$   s
             r%   standalone_compiler     s     '0@	R	5 "r"B 
1A
EL
 $$$;4"R  '{3  NN779	KKY!* !i88+ s   AC=5CCc                    ddl m} t        |      }t        | ||      5  |j	                  | ||      cd d d        S # 1 sw Y   y xY w)Nr   r   )r   ) r   r   r   autograd_cache_key)r   r   r   r   r   r   s         r%   r   r     sO     0@	UNC	8 
,,- - 

 
 
s	   =A)r   r   )r   r   r   r   rZ   r   )r   r   r   r   r   r   )r   r   r   zSequence[InputType]r   r   r   r   r   r   r   r   rZ   r   )F)r   r   r   r   )8
__future__r   
contextlibr   loggingrv   r   r}   abcr   r   r   r   typingr   r	   r
   torch.fxrJ   torch._dynamo.aot_compile_typesr   torch._dynamo.utilsr   torch._inductor.cpp_builderr   torch._inductor.cudagraph_utilsr   'torch._inductor.runtime.cache_dir_utilsr   torch._inductor.utilsr   r   torch._subclassesr   %torch.fx.experimental.symbolic_shapesr   r   r   collections.abcr   r   r   r   r   	getLoggerr`   r   r   r=   rD   r   r   contextmanagerr   r   r   r*   r(   r%   <module>r     s*   "    	   # : . .  R , @ < G 6 , :  2/$ g!Ds DNpD, pDfV=* V=r3'
T  (  %$9$9'$9 	$9
 $9 
$9 $9 $9V 	
 
 
	
r(   