
    VhI                       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
mZ d dlmZ d dlZd dlmZmZ d dlmZ ddlmZmZmZmZmZ d	d
lmZ erddlmZ  ej:                  e      Ze e!e"f   Z#ddZ$ejJ                   G d d             Z& G d d      Z' G d d      Z(ddZ)ddZ*	 	 	 	 	 	 	 	 	 	 ddZ+ G d dee,         Z- G d dee         Z.ddZ/y)     )annotationsN)AnyOptionalTYPE_CHECKING)override)CacheArtifactManagerCacheArtifactType)
has_triton   )create_cache
JsonDataTyRemoteCacheRemoteCacheBackendRemoteCacheJsonSerde   )Config)Samplec                 R   ddl m}  d }t               r)	 t        j                  j
                  j                         }d }t        j                  j                  d}| j                  | j                  || j                  | j                  | j                         |dS # t        $ r Y iw xY w)Nr   configT)autotune_local_cacheautotune_remote_cachebackend_hashbundled_autotune_remote_cachecoordinate_descent_tuning	is_fbcodeis_hip)torch._inductorr   r
   torchutils_tritontriton_hash_with_backendRuntimeErrorversionhipr   r   r   r   r   )r   r   r   s      V/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/runtime/autotune_cache.pyinductor_meta_from_configr'   #   s    &L|	 ;;..GGIL F}}$ !' ; ;!'!=!=$)/)M)M%+%E%E%%'   	 	s   (B 	B&%B&c                      e Zd ZU ded<   dZded<   dZded<   e	 	 	 	 	 	 	 	 dd       Zedd       Zdd	Z		 	 	 	 	 	 dd
Z
	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZddZddZ	 d	 	 	 	 	 	 	 ddZy)AutotuneCachestrconfigs_hashNz-Optional[tuple[RemoteCache[JsonDataTy], str]]local_cacheremote_cachec                    t        |      }t         j                  |      }|j                  | t        j                  j                  |      |       |j                  | |       |j                  s|j                  r|S y N)	r)   _prepare_key_setup_local_cacheospathdirname_setup_remote_autotune_cacher,   r-   )inductor_metafilenamer+   cachekeys        r&   createzAutotuneCache.createE   sf     l+((2  0I3O**=#> 2 2L    c                    ddl m} t        j                  j	                  |        d|j
                   }t        j                  |j                  d            j                         S )Nr   r   :utf-8)
torch.compilerr   r2   r3   basenamecache_key_taghashlibsha256encode	hexdigest)r7   cconfigr9   s      r&   r0   zAutotuneCache._prepare_keyR   sO    4 !!(+,Ag.C.C-DE~~cjj12<<>>r;   c                    | j                   x}r*|\  }}|j                  |      x}rt        |t              r|S | j                  x}r*|\  }}|j                  |      x}rt        |t              r|S y r/   )r,   get
isinstancedictr-   )selfr,   r8   r9   best_configr-   s         r&   _readzAutotuneCache._read[   s    ***;*$JE3#iin,{,k40&&,,,<,%JE3#iin,{,k40&&r;   c                X    | j                         x}rt        || j                  ||      S y r/   )rM   _load_cached_autotuningr+   )rK   r6   configsbests       r&   	read_bestzAutotuneCache.read_bestl   s6     ::<4*d''-  r;   c                `    |j                  dd      sy | d| d}t               }||f| _        y )Nr   T/z.best_config)rH   LocalAutotuneCacher,   )rK   r6   r4   	cache_keycache_filenamer,   s         r&   r1   z AutotuneCache._setup_local_cachev   s?       !7>#9Ai[=(*'8r;   c                   t        |      sy |j                  dd       x}t        j                  d       y t	        |t
              sJ t        |j                  dd            }d}|| j                  z   |z   }t        j                  |j                  d            j                         }t        ||dd      }|sy || _        || _        ||f| _        y )	Nr   zTbackend_hash is not passed on the inductor_meta, unable to use autotune remote cacher   Fzautotune-best-config-v2r>   FbRemoteAutotuneCacheRemoteAutotuneCache)!_should_use_remote_autotune_cacherH   logdebugrI   r*   boolr+   rB   rC   rD   rE   r   remote_cache_full_keyr   r-   )rK   r6   rV   r   r   saltr9   r-   s           r&   r5   z*AutotuneCache._setup_remote_autotune_cache   s     1?)--ndCCLLIIf ,,,,**;>?	(T...5nnSZZ01;;=##!	
  &)"")95r;   c                N    t        | dd       }i | j                  d|xr |d   iS )Nr-   r   )getattr__dict__)rK   r-   s     r&   __getstate__zAutotuneCache.__getstate__   s=     t^T:
