
    Vh                       d dl mZ d dlZd dlZd dlmZmZ d dlZd dlm	Z	m
Z
mZ erd dlmZ ddlmZ ddZdd	Zdd
Zd dZd dd!dZd"dZd#dZ	 	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZd&dZ	 d dlZdZerd'dZnd'dZd(dZd(dZd(dZd(dZ d)dZ!ejD                  jF                  jH                  Z$d*dZ%d+dZ&y# e$ r dZdZY Sw xY w),    )annotationsN)AnyTYPE_CHECKING)	cache_dirdefault_cache_dirtriton_cache_dir)Hashable   )Configc                 v    t        j                  t        j                  | D cg c]  }|s|	 c}      S c c}w N)	functoolsreduceoperatormul)argsxs     U/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/runtime/runtime_utils.pyconditional_productr      s)    HLLd*@a1*@AA*@s   6
6
c                    | | z   S r    )numerdenoms     r   ceildivr      s    uf_    c                &    | dkD  xr | | dz
  z  dk(  S )z.Returns whether n = 2 ** m for some integer m.r   r
   r   ns    r   is_power_of_2r      s    q5#QQY!^#r   c                z    | dz  } | | dz	  z  } | | dz	  z  } | | dz	  z  } | | dz	  z  } | | dz	  z  } | | dz	  z  } | dz  } | S )z9Return the smallest power of 2 greater than or equal to nr
                   r   r   s    r   next_power_of_2r&   "   sg    FAaKAaKAaKAaKAbLAbLAFAHr   )num_in_out_argsc                >     t         fdt        |      D              S )z
    Return the total number of bytes the arguments of tensor type takes.

    For in/out args, tensor sizes are counted twice: once for reading and
    once for writing.

    The first num_in_out_args arguments are in out tensors.
    c              3     K   | ]T  \  }}t        |t        j                        r5|j                         |j	                         z  d t        |k        z   z   V yw)r
   N)
isinstancetorchTensornumelelement_sizeint).0iargr'   s      r   	<genexpr>z get_num_bytes.<locals>.<genexpr>8   sS      Asc5<<( 			c&&((AA4G0H,HIs   AA)sum	enumerate)r'   r   s   ` r   get_num_bytesr6   /   s#      o  r   c                    t        | j                  j                               }|j                  d| j                  f       |j                  d| j
                  f       t        |      S )z~
    Convert triton config to a tuple that can uniquely identify it. We can use
    the return value as a dictionary key.
    	num_warps
num_stages)sortedkwargsitemsappendr8   r9   tuple)cfgr<   s     r   triton_config_to_hashabler@   ?   sO    
 3::##%&E	LL+s}}-.	LL,/0<r   c                ,    t        | dd       J d       y )Npre_hookz+triton configs with pre_hooks not supported)getattr)r?   s    r   validate_triton_configrD   J   s#     3
D)1 51r   Tc                `    | | dd|dd|dd| }| dkD  xr |dk  }|r|rt        |      S |S )Nz.3fzms    	z GB 	 z7.2fzGB/sg~jt?i  )red_text)msnum_gbgb_per_sprefixsuffixcolorinfo_strslows           r   create_bandwidth_info_strrO   U   sQ     "S&WXdO4PVxXH:((S.D!&48H=X=r   c                      y)Ni  r   r   r   r   get_max_y_gridrQ   b   s    r   Fc                    t        t        j                  |j                               | z   t        j                  j                  z   S r   )rC   coloramaForeupperRESETmsgrL   s     r   _color_textrY   q   s-    x}}ekkm4s:X]]=P=PPPr   c                    | S r   r   rW   s     r   rY   rY   v   s    
r   c                    t        | d      S )NgreenrY   rX   s    r   
green_textr_   z   s    sG$$r   c                    t        | d      S )Nyellowr]   r^   s    r   yellow_textrb   ~   s    sH%%r   c                    t        | d      S )Nredr]   r^   s    r   rF   rF      s    sE""r   c                    t        | d      S )Nbluer]   r^   s    r   	blue_textrg      s    sF##r   c                d    |D ]  }t        | |      st        | |      c S  t        |  d|       )zX
    Return the first available attribute or throw an exception if none is present.
    z% does not has any of the attributes: )hasattrrC   AssertionError)objattrsattrs      r   get_first_attrrn      sC      &33%%& C5 EeWM
NNr   c                    	 ddl m}  ||       S # t        $ r% 	 ddl m}  ||       cY S # t        $ r | cY cY S w xY ww xY w)Nr   )_base64)_base32)triton.runtime.cacherp   	Exceptionrq   )keyrp   rq   s      r   triton_hash_to_path_keyru      sI    
0s| 	43< 	J	s     	>*>:>:>c                    	 t         j                  j                  |       S # t        $ r!}t        d|  d|j                         |d}~ww xY w)zT
    Compiles shader source but raise more actionable error message when needed
    zfailed to compile z with N)r+   mpscompile_shaderSyntaxErrorrX   )sourceerrs     r   compile_mps_shaderr|      sN    Qyy''// Q.vhfSWWIFGSPQs   ! 	AAA)r   r/   returnr/   )r   r/   r   r/   r}   r/   )r   r/   r}   bool)r   r/   r}   r/   )r   ztorch.Tensorr'   r/   r}   r/   )r?   r   r}   r	   )r?   r   r}   None) r   T)rG   floatrH   r   rI   r   rJ   strrK   r   rL   r~   r}   r   )r}   r/   )rX   r   rL   r   r}   r   )rX   r   r}   r   )rk   r   rl   r   r}   r   )rt   r   r}   r   )rz   r   r}   r   )'
__future__r   r   r   typingr   r   r+   'torch._inductor.runtime.cache_dir_utilsr   r   r   collections.abcr	   triton_compatr   r   r   r   r&   r6   r@   rD   rO   rQ   rS   HAS_COLORAMAModuleNotFoundErrorrY   r_   rb   rF   rg   rn   _dynamoutilsdynamo_timedru   r|   r   r   r   <module>r      s   "   %   (%B$

 ?@   
>
>
> 
> 	
>
 
> 
> 	
>L Q
%&#$O }}""//,QI  LHs   ,B: :	CC