Ë
    ŒÓ^j:6  ã                  óì  — d dl mZ d dlmZmZmZ d dlmZ d dlm	c m
c mc mZ d dlmZmZ d dlmZmZ erd dlmZ d dlmZ g d	¢Z ed
¬«       G d„ dej.                  «      «       Ze G d„ dej2                  «      «       Ze	 d	 	 	 	 	 	 	 dd„«       Ze	 d	 	 	 dd„«       Ze	 d	 	 	 dd„«       Zeddd„«       Ze	 d	 	 	 d d„«       Ze	 d	 	 	 d!d„«       Z e	 	 d"	 	 	 d#d„«       Z!e	 	 d"	 	 	 d$d„«       Z"y)%é    )Úannotations)ÚListÚTupleÚTYPE_CHECKING)Ú	dataclassN)ÚPaddedSharedLayoutÚSwizzledSharedLayout)ÚbuiltinÚ_unwrap_if_constexpr)Úir)Úshared_memory_descriptor)Ú
async_loadÚ
async_waitÚmake_tensor_descriptorÚtensor_descriptorÚtensor_descriptor_typeÚprefetchÚasync_scatterT)Úeqc                  ób   — e Zd ZU dZded<   ded<   ded<   ded<   dd	„Zdd
„Zdd„Zdd„Zdd„Z	y)r   z!The type for a tensor descriptor.zttgl.block_typeÚ
block_typezttgl.tuple_typeÚ
shape_typeÚstrides_typeú)PaddedSharedLayout | SwizzledSharedLayoutÚlayoutc                ó<   — d| j                   › d| j                  › d�S )Nztensor_descriptor<z, ú>)r   r   ©Úselfs    ú}/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/triton/experimental/gluon/language/amd/gfx1250/tdm.pyÚ__str__ztensor_descriptor_type.__str__   s    € Ø# D§O¡OÐ#4°B°t·{±{°mÀ1ÐEÐEó    c                ó¶   — ||   }|dz  }| j                   j                  ||«      \  }}| j                  j                  ||«      \  }}t        |||| «      }||fS )Né   )r   Ú_unflatten_irr   r   )r   ÚhandlesÚcursorÚhandleÚshapeÚstridesÚvalues          r    r%   z$tensor_descriptor_type._unflatten_ir   sd   € Ø˜‘ˆØ�!‰ˆØŸ™×5Ñ5°g¸vÓF‰ˆˆvØ×+Ñ+×9Ñ9¸'À6ÓJ‰ˆ�Ü! &¨%°¸$Ó?ˆØ�fˆ}Ðr"   c                óÔ   — | j                   j                  j                  «       }|j                  | j                   j	                  |«      || j
                  j                  |«      «      S ©N)r   Ú
