
    9j0                     `   U d dl 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 d dlZd dlZd dlmZ dd	lmZ  ej&                  e      Ze G d
 d             Zi Zeeef   ed<   dededef   deeeegeeee
f   dz     f   ddfdZdededefdZdede
fdZdedededeeee
f   dz     fdZdee ejB                  f   fdZ"de#e df   fdZ$de%dejL                  deee
f   fdZ'de%deejL                     deee
f   fdZ(dede
deee
f   fd Z)dedeee
f   fd!Z*dejB                  deee
f   fd"Z+dejX                  deee
f   fd#Z-dej\                  deee
f   fd$Z/ddd%d%d&dededede%d'ede
f   d(e#e
   d)eee
f   d*eee
f   dz  d+eee
f   dz  d,e%d-e%defd.Z0y)/    N)Callable)	dataclass)Path)Any)mock)is_cpu_device   )	cache_dirc                   R    e Zd ZU edef   ed<   eeeegeeeef   dz     f   ed<   y)AOTICompileBackend.
compile_fnNload_fn)	__name__
__module____qualname__r   str__annotations__listdictr        Z/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_inductor/aoti_eager.pyr   r      s9    c""sCotDcNT,A'BBCCr   r   _aoti_compile_backendsdevice_typer   .r   returnc                 ,    t        ||      t        | <   y )N)r   r   )r   r   )r   r   r   s      r   register_aoti_compile_backendr      s    
 +=+;'r   	namespacedevicec                 :    t        t                     dz  | z  |z  S )N
aoti_eager)r   r
   )r   r   s     r   aoti_eager_cache_dirr"   (   s    	|+i7&@@r   op_func_name_with_overloadc                     ddl m}m} ddlm} |  d} |       } |t
        j                  j                  ||      |      S )Nr   )get_lock_dirLOCK_TIMEOUT)FileLockz.lock)timeout)torch._inductor.codecacher%   r&   torch.utils._filelockr'   ospathjoin)r#   r%   r&   r'   op_conf_lock_filelock_dirs         r   aoti_eager_op_conf_lockr0   ,   s:    D.56e<~HBGGLL+<=|TTr   nsc                    t         j                  |      }|r|j                  | ||      S t        | |      }|| dz  }|j	                         sg S 	 t        |      5  t        |      5 }t        j                  |      }|D ]  }||d   z  }	|	j                         |d<   |	j	                         sg c cd d d        cd d d        S |d   D ]  }
|
j                  d      rt        d      d|
v r|
d   dk(  rd|
d	<   d
D ]/  }||
v st        t        |
|   j                  d      d         |
|<   1 d|
v r(t        t        |
d   j                  d      d         |
d<   d|
v st        t        |
d   j                  d      d         |
d<     |cd d d        cd d d        S # 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY w# t        $ r&}d| }t        j!                  |       g cY d }~S d }~ww xY w)N.jsonkernel_path	meta_info
is_dynamic!Only support static shape for nowr   cpudevice_index)dtypedtype_value.layout_valuememory_format_valuez!Failed to load aoti eager cache: )r   getr   r"   existsr0   openjsonloadas_posixNotImplementedErrorgetattrtorchsplit	Exceptionlog	exception)r1   r#   r   backenddevice_kernel_cacheop_conff	json_dataitemkernel_lib_abs_pathmetadata	dtype_keyeerr_msgs                 r   load_aoti_eager_cacherX   6   sG    %((5Gr#={KK.r;?!'A&B%$HHG>>	)$%?@ $	!g #!! IIaL	% D*=]@S*S'*=*F*F*HD' /557!	#! #!$	! $	! %)$5 #<<5"5 C#  *X5 ( 75 @79H^4)A "I(H46=$)8I+>+D+DS+I"+M7" 3"
 *X57> %x'?'E'Ec'J2'N8H^4 1H<>E %x0E'F'L'LS'QRT'U?H%:;)B !G#! #!$	! $	!#! #! #!$	! $	! $	!J  5aS9g	sz   G F7)A
F!3	F7<	G >F!AF! .F!	F7	G !F*	&F7.G 7G <G  G 	G2G-'G2-G2c                  ~    t         t        j                  t        t        j                  t        t        j                  iS N)intrH   int32floatboolr   r   r   #supported_builtin_dtype_torch_dtyper_   n   s!    eU[[$

CCr   c                  H    t               } t        | j                               S rZ   )r_   tuplekeys)type_to_torch_dtypes    r   supported_scalar_typesrd   r   s     =?$))+,,r   dynamicinputc                    i }| |d<   t        |t        j                        sJ |j                  j                   |d<   t        |g      rd|d<   n|j                  j                  |d<   |j                   |d<   t        |j                               |d<   t        |j                               |d<   |j                  |d<   t        j                  j                  |      j                         |d	<   |S )
Nr6   r   r9   r:   r;   sizesstridesrequires_graddispatch_key_set)
isinstancerH   Tensorr   typer   indexr;   r   sizestriderj   _C_dispatch_keysraw_repr)re   rf   rT   s      r   extract_tensor_metadataru   w   s    !H$H\eU\\***!&!2!2 3H]eW#% #(<<#5#5  ;;-HWUZZ\*HWu||~.HY % 3 3H_#(88#:#:5#A#J#J#LH Or   c                     g }|D ]9  }t        |t        j                        sJ |j                  t	        | |             ; i }||d<   |S )Ntensor_list)rl   rH   rm   appendru   )re   rf   metadata_listrR   rT   s        r   extract_tensor_list_metadatarz      sZ     M E$---4WdCDE  "H+H]Or   c                     t        |t                     sJ i }d|d<   | |d<   | dk(  rdnd|d<   t               }|t        |          |d<   ||d	<   |S )
NFr6   r   r8   r9   r   r:   r;   scalar_value)rl   rd   r_   rn   )r   rf   rT   rc   s       r   extract_scalar_metadatar}      sp    e35666!H"H\)H]%0E%9rqH^=?.tE{;<HW$H^Or   c                 8    t        | t              sJ i }| |d<   |S )Nstring_value)rl   r   rf   rT   s     r   extract_string_metadatar      s&    eS!!!!H$H^Or   c                 N    t        | t        j                        sJ i }|  |d<   |S )Nr<   )rl   rH   r;   r   s     r   extract_dtype_metadatar      s,    eU[[)))!H!&H]Or   c                     t        | t        j                        sJ i }| j                   |d<   | j                  |d<   |S )Ndevice_type_valuedevice_index_value)rl   rH   r   rn   ro   r   s     r   extract_device_metadatar      s@    eU\\***!H',zzlH !%*[[H!"Or   c                 N    t        | t        j                        sJ i }|  |d<   |S )Nr>   )rl   rH   layoutr   s     r   extract_layout_metadatar      s,    eU\\***!H"'H^Or   Fdynamic_shapesoptionsremove_runtime_assertionsdisable_constraint_solverrP   argskwargsr   r   r   r   c                6	   t         j                  |      }|r|j                  | |||||||||	|
      S |rJ d       t        |      t        |j	                               z   }t        d |D              s%d| }t        j                  |       t        |      |D ]J  }t        |t              st        d |D              r'd| }t        j                  |       t        |       t        | |      }|j                         s|j                  d       |d	z  }|j                         s|j                          t        j                  j                  t         j"                  d
|j%                         j'                         i      5  	 t(        j*                  j-                  |||||	|
d      }t        |t.              sJ g }t1        |      D ]:  \  }}t        |t(        j2                        rt5        ||      }nt        |t              r!t        d |D              sJ t7        ||      }nt        |t9                     rt;        ||      }nt        |t.              rt=        |      }nt        |t(        j>                        rtA        |      }nct        |t(        jB                        rtE        |      }n=t        |t(        jF                        rtI        |      }nt        dtK        |             ||d<   |jM                  |       = i }||d<   tO        |      jQ                  |      j'                         |d<   g }d}|| dz  }|j                         rdnd}tS        |      5  tU        ||      5 }	 tW        jX                  |      }t        |t              sJ |D ]   }t        |t              sJ |d   |k(  sd} n ddd       |r>|jM                  |       tU        |d      5 }tW        j\                  ||d       ddd       ddd       |cddd       S # tZ        $ r g }Y w xY w# 1 sw Y   mxY w# 1 sw Y   9xY w# 1 sw Y   =xY w# tZ        $ r/}d| d| }t        j                  |       Y d}~ddd       yd}~ww xY w# 1 sw Y   yxY w)zO
    Compile the given function with persistent cache for AOTI eager mode.
    r   r7   c              3      K   | ]_  }t        |t               t        j                  t        t
        t        j                  t        j                  t        j                  f       a y wrZ   )	rl   rd   rH   rm   r   r   r;   r   r   ).0rf   s     r   	<genexpr>z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   sO        	&(	
s   A%A'zUnsupported input types: c              3   P   K   | ]  }t        |t        j                           y wrZ   rl   rH   rm   r   rR   s     r   r   z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   s       /
/3JtU\\*/
   $&z<_impl_with_aoti_compile encounters unsupported input types: T)parentslibTORCHINDUCTOR_CACHE_DIRF)r   r   r   same_signaturec              3   P   K   | ]  }t        |t        j                           y wrZ   r   r   s     r   r   z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   s     P$z$=Pr   zUnsupported input type: 	arg_orderr5   r4   r3   rwN   )indentzFailed to compile z:  )/r   r@   r   r   valuesallrK   rL   rF   rl   r"   rA   mkdirr   patchr   r+   environabsoluterE   rH   _exportaot_compiler   	enumeraterm   ru   rz   rd   r}   r   r;   r   r   r   r   r   rn   rx   r   relative_tor0   rB   rC   rD   rJ   dump)r1   r#   r   re   rP   r   r   r   r   r   r   rM   flattened_inputsrW   rf   persistent_cachepersistent_cache_libkernel_lib_pathkernel_metadata_itemsidxrT   kernel_meta_inforQ   update_jsonrO   modeop_conf_filerR   rV   s                                r   "aoti_compile_with_persistent_cacher      s   " %((5G!!&)&?&? " 
 	
 ;;;;DzD$99  &  ..>-?@g!'**! /eT"3 /
7</
 ,
 UUeTfgGMM'"%g../ ,B<""$t,+e3&&(""$	


	"$8$A$A$C$L$L$NO
 MI	#mm77-*C*C  % 8 O os333$&!'(89 7
UeU\\26wFHt,P%PPPP;GUKH'='?@6{EJHs+6u=Hu{{35e<Hu||46u=Hu||46u=H-0He.VWW(+%%,,X6)7, 02,A[)_%112BCLLN ]+ IK&,F+Gu)MMG!..*3D()CD E'4( "L'$(IIl$;	 &i666 ) ")$555,0EE*/K!"" $$%56gs+ E|		)\!DE#E( #SM Mr % '$&	'" " E E#E E*  	*+E*FbLGMM'"[M MT	UM Ms   R	G Q)Q6P08P1P0?P0'Q+P<Q	QP-	*P0,P-	-P00P95Q<QQQ	Q	RR:RRRR)1rC   loggingr+   collections.abcr   dataclassesr   pathlibr   typingr   unittestr   rH   torch._exporttorch._inductor.utilsr   runtime.runtime_utilsr
   	getLoggerr   rK   r   r   r   r   r   r   r   r"   r0   rX   rn   r;   r_   ra   rd   r^   rm   ru   rz   r}   r   r   r   r   r   r   r   r   r   r   <module>r      s     	 $ !      / , g! D D D
 9; S"445 :c" sCotDcNT,A'BBC 
	AC A A AU U U55),5;>5	$sCx.4
 5pDT$2C-D D-dCi 0 -
T %,, 4S> $ 
#s(^
 
S 
T#s(^ 
3 4S> %++ $sCx. 5<< DcN 5<< DcN   -1%)&+&+SS #S S 	S
 SS *S cNS cNT)S #s(^d"S  $S  $S 	Sr   