
    ɯwgZ+                        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m	Z	m
Z
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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ef         fdZd
eeej8                  f   fdZd
eedf   fdZdedej@                  d
eeef   fdZ!dede	ej@                     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j8                  d
eeef   fdZ%dejL                  d
eeef   fdZ'dejP                  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ef      d#e
eeef      d$ed%ed
efd&Z*y)'    N)Path)AnyCallableDictListOptionalTuple)mock)is_cpu_device   )	cache_dir	namespacedevicereturnc                 :    t        t                     dz  | z  |z  S )N
aoti_eager)r   r   )r   r   s     _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_inductor/aoti_eager.pyaoti_eager_cache_dirr      s    	|+i7&@@    op_func_name_with_overloadc                     ddl m} ddlm}m} |  d} |       } |t
        j                  j                  ||      |      S )Nr   )FileLock)get_lock_dirLOCK_TIMEOUTz.lock)timeout)filelockr   torch._inductor.codecacher   r   ospathjoin)r   r   r   r   op_conf_lock_filelock_dirs         r   aoti_eager_op_conf_lockr#      s=    ! E56e<~HBGGLL+<=|TTr   nsdevice_typec                    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   existsr#   openjsonloadas_posixgetNotImplementedErrorgetattrtorchsplit	Exceptionlog	exception)r$   r   r%   device_kernel_cacheop_conff	json_dataitemkernel_lib_abs_pathmetadata	dtype_keyeerr_msgs                r   load_aoti_eager_cacherK   !   s     /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	sw   F FA
E7		F	F >E7AE76.E7$	F-	F 7F 	<FF FF F 	G"G=GGc                  ~    t         t        j                  t        t        j                  t        t        j                  iS N)intr<   int32floatbool r   r   #supported_builtin_dtype_torch_dtyperS   U   s!    eU[[$

CCr   .c                  H    t               } t        | j                               S rM   )rS   tuplekeys)type_to_torch_dtypes    r   supported_scalar_typesrX   Y   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 )
Nr*   r%   r-   r.   r/   sizesstridesrequires_graddispatch_key_set)
isinstancer<   Tensorr   typer   indexr/   listsizestrider^   _C_dispatch_keysraw_repr)rY   rZ   rG   s      r   extract_tensor_metadatarj   ^   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)r`   r<   ra   appendrj   )rY   rZ   metadata_listrE   rG   s        r   extract_tensor_list_metadataro   p   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 )
NFr*   r%   r,   r-   r   r.   r/   scalar_value)r`   rX   rS   rb   )r%   rZ   rG   rW   s       r   extract_scalar_metadatarr   ~   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)r`   strrZ   rG   s     r   extract_string_metadatarw      s&    eS!!!!H$H^Or   c                 N    t        | t        j                        sJ i }|  |d<   |S )Nr0   )r`   r<   r/   rv   s     r   extract_dtype_metadatary      s,    eU[[)))!H!&H]Or   c                     t        | t        j                        sJ i }| j                   |d<   | j                  |d<   |S )Ndevice_type_valuedevice_index_value)r`   r<   r   rb   rc   rv   s     r   extract_device_metadatar}      s@    eU\\***!H',zzlH !%*[[H!"Or   c                 N    t        | t        j                        sJ i }|  |d<   |S )Nr2   )r`   r<   layoutrv   s     r   extract_layout_metadatar      s,    eU\\***!H"'H^Or   F)dynamic_shapesoptionsremove_runtime_assertionsdisable_constraint_solverrC   argskwargsr   r   r   r   c          
         |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
      }g }t)        |      D ]:  \  }}t        |t"        j*                        rt-        ||      }nt        |t               r!t        d |D              sJ t/        ||      }nt        |t1                     rt3        ||      }nt        |t4              rt7        |      }nt        |t"        j8                        rt;        |      }nct        |t"        j<                        rt?        |      }n=t        |t"        j@                        rtC        |      }nt        dtE        |             ||d<   |jG                  |       = i }||d<   tI        |      jK                  |      j!                         |d<   g }d}|| dz  }|j                         rdnd}tM        |      5  tO        ||      5 }	 tQ        jR                  |      }t        |t               sJ |D ]   }t        |t              sJ |d   |k(  sd
} n ddd       |r>|jG                  |       tO        |d      5 }tQ        jV                  ||d       ddd       ddd       |cddd       S # tT        $ r}g }Y d}~d}~ww xY w# 1 sw Y   txY w# 1 sw Y   @xY w# 1 sw Y   DxY w# tT        $ 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+   c              3      K   | ]_  }t        |t               t        j                  t        t
        t        j                  t        j                  t        j                  f       a y wrM   )	r`   rX   r<   ra   rd   ru   r/   r   r   ).0rZ   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rM   r`   r<   ra   r   rE   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rM   r   r   s     r   r   z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   s     P$z$=Pr   zUnsupported input type: 	arg_orderr)   r(   r'   rwN   )indentzFailed to compile z:  ),rd   valuesallr?   r@   r:   r`   r   r4   mkdirr
   patchdictr   environabsoluter8   r<   _exportaot_compile	enumeratera   rj   ro   rX   rr   ru   rw   r/   ry   r   r}   r   r   rb   rm   r   relative_tor#   r5   r6   r7   r>   dump)r$   r   r%   rY   rC   r   r   r   r   r   r   flattened_inputsrJ   rZ   persistent_cachepersistent_cache_libkernel_lib_pathkernel_metadata_itemsidxrG   kernel_meta_inforD   update_jsonrB   modeop_conf_filerI   rE   s                               r   "aoti_compile_with_persistent_cacher      sO   " ;;;;DzD$99  &  ..>-?@g!'**! /eT"3 /
7</
 ,
 UUeTfgGMM'"%g../ ,B<""$t,+e3&&(""$	


	"$8$A$A$C$L$L$NO
 LH	#mm77-*C*C  % 8 O %'!'(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( #QL Lp % '$&	'" " E E#E E*  	*+E*FbLGMM'"YL LR	SL Ls   QGP$P
1O23O1O2:O2?'P
&O>?P
	P	O/	#O*	%O2*O/	/O22O;7P
>PP

P	P	QQ	<Q	QQQ)+r6   loggingr   pathlibr   typingr   r   r   r   r   r	   unittestr
   r<   torch._exporttorch._inductor.utilsr   runtime.runtime_utilsr   	getLogger__name__r?   ru   r   r#   rK   rb   r/   rS   rX   rQ   ra   rj   ro   rr   rw   ry   r   r}   r   r   r   rR   r   r   <module>r      s     	  = =    / , g!AC A A AU U U11),1;>1	(4S>
"#1h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   04(,&+&+BB #B B 	B
 SB *B cNB T#s(^,B d38n%B  $B  $B 	Br   