
    bi                    p    d dl mZ d dlZd dlmZmZ d dlmZmZ d dl	m
Z
 d dlmZ e G d de
             Zy)	    )annotationsN)	dataclassfield)OptionalUnion)
PeftConfig)PeftTypec                      e Zd ZU dZ edddi      Zded<    eddd	i      Zd
ed<    edddi      Zded<    edddi      Z	ded<    edddi      Z
ded<    edddi      Zded<    edddi      Zded<    edddi      Zded <    eddd!i      Zd"ed#<    eddd$i      Zded%<    eddd&i      Zd'ed(<    eddd)i      Zd
ed*<    fd+Z xZS ),
VeraConfiga  
    This is the configuration class to store the configuration of a [`VeraModel`].

    Paper: https://huggingface.co/papers/2310.11454.

    Args:
        r (`int`, *optional*, defaults to `256`):
            VeRA parameter dimension ("rank"). Choose higher values than LoRA ranks here, since VeRA uses far fewer
            parameters than LoRA (see Table 1).
        target_modules (`Union[List[str], str]`):
            The names of the modules to apply Vera to. Only linear layers are supported.
        projection_prng_key (`int`):
            Vera PRNG init key. Used for initialising vera_A and vera_B for new models or when loading a checkpoint
            that did not include these projections. Defaults to `0`.
        save_projection (`bool`):
            Whether to save the vera_A / vera_B projections in the state dict alongside per layer lambda_b / lambda_d
            weights. This will increase the size of the checkpoint, but guarantee that we can reload the checkpoint on
            all system configurations. Defaults to `True`.
        vera_dropout (`float`):
            The dropout probability for Vera layers.
        d_initial (`float`, *optional*, defaults to `0.1`):
            Initial init value for `vera_lambda_d` vector used when initializing the VeRA parameters. Small values
            (<=0.1) are recommended (see Table 6c in the paper).
        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`.
        bias (`str`):
            Bias type for Vera. Can be 'none', 'all' or 'vera_only'. If 'all' or 'vera_only', the corresponding biases
            will be updated during training. Be aware that this means that, even when disabling the adapters, the model
            will not produce the same output as the base model would have without adaptation.
        modules_to_save (`List[str]`):
            List of modules apart from Vera layers to be set as trainable and saved in the final checkpoint.
        init_weights (`bool`):
            Whether to initialize the weights of the Vera layers with their default initialization. Don't change this
            setting, except if you know exactly what you're doing.
        layers_to_transform (`Union[List[int],int]`):
            The layer indexes to transform, if this argument is specified, it will apply the Vera transformations on
            the layer indexes that are specified in this list. If a single integer is passed, it will apply the Vera
            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'`.
       helpzVera attention dimension)defaultmetadataintrNzList of module names or regex expression of the module names to replace with Vera.For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$'. Only linear layers are supported.zOptional[Union[list[str], str]]target_modulesr   zVera PRNG init key. Used for initialising vera_A and vera_B for new models or when loading a checkpoint that did not include these projections.projection_prng_keyTzWhether to save the vera_A / vera_B projections in the state dict alongside per layer lambda_b / lambda_d weights. This will increase the size of the checkpoint, but guarantee that we can reload the checkpoint on all system configurations.boolsave_projectiong        zVera dropoutfloatvera_dropoutg?z Initial init value for d vector.	d_initialFzMSet this to True if the layer to replace stores weight like (fan_in, fan_out)fan_in_fan_outnonez7Bias type for Vera. Can be 'none', 'all' or 'vera_only'strbiasa  List of modules apart from Vera 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_savezWhether to initialize the weights of the Vera layers with their default initialization. Don't change this setting, except if you know exactly what you're doing.init_weightszThe 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.zOptional[Union[list[int], int]]layers_to_transformzThe 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                V   t         |           t        j                  | _        t        | j                  t              rt        | j                        n| j                  | _        | j                  r| j                  st        d      | j                  st        j                  d       y y )NzRWhen `layers_pattern` is specified, `layers_to_transform` must also be specified. a  Specified to not save vera_A and vera_B within the state dictionary, instead they will be restored using the PRNG key store in `config.projection_prng_key`. Consider setting `config.save_projection` to `True` to guarantee restoring the checkpoint correctly on all system configurations.)super__post_init__r	   VERA	peft_type
isinstancer   listsetr    r   
ValueErrorr   warningswarn)self	__class__s    R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/peft/tuners/vera/config.pyr#   zVeraConfig.__post_init__   s    !(243F3F(MC##$SWSfSf 	 t'?'?qrr##MMj $    )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   r   r   r   r   r   r   r    r#   __classcell__)r-   s   @r.   r   r      s   *X 3&2L)MNAsN6;4
	7N3 	  %E
   "?
	OT 	  v~6NOL%OSF<^3_`Iu` ijND  f8q/rsD#s+0k
	,O( 	 N
L$  <A,
	<8 	 7<D
	7N3 	 r/   r   )
__future__r   r*   dataclassesr   r   typingr   r   peft.configr   
peft.utilsr	   r    r/   r.   <module>r<      s7    #  ( " "  I I Ir/   