element_tyÚis_int_signedÚ!get_tensor_descriptor_layout_typeÚto_irr   Ú_to_ir)r   ÚbuilderÚ	is_signeds      r    r2   ztensor_descriptor_type._to_ir'   sT   € Ø—O‘O×.Ñ.×<Ñ<Ó>ˆ	Ø×8Ñ8Ø�O‰O×!Ñ! 'Ó*ØØ�K‰K×Ñ˜wÓ'ó
ð 	
r"   c                ó´   — |j                  | j                  |«      «       | j                  j                  ||«       | j                  j                  ||«       y r-   )Úappendr2   r   Ú_flatten_ir_typesr   )r   r3   Úouts      r    r7   z(tensor_descriptor_type._flatten_ir_types/   sA   € Ø�
‰
�4—;‘;˜wÓ'Ô(Ø�‰×)Ñ)¨'°3Ô7Ø×Ñ×+Ñ+¨G°SÕ9r"   c           	     óà   — d| j                   j                  «       › d| j                  j                  «       › d| j                  j                  «       › d| j                  j                  «       › d�	S )NÚTDÚ_)r   Úmangler   r   r   r   s    r    r<   ztensor_descriptor_type.mangle4   sb   € Ø�D—O‘O×*Ñ*Ó,Ð-¨Q¨t¯©×/EÑ/EÓ/GÐ.HÈÈ$×J[ÑJ[×JbÑJbÓJdÐIeÐefÐgk×grÑgr×gyÑgyÓg{Ðf|Ð|~ÐÐr"   N)ÚreturnÚstr)r&   úList[ir.value]r'   Úintr=   zTuple[tensor_descriptor, int])r3   ú
ir.builderr=   zir.type)r3   rA   r8   zList[ir.type]r=   ÚNone)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r!   r%   r2   r7   r<   © r"   r    r   r      s8   … á+àÓØÓØ!Ó!Ø5Ó5óFóó
ó:ô
@r"   r   c                  ó‚   — e Zd ZU dZded<   ded<   ded<   ded<   dd	„Zed
„ «       Zed„ «       Zed„ «       Z	ed„ «       Z
y)r   z4A descriptor representing a tensor in global memory.zir.valuer(   z
ttgl.tupler)   r*   r   Útypec                ó¦   — |j                  | j                  «       | j                  j                  |«       | j                  j                  |«       y r-   )r6   r(   r)   Ú_flatten_irr*   )r   r&   s     r    rL   ztensor_descriptor._flatten_irA   s6   € Ø�‰�t—{‘{Ô#Ø�
‰
×Ñ˜wÔ'Ø�‰× Ñ  Õ)r"   c                ó.   — | j                   j                  S r-   )rJ   r   r   s    r    r   ztensor_descriptor.block_typeF   s   € à�y‰y×#Ñ#Ð#r"   c                óB   — | j                   j                  j                  S r-   )rJ   r   r)   r   s    r    Úblock_shapeztensor_descriptor.block_shapeJ   s   € à�y‰y×#Ñ#×)Ñ)Ð)r"   c                óB   — | j                   j                  j                  S r-   )rJ   r   r.   r   s    r    Údtypeztensor_descriptor.dtypeN   s   € à�y‰y×#Ñ#×.Ñ.Ð.r"   c                ó.   — | j                   j                  S r-   )rJ   r   r   s    r    r   ztensor_descriptor.layoutR   s   € à�y‰y×ÑÐr"   N)r&   r?   r=   rB   )rC   rD   rE   rF   rG   rL   Úpropertyr   rO   rQ   r   rH   r"   r    r   r   8   sr   … á>àÓØÓØÓØ
 Ó ó*ð
 ñ$ó ð$ð ñ*ó ð*ð ñ/ó ð/ð ñ ó ñ r"   r   c                óÈ  — t        |«      }d|cxk  rdk  sn J d|› d�«       ‚t        |«      |k(  sJ d|› dt        |«      › �«       ‚t        |«      |k(  sJ d|› dt        |«      › �«       ‚t        | j                  t        j                  «      sJ d	«       ‚t        |«      }t        |t        t        f«      sJ d
«       ‚t        |t        «      r|j                  dk(  sJ d«       ‚| j                  }|j                  |d¬«      }|j                  |d¬«      }	t        j                  |«      }t        j                  |«      }t        j                  | j                  j                  |«      }
t        |
|j                  |j                  |«      }|j!                  d«      }|j"                  j%                  |j'                  |j"                  «      |||	|«      }t)        ||||«      S )aÉ  Make a tensor descriptor object.

    Args:
        base (tensor): base pointer of the tensor in global memory.
        shape (List[int]): shape of the tensor.
        strides (List[int]): strides of the tensor.
        block_shape (List[int]): block shape of the tensor.
        layout (PaddedSharedLayout | SwizzledSharedLayout): the layout of the tensor in shared memory.

    Returns:
        tensor_descriptor: the created tensor descriptor object
    r$   é   z Expected 1 <= ndim <= 5 but got z dimensionsz	Expected z strides but got zExpected block_shape to have z dimensions but got zExpected base to be a pointerzBExpected layout to be a PaddedSharedLayout or SwizzledSharedLayoutz3Expected max_phase to be 1 for SwizzledSharedLayoutF©Úrequire_i64TÚzero)ÚlenÚ
