ó
    >:jD  ã                  ót   • S SK Jr  S SKrS SKJrJr  S SKJrJrJ	r	  S SK
Jr  S SKJr  \ " S S\5      5       rg)	é    )ÚannotationsN)Ú	dataclassÚfield)ÚLiteralÚOptionalÚUnion)Ú
PeftConfig)ÚPeftTypec                  óB  ^ • \ 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S.0S9rS\S/'   \" S	SS00S9rS1\S2'   \" S	SS30S9rS4\S5'   \" S$SS60S9rS\S7'   \" S	SS80S9rS9\S:'   \" S	SS;0S9rS\S<'   U 4S= jrS>rU =r$ )?ÚPsoftConfigé   uo  
Configuration for PSOFT (Efficient Orthogonal Fine-Tuning with Principal Subspace Adaptation).

PSOFT inserts an r*r orthogonal transformation R between low-rank matrices A and B, so the low-rank update is Î”W =
B @ (R-I) @ A. Only R (and optional tunable vectors) are trained; A and B are initialized with psoft_init
(SVD-based, row-orthogonal A) and frozen.

Args:
    r (`int`):
        Defaults to 32. PSOFT rank (r) controls the adapter capacity through an r*r transformation R. Smaller ranks
        32-128 are typically sufficient for simple tasks, More complex tasks may benefit from 64-256, increasing
        expressiveness at the cost of additional parameters and computation. See the paper for empirically
        validated settings: https://openreview.net/forum?id=FSHrinMArK.
    target_modules (`Optional[Union[List[str], str]]`):
        The names of the modules to apply the adapter to. If this is specified, only the modules with the specified
        names will be replaced. 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. If this is specified as 'all-linear', then all linear/Conv1D modules are chosen (if
        the model is a PreTrainedModel, the output layer excluded). If this is not specified, modules will be
        chosen according to the model architecture. If the architecture is not known, an error will be raised -- in
        this case, you should specify the target modules manually.
    exclude_modules (`Optional[Union[List[str], str]]`):
        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.
    psoft_alpha (`int`): Defaults to 32. It controls PSOFT scaling factor. Same semantics as LoRA alpha.
    psoft_dropout (`float`): Defaults to 0.0. Dropout for PSOFT path. Same semantics as LoRA dropout.
    fan_in_fan_out (`bool`):
        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) and hence this should be set to `True`.
    ab_svd_init (`Literal["psoft_init", "pissa_init"]`):
        Defaults to 'psoft_init'. Initialization strategy for A and B used to construct the principal subspace in
        PSOFT. 'psoft_init': SVD-based initialization with row-orthogonal A, ensuring strict orthogonality (PSOFT).
        'pissa_init': SVD-based initialization with symmetric A and B (standard PiSSA).
    psoft_svd (`Literal["full", "lowrank"]`):
        Defaults to 'full'. SVD backend for initialization: 'full' uses torch.linalg.svd; 'lowrank' uses
        torch.svd_lowrank.
    psoft_svd_lowrank_niter (`int`):
        Only used when psoft_svd='lowrank'. Defaults to 10. Number of power iterations used by torch.svd_lowrank
        when psoft_svd='lowrank'.
    psoft_orth (`bool`):
        Defaults to 'True'. If True, constrains R to be orthogonal via Cayley parameterization, preserving the
        geometric relationships among column of the pre-trained weight vectors. If False, R is a free matrix
        without orthogonality constraints.
    psoft_mag_b (`bool`):
        Defaults to 'True'. If True, learns a diagonal scaling vector on the 'output' side of R. Commonly paired
        with psoft_mag_a to increase task adaptability, with slight distortion to the pre-trained geometry.
    psoft_mag_a (`bool`):
        Defaults to 'True'. If True, learns a diagonal scaling vector on the 'input' side of R. Commonly paired
        with psoft_mag_b to increase task adaptability, with slight distortion to the pre-trained geometry.
    use_cayley_neumann (`bool`):
        Defaults to 'False'. Whether to use the Cayley-Neumann formulation of PSOFT or not. Set to True to improve
        computational efficiency but comes at costs of bigger approximation error for orthogonality.
    num_cayley_neumann_terms (`int`):
        Defaults to 5. Only used when use_cayley_neumann=True. Number of Cayley-Neumann terms to use. Higher number
        results in less approximation error for orthogonality.
    cayley_neumann_eps (`optional[float]`):
        Defaults to 'None'. Only used when use_cayley_neumann=True. Optional Frobenius-norm bound for the generator
        matrix Q in the Cayley-Neumann approximation. If None (default), no rescaling is applied. If set to a value
        in (0, 1) (e.g., 0.9), Q is rescaled whenever ||Q||_F exceeds the threshold to improve numerical stability.
        See https://spherelab.ai/oftv2/ for details.
    init_weights (`bool`):
        Defaults to 'True'. Whether to initialize the weights of the PSOFT layers with their default
        initialization. Don't change this setting, except if you know exactly what you're doing.
    modules_to_save (`List[str]`):
        List of modules apart from adapter layers to be set as trainable and saved in the final checkpoint.
    layers_to_transform (`Union[List[int], int]`):
        The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
        that are specified in this list. If a single integer is passed, it will apply the transformations on the
        layer at this index.
    layers_pattern (`Optional[Union[List[str], str]]`):
        The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
        `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
é    Úhelpah  PSOFT rank (r) controls the adapter capacity through an r*r transformation R. Smaller ranks 32-128 are typically sufficient for simple tasks, More complex tasks may benefit from 64-256, increasing expressiveness at the cost of additional parameters and computation. See the paper for empirically validated settings: https://openreview.net/forum?id=FSHrinMArK. )ÚdefaultÚmetadataÚintÚrNa÷  List of module names or regex expression of the module names to replace with PSOFT. For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$'. This can also be a wildcard 'all-linear' which matches all linear/Conv1D (if the model is a PreTrainedModel, the output layer excluded). If not specified, modules will be chosen according to the model architecture, If the architecture is not known, an error will be raised -- in this case, you should specify the target modules manually. zOptional[Union[list[str], str]]Útarget_moduleszTList of module names or regex expression of the module names to exclude from PSOFT. Úexclude_modulesz@It controls PSOFT scaling factor. Same semantics as LoRA alpha. Úpsoft_alphaç        z8Dropout for PSOFT path. Same semantics as LoRA dropout. ÚfloatÚpsoft_dropoutFzOSet this to True if the layer to replace stores weight like (fan_in, fan_out). ÚboolÚfan_in_fan_outÚ
psoft_initaN  Initialization strategy for A and B used to construct the principal subspace in PSOFT. - 'psoft_init': SVD-based initialization with row-orthogonal A (asymmetric A and B), ensuring strict orthogonality (PSOFT). - 'pissa_init': SVD-based initialization with symmetric A and B, without strict orthogonality constraint (standard PiSSA). z#Literal['psoft_init', 'pissa_init']Úab_svd_initÚfullz`SVD backend for initialization: 'full' uses torch.linalg.svd; 'lowrank' uses torch.svd_lowrank. zLiteral['full', 'lowrank']Ú	psoft_svdé
   zsNumber of power iterations used by torch.svd_lowrank when psoft_svd='lowrank'. Only used when psoft_svd='lowrank'. Úpsoft_svd_lowrank_niterTzÛIf True, constrains R to be orthogonal via Cayley parameterization, preserving the geometric relationships among column of the pre-trained weight vectors. If False, R is a free matrix without orthogonality constraints. Ú
