
    Vh+                        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	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j6                  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j6                  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Optional)mock)is_cpu_device   )	cache_dir	namespacedevicereturnc                 :    t        t                     dz  | z  |z  S )N
aoti_eager)r   r
   )r   r   s     J/home/dcms/DCMS/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}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_lockr       s:    D.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_cacherH       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)intr9   int32floatbool r   r   #supported_builtin_dtype_torch_dtyperP   T   s!    eU[[$

CCr   .c                  H    t               } t        | j                               S rJ   )rP   tuplekeys)type_to_torch_dtypes    r   supported_scalar_typesrU   X   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)
isinstancer9   Tensorr   typer   indexr,   listsizestrider[   _C_dispatch_keysraw_repr)rV   rW   rD   s      r   extract_tensor_metadatarg   ]   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]   r9   r^   appendrg   )rV   rW   metadata_listrB   rD   s        r   extract_tensor_list_metadatarl   o   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]   rU   rP   r_   )r"   rW   rD   rT   s       r   extract_scalar_metadataro   }   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rW   rD   s     r   extract_string_metadatart      s&    eS!!!!H$H^Or   c                 N    t        | t        j                        sJ i }|  |d<   |S )Nr-   )r]   r9   r,   rs   s     r   extract_dtype_metadatarv      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]   r9   r   r_   r`   rs   s     r   extract_device_metadatarz      s@    eU\\***!H',zzlH !%*[[H!"Or   c                 N    t        | t        j                        sJ i }|  |d<   |S )Nr/   )r]   r9   layoutrs   s     r   extract_layout_metadatar}      s,    eU\\***!H"'H^Or   F)dynamic_shapesoptionsremove_runtime_assertionsdisable_constraint_solverr@   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
      }t        |t(              sJ g }t+        |      D ]:  \  }}t        |t"        j,                        rt/        ||      }nt        |t               r!t        d |D              sJ t1        ||      }nt        |t3                     rt5        ||      }nt        |t(              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 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# 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rJ   )	r]   rU   r9   r^   ra   rr   r,   r   r|   ).0rW   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rJ   r]   r9   r^   r   rB   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rJ   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:  ),ra   valuesallr<   r=   r7   r]   r   r1   mkdirr   patchdictr   environabsoluter5   r9   _exportaot_compilerr   	enumerater^   rg   rl   rU   ro   rt   r,   rv   r   rz   r|   r}   r_   rj   r   relative_tor    r2   r3   r4   r;   dump)r!   r   r"   rV   r@   r   r   r~   r   r   r   flattened_inputsrG   rW   persistent_cachepersistent_cache_libkernel_lib_pathkernel_metadata_itemsidxrD   kernel_meta_inforA   update_jsonr?   modeop_conf_filerB   rF   s                               r   "aoti_compile_with_persistent_cacher      s^   " ;;;;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   QG P!6PO=O,1O=O='P8P	P	P!,O:	7O=9O:	:O==PP	PPP	P!!	Q*QQQQQ%)+r3   loggingr   pathlibr   typingr   r   r   unittestr   r9   torch._exporttorch._inductor.utilsr   runtime.runtime_utilsr
   	getLogger__name__r<   rr   r   r    ra   r   rH   r_   r,   rP   rR   rU   rN   r^   rg   rl   ro   rt   rv   r   rz   r|   r}   r   rO   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(,&+&+CC #C C 	C
 SC *C cNC T#s(^,C d38n%C  $C  $C 	Cr   