
    wi                      D    d dl mZmZ ddlmZmZmZ  G d d          ZdS )    )ListUnion   )MIN_PEFT_VERSIONcheck_peft_versionis_peft_availablec                   |    e Zd ZdZdZddeddfdZdeeee         f         ddfdZ	dd	Z
dd
Zdee         fdZdS )PeftAdapterMixina,  
    A class containing all functions for loading and using adapters weights that are supported in PEFT library. For
    more details about adapters and injecting them in a transformer-based model, check out the PEFT
    [documentation](https://huggingface.co/docs/peft/index).

    Install the latest version of PEFT, and use this mixin to:

    - Attach new adapters in the model.
    - Attach multiple adapters and iteratively activate/deactivate them.
    - Activate/deactivate all adapters from the model.
    - Get a list of the active adapters.
    Fdefaultadapter_namereturnNc                    t          t                     t                      st          d          ddlm}m} | j        sd| _        n|| j        v rt          d| d          t          ||          s t          dt          |           d	          d
|_         ||| |           |                     |           d
S )a  
        Adds a new adapter to the current model for training. If no adapter name is passed, a default name is assigned
        to the adapter to follow the convention of the PEFT library.

        If you are not familiar with adapters and PEFT methods, we invite you to read more about them in the PEFT
        [documentation](https://huggingface.co/docs/peft).

        Args:
            adapter_config (`[~peft.PeftConfig]`):
                The configuration of the adapter to add; supported adapters are non-prefix tuning and adaption prompt
                methods.
            adapter_name (`str`, *optional*, defaults to `"default"`):
                The name of the adapter to add. If no name is passed, a default name is assigned to the adapter.
        min_versionTPEFT is not available. Please install PEFT to use this function: `pip install peft`.r   )
PeftConfiginject_adapter_in_modelTzAdapter with name z- already exists. Please use a different name.z8adapter_config should be an instance of PeftConfig. Got z	 instead.N)r   r   r   ImportErrorpeftr   r   _hf_peft_config_loadedpeft_config
ValueError
isinstancetypebase_model_name_or_pathset_adapter)selfadapter_configr   r   r   s        f/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/loaders/peft.pyadd_adapterzPeftAdapterMixin.add_adapter$   s     	'78888 "" 	vtuuu<<<<<<<<* 	o*.D''T---m,mmmnnn.*55 	j4P^K_K_jjj   26.lCCC&&&&&    c           	         t          t                     | j        st          d          t	          |t
                    r|g}t          |          t          | j                  z
  }t          |          dk    rLt          dd	                    |           dt          | j                                                             ddlm} d}|                                 D ]v\  }}t	          ||          rat          |d	          r|                    |           n9t          |d	          s"t          |          d
k    rt          d          ||_        d}w|st          d          dS )a  
        Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters.

        If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
        [documentation](https://huggingface.co/docs/peft).

        Args:
            adapter_name (Union[str, List[str]])):
                The list of adapters to set or the adapter name in the case of a single adapter.
        r   0No adapter loaded. Please load an adapter first.r   z)Following adapter(s) could not be found: z, zV. Make sure you are passing the correct adapter name(s). current loaded adapters are: BaseTunerLayerFr      zYou are trying to set multiple adapters and you have a PEFT version that does not support multi-adapter inference. Please upgrade to the latest version of PEFT. `pip install -U peft` or `pip install -U git+https://github.com/huggingface/peft.git`TzhDid not succeeded in setting the adapter. Please make sure you are using a model that supports adapters.N)r   r   r   r   r   strsetr   lenjoinlistkeyspeft.tuners.tuners_utilsr%   named_moduleshasattrr   active_adapter)r   r   missingr%   _adapters_has_been_set_modules          r   r   zPeftAdapterMixin.set_adapterJ   s    	'78888* 	QOPPPlC(( 	*(>Ll##c$*:&;&;;w<<!QDIIg<N<N Q Q15d6F6K6K6M6M1N1NQ Q  
 	<;;;;;!&++-- 	. 	.IAv&.11 .6=11 	9&&|4444 77 9C<M<MQR<R<R$q  
 -9F))-&% 	z  	 	r!   c                    t          t                     | j        st          d          ddlm} |                                 D ]C\  }}t          ||          r.t          |d          r|	                    d           <d|_
        Dd	S )
a  
        Disable all adapters attached to the model and fallback to inference with the base model only.

        If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
        [documentation](https://huggingface.co/docs/peft).
        r   r#   r   r$   enable_adaptersFenabledTNr   r   r   r   r-   r%   r.   r   r/   r6   disable_adaptersr   r%   r3   r4   s       r   r:   z!PeftAdapterMixin.disable_adapters{   s     	'78888* 	QOPPP;;;;;;++-- 	3 	3IAv&.11 36#455 3**5*9999 /3F+	3 	3r!   c                    t          t                     | j        st          d          ddlm} |                                 D ]C\  }}t          ||          r.t          |d          r|	                    d           <d|_
        Dd	S )
aK  
        Enable adapters that are attached to the model. The model uses `self.active_adapters()` to retrieve the list of
        adapters to enable.

        If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
        [documentation](https://huggingface.co/docs/peft).
        r   r#   r   r$   r6   Tr7   FNr9   r;   s       r   r6   z PeftAdapterMixin.enable_adapters   s     	'78888* 	QOPPP;;;;;;++-- 	4 	4IAv&.11 46#455 4**4*8888 /4F+	4 	4r!   c                    t          t                     t                      st          d          | j        st          d          ddlm} |                                 D ]\  }}t          ||          r	|j
        c S dS )z
        Gets the current list of active adapters of the model.

        If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
        [documentation](https://huggingface.co/docs/peft).
        r   r   r#   r   r$   N)r   r   r   r   r   r   r-   r%   r.   r   r0   r;   s       r   active_adaptersz PeftAdapterMixin.active_adapters   s     	'78888 "" 	vtuuu* 	QOPPP;;;;;;++-- 	- 	-IAv&.11 -,,,,-	- 	-r!   )r   )r   N)__name__
__module____qualname____doc__r   r'   r    r   r   r   r:   r6   r>    r!   r   r
   r
      s          #$' $' $'D $' $' $' $'L/c49n(= /$ / / / /b3 3 3 3,4 4 4 4.-c - - - - - -r!   r
   N)typingr   r   utilsr   r   r   r
   rC   r!   r   <module>rF      sy            K K K K K K K K K Kg- g- g- g- g- g- g- g- g- g-r!   