psoft_orthz¹If True, learns a diagonal scaling vector on the 'output' side of R. Commonly paired with psoft_mag_a to increase task adaptability, with slight distortion to the pre-trained geometry. Úpsoft_mag_bz¸If True, learns a diagonal scaling vector on the 'input' side of R. Commonly paired with psoft_mag_b to increase task adaptability, with slight distortion to the pre-trained geometry. Úpsoft_mag_az³Whether to use the Cayley-Neumann Formulation of PSOFT or not. Set to True to improve computational efficiency but comes at costs of bigger approximation error for orthogonality. Úuse_cayley_neumanné   z“Number of Cayley-Neumann terms to use. Higher number results in less approximation error for orthogonality. Only used when use_cayley_neumann=True.Únum_cayley_neumann_termsa]  Optional Frobenius-norm bound for the generator matrix Q in the Cayley-Neumann approximation. Only used when use_cayley_neumann=True. If None (default), no rescaling is applied. If set to a value in (0, 1) (e.g., 0.9), Q is rescaled whenever ||Q||_F exceeds the threshold to improve numerical stability. See https://spherelab.ai/oftv2/ for details. zOptional[float]Úcayley_neumann_epsa  List of modules apart from PSOFT 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¢Whether to initialize the weights of the PSOFT layers with their default initialization. Don't change this setting, except if you know exactly what you're doing. Úinit_weightsa  The layer indexes to transform, is 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. This only works when target_modules is a list of str.zOptional[Union[list[int], int]]Úlayers_to_transforma$  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 only works when target_modules is a list of str. 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                  [        5      (       a  [        U R                  5      OU R                  U l	        [        U R                  [        5      (       a  U R                  b  [        S5      e[        U R                  [        5      (       a  U R                  b  [        S5      eU R                  (       a  U R                  (       d  [        S5      eU R                  S::  a  [        SU R                   S35      eSS1nU R                  U;  a&  [        S	[!        U5       S
U R                  < S35      eSS1nU R"                  U;  a&  [        S[!        U5       S
U R"                  < S35      eU R$                  S   R&                  nU R"                  S:w  aG  U R(                  U:w  a7  [*        R,                  " SU R"                  < SU R(                   S3[.        5        U R$                  S   R&                  nU R0                  (       aj  U R2                  S::  a  [        SU R2                   S35      eU R4                  b3  SU R4                  s=:  a  S:  d  O  [        SU R4                   S35      eg g U R2                  U:w  a)  [*        R,                  " SU R2                   S3[.        5        U R4                  b*  [*        R,                  " SU R4                   S3[.        5        g g )NzD`layers_to_transform` cannot be used when `target_modules` is a str.z?`layers_pattern` cannot be used when `target_modules` is a str.zRWhen `layers_pattern` is specified, `layers_to_transform` must also be specified. r   z$`r` must be a positive integer; got Ú.r   Ú
pissa_initz`ab_svd_init` must be one of z; got r   Úlowrankz`psoft_svd` must be one of r!   zQ`psoft_svd_lowrank_niter` is only used when `psoft_svd='lowrank'`. Got psoft_svd=z, so psoft_svd_lowrank_niter=z will be ignored.r'   z;`num_cayley_neumann_terms` must be a positive integer; got r   g      ð?z5`cayley_neumann_eps` must be in (0, 1) when set; got z„`num_cayley_neumann_terms` is only used when `use_cayley_neumann=True`. Since `use_cayley_neumann=False`, `num_cayley_neumann_terms=z` will be ignored.zx`cayley_neumann_eps` is only used when `use_cayley_neumann=True`. Since `use_cayley_neumann=False`, `cayley_neumann_eps=)ÚsuperÚ__post_init__r
   ÚPSOFTÚ	peft_typeÚ
isinstancer   ÚlistÚsetr   Ústrr+   Ú
ValueErrorr,   r   r   Úsortedr   Ú__dataclass_fields__r   r!   ÚwarningsÚwarnÚUserWarningr%   r'   r(   )ÚselfÚallowed_initsÚallowed_svd_backendsÚDEFAULT_LOW_RANK_NITERÚ DEFAULT_NUM_CAYLEY_NEUMANN_TERMSÚ	__class__s        €ÚU/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/psoft/config.pyr2   ÚPsoftConfig.__post_init__û   s   ø€ Ü‰ÑÔÜ!Ÿ™ˆŒä(2°4×3FÑ3FÌ×(MÑ(MŒC�×#Ñ#Ô$ÐSW×SfÑSfð 	Ôô *4°D×4HÑ4HÌ$×)OÑ)OŒC�×$Ñ$Ô%ÐUY×UiÑUið 	Ôô
 �d×)Ñ)¬3×/Ñ/°D×4LÑ4LÑ4XÜÐcÓdÐdô �d×)Ñ)¬3×/Ñ/°D×4GÑ4GÑ4SÜÐ^Ó_Ð_ð ×× t×'?×'?ÜÐqÓrÐrà�6‰6�Q‹;ÜÐCÀDÇFÁFÀ8È1ÐMÓNÐNà% |Ð4ˆØ×Ñ =Ó0ÜÐ<¼VÀMÓ=RÐ<SÐSYÐZ^×ZjÑZjÑYmÐmnÐoÓpÐpà &¨	Ð2ÐØ�>‰>Ð!5Ó5ÜÐ:¼6ÐBVÓ;WÐ:XÐX^Ð_c×_mÑ_mÑ^pÐpqÐrÓsÐsà!%×!:Ñ!:Ð;TÑ!U×!]Ñ!]ÐØ�>‰>˜YÓ&¨4×+GÑ+GÐKaÓ+aÜ�MŠMð!Ø!%§¡Ñ 2Ð2OØ×/Ñ/Ð0Ð0AðCô ô	ð ,0×+DÑ+DÐE_Ñ+`×+hÑ+hÐ(Ø×"×"Ø×,Ñ,°Ó1Ü ØQÐRV×RoÑRoÐQpÐpqÐróð ð ×&Ñ&Ñ2¸CÀ$×BYÑBYÕ<_Ð\_Õ<_Ü Ð#XÐY]×YpÑYpÐXqÐqrÐ!sÓtÐtð =`Ð2ð ×,Ñ,Ð0PÓPÜ—’ðSØSW×SpÑSpÐRqð  rDðEäôð
 ×&Ñ&Ñ2Ü—’ðMØMQ×MdÑMdÐLeÐewðyäõð 3ó    )r   r4   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   Ú__annotations__r   r   r   r   r   r   r   r!   r"   r#   r$   r%   r'   r(   r)   r*   r+   r,   r2   Ú__static_attributes__Ú__classcell__)rD   s   @rE   r   r      s´  ø‡ ñIñV Øàðqð
