§
     ’Þj¡‚  ã                   ó2  — d Z ddlZddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZ 	 ddlmZ n# e$ r dZY nw xY wg d	¢Zd
ZdZdZdada edd¦  «        Z edd¦  «        ZdZdZdZd„ Zej                             ¦   «         d*d„¦   «         Z d*d„Z!d+d„Z"d„ Z#d„ Z$e$Z%d„ Z&d„ Z'd,d„Z(d„ Z)ej*        fd „Z+ej,         G d!„ d"¦  «        ¦   «         Z- G d#„ d$¦  «        Z.d%„ Z/d&„ Z0d*d'„Z1d(„ Z2ej                             ¦   «         	 	 	 d-d)„¦   «         Z3dS ).a¼  VRAM savings shared by model attentions: a model calls qkv_attention() (joint_qkv_attention() for several token streams) instead of
projecting q/k/v and calling pay_attention.

- SageAttention 2 (sm86/sm89/sm120, and its masked Triton path on every architecture) and SageAttention (1, without a mask): v, k and q
  are quantized as soon as each is projected (staged functions of shared.sage2_core / shared.sage1_core), so at most one of them exists
  in 16 bits (with the Triton kernels v stays in fp16); same results as pay_attention.
- The three projections share one input prepared by mmgp (offload.prepare_linear_input) when their weight format supports it: the
  input is quantized once instead of once per projection, and the 16-bit input is released before the projections.
- Attention Head Split (Performance tab, off by default): long sequences (all batch items together) are attended one group of heads
  at a time into one output. q/k/v rows, per-head norms, RoPE and attention are per head; norms over all heads get per token
  statistics computed beforehand.
- Joint attentions: the projections of several streams (text and image tokens) are joined along the tokens in their order, and keys
  and values computed beforehand (a cached prefix) can be put before the projected ones.
- Fused quantization (fused_quant): with a staged SageAttention 2 core, a model whose q/k norm and RoPE have a fused kernel
  (shared.kernels.ops.split_half_norm_rope_quant_) quantizes q and k straight from the projection output, without the in-place
  16-bit pass and the core's separate quantizers (and, for k, its mean and subtraction passes).
- Cached prefixes (cache_prefix, q8_prefix; PrefixCache for a model's layers, in VRAM or streamed from RAM): keys and values computed
  once and attended at every step (reference images, a history) kept quantized, about half of their 16-bit memory. With a staged SageAttention core they keep its own quantized layout and are
  attended in a pass of their own, merged with the projected keys by log-sum-exp: the error of attending the joined keys, and no joined
  16-bit copy at each step. Otherwise they are q8 (int8 per token, head and 32 channels): dequantized into the joined keys and values.
é    N)Ú
namedtuple)Útqdm)ÚoffloadÚruntime)Úpay_attentionÚsage_stagedÚsage_attention_mask)Údispatch)Úkv_prefix_triton))ÚOffr   )zLow: saves some VRAMé   )z&Medium (good balance): saves more VRAMé   )zHigh: saves the most VRAMé   )r   é   é   é   i    i  € FÚ	HeadGroupzq kv mean_squaresÚKVPrefixz!k k_scale k_mean v v_scale v_meané    i   )é@   é€   é   c                 ó$   — t          | ¦  «        ad S ©N)ÚintÚ
head_split)Úlevels    ú<C:\Users\johnn\OneDrive\Documents\AI\shared\attention_kit.pyÚ	configurer   ;   s   € å�U‘”€J€J€Jó    c                 ó¨  — t          | d         j        | d         j        d         |¦  «        }|€t          | |d¬¦  «        S |\  }}| d         | dd…         | dd…         }}}|                      ¦   «          d|                     ||¦  «        |                     ||¦  «        g}|                     ||¦  «        |d<   |                     |||¦  «        S )	a  pay_attention(qkv_list, force_attention=force_attention, recycle_q=True) when nothing else holds k and v: with SageAttention 2
    (sm86/sm89/sm120) or SageAttention, k is quantized and released before v is copied for its quantization, a peak lower by half of k;
    same results.r   éÿÿÿÿNT)Úforce_attentionÚ	recycle_qr   r   r   )	r   ÚdeviceÚshaper   ÚclearÚstaged_quantize_kÚstaged_quantize_vÚstaged_quantize_qÚstaged_attention)	Úqkv_listr#   ÚstagedÚcoreÚsettingsÚqueryÚkey_listÚ
value_listÚ	quantizeds	            r   Úkv_first_attentionr4   @   sÜ   € õ
 ˜ !œÔ+¨X°a¬[Ô->¸rÔ-BÀOÑTÔT€FØ€~Ý˜X°ÐRVÐWÑWÔWÐWØ�N€Dˆ(Ø"*¨1¤+¨x¸¸!¸¬}¸hÀqÈÀs¼m�Zˆ8€EØ‡N‚NÑÔÐØ�t×-Ò-¨h¸ÑAÔAÀ4×CYÒCYÐZdÐfnÑCoÔCoÐp€IØ×)Ò)¨%°Ñ:Ô:€Iˆa�LØ× Ò  ¨	°8Ñ<Ô<Ð<r    c                 ó¤   ‡ ‡— ‰€t           n‰Š‰dk    s|t          k     rdS t          ˆ fd„t          d‰ dz   ¦  «        D ¦   «         ˆfd„¬¦  «        S )zˆGroups of heads of an attention over tokens (of all batch items): the divisor of kv_heads closest to the level's target, 1 when
    off.Nr   r   c              3   ó.   •K  — | ]}‰|z  d k    ¯|V — ŒdS ©r   N© )Ú.0ÚgroupsÚkv_headss     €r   ú	<genexpr>zhead_groups.<locals>.<genexpr>V   s3   øè è € ÐVÐV˜6¸xÈ&Ñ?PÐTUÒ?UÐ?U�Ð?UÐ?UÐ?UÐ?UÐVÐVr    c                 óB   •— t          | t          ‰         z
  ¦  «        | fS r   )ÚabsÚ_TARGET_GROUPS)r:   r   s    €r   ú<lambda>zhead_groups.<locals>.<lambda>V   sC   ø€ ÕloÐpvõ  zHð  INô  zOñ  qOñ  mPô  mPð  RXð  lY€ r    )Úkey)r   ÚMIN_SPLIT_TOKENSÚminÚrange)r;   Útokensr   s   ` `r   Úhead_groupsrF   P   sˆ   øø€ ð  ˜-�JˆJ¨U€EØ�‚z€z�VÕ.Ò.Ð.ØˆqÝÐVÐVÐVÐV¥U¨1¨h¸©lÑ%;Ô%;ÐVÑVÔVð  ]Yð  ]Yð  ]Yð  ]Yð  Zñ  Zô  Zð  Zr    c                 ó\  — |€| j         d         n|}t          dt          t          d| dd…dd…f                              ¦   «         ¦  «        z  ¦  «        }t	          |||¦  «        D ]D}t          ||z   |¦  «        }| dd…||…f         }|                      ||||¦  «        ¦  «         ~ŒEdS )zûtensor[:, first:last] = fn(tensor[:, first:last], first, last) by chunks of tokens of [start, stop), in place: fn computes each
    row on its own (norm, RoPE, possibly in place), so the result is that of fn on the whole range, with a bounded scratch.Nr   )r&   ÚmaxÚ_ROW_ELEMENTSÚnumelrD   rC   Úcopy_)ÚtensorÚfnÚstartÚstopÚstepÚfirstÚlastÚparts           r   Úrows_rT   Y   sÄ   € ð #˜lˆ6Œ<˜Œ?ˆ?°€DÝˆq•-¥3 q¨&°°°°B°Q°B°¬-×*=Ò*=Ñ*?Ô*?Ñ#@Ô#@Ñ@ÑAÔA€DÝ�u˜d DÑ)Ô)ð ð ˆÝ�5˜4‘< Ñ&Ô&ˆØ�a�a�a˜˜t˜�mÔ$ˆØ�
