
    bi                    l    d dl mZ d dlmZmZ d dlmZmZmZ d dl	m
Z
 d dlmZ e G d de
             Zy)	    )annotations)	dataclassfield)LiteralOptionalUnion)
PeftConfig)PeftTypec                  ~    e Zd ZU dZ edddd      Zded<    ed	d
di      Zded<    edddd      Zded<    edddd      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 ),
MissConfiga  
    This is the configuration class to store the configuration of a [`MiSSModel`].

    Args:
        r (`int`):
            The rank of MiSS across different layers. It is best to set 'r' to an even number; otherwise, the default
            initialization method will not work. The rank of MiSS corresponds to a low-rank decomposition along the
            in_features dimension.
        miss_dropout (`float`):
            The dropout probability for MiSS layers.
        mini_r (`int`):
            The rank of MiSS corresponds to a low-rank decomposition along the out_features dimension. When you set
            `init_weights=mini`, you need to set `mini_r`. Please make sure that `out_features` is divisible by
            `mini_r`.
        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 modules are chosen, excluding
            the output layer. 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.
        init_weights (bool | Literal["bat", "mini"]):
            Different initializations correspond to different MiSS variants. By default(balance), the most efficient
            and general method in MiSS will be used. 'bat': In this mode, you can enable nonlinear updates across
            different shards. 'mini': In this mode, you can set a smaller rank to use fewer trainable parameters, but
            it is recommended to keep `out_features % mini_r == 0`.
        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 (`str`):
            The layer pattern name, used only if `layers_to_transform` is different from `None`.
        modules_to_save (`List[str]`):
            List of modules apart from adapter layers to be set as trainable and saved in the final checkpoint.
    @   zYThe rank of MiSS corresponds to a low-rank decomposition along the in_features dimension.zdIt is best to set 'r' to an even number; otherwise, the default initialization method will not work.)helpnote)defaultmetadataintrg        r   zMiSS dropoutfloatmiss_dropout   zZThe rank of MiSS corresponds to a low-rank decomposition along the out_features dimension.zIt is recommended that mini_r be divisible by out_features. When mini_r == out_features, the mini method is equivalent to the default efficient MiSS.mini_rNzRList of module names or regex expression of the module names to replace with MiSS.zPFor example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$' )r   examplezOptional[Union[list[str], str]]target_moduleszRList of module names or regex expression of the module names to exclude from MiSS.exclude_modulesTzTrue -> MiSS balance; `bat` -> Bat; `mini` -> smaller rank and efficiencyWhether to initialize the weights of the MiSS layers with their default initialization. Don't change this setting, except if you know exactly what you're doing.zbool | Literal['bat', 'mini']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.zOptional[str]layers_patternnonez7Bias type for MiSS. Can be 'none', 'all' or 'MiSS_only'strbiasa  List of modules apart from MiSS 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_savec                   t         |           t        j                  | _        t        | j                  t              rt        | j                        n| j                  | _        t        | j                  t              rt        | j                        n| j                  | _	        t        | j                  t              r| j                  t        d      t        | j                  t              r| j                  t        d      y y )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.)super__post_init__r
   MISS	peft_type
isinstancer   listsetr   r   r   
ValueErrorr   )self	__class__s    R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/peft/tuners/miss/config.pyr$   zMissConfig.__post_init__}   s    !(243F3F(MC##$SWSfSf 	 *4D4H4H$)OC$$%UYUiUi 	 d))3/D4L4L4Xcdd d))3/D4G4G4S^__ 5T/    )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   r   r   r   r    r!   r$   __classcell__)r,   s   @r-   r   r      s^   'R oz
As   v~6NOL%Op l
FC  7<hi
7N3  8=no8O4  38N
	3L/ 	 <A  w
<8  %*  d
%NM  f8q/rsD#s+0 v
,O( ` `r.   r   N)
__future__r   dataclassesr   r   typingr   r   r   peft.configr	   
peft.utilsr
   r    r.   r-   <module>r;      s:    # ( + + "  s` s` s`r.   