ó
    >:j@  ã                  ór   • S SK Jr  S SKrS SKJr  S SKJrJr  S SKJr  SSK	J
r
  SSKJrJr   " S	 S
\5      rg)é    )ÚannotationsN)Únn)Ú	BaseTunerÚBaseTunerLayer)Ú2TRANSFORMERS_MODELS_TO_LILY_TARGET_MODULES_MAPPINGé   )Ú
LilyConfig)Ú	LilyLayerÚLinearc                  óP   • \ rS rSr% SrSrS\S'   \r\	r
S r\S 5       rSS jrS	rg
)Ú	LilyModelé   aj  
Creates a Low-Rank Interconnected Adaptation Across Layers (Lily) model from a pretrained transformers model.

The method is described in detail in https://arxiv.org/abs/2407.09946.

Args:
    model ([`torch.nn.Module`]): The model to be adapted.
    config ([`LilyConfig`]): The configuration of the Lily model.
    adapter_name (`str`): The name of the adapter, defaults to `"default"`.

Returns:
    `torch.nn.Module`: The Lily PEFT model.

**Attributes**:
    - **model** ([`~transformers.PreTrainedModel`]) -- The model to be adapted.
    - **peft_config** ([`LilyConfig`]): The configuration of the Lily model.
Úlily_ÚstrÚprefixc           	     ó`  • Uc  [        S5      e[        U[        5      (       aG  UR                  UUR                  UR
                  UR                  UR                  UR                  S9  gU R                  XU5      nX R                  ;  a  UR                  S5        U R                  XTXƒ5        g)z„
Create a new Lily layer with independent A and B for each layer. Sharing of A/B across layers is deferred to
_post_injection_hook.
NzCurrent Key shouldn't be `None`)ÚscalingÚstride_AÚnum_BÚinit_weightsF)Ú
ValueErrorÚ
isinstancer
   Úupdate_layerÚrr   r   r   r   Ú_create_new_moduleÚactive_adaptersÚrequires_grad_Ú_replace_module)	ÚselfÚlily_configÚadapter_nameÚtargetÚtarget_nameÚparentÚcurrent_keyÚoptional_kwargsÚ
new_modules	            ÚS/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/lily/model.pyÚ_create_and_replaceÚLilyModel._create_and_replace5   s¢   € ð ÑÜÐ>Ó?Ð?ä�fœi×(Ñ(Ø×ÑØØ—‘Ø#×+Ñ+Ø$×-Ñ-Ø!×'Ñ'Ø(×5Ñ5ð  ò ð ×0Ñ0°ÈFÓSˆJØ×#7Ñ#7Ó7Ø×)Ñ)¨%Ô0Ø× Ñ  °jÕIó    c           
     óV  • [        U[        5      (       a  UR                  5       nOUn[        U[        R                  R
                  5      (       aA  [        UUU R                  U R                  U R                  U R                  U R                  S9$ [        S[        U5       S35      e)N)r   r   r   r   r   z-Lily does not support target modules of type z yet.)r   r   Úget_base_layerÚtorchr   r   r   r   r   r   r   ÚNotImplementedErrorÚtype)r    r!   r"   Útarget_base_layers       r(   r   ÚLilyModel._create_new_moduleU   s˜   € ä�fœn×-Ñ-Ø &× 5Ñ 5Ó 7Ñà &ÐäÐ'¬¯©¯©×9Ñ9ÜØØØ—-‘-Ø#×+Ñ+Ø$×-Ñ-Ø!×'Ñ'Ø(×5Ñ5ñð ô "Ð$QÔRVÐWhÓRiÐQjÐjoÐ"pÓqÐqr+   c                óú  • UR                   n0 nUR                  5        HÒ  u  pg[        U[        5      (       d  M  X7R                  ;  a  M-  UR                  5       nUR                  R                  n	Sn
[        UR                  [        5      (       a  UR                  n
O-UR                   H  nUR                  U5      (       d  M  Un
M     U
c  M³  X©4nXÅ;  a  / X\'   X\   R                  U5        MÔ     UR                  5        Hs  u  u  pÙnUS   R                  U   n[        U5       HJ  u  nnUS:w  a  UUR                  U'   UU-  U-  nUU:w  d  M+  UU   R                  U   UR                  U'   ML     Mu     g)aC  
After all layers have been independently initialized, apply A/B sharing across layers.

A sharing: for each (target_module_suffix, weight_shape) group, consecutive blocks of `stride_A` layers share
the same A. The first layer in each block keeps its own A; subsequent layers in the block have their lily_A
replaced by the group leader's.

B sharing: all layers in the same (target_module_suffix, weight_shape) group share the B from the very first
layer in that group.

Both lily_A and lily_B are nn.Linear modules, so they move correctly with model.to(device) via standard
nn.Module parameter propagation.

Note: (target_module_suffix, weight_shape) is used as the group key rather than target_module_suffix alone, to
correctly handle architectures like UNet where the same target key can appear with different shapes across
layers.
Nr   )r   Únamed_modulesr   r
   Úlily_Ar-   ÚweightÚshapeÚtarget_modulesr   ÚendswithÚappendÚitemsÚlily_BÚ	enumerate)r   ÚmodelÚconfigr!   r   Útarget_to_layersÚkeyÚmoduleÚbaser7   Úmatched_suffixÚsuffixÚ	group_keyÚtarget_suffixÚlayersÚshared_BÚiÚlayerÚ	group_idxs                      r(   Ú_post_injection_hookÚLilyModel._post_injection_hooki   sq  € ð$ —?‘?ˆð
 KMÐà ×.Ñ.Ö0‰KˆCÜ˜f¤i×0Ñ0ÙØ§=¡=Ó0Ùà×(Ñ(Ó*ˆDØ—K‘K×%Ñ%ˆEð "ˆNÜ˜&×/Ñ/´×5Ñ5Ø!'×!6Ñ!6‘à$×3Ô3�FØ—|‘| F×+Ó+Ø)/šñ 4ð Ñ%áà'Ð/ˆIØÓ0Ø.0Ð Ñ+ØÑ'×.Ñ.¨vÖ6ñ3 1ð8 /?×.DÑ.DÖ.FÑ*Ñ"ˆ] Fà˜a‘y×'Ñ'¨Ñ5ˆHÜ% fÖ-‘��5Ø˜“6Ø19�E—L‘L Ñ.ð  (™]¨hÑ6�	Ø˜	•>Ø17¸	Ñ1B×1IÑ1IÈ,Ñ1W�E—L‘L Ó.ó .ò /Gr+   © N)r>   z	nn.Moduler?   r	   r!   r   ÚreturnÚNone)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__annotations__r
   Útuner_layer_clsr   Útarget_module_mappingr)   Ústaticmethodr   rM   Ú__static_attributes__rO   r+   r(   r   r      sB   ‡ ñð$ €FˆCÓØ€OØNÐòJð@ ñró ðr÷&@Xr+   r   )Ú
__future__r   r.   r   Úpeft.tuners.tuners_utilsr   r   Ú
peft.utilsr   r?   r	   rK   r
   r   r   rO   r+   r(   Ú<module>r_      s1   ðõ #ã Ý ÷õ Jå ß $ôKX�	õ KXr+   