ñ
€A€só 
ñ 7<Øàðwð	
ñ7€NÐ3ó ñ 8=ØØÐpÐqñ8€OÐ4ó ñ Ø˜fÐ&hÐiñ€K�ó ñ !Ø˜vÐ'aÐbñ€M�5ó ñ !ØØÐkÐlñ€N�Dó ñ 8=ØàðNð
ñ	8€KÐ4ó 	ñ -2ØàÐvð
ñ-€IÐ)ó ñ $)Øàð  Jð
ñ$Ð˜Só ñ ØàðSð
ñ€J�ó ñ ØàðGð
ñ€K�ó ñ ØàðGð
ñ€K�ó ñ  %Øàð  Jð
ñ Ð˜ó ñ %*Øàð  jð
ñ%Ð˜có ñ +0Øàð@ð
ñ
+Ð˜ó 
ñ ,1Øàð{ð
ñ	,€OÐ(ó 	ñ Øàð\ð
ñ€L�$ó ñ <AØàðHð
ñ<ÐÐ8ó ñ 7<ØàðEð
ñ	7€NÐ3ó 	÷>ó >rG   r   )Ú
__future__r   r<   Údataclassesr   r   Útypingr   r   r   Úpeft.configr	   Ú
peft.utilsr
   r   © rG   rE   Ú<module>rV      s:   ðõ #ã ß (ß +Ñ +å "Ý ð ô_�*ó _ó ñ_rG   