ó
    >:j,:  ã                  óh   • S SK Jr  S SKJrJr  S SKJrJr  S SKJ	r	  S SK
Jr  \ " S S\	5      5       rg)	é    )Úannotations)Ú	dataclassÚfield)ÚOptionalÚUnion)Ú
PeftConfig)ÚPeftTypec                  óF  ^ • \ rS rSr% Sr\" SSS0S9rS\S'   \" S	SS
0S9rS\S'   \" SSS0S9r	S\S'   \" SSS0S9r
S\S'   \" SSS0S9rS\S'   \" SSS0S9rS\S'   \" SSS0S9rS\S'   \" SSS0S9rS\S'   \" SSS0S9rS\S '   \" S!SS"0S9rS#\S$'   U 4S% jrS&rU =r$ )'Ú
LilyConfigé   u£  
This is the configuration class to store the configuration of a [`LilyModel`].

Args:
    r (`int`):
        Lily's rank. Determines the inner hidden dimension of each adapter and the rank of the weight update `A @
        B`. In Lily, since the number of adapters is typically smaller than in LoRA, each adapter needs to carry
        more capacity, so it is recommended to use a larger `r` than in LoRA â€” typically `2x`, `3x`, or `4x` the
        LoRA rank you would normally use. The total number of trainable parameters scales with `r * (total_layers /
        stride_A + num_B)`, so increasing `r` while keeping `stride_A` large and `num_B` small is the recommended
        trade-off.
    stride_A (`int`):
        The number of consecutive layers that share one A adapter. For example, if `stride_A=4`, every 4 adjacent
        layers share the same A adapter, resulting in `total_layers / stride_A` distinct A adapters in total. The A
        adapter compresses the input into a low-rank representation of size `r`. `stride_A` should be no less than
        1. Suggested values: `2`, `3`, or `4` (i.e. sharing every 2, 3, or 4 layers). Keeping `stride_A` large
        (fewer distinct A adapters) and increasing `r` instead leads to better performance than the opposite
        trade-off (small `stride_A`, small `r`). Setting `stride_A=1` means every layer has its own A adapter.
        NOTE: the A sharing happens within each target (layers with the same target suffix). For example, if your
        target_modules are `['q_proj', 'v_proj']` and you set `stride_A=2`, then every 2 adjacent q_proj layers
        will share an A adapter, and every 2 adjacent v_proj layers will share another A adapter, but the q_proj
        and v_proj layers will not share A adapters with each other since they have different suffixes.
    num_B (`int`):
        The number of shared B adapters. Unlike A adapters (which are grouped by layer), all B adapters are shared
        globally across every layer. For each forward pass, a router computes a weighted combination of all `num_B`
        B adapters (using softmax-normalized weights) to produce a single combined B adapter, which then projects
        the low-rank representation back to the original dimension. It is recommended to set `num_B` to a similar
        order as `total_layers / stride_A`. Suggested values: `total_layers / 2`, `total_layers / 3`, or
        `total_layers / 4`. Similar to `stride_A`, prefer smaller `num_B` with larger `r` over larger `num_B` with
        smaller `r`. NOTE: to train the router, you need at least 2 B adapters (i.e. `num_B >= 2`), since the
        router learns to compute a weighted combination of the B adapters. NOTE: the B sharing happens within each
        target (layers with the same target suffix). For example, if your target_modules are `['q_proj', 'v_proj']`
        and you set `num_B=4`, then there will be 4 B adapters shared across all q_proj layers, and another 4 B
        adapters shared across all v_proj layers, but the q_proj and v_proj layers will not share B adapters with
        each other since they have different suffixes.
    target_modules (`Union[List[str], str]`, *optional*):
        The names of the modules to apply Lily to. Can be a list of module name strings (e.g. `['q_proj',
        'v_proj']`) or a regex pattern (e.g. `'.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$'`). If not
        specified, Lily will be applied to all supported linear layers.
    scaling (`float`):
        A scalar multiplier applied to the combined adapter output (`scaling * A @ combined_B`) before adding it to
        the frozen weight's forward pass. Unlike LoRA, Lily does not use an `alpha / r` formulation; instead,
        `scaling` is a direct multiplier. This design makes it straightforward to sweep over values on a log scale
        (e.g. `0.01`, `0.1`, `1.0`, `10.0`). The optimal value is task-dependent and should be treated as a
        hyperparameter. We recommend starting with `1.0`.
    modules_to_save (`List[str]`, *optional*):
        List of modules apart from Lily layers to be set as trainable and saved in the final checkpoint. For
        example, in Sequence Classification or Token Classification tasks, the final layer `classifier/score` are
        randomly initialized and as such need to be trainable and saved.
    exclude_modules (`Union[List[str], str]`, *optional*):
        The names of the modules to not apply the adapter. When passing a string, a regex match will be performed.
        When passing a list of strings, either an exact match will be performed or it is checked if the name of the
        module ends with any of the passed strings.
    layers_to_transform (`Union[list[int], int]`, *optional*):
        The layer indexes to transform, if this argument is specified, PEFT will transform only the layers indexes
        that are specified inside this list. If a single integer is passed, PEFT will transform only the layer at
        this index.
    layers_pattern (`Optional[Union[List[str], str]]`, *optional*):
        The layer pattern name, used only if `layers_to_transform` is different to None and if the layer pattern is
        not in the common layers pattern. This should target the `nn.ModuleList` of the model, which is often
        called `'layers'` or `'h'`.
    init_weights (`bool`):
        Whether to initialize Lily adapter weights using the default initialization scheme: A matrices are
        initialized with Kaiming uniform, and B matrices are initialized to zero, ensuring that the adapter output
        is zero at the start of training and does not disturb the pretrained model. It is strongly recommended to
        keep this as `True` unless you have a specific reason to change it.
é    Úhelpu  Lily's rank. Determines the inner hidden dimension of each adapter and the rank of the weight update `A @ B`. In Lily, since the number of adapters is typically smaller than in LoRA, each adapter needs to carry more capacity, so it is recommended to use a larger `r` than in LoRA â€” typically `2x`, `3x`, or `4x` the LoRA rank you would normally use. The total number of trainable parameters scales with `r * (total_layers / stride_A + num_B)`, so increasing `r` while keeping `stride_A` large and `num_B` small is the recommended trade-off.)ÚdefaultÚmetadataÚintÚré   a¿  The number of consecutive layers that share one A adapter. For example, if `stride_A=4`, every 4 adjacent layers share the same A adapter, resulting in `total_layers / stride_A` distinct A adapters in total.The A adapter compresses the input into a low-rank representation of size `r`. `stride_A` should be no less than 1.Suggested values: `2`, `3`, or `4` (i.e. sharing every 2, 3, or 4 layers). Keeping `stride_A` large (fewer distinct A adapters) and increasing `r` instead leads to better performance than the opposite trade-off (small `stride_A`, small `r`). Setting `stride_A=1` means every layer has its own A adapter.Note: A sharing happens within each target module type independently. For example, if `target_modules=['q_proj', 'v_proj']` and `stride_A=2`, then every 2 adjacent `q_proj` layers share one A adapter and every 2 adjacent `v_proj` layers share another A adapter, but `q_proj` and `v_proj` layers never share A adapters with each other.Ústride_Aé   ap  The number of shared B adapters. Unlike A adapters (which are grouped by layer), all B adapters are shared globally across every layer. For each forward pass, a router computes a weighted combination of all `num_B` B adapters (using softmax-normalized weights) to produce a single combined B adapter, which then projects the low-rank representation back to the original dimension. It is recommended to set `num_B` to a similar order as `total_layers / stride_A`. Suggested values: `total_layers / 2`, `total_layers / 3`, or `total_layers / 4`. Similar to `stride_A`, prefer smaller `num_B` with larger `r` over larger `num_B` with smaller `r`. NOTE: to train the router, you need at least 2 B adapters (i.e. `num_B >= 2`), since the router learns to compute a weighted combination of the B adapters.Note: B sharing happens within each target module type independently. For example, if `target_modules=['q_proj', 'v_proj']` and `num_B=4`, then there will be 4 B adapters shared across all `q_proj` layers and another 4 B adapters shared across all `v_proj` layers, but `q_proj` and `v_proj` layers never share B adapters with each other.Únum_Bg      ð?aÒ  A scalar multiplier applied to the combined adapter output (`scaling * A @ combined_B`) before adding it to the frozen weight's forward pass. Unlike LoRA, Lily does not use an `alpha / r` formulation; instead, `scaling` is a direct multiplier. This design makes it straightforward to sweep over values on a log scale (e.g. `0.01`, `0.1`, `1.0`, `10.0`). The optimal value is task-dependent and should be treated as a hyperparameter. We recommend starting with `1.0`.ÚfloatÚscalingNzêList of module names or regex expression of the module names to replace with Lily. For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$'. If not specified, Lily will be applied to all supported linear layers.zOptional[Union[list[str], str]]Útarget_modulesa"  List of module names or regex expression of the module names to exclude from Lily. When passing a string, a regex match will be performed. When passing a list of strings, either an exact match will be performed or it is checked if the name of the module ends with any of the passed strings.Úexclude_modulesa  List of modules apart from Lily layers to be set as trainable and saved in the final checkpoint. For example, in Sequence Classification or Token Classification tasks, the final layer `classifier/score` are randomly initialized and as such need to be trainable and saved.zOptional[list[str]]Úmodules_to_savezàThe layer indexes to transform, if this argument is specified, PEFT will transform only the layers indexes that are specified inside this list. If a single integer is passed, PEFT will transform only the layer at this index.zOptional[Union[list[int], int]]Úlayers_to_transformzíThe layer pattern name, used only if `layers_to_transform` is different to None and if the layer pattern is not in the common layers pattern. This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.Úlayers_patternTa{  Whether to initialize Lily adapter weights using the default initialization scheme: A matrices are initialized with Kaiming uniform, and B matrices are initialized to zero, ensuring that the adapter output is zero at the start of training and does not disturb the pretrained model. It is strongly recommended to keep this as `True` unless you have a specific reason to change it.ÚboolÚinit_weightsc                ó&  >• [         TU ]  5         [        R                  U l        [        U R                  [        5      (       a  [        U R                  5      OU R                  U l        [        U R                  [        5      (       a  [        U R                  5      OU R                  U l	        U R                  (       a  U R                  (       d  [        S5      eU R                  S:  a  [        S5      eU R                  S:  a  [        S5      eg )NzQWhen `layers_pattern` is specified, `layers_to_transform` must also be specified.r   z`stride_A` must be at least 1.r   z8`num_B` must be at least 2 for the router to be trained.)ÚsuperÚ__post_init__r	   ÚLILYÚ	peft_typeÚ
isinstancer   ÚlistÚsetr   r   r   Ú
ValueErrorr   r   )ÚselfÚ	__class__s    €ÚT/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/lily/config.pyr"   ÚLilyConfig.__post_init__ß   sÐ   ø€ Ü‰ÑÔÜ!Ÿ™ˆŒä(2°4×3FÑ3FÌ×(MÑ(MŒC�×#Ñ#Ô$ÐSW×SfÑSfð 	Ôô *4°D×4HÑ4HÌ$×)OÑ)OŒC�×$Ñ$Ô%ÐUY×UiÑUið 	Ôð ×× t×'?×'?ÜÐpÓqÐqØ�=‰=˜1ÓÜÐ=Ó>Ð>Ø�:‰:˜‹>ÜÐWÓXÐXð ó    )r   r$   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   Ú__annotations__r   r   r   r   r   r   r   r   r   r"   Ú__static_attributes__Ú__classcell__)r*   s   @r+   r   r      s‚  ø‡ ñBñH Øàðsð	
ñ€A€só ñ Øàð[ð
ñ€Hˆcó ñ( Øàðcð
ñ€Eˆ3ó ñ* ØàðDð	
ñ€GˆUó ñ 7<ØàðYð
ñ	7€NÐ3ó 	ñ 8=Øàð2ð
ñ
8€OÐ4ó 
ñ ,1Øàðzð
ñ	,€OÐ(ó 	ñ <AØàð+ð
ñ	<ÐÐ8ó 	ñ 7<Øàð=ð
ñ	7€NÐ3ó 	ñ Øàðeð
ñ
€L�$ó 
÷Yó Yr-   r   N)Ú
__future__r   Údataclassesr   r   Útypingr   r   Úpeft.configr   Ú
peft.utilsr	   r   © r-   r+   Ú<module>r<      s7   ðõ #ç (ß "å "Ý ð ôUY�ó UYó ñUYr-   