§
    ’Þj  ã                   óÂ   — d Z ddlZddlmZ dadad„ Zd„ Zd„ Z G d„ dej	        ¦  «        Z
 G d	„ d
ej        j        ¦  «        Z G d„ d¦  «        Z e¦   «         Zd„ ZdS )a  Row views: the projections of a fused linear whose rows interleave them head by head, (heads, slots, head_dim, in_features), as
the qkv projection of some attention checkpoints. Splitting such a weight into separate projections at load copies all its rows into
private RAM; a row view computes its projection from the fused weight in place instead, with a kernel registered by the application
(register_kernel), so the fused weight stays in its checkpoint file and MMGP manages it as one parameter (sizes, pinning, transfers).

A split map entry declares this layout with "interleaved": True and "attention_head_dim"; load_model_data keeps such weights fused and
installs row views when the registered kernel supports them, and splits them into copies of their rows otherwise.é    N©Útree_mapc                 ó<   — | |ca addlm}  |t          ¦  «         dS )zÿrows computes columns [start, stop) (multiples of head_dim) of projection slot of an interleaved fused weight for x of shape
    (tokens, in_features); available(dtype, head_dim) tells at load whether it computes them as fast as separate weights would be.é   )Úregister_row_projectionN)Ú_kernelÚ
_availableÚoffloadr   Ú_ROW_VIEW_PROJECTION)ÚrowsÚ	availabler   s      ú6C:\Users\johnn\OneDrive\Documents\AI\mmgp\row_views.pyÚregister_kernelr      s:   € ð  	Ð€GˆZØ0Ð0Ð0Ð0Ð0Ð0ØÐÕ0Ñ1Ô1Ð1Ð1Ð1ó    c                 ó4   — t           d uot          | |¦  «        S ©N)r	   )ÚdtypeÚhead_dims     r   r   r      s   € Ý˜TÐ!ÐA¥j°¸Ñ&AÔ&AÐAr   c                 ó¦   —   | j         d||g| j        dd…         ¢R Ž dd…|f         j         dg| j        dd…         ¢R Ž                      ¦   «         S )z;The rows of projection slot, copied: a split without views.éÿÿÿÿr   N)ÚreshapeÚshapeÚ
contiguous)ÚtensorÚslotÚslotsr   s       r   Úgatherr      sj   € àRˆ>ˆ6Œ>˜"˜e XÐA°´¸Q¸R¸RÔ0@ÐAÐAÐAÀ!À!À!ÀTÀ'ÔJÔRÐSUÐiÐX^ÔXdÐefÐegÐegÔXhÐiÐiÐi×tÒtÑvÔvÐvr   c                   ód   — e Zd ZdZej        j        Zed„ ¦   «         Z	d„ Z
d„ Zedd„¦   «         ZdS )	ÚRowViewWeightz¸The weight of a row view as code inspecting linear modules sees it (shape, dtype, device); any operation on it works on a copy
    of the projection's rows (DoRA merges, for instance).c                 óŒ   — |j         j        }t          j                             | |j        |j        f|j        |j        ¬¦  «        S )N)r   Údevice)	ÚfusedÚweightÚtorchÚTensorÚ_make_wrapper_subclassÚout_featuresÚin_featuresr   r!   )ÚclsÚviewr"   s      r   Ú__new__zRowViewWeight.__new__'   sK   € à”