Š
�2�2�d˜E 4Ñ(Ô(Ñ)Ô)Ð)ØˆDð	ð r    c                 ó–   — t           duo@| j        d         t          v o,t          j        | t
          j        t
          j        fd¬¦  «        S )zvThe Triton kernels of kv_prefix_triton for this 16-bit output: compute capability 7.5 and up (BF16 as bits below 8.0).Nr"   )é   é   )ÚdtypesÚcapability_floor)Ú
_kv_tritonr&   Ú_TRITON_DIMSr
   Úcuda_inferenceÚtorchÚbfloat16Úfloat16)Úouts    r   Ú_kernels_eligiblera   e   sH   € å˜dÐ"ð n s¤y°¤}½Ð'Dð nÝÔ'¨µU´^ÅUÄ]Ð4SÐflÐmÑmÔmðor    c                 óT  — t          j        | j        t           j        | j        ¬¦  «        }t          j        g | j        dd…         ¢| j        d         t
          z  ‘R t           j        | j        ¬¦  «        }t          d| j        d         t          ¦  «        D �]}|t          z   }| dd…||…f          	                    ¦   «          
                    ddt
          f¦  «        }|                     ¦   «                              d¦  «                             d¦  «                             d¦  «        }t          j        ||                     d¦  «        ¦  «                             ¦   «                              dd¦  «                             d	¦  «        |dd…||…f<   ||dd…||…f<   ~~�Œ||fS )
z°x (batch, tokens, ..., channels) in the q8 format: (int8 data, FP16 scales (..., channels // 32)), each group of 32 channels
    scaled by max|x| / 127 (that of kv_formats.Q8).©Údtyper%   Nr"   r   r   é   g:Œ0âŽyE>i�ÿÿÿéþÿÿÿ)r]   Úemptyr&   Úint8r%   ÚGROUPr_   rD   Ú_Q8_ROWSÚfloatÚ	unflattenr>   ÚamaxÚdiv_Ú
clamp_min_ÚdivÚ	unsqueezeÚround_Úclamp_Úflatten)ÚxÚdataÚscalesrN   rO   rS   Úscales          r   Úquantize_q8ry   k   sw  € õ Œ;�q”w¥e¤j¸¼ÐBÑBÔB€DÝŒ[Ð>˜1œ7 3 B 3œ<Ð>¨¬°¬½Ñ)=Ð>Ð>ÅeÄmÐ\]Ô\dÐeÑeÔe€FÝ�q˜!œ' !œ*¥hÑ/Ô/ð ñ ˆØ•xÑˆØ����E˜$�J�Ô×%Ò%Ñ'Ô'×1Ò1°"°r½5°kÑBÔBˆØ—’‘
”
—’ Ñ#Ô#×(Ò(¨Ñ-Ô-×8Ò8¸Ñ>Ô>ˆÝ#œi¨¨e¯oªo¸bÑ.AÔ.AÑBÔB×IÒIÑKÔK×RÒRÐSWÐY\Ñ]Ô]×eÒeÐfhÑiÔiˆˆQˆQˆQ��d�
ˆ]ÑØ %ˆˆqˆqˆq�%˜�*ˆ}ÑØ�%‘%Ø�ˆ<Ðr    c                 óÂ  — t          | ¦  «        rt          j        | ||¦  «        S t          d| j        d         t
          ¦  «        D ]—}|t
          z   }|dd…||…f                              ¦   «                              ddt          f¦  «        |dd…||…f                              ¦   «          	                    d¦  «        z   
                    d¦  «        | dd…||…f<   Œ˜| S )zqout (batch, tokens, heads, head_dim) 16-bit, written: q8 data (batch or 1, ...) with its scales, through strides.r   r   Nr"   rf   )ra   rZ   Údequantize_q8_intorD   r&   rj   rk   rl   ri   rq   rt   )r`   rv   rw   rN   rO   s        r   r{   r{   }   s  € å˜ÑÔð @ÝÔ,¨S°$¸Ñ?Ô?Ð?Ý�q˜#œ) Aœ,­Ñ1Ô1ð Pð PˆØ•xÑˆØ" 1 1 1 e¨D j =Ô1×7Ò7Ñ9Ô9×CÒCÀBÈÍUÈÑTÔTÐW]Ð^_Ð^_Ð^_ÐafÐgkÐakÐ^kÔWl×WrÒWrÑWtÔWt×W~ÒW~ð  @Bñ  XCô  XCñ  C÷  Lò  Lð  MOñ  Pô  PˆˆAˆAˆAˆu�TˆzˆMÑÐØ€Jr    c                 óÖ  — t          | ¦  «        rt          j        | |||¦  «        S t          d| j        d         t
          ¦  «        D �]}|t
          z   }|dd…dd…||…f         |dd…dd…||…f         }}t          j        ||¦  «        }t          j        ||z
  ¦  «        t          j        ||z
  ¦  «        }}||z   }	||	z   	                    dd¦  «         
                    d¦  «        ||	z   	                    dd¦  «         
                    d¦  «        }}| dd…||…f                              ¦   «         |z  |dd…||…f                              ¦   «         |z  z   | dd…||…f<   �Œ!| S )z®In place: out = the attention over both key sets of queries attended over one (out, lse: base-2 log-sum-exp (batch, heads,
    tokens)) and over the other (other, other_lse).r   r   Nr   r"   )ra   rZ   Ú
merge_lse_rD   r&   rj   r]   ÚmaximumÚexp2Ú	transposerq   rk   )
r`   ÚlseÚotherÚ	other_lserN   rO   ÚaÚbÚtopÚtotals
             r   Ú_merge_lse_rˆ   ‡   s‰  € õ ˜ÑÔð AÝÔ$ S¨#¨u°iÑ@Ô@Ð@Ý�q˜#œ) Aœ,­Ñ1Ô1ð _ñ _ˆØ•xÑˆØ�1�1�1�a�a�a˜˜t˜Ð#Ô$ i°°°°1°1°1°e¸D°jÐ0@Ô&Aˆ1ˆÝŒm˜A˜qÑ!Ô!ˆÝŒz˜!˜c™'Ñ"Ô"¥E¤J¨q°3©wÑ$7Ô$7ˆ1ˆØ�A‘ˆØ�E‘	×$Ò$ Q¨Ñ*Ô*×4Ò4°RÑ8Ô8¸1¸u¹9×:OÒ:OÐPQÐSTÑ:UÔ:U×:_Ò:_Ð`bÑ:cÔ:cˆ1ˆØ     E¨$ J Ô/×5Ò5Ñ7Ô7¸!Ñ;¸eÀAÀAÀAÀuÈTÀzÀMÔ>R×>XÒ>XÑ>ZÔ>ZÐ]^Ñ>^Ñ^ˆˆAˆAˆAˆu�TˆzˆMÑÑØ€Jr    Tc                 ó  — | \  }}|                       ¦   «          |r t          |j        |j        d         ¦  «        nd}|��t	          |d         d¦  «        rÿ|d                              |d         |j        |j        ¦  «        rÒ|d         }|                     |d         |j        t          j	         
                    dd¦  «        ¦  «        }|                     |¦  «        }|                     |g||¦  «        \  }}	|                     |¦  «        r|                     |g|¦  «        \  }
}}nt          |¦  «        dc\  }
}}t!          ||	||
||¦  «        S t          |¦  «        \  }}	~t          |¦  «        \  }
}t!          ||	d|
|d¦  «        S )a  A cached prefix (KVPrefix) for joint_qkv_attention's kv_prefix, of keys and values (batch, tokens, kv_heads, head_dim) normalized
    and rotated, handed off in kv_list ([key, value], cleared; both are consumed). native: in the quantized layout of the staged
    SageAttention core that will attend it where that core supports it (unmasked attentions without attention_fn: pass False for the
    others), attended in a pass of its own and merged by log-sum-exp, the error of attending the joined keys; otherwise q8, dequantized
    into the joined keys and values at each attention (a small error on 16-bit backends). About half of the 16-bit memory either way.
    A block recorded for a CUDA graph keeps the settings of an eager one, so the cached steps can be recorded.r"   Nr   Úprefix_supportedr   Ú_sage_v_scalingF)r'   r   r%   r&   ÚhasattrrŠ   rd   Úprefix_settingsr   Úshared_stateÚgetÚprefix_meanÚprefix_quantize_kÚprefix_fp8_vr)   Ú_q8r   )Úkv_listÚnativerA   Úvaluer-   r.   r/   ÚmeanÚkÚk_scaleÚvÚv_scaleÚv_means                r   Úcache_prefixr�   —   s”  € ð �J€CˆØ‡M‚M�O„O€OØ7=ÐG�[˜œ S¤Y¨r¤]Ñ3Ô3Ð3À4€FØÑ�g f¨Q¤iÐ1CÑDÔDÐÈÐPQÌ×IcÒIcÐdjÐklÔdmÐorÔoyÐ{~ô  |Eñ  JFô  JFÐØ�aŒyˆØ×'Ò'¨¨q¬	°3´:½wÔ?S×?WÒ?WÐXiÐkpÑ?qÔ?qÑrÔrˆØ×Ò Ñ$Ô$ˆØ×+Ò+¨S¨E°8¸TÑBÔB‰
