
    "h|,                         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
mZmZmZmZ d dlmZ i Zd Ze G d d             Z e	d	
       G d de             Z G d de      Zy)    N)ABCMetaabstractmethodabstractclassmethod)	dataclass)DictListTupleUnion)
ModuleTypec                 ,    | t         | j                  <   | S )z9
    Register a descriptor into the descriptor table
    )_descriptor_table__name__)clss    U/var/www/html/sandstorm/venv/lib/python3.12/site-packages/triton/backends/compiler.pyregister_descriptorr      s     '*cll#J    c                       e Zd ZdZdZddZd ZddZd Zde	fd	Z
de	fd
Zd Zd Zd Zed        Zedeeeef      fd       Zed        Zed        Zed        Zd Zy)AttrsDescriptoraG  
    This class handles compile-time properties for specific function parameters.

    Different backends can add more properties to the common ones. The class
    contains two fields:

    `arg_properties`: a dictionary containing the different compile-time properties for different
        parameters. I.e., the dictionary is a map from property names to parameter indices
        {
        "prop0": (0, 2, 3)
        "prop1": (0, 4, 5)
        }
        Different backends might need different properties on those paraemters to enable
        specific optimizations. The common compile time properties contained in this class
        are :
        - "tt.divisibility", i.e., is the given parameter divisible by 16
        - "tt.equal_to_1", i.e., is the given parameter an integer constant 1

    `property_values`: a dictionary containing the value of the different compile-time properties, like:
        {
            "prop0": val0
            "prop1": val1
        }

    `constant_properties`: a set containing the properties that can be used to determine if a parameter is constant

    )divisibility_16
equal_to_1arg_propertiesproperty_valuesconstant_propertiesNc                     i | _         i | _        t               | _        | j	                  ||       | j                  ||       | j                          y)a  
        Initialize the compile-time properties

        We can initialize the AttrsDescriptor class by passing the list of params
        of the function and their `values`. The function will try to apply the properties
        to the values and save the parameters in the `arg_properties` list. If we don't pass
        either the `params` or the `values` we should initialize the class via an alternative method
        (see `from_dict` or `from_hints`)
        N)r   r   setr   _add_common_properties_add_backend_properties_init_slotsselfparamsvaluess      r   __init__zAttrsDescriptor.__init__8   sJ     !!#&5 ##FF3$$VV4r   c                 @   d| j                   d<   d| j                   d<   | j                  j                  d       ||yt        |      t        |      k(  sJ t	        ||      D cg c]?  \  }}t
        j                  |      s|j                  s|j                  s|j                  A c}}| j                  d<   t	        ||      D cg c]2  \  }}t
        j                  |      r|j                  s|j                  4 c}}| j                  d<   yc c}}w c c}}w )z$ Add common compile-time properties    ztt.divisibility   ztt.equal_toN)r   r   addlenzipr   is_divisible_by_16do_not_specializedo_not_specialize_on_alignmentnumr   is_equal_to_1)r    r!   r"   paramargs        r   r   z&AttrsDescriptor._add_common_propertiesK   s   24././]+  $$]3N Fs6{*+* ),FF(;2
$%?a?abe?f++E4X4X II2
-. "&&1.
s,,S1%:Q:Q II.
M*2
.
s   'D&D7Dc                      y)zX This method is for different subclasses to implement their own compile-time properties N r   s      r   r   z'AttrsDescriptor._add_backend_propertiesd   s    r   c           	          | j                   j                         D ]=  \  }}t        | |j                  d      dz   t	        | j
                  |         z   |       ? y)z$ Initialize the slots of this class ztt._N)r   itemssetattrremoveprefixstrr   )r    namevals      r   r   zAttrsDescriptor._init_slotsh   sY    ,,224 	aID#D$++E2S83t?S?STX?Y;ZZ\_`	ar   returnc                     i }| j                   j                         D ]6  \  }}| j                  |   }|D ]  }|j                  |g       ||fgz   ||<    8 |S )a  
        Get the function attributes as a dictionary.

        The returned dictionary will look like :
            {
            "arg0" : [(prop_name00, val00), (prop_name01, val01), ...)]}
            "arg1" : [(prop_name10, val10), (prop_name11, val11), ...)]}
            }
        )r   r5   r   get)r    attrs	prop_namearg_setprop_valr0   s         r   get_fn_attrszAttrsDescriptor.get_fn_attrsm   sw     "&"5"5";";"= 	JIw++I6H J"YYsB/Ix3H2IIc