Ô!ˆÝŒ|×2Ò2°3¸Ô9JÈDÔL\Ð8]ÐejÔepÐy~ô  zFÐ2ñ  Gô  Gð  	Gr   c                 ó   — || _         d S r   )r*   )Úselfr*   s     r   Ú__init__zRowViewWeight.__init__,   s   € ØˆŒ	ˆ	ˆ	r   c           	      óv   — d| j         j        › d| j         j        › dt          | j        ¦  «        › d| j        › d�	S )NzRowViewWeight(projection ú of z, ú))r*   r   r   Útupler   r   ©r-   s    r   Ú__repr__zRowViewWeight.__repr__/   sD   € Øt¨4¬9¬>ÐtÐt¸t¼y¼ÐtÐtÕRWÐX\ÔXbÑRcÔRcÐtÐtÐgkÔgqÐtÐtÐtÐtr   © Nc                 óT   — d„ } |t          ||¦  «        i t          ||pi ¦  «        ¤ŽS )Nc                 ób   — t          | t          ¦  «        r| j                             ¦   «         n| S r   )Ú
isinstancer   r*   Úmaterialize)Úvalues    r   ú<lambda>z2RowViewWeight.__torch_dispatch__.<locals>.<lambda>4   s*   € Å
È5ÕR_Ñ@`Ô@`Ð$k E¤J×$:Ò$:Ñ$<Ô$<Ð$<Ðfk€ r   r   )r)   ÚfuncÚtypesÚargsÚkwargsr9   s         r   Ú__torch_dispatch__z RowViewWeight.__torch_dispatch__2   s8   € àkÐkˆØˆt•X˜k¨4Ñ0Ô0ÐXµH¸[È&È,ÐTVÑ4WÔ4WÐXÐXÐXr   )r5   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   Ú_CÚ_disabled_torch_function_implÚ__torch_function__Ústaticmethodr+   r.   r4   Úclassmethodr@   r5   r   r   r   r   "   sŒ   € € € € € ð=ð =àœÔ?ÐàðGð Gñ „\ðGðð ð ðuð uð uð ðYð Yð Yñ „[ðYð Yð Yr   r   c                   ó`   — e Zd ZdZdZd„ Zed„ ¦   «         Zed„ ¦   «         Zd„ Z	d„ Z
d„ Zd	„ Zd
S )ÚRowViewLinearzêProjection `slot` of a fused linear, computed in place from the fused weight: a linear module without parameters of its own (the
    fused module holds them), seen as a linear by LoRAs, hooks and row projections (offload.linear_rows).Tc                 óÆ   — t           j        j                             | ¦  «         || j        d<   |||c| _        | _        | _        |j        |j	        |z  c| _        | _	        d S )Nr"   )
r$   ÚnnÚModuler.   Ú__dict__r   r   r   r(   r'   )r-   r"   r   r   r   s        r   r.   zRowViewLinear.__init__=   s`   € ÝŒŒ× Ò  Ñ&Ô&Ð&Ø!&ˆŒ�gÑØ/3°U¸HÐ,ˆŒ	�4”:˜tœ}Ø.3Ô.?ÀÔASÐW\ÑA\Ð+ˆÔ˜$Ô+Ð+Ð+r   c                 ó    — t          | ¦  «        S r   )r   r3   s    r   r#   zRowViewLinear.weightC   s   € å˜TÑ"Ô"Ð"r   c                 ód   — | j         j        }|€d n t          || j        | j        | j        ¦  «        S r   )r"   Úbiasr   r   r   r   )r-   rR   s     r   rR   zRowViewLinear.biasG   s-   € àŒzŒˆØ�|ˆtˆt­°°d´iÀÄÈTÌ]Ñ)[Ô)[Ð[r   c                 óX   — t          | j        j        | j        | j        | j        ¦  «        S r   )r   r"   r#   r   r   r   r3   s    r   r9   zRowViewLinear.materializeL   s!   € Ý�d”jÔ'¨¬°D´JÀÄÑNÔNÐNr   c           
      ót   — t          || j        j        | j        j        | j        | j        | j        ||¦  «        S r   )r   r"   r#   rR   r   r   r   )r-   ÚxÚstartÚstops       r   r   zRowViewLinear.rowsO   s1   € Ý�q˜$œ*Ô+¨T¬Z¬_¸d¼iÈÌÐUYÔUbÐdiÐkoÑpÔpÐpr   c                 ó¶   —  |                       |                     d|j        d         ¦  «        d| j        ¦  «        j        g |j        d d…         ¢| j        ‘R Ž S )Nr   r   )r   r   r   r'   r*   )r-   rU   s     r   ÚforwardzRowViewLinear.forwardR   s[   € ØOˆt�yŠy˜Ÿš 2 q¤w¨r¤{Ñ3Ô3°Q¸Ô8IÑJÔJÔOÐqÐQRÔQXÐY\ÐZ\ÐY\ÔQ]ÐqÐ_cÔ_pÐqÐqÐqÐqr   c           
      óV   — d| j         › d| j        › d| j        › d| j        › d| j        › �
