
    `i+                     
   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j        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j        d
eeef         fdZ%dej&        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ef                  d#eeeef                  d$ed%ed
efd&Z*dS )'    N)Path)AnyCallableOptional)mock)is_cpu_device   )	cache_dir	namespacedevicereturnc                 J    t          t                                dz  | z  |z  S )N
aoti_eager)r   r
   )r   r   s     n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/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                            ||          |          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       sl    DDDDDDDD......5<<<|~~H8BGLL+<==|TTTTr   nsdevice_typec                 ^   t          | |          }|| dz  }|                                sg S 	 t          |          5  t          |          5 }t	          j        |          }|D ]O}||d         z  }|                                |d<   |                                sg c cd d d            cd d d            S |d         D ]}	|	                    d          rt          d          d|	v r|	d         dk    rd|	d	<   d
D ]=}
|
|	v r7t          t          |	|
                             d          d                   |	|
<   >d|	v r7t          t          |	d                             d          d                   |	d<   d|	v r7t          t          |	d                             d          d                   |	d<   Q|cd d d            cd d d            S # 1 swxY w Y   	 d d d            d S # 1 swxY w Y   d S # t          $ r+}d| }t                              |           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       sy    /r;??!'A$H$H$HHG>> 	)$%?@@ $	! $	!g #!! IaLL	%  D*=]@S*S'*=*F*F*H*HD' /5577 "!		#! #! #! #! #! #! #!$	! $	! $	! $	! $	! $	! $	! $	! %)$5  #<<55 "5 C# #  *X55 ( 75 @ @79H^4)A " "I(H446=$)8I+>+D+DS+I+I"+M7" 7" 3 *X557> %x'?'E'Ec'J'J2'N8 8H^4 1H<<>E %x0E'F'L'LS'Q'QRT'U? ?H%:;)0 !G#! #! #! #! #! #! #!$	! $	! $	! $	! $	! $	! $	! $	!#! #! #! #! #! #! #! #! #!$	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	! $	!J    9a99g						s~   G7 G*AG"G*.G7 ;C=G8G*G7 G	G*G	G*G7 *G..G7 1G.2G7 7
H, H'!H,'H,c                  l    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                  `    t                      } t          |                                           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          |	                                          |d<   t          |
                                          |d<   |j        |d<   t          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4H]eW 6#%  #(<#5  ;(HWUZZ\\**HWu||~~..HY % 3H_#(8#:#:5#A#A#J#J#L#LH Or   c                     g }|D ]A}t          |t          j                  sJ |                    t	          | |                     Bi }||d<   |S )Ntensor_list)r]   r9   r^   appendrg   )rV   rW   metadata_listrB   rD   s        r   extract_tensor_list_metadatarl   o   sh     M E E$-----4WdCCDDDD!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   }   s    e35566666!H"H\)H]%0E%9%9rrqH^=??.tE{{;=HW$H^Or   c                 B    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      s0    eU[)))))!H!&jH]Or   c                 l    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',zOH !%*[H!"Or   c                 N    t          | t          j                  sJ i }|  |d<   |S )Nr/   )r]   r9   layoutrs   s     r   extract_layout_metadatar}      s0    eU\*****!H"'zH^Or   F)dynamic_shapesoptionsremove_runtime_assertionsdisable_constraint_solverr@   argskwargsr~   r   r   r   c          
      z
   |r
J d            t          |          t          |                                          z   }t          d |D                       s.d| }t                              |           t          |          |D ]^}t          |t                     rGt          d |D                       s.d| }t                              |           t          |          _t          | |          }|                                s|	                    d           |dz  }|                                s|	                                 t          j                            t          j        d	|                                                                i          5  	 t"          j                            |||||	|
d
          }t          |t(                    sJ g }t+          |          D ]|\  }}t          |t"          j                  rt/          ||          }n0t          |t                     r,t          d |D                       sJ t1          ||          }nt          |t3                                rt5          ||          }nt          |t(                    rt7          |          }nt          |t"          j                  rt;          |          }nst          |t"          j                  rt?          |          }nIt          |t"          j                   rtC          |          }nt          dtE          |                     ||d<   |#                    |           ~i }||d<   tI          |          %                    |                                          |d<   g }d}|| dz  }|                                rdnd}tM          |          5  tO          ||          5 }	 tQ          j)        |          }n# tT          $ r g }Y nw xY wt          |t                     sJ |D ])}t          |t                    sJ |d         |k    rd
} n*ddd           n# 1 swxY w Y   |rT|#                    |           tO          |d          5 }tQ          j+        ||d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |cddd           S # tT          $ r8}d| d| }t                              |           Y d}~ddd           dS d}~ww xY w# 1 swxY w Y   dS )zO
    Compile the given function with persistent cache for AOTI eager mode.
    r(   c              3      K   | ]Y}t          |t                      t          j        t          t
          t          j        t          j        t          j        f          V  Zd S rJ   )	r]   rU   r9   r^   ra   rr   r,   r   r|   ).0rW   s     r   	<genexpr>z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   sm          	&((	
 	
     r   zUnsupported input types: c              3   J   K   | ]}t          |t          j                  V  d S rJ   r]   r9   r^   r   rB   s     r   r   z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   s?       /
 /
/3JtU\**/
 /
 /
 /
 /
 /
r   z<_impl_with_aoti_compile encounters unsupported input types: T)parentslibTORCHINDUCTOR_CACHE_DIRF)r~   r   r   same_signaturec              3   J   K   | ]}t          |t          j                  V  d S rJ   r   r   s     r   r   z5aoti_compile_with_persistent_cache.<locals>.<genexpr>   s.      PP$z$==PPPPP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.   " ;;;;;;DzzD$9$99   &     + A.>@@g!'***! / /eT"" 	/3 /
 /
7</
 /
 /
 ,
 ,
 	/ hUeggGMM'"""%g...+B<<""$$ -t,,,+e3&&(( %""$$$	

	"$8$A$A$C$C$L$L$N$NO
 
 M MI	#m77-*C*C  % 8  O os33333$&!'(899 7 7
UeU\22 X6wFFHHt,, XPP%PPPPPPPP;GUKKHH'='?'?@@ X6{EJJHHs++ 	X6u==HHu{33 X5e<<HHu|44 X6u==HHu|44 X6u==HH-.Ve.V.VWWW(+%%,,X6666/1,A[)_%%112BCCLLNN ]+ IK&,F)M)M)MMG!..**333D()CDD E E'4(( "L'$(Il$;$;		$ ' ' '$&			' &i66666 ) " ")$55555,0EEE*/K!E F" " " " " " " " " " " " " " "  E$$%5666gs++ E|	)\!DDDDE E E E E E E E E E E E E E E#E E E E E E E E E E E E E E E( #SM M M M M M M MT  	 	 	L+ELLLLGMM'"""222[M M M M M M M MT	UM M M M M M M M M Ms   T0H=S+S-Q%/PQ%P	Q%P	AQ%S%Q))S,Q)-*SR;/S;R??SR?SS+S	S+S	S++
T-5"T(T0(T--T00T47T4)+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 A A AU U U U U U11),1;>1	(4S>
"#1 1 1 1hDT$2C-D D D 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 C 	C
 SC *C cNC T#s(^,C d38n%C  $C  $C 	C C C C C Cr   