isinstancerQ   ÚttglÚpointer_typer   r   r	   Ú	max_phaser(   Ú_convert_to_ir_valuesÚtupler   rJ   r.   r   Ú_str_to_padding_optionr3   Úcreate_make_tensor_descriptorr2   r   )Úbaser)   r*   rO   r   Ú	_semanticÚndimÚbase_handleÚshape_handlesÚstride_handlesr   rJ   Úpaddingr(   s                 r    r   r   W   sÍ  € ô  ˆu‹:€DØ�Œ>˜Œ>ÐOÐ=¸d¸VÀ;ÐOÓOˆ>Üˆw‹<˜4ÒÐR 9¨T¨FÐ2CÄCÈÃLÀ>Ð!RÓRÐÜˆ{Ó˜tÒ#ÐmÐ'DÀTÀFÐJ^Ô_bÐcjÓ_kÐ^lÐ%mÓmÐ#Ü�d—j‘j¤$×"3Ñ"3Ô4ÐUÐ6UÓUÐ4ä! &Ó)€FÜ�fÔ1Ô3GÐHÔIð MØLóMÐIä�&Ô.Ô/Ø×Ñ 1Ò$Ð[Ð&[Ó[Ð$à—+‘+€KØ×3Ñ3°EÀuÐ3ÓM€MØ×4Ñ4°WÈ$Ð4ÓO€Nä�J‰J�uÓ€EÜ�j‰j˜Ó!€GÜ—‘ §¡×!5Ñ!5°{ÓC€JÜ! *¨e¯j©j¸'¿,¹,ÈÓO€Dà×.Ñ.¨vÓ6€GØ×Ñ×<Ñ<¸T¿[¹[È×IZÑIZÓ=[Ð]hÐjwØ=KÈWóV€Fô ˜V U¨G°TÓ:Ð:r"   c                ó8  — |j                  |d¬«      }|j                  |«      }|j                  }t        |«      }|�|j                  nt        j
                  j                  «       }|j                  j                  | j                  ||j                  ||«       y)a)  Load a block of tensor specified in tensor descriptor from global memory to shared memory asynchronously.

    Args:
        src (tensor_descriptor): the source tensor descriptor.
        offsets (List[int]): the offsets from the base pointer in the tensor descriptor.
        dest (shared_memory_descriptor): the shared memory destination to store the loaded data.
        pred (int, optional): Predicate to enable or disable the load. Defaults to 1.
        mbarrier (shared_memory_descriptor, optional): The barrier object to signal "arrive" on.
    FrV   N)	r^   Ú	to_tensorr(   r   r[   r   r+   r3   Ú%create_async_tdm_copy_global_to_local)	ÚsrcÚoffsetsÚdestÚpredÚmbarrierrc   Úoffset_handlesÚpred_handleÚmbarrier_handles	            r    r   r   ƒ   s‚   € ð ×4Ñ4°WÈ%Ð4ÓP€NØ×Ñ˜tÓ$€DØ—+‘+€KÜ# HÓ-€HØ)1Ð)=�h—o’oÄ4Ç7Á7Ç=Á=Ã?€OØ×Ñ×;Ñ;¸C¿J¹JÈÐX\×XcÑXcÐepØ<KõMr"   c                óü   — |j                  |d¬«      }t        |«      }|�|j                  nt        j                  j                  «       }|j                  j                  | j                  ||j                  |«       y)aÌ  Store a block of tensor specified in tensor descriptor from shared memory to global memory asynchronously.

    Args:
        dest (tensor_descriptor): the destination tensor descriptor.
        offsets (List[int]): the offsets from the base pointer in the tensor descriptor.
        src (shared_memory_descriptor): the shared memory source to load the data.
        mbarrier (shared_memory_descriptor, optional): The barrier object to signal "arrive" on.
    FrV   N)r^   r   r(   r[   r   r+   r3   Ú%create_async_tdm_copy_local_to_global)rn   rm   rl   rp   rc   rq   rs   s          r    Úasync_storerv   ˜   sc   € ð ×4Ñ4°WÈ%Ð4ÓP€NÜ# HÓ-€HØ)1Ð)=�h—o’oÄ4Ç7Á7Ç=Á=Ã?€OØ×Ñ×;Ñ;¸D¿K¹KÈÐY\×YcÑYcÐetÕur"   c                óP   — t        | «      } |j                  j                  | «       y)z®Wait for the outstanding asynchronous tensor operations to complete.

    Args:
        num_outstanding (int): number of outstanding async tensor operations to wait for.
    N)r   r3   Úcreate_async_tdm_wait)Únum_outstandingrc   s     r    r   r   ©   s"   € ô +¨?Ó;€OØ×Ñ×+Ñ+¨OÕ<r"   c                ó®  — t        | j                  «      }|dk(  sJ d|› d�«       ‚t        |j                  «      }|dk(  sJ d|› d�«       ‚|j                  |gd¬«      d   }t	        |«      }|�|j
                  nt        j                  j                  «       }	|j                  j                  | j
                  |j
                  ||j
                  |	«       y)	a†  Scatter data from shared memory to non-contiguous rows in global memory asynchronously.

    This operation uses TDM scatter mode to write data to non-contiguous rows in global memory.
    Unlike async_store which writes to contiguous rows, scatter allows writing to arbitrary
    rows specified by the dst_row_indices tensor.

    The dtype of dst_row_indices determines the index size:
    - int16: up to 16 rows can be scattered per TDM instruction
    - int32: up to 8 rows can be scattered per TDM instruction
    If more rows are needed, multiple TDM instructions will be automatically issued.

    Args:
        desc (tensor_descriptor): the destination tensor descriptor. Must be 2D.
        dst_row_indices (tensor): 1D tensor of row indices (int16 or int32) in the destination tensor.
        dst_col_offset (int or tensor): the starting column offset in the destination tensor
                                        for all scattered rows.
        src (shared_memory_descriptor): the shared memory source containing data to scatter. Must be 2D.
        mbarrier (shared_memory_descriptor, optional): The barrier object to signal "arrive" on.
    é   z*TDM scatter only supports 2D tensors, got ÚDz TDM scatter src must be 2D, got FrV   r   N)rY   rO   r)   r^   r   r(   r[   r   r+   r3   Úcreate_async_tdm_scatter)
ÚdescÚdst_row_indicesÚdst_col_offsetrl   rp   rc   rd   Úsrc_ndimÚdst_col_offset_handlers   s
             r    r   r   ´   sÏ   € ô, ˆt×ÑÓ €DØ�1Š9ÐJÐBÀ4À&ÈÐJÓJˆ9ä�3—9‘9‹~€HØ�qŠ=ÐHÐ<¸X¸JÀaÐHÓHˆ=ð &×;Ñ;¸^Ð<LÐZ_Ð;Ó`ÐabÑcÐä# HÓ-€HØ)1Ð)=�h—o’oÄ4Ç7Á7Ç=Á=Ã?€Oà×Ñ×.Ñ.¨t¯{©{¸O×<RÑ<RÐTiÐkn×kuÑkuØ/>õ@r"   c                ó®  — t        | j                  «      }|dk(  sJ d|› d�«       ‚t        |j                  «      }|dk(  sJ d|› d�«       ‚|j                  |gd¬«      d   }t	        |«      }|�|j
                  nt        j                  j                  «       }	|j                  j                  | j
                  |j
                  ||j
                  |	«       y)	au  Gather data from non-contiguous rows in global memory to shared memory asynchronously.

    This operation uses TDM gather mode to read data from non-contiguous rows in global memory.
    Unlike async_load which reads from contiguous rows, gather allows reading from arbitrary
    rows specified by the src_row_indices tensor.

    The dtype of src_row_indices determines the index size:
    - int16: up to 16 rows can be gathered per TDM instruction
    - int32: up to 8 rows can be gathered per TDM instruction
    If more rows are needed, multiple TDM instructions will be automatically issued.

    Args:
        desc (tensor_descriptor): the source tensor descriptor. Must be 2D.
        src_row_indices (tensor): 1D tensor of row indices (int16 or int32) in the source tensor.
        src_col_offset (int or tensor): the starting column offset in the source tensor
                                        for all gathered rows.
        dst (shared_memory_descriptor): the shared memory destination to store gathered data. Must be 2D.
        mbarrier (shared_memory_descriptor, optional): The barrier object to signal "arrive" on.
    r{   z)TDM gather only supports 2D tensors, got r|   zTDM gather dst must be 2D, got FrV   r   N)rY   rO   r)   r^   r   r(   r[   r   r+   r3   Úcreate_async_tdm_gather)
r~   Úsrc_row_indicesÚsrc_col_offsetÚdstrp   rc   rd   Údst_ndimÚsrc_col_offset_handlers   s
             r    Úasync_gatherrŠ   Ú   sÎ   € ô, ˆt×ÑÓ €DØ�1Š9ÐIÐAÀ$ÀÀqÐIÓIˆ9ä�3—9‘9‹~€HØ�qŠ=ÐGÐ;¸H¸:ÀQÐGÓGˆ=ð &×;Ñ;¸^Ð<LÐZ_Ð;Ó`ÐabÑcÐä# HÓ-€HØ)1Ð)=�h—o’oÄ4Ç7Á7Ç=Á=Ã?€Oà×Ñ×-Ñ-¨d¯k©k¸?×;QÑ;QÐShÐjm×jtÑjtØ.=õ?r"   c                óÌ   — |j                  |d¬«      }|j                  |«      }|j                  }t        |«      }|j                  j                  | j                  |||d«       y)aÿ  Prefetches a block of tensor specified in tensor descriptor from global memory into L2. Speculative prefetches can generate more
    efficient assembly because they do not require out of bounds checks. However, they are dropped by the hardware if their virtual address translation is not cached.
    So speculative should only be set if previous iterations have accessed the same virtual page (e.g. column major)
    Args:
        src (tensor_descriptor): the source tensor descriptor.
        offsets (List[int]): the offsets from the base pointer in the tensor descriptor.
        pred (bool, optional): Predicate to enable or disable the prefetch. Defaults to True.
        speculative (bool, optional): Whether the prefetch is speculative. Defaults to False.
    FrV   N)r^   rj   r(   r   r3   Úcreate_tdm_prefetch)rl   rm   ro   Úspeculativerc   rq   rr   s          r    r   r      s^   € ð ×4Ñ4°WÈ%Ð4ÓP€NØ×Ñ˜tÓ$€DØ—+‘+€KÜ& {Ó3€KØ×Ñ×)Ñ)¨#¯*©*°nÀkÐS^Ð`eÕfr"   c                ó°  — |j                  |d¬«      }|j                  |«      }|j                  }t        |«      }|j                  j                  | j                  |||d«      }|j                  j                  |«      }|j                  j                  |«      }	t        j                  t        j                  ||	«      }