ˆˆ7Ø×Ò˜XÑ&Ô&ð 	4Ø!%×!7Ò!7¸¸ÀÑ!JÔ!JÑˆAˆw˜˜å#& u¡:¤:¨tÐ ‰LˆQ�˜&Ý˜˜7 D¨!¨W°fÑ=Ô=Ð=Ý�S‘”�J€A€wØÝ�U‘”�J€A€wÝ�A�w  a¨°$Ñ7Ô7Ð7r    c                 óT   — d„ | |||fD ¦   «         \  } }}}t          | |d||d¦  «        S )zðA cached prefix of keys and values already in the q8 format (shared.llm_engines.lmgp.layers.kv_formats.Q8: int8 (batch, tokens,
    kv_heads, head_dim), or without batch, with FP16 scales per 32 channels), as kept by an archive: not copied.c              3   ót   K  — | ]3}|                      ¦   «         d k    r|n|                     d¦  «        V — Œ4dS )r   r   N)Údimrq   ©r9   Úts     r   r<   zq8_prefix.<locals>.<genexpr>´   s@   è è € ÐfÐfÈ 1§5¢5¡7¤7¨a¢< <˜a˜a°Q·[²[À±^´^ÐfÐfÐfÐfÐfÐfr    N)r   )r˜   r™   rš   r›   s       r   Ú	q8_prefixr£   ±   sE   € ð gÐfÈaÐQXÐZ[Ð]dÐMeÐfÑfÔfÑ€A€w��7Ý�A�w  a¨°$Ñ7Ô7Ð7r    c                 ón   ‡— t          ˆfd„| j        | j        f| j        | j        ffD ¦   «         ¦  «        S )z.(keys, values) in dtype of a q8 cached prefix.c              3   ó~   •K  — | ]7\  }}t          t          j        |j        ‰|j        ¬ ¦  «        ||¦  «        V — Œ8dS )rc   N)r{   r]   rg   r&   r%   )r9   rv   rw   rd   s      €r   r<   z$dequantize_prefix.<locals>.<genexpr>º   sh   øè è € ð _ð _Ù!�T˜6õ $¥E¤K°´
