class_docstr: Implements adaption prompts as described in https://arxiv.org/pdf/2303.16199.pdf.
  
  The top L attention modules are replaced with AdaptedAttention modules that wrap the original ones, but insert
  trainable prompts with gates (for zero init).
  
  Notes on the multi-adapter pattern:
  - We store the states of different adapters by keeping a dictionary of AdaptedAttention modules indexed by adapter
    name.
  - Every time we switch adapters, we remove the modules of the currently active adapter from the model, store them
    in the dictionary, and replace them with the modules of the new adapter.
  - To avoid duplicated and potentially inconsistent state, the currently active adapter is always removed from the
    dictionary.
  - Disabling the adapter would also result in the modules being removed from the model.
class_docstr_tok: ["Implements","adaption","prompts","as","described","in","https",":","\/\/arxiv.org\/pdf\/2303.16199.pdf",".","The","top","L","attention","modules","are","replaced","with","AdaptedAttention","modules","that","wrap","the","original","ones",",","but","insert","trainable","prompts","with","gates","(","for","zero","init",")",".","Notes","on","the","multi-adapter","pattern",":","-","We","store","the","states","of","different","adapters","by","keeping","a","dictionary","of","AdaptedAttention","modules","indexed","by","adapter","name",".","-","Every","time","we","switch","adapters",",","we","remove","the","modules","of","the","currently","active","adapter","from","the","model",",","store","them","in","the","dictionary",",","and","replace","them","with","the","modules","of","the","new","adapter",".","-","To","avoid","duplicated","and","potentially","inconsistent","state",",","the","currently","active","adapter","is","always","removed","from","the","dictionary",".","-","Disabling","the","adapter","would","also","result","in","the","modules","being","removed","from","the","model","."]
docstr: Add an adapter with the given name and config.
docstr_tok: ["Add","an","adapter","with","the","given","name","and","config","."]
code: def add_adapter(
      self, adapter_name: str, config: AdaptionPromptConfig
  ) -> None:
      """Add an adapter with the given name and config."""
      config = prepare_config(config, self.model)
      if adapter_name in self.peft_config:
          raise ValueError(
              f"Adapter with name '{adapter_name}' already exists."
          )
  
      parents = []
      for name, _ in self.model.named_modules():
          if name.endswith(config.target_modules):
              par, _, _ = _get_submodules(self.model, name)
              parents.append(par)
      if len(parents) < config.adapter_layers:
          raise ValueError(
              f"Config specifies more adapter layers '{config.adapter_layers}'"
              f" than the model has '{len(parents)}'."
          )
      # Note that if the target modules are not in Sequential, ModuleList, or
      # some other PyTorch ordered container, the behavior is undefined as we
      # assume here that the order of the modules is the same as the order of
      # the transformer decoder layers.
      parents = parents[-config.adapter_layers :]
      self._parents[adapter_name] = parents
  
      # It is only None during initialization.
      # If it is disabled, we don't have to remove the modules.
      if self._active_adapter is not None and self._enabled:
          self._remove_adapted_attentions(self._active_adapter)
      self._active_adapter = adapter_name
      self.peft_config[adapter_name] = config
      self._create_adapted_attentions(config, parents)
      if not self._enabled:
          self._remove_adapted_attentions(self._active_adapter)
  
      if config.inference_mode:
          _freeze_adapter(self.model, adapter_name)
  
file_path: peft/src/peft/tuners/adaption_prompt/model.py
imports: from typing import Dict, List
  import torch.nn
  from peft.utils import _freeze_adapter, _get_submodules
  from .config import AdaptionPromptConfig, prepare_config
  from .layer import AdaptedAttention
  from .utils import is_adaption_prompt_trainable
label_desc: Use image node_id 2 for calling the AdaptionPromptModel obj's underlying member method code with example usage: obj.add_adapter(adapter_name, config) without return types