ó
    >:j—'  ã                  óp   • S SK Jr  S SK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)	é    )ÚannotationsN)Ú	dataclassÚfield)ÚOptionalÚUnion)Ú
PeftConfig)ÚPeftTypec                  ó¶  ^ • \ 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&'   \" 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S00S9rS\S1'   U 4S2 jrS3rU =r$ )4ÚTinyLoraConfigé   a¢  
This is the configuration class to store the configuration of a [`TinyLoraModel`].

TinyLoRA is an extremely parameter-efficient fine-tuning method based on the paper "Learning to Reason in 13
Parameters" (arXiv:2602.04118). It uses SVD decomposition of frozen weights and projects a tiny trainable vector
through fixed random tensors.

Paper: https://arxiv.org/abs/2602.04118

Args:
    r (`int`, *optional*, defaults to `2`):
        SVD rank for the frozen U, Sigma, V decomposition. The paper recommends r=2.
    u (`int`, *optional*, defaults to `64`):
        Trainable vector dimension per group. This controls the expressivity of the adaptation. Can be as low as
        1-13 for extreme parameter efficiency.
    weight_tying (`float`, *optional*, defaults to `0.0`):
        Degree of weight tying across target modules, as a ratio between 0.0 and 1.0. Controls how many modules
        share the same trainable vector v. 0.0 means no sharing (each module has its own v). 1.0 means full sharing
        (all modules share one v). Values in between give partial sharing.
    projection_seed (`int`, *optional*, defaults to `42`):
        Random seed for generating the fixed projection matrices P.
    save_projection (`bool`, *optional*, defaults to `True`):
        Whether to save the projection tensors P in the state dict. If False, they will be regenerated from the
        seed when loading.
    init_v_bound (`float`, *optional*, defaults to `0.02`):
        Uniform initialization bound for the trainable vector v. Values are initialized in [-init_v_bound,
        init_v_bound].
    target_modules (`Union[List[str], str]`, *optional*):
        The names of the modules to apply TinyLoRA to. This can be a list of module names (e.g. `['q_proj',
        'v_proj']`), a regex pattern (e.g. `'.*decoder.*(q|v)_proj$'`), or the special keyword `"all-linear"` to
        target all linear modules. Only `nn.Linear`, `nn.Embedding`, and `transformers.pytorch_utils.Conv1D` layers
        are supported.
    tinylora_dropout (`float`, *optional*, defaults to `0.0`):
        The dropout probability for TinyLoRA layers.
    fan_in_fan_out (`bool`, *optional*, defaults to `False`):
        Set this to True if the layer to replace stores weight like (fan_in, fan_out). For example, gpt-2 uses
        `Conv1D` which stores weights like (fan_in, fan_out).
    bias (`str`, *optional*, defaults to `"none"`):
        Bias type for TinyLoRA. Can be 'none', 'all' or 'tinylora_only'.
    modules_to_save (`List[str]`, *optional*):
        List of modules apart from TinyLoRA layers to be set as trainable and saved.
    init_weights (`bool` | `Literal["uniform"]`, *optional*, defaults to `True`):
        How to initialize the trainable vector v. Passing `True` (default) initializes v to zeros, making the
        adapter a no-op (identity operation). Passing `"uniform"` initializes v with uniform random values in
        `[-init_v_bound, init_v_bound]`. Passing `False` leaves v uninitialized (for advanced use cases).
    layers_to_transform (`Union[List[int], int]`, *optional*):
        The layer indexes to transform. If specified, only these layers will be adapted.
    layers_pattern (`Optional[Union[List[str], str]]`, *optional*):
        The layer pattern name, used only if `layers_to_transform` is different from `None`.

Example:
    ```python
    from peft import get_peft_model, TinyLoraConfig

    config = TinyLoraConfig(
        r=2,  # SVD rank (paper recommends 2)
        u=64,  # Trainable vector dimension
        weight_tying=0.0,  # No weight tying (0.0 = none, 1.0 = full)
        target_modules=["q_proj", "v_proj"],
        projection_seed=42,
    )
    model = get_peft_model(base_model, config)
    ```