À%ÐPTÔP[Ð$\Ñ$\Ô$\Ð^bÐdjÑkÔkð _ð _ð _ð _ð _ð _r    )Útupler˜   r™   rš   r›   )Úprefixrd   s    `r   Údequantize_prefixr¨   ¸   s\   ø€ åð _ð _ð _ð _Ø'-¤x°´Ð&@À6Ä8ÈVÌ^ÐB\Ð%]ð_ñ _ô _ñ _ô _ð _r    c                   ó®   — e Zd ZU dZdZej        dz  ed<   dZej        dz  ed<   dZ	e
dz  ed<   dZeed<   dZeed	<   d
Zeed<   dd„Zd„ Zd„ Zd„ Zd„ ZdS )ÚPrefixLayerzZOne layer of a PrefixCache: its cached keys / values, 16-bit (k, v) or quantized (prefix).Nr˜   rš   r§   r   ÚindexFÚquantizeTr•   c                 óœ  ‡— | j         rt          ||g|o| j        ¬¦  «        nd}| j        j        }|�zt          |                     | j        d„ | j         r|n||fD ¦   «         d¬¦  «        ¦  «        Š| j         rt          ˆfd„|D ¦   «         Ž }nt          ‰¦  «        t          ‰¦  «        }}| j         r	|| _
        dS ||c| _        | _        dS )a>  k / v (batch, tokens, kv_heads, head_dim), normalized and rotated (consumed when quantized); kit: attended through the
        unmasked attention of joint_qkv_attention (a native prefix then). With a RAM cache the layer goes to RAM, its tensors being
        those of its GPU slot (holding them for this pass already).)r•   Nc                 ó   — g | ]}|®|‘ŒS r   r8   r¡   s     r   ú
<listcomp>z%PrefixLayer.store.<locals>.<listcomp>Ï   s   € Ð0sÐ0sÐ0s°qÐefÐer°ÐerÐerÐerr    T)Úcurrentc              3   ó>   •K  — | ]}|€d nt          ‰¦  «        V — Œd S r   )Únext)r9   r¢   Úslotss     €r   r<   z$PrefixLayer.store.<locals>.<genexpr>Ñ   s1   øè è € Ð#WÐ#WÈ1¨A¨I D D½4À¹;¼;Ð#WÐ#WÐ#WÐ#WÐ#WÐ#Wr    )r¬   r�   r•   ÚownerÚringÚiterÚstorer«   r   r²   r§   r˜   rš   )Úselfr˜   rš   Úkitr§   rµ   r³   s         @r   r·   zPrefixLayer.storeÈ   sü   ø€ ð FJÄ]Ð\•˜q !˜f¨SÐ-@°T´[ÐAÑAÔAÐAÐX\ˆØŒzŒˆØÐÝ˜Ÿš D¤JÐ0sÐ0sÀtÄ}Ð=`¸V¸VÐ[\Ð^_ÐZ`Ð0sÑ0sÔ0sð  ~B˜ñ  Cô  Cñ  Dô  DˆEØŒ}ð 0Ý!Ð#WÐ#WÐ#WÐ#WÐPVÐ#WÑ#WÔ#WÐX��å˜E‘{”{¥D¨¡K¤K�1�ØŒ=ð 	"Ø ˆDŒKˆKˆKà ˆNˆDŒF�D”F�F�Fr    c                 ó&   — | j         d u o| j        d u S r   )r˜   r§   ©r¸   s    r   rg   zPrefixLayer.emptyÙ   s   € ØŒv˜ˆ~Ð5 $¤+°Ð"5Ð5r    c                 ó€   — |                       ¦   «         rt          d¦  «        ‚| j        r| j        n| j        | j        fS )z%The kv_prefix of joint_qkv_attention.ú$KV cache has not been populated yet.)rg   ÚRuntimeErrorr¬   r§   r˜   rš   r»   s    r   Ú	kv_prefixzPrefixLayer.kv_prefixÜ   s>   € à�:Š:‰<Œ<ð 	GÝÐEÑFÔFÐFØ"œmÐAˆtŒ{ˆ{°$´&¸$¼&Ð1AÐAr    c                 óš   — |                       ¦   «         rt          d¦  «        ‚| j        rt          | j        ¦  «        n| j        | j        fS )zBThe 16-bit keys / values (attentions outside the kit: masks, NAG).r½   )rg   r¾   r¬   r¨   r§   r˜   rš   r»   s    r   r�   zPrefixLayer.getâ   sH   € à�:Š:‰<Œ<ð 	GÝÐEÑFÔFÐFØ15´ÐTÕ  ¤Ñ-Ô-Ð-ÀTÄVÈTÌVÐDTÐTr    c                 ó0   — d x| _         x| _        | _        d S r   )r˜   rš   r§   r»   s    r   r'   zPrefixLayer.clearè   s   € Ø(,Ð,ˆŒÐ,�”˜$œ+˜+˜+r    ©F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r˜   r]   ÚTensorÚ__annotations__rš   r§   r   r«   r   r¬   Úboolr•   r·   rg   r¿   r�   r'   r8   r    r   rª   rª   ¾   sá   € € € € € € àdÐdØ!€A€u„|�dÑÐ!Ð!Ñ!Ø!€A€u„|�dÑÐ!Ð!Ñ!Ø"€FˆH�t‰OÐ"Ð"Ñ"Ø€Eˆ3€N€N�NØ€HˆdÐÐÑØ€FˆDÐÐÑð"ð "ð "ð "ð"6ð 6ð 6ðBð Bð BðUð Uð Uð-ð -ð -ð -ð -r    rª   c                   ó4   — e Zd ZdZdd„Zd„ Zd„ Zd„ Zdd	„Zd
S )ÚPrefixCacheaƒ  Cached keys / values of a model's attention layers: a prefix (reference images, a history) computed once and attended at the
    following passes. One PrefixLayer per block, which the model passes to its block: layer.store(k, v) where it computes them,
    layer.kv_prefix() for joint_qkv_attention afterwards.
    - quantize: INT8 (cache_prefix: about half the memory, the layout of SageAttention where it attends them).
    - ram: kept in RAM and streamed to the GPU block by block with the weights (mmgp.runtime.state_ring, locked RAM as far as the reserved
      RAM allows): the VRAM of two layers. blocks: the model's blocks (a ModuleList called one by one), whose forward then fetches the
      layer's cache; without ram, blocks may be a number of layers.
    clear() at the end of the generation (clear(keep_ring=True) to store the layers again, e.g. when step-dependent LoRAs change them).FTúKV cachec                 óò   ‡‡— |rt          j        ||¦  «        nd | _        ˆˆfd„t          t	          |t
          ¦  «        r|nt          |¦  «        ¦  «        D ¦   «         | _        | j        D ]	}| |_        Œ
d S )Nc                 ó4   •— g | ]}t          |‰‰¬ ¦  «        ‘ŒS ))r«   r¬   r•   )rª   )r9   r«   r•   r¬   s     €€r   r¯   z(PrefixCache.__init__.<locals>.<listcomp>ø   s;   ø€ ð  Zð  Zð  ZÐV[•{¨¸È&ÐQÑQÔQð  Zð  Zð  Zr    )	r   Ú
state_ringrµ   rD   Ú
isinstancer   ÚlenÚlayersr´   )r¸   Úblocksr¬   r•   ÚramÚnameÚlayers     ``   r   Ú__init__zPrefixCache.__init__ö   sÓ   øø€ Ø8;ÐE•GÔ& v¨tÑ4Ô4Ð4ÀˆŒ	ð Zð  Zð  Zð  Zð  ZÕ_dÕoyð  {Aõ  CFñ  pGô  pGð  fXÐekÐekõ  MPð  QWñ  MXô  MXñ  `Yô  `Yð  Zñ  Zô  ZˆŒØ”[ð 	ð 	ˆEØˆEŒKˆKð	ð 	r    c                 ó*   — t          | j        ¦  «        S r   )rÑ   rÒ   r»   s    r   Ú__len__zPrefixCache.__len__ü   s   € Ý�4”;ÑÔÐr    c                 ó   — | j         |         S r   )rÒ   )r¸   r«   s     r   Ú__getitem__zPrefixCache.__getitem__ÿ   s   € ØŒ{˜5Ô!Ð!r    c                 ó@   — | j         d                              ¦   «         S ©Nr   )rÒ   rg   r»   s    r   rg   zPrefixCache.empty  s   € ØŒ{˜1Œ~×#Ò#Ñ%Ô%Ð%r    c                 ó¼   — | j         D ]}|                     ¦   «          Œ| j        �6|r| j                             ¦   «         n| j                             ¦   «          d S d S r   )rÒ   r'   rµ   ÚresetÚrelease)r¸   Ú	keep_ringrÖ   s      r   r'   zPrefixCache.clear  s^   € Ø”[ð 	ð 	ˆEØ�KŠK‰MŒMˆMˆMØŒ9Ð Ø!*ÐCˆDŒI�OŠOÑÔÐ°´	×0AÒ0AÑ0CÔ0Cøè è ð !Ð r    N)FTFrÌ   rÂ   )	rÃ   rÄ   rÅ   rÆ   r×   rÙ   rÛ   rg   r'   r8   r    r   rË   rË   ì   s{   € € € € € ðKð Kðð ð ð ð ð  ð  ð"ð "ð "ð&ð &ð &ðDð Dð Dð Dð Dð Dr    rË   c           	      ó‚  — |j         d         }|d         }|                      |j        |j        ||¦  «        }|j        j        t          j        k    rÔ|                     ||j        j         d         |j	        |j
        z
  |j        j         d         f|                     d¦  «        r|j        nt          j        ¬¦  «        }	t          |	|j        dd…dd…|j
        |j	        …f         |j        dd…dd…|j
        |j	        …f         ¦  «         |                      |	g|¦  «        }
~	n(|                      |j        |j        |j        ||¦  «        }
|                      t          j        |j         |j        |j        ¬¦  «        |||
g|d¬	¦  «        \  }}~~
|                      |||d¬	¦  «        \  }}t+          ||||¦  «        S )
z½The staged attention of the quantized [q, k, v] written into out, merged with the queries' attention over the heads of a native
    cached prefix (two passes sharing the prefix's key mean).r   r   r"   Úscale_v)rd   Nrc   T)Ú
return_lse)r&   Úprefix_kr˜   r™   rš   rd   r]   rh   Ú	new_emptyrO   rN   r�   r_   r{   r›   r)   Úprefix_vrœ   r+   rg   r%   rˆ   )r.   r/   r`   r3   r§   ÚheadsÚbatchÚq_quantizedr˜   r–   rš   Ú
prefix_outÚ
prefix_lser�   s                 r   Ú_attend_with_prefixrí     sù  € ð ŒI�aŒL€EØ˜A”,€KØ�Š�f”h ¤°°uÑ=Ô=€AØ„x„~�œÒ#Ð#à—’˜u f¤h¤n°QÔ&7¸¼ÀeÄkÑ9QÐSYÔS[ÔSaÐbdÔSeÐfð  |D÷  |Hò  |Hð  IRñ  |Sô  |Sð  ofÐnqÔnwÐnwõ  Y^ô  Yf�ñ  gô  gˆÝ˜5 &¤(¨1¨1¨1¨a¨a¨a°´¸U¼ZÐ1GÐ+GÔ"HÈ&Ì.ÐYZÐYZÐYZÐ\]Ð\]Ð\]Ð_dÔ_jÐkpÔkuÐ_uÐYuÔJvÑwÔwÐwØ×"Ò" E 7¨HÑ5Ô5ˆØˆEà�MŠM˜&œ( F¤N°F´MÀ5È%ÑPÔPˆØ!×2Ò2µ5´;¸s¼yÐPSÔPYÐbeÔblÐ3mÑ3mÔ3mÐp{Ð}~ð  ABð  pCð  EMð  Z^Ð2ñ  _ô  _Ñ€J�
Ø	ˆ1Ø×$Ò$ S¨)°XÈ$Ð$ÑOÔO�H€CˆÝ�s˜C ¨ZÑ8Ô8Ð8r    c                 ó®   — | �)|                       ¦   «         dk     s| j        |         dk    r| n(|                      ||j        |j        |j        z
  ¦  «        S )z^mask restricted to the range heads along its head dimension dim, when it has one row per head.Nr   r   )r    r&   ÚnarrowrN   rO   )Úmaskr    rè   s      r   Ú_head_slicerñ      se   € à�< 4§8¢8¡:¤:°¢> >°T´ZÀ´_ÈÒ5IÐ5Iˆ4ˆ4ÈtÏ{Ê{Ð[^Ð`eÔ`kÐmrÔmwÐzô  {Fñ  nFñ  PGô  PGð  Gr    c                 óB   — |||f|�|fndz   }	t          | |	fg|||fi |¤ŽS )a¨  Attention of the q/k/v projections of x (handed off in x_list, shape (batch, tokens, features) or (tokens, features)), returned
    as (batch, tokens, heads, head_dim), batch 1 for a 2D x. See joint_qkv_attention for norm_rope and the options; extra_proj is a
    fourth per-head projection of x (heads x head_dim wide) computed by head groups from the same prepared input and handed to
    attention_fn as a third argument.Nr8   ©Újoint_qkv_attention)
Úx_listÚq_projÚk_projÚv_projrè   Úhead_dimÚ	norm_ropeÚ
extra_projÚkwargsÚprojectionss
             r   Úqkv_attentionrþ   %  sF   € ð
 ˜6 6Ð*¸zÐ?U¨z¨m¨mÐ[]Ñ^€KÝ ¨Ð 5Ð6¸¸xÈÐ]Ð]ÐV\Ð]Ð]Ð]r    c                 ó>   — t          | |ddff|d||ffg|||fddi|¤ŽS )zÝThe same projection/attention path with queries from x and keys/values from context, both handed off in lists.
    Head splitting considers the longer sequence, including a long video used as keys for short audio queries.NÚ_crossTró   )	rõ   Úcontext_liströ   r÷   rø   rè   rù   rú   rü   s	            r   Úcross_qkv_attentionr  .  sZ   € õ  ¨&°$¸Ð)=Ð >ÀÐPTÐV\Ð^dÐOeÐ@fÐgØ# X¨yðQð QØAEðQØIOðQð Qð Qr    c           
      óæ  ‡‡‡‡3‡4‡5‡6‡7‡8‡9‡:‡;‡<‡=‡>‡?‡@‡A‡B— |�|	dk    s|