S )Nzprojection r0   z, in_features=z, out_features=z, head_dim=)r   r   r(   r'   r   r3   s    r   Ú
extra_reprzRowViewLinear.extra_reprU   sk   € ð U˜TœYð  Uð  U¨D¬Jð  Uð  UÀdÔFVð  Uð  UÐgkÔgxð  Uð  Uð  FJô  FSð  Uð  Uð  	Ur   N)rA   rB   rC   rD   Ú"_mm_requires_native_linear_forwardr.   Úpropertyr#   rR   r9   r   rY   r[   r5   r   r   rK   rK   8   s»   € € € € € ðmð mà)-Ð&ð]ð ]ð ]ð ð#ð #ñ „Xð#ð ð\ð \ñ „Xð\ðOð Oð Oðqð qð qðrð rð rðUð Uð Uð Uð Ur   rK   c                   ój   — e Zd ZdZed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         ZdS )Ú_RowViewProjectionzZoffload.linear_rows of row views: a range of a projection's output columns, from x itself.c                 ó`   — t          | t          ¦  «        o| j        j        j        |j        k    S r   )r8   rK   r"   r#   r   ©ÚmodulerU   s     r   Úsupportsz_RowViewProjection.supports[   s(   € å˜&¥-Ñ0Ô0ÐY°V´\Ô5HÔ5NÐRSÔRYÒ5YÐYr   c                 ó   — d S r   r5   )rb   s    r   Úkeyz_RowViewProjection.key_   ó   € àˆtr   c                 ó   — d S r   r5   ra   s     r   Úpreparez_RowViewProjection.preparec   rf   r   c                 ó0   — |                       |||¦  «        S r   )r   )rb   rU   rV   rW   s       r   r   z_RowViewProjection.rowsg   s   € à�{Š{˜1˜e TÑ*Ô*Ð*r   N)	rA   rB   rC   rD   rH   rc   re   rh   r   r5   r   r   r_   r_   Y   s‡   € € € € € ØdÐdØðZð Zñ „\ðZð ðð ñ „\ðð ðð ñ „\ðð ð+ð +ñ „\ð+ð +ð +r   r_   c                 ó¬  — |                      d¦  «        \  }}}|r|                      |¦  «        n| }||dz            |                     |dz   ¦  «        }	}t          j        d¦  «        5  t          j                             |j        d         |j        d         |	du|j        ¬¦  «        }
ddd¦  «         n# 1 swxY w Y   t          |||
¦  «         |d	         }t          |¦  «        D ]i\  }}|                      d¦  «        \  }}}t          |r|                     |¦  «        n||t          |
|t          |¦  «        |d
         ¦  «        ¦  «         ŒjdS )znKeeps the fused linear `base` (its weight in state_dict) and puts row views in place of its split projections.ú.z.weightz.biasÚmetar   r   N)rR   r   Úmapped_modulesÚattention_head_dim)Ú
rpartitionÚget_submoduleÚgetr$   r!   rM   ÚLinearr   r   ÚsetattrÚ	enumeraterK   Úlen)ÚmodelÚbaseÚinfoÚ
state_dictÚprefixÚ_ÚsuffixÚparentr#   rR   r"   Úmappedr   ÚnameÚownerÚleafs                   r   Úinstallr‚   o   sÈ  € àŸš¨Ñ,Ô,Ñ€FˆAˆvØ,2Ð=ˆU× Ò  Ñ(Ô(Ð(¸€FØ˜d YÑ.Ô/°·²ÀÀwÁÑ1OÔ1OˆD€FÝ	Œ�fÑ	Ô	ð mð mÝ”—’ ¤¨Q¤°´¸a´ÀtÐSWÐGWÐ_eÔ_k�ÑlÔlˆðmð mð mñ mô mð mð mð mð mð mð møøøð mð mð mð måˆF�F˜EÑ"Ô"Ð"ØÐ"Ô#€FÝ Ñ'Ô'ð Mð M‰
ˆˆdØŸš¨Ñ-Ô-‰ˆˆq�$Ý¨uÐ@�×$Ò$ UÑ+Ô+Ð+¸&À$ÍÐV[Ð]aÕcfÐgmÑcnÔcnÐptð  vJô  qKñ  ILô  ILñ  	Mô  	Mð  	Mð  	MðMð Ms   Á*AB7Â7B;Â>B;)rD   r$   Útorch.utils._pytreer   r   r	   r   r   r   r%   r   rM   rr   rK   r_   r   r‚   r5   r   r   ú<module>r„      s-  ððuð uð €€€Ø (Ð (Ð (Ð (Ð (Ð (à
€Ø€
ð2ð 2ð 2ðBð Bð Bðwð wð wð
Yð Yð Yð Yð Y�E”Lñ Yô Yð Yð,Uð Uð Uð Uð U�E”H”Oñ Uô Uð UðB+ð +ð +ð +ð +ñ +ô +ð +ð& *Ð)Ñ+Ô+Ð ðMð Mð Mð Mð Mr   