§
    Û×jm	  ã                   ó*   — d Z ddlmZ ddlZd„ Zd„ ZdS )zýRuntime equivalent of SageAttention PR #387; no installed files or tensor layouts change.

https://github.com/thu-ml/SageAttention/pull/387
Apply before attention runs. These JIT entry points are launched from Python, not called by other JIT functions.
é    )Úimport_moduleNc                 ó¤   — t          j        dd                     |¦  «        z   dz   t           j        ¦  «        }d„ }|                     || ¦  «        S )Nz^(\s*(?:ú|z)\s*=\s*)([^\n#]+)c                 óÊ   — | d                               ¦   «         }t          j        d|¦  «        r| d         S | d         d|› d�z   | d         t          |¦  «        d …         z   S )Né   z\.to\(\s*tl\.int64\s*\)r   é   ú(z).to(tl.int64))ÚrstripÚreÚsearchÚlen)ÚmatchÚ
expressions     úBC:\Users\johnn\OneDrive\Documents\AI\shared\sageattention_patch.pyÚpromotez_int64_offsets.<locals>.promote   sh   € Ø˜1”X—_’_Ñ&Ô&ˆ
ÝŒ9Ð/°Ñ<Ô<ð 	Ø˜”8ˆOØ�QŒxÐ8˜jÐ8Ð8Ð8Ñ8¸5À¼8ÅCÈ
ÁOÄOÐDTÐDTÔ;UÑUÐUó    )r   ÚcompileÚjoinÚ	MULTILINEÚsub)ÚsourceÚnamesÚpatternr   s       r   Ú_int64_offsetsr      sT   € ÝŒj˜ s§x¢x°¡¤Ñ6Ð9NÑNÕPRÔP\Ñ]Ô]€GðVð Vð Vð �;Š;�w Ñ'Ô'Ð'r   c            	      óT  — d} d}d| fd| fdg | ¢d‘d‘R fd|fd	|fd
g |¢d‘d‘R fdg |¢d‘d‘R ff}g }|D ]¥\  }}t          d|› �¦  «        }t          |¦  «                             ¦   «         D ]l\  }}|dk    s*|                     d¦  «        rL|                     d¦  «        r7t          |j        |¦  «        }	|	|j        k    r|                     ||	f¦  «         ŒmŒ¦|D ]3\  }}	|                     |	¦  «         |j	         
                    ¦   «          Œ4t          d„ |D ¦   «         ¦  «        S )zfPromote vulnerable offsets in place, preserving imported aliases; already-fixed kernels are untouched.)Úoff_blkÚoff_hÚoff_b)Ústart_mÚquant_per_threadÚquant_per_blockÚquant_per_block_varlenÚcu_seqlens_input_startÚcu_seqlens_scale_startÚattn_qk_int8_per_blockÚattn_qk_int8_per_block_causalÚattn_qk_int8_block_varlenÚcu_seqlens_q_startÚcu_seqlens_k_startÚ$attn_qk_int8_per_block_causal_varlenzsageattention.triton.Ú	_attn_fwdÚquant_Ú_kernelc              3   ó@   K  — | ]\  }}|j         d z   |j        z   V — ŒdS )ú.N)Ú
__module__Ú__name__)Ú.0ÚkernelÚ_s      r   ú	<genexpr>z&patch_sageattention.<locals>.<genexpr>/   s5   è è € ÐTÐT¹y¸vÀq�Ô" SÑ(¨6¬?Ñ:ÐTÐTÐTÐTÐTÐTr   )r   ÚvarsÚitemsÚ
startswithÚendswithr   ÚsrcÚappendÚ_unsafe_update_srcÚdevice_cachesÚclearÚtuple)
Úquant_offsetsÚattention_offsetsÚtargetsÚchangesÚmodule_nameÚoffsetsÚmoduleÚnamer3   r   s
             r   Úpatch_sageattentionrH      s¬  € à1€MØ$Ðà	˜]Ð+Ø	˜MÐ*Ø	!Ð#g ]Ð#gÐ4LÐ#gÐNfÐ#gÐ#gÐhØ	!Ð#4Ð5Ø	(Ð*;Ð<Ø	$Ð&fÐ(9Ð&fÐ;OÐ&fÐQeÐ&fÐ&fÐgØ	/Ð1qÐ3DÐ1qÐFZÐ1qÐ\pÐ1qÐ1qÐrð€Gð €GØ 'ð 5ð 5Ñˆ�WÝÐD°{ÐDÐDÑEÔEˆÝ  ™LœL×.Ò.Ñ0Ô0ð 	5ð 	5‰LˆD�&Ø�{Ò"Ð" d§o¢o°hÑ&?Ô&?Ð"ÀDÇMÂMÐR[ÑD\ÔD\Ð"Ý'¨¬
°GÑ<Ô<�Ø˜VœZÒ'Ð'Ø—N’N F¨FÐ#3Ñ4Ô4Ð4øð		5ð
 "ð %ð %‰ˆ�Ø×!Ò! &Ñ)Ô)Ð)ØÔ×"Ò"Ñ$Ô$Ð$Ð$ÝÐTÐTÈGÐTÑTÔTÑTÔTÐTr   )Ú__doc__Ú	importlibr   r   r   rH   © r   r   ú<module>rL      sY   ððð ð
 $Ð #Ð #Ð #Ð #Ð #Ø 	€	€	€	ð	(ð 	(ð 	(ðUð Uð Uð Uð Ur   