�t          d¦  «        ‚g g cŠ7Š=| D ]x\  }}|d         }|                     ¦   «          ‰7                     |                     ¦   «         dk    r|n|                     d¦  «        ¦  «         ‰=                     |¦  «         Œydx} }‰r‰=d         d         ‰=d         d         u nt          d„ ‰=D ¦   «         ¦  «        }‰ ot          ‰=d         ¦  «        dk    }‰7d         j        d         d	„ ‰7D ¦   «         ‰7d         j        cŠ3Š9}‰r‰9d         nt          ‰9¦  «        Š@|p|}|r%t          |‰3‰rt          ‰9¦  «        n‰@z  |¦  «        ndŠ6|
�dnt          ‰7d         j        ‰|	|du¦  «        }t          |¦  «        r|€t          d
¦  «        ‚t          ‰t           ¦  «        Š4‰4o‰j        duŠ;‰;r)|�|€t%          |d         d¦  «        st          d¦  «        ‚dgt          ‰7¦  «        z  Š:t'          ‰=¦  «        D ]‹\  }}‰rt)          d„ |D ¦   «         ¦  «        }|r't)          t*                               |¦  «        ¦  «        }t/          j        |‰7|         ¦  «        r'‰7|         gdc}‰7|<   t/          j        ||¦  «        ‰:|<   ŒŒ‰6dk    rR‰3t          d„ t5          ‰9‰:¦  «        D ¦   «         ¦  «        z  t6          k    rt8          st;          j        d¦  «         dadŠ6i ŠAˆ3ˆ6ˆˆ7ˆ9ˆ:ˆ=ˆAfd„Š<ˆˆ3ˆ9ˆ<ˆ@fd„}ˆ7ˆ:ˆAfd„}ˆ4ˆˆ;fd„ŠBd}|r‰6dk    r|� ||¦  «        }nó|rñ‰6dk    rëg }d|fd|ffD ]Ñ\  }}|‰6z  d}!} t?          ‰6¦  «        D ]wŠ5 ||t?          ‰5| z  ‰5dz   | z  ¦  «        ¦  «                              ¦   «          !                    ¦   «          	                    d¬¦  «        }"|!€|"n|! "                    |"¦  «        }!~"Œx|                     |! #                    |‰z  ¦  «                             d¦  «        ¦  «         ŒÒt)          |¦  «        }|�g|\  }#}$‰;r?|# $                    |$‰j%        j        t.          j&         '                    dd¦  «        ¦  «        }$|�t          |¦  «        stQ          ||¦  «        }|duo|duot          |¦  «         }%|%o‰du p‰;}&|‰6z  |‰6z  cŠ>Š8d}'t?          ‰6¦  «        D �]�}tS          t?          |‰>z  |dz   ‰>z  ¦  «        t?          |‰8z  |dz   ‰8z  ¦  «        |¦  «        Š5|‰6dz
  k    }(|�€Œ |d‰5j*        ¦  «         |d‰5j+        ¦  «        gŠ?‰?                     |r‰?d          ,                    ¦   «         n |d‰5j+        ¦  «        ¦  «         |r |d‰5j*        ¦  «        gnd})|(r
 |¦   «          |� |‰?d         ‰5¦  «          |‰?d         ‰?d         ‰5¦  «         ˆ5ˆ?ˆBfd„dD ¦   «         ‰?dd…<   ‰>‰8k    rˆ8ˆ>fd„‰?dd…         D ¦   «         ‰?dd…<   |
€)t[          ‰?t]          |d‰5j*        ¦  «        |	d|¬¦  «        }*n|)€ |
‰?‰5¦  «        n |
‰?‰5|)¦  «        }*‰6dk    r|*c S |'€|* /                    ‰3‰@|‰f¦  «        }'|'d‰5j*        j0        ‰5j*        j1        …dd…f          2                    |*¦  «         ~*�ŒÚ|r |d‰5j+        ¦  «        nd}+|r|+ ,                    ¦   «         n |d‰5j+        ¦  «        },|� ||,‰5¦  «          ‰B|,d‰5j+        ¦  «        g}-~,dd|# 3                    |-|$¦  «        g}.‰;r&i |$¥d|# 4                    ‰j        ‰5j+        ¦  «        i¥n|$}/‰6dk    �rF|s |d‰5j+        ¦  «        }+|&r |d|+‰5|#|/¦  «        nd}0|0€V |d|+‰5¦  «          ‰B|+d‰5j+        ¦  «        }+t          |¦  «        r |d|+‰5¦  «         |# 5                    |+g|/¦  «        |.d<   n|0d         |.d<   ~+~0 |d‰5j*        ¦  «        }1 |¦   «          |%r ||1d‰5|#|$¦  «        nd}0|0€E ||1d‰5¦  «         t          |¦  «        r ||1d‰5¦  «        n|}2|# 6                    |1|$¦  «        |.d<   n||0d         c}2|.d<   ~0‰;rto          |#|$|1|.‰‰5j+        ¦  «        c S |# 8                    |1|.|$|2¦  «        c S  |d‰5j*        ¦  «        }1|s |d‰5j+        ¦  «        }+|(r
 |¦   «          |&r ||1|+‰5|#|/¦  «        n|%r ||1d‰5|#|$¦  «        nd}0|0�|0d         €Z ||0€|1nd|+‰5¦  «          ‰B|+d‰5j+        ¦  «        }+t          |¦  «        r |d|+‰5¦  «         |# 5                    |+g|/¦  «        |.d<   n|0d         |.d<   ~+|0€Lt          |¦  «        r ||1d‰5¦  «        nt]          |d‰5j*        ¦  «        }2|# 6                    |1|$¦  «        |.d<   n"t]          |d‰5j*        ¦  «        |0d         c}2|.d<   ~0|'€|1 /                    ‰3‰@|‰f¦  «        }'‰;r;to          |#|$|'d‰5j*        j0        ‰5j*        j1        …dd…f         |.‰‰5j+        ¦  «         n9|# 8                    |'d‰5j*        j0        ‰5j*        j1        …dd…f         |.|$|2¦  «         ~1�Œ‘|'S ) a™  Attention of the q/k/v projections of several streams joined along the tokens in their order: streams is a list of
    (x_list, (q_proj, k_proj, v_proj)), each x handed off in its list, of shape (batch, tokens, features) or (tokens, features).
    Returns (batch, all tokens, heads, head_dim).

    norm_rope(query, key, group) normalizes and rotates in place query and/or key, the other one may be None: the joined projections,
    (batch, all tokens, heads of the group, head_dim), for the head ranges group.q and group.kv. With norm_spans_heads, a split group
    gets group.mean_squares to scale its rows; otherwise the tensors hold all heads. split_heads=False keeps all heads together.
    attention_fn: optional model-specific sparse attention accepting the disposable qkv_list and HeadGroup. It owns attention
    computation; projection preparation, head splitting and normalization still use this kit. Staged Sage quantization is disabled.
    kv_prefix: (key, value) of shape (batch, prefix tokens, kv_heads, head_dim), already normalized and rotated, put before the
    projected keys and values; or a cached prefix (cache_prefix, q8_prefix: KVPrefix), q8 dequantized into the joined keys and values,
    native attended in a pass of its own (a native prefix needs the staged core it was made for: no mask, no attention_fn), a prefix
    of one batch item serving all. attention_mask: pay_attention's mask, or on a staged core a callable mask(query, key, group) building
    the group's mask from its normalized and rotated query and key (the key with its prefix): called with the key alone first (it
    keeps what it needs, the key is then quantized and released), then with the query alone, returning the mask.
    kv_heads < heads: grouped query attention.
    norm_stats(project): optional model-native mean-square calculation, used only when splitting a norm spanning all heads;
    project(which, head_range) supplies disposable projections. Otherwise statistics are accumulated by groups below.
    norm_v(value, group): optional in-place value normalization, before prefix concatenation or quantization.
    When every K/V stream shares its k and v module, reuse its projection with independent tensors for normalization.
    softmax_scale: explicit SDPA scale; requires force_attention="sdpa".
    head_split_level: Attention Head Split level of this call instead of the Performance tab's (a model-specific sparse attention
    bounding its working set). A stream may carry a fourth projection: attention_fn then receives its head group's rows in a list.
    fused_quant(query, key, group, core, settings): optional norm + RoPE fused with a staged core's quantization (for example
    shared.kernels.ops.split_half_norm_rope_quant_), returning (q_quantized, k_quantized) for the tensors given (the other None) as
    the core's staged_quantize_q / staged_quantize_k do, or None to run norm_rope and the core's quantizers instead; used by the
    staged cores unless the attention_mask is a callable (it reads the normalized 16-bit q and k); with a kv_prefix only q is fused
    (the prefix joins the normalized 16-bit k), except with a native prefix (k fused with the prefix's mean: settings["k_mean"]).NÚsdpazMAn explicit softmax_scale requires force_attention='sdpa' and no attention_fnr   r   r   c              3   ó6   K  — | ]}|d          |d         u V — ŒdS )r   r   Nr8   )r9   Úmoduless     r   r<   z&joint_qkv_attention.<locals>.<genexpr>_  s4   è è € ÐI~ÐI~ÐgnÈ'ÐRSÌ*ÐX_Ð`aÔXbÐJbÐI~ÐI~ÐI~ÐI~ÐI~ÐI~r    r   c                 ó(   — g | ]}|j         d          ‘ŒS )r   )r&   )r9   ru   s     r   r¯   z'joint_qkv_attention.<locals>.<listcomp>a  s   € Ð0LÐ0LÐ0LÀ°´¸´Ð0LÐ0LÐ0Lr    zEA per-group attention mask (a callable) needs a staged attention corerå   zyA native cached prefix is attended by the unmasked staged attention it was made for (cache_prefix native=False otherwise)c              3   ó   K  — | ]}|®|V — Œ	d S r   r8   )r9   Úmodules     r   r<   z&joint_qkv_attention.<locals>.<genexpr>o  s'   è è € ÐOÐO v¸FÐ<N˜FÐ<NÐ<NÐ<NÐ<NÐOÐOr    c              3   ó$   K  — | ]\  }}|­|V — Œd S r   r8   )r9   ÚlengthÚprepareds      r   r<   z&joint_qkv_attention.<locals>.<genexpr>v  s-   è è € Ð!qÐ!qÑ-=¨V°XÐ`hÐ`p &Ð`pÐ`pÐ`pÐ`pÐ!qÐ!qr    z‚[Attention] Head Split is not applied: these q/k/v projections cannot be computed by groups of heads (weight format or LoRA type).Tc                 óÖ  •— ‰|          |         ‰
|          }}|�Ct          j        |||j        ‰z  |j        ‰z  ¦  «                             ‰‰	|          d‰¦  «        S ‰dk    r- |‰|          ¦  «                             ‰‰	|          d‰¦  «        S | |f‰vr2 |‰|          ¦  «                             ‰‰	|          d‰¦  «        ‰| |f<   ‰| |f         d d …d d …|j        |j        …f         S )Nr"   r   )r   Úlinear_rowsrN   rO   Úview)r«   ÚwhichÚ
head_ranger	  Úlinear_inputré   r:   rù   ÚinputsÚlengthsÚlinear_inputsrý   Úwholes        €€€€€€€€r   Úproject_streamz+joint_qkv_attention.<locals>.project_stream}  s1  ø€ Ø*¨5Ô1°%Ô8¸-ÈÔ:N�ˆØÐ#ÝÔ& v¨|¸ZÔ=MÐPXÑ=XÐZdÔZiÐltÑZtÑuÔu×zÒzð  |Að  CJð  KPô  CQð  SUð  W_ñ  `ô  `ð  `Ø�QŠ;ˆ;Ø�6˜& œ-Ñ(Ô(×-Ò-¨e°W¸U´^ÀRÈÑRÔRÐRØ�5ˆ> Ð&Ð&Ø"( &¨°¬Ñ"7Ô"7×"<Ò"<¸UÀGÈEÄNÐTVÐX`Ñ"aÔ"aˆE�%˜�,ÑØ�U˜E�\Ô" 1 1 1 a a a¨Ô)9¸*¼/Ð)IÐ#IÔJÐJr    c                 ój  •— ‰r ‰
| dk    rdnd| |¦  «        S t          ‰	¦  «        dk    r ‰
d| |¦  «        S d\  }}t          ‰	¦  «        D ]e\  }} ‰
|| |¦  «        }|€'|                     ‰‰g|j        dd…         ¢R ¦  «        }|dd…|||z   …f                              |¦  «         ||z  }~Œf|S )z^Projection which (0 q, 1 k, 2 v) of the heads head_range, the streams joined along the tokens.r   r   rÝ   Nr   )rÑ   Ú	enumerateræ   r&   rK   )r  r  ÚjoinedrN   r«   r  rS   r   ré   r  r  rE   s          €€€€€r   Úprojectz$joint_qkv_attention.<locals>.project‡  só   ø€ àð 	MØ!�> u°¢z z ! !°q¸%ÀÑLÔLÐLÝˆw‰<Œ<˜1ÒÐØ!�> ! U¨JÑ7Ô7Ð7Ø‰ˆ�Ý& wÑ/Ô/ð 	ð 	‰MˆE�6Ø!�> %¨°
Ñ;Ô;ˆDØˆ~ØŸš¨°Ð(H¸¼ÀAÀBÀB¼Ð(HÐ(HÑIÔI�Ø�1�1�1�e˜E F™NÐ*Ð*Ô+×1Ò1°$Ñ7Ô7Ð7Ø�V‰OˆEØ�Øˆr    c                  ó€   •— ‰                       ¦   «          ‰                      ¦   «          ‰                      ¦   «          d S r   )r'   )r  r  r  s   €€€r   rà   z$joint_qkv_attention.<locals>.release—  s3   ø€ Ø�Š‰ŒˆØ×ÒÑÔÐØ�Š‰Œˆˆˆr    c           	      óx  •— ‰�‰r| S ‰s:t          j        ‰|dz
           d d …d d …|j        |j        …f         | gd¬¦  «        S |dk    r‰j        ‰j        fn‰j        ‰j        f\  }}|                      | j	        d         |j	        d         | j	        d         z   g| j	        dd …         ¢R ¦  «        }t          |d d …d |j	        d         …f         |d d …d d …|j        |j        …f         |d d …d d …|j        |j        …f         ¦  «         |d d …|j	        d         d …f                              | ¦  «         |S )Nr   ©r    r   r   )r]   ÚcatrN   rO   r˜   r™   rš   r›   ræ   r&   r{   rK   )	rL   r  r  rv   rw   r  Úcachedr¿   r•   s	         €€€r   Úwith_prefixz(joint_qkv_attention.<locals>.with_prefixœ  s–  ø€ ØÐ ÐØˆMØð 	lÝ”9˜i¨°©	Ô2°1°1°1°a°a°a¸Ô9IÈ*Ì/Ð9YÐ3YÔZÐ\bÐcÐijÐkÑkÔkÐkØ;@ÀAº:¸:˜	œ YÔ%6Ð7Ð7ÈIÌKÐYbÔYjÐKk‰ˆˆfØ×!Ò! 6¤<°¤?°D´J¸q´MÀFÄLÐQRÄOÑ4SÐ"gÐV\ÔVbÐcdÐceÐceÔVfÐ"gÐ"gÑhÔhˆÝ˜6 ! ! ! ^ d¤j°¤m ^Ð"3Ô4°d¸1¸1¸1¸a¸a¸aÀÔAQÐR\ÔRaÐAaÐ;aÔ6bÐdjÐklÐklÐklÐnoÐnoÐnoÐq{ô  rBð  CMô  CRð  rRð  lRô  eSñ  	Tô  	Tð  	TØˆqˆqˆq�$”*˜Q”-�.�.Ð Ô!×'Ò'¨Ñ/Ô/Ð/Øˆr    )rf   r"   r  r"   r‹   Fc                 ó@   •— g | ]} ‰‰|         |‰j         ¦  «        ‘ŒS r8   )Úkv)r9   r  Úgroupr,   r!  s     €€€r   r¯   z'joint_qkv_attention.<locals>.<listcomp>Ì  s-   ø€ Ð^Ð^Ð^Èe˜K˜K¨°¬¸ÀÄÑIÔIÐ^Ð^Ð^r    )r   r   c                 óD   •— g | ]}|                      ‰‰z  d ¬¦  «        ‘ŒS )r   r  )Úrepeat_interleave)r9   rL   Úkv_group_headsÚq_headss     €€r   r¯   z'joint_qkv_attention.<locals>.<listcomp>Î  s3   ø€ ÐvÐvÐvÐ_e × 8Ò 8¸ÀNÑ9RÐXYÐ 8Ñ ZÔ ZÐvÐvÐvr    )Úattention_maskr#   r$   Úsoftmax_scale.Úk_mean)9Ú