J	J r   c                     i }| j                   D ]5  }| j                  j                  |g       D ]  }| j                  |   ||<    7 |S )z9 Return a mapping of constant parameters to their values )r   r   r=   r   )r    	constantsr?   ps       r   get_constantszAttrsDescriptor.get_constants~   sZ    	11 	?I((,,Y; ?#33I>	!?	? r   c                     ddl }|j                  |       }|j                  D ]:  }|j                  j	                  |d       |j
                  j	                  |d       < i |_        |S )z? Return the same object, without properties marked as constantsr   N)copydeepcopyr   r   popr   )r    rH   cr?   s       r   filter_out_constantsz$AttrsDescriptor.filter_out_constants   sc    MM$.. 	3I  D1!!)T2	3 !#r   c                 D   t        | j                  j                               g}|t        | j                  j                               gz  }|t        | j                        gz  }t        |      }t        j                  |j                  d            j                         S )Nutf-8)
sortedr   r"   r   r   r8   hashlibsha256encode	hexdigest)r    r"   keys      r   hashzAttrsDescriptor.hash   s    ,,335676$..5578996$22344&k~~cjj12<<>>r   c                 F    | j                   t        |       j                  dS )zM
        Store the fields of this class in a serializable dictionary
        )r   r   )r   typer   r    s    r   to_dictzAttrsDescriptor.to_dict   s     #'"5"5d4j>Q>QRRr   c                     t        | d             }| d   j                         D ]  \  }}||j                  |<    |j                          |S )zB
        Create the object from a serializable dictionary
        r   r   )r   r5   r   r   )dataattrs_descriptorr?   	param_idss       r   	from_dictzAttrsDescriptor.from_dict   s^    
 -T%[9;$()9$:$@$@$B 	C Iy9B++I6	C$$&r   hintsc                      |        }|j                   j                         D ];  \  }}|j                         D cg c]  \  }}||k(  s| c}}|j                  |<   = |j                          |S c c}}w )a  
        Create the class from a set of hints that are passed in.

        Instead of deducing the properties from a list of paramaters and values,
        the user can pass in a list of `hints=[(param_index, val)]` and if `val`
        matches one of the values of the properties (e.g., `prop_val[prop0]`),
        then we insert `param_index` into the correct list (e.g., in
        `arg_properties[prop0]`)
        )r   r5   r   r   )r   r_   r\   r?   rA   ihs          r   
from_hintszAttrsDescriptor.from_hints   sx     5#3#C#C#I#I#K 	hIxHM9g1YZ^fYf!9g++I6	h$$& :hs   A2	A2c                 ~    t        | d      r| j                         dz  dk(  S t        | t              r| dz  dk(  S | yy)z+ Return if the argument is a multiple of 16data_ptrr%   r   TF)hasattrre   
isinstanceintxs    r   r*   z"AttrsDescriptor.is_divisible_by_16   sG     1j!::<"$))3r6Q;9r   c                 T    t        | t              rt        | t              s| dk(  rdS dS )z' Return if the argument is a constant 1r&   TF)rg   rh   boolri   s    r   r.   zAttrsDescriptor.is_equal_to_1   s(     "!S)*Q2E!q&t[V[[r   c                 `    |rt         j                  |       ryt         j                  |       ryy)ND1N)r   r*   r.   )r:   aligns     r   get_property_keyz AttrsDescriptor.get_property_key   s)    _77<((-r   c                 *    d| j                         dS )NzAttrsDescriptor.from_dict())rY   rX   s    r   __repr__zAttrsDescriptor.__repr__   s    +DLLN+=Q??r   )NN)r   
__module____qualname____doc__	__slots__r#   r   r   r   r   rB   rF   rL   rU   rY   staticmethodr^   classmethodr   r	   rh   rc   r*   r.   rr   ru   r2   r   r   r   r      s    6 nI&
2a
d "t ?S      tE#s(O4        \ \  @r   r   T)frozenc                   6    e Zd ZU eed<   eeef   ed<   eed<   y)	GPUTargetbackendarch	warp_sizeN)r   rv   rw   r8   __annotations__r
   rh   r2   r   r   r~   r~      s     L
