
    Ϧi|,                         S SK r S SKrS SKrS SKrS SKJrJrJr  S SKJ	r	  S SK
JrJrJrJr  S SKJr  0 rS r\ " S S5      5       r\	" S	S
9 " S S\5      5       r " S S\S9rg)    N)ABCMetaabstractmethodabstractclassmethod)	dataclass)DictListTupleUnion)
ModuleTypec                 ,    U [         U R                  '   U $ )z1
Register a descriptor into the descriptor table
)_descriptor_table__name__)clss    W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/triton/backends/compiler.pyregister_descriptorr      s     '*cll#J    c                       \ rS rSrSrSrSS jrS rSS jrS r	S	\
4S
 jrS	\
4S jrS rS rS r\S 5       r\S\\\\4      4S j5       r\S 5       r\S 5       r\S 5       rS rSrg)AttrsDescriptor   a  
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                     0 U l         0 U l        [        5       U l        U R	                  X5        U R                  X5        U R                  5         g)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__AttrsDescriptor.__init__8   sF     !!#&5 ##F3$$V4r   c                    SU R                   S'   SU R                   S'   U R                  R                  S5        Ub  Uc  g[        U5      [        U5      :X  d   e[	        X5       VVs/ s HS  u  p4[
        R                  U5      (       d  M!  UR                  (       a  M4  UR                  (       a  MG  UR                  PMU     snnU R                  S'   [	        X5       VVs/ s H@  u  p4[
        R                  U5      (       d  M!  UR                  (       a  M4  UR                  PMB     snnU R                  S'   gs  snnf s  snnf )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   &AttrsDescriptor._add_common_propertiesK   s   24././]+  $$]3N Fs6{*+* ),F(;2
(;*%?a?abe?fI++ 494X4X EII(;2
-. "&1.
1
,,S1 :?:Q:Q EII1.
M*2
.
s*   ) D;D; D;3D;" EEEc                     g)zWThis method is for different subclasses to implement their own compile-time properties N r    s      r   r   'AttrsDescriptor._add_backend_propertiesd   s    r   c           	          U R                   R                  5        H<  u  p[        XR                  S5      S-   [	        U R
                  U   5      -   U5        M>     g)z#Initialize the slots of this class ztt._N)r   itemssetattrremoveprefixstrr   )r!   namevals      r   r   AttrsDescriptor._init_slotsh   sN    ,,224IDD++E2S83t?S?STX?Y;ZZ\_` 5r   returnc                     0 nU R                   R                  5        H6  u  p#U R                  U   nU H  nUR                  U/ 5      X$4/-   X'   M     M8     U$ )z
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   r9   r   get)r!   attrs	prop_namearg_setprop_valr2   s         r   get_fn_attrsAttrsDescriptor.get_fn_attrsm   sc     "&"5"5";";"=I++I6H"YYsB/I3H2II
  #> r   c                     0 nU R                    H7  nU R                  R                  U/ 5       H  nU R                  U   X'   M     M9     U$ )z8Return a mapping of constant parameters to their values )r   r   rB   r   )r!   	constantsrD   ps       r   get_constantsAttrsDescriptor.get_constants~   sN    	11I((,,Y;#33I>	 < 2 r   c                     SSK nUR                  U 5      nUR                   H;  nUR                  R	                  US5        UR
                  R	                  US5        M=     0 Ul        U$ )z>Return the same object, without properties marked as constantsr   N)copydeepcopyr   r   popr   )r!   rO   crD   s       r   filter_out_constants$AttrsDescriptor.filter_out_constants   s^    MM$..I  D1!!)T2 / !#r   c                 F   [        U R                  R                  5       5      /nU[        U R                  R                  5       5      /-  nU[        U R                  5      /-  n[        U5      n[        R                  " UR                  S5      5      R                  5       $ )Nutf-8)