t        j                  ||
«      }|S )z?Test-only prefetch variant that returns offsets for validation.FrV   T)r^   rj   r(   r   r3   rŒ   Úget_shape_from_tensorÚget_gluon_layout_from_tensorr[   Údistributed_typeÚint64Útensor)rl   rm   ro   r�   rc   rq   rr   r(   r)   r   Úret_tyr“   s               r    Ú_test_prefetch_with_offsetsr•     s¹   € ð ×4Ñ4°WÈ%Ð4ÓP€NØ×Ñ˜tÓ$€DØ—+‘+€KÜ& {Ó3€KØ×Ñ×2Ñ2°3·:±:¸~È{Ð\gÐimÓn€FØ×Ñ×3Ñ3°FÓ;€EØ×Ñ×;Ñ;¸FÓC€FÜ×"Ñ"¤4§:¡:¨u°fÓ=€FÜ�[‰[˜ Ó(€FØ€Mr"   r-   )rb   úttgl.tensorr)   ú"List[ttgl.constexpr | ttgl.tensor]r*   r—   rO   zList[ttgl.constexpr]r   r   r=   r   )r$   NN)
rl   r   rm   r—   rn   r   rp   r   r=   rB   )NN)
rn   r   rm   r—   rl   r   rp   r   r=   rB   )r   N)r=   rB   )
r~   r   r   r–   rl   r   rp   r   r=   rB   )
r~   r   r…   r–   r‡   r   rp   r   r=   rB   )TFN)
rl   r   rm   r—   ro   Úboolr�   r˜   r=   rB   )
rl   r   rm   r—   ro   r˜   r�   r˜   r=   r–   )#Ú
__future__r   Útypingr   r   r   Údataclassesr   Ú(triton.experimental.gluon.language._coreÚexperimentalÚgluonÚlanguageÚ_corer[   Ú+triton.experimental.gluon.language._layoutsr   r	   r
   r   Ú	triton._Cr   r   Ú__all__Ú	base_typer   Ú
base_valuer   r   r   rv   r   r   rŠ   r   r•   rH   r"   r    ú<module>r¦      sÆ  ðÝ "ß -Ñ -Ý !ç 7Ö 7ß `ß RáÝÝQò€ñ ˆdÔô!@˜TŸ^™^ó !@ó ð!@ðH ô ˜Ÿ™ó  ó ð ð< 	ð Y]ð(;Ø$Fð(;ØUið(;à#Lð(;àarò(;ó 	ð(;ðV 	àLPðMØ!9ðMØUYòMó 	ðMð( 	àEIðvØ2ðvØNRòvó 	ðvð  	ó=ó 	ð=ð 	àGKð"@Ø4ð"@ØPTò"@ó 	ð"@ðJ 	àFJð"?Ø3ð"?ØOSò"?ó 	ð"?ðJ 	Ø_cØ26ðgØðgØ;?ògó 	ðgð$ 	ØrvØEIðØ-1ðØNYòó 	ñr"   