mm
 L<\!_
 	
r;   c                p   | j                   j                  |       | j                  t        | j                  t              sJ t        | d      sJ t        | d      sJ | j                  }t        | j                  | j                  dd      }|
||f| _        y t        j                  d       d | _        y y )Nr_   r   rY   rZ   z7Warning, failed to recreate remote cache after pickling)rc   updater-   rI   r*   hasattrr   r_   r   r\   warning)rK   staterV   r-   s       r&   __setstate__zAutotuneCache.__setstate__   s    U#(d//5554!89994---))I'**'%	L '%19$=!UV$(! )r;   c                8   i |j                   |j                  |j                  | j                  ||dz  d}| j                  x}r|\  }}|j                  ||       t        j                  ||       t        j                  t        j                  t        j                  j                  |      |       t        j                  t         j"                        r|rdnd}t        j%                  d||       | j&                  x}	r|	\  }}|j                  ||       y y )Ni@B )	num_warps
num_stagesr+   found_by_coordesctime_taken_mscoordesc	heuristiczSave %s tuning result to %s)kwargsrl   rm   r+   r,   putAutotuneCacheBundlerr   record_artifactr	   AUTOTUNEr2   r3   r@   r\   isEnabledForloggingDEBUGr]   r-   )
rK   r   time_taken_nsrn   datar,   r8   r9   type_strr-   s
             r&   savezAutotuneCache.save   s    
mm
)) ++ --!2*g5
 ***;*$JE3IIc4  $$S$/ 00!**BGG,<,<S,A4 .)::		73G,,,<,%JE3IIc4  -r;   )r6   _InductorMetaTyr7   r*   r+   r*   returnzOptional[AutotuneCache])r7   r*   r   r*   )r   zOptional[dict[str, JsonDataTy]])r6   r~   rP   list[Config]r   Optional[Config])r6   r~   r4   r*   rV   r*   r   None)r6   r~   rV   r*   r   r   )r   dict[str, Any])ri   r   r   r   )F)r   r   rz   intrn   r^   r   r   )__name__