sortedr   r#   r   r   r<   hashlibsha256encode	hexdigest)r!   r#   keys      r   hash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    U R                   [        U 5      R                  S.$ )z=
Store the fields of this class in a serializable dictionary
)r   r   )r   typer   r!   s    r   to_dictAttrsDescriptor.to_dict   s     #'"5"5d4j>Q>QRRr   c                     [         U S      " 5       nU S   R                  5        H  u  p#X1R                  U'   M     UR                  5         U$ )z2
Create the object from a serializable dictionary
r   r   )r   r9   r   r   )dataattrs_descriptorrD   	param_idss       r   	from_dictAttrsDescriptor.from_dict   sS    
 -T%[9;$()9$:$@$@$B I9B++I6 %C$$&r   hintsc                     U " 5       nUR                   R                  5        H>  u  p4UR                  5        VVs/ s H  u  pVXd:X  d  M  UPM     snnUR                  U'   M@     UR                  5         U$ s  snnf )aj  
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   r9   r   r   )r   rj   rf   rD   rF   ihs          r   
from_hintsAttrsDescriptor.from_hints   so     5#3#C#C#I#I#KIHM9gYZYf!9g++I6 $L$$& :hs   A6	A6c                     [        U S5      (       a  U R                  5       S-  S:H  $ [        U [        5      (       a  U S-  S:H  $ U c  gg)z*Return if the argument is a multiple of 16data_ptrr'   r   TF)hasattrrq   
isinstanceintxs    r   r,   "AttrsDescriptor.is_divisible_by_16   sM     1j!!::<"$))3r6Q;9r   c                 j    [        U [        5      (       a  [        U [        5      (       d  U S:X  a  S$ S$ )z&Return if the argument is a constant 1r(   TF)rs   rt   boolru   s    r   r0   AttrsDescriptor.is_equal_to_1   s.     "!S))*Q2E2E!q&t[V[[r   c                 ~    U(       a  [         R                  U 5      (       a  g[         R                  U 5      (       a  gg)ND1N)r   r,   r0   )r>   aligns     r   get_property_key AttrsDescriptor.get_property_key   s/    _77<<((--r   c                 ,    SU R                  5       < S3$ )NzAttrsDescriptor.from_dict())rb   ra   s    r   __repr__AttrsDescriptor.__repr__   s    +DLLN+=Q??r   )r   r   r   )NN)r   
__module____qualname____firstlineno____doc__	__slots__r$   r   r   r   r   rG   rL   rS   r]   rb   staticmethodrh   classmethodr   r	   rt   rn   r,   r0   r   r   __static_attributes__r5   r   r   r   r      s    6 nI&
2a
d "t ?S      tE#s(O4        \ \  @r   r   T)frozenc                   >    \ rS rSr% \\S'   \\\4   \S'   \\S'   Srg)	GPUTarget   backendarch	warp_sizer5   N)	r   r   r   r   r<   __annotations__r
   rt   r   r5   r   r   r   r      s     L
S/Nr   r   c                       \ rS rSrS\SS4S jr\S\4S j5       r\	S\4S j5       r
\S\4S	 j5       r\S
\S\4S j5       r\S\S
\SS4S j5       r\S 5       r\S\\\4   4S j5       rS rS rSrg)BaseBackend   targetr@   Nc                 @    Xl         U R                  U5      (       d   eg N)r   supports_target)r!   r   s     r   r$   BaseBackend.__init__   s    ##F++++r   binaryc                     [         R                  R                  [         R                  R                  [        5      [         R
                  5      n[         R                  R                  SU R                  5        S3S5      [         R                  R                  USSSU 5      /nU H  nUR                  S5      S   n[         R                  R                  U5      (       d  M=  [         R                  R                  U5      (       d  Mc  [        R                  " US	/[        R                  S
9nUc  M  [        R                   " SUR#                  S5      [        R$                  S9nUc  M  X6R'                  S5      4s  $    [)        SU  35      e)NTRITON__PATH third_partycudabin r   z	--version)stderrz.*release (\d+\.\d+).*rV   )flagsr(   zCannot find )ospathjoindirname__file__pardirenvironrB   uppersplitexistsisfile
subprocesscheck_outputSTDOUTresearchdecode	MULTILINEgroupRuntimeError)r   base_dirpathsrK   r   resultversions          r   _path_to_binaryBaseBackend._path_to_binary   s   77<< 9299EJJNNWV\\^$4E:B?GGLL=&%H
 A''#,q/Cww~~c""rww~~c':':#00#{1CJL]L]^% ii(A6==QXCYacamamnG* --"222  \&233r   c                     [         er   NotImplementedErrorr   s    r   r   BaseBackend.supports_target   s    !!r   c                     [         e)z,Returns a unique identifier for this backendr   ra   s    r   r]   BaseBackend.hash   s
     "!r   optionsc                     [         e)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_optionsBaseBackend.parse_options  s
     "!r   stagesc                     [         e)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_stagesBaseBackend.add_stages
  s
     "!r   c                     [         e)z;
Load additional MLIR dialects into the provided `context`
r   )r!   contexts     r   load_dialectsBaseBackend.load_dialects  
    
 "!r   c                     [         e)zL
Return a map of interface modules to their device-specific implementations
r   ra   s    r   get_module_mapBaseBackend.get_module_map  r   r   c                     [        X5      $ )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_descriptor BaseBackend.get_attrs_descriptor$  s     v,,r   c                 ,    [         R                  X5      $ )zJ
Return the ascii key for a given argument with a given set of properties
)r   r   )r!   r2   r   s      r   compute_spec_keyBaseBackend.compute_spec_key,  s     //;;r   r   )r   r   r   r   r   r$   r   r<   r   r   r   r   r]   dictobjectr   r   r   r   r   r   r   r   r   r5   r   r   r   r      s    ,y ,T , 4 4 4  "	 " " "c " " "T "f " " 	" 	" 	"4 	" 	" " " "S*_ 5 " "-<r   r   )	metaclass)r   r   rX   r   abcr   r   r   dataclassesr   typingr   r   r	   r
   typesr   r   r   r   r   r   r   r5   r   r   <module>r      s~    	 	   < < ! + + 
   |@ |@ |@~ $  N<G N<r   