ValueErrorr'   Úappendr    rq   ÚallrÑ   r&   rd   ÚsumrF   rH   r   r%   ÚcallablerÐ   r   r+  rŒ   r  r¦   ÚdictÚfromkeysr   Úlinear_rows_supportedÚprepare_linear_inputÚziprB   Ú_unsplit_noticer   ÚwriterD   rk   Úsquare_Úadd_rn   r�   r˜   rŽ   r�   r	   r   Úqr#  Úcloner   rñ   ræ   rN   rO   rK   r)   Úprefix_mean_headsr(   r*   rí   r+   )CÚstreamsrè   rù   rú   r;   Únorm_spans_headsÚsplit_headsr¿   r)  r#   Úattention_fnÚ
norm_statsr   Únorm_vr*  Úhead_split_levelÚfused_quantrõ   Ústream_projectionsru   Ú	shared_kvÚextrard   r-   r«   r  Úx_holderr  rà   Úmean_squaresr  r‡   rP   ÚsquaresrS   r.   r/   Úfusing_qÚfusing_kÚoutputrR   Ú
extra_listÚ	attentionrA   r–   r2   r3   Ú
k_settingsÚfusedr0   rð   ré   r   r$  r:   r  r'  r  r  r•   r  rý   r(  r,   rE   r  r!  sC     `    `    `                                      @@@@@@@@@@@@@@@@r   rô   rô   5  sÒ  øøøøøøøøøøøøøøøøøøø€ ðB Ð  o¸Ò&?Ð&?À<ÐC[ÝÐhÑiÔiÐiØ˜bÐ€FˆKØ&-ð /ð /Ñ"ˆÐ"Ø�1ŒIˆØ�Š‰ŒˆØ�Š˜1Ÿ5š5™7œ7 Qš;˜;�a�a¨A¯KªK¸©N¬NÑ;Ô;Ð;Ø×ÒÐ-Ñ.Ô.Ð.Ð.ØÐ€GˆaØ:@Ð~�˜A”˜qÔ! [°¤^°AÔ%6Ð6Ð6ÅcÐI~ÐI~Ðr}ÐI~ÑI~ÔI~ÑF~ÔF~€IØˆJÐ2�3˜{¨1œ~Ñ.Ô.°Ò2€EØ" 1œIœO¨AÔ.Ð0LÐ0LÀVÐ0LÑ0LÔ0LÈfÐUVÌiÌoÐ€Eˆ7�EØ!Ð3ˆW�QŒZˆZ¥s¨7¡|¤|€FØÐ ˜5€HØfqÐx�[˜ 5¸FÐ,N­C°©L¬L¨LÈÑ#OÐQaÑbÔbÐbÐwx€FØ!Ð-ˆTˆTµ;¸vÀa¼yÔ?OÐQYÐ[jÐlzð  CGð  mGñ  4Hô  4H€FÝ�ÑÔð b F NÝÐ`ÑaÔaÐaÝ˜	¥8Ñ,Ô,€FØÐ4˜	Ô(°Ð4€FØð V�6�> ^Ð%?ÅwÈvÐVWÌyÐZdÑGeÔGeÐ%?Ýð  Uñ  Vô  Vð  	VØ�F�S ™[œ[Ñ(€MÝ# KÑ0Ô0ð Sð S‰ˆˆwØð 	PÝÐOÐO°ÐOÑOÔOÑOÔOˆGØð 	4Ý�DŸMšM¨'Ñ2Ô2Ñ3Ô3ˆGÝÔ(¨°&¸´-Ñ@Ô@ð 	SØ'-¨e¤} o°tÐ#ˆH�f˜U‘mÝ#*Ô#?ÀÈ'Ñ#RÔ#RˆM˜%Ñ øà�‚z€z�e�cÐ!qÐ!qÅÀWÈmÑA\ÔA\Ð!qÑ!qÔ!qÑqÔqÑqõ  vFò  Fð  FÝð 	#ÝŒJð  \ñ  ]ô  ]ð  ]Ø"ˆOØˆØ€EðKð Kð Kð Kð Kð Kð Kð Kð Kð Kð Kð Kðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð
	ð 	ð 	ð 	ð 	ð 	ð 	ð €LØð +˜F QšJ˜J¨:Ð+AØ!�z 'Ñ*Ô*ˆˆØ	ð 	+˜f qšj˜jØˆØ ˜Z¨!¨X¨Ð7ð 	Nð 	N‰LˆE�5Ø! V™O¨T�'ˆDÝ˜v™œð ð �Ø�w˜u¥e¨E°D©L¸5À1¹9ÈÑ:LÑ&MÔ&MÑNÔN×TÒTÑVÔV×^Ò^Ñ`Ô`×dÒdÐiqÐdÑrÔr�Ø") /˜$˜$°w·|²|ÀDÑ7IÔ7I�Ø�DØ×Ò §¢¨U°XÑ-=Ñ >Ô >× HÒ HÈÑ LÔ LÑMÔMÐMÐMÝ˜\Ñ*Ô*ˆàÐØ‰ˆˆhØð 	~Ø×+Ò+¨H°i´kÔ6HÍ'ÔJ^×JbÒJbÐctÐv{ÑJ|ÔJ|Ñ}Ô}ˆHØÐ%­h°~Ñ.FÔ.FÐ%Ý0°ÀÑGÔGˆNà˜TÐ!Ð^ k¸Ð&=Ð^ÅhÈ~ÑF^ÔF^ÐB^€HØÐ9˜Y¨$Ð.Ð8°&€HØ# v™o¨x¸6Ñ/AÐ€Gˆ^Ø€FÝ�v‘”ð Xñ XˆÝ�% ¨¡°%¸!±)¸wÑ1FÑGÔGÍÈuÐWeÑOeÐhmÐpqÑhqð  vDñ  hDñ  JEô  JEð  GSñ  Tô  TˆØ˜ ™
Ò"ˆØ‰>Ø˜  5¤7Ñ+Ô+¨W¨W°Q¸¼Ñ-AÔ-AÐBˆHØ�OŠO°9ÐV˜H QœK×-Ò-Ñ/Ô/Ð/À'À'È!ÈUÌXÑBVÔBVÑWÔWÐWØ27ÐA˜'˜' ! U¤WÑ-Ô-Ð.Ð.¸TˆJØð Ø�‘	”	�	ØÐ!Ø��x ”{ EÑ*Ô*Ð*ØˆI�h˜q”k 8¨A¤;°Ñ6Ô6Ð6Ø^Ð^Ð^Ð^Ð^Ð^ÐW]Ð^Ñ^Ô^ˆH�Q�R�R‰LØ˜.Ò(Ð(ØvÐvÐvÐvÐvÐiqÐrsÐrtÐrtÔiuÐvÑvÔv�˜˜˜‘ØÐ#Ý)¨(Å;È~Ð_`ÐbgÔbiÑCjÔCjð  }Lð  X\ð  lyð  zñ  zô  z�	�	à=GÐ=O˜L˜L¨°5Ñ9Ô9Ð9ÐUaÐUaÐbjÐlqÐs}ÑU~ÔU~�	Ø˜Š{ˆ{Ø Ð Ð Ð Øˆ~Ø"×,Ò,¨e°V¸UÀHÐ-MÑNÔN�Ø�3˜œœ e¤g¤lÐ2°A°A°AÐ5Ô6×<Ò<¸YÑGÔGÐGØÙØ&/Ð9ˆgˆg�a˜œÑ"Ô"Ð"°TˆØ(ÐB�—	’	‘”�¨g¨g°a¸¼Ñ.BÔ.BˆØÐØˆF�5˜%Ñ Ô Ð Ø!�k %¨¨E¬HÑ5Ô5Ð6ˆ
ØØ˜4 ×!7Ò!7¸
ÀHÑ!MÔ!MÐNˆ	àciÐwÐ_˜Ð_ (¨D×,BÒ,BÀ9ÔCSÐUZÔU]Ñ,^Ô,^Ð_Ð_Ð_Ðowˆ
Ø�QŠ;‰;Øð +Ø�g˜a ¤Ñ*Ô*�ØGOÐY�K�K  c¨5°$¸
ÑCÔCÐCÐUYˆEØˆ}Ø�	˜$  UÑ+Ô+Ð+Ø!�k # q¨%¬(Ñ3Ô3�Ý˜NÑ+Ô+ð 5Ø"�N 4¨¨eÑ4Ô4Ð4Ø#×5Ò5°s°e¸ZÑHÔH�	˜!‘�à$ Qœx�	˜!‘Ø�UØ�G˜A˜uœwÑ'Ô'ˆEØˆG‰IŒIˆIØGOÐY�K�K  t¨U°D¸(ÑCÔCÐCÐUYˆEØˆ}Ø�	˜%  uÑ-Ô-Ð-Ý=EÀnÑ=UÔ=UÐi�~�~ e¨T°5Ñ9Ô9Ð9Ð[i�Ø#×5Ò5°e¸XÑFÔF�	˜!‘�à%3°U¸1´XÐ"��i ‘lØØð bÝ*¨4°¸5À)ÈYÐX]ÔX`ÑaÔaÐaÐaÐaØ×(Ò(¨°	¸8ÀTÑJÔJÐJÐJÐJØ�˜˜5œ7Ñ#Ô#ˆØð 	'Ø�'˜!˜UœXÑ&Ô&ˆCØð 	ØˆG‰IŒIˆIØDLð  X��˜E 3¨¨t°ZÑ@Ô@Ð@ð  FNð  SXÐR]ÐR]Ð^cÐeiÐkpÐrvð  yAñ  SBô  SBð  SBð  TXˆØˆ=˜E !œHÐ,ØˆI˜u˜}�e�e°$¸¸UÑCÔCÐCØ�+˜c 1 e¤hÑ/Ô/ˆCÝ˜Ñ'Ô'ð 1Ø�˜t S¨%Ñ0Ô0Ð0Ø×1Ò1°3°%¸ÑDÔDˆI�a‰LˆLà  œ8ˆI�a‰LØØˆ=Ý9AÀ.Ñ9QÔ9QÐ~�>�> %¨¨uÑ5Ô5Ð5ÕWbÐcqÐstÐv{Ôv}ÑW~ÔW~ˆDØ×1Ò1°%¸ÑBÔBˆI�a‰LˆLå!,¨^¸QÀÄÑ!HÔ!HÈ%ÐPQÌ(ÐˆD�)˜A‘,ØØˆ>Ø—_’_ e¨V°U¸HÐ%EÑFÔFˆFØð 	iÝ  h°°s¸E¼G¼MÈ%Ì'Ì,Ð<VÐXYÐXYÐXYÐ7YÔ0ZÐ\eÐgpÐrwÔrzÑ{Ô{Ð{Ð{à×!Ò! &¨¨e¬g¬m¸E¼G¼LÐ.HÈ!È!È!Ð)KÔ"LÈiÐYaÐcgÑhÔhÐhØ‰EØ€Mr    r   r7   )T)NFTNNNNNFNNNN)4rÆ   ÚdataclassesÚcollectionsr   r]   r   Úmmgpr   r   Úshared.attentionr   r   r	   Úshared.kernelsr
   r   rZ   ÚImportErrorÚHEAD_SPLIT_CHOICESr?   rB   rI   r   r6  r   r   ri   rj   r[   r   ÚcompilerÚdisabler4   rF   rT   ra   ry   r“   r{   rˆ   r�   r£   r^   r¨   Ú	dataclassrª   rË   rí   rñ   rþ   r  rô   r8   r    r   ú<module>r\     s-  ððð ð* Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ð !Ð !à LÐ LÐ LÐ LÐ LÐ LÐ LÐ LÐ LÐ LØ #Ð #Ð #Ð #Ð #Ð #ðØ=Ð=Ð=Ð=Ð=Ð=Ð=øØð ð ð Ø€J€J€Jðøøøð Pð  Pð  PÐ Ø€ØÐ Ø€Ø€
Ø€ð ˆJ�{Ð$7Ñ8Ô8€	ð ˆ:�jÐ"EÑFÔF€Ø
€Ø€Ø€ðð ð ð
 „×ÒÑÔð=ð =ð =ñ Ôð=ðZð Zð Zð Zð	ð 	ð 	ð 	ðoð oð oðð ð ð €ðð ð ðð ð ð 8ð 8ð 8ð 8ð48ð 8ð 8ð %*¤Nð _ð _ð _ð _ð Ôð*-ð *-ð *-ð *-ð *-ñ *-ô *-ñ Ôð*-ðZDð Dð Dð Dð Dñ Dô Dð Dð@9ð 9ð 9ð(Gð Gð Gð
^ð ^ð ^ð ^ðQð Qð Qð „×ÒÑÔð AEð QUØ;?ðcð cð cñ Ôðcð cð cs   °7 ·AÁ A