__module____qualname____annotations__r,   r-   staticmethodr:   r0   rM   rR   r1   r5   rd   rj   r}    r;   r&   r)   r)   >   s   AEK>EBFL?F 
&
25
EH
	 
 
 ? ?",7C	9,97:9GJ9	9 6, 69< 6	 6N
), MR!!-0!EI!	!r;   r)   c                      e Zd ZU dZded<   ded<   ded<   ddZdd	Zdd
ZddZe		 	 	 	 dd       Z
ddZedd       Zedd       Zy)_AutotuneCacheBundlerImplz[
    Caches a set of LocalAutotuneCacheBackend entries together in a single
    cache.
    r*   _keyRemoteCache[JsonDataTy]_cachedict[str, JsonDataTy]_entriesc                ~    | j                   r1| j                  j                  | j                  | j                          y y r/   )r   r   rs   r   rK   s    r&   end_compilez%_AutotuneCacheBundlerImpl.end_compile   s(    ==KKOODIIt}}5 r;   c                "    || j                   |<   y r/   )r   )rK   r@   r{   s      r&   rs   z_AutotuneCacheBundlerImpl.put   s     #'hr;   c                .    || _         || _        i | _        y r/   )r   r   r   )rK   r9   r8   s      r&   __init__z"_AutotuneCacheBundlerImpl.__init__   s    	r;   c                     y r/   r   r   s    r&   syncz_AutotuneCacheBundlerImpl.sync   s     	r;   c                `   |j                  dd      sy|j                  d      x}	 t        |      S | j                  |      syt        j                  j                         ry|j                  d      ry	 ddlm} t        j                  j                  d      }||k\  S # t        $ r Y yw xY w)	Nr   TFr   r   r   REMOTE_CACHE_VERSIONz:pytorch/remote_cache:bundled_autotune_remote_cache_version)
rH   r^   _get_is_fbcoder   _utils_internalis_fb_unit_testtorch._inductor.fb.remote_cacher   ModuleNotFoundErrorjustknobs_getval_int)clsr6   r   r   jks        r&   )_should_use_bundled_autotune_remote_cachezC_AutotuneCacheBundlerImpl._should_use_bundled_autotune_remote_cache  s       !7> .;->->/. ) 	
 566!!-0  002X&	L ""77H
 $r)) # 		s   7B! !	B-,B-c                   ddl m} | j                  j                  | j                        }|t        |t              syd}|j                         D ]v  \  }}t        |      \  }}|j                  ||      \  }}}	t        |t              r!|j                  d      x}
r|t        |
      z  }t               }|j                  |	|       x |j                  |       y)Nr   )	codecacheFtime_saved_nsT)r   r   r   rH   r   rI   rJ   items_splitext_nodotget_pathr   rU   rs   .add_ephemeral_timeout_increase_for_distributed)rK   r   entriesr   r@   r{   rootext_r7   tsnsr,   s               r&   _load_cachez%_AutotuneCacheBundlerImpl._load_cache&  s    - ++//$)),?*Wd";  %mmo 	,NHd'1ID#&//c:NAq($%488O3L+L4+LT*,.KOOHd+	, 	@@Or;   c                8    t        | j                  dd            S )Nr   F)r^   rH   )r6   s    r&   r   z(_AutotuneCacheBundlerImpl._get_is_fbcode?  s    M%%k59::r;   c                4    | d   }t        |t              sJ |S )Nr   )rI   r*   )r6   r   s     r&   _get_backend_hashz+_AutotuneCacheBundlerImpl._get_backend_hashC  s"    $^4,,,,r;   Nr   r   )r@   r*   r{   r   r   r   )r9   r*   r8   r   r   r   r6   r~   r   r^   )r   r^   )r6   r~   r   r*   )r   r   r   __doc__r   r   rs   r   r   classmethodr   r   r   r   r   r   r;   r&   r   r      s    
 I## $#6
'


 *+*	* *B2 ; ;  r;   r   c                      e Zd ZU dZded<   d
dZeddd	 	 	 	 	 	 	 dd       Zed
d       Zed
d       Z	edd	       Z
y)rt   Nz#Optional[_AutotuneCacheBundlerImpl]_bundlerc                     y r/   r   r   s    r&   r   zAutotuneCacheBundler.__init__M  s    r;   )code	code_hashc                  | j                   J ||J d       t        |      }|J t        j                  |      sy t	        dt        j                  |      dd      }|sy d}t        j                  |      }||z   |z   }t        j                  |j                  d            j                         }t        ||      }|j                         s|| _         y y )Nz&Cannot specify both code and code_hashzbundled-autotune-v1FbRemoteBundledAutotuneCacheRemoteBundledAutotuneCachez bundled-autotune-best-configs-v1r>   )r   _comment_stripped_hashr   r   r   r   r   rB   rC   rD   rE   r   )	r   r6   r   r   r8   r`   r   r9   bundlers	            r&   begin_compilez"AutotuneCacheBundler.begin_compileS  s     ||###$N&NN$.t4I$$$(RR
 !%44]C*(	
  20BB=Q ,&-nnSZZ01;;=+C7""$ #CL %r;   c                P    | j                   x}rd | _         |j                          y y r/   )r   r   r   r   s     r&   r   z AutotuneCacheBundler.end_compile  s)    ll"7"CL! #r;   c                B    | j                   x}r|j                          y y r/   )r   r   r   s     r&   r   zAutotuneCacheBundler.sync  s    ll"7"LLN #r;   c                    | j                   x}r2t        j                  j                  |      }|j	                  ||       y y r/   )r   r2   r3   r@   rs   )r   r7   r{   r   r@   s        r&   rs   zAutotuneCacheBundler.put  s<    ll"7" ww''1H KK$' #r;   r   )r6   r~   r   Optional[str]r   r   r   r   )r7   r*   r{   r   r   r   )r   r   r   r   r   r   r   r   r   r   rs   r   r;   r&   rt   rt   J  s    48H18 
 ##'0#&0# 	0#
 !0# 
0# 0#p " "
   
( 
(r;   rt   c                    t        j                  dd| dt         j                        } t        j                  j
                  j                  |       S )Nz#.*$ r   )countflags)resub	MULTILINEr   	_inductorr   r   )r   s    r&   r   r     s7    66'2t1BLLAD??$$..t44r;   c                4   | j                  d      x}t        |      S | j                  d      syt        j                  j	                         ry| j                  d      ry	 ddlm} |t        j                  j                  d      k\  S # t        $ r Y yw xY w)Nr   r   Fr   r   r   z.pytorch/remote_cache:autotune_memcache_version)	rH   r^   r   r   r   r   r   r   r   )r6   r   r   s      r&   r[   r[     s    ##$;<<IF|[),,."H  5#8#8#M#M8$    s   #B 	BBc                2     y  j                  dd       |k7  ry  j                  dd        |j                  d      rK j                  dd      r9 j                  d      } j                  d      }t         ||      }d	|_        |S |D cg c]l  }t	         fd
|j
                  j                         D              r>|j                   j                  d      k(  r |j                   j                  d      k(  r|n }}t        |      dk7  ry |d   S c c}w )Nr+   ro   r   rn   Frl   rm   )rl   rm   Tc              3  L   K   | ]  \  }}|j                  |      k(    y wr/   )rH   ).0r9   valrL   s      r&   	<genexpr>z*_load_cached_autotuning.<locals>.<genexpr>  s#     NxsCskooc**Ns   !$r   r   )