é   ÚhelpzTinyLoRA SVD rank (frozen))ÚdefaultÚmetadataÚintÚré@   z$Trainable vector dimension per groupÚuç        zzDegree of weight tying across target modules (0.0 to 1.0). 0.0 = no sharing, 1.0 = full sharing (all modules share one v).ÚfloatÚweight_tyingé*   z±Random seed for generating the fixed projection matrices P. Used for initialising projections for new models or when loading a checkpoint that did not include these projections.Úprojection_seedTzáWhether to save the projection tensors P in the state dict. If False, they will be regenerated from the seed when loading. Setting to True increases checkpoint size but guarantees reproducibility across system configurations.ÚboolÚsave_projectiong{®Gáz”?z+Uniform init bound for v in [-bound, bound]Úinit_v_boundNa  List of module names, regex expression, or the keyword 'all-linear' to replace with TinyLoRA. For example, ['q_proj', 'v_proj'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$'. Only nn.Linear, nn.Embedding, and transformers.pytorch_utils.Conv1D layers are supported.zOptional[Union[list[str], str]]Útarget_moduleszTinyLoRA dropoutÚtinylora_dropoutFzMSet this to True if the layer to replace stores weight like (fan_in, fan_out)Úfan_in_fan_outÚnonez?Bias type for TinyLoRA. Can be 'none', 'all' or 'tinylora_only'ÚstrÚbiasa  List of modules apart from TinyLoRA 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¾How to initialize the trainable vector v. True (default) initializes v to zeros, making the adapter a no-op. 'uniform' initializes v with uniform random values. False leaves v uninitialized.zUnion[bool, str]Úinit_weightszà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_patternc                ó€  >• [         TU ]  5         [        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                  (       d  [        R                  " S5        U R                  S::  a  [        SU R                   35      eU R                   S::  a  [        SU R                    35      eSU R"                  s=::  a  S::  d  O  [        SU R"                   35      eg )	NzQWhen `layers_pattern` is specified, `layers_to_transform` must also be specified.a  Specified to not save projection tensors P within the state dictionary. They will be restored using the PRNG key stored in `config.projection_seed`. Consider setting `config.save_projection` to `True` to guarantee restoring the checkpoint correctly on all system configurations.r   z?`r` should be a positive integer value but the value passed is z?`u` should be a positive integer value but the value passed is r   g      ð?zM`weight_tying` should be a float between 0.0 and 1.0 but the value passed is )ÚsuperÚ__post_init__r	   ÚTINYLORAÚ	peft_typeÚ
isinstancer   ÚlistÚsetr&   r%   Ú
ValueErrorr   ÚwarningsÚwarnr   r   r   )ÚselfÚ	__class__s    €ÚX/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/tinylora/config.pyr)   ÚTinyLoraConfig.__post_init__´   s  ø€ Ü‰ÑÔÜ!×*Ñ*ˆŒä(2°4×3FÑ3FÌ×(MÑ(MŒC�×#Ñ#Ô$ÐSW×SfÑSfð 	Ôð ×× t×'?×'?ÜÐpÓqÐqØ×#×#Ü�MŠMðjôð
 �6‰6�Q‹;ÜÐ^Ð_c×_eÑ_eÐ^fÐgÓhÐhØ�6‰6�Q‹;ÜÐ^Ð_c×_eÑ_eÐ^fÐgÓhÐhØ�t×(Ñ(Õ/¨CÕ/ÜØ_Ð`d×`qÑ`qÐ_rÐsóð ð 0ó    )r+   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   Ú__annotations__r   r   r   r   r   r   r   r   r"   r#   r$   r%   r&   r)   Ú__static_attributes__Ú__classcell__)r3   s   @r4   r   r      sÓ  ø‡ ñ?ñB ˜1¨Ð0LÐ'MÑN€A€sÓNÙ˜2¨Ð1WÐ(XÑY€A€sÓYÙØàðRð
ñ€L�%ó ñ !Øàðrð
ñ€O�Só ñ "ØàðKð
ñ	€O�Tó 	ñ  ¨¸Ð@mÐ7nÑo€L�%ÓoÙ6;Øàðlð
ñ	7€NÐ3ó 	ñ $¨C¸6ÐCUÐ:VÑWÐ�eÓWÙ ØØÐiÐjñ€N�Dó ñ Ø &Ð*kÐ!lñ€Dˆ#ó ñ ,1Øàðjð
ñ	,€OÐ(ó 	ñ &+Øàðuð
ñ&€LÐ"ó ñ <AØàð+ð
ñ	<ÐÐ8ó 	ñ 7<ØàðDð
ñ	7€NÐ3ó 	÷ó r6   r   )Ú
__future__r   r0   Údataclassesr   r   Útypingr   r   Úpeft.configr   Ú
peft.utilsr	   r   © r6   r4   Ú<module>rE      s7   ðõ #ã ß (ß "å "Ý ð ôq�Zó qó ñqr6   