S/Nr   r~   c                       e Zd ZdeddfdZedefd       Zedefd       Z	e
defd       Ze
d	edefd
       Ze
ded	eddfd       Ze
d        Ze
deeef   fd       Zd Zd Zy)BaseBackendtargetr;   Nc                 8    || _         | j                  |      sJ y N)r   supports_target)r    r   s     r   r#   zBaseBackend.__init__   s    ##F+++r   binaryc                    t         j                  j                  t         j                  j                  t              t         j
                        }t         j                  j                  d| j                          dd      t         j                  j                  |ddd|       g}|D ]  }|j                  d      d   }t         j                  j                  |      s7t         j                  j                  |      sWt        j                  |d	gt        j                  
      }|t        j                   d|j#                  d      t        j$                        }|||j'                  d      fc S  t)        d|        )NTRITON__PATH third_partycudabin r   z	--version)stderrz.*release (\d+\.\d+).*rN   )flagsr&   zCannot find )ospathjoindirname__file__pardirenvironr=   uppersplitexistsisfile
subprocesscheck_outputSTDOUTresearchdecode	MULTILINEgroupRuntimeError)r   base_dirpathsrE   r   resultversions          r   _path_to_binaryzBaseBackend._path_to_binary   s   77<< 9299EJJNNWV\\^$4E:B?GGLL=&%H
  	3A''#,q/Cww~~c"rww~~c':#00#{1CJL]L]^% ii(A6==QXCYacamamnG* '--"222	3 \&233r   c                     t         r   NotImplementedError)r   s    r   r   zBaseBackend.supports_target   s    !!r   c                     t         )z,Returns a unique identifier for this backendr   rX   s    r   rU   zBaseBackend.hash   s
     "!r   optionsc                     t         )z
        Converts an `options` dictionary into an arbitrary object and returns it.
        This function may contain target-specific heuristics and check the legality of the provided options
        r   )r    r   s     r   parse_optionszBaseBackend.parse_options  s
     "!r   stagesc                     t         )a  
        Populates `stages` dictionary with entries of the form:
        ir_name [str] => Function[(src: str, metadata: dict) -> str|bytes]
        The value of each entry may populate a `metadata` dictionary.
        Stages will be run sequentially (in inseriton order) and can communicate using `metadata`.
        All stages are expected to return a `str` object, except for the last stage which returns
        a `bytes` object for execution by the launcher.
        r   )r    r   r   s      r   
add_stageszBaseBackend.add_stages
  s
     "!r   c                     t         )zK
        Load additional MLIR dialects into the provided `context`
        r   )r    contexts     r   load_dialectszBaseBackend.load_dialects  
    
 "!r   c                     t         )z\
        Return a map of interface modules to their device-specific implementations
        r   rX   s    r   get_module_mapzBaseBackend.get_module_map  r   r   c                     t        ||      S )z
        Return an attribute descriptor: given a set of parameters and arguments
        the descriptor stores a set of compile time properties that can improve code
        generation. Different backends might benefit from different properties
        )r   )r    r!   argss      r   get_attrs_descriptorz BaseBackend.get_attrs_descriptor$  s     vt,,r   c                 .    t         j                  ||      S )zZ
        Return the ascii key for a given argument with a given set of properties
        )r   rr   )r    r0   rq   s      r   compute_spec_keyzBaseBackend.compute_spec_key,  s     //U;;r   )r   rv   rw   r~   r#   rz   r8   r   r   r   r   rU   dictobjectr   r   r   r   r   r   r   r   r2   r   r   r   r      s    ,y ,T , 4 4 4  "	 " " "c " " "T "f " " 	" 	" 	"4 	" 	" " " "S*_ 5 " "-<r   r   )	metaclass)r   r   rP   r   abcr   r   r   dataclassesr   typingr   r   r	   r
   typesr   r   r   r   r   r~   r   r2   r   r   <module>r      s~    	 	   < < ! + + 
   |@ |@ |@~ $  N<G N<r   