poprH   r   rn   allrr   r   rl   rm   len)	rL   r+   rP   r6   rl   rm   triton_configcfgmatching_configss	   `        r&   rO   rO     s    ~t,< OOOT*45+//U;  OOK0	 __\2
{iJW*.' N3::;K;K;MNNMM[__[99NNkool;;	 	  !As   A1Dc                  0    e Zd Zedd       Zedd       Zy)_LocalAutotuneCacheBackendc                    	 t        |d      5 }|j                         cd d d        S # 1 sw Y   y xY w# t        $ r Y y w xY w)Nrb)openreadFileNotFoundError)rK   r9   fds      r&   _getz_LocalAutotuneCacheBackend._get  sA    	c4 !Bwwy! ! !  		s"   4 (	4 14 4 	A A c                    t        j                  t         j                  j                  |      d       t	        |d      5 }|j                  |       d d d        y # 1 sw Y   y xY w)NT)exist_okwb)r2   makedirsr3   r4   r   write)rK   r9   r{   r   s       r&   _putz_LocalAutotuneCacheBackend._put  sH    
BGGOOC(48#t_ 	HHTN	 	 	s   AA%N)r9   r*   r   zOptional[bytes])r9   r*   r{   bytesr   r   )r   r   r   r   r   r   r   r;   r&   r   r     s(       r;   r   c                  L     e Zd Zd fdZed fd       Zed fd       Z xZS )rU   c                N    t               }t               }t        |   ||       y r/   )r   r   superr   )rK   backendserde	__class__s      r&   r   zLocalAutotuneCache.__init__  s"    ,.$&%(r;   c                (   t         j                          t        |   ||      }|jt	        |t
              sJ t         j                  ||       t        j                  t        j                  t        j                  j                  |      |       |S r/   )rt   r   r   r   rI   rJ   rs   r   ru   r	   rv   r2   r3   r@   )rK   r9   sampleresultr   s       r&   r   zLocalAutotuneCache._get  sx    !!#c6*fd+++ !$$S&1 00!**BGG,<,<S,A6 r;   c                T    t         j                  ||       t        |   |||       y r/   )rt   rs   r   r   )rK   r9   valuer   r   s       r&   r   zLocalAutotuneCache._put  s#      e,S%(r;   r   )r9   r*   r   Optional[Sample]r   zOptional[JsonDataTy])r9   r*   r   r   r   r   r   r   )r   r   r   r   r   r   r   __classcell__)r   s   @r&   rU   rU     s0    )
    ) )r;   rU   c                \    t         j                  j                  |       \  }}|r|dd  }||fS )Nr   )r2   r3   splitext)r@   r   r   s      r&   r   r     s2      *ID#
!"g9r;   )r   r~   )r   r*   r   r*   r   )
rL   r   r+   r*   rP   r   r6   r~   r   r   )r@   r*   r   ztuple[str, str])0
__future__r   dataclassesrB   rx   r2   os.pathr   typingr   r   r   typing_extensionsr   r   torch.compiler._cacher   r	   torch.utils._tritonr
   r-   r   r   r   r   r   triton_compatr   r   	getLoggerr   r\   rJ   r*   objectr~   r'   	dataclassr)   r   rt   r   r[   rO   r   r   rU   r   r   r;   r&   <module>r     s   "    	  	 / / &  I *  " %g! sF{#6 b! b! b!Jc cLX( X(z5
(!&!! ! #	!
 !H!3E!:  